Merge branch 'master' of krull:webcc
[henge/webcc.git] / Makefile
1 ################################################################################
2 # Desc: Web Cross Compiler
3 # Author: Ken Grimes
4 # Date: 2016
5 ################################################################################
6 # This makefile manages a build environment targeting native platforms with gcc
7 # and web platforms with either emscripten or binaryen (js or wasm).
8 ################################################################################
9 # Each .c file is automatically compiled into an environment-dependent object
10 # file of compiler-specific format (.o for gcc, .bc for llvm/emcc/em++).
11 ################################################################################
12 # The default rule to be built if none specified
13 default: all
14 # Base directories
15 # webcc's root
16 ROOT_DIR := .
17 # source directory to find compilable language files in
18 SRC_DIR := $(ROOT_DIR)/src
19 # directory where files with 'main' functions live (can be in SRC_DIR)
20 DRIVER_DIR := $(SRC_DIR)/bin
21 # make configuration directory
22 CONF_DIR := $(ROOT_DIR)/.make
23 # Source languages handled by this build system. These languages are capable of
24 # being compiled to an intermediary format for binary output by one of the
25 # provided compilersfor each language
26 LANGS := c cpp go
27 # Source-to-source languages handled by this build system
28 SLANGS := y rl
29 # Language-specific compilers and flags passed in from environment
30 c_C := $(strip $(notdir $(CC)))
31 c_FLAGS := $(strip $(CFLAGS)) -I$(SRC_DIR)
32 c_LIBS := SDL2 wolfssl
33 c_SRCL := y
34 cpp_C := $(strip $(notdir $(CXX)))
35 cpp_FLAGS := $(strip $(CXXFLAGS)) $(c_FLAGS)
36 cpp_LIBS := $(c_LIBS)
37 go_C := gccgo
38 go_FLAGS := $(c_FLAGS)
39 # Source to source languages
40 # Bison
41 y_C := bison
42 y_FLAGS := -d
43 y_STEM := tab
44 y_TRG := c h
45 # Ragel
46 rl_C := ragel
47 rl_FLAGS := -C
48 rl_TRG := c
49 # Compiler-specific associations. Each compiler has a binary object suffix
50 # (OBJ), an archiver (AR), and an archiver object suffix (AROBJ). Each compiler
51 # may optionally have defined a linker (LD), and a binary output suffix (OUT).
52 cc_OBJ := o
53 cc_LD := $(LD)
54 cc_AR := $(AR)
55 cc_AROBJ := a
56 cc_DBG := gdb
57 $(cpp_C)_LD := $(cc_LD)
58 gcc_OBJ := $(cc_OBJ)
59 gcc_LD := $(cc_LD)
60 ngcc_AR := $(cc_AR)
61 gcc_AROBJ := $(cc_AROBJ)
62 gcc_DBG := $(cc_DBG)
63 emcc_OBJ := bc
64 emcc_AR := emar
65 emcc_AROBJ := $(emcc_OBJ) #emar is just a python script that reparses shit for emcc
66 emcc_OUT := .js
67 g++_OBJ := $(cc_OBJ)
68 em++_OBJ := $(emcc_OBJ)
69 em++_AR := $(emcc_AR)
70 em++_AROBJ := $(emcc_AROBJ)
71 em++_OUT := $(emcc_OUT)
72 gccgo_OBJ := o
73 gccgo_LD := $(cc_LD)
74 gccgo_AR := $(cc_AR)
75 gccgo_AROBJ := $(cc_AROBJ)
76 # Shell functions to determine what libraries can be linked by the compiler
77 cc_LDLIBS := $(shell ls /usr/lib | grep ".o" | sed -e 's@^.*lib\([_\+a-zA-Z0-9\-]*\)\..*@\1@g')
78 gcc_LDLIBS := $(cc_LDLIBS)
79 emcc_LDLIBS :=
80 g++_LDLIBS := $(cc_LDLIBS)
81 em++_LDLIBS :=
82 go_LDLIBS := $(cc_LDLIBS)
83 # location to build cross-compilation libraries in, not intended to be installed
84 # in the host system
85 LIB_DIR := $(ROOT_DIR)/lib$(shell uname -m)/$($(c_C)_OBJ)
86 LIBDL_DIR := $(LIB_DIR)/.cache
87 LIBINC_DIR := $(ROOT_DIR)/include
88 # The makefile MUST be capable of generating these directories and all of their
89 # contents. These directories are removed during the 'scrub' rule
90 MAKE_DIRS := $(LIB_DIR) $(LIBDL_DIR)
91 # Set up lib inclusions, and scan for built libs
92 c_FLAGS += -I$(LIBINC_DIR)
93 c_OBJ := $($(c_C)_OBJ)
94 # Modules are any directories other than 'DRIVER_MODULE' in 'SRC_DIR' and
95 # produce a single archive in 'SRC_DIR' containing all of the module's symbols
96 MODULES := $(patsubst $(SRC_DIR)/%/,%,$(filter-out $(DRIVER_DIR)/%,$(shell ls -d $(SRC_DIR)/*/)))
97
98 #AWK_REVERSE_SQUASH##############################################################
99 # The following awk program reverses the order of a list while also removing
100 # duplicate entries. The effect of this when run on the dependency tree is that
101 # it will remove duplicates occurring in reverse order, allowing the most deeply
102 # nested libraries to be built, and linked, first. ##############################
103 define AWK_REVERSE_SQUASH =
104 awk \
105 '
106 { for(i=NF;i>0;i--)
107 printf (!a[$$i]++) ? $$i FS : "";
108 i=split("",a);
109 print ""
110 }
111 '
112 #/AWK_REVERSE_SQUASH#############################################################
113 endef
114
115 #PREMAKE_SOURCE_COMPILER#########################################################
116 # Compile any source-to-source languages before executing the majority of make ##
117 #################################################################################
118 define PREMAKE_SOURCE_COMPILER =
119 # Find the sources for each source-to-source language
120 $(foreach slang,$(SLANGS),
121 $(eval SLANG_SRC := $(shell find $(SRC_DIR) -name "*.$(slang)" -not -name ".*"))
122 # Foreach target type in the source-to-source language, add this source's
123 # targets to the list of the current source-to-source language targets
124 $(eval undefine SLANG_TRG)
125 $(foreach trg,$($(slang)_TRG),
126 $(eval SLANG_TRG += $(SLANG_SRC:%.$(slang)=%.$(if $($(slang)_STEM),$($(slang)_STEM).)$(trg))))
127 # Stat the source file's last-modified time to the var SRC_TIME
128 $(foreach src,$(SLANG_SRC),
129 # Establish a command to run for compiling this file
130 $(eval SHELL_CMD := cd $(dir $(src)) && $($(slang)_C) $($(slang)_FLAGS) $(notdir $(src)))
131 # Evaluate missing targets
132 $(eval FOUND_TRG := $(shell find $(dir $(src)) -name "$(basename $(notdir $(src))).*" -not -name ".*"))
133 $(eval MISSING_TRG := $(filter-out $(FOUND_TRG), $(SLANG_TRG)))
134 # Check timings of existing files
135 $(eval SRC_TIME := $(shell stat -c %Y $(src)))
136 # For each of the targets created by this source language, evaluate the
137 # last-modified times of each potential target. If the file does not exist, set
138 # the time to '0', otherwise set it to the -c %Y result from statting the file.
139 $(foreach srctrg,$(filter $(basename $(src))%, $(SLANG_TRG)),
140 $(eval TRG_TIMES += $(if $(wildcard $(srctrg)),$(shell stat -c %Y $(srctrg)),0)==$(srctrg)))
141 # Sort TRG_TIMES, which will produce a list of oldest-first files and timestamps
142 # of the form [last-modified-deltatime]==[filename]
143 $(eval TRG_TIMES := $(shell echo $(sort $(TRG_TIMES)) | $(call AWK_REVERSE_SQUASH)))
144 # Evaluate the newest time from our ordered list by splitting the word up by its
145 # '==' connectors and looking at the first word
146 $(eval NEWEST_TRG_TIME := $(word 1,$(subst ==, ,$(TRG_TIMES))))
147 # Find the older of the two times (between SRC_TIME and NEWEST_TRG_TIME)
148 $(eval OLDER_TIME := $(firstword $(sort $(NEWEST_TRG_TIME) $(SRC_TIME))))
149 # If the older of the two times is the newest target time found, then we need to
150 # rebuild, but only if our build rule intends to actually make something. If it
151 # instead does not intend to actually make a target, drop a functional rule that
152 # could hypothetically make the target for debugging and analysis purposes
153 $(if $(MAKECMDGOALS),
154 $(eval BUILDGOALS := $(filter-out clean scrub purge uninstall,$(MAKECMDGOALS))),
155 $(eval BUILDGOALS := all))
156 $(if $(and $(BUILDGOALS),$(or $(MISSING_TRG), $(filter $(OLDER_TIME),$(NEWEST_TRG_TIME)))),
157 $(if $(findstring n,$(MAKEFLAGS)),
158 $(SLANG_TRG):
159 $(SHELL_CMD)
160 ,
161 $(info $(SHELL_CMD) $(eval $(shell $(SHELL_CMD)))))
162 ))
163 # Put these targets on the MAKE_TARGETS list to be removed during "clean",
164 # regardless of whether or not they were built just now.
165 $(eval MAKE_TARGETS += $(SLANG_TRG))
166 )
167 #/PREMAKE_SOURCE_COMPILER########################################################
168 endef
169 # Compile a source language to a language that compiles to binary, but only if
170 # our build rule intends to build some kind of target
171 $(eval $(call PREMAKE_SOURCE_COMPILER,))
172
173
174 # Backup the CVS password and CVSROOT environment vars in case we change them
175 $(if $(wildcard ~/.cvspass),\
176 $(eval CVSPASS_BAK := $(file <~/.cvspass)))
177 $(if $(CVSROOT),\
178 $(eval CVSROOT_BAK := $(CVSROOT)))
179
180 # A long function definition to grab deps from the source's compiler and route
181 # errors to /dev/null if the rule generator expresses bogus errors
182 define SRC_LANG_FLAGS_OPT_DEPS =
183 $(filter-out \ %:,$(shell $($2_C) $3 $4 -MG $1 2> /dev/null))
184 endef
185
186 #SRC_LANG_RULE###################################################################
187 # Given a source and a language, generate a rule to make the object. The second
188 # invocation of gcc per file lists only its local includes, and filters out
189 # anything already caught. Anything remaining should be considered to be
190 # available in the current working directory of the source file(i.e. if you
191 # '#include "something.h"' in 'mymodule/horseradish.c', you would expect to
192 # include 'mymodule/something.h'. this check lets that happen), otherwise it is
193 # some kind of user error #######################################################
194 define SRC_LANG_RULE =
195 $(if $($1),,$(eval $1 := t)\
196 $(eval MOD := $(filter $(MODULES),$(firstword $(subst /, ,$(subst $(SRC_DIR),,$(dir $1))))))\
197 $(eval FLG := $(MOD:%=-I$(SRC_DIR)/% ))\
198 $(if $(wildcard $1),
199 $(eval DEPS := $(filter-out \ %:,$(shell $($2_C) $(FLG) -M -MG $1)))\
200 $(eval DDEPS := $(filter $(DRIVER_DIR:$(SRC_DIR)/%=%/%),$(DEPS)))\
201 $(eval MDEPS := $(filter $(MODULES:%=%/%),$(DEPS)))\
202 $(eval DEPS := $(filter-out $(DDEPS) $(MDEPS),$(DEPS)))\
203 $(eval MDEPS := $(MDEPS:%=$(SRC_DIR)/%))\
204 $(eval DDEPS := $(DDEPS:%=$(SRC_DIR)/%))\
205 $(eval LOST := $(filter-out \ %: $(DRIVER_DIR:./%=%)/% $(MODULES:%=$(SRC_DIR:./%=%)/%/%),$(shell $($2_C) $(FLG) -MM -MG $1)))\
206 $(eval MDEPS += $(LOST:%=$(dir $1)%))\
207 $(eval MDEPS := $(shell echo $(MDEPS) | sed -e 's@[a-zA-Z0-9\-\+/]*\.\./include@$(LIBINC_DIR)@g'))\
208 $(eval ALLDEPS := $(DDEPS) $(MDEPS) $(DEPS)),\
209 $(error Cannot generate deps for: $1, file not found))\
210 $(eval SRC_OBJ := $(basename $1).$($2_OBJ))\
211 $(eval MAKE_TARGETS += $(SRC_OBJ))
212 # Assume that lost dependencies are in the folder with the source
213 $(foreach lost,$(LOST),
214 $(eval ALLDEPS := $(subst $(lost), $(dir $1)$(lost),$(ALLDEPS))))
215
216 # Find any deps that aren't built yet, which the compiler has flagged
217 # as missing, but which we know the expected location of libs for
218 # this language should have their includes in 'LIBINC_DIR'
219 $(eval BUILDDEPS := $(filter $($2_LIBS:%=%/%),$(ALLDEPS)))
220 $(eval ALLDEPS := $(filter-out $(BUILDDEPS),$(ALLDEPS)) $(BUILDDEPS:%=$(LIBINC_DIR)/%))
221 # Object for $1
222 $(SRC_OBJ): $(ALLDEPS)
223 $($2_C) $$($2_FLAGS) $(FLG) -c -o $$@ $1
224 $(if $($2_DBG),\
225 $(eval DBG_OBJ := $(dir $1).$($2_DBG)/$(basename $(notdir $1)).$($2_OBJ))\
226 $(if $(findstring $(dir $(DBG_OBJ)),$(MAKE_DIRS)),,\
227 $(eval MAKE_DIRS += $(dir $(DBG_OBJ))))
228 $(eval MAKE_TARGETS += $(DBG_OBJ))
229 # Object for $1 with $($2_DBG) symbols
230 $(DBG_OBJ): $(ALLDEPS) | $(dir $(DBG_OBJ))
231 $($2_C) $$($2_FLAGS) $(FLG) -Og -g$($2_DBG) -c -o $$@ $1
232 ))
233 #/SRC_LANG_RULE##################################################################
234 endef
235
236 #SRC_LANG_DRVRULE################################################################
237 # establish a build and link rule given a source driver and language ############
238 define SRC_LANG_DRVRULE =
239 $(eval DRIVER_NAME := $(basename $(notdir $1)))
240 $(eval DRIVER_TARG := $(1:$(SRC_DIR)/%.$2=$(ROOT_DIR)/%$($2_OUT)))
241 $(eval DRIVER_SOB := $(basename $1).$($2_OBJ))
242
243 # Generate a rule for the driver object
244 $(eval $(call SRC_LANG_RULE,$1,$2))
245
246 # Modules can be implicitly specified by a driver if its dependencies include
247 # ANY files from any module directory. The 'MDEPS' var is set by SRC_LANG_RULE
248 # and expresses the dependencies required to build the object file only.
249 $(foreach module_dep,$(MDEPS),
250 $(eval DRIVER_MODULES += $(firstword $(subst /, ,$(module_dep))))
251 )
252 $(eval DRIVER_MODULES := $(filter-out $(DRIVER_DIR) . ..,$(sort $(DRIVER_MODULES))))
253
254 # In the event that the driver developer does not wish to include a header to
255 # any internal modules, the developer may instead create a file with the same
256 # basename as the driver, but with the '.ld' suffix, which contains a space
257 # separated list of internal modules to link together during compilation
258 $(eval DRIVER_LDVALS := $(file <$(1:%.$2=%.ld)))
259 $(eval DRIVER_LFLAGS := $(filter -l%,$(DRIVER_LDVALS)))
260 $(eval DRIVER_MODULES += $(filter-out -l%,$(DRIVER_LDVALS)))
261
262 # List of module archives to link together during compilation
263 $(eval DRIVER_ARCHIVES := $(DRIVER_MODULES:%=$(SRC_DIR)/%.$($2_AROBJ)))
264 $(eval DRIVER_DEPS := $(DRIVER_SOB) $(DRIVER_ARCHIVES))
265 $(eval DRIVER_DBGARCHIVES := $(DRIVER_MODULES:%=$(SRC_DIR)/.$($2_DBG)/%.$($2_AROBJ)))
266 $(eval DRIVER_DBGDEPS := $(dir $(DRIVER_SOB)).$($2_DBG)/$(notdir $(DRIVER_SOB)))
267 $(eval DRIVER_DBGDEPS += $(DRIVER_DBGARCHIVES))
268
269 # If the compiler supports linking, distinguish static from dynamic links,
270 # otherwise set all libraries to static
271 $(if $($($2_C)_LD),\
272 $(eval STLIBS := $(filter-out $($($2_C)_LDLIBS),$($2_LIBS)))\
273 $(eval DLIBS := $(filter-out $(STLIBS),$($2_LIBS))),\
274 $(eval STLIBS := $($2_LIBS)))
275
276 # Directory setup
277 $(eval DRIVER_TARG_DIR := $(dir $(1:$(SRC_DIR)/%.$2=$(ROOT_DIR)/%)))
278 $(eval MAKE_DIRS += $(DRIVER_TARG_DIR))
279 $(if $($2_DBG),$(eval MAKE_DIRS += $(DRIVER_TARG_DIR).$($2_DBG)))
280
281 # Setup the sources for this object. A driver build rule does not include any
282 # compile steps at all, and so should not contain any sources that are,
283 # themselves, traditional "source files" like a language-compiled 'c' file. The
284 # driver depends on the existence of its equivalent object file (created through
285 # compilation), which is expected to contain a 'main' function (or entry for
286 # '_start'). This driver object is then simply linked to its libraries and
287 # module archives to create an executable binary in the output folder.
288 # ORDER MATTERS HERE, this is for the linker:
289 $(eval DRIVER_SRC := $(DRIVER_LFLAGS) $(DRIVER_DEPS))
290 $(eval DRIVER_DBGSRC := $(DRIVER_LFLAGS) $(DRIVER_DBGDEPS))
291 # Iterate through the list of libraries in our language and stack commands from
292 # left to right in the "sources" section of the LD command (or LD section of the
293 # commandline for the compiler), to preserve ordering and also ensure that it is
294 # linked correctly IN THAT ORDER (static vs dynamic).
295 $(foreach lib,$($2_LIBS),\
296 $(if $(findstring $(lib),$(STLIBS)),
297 # Evaluate static lib $(lib) and add it to the dependencies and sources of the rules we built
298 $(eval STATIC_SRC := $(LIB_DIR)/lib$(lib).$($2_AROBJ))\
299 $(eval STATIC_DBGSRC := $(LIB_DIR)/.$($2_DBG)/lib$(lib).$($2_AROBJ))\
300 $(eval DRIVER_SRC := $(STATIC_SRC) $(DRIVER_SRC))\
301 $(eval DRIVER_DBGSRC := $(STATIC_DBGSRC) $(DRIVER_DBGSRC))\
302 $(eval DRIVER_DEPS += $(STATIC_SRC))\
303 $(eval DRIVER_DBGDEPS += $(STATIC_DBGSRC)),
304 # Add dynamic lib to the sources with a link flag, in its correct ordering
305 $(eval DRIVER_SRC := -l$(lib) $(DRIVER_SRC))\
306 $(eval DRIVER_DBGSRC := -l$(lib) $(DRIVER_DBGSRC))\
307 ))
308
309 # Add the driver target to this language's targets for cleaning
310 $(eval SCRUB_TARGETS += $(DRIVER_TARG))
311 $(DRIVER_TARG): $(DRIVER_DEPS) | $(DRIVER_TARG_DIR)
312 $($2_C) $($2_FLAGS) $(DRIVER_SRC) -o $$@
313 # Output a rule for building with debug flags
314 $(eval DRIVER_DBGTARG := $(DRIVER_TARG_DIR)$(basename $(notdir $(DRIVER_TARG)))-d$($2_OUT))
315 $(eval SCRUB_TARGETS += $(DRIVER_DBGTARG))
316 $(eval MAKE_DIRS += $(DRIVER_TARG_DIR).$($2_DBG)/)
317 $(DRIVER_DBGTARG): $(DRIVER_DBGDEPS) | $(DRIVER_TARG_DIR)
318 $($2_C) $($2_FLAGS) $(DRIVER_DBGSRC) -o $$@
319 # Make a rule to run this driver after building
320 $(DRIVER_NAME)-run: $(DRIVER_TARG)
321 $(DRIVER_TARG)
322 $(DRIVER_NAME)-d: $(DRIVER_DBGTARG)
323
324 #/SRC_LANG_DRVRULE###############################################################
325 endef
326
327 #MODULE_ARCRULE##################################################################
328 # generate rule for turning an entire module's collection of binary objects into
329 # a single locally-linked (no external -L libs) object (for simplified linking
330 # modules as static libs).#######################################################
331 define MODULE_ARCRULE =
332 $(eval ARCDEPS := $(filter $(SRC_DIR)/$1/%.$(c_OBJ),$(foreach lang,$(LANGS),$($(lang)_MOD_TRG))))\
333 $(eval MAKE_TARGETS+= $1.$(c_AROBJ))\
334
335 $(SRC_DIR)/$1.$(c_AROBJ): $(ARCDEPS)
336 $(c_AR) cr $$@ $$^
337 $(if $(c_DBG),
338 $(eval undefine DBGARCDEPS)
339 $(foreach arcdep,$(ARCDEPS),$(eval DBGARCDEPS += $(dir $(arcdep)).$(c_DBG)/$(notdir $(arcdep))))
340 $(eval MAKE_TARGETS+= .$(c_DBG)/$1.$(c_AROBJ))\
341
342 $(SRC_DIR)/.$(c_DBG)/$1.$(c_AROBJ): $(DBGARCDEPS) | .$(c_DBG)/
343 $(c_AR) cr $$@ $$^
344 )
345 #/MODULE_ARCRULE#################################################################
346 endef
347
348 # LANG_LIB_PARENT_BUILDRULE######################################################
349 # define rules for creating unknown libraries in the local build environment for
350 # either binary or bytecode representation given a library name and, optionally,
351 # a parent lib that included it for error reporting #############################
352 define LANG_LIB_PARENT_BUILDRULE =
353 $(eval CONF_FLAG := --prefix=$(abspath $(LIB_DIR)).trash)
354 $(eval CONF_FLAG += --includedir=$(abspath $(LIBINC_DIR)))
355 # Setup installation rule steps for this lib
356 $(eval
357 BUILDSTEPS := $(if $(AUTOGEN),$(AUTOGEN) && )
358 BUILDSTEPS += $(if $(CONFIGURE),$(CONFIGURE) $(CONF_FLAG) --libdir=$(abspath $(LIB_DIR)) && )
359 $(if $(MKCMD),\
360 BUILDSTEPS += $(MKCMD),
361 $(error $2.mk requires a valid MKCMD definition))
362 $(if $(MKINSTALL),\
363 INSTALLCMD := $(MKINSTALL),\
364 $(error $2.mk requires a valid MKINSTALL definition))
365 CLEANCMD := $(MKCLEAN)
366 )
367 # Construct the download method, or error if there isn't a valid one
368 $(if $(GITADDR),$(eval LIBDL := git clone $(GITADDR) $2),\
369 $(if $(HGADDR),$(eval LIBDL := hg clone $(HGADDR) $2),\
370 $(if $(CVSADDR),\
371 $(eval LIBDL := export CVSROOT=$(CVSADDR))\
372 $(eval LIBDL += && echo '$(CVSPASS)' > ~/.cvspass)\
373 $(eval LIBDL += && cvs $($2_CVSGET))
374 $(eval LIBDL += && $(if $(CVSPASS_BAK),echo $(CVSPASS_BAK) > ~/.cvspass,rm ~/.cvspass)),\
375 $(if $(WEBADDR),$(eval LIBDL := wget -O $(WEBTARG) $(WEBADDR)),\
376 $(eval $(error No way to download $2 needed by $3 for $1 language ))))))
377 # '$2' Download Rule
378 $(LIBDL_DIR)/$2:
379 mkdir -p $$@
380 cd $(LIBDL_DIR) && $(LIBDL) $(if $(WEBINIT),&& $(WEBINIT))
381 # '$2' Make and install rule, and ensure it isn't marked "clean"
382 $(LIB_DIR)/lib$2.$($1_AROBJ): $(LIBDEPS:%=$(LIB_DIR)/lib%.$($1_AROBJ)) | $(LIBDL_DIR)/$2
383 @mkdir -p $(LIB_DIR)
384 cd $(LIBDL_DIR)/$2 && export LD_RUN_PATH=$(LIBLDPATH) && $(BUILDSTEPS)
385 cd $(LIBDL_DIR)/$2 && $(INSTALLCMD)
386 @rm -f $(LIBDL_DIR)/$2.clean
387
388 # '$2' Rule for building under debug mode
389 $(if $($1_DBG),\
390 $(eval LIBDBG_DIR := $(LIB_DIR)/.$($1_DBG))\
391 $(eval $(if $(findstring $(LIBDBG_DIR),$(MAKE_DIRS)),,MAKE_DIRS += $(LIBDBG_DIR)))\
392 $(eval LIBDBG_TARG := $(LIBDBG_DIR)/lib$2.$($1_AROBJ))\
393 $(eval LIBDBG_DEPS := $(LIBDEPS:%=$(LIB_DIR)/.$($1_DBG)/lib%.$($1_AROBJ)))\
394 $(eval DBGBUILDSTEPS := $(if $(AUTOGEN),$(if $(AUTOGENDBG),$(AUTOGENDBG),$(AUTOGEN)) && ))\
395 $(eval DBGBUILDSTEPS += $(if $(CONFIGURE),$(if $(CONFIGUREDBG),$(CONFIGUREDBG),$(CONFIGURE)) $(CONF_FLAG) --libdir=$(abspath $(LIB_DIR))/.$($1_DBG)))\
396 $(eval DBGBUILDSTEPS += $(if $(MKDBGCMD),$(MKDBGCDM),$(MKCMD)))\
397
398 # '$2' Make and install rule for debugging
399 $(LIBDBG_TARG): $(LIBDBG_DEPS) | $(LIBDL_DIR)
400 @mkdir -p $(LIBDBG_DIR)
401 cd $(LIBDL_DIR)/$2 && export LD_RUN_PATH=$(LIBLDPATH:$(LIB_DIR)%=$(LIBDBG_DIR)) && $(DBGBUILDSTEPS)
402 cd $(LIBDL_DIR)/$2 && $(INSTALLCMD)
403 @rm -f $(LIBDL_DIR)/$2.clean
404 )
405
406 # '$2' Clean rule - drop a marker in the directory so we don't bother cleaning
407 # things that are clean... by default there is no real way of tracking this in a
408 # makefile
409 clean_$2:
410 $$(if $$(wildcard $(LIBDL_DIR)/$2),$$(if $$(wildcard $(LIBDL_DIR)/$2.clean),,\
411 cd $(LIBDL_DIR)/$2 && $(CLEANCMD) && touch $(abspath $(LIBDL_DIR))/$2.clean))
412
413 #/LANG_LIB_PARENT_BUILDRULE######################################################
414 endef
415
416
417 #LANG_LIB_INIT###################################################################
418 # For every library in this language, we need to determine how, and if, to link
419 # its data. This could potentially be done recursively as libraries have
420 # dependencies upon each other. We call a recursive macro here to expand out
421 # all the libs for this language, and then flatten the tree by removing
422 # duplicates with shell commands. We can't use make's 'sort' function because
423 # we actually care about the ordering so that interdependent libraries can link
424 # back up the tree ##############################################################
425 define LANG_LIB_INIT =
426 # Initialize per-library data, then import data from an accompanying file that
427 # can overwrite these vars
428 $(eval
429 # Implicit defaults for lib.mk files
430 AUTOGEN := ./autogen.sh
431 AUTOGENDBG := ./autogen.sh --enable-debug
432 CONFIGURE := ./configure
433 MKCMD := make -k
434 MKINSTALL := make -k install
435 MKCLEAN := make clean
436 MKDBGCMD := make -k
437 LIBDEPS :=
438 LIBLANGS := c cpp go
439 LIBLDPATH := $(if $($($1_C)_LD),$(if $(LD_LIBRARY_PATH),$(LD_LIBRARY_PATH):,/usr/lib:)$(abspath $(LIB_DIR)))
440 # One of these must be defined in the .mk file imported
441 $(foreach var, GITADDR WEBADDR HGADDR CVSADDR,\
442 $(eval undefine $(var)))
443 # Other, optional, variables to clear
444 $(foreach var, WEBTARG WEBINIT CVSGET CVSPASS CONFIGUREDBG,
445 $(eval undefine $(var)))
446 )
447 # If there is a .mk file for this lib, import it. Otherwise, we'll try to link
448 # it later, unless there is no linker for the language's compiler.
449 $(if $(wildcard $(CONF_DIR)/$2.mk),$(eval $(file <$(CONF_DIR)/$2.mk)),\
450 $(if $($($1_C)_LD),\
451 $(warning No $2.mk for lib$2, attempting to link from the system),\
452 $(error No $2.mk for lib$2 and '$($1_C)' is not configured for linking)))\
453
454 # Add dependencies we found (potentially recursively, ignore repeats until later
455 # so we can preserve ordering information for interdependency build ordering)
456 $(if $(LIBDEPS),\
457 $(eval $1_LIBS += $(LIBDEPS)))
458 # languages that can link this library (typically any lang that shares the
459 # object format (ELF by default, but this is compiler-dependent (e.g. emcc uses
460 # llvm bitcode, which is just an intermediary representation of data that can
461 # link to anything else statically, or as an archive)))
462 $(if $(findstring $1,$(LIBLANGS)),,\
463 $(error $2.mk must specifically set LIBLANGS := $1 if it is compatible with $1))
464 # Mark this lib as available for all its other compatible languages. This list
465 # is assumed to be sorted later, so we will ignore repeats for speed
466 $(foreach lang,$(LIBLANGS),\
467 $(if $(findstring $2,$($(lang)_LIBS)),,\
468 $(eval $(lang)_LIBS += $2)))
469 # Generate a build rule for this lib, unless it's already built
470 $(if $(findstring $2,$(BUILTLIBS)),,\
471 $(eval $(call LANG_LIB_PARENT_BUILDRULE,$1,$2,$3))\
472 $(eval BUILTLIBS += $2))
473 # Recurse this function for every libdep encountered
474 $(foreach libdep,$(LIBDEPS),\
475 $(call LANG_LIB_INIT,$1,$(libdep),$2))
476 #/LANG_LIB_INIT#################################################################
477 endef
478
479 # Initialize data for supported lanaguages ######################################
480 define LANG_INIT =
481 $(eval
482 $1_OBJ := $($($1_C)_OBJ)
483 $1_OUT := $($($1_C)_OUT)
484 $1_AROBJ := $($($1_C)_AROBJ)
485 $1_SOURCES := $(shell find $(SRC_DIR) -name "*.$1" -not -name ".*")
486 $1_DBG := $($($1_C)_DBG)
487 $1_AR := $($($1_C)_AR)
488 )
489 # Find save language-specific driver and module sources and targets
490 $(eval $1_DRV_SRC := $(filter $(DRIVER_DIR)/%,$($1_SOURCES)))
491 $(eval $1_DRV_TRG := $(patsubst $(SRC_DIR)/%.$1,$(ROOT_DIR)/%$($1_OUT),$($1_DRV_SRC)))
492 $(eval $1_MOD_SRC := $(filter-out $(DRIVER_DIR)/%,$($1_SOURCES)))
493 $(eval $1_MOD_TRG := $(addsuffix .$($1_OBJ),$(basename $($1_MOD_SRC))))
494 # Add those sources and targets to a language-irreverant var
495 $(eval
496 DRV_SRC += $($1_DRV_SRC)
497 DRV_TRG += $($1_DRV_TRG)
498 MOD_SRC += $($1_MOD_SRC)
499 MOD_TRG += $($1_MOD_TRG)
500 )\
501 # First, initialize language-module, module, and language relative data. Then,
502 # filter out each source file from the module and generate Dependency Rules and
503 # Module Object Rules for each of them.
504 $(foreach module,$(MODULES),
505 # Evaluate the module sources for this language
506 $(eval $(module)_$1_SRC := $(filter $(SRC_DIR)/$(module)/%,$($1_MOD_SRC)))
507 # Evaluate module targets for this language
508 $(eval $(module)_$1_TRG := $($(module)_$1_SRC:%.$1=%.$($1_OBJ)))
509 # Add these language-specific sources and targets to the module's src/targs
510 $(eval
511 $(module)_SRC += $($(module)_$1_SRC)
512 $(module)_TRG += $($(module)_$1_TRG)
513 )
514 # For every source file, create a build rule for it for our language
515 $(foreach src,$(filter $(SRC_DIR)/$(module)/%,$($1_MOD_SRC)),\
516 $(eval $(call SRC_LANG_RULE,$(src),$1))\
517 ))
518 # For all the listed libraries, initialize the library, which will set up all of
519 # its local ($1_LIBS, etc) data
520 $(foreach lib,$($1_LIBS),\
521 $(eval $(call LANG_LIB_INIT,$1,$(lib),$1)))
522 # The initializer produces an ordered list of interlinked dependencies in groups
523 # by depth, so when reading backwards and compressing repeats we are left with a
524 # reliable build order for library interlinking. this awk program just looks
525 # through a list of space separated words backwards failing to print when it
526 # encounters a repeat
527 $1_LIBS := $(shell echo "$($1_LIBS)" | $(call AWK_REVERSE_SQUASH))
528 #/LANG_INIT######################################################################
529 endef
530
531 # The recursive library dependecy traversal constructs a tree ordered by nested
532 # dependency depth. when linking, this order is reversed.
533 # Initialize each language and look for its files
534 $(eval $(foreach lang,$(LANGS),\
535 $(eval $(call LANG_INIT,$(lang)))\
536 $(eval $(lang)_LIBS := $(shell echo $($(lang)_LIBS) | $(call AWK_REVERSE_SQUASH)))\
537 ))
538
539 # Create module object rules for each module
540 $(foreach module,$(MODULES),\
541 $(eval $(call MODULE_ARCRULE,$(module)))\
542 )
543 $(if $(c_DBG),$(eval MAKE_DIRS += .$(c_DBG)/))
544
545 # Create lang-specific rules for producing final (linked) targets
546 $(foreach lang,$(LANGS),\
547 $(foreach drvsrc,$($(lang)_DRV_SRC),\
548 $(eval $(call SRC_LANG_DRVRULE,$(drvsrc),$(lang)))\
549 ))
550
551 # Create driver rules for each driver we found, let users call make specifying
552 # the basename of the driver file, and just route to the correct linkrule we
553 # made in the last paragraph
554 DRV_FNAMES := $(notdir $(DRV_SRC))
555 .PHONY: all $(basename $(DRV_NAMES))
556 $(foreach fname,$(DRV_FNAMES),\
557 $(eval $(basename $(fname)): \
558 $(filter %/$(basename $(fname))$($(lastword $(subst ., ,$(fname))_OUT)),$(DRV_TRG)))\
559 )
560 all: $(basename $(DRV_FNAMES))
561 @echo Build Complete
562
563 # Rule to make any dirs that we're in charge of
564 $(sort $(MAKE_DIRS)):
565 @mkdir -p $@
566
567 # Cleaning rules.
568 # Clean destroys all object files and internal module archives.
569 CLEAN_TARGETS := $(sort $(foreach mtarg,$(MAKE_TARGETS),$(if $(wildcard $(mtarg)),$(mtarg))))
570 clean: $(BUILTLIBS:%=clean_%)
571 $(if $(CLEAN_TARGETS),rm $(CLEAN_TARGETS))
572
573 # Scrub destroys all distributable binaries
574 SCRUB_TARGETS := $(sort $(foreach starg,$(SCRUB_TARGETS),$(if $(wildcard $(starg)),$(starg))))
575 scrub: clean
576 $(if $(SCRUB_TARGETS),rm $(SCRUB_TARGETS))
577
578 # Purge destroys all distributables created by a distributable binary
579 # (e.g. assets)
580
581 # Uninstall destroys all objects and directories that cannot be recreated by
582 # this make file
583 uninstall: scrub
584 rm -Rf $(MAKE_DIRS)