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