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