This repository has been archived by the owner on Apr 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGNUmakefile
191 lines (181 loc) · 5.35 KB
/
GNUmakefile
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#
################################################################################
all ::
clean ::
clean-all : clean ; $(RM) settings.mk
.PHONY : all clean clean-all help configure
################################################################################
CONFIG_CHOICES = $(patsubst settings-%.mk.template,%,$(notdir $(wildcard configs/settings-*.mk.template)))
# generate shell commands to display config helps on config-XXXXX
define echo_config_choices
$(foreach i,$1,
@echo configure-$i
@echo " Configure for $i."
)
endef
################################################################################
configure :
@echo Select one of
@echo -------------
$(call echo_config_choices,$(CONFIG_CHOICES))
configure-% :
cp configs/settings-$*.mk.template settings.mk
help :
@echo Build Help
@echo ----------
@echo usage: make [goals...]
@echo
@echo make all
@echo " Builds everything. (default)"
@echo make clean
@echo " Cleans all objects and executables."
@echo make help
@echo " This help."
@echo make configure
@echo " Show configuration help."
$(call echo_config_choices,$(CONFIG_CHOICES))
################################################################################
-include settings.mk
ifneq ($(lastword $(MAKEFILE_LIST)),settings.mk)
.DEFAULT_GOAL := configure
endif
################################################################################
HOSTCC ?= $(CC)
HOSTLD ?= $(LD)
HOSTAS ?= $(AS)
################################################################################
ARFLAGS = rvU
GLOBAL_CFLAGS = $(CFLAGS)
GLOBAL_CPPFLAGS = $(CPPFLAGS)
GLOBAL_CXXFLAGS = $(CXXFLAGS)
################################################################################
# Detect OS if not set
ifeq ($(OS),)
OS := $(shell uname -s | tr A-Z a-z)
endif
ifeq ($(OS),Windows_NT)
OS := windows
UNIX :=
endif
ifeq ($(OS),linux)
UNIX = unix
endif
ifeq ($(OS),freebsd)
UNIX = unix
endif
################################################################################
# reset our variables between runs
define wipe
$(eval
SRCS :=
SRCS_$(OS) :=
SRCS_unix :=
SRCS_windows :=
OBJS :=
EXEC :=
USES :=
USES_$(OS) :=
USES_unix :=
USES_windows :=
CFLAGS :=
CFLAGS_$(OS) :=
CPPFLAGS :=
CPPFLAGS_$(OS) :=
CXXFLAGS :=
CXXFLAGS_$(OS) :=
LDFLAGS :=
LDFLAGS_$(OS) :=
LDLIBS :=
LDLIBS_$(OS) :=
PROVIDE_CFLAGS :=
PROVIDE_CFLAGS_$(OS) :=
PROVIDE_CPPFLAGS :=
PROVIDE_CPPFLAGS_$(OS) :=
PROVIDE_CXXFLAGS :=
PROVIDE_CXXFLAGS_$(OS) :=
PROVIDE_LDFLAGS :=
PROVIDE_LDFLAGS_$(OS) :=
PROVIDE_LDLIBS :=
PROVIDE_LDLIBS_$(OS) :=
TYPE :=
TAG :=
CUR :=
)
endef
# create rules if extension is set
ifneq ($(EXT),)
%$(EXT) : %.o
$(LINK.o) $^ $(LOADLIBES) $(LDLIBS) -o $@
%$(EXT) : %.c
$(LINK.c) $^ $(LOADLIBES) $(LDLIBS) -o $@
endif
# use an extra expansion to create a clean rule
clean = $(eval clean :: ; $$(RM) $1)
# load config
# $1 = path to project.cfg
define loadconfig
$(eval
# find the directory
CUR := $$(dir $1)
TAG := $$(patsubst %/,%,$$(CUR))
ifeq ($$(TAG),)
TAG := none
endif
$(info Using project: $1)
include $1
OBJS += $$(SRCS:.c=.o) $$(SRCS_$(OS):.c=.o) $(if $(UNIX),$$(SRCS_unix:.c=.o))
OBJS := $$(addprefix $$(CUR),$$(OBJS))
EXEC := $$(addprefix $$(CUR),$$(EXEC))
SRCS := $$(addprefix $$(CUR),$$(SRCS))
ifneq ($$(filter lib%,$$(TYPE)),)
# are we building a static library?
TYPE.$$(TAG) := library
PROVIDE_CFLAGS.$$(TAG) := $$(PROVIDE_CFLAGS) $$(PROVIDE_CFLAGS_$(OS))
PROVIDE_CXXFLAGS.$$(TAG) := $$(PROVIDE_CXXFLAGS) $$(PROVIDE_CXXFLAGS_$(OS))
PROVIDE_CPPFLAGS.$$(TAG) := $$(PROVIDE_CPPFLAGS) $$(PROVIDE_CPPFLAGS_$(OS))
PROVIDE_LDFLAGS.$$(TAG) := $$(PROVIDE_LDFLAGS) $$(PROVIDE_LDFLAGS_$(OS))
PROVIDE_LDLIBS.$$(TAG) := $$(PROVIDE_LDLIBS) $$(PROVIDE_LDLIBS_$(OS))
EXEC := $(addsuffix .a,$$(EXEC))
else
# we must be building a regular executable
TYPE.$$(TAG) := executable
EXEC := $(addsuffix $(EXT),$$(EXEC))
endif
# Copy all settings into our database
USES.$$(TAG) := $$(USES) $$(USES_$(OS))
EXEC.$$(TAG) := $$(EXEC)
OBJS.$$(TAG) := $$(OBJS)
SRCS.$$(TAG) := $$(SRCS) $$(SRCS_$(OS))
CFLAGS.$$(TAG) := $$(CFLAGS)
CXXFLAGS.$$(TAG) := $$(CXXFLAGS)
CPPFLAGS.$$(TAG) := $$(CPPFLAGS)
LDFLAGS.$$(TAG) := $$(LDFLAGS) $$(LDFLAGS_$(OS))
LDLIBS.$$(TAG) := $$(LDLIBS) $$(LDLIBS_$(OS))
)
endef
# generate rules to build an executable or library
# $1 = tag
define genrule
$(eval
all :: $$(EXEC.$1)
ifneq ($$(filter lib%,$$(TYPE.$1)),)
# are we building a static library?
$$(EXEC.$1) :: $$(EXEC.$1)($$(OBJS.$1))
else
# we must be building a regular executable
$$(EXEC.$1) :: $$(OBJS.$1) $$(foreach u,$$(USES.$1),$$(EXEC.$$u))
endif
$$(EXEC.$1) :: CFLAGS := $$(GLOBAL_CFLAGS) $$(foreach u,$$(USES.$1),$$(PROVIDE_CFLAGS.$$u)) $$(CFLAGS.$1)
$$(EXEC.$1) :: CXXFLAGS := $$(GLOBAL_CXXFLAGS) $$(foreach u,$$(USES.$1),$$(PROVIDE_CXXFLAGS.$$u)) $$(CXXFLAGS.$1)
$$(EXEC.$1) :: CPPFLAGS := $$(GLOBAL_CPPFLAGS) $$(foreach u,$$(USES.$1),$$(PROVIDE_CPPFLAGS.$$u)) -I. $$(CPPFLAGS.$1)
$$(EXEC.$1) :: LDFLAGS := $$(foreach u,$$(USES.$1),$$(PROVIDE_LDFLAGS.$$u)) $$(LDFLAGS.$1)
$$(EXEC.$1) :: LDLIBS := $$(foreach u,$$(USES.$1),$$(PROVIDE_LDLIBS.$$u)) $$(LDLIBS.$1)
)
endef
################################################################################
PROJECTS := $(wildcard */project.cfg project.cfg)
$(call wipe) # make sure nothing stale slips in...
# First phase: pull in config files
$(foreach d,$(PROJECTS),$(call loadconfig,$d) $(eval TARGET_TAGS += $(TAG)) $(call wipe))
# Second phase: generate rules
$(foreach t,$(TARGET_TAGS),$(call genrule,$t) $(call clean,$$(EXEC.$t) $$(OBJS.$t)))