summaryrefslogtreecommitdiffstats
path: root/makefile
blob: fff6a5a5ff9246121ab85a27a930594c415e8dc7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#version 5

out_name   := numper
build      := release
tmp_dir    := tmp
bin_dir    := bin
lib_dir    := lib
src_dir    := src

CFLAGS     := -std=c11 -fdata-sections -ffunction-sections -D_POSIX_C_SOURCE=200809L
CFLAGS     += -pedantic -Wall -Wextra -Wcast-qual -Wstrict-aliasing -Wpointer-arith -Wstrict-prototypes -Wmissing-prototypes -Wvla -Wno-parentheses -Wno-unused-variable -Wno-unused-function -Wno-unused-parameter -Wno-unused-label -Wno-format
LDFLAGS    := -Wl,--gc-sections
LDLIBS     := 
ARFLAGS    := -rcs

compile     = $(CC) $(CFLAGS) -MMD -c $< -o $@
link        = $(CC) $(LDFLAGS) $^ -o $@ $(LDLIBS)

ifeq ($(build),release)
  out_file := $(out_name)
else
  out_file := $(out_name)_$(build)
endif

bin        := $(bin_dir)/$(out_file)

ifneq ($(MSYSTEM),)
  bin      := $(bin).exe
  LDFLAGS  += -static
  #RELFLAGS := -s
else
  RELFLAGS := 
endif

out        := $(bin)

ifeq ($(build),debug)
  CFLAGS   += -g
else ifeq ($(build),release)
  CFLAGS   += -Ofast -march=native -mtune=native
  LDFLAGS  += $(RELFLAGS) 
else ifeq ($(build),analyze)
  CC       := clang
  CFLAGS   += --analyze -Xanalyzer -analyzer-output=text
  link     := 
else
  $(error invalid build: "$(build)")
endif

LDLIBS     := -lm $(LDLIBS)

####################################################################

ifneq ($(run),)
  run_cmd  := @echo "run     $(bin)"
  run_cmd  += && cd bin
  run_cmd  += && echo "----------------------------------------------------------------" 
  run_cmd  += && time ./$(out_file) -a6 100000000
  run_cmd  += ; echo "----------------------------------------------------------------"
  run_cmd  += && cd ..
endif

ifneq ($(debug),)
  CC       := gcc
  run_cmd  := @echo "debug   $(bin)"
  run_cmd  += && cd bin
  run_cmd  += && echo "----------------------------------------------------------------" 
  run_cmd  += && gdb $(out_file)
  run_cmd  += ; echo "----------------------------------------------------------------"
  run_cmd  += && cd ..
endif

ifeq ($(PREFIX),)
  PREFIX   := /usr/local
endif

ifeq ($(CC),clang)
  CFLAGS   += -fcolor-diagnostics -fansi-escape-codes
  LDFLAGS  := $(filter-out -pthread,$(LDFLAGS))
	LDLIBS   += -lpthread
endif

rwildcard   = $(wildcard $1$2) $(foreach d,$(wildcard $1*),$(call rwildcard,$d/,$2))

build_dir  := $(tmp_dir)/$(build)
src        := $(foreach dir,$(src_dir),$(call rwildcard,$(dir)/,*.c))
obj        := $(src:.c=.o)
obj        := $(addprefix $(build_dir)/,$(obj))
dep        := $(obj:.o=.d)
dst_dir    := $(DESTDIR)$(PREFIX)

####################################################################

.PHONY: all
all: $(out)
	@$(run_cmd)

$(out): $(obj)
	@echo "link    $@"
	@mkdir -p $(@D)
	@$(link)

-include $(dep)

$(build_dir)/%.o: %.c
	@echo "compile $<"
	@mkdir -p $(@D)
	@$(compile)

.PHONY: clean
clean:
	@-rm -rf $(tmp_dir)/*