summaryrefslogtreecommitdiffstats
path: root/makefile
blob: bf74c3439cc980fe0717d18b88ee12e525003210 (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#version static 5

out_name   := okeshell
build      := static64
inc_dir    := inc
tmp_dir    := tmp
bin_dir    := bin
src_dir    := src

CFLAGS     := -pthread -std=c11 -fdata-sections -ffunction-sections -I$(inc_dir)
CFLAGS     += -pedantic -Wall -Wextra -Wcast-qual -Wstrict-aliasing -Wpointer-arith -Wstrict-prototypes -Wmissing-prototypes -Wvla -Wno-parentheses
LDFLAGS    := -pthread 
LDLIBS     := -lm
ARFLAGS    := -rcs

compile     = $(cc_prefix) $(CC) $(CFLAGS) -MMD -c $< -o $@
dlink       = $(cc_prefix) $(CC) $(LDFLAGS) $^ -o $@ $(LDLIBS)
slink       = $(AR) $(ARFLAGS) $@ $^
link        = $(slink)

bin        := $(out_name)_$(build)
ifneq ($(MSYSTEM),)
  bin      := $(bin).exe
  LDLIBS   += 
endif

ifeq ($(build),static64)
  CFLAGS   += -O3 -DNDEBUG
  out      := lib$(out_name)64.a
  lib      := lib$(out_name).a
else ifeq ($(build),static32)
  CFLAGS   += -O3 -m32 -DNDEBUG
  out      := lib$(out_name)32.a
  lib      := lib$(out_name).a
else ifeq ($(build),static64d)
  CFLAGS   += -g
  out      := lib$(out_name)64d.a
  lib      := lib$(out_name)d.a
else ifeq ($(build),debug)
  src_dir  += test
  CFLAGS   += -g
  LDLIBS   += -lokeutild -lokelib3d
  CFLAGS   += -Wno-unused-variable -Wno-unused-function -Wno-unused-parameter -Wno-unused-label
  link      = $(dlink)
  out      := $(bin)
else ifeq ($(build),release)
  src_dir  += test
  CFLAGS   += -O3 -DNDEBUG
  LDLIBS   += -lokeutil -lokelib3
  link      = $(dlink)
  out      := $(bin)
else ifeq ($(build),analyze)
  src_dir  += test
  CC       := clang
  CFLAGS   += --analyze -Xanalyzer -analyzer-output=text
  link     := 
else
  $(error invalid build: "$(build)")
endif

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

out        := $(bin_dir)/$(out)

ifneq ($(run),)
  run_cmd  := @echo "run     $(out)"
  run_cmd  += && ./$(out)
endif

ifneq ($(debug),)
  CC       := gcc
  run_cmd  := @echo "debug   $(out)"
  run_cmd  += && gdb ./$(out)
  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)
	@rm -f $@
	@$(link)

-include $(dep)

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

.PHONY: install
install:
	@mkdir -p $(dst_dir)/lib
	@mkdir -p $(dst_dir)/include
	@printf "copy    %-24s -> %s\n" "$(out)" "$(dst_dir)/lib/$(lib)"
	@cp $(out) $(dst_dir)/lib/$(lib)
	@printf "copy    %-24s -> %s\n" "$(inc_dir)/*" "$(dst_dir)/include"
	@cp -R $(inc_dir)/* $(dst_dir)/include

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