-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMakefile.common
299 lines (238 loc) · 9.19 KB
/
Makefile.common
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#
# Common parts of GNU Make/GCC build system
# Copyright (C) 2022 by KO Myung-Hun <komh@chollian.net>
#
# This file is part of GNU Make/GCC build system.
#
# This program is free software. It comes without any warranty, to
# the extent permitted by applicable law. You can redistribute it
# and/or modify it under the terms of the Do What The Fuck You Want
# To Public License, Version 2, as published by Sam Hocevar. See
# http://www.wtfpl.net/ for more details.
#
##### Common parts that you don't have to modify
OBJ_EXT := .o
LIB_EXT := .a
OMF_LIB_EXT := .lib
EXE_EXT := .exe
DLL_EXT := .dll
.SUFFIXES : .def .exe .dll .a .lib .o .obj .res .c .cpp .h .rc .d $(OBJ_EXT) $(LIB_EXT) $(OMF_LIB_EXT) $(EXE_EXT) $(DLL_EXT)
# set compiler and linker
CC := gcc
CXX := g++
LD := g++
# always build with -Zomf
LDFLAGS += -Zomf
ifneq ($(filter wlink WLINK,$(EMXOMFLD_TYPE)),)
LDFLAGS += -Zlinker "DISABLE 1121"
endif
ifdef RELEASE
# remove symbols
LDFLAGS += -s
# pack the executable and DLLs
LXLITE := lxlite /B- /L- /CS
else
# enable symbolic debugging
CFLAGS += -g
CXXFLAGS += -g
LDFLAGS += -g
# ignore packing
LXLITE := echo ignore packing
endif
# set archiver
AR := ar
# default resource compiler is rc
ifeq ($(strip $(RC)),)
RC := rc
endif
# set mkdir -p
MKDIR_P := mkdir -p
# BLDLEVEL string
BLDLEVEL_DATE := $(shell LANG=C date +"\" %F %T %^Z \"")
BLDLEVEL_HOST = $(shell echo $(HOSTNAME) | cut -b -11)
ifeq ($(strip $(BLDLEVEL_VERSION)),)
ifneq ($(strip $(BLDLEVEL_VERSION_FILE)),)
BLDLEVEL_VERSION := $(shell sed -n -e "s/^[ \t]*\#[ \t]*define[ \t]\+$(BLDLEVEL_VERSION_MACRO)[ \t]\+\"\(.*\)\"/\1/p" $(BLDLEVEL_VERSION_FILE))
endif
endif
BLDLEVEL := @\#$(BLDLEVEL_VENDOR):$(BLDLEVEL_VERSION)\#@\#\#1\#\#$(BLDLEVEL_DATE)$(BLDLEVEL_HOST)::::::@@
# default verbose is quiet, that is V=0
QUIET_ = @
QUIET_0 = @
QUIET = $(QUIET_$(V))
# template to build the executable
define program_template
$(1)_DEPS := $(foreach s,$($(1)_SRCS),$(s:$(suffix $(s))=.d))
$$($(1)_DEPS) : CFLAGS += $($(1)_CFLAGS)
$$($(1)_DEPS) : CXXFLAGS += $($(1)_CXXFLAGS)
$(1)_OBJS := $$($(1)_DEPS:.d=$(OBJ_EXT))
$$($(1)_OBJS) : CFLAGS += $($(1)_CFLAGS)
$$($(1)_OBJS) : CXXFLAGS += $($(1)_CXXFLAGS)
$(1)_RES := $$($(1)_RCSRC:.rc=.res)
$(1)_DESC := $(if $($(1)_DESC),$($(1)_DESC),$(1))
$(1)_OUTDIR := $(if $($(1)_OUTDIR),$($(1)_OUTDIR),.)
# create a output directory
ifneq ($$($(1)_OUTDIR),.)
$$(shell $(MKDIR_P) $$($(1)_OUTDIR))
endif
$$($(1)_OUTDIR)/$(1)$(EXE_EXT) : $$($(1)_OBJS) $$($(1)_RES) $$($(1)_DEF) $$($(1)_EXTRADEPS)
$(if $(QUIET), @echo [LD] $$@)
$(QUIET)$(LD) $(LDFLAGS) $$($(1)_LDFLAGS) -o $$@ $$($(1)_OBJS) $$($(1)_DEF) $(LDLIBS) $$($(1)_LDLIBS)
ifneq ($$($(1)_RES),)
ifeq ($(strip $(NO_COMPRESS_RES)),)
$(if $(QUIET), @echo [BIND] $$($(1)_RES) to $$@)
$(QUIET)$(RC) $$($(1)_RES) $$@
endif
endif
$(if $(QUIET), @echo [LXLITE] $$@)
$(QUIET)$(LXLITE) $$@
ifneq ($$($(1)_RES),)
ifneq ($(strip $(NO_COMPRESS_RES)),)
$(if $(QUIET), @echo [BIND] $$($(1)_RES) to $$@)
$(QUIET)$(RC) $$($(1)_RES) $$@
endif
endif
$(if $(QUIET), @echo [BLDLEVEL] $$@)
$(QUIET)echo $(BLDLEVEL)$$($(1)_DESC) >> $$@
$(1) : $$($(1)_OUTDIR)/$(1)$(EXE_EXT)
endef
# template to create a static library
define lib_template
$(1)_DEPS := $(foreach s,$($(1)_SRCS),$(s:$(suffix $(s))=.d))
$$($(1)_DEPS) : CFLAGS += $($(1)_CFLAGS)
$$($(1)_DEPS) : CXXFLAGS += $($(1)_CXXFLAGS)
$(1)_OBJS := $$($(1)_DEPS:.d=$(OBJ_EXT))
$$($(1)_OBJS) : CFLAGS += $($(1)_CFLAGS)
$$($(1)_OBJS) : CXXFLAGS += $($(1)_CXXFLAGS)
$(1)_OUTDIR := $(if $($(1)_OUTDIR),$($(1)_OUTDIR),.)
# create a output directory
ifneq ($$($(1)_OUTDIR),.)
$$(shell $(MKDIR_P) $$($(1)_OUTDIR))
endif
$$($(1)_OUTDIR)/$(1)$(LIB_EXT) : $$($(1)_OBJS) $$($(1)_EXTRADEPS)
$(if $(QUIET), @echo [AR] $$@)
$(QUIET)$(AR) cr$(if $(QUIET),,v) $$@ $$($(1)_OBJS)
$$($(1)_OUTDIR)/$(1)$(OMF_LIB_EXT) : $$($(1)_OUTDIR)/$(1)$(LIB_EXT)
$(if $(QUIET), @echo [EMXOMF] $$@)
$(QUIET)emxomf -o $$@ $$($(1)_OUTDIR)/$(1)$(LIB_EXT)
$(1) : $$($(1)_OUTDIR)/$(1)$(LIB_EXT) $$($(1)_OUTDIR)/$(1)$(OMF_LIB_EXT)
endef
# template to build DLL
define dll_template
$(1)_DEPS := $(foreach s,$($(1)_SRCS),$(s:$(suffix $(s))=.d))
$$($(1)_DEPS) : CFLAGS += $($(1)_CFLAGS)
$$($(1)_DEPS) : CXXFLAGS += $($(1)_CXXFLAGS)
$(1)_OBJS := $$($(1)_DEPS:.d=$(OBJ_EXT))
$$($(1)_OBJS) : CFLAGS += $($(1)_CFLAGS)
$$($(1)_OBJS) : CXXFLAGS += $($(1)_CXXFLAGS)
$(1)_RES := $($(1)_RCSRC:.rc=.res)
$(1)_OUTDIR := $(if $($(1)_OUTDIR),$($(1)_OUTDIR),.)
$(1)_DEF := $(if $($(1)_DEF),$($(1)_DEF),$($(1)_OUTDIR)/$(1)_auto.def)
$(1)_DLL_TARGET := $(if $($(1)_DLLNAME),$($(1)_DLLNAME),$(1))$(DLL_EXT)
$(1)_DLL_LIBNAME := $$(notdir $$(basename $$($(1)_DLL_TARGET)))
$(1)_DESC := $(if $($(1)_DESC),$($(1)_DESC),$(1))
# create a output directory
ifneq ($$($(1)_OUTDIR),.)
$$(shell $(MKDIR_P) $$($(1)_OUTDIR))
endif
ifneq ($$(wildcard $(1)_auto.def),)
ifneq ($$(shell sed -n -e "1s/^LIBRARY[ \t]\+\([^ \t]\+\).*$$$$/\1/p" $$($(1)_DEF)),$$($(1)_DLL_LIBNAME))
$$(shell touch $$($(1)_OUTDIR)/$(1)_auto.libname)
endif
endif
$$($(1)_OUTDIR)/$(1)_auto.libname :
$(if $(QUIET), @echo [TOUCH] $$@)
$(QUIET)touch $$@
$$($(1)_OUTDIR)/$(1)_auto.def : $$($(1)_OBJS) $$($(1)_OUTDIR)/$(1)_auto.libname
$(if $(QUIET), @echo [GEN] $$@)
$(QUIET)echo LIBRARY $$($(1)_DLL_LIBNAME) INITINSTANCE TERMINSTANCE > $$@
$(QUIET)echo DATA MULTIPLE NONSHARED >> $$@
ifeq ($(strip $$($(1)_NO_EXPORT)),)
$(QUIET)echo EXPORTS >> $$@
$(QUIET)emxexp $$($(1)_OBJS) | sed /_DLL_InitTerm/d >> $$@
endif
$$($(1)_OUTDIR)/$$($(1)_DLL_TARGET) : $$($(1)_OBJS) $$($(1)_RES) $$($(1)_DEF) $$($(1)_EXTRADEPS)
$(if $(QUIET), @echo [LD] $$@)
$(QUIET)$(LD) -Zdll $(LDFLAGS) $$($(1)_LDFLAGS) -o $$@ $$($(1)_OBJS) $$($(1)_DEF) $(LDLIBS) $$($(1)_LDLIBS)
ifneq ($$($(1)_RES),)
ifeq ($(strip $(NO_COMPRESS_RES)),)
$(if $(QUIET), @echo [BIND] $$($(1)_RES) to $$@)
$(QUIET)$(RC) $$($(1)_RES) $$@
endif
endif
$(if $(QUIET), @echo [LXLITE] $$@)
$(QUIET)$(LXLITE) $$@
ifneq ($$($(1)_RES),)
ifneq ($(strip $(NO_COMPRESS_RES)),)
$(if $(QUIET), @echo [BIND] $$($(1)_RES) to $$@)
$(QUIET)$(RC) $$($(1)_RES) $$@
endif
endif
$(if $(QUIET), @echo [BLDLEVEL] $$@)
$(QUIET)echo $(BLDLEVEL)$$($(1)_DESC) >> $$@
$$($(1)_OUTDIR)/$(1)_dll$(LIB_EXT) : $$($(1)_OUTDIR)/$$($(1)_DLL_TARGET)
ifeq ($$($(1)_NO_IMPLIB),)
$(if $(QUIET), @echo [IMPLIB] $$@)
$(QUIET)emximp -o $$@ $$($(1)_OUTDIR)/$$($(1)_DLL_TARGET)
endif
$$($(1)_OUTDIR)/$(1)_dll$(OMF_LIB_EXT) : $$($(1)_OUTDIR)/$$($(1)_DLL_TARGET)
ifeq ($$($(1)_NO_IMPLIB),)
$(if $(QUIET), @echo [IMPLIB] $$@)
$(QUIET)emximp -o $$@ $$($(1)_OUTDIR)/$$($(1)_DLL_TARGET)
endif
$(1) : $$($(1)_OUTDIR)/$(1)_dll$(LIB_EXT) $$($(1)_OUTDIR)/$(1)_dll$(OMF_LIB_EXT)
endef
%.d : %.c
$(if $(QUIET), @echo [DEP] $@)
$(QUIET)$(CC) $(CFLAGS) -MM -MP -MT "$(@:.d=$(OBJ_EXT)) $@" -MF $@ $<
%.d : %.cpp
$(if $(QUIET), @echo [DEP] $@)
$(QUIET)$(CXX) $(CXXFLAGS) -MM -MP -MT "$(@:.d=$(OBJ_EXT)) $@" -MF $@ $<
%.o : %.c
$(if $(QUIET), @echo [CC] $@)
$(QUIET)$(CC) $(CFLAGS) -c -o $@ $<
%.obj : %.c
$(if $(QUIET), @echo [CC] $@)
$(QUIET)$(CC) -Zomf $(CFLAGS) -c -o $@ $<
%.o : %.cpp
$(if $(QUIET), @echo [CXX] $@)
$(QUIET)$(CXX) $(CXXFLAGS) -c -o $@ $<
%.obj : %.cpp
$(if $(QUIET), @echo [CXX] $@)
$(QUIET)$(CXX) -Zomf $(CXXFLAGS) -c -o $@ $<
%.res : %.rc
$(if $(QUIET), @echo [RC] $@)
$(QUIET)$(RC) $(RCFLAGS) -r $< $@
.PHONY : all $(BIN_PROGRAMS) $(BIN_LIBRARIES) clean $(SUBDIRS)
all : $(SUBDIRS) $(BIN_LIBRARIES) $(BIN_PROGRAMS)
# check if both library_LIB and library_DLL is not set
$(foreach lib,$(BIN_LIBRARIES),$(if $(strip $($(lib)_LIB)$($(lib)_DLL)),,$(error Either of $(lib)_LIB or $(lib)_DLL SHOULD be SET)))
LIB_ENABLED = $(foreach lib,$(1),$(if $($(lib)_LIB),$(lib)))
DLL_ENABLED = $(foreach dll,$(1),$(if $($(dll)_DLL),$(dll)))
$(foreach prog,$(BIN_PROGRAMS),$(eval $(call program_template,$(prog))))
$(foreach lib,$(call LIB_ENABLED,$(BIN_LIBRARIES)),$(eval $(call lib_template,$(lib))))
$(foreach dll,$(call DLL_ENABLED,$(BIN_LIBRARIES)),$(eval $(call dll_template,$(dll))))
clean : $(SUBDIRS)
rm -f $(foreach prog,$(BIN_PROGRAMS),$($(prog)_DEPS:.d=.bak))
rm -f $(foreach prog,$(BIN_PROGRAMS),$($(prog)_DEPS))
rm -f $(foreach prog,$(BIN_PROGRAMS),$($(prog)_OBJS))
rm -f $(foreach prog,$(BIN_PROGRAMS),$($(prog)_RES))
rm -f $(foreach prog,$(BIN_PROGRAMS),$(addsuffix $(EXE_EXT),$($(prog)_OUTDIR)/$(prog)))
rm -f $(foreach lib,$(BIN_LIBRARIES),$($(lib)_DEPS:.d=.bak))
rm -f $(foreach lib,$(BIN_LIBRARIES),$($(lib)_DEPS))
rm -f $(foreach lib,$(BIN_LIBRARIES),$($(lib)_OBJS))
rm -f $(foreach lib,$(BIN_LIBRARIES),$(addsuffix $(LIB_EXT),$($(lib)_OUTDIR)/$(lib)))
rm -f $(foreach lib,$(BIN_LIBRARIES),$(addsuffix $(OMF_LIB_EXT),$($(lib)_OUTDIR)/$(lib)))
rm -f $(foreach lib,$(BIN_LIBRARIES),$($(lib)_RES))
rm -f $(foreach lib,$(BIN_LIBRARIES),$($(lib)_OUTDIR)/$(lib)_auto.def)
rm -f $(foreach lib,$(BIN_LIBRARIES),$($(lib)_OUTDIR)/$(lib)_auto.libname)
rm -f $(foreach lib,$(BIN_LIBRARIES),$(addsuffix _dll$(LIB_EXT),$($(lib)_OUTDIR)/$(lib)))
rm -f $(foreach lib,$(BIN_LIBRARIES),$(addsuffix _dll$(OMF_LIB_EXT),$($(lib)_OUTDIR)/$(lib)))
rm -f $(foreach lib,$(BIN_LIBRARIES),$($(lib)_OUTDIR)/$($(lib)_DLL_TARGET))
rm -f $(CLEAN-FILES)
$(SUBDIRS) :
$(MAKE) -C $@ RELEASE=$(RELEASE) $(MAKECMDGOALS)
ifeq ($(filter %clean, $(MAKECMDGOALS)),)
-include $(foreach prog,$(BIN_PROGRAMS),$($(prog)_DEPS))
-include $(foreach lib,$(BIN_LIBRARIES),$($(lib)_DEPS))
endif