1 ################################################################################
2 # Desc: Mihrtec Standard Transpilation Makefile
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 ################################################################################
13 # Source languages handled by this build system
15 # Source-to-source languages handled by this build system
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
22 cpp_C
:= $(strip $(notdir $(CXX
)))
23 cpp_FLAGS
:= $(strip $(CXXFLAGS
)) $(c_FLAGS
)
26 go_FLAGS
:= $(c_FLAGS
)
27 # Source to source languages
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).
45 $(cpp_C
)_LD
:= $(cc_LD
)
49 gcc_AROBJ
:= $(cc_AROBJ
)
53 emcc_AROBJ
:= $(emcc_OBJ
) #emar is just a python script that reparses shit for emcc
56 em
++_OBJ
:= $(emcc_OBJ
)
58 em
++_AROBJ
:= $(emcc_AROBJ
)
59 em
++_OUT
:= $(emcc_OUT
)
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
)
68 g
++_LDLIBS
:= $(cc_LDLIBS
)
70 go_LDLIBS
:= $(cc_LDLIBS
)
72 # The directory in './' containing app drivers and compilation info
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
87 MODULES
:= $(filter-out $(DRIVER_DIR
),$(subst /,,$(shell ls
-d
*/)))
89 #AWK_REVERSE_SQUASH##############################################################
90 # The following awk program reverses the order of a list while also removing
91 # duplicate entries. The effect of this when run on the dependency tree is that
92 # it will remove duplicates occurring in reverse order, allowing the most deeply
93 # nested libraries to be built, and linked, first. ##############################
94 define AWK_REVERSE_SQUASH
=
98 printf (!a[$$i]++) ? $$i FS : "";
103 #/AWK_REVERSE_SQUASH#############################################################
106 #PREMAKE_SOURCE_COMPILER#########################################################
107 # Compile any source-to-source languages before executing the majority of make ##
108 #################################################################################
109 define PREMAKE_SOURCE_COMPILER
=
110 # Find the sources for each source-to-source language
111 $(foreach slang
,$(SLANGS
),
112 $(eval SLANG_SRC
:= $(patsubst .
/%, %,$(shell find
-name
"*.$(slang)" -not
-name
".*")))
113 # Foreach target type in the source-to-source language, add this source's
114 # targets to the list of the current source-to-source language targets
115 $(eval undefine SLANG_TRG
)
116 $(foreach trg
,$($(slang
)_TRG
),
117 $(eval SLANG_TRG
+= $(SLANG_SRC
:%.
$(slang
)=%.
$(if
$($(slang
)_STEM
),$($(slang
)_STEM
).
)$(trg
))))
118 # Stat the source file's last-modified time to the var SRC_TIME
119 $(foreach src
,$(SLANG_SRC
),
120 # Establish a command to run for compiling this file
121 $(eval SHELL_CMD
:= cd
$(dir $(src
)) && $($(slang
)_C
) $($(slang
)_FLAGS
) $(notdir $(src
)))
122 # Evaluate missing targets
123 $(eval FOUND_TRG
:= $(shell find
$(dir $(src
)) -name
"$(basename $(notdir $(src))).*" -not
-name
".*"))
124 $(eval MISSING_TRG
:= $(filter-out $(FOUND_TRG
), $(SLANG_TRG
)))
125 # Check timings of existing files
126 $(eval SRC_TIME
:= $(shell stat
-c
%Y
$(src
)))
127 # For each of the targets created by this source language, evaluate the
128 # last-modified times of each potential target. If the file does not exist, set
129 # the time to '0', otherwise set it to the -c %Y result from statting the file.
130 $(foreach srctrg
,$(filter $(basename $(src
))%, $(SLANG_TRG
)),
131 $(eval TRG_TIMES
+= $(if
$(wildcard $(srctrg
)),$(shell stat
-c
%Y
$(srctrg
)),0)==$(srctrg
)))
132 # Sort TRG_TIMES, which will produce a list of oldest-first files and timestamps
133 # of the form [last-modified-deltatime]==[filename]
134 $(eval TRG_TIMES
:= $(shell echo
$(sort $(TRG_TIMES
)) |
$(call AWK_REVERSE_SQUASH
)))
135 # Evaluate the newest time from our ordered list by splitting the word up by its
136 # '==' connectors and looking at the first word
137 $(eval NEWEST_TRG_TIME
:= $(word 1,$(subst ==, ,$(TRG_TIMES
))))
138 # Find the older of the two times (between SRC_TIME and NEWEST_TRG_TIME)
139 $(eval OLDER_TIME
:= $(firstword $(sort $(NEWEST_TRG_TIME
) $(SRC_TIME
))))
140 # If the older of the two times is the newest target time found, then we need to
141 # rebuild, but only if our build rule intends to actually make something. If it
142 # does not intend to make something, drop a functional rule to actually make the
144 $(if
$(MAKECMDGOALS
),
145 $(eval BUILDGOALS
:= $(filter-out clean scrub purge
uninstall,$(MAKECMDGOALS
))),
146 $(eval BUILDGOALS
:= all))
147 $(if
$(and
$(BUILDGOALS
),$(or
$(MISSING_TRG
), $(filter $(OLDER_TIME
),$(NEWEST_TRG_TIME
)))),
148 $(if
$(findstring n
,$(MAKEFLAGS
)),
152 $(info $(SHELL_CMD
) $(eval
$(shell $(SHELL_CMD
)))))
154 # Put these targets on the MAKE_TARGETS list to be removed during "clean",
155 # regardless of whether or not they were built just now.
156 $(eval MAKE_TARGETS
+= $(SLANG_TRG
))
158 #/PREMAKE_SOURCE_COMPILER########################################################
160 # Compile a source language to a language that compiles to binary, but only if
161 # our build rule intends to build some kind of target
162 $(eval
$(call PREMAKE_SOURCE_COMPILER
,))
165 # Backup the CVS password and CVSROOT environment vars in case we change them
166 $(if
$(wildcard ~
/.cvspass
),\
167 $(eval CVSPASS_BAK
:= $(file
<~
/.cvspass
)))
169 $(eval CVSROOT_BAK
:= $(CVSROOT
)))
171 # A long function definition to grab deps from the source's compiler and route
172 # errors to /dev/null if the rule generator expresses bogus errors
173 define SRC_LANG_FLAGS_OPT_DEPS
=
174 $(filter-out \
%:,$(shell $($2_C) $3 $4 -MG
$1 2> /dev
/null
))
177 #SRC_LANG_RULE###################################################################
178 # Given a source and a language, generate a rule to make the object. The second
179 # invocation of gcc per file lists only its local includes, and filters out
180 # anything already caught. Anything remaining should be considered to be
181 # available in the current working directory of the source file(i.e. if you
182 # '#include "something.h"' in 'mymodule/horseradish.c', you would expect to
183 # include 'mymodule/something.h'. this check lets that happen), otherwise it is
184 # some kind of user error #######################################################
185 define SRC_LANG_RULE
=
186 $(if
$($1),,$(eval
$1 := t
)\
187 $(eval MOD
:= $(filter $(MODULES
),$(firstword $(subst /, ,$(dir $1)))))\
188 $(eval FLG
:= $(MOD
:%=-I
% ))\
190 $(eval DEPS
:= $(filter-out \
%:,$(shell $($2_C) $(FLG
) -M
-MG
$1)))\
191 $(eval MDEPS
:= $(filter $(MODULES
:%=%/%),$(DEPS
)))\
192 $(eval LOST
:= $(filter-out \
%: $(DRIVER_DIR
)/% $(MODULES
:%=%/%),$(shell $($2_C) $(FLG
) -MM
-MG
$1)))\
193 $(eval MDEPS
+= $(LOST
:%=$(dir $1)%))\
194 $(eval MDEPS
:= $(shell echo
$(MDEPS
) | sed
-e
's@[a-zA-Z0-9\-\+/]*\.\./include@../include@g'))\
195 $(eval ALLDEPS
:= $(MDEPS
) $(DEPS
)),\
196 $(error Cannot generate deps for
: $1, file not found
))\
197 $(eval SRC_OBJ
:= $(basename $1).
$($2_OBJ))\
198 $(eval MAKE_TARGETS
+= $(SRC_OBJ
))
199 # Assume that lost dependencies are in the folder with the source
200 $(foreach lost
,$(LOST
),
201 $(eval ALLDEPS
:= $(subst $(lost
), $(dir $1)$(lost
),$(ALLDEPS
))))
203 # Find any deps that aren't built yet, which the compiler has flagged
204 # as missing, but which we know the expected location of libs for
205 # this language should have their includes in 'LIBINC_DIR'
206 $(eval BUILDDEPS
:= $(filter $($2_LIBS:%=%/%),$(ALLDEPS
)))
207 $(eval ALLDEPS
:= $(filter-out $(BUILDDEPS
),$(ALLDEPS
)) $(BUILDDEPS
:%=$(LIBINC_DIR
)/%))
209 $(SRC_OBJ
): $(ALLDEPS
)
210 $($2_C) $$($2_FLAGS) $(FLG
) -c
-o
$$@
$1
212 $(eval DBG_OBJ
:= $(dir $1).
$($2_DBG)/$(basename $(notdir $1)).
$($2_OBJ))\
213 $(if
$(findstring $(dir $(DBG_OBJ
)),$(MAKE_DIRS
)),,\
214 $(eval MAKE_DIRS
+= $(dir $(DBG_OBJ
))))
215 $(eval MAKE_TARGETS
+= $(DBG_OBJ
))
216 # Object for $1 with $($2_DBG) symbols
217 $(DBG_OBJ
): $(ALLDEPS
) |
$(dir $(DBG_OBJ
))
218 $($2_C) $$($2_FLAGS) $(FLG
) -Og
-g
$($2_DBG) -c
-o
$$@
$1
220 #/SRC_LANG_RULE##################################################################
223 #SRC_LANG_DRVRULE################################################################
224 # establish a build and link rule given a source driver and language ############
225 define SRC_LANG_DRVRULE
=
226 $(eval DRIVER_NAME
:= $(basename $(notdir $1)))
227 $(eval DRIVER_TARG
:= $(1:%.
$2=$(ROOT_DIR
)/%$($2_OUT)))
228 $(eval DRIVER_SOB
:= $(basename $1).
$($2_OBJ))
230 # Generate a rule for the driver object
231 $(eval
$(call SRC_LANG_RULE
,$1,$2))
233 # Modules can be implicitly specified by a driver if its dependencies include
234 # ANY files from any module directory. The 'MDEPS' var is set by SRC_LANG_RULE
235 # and expresses the dependencies required to build the object file only.
236 $(foreach module_dep
,$(MDEPS
),
237 $(eval DRIVER_MODULES
+= $(firstword $(subst /, ,$(module_dep
))))
239 $(eval DRIVER_MODULES
:= $(filter-out $(DRIVER_DIR
) ..
,$(sort $(DRIVER_MODULES
))))
241 # In the event that the driver developer does not wish to include a header to
242 # any internal modules, the developer may instead create a file with the same
243 # basename as the driver, but with the '.ld' suffix, which contains a space
244 # separated list of internal modules to link together during compilation
245 $(eval DRIVER_LDVALS
:= $(file
<$(1:%.
$2=%.
ld)))
246 $(eval DRIVER_LFLAGS
:= $(filter -l
%,$(DRIVER_LDVALS
)))
247 $(eval DRIVER_MODULES
+= $(filter-out -l
%,$(DRIVER_LDVALS
)))
249 # List of module archives to link together during compilation
250 $(eval DRIVER_ARCHIVES
:= $(DRIVER_MODULES
:%=%.
$($2_AROBJ)))
251 $(eval DRIVER_DEPS
:= $(DRIVER_SOB
) $(DRIVER_ARCHIVES
))
252 $(eval DRIVER_DBGARCHIVES
:= $(DRIVER_MODULES
:%=.
$($2_DBG)/%.
$($2_AROBJ)))
253 $(eval DRIVER_DBGDEPS
:= $(dir $(DRIVER_SOB
)).
$($2_DBG)/$(notdir $(DRIVER_SOB
)))
254 $(eval DRIVER_DBGDEPS
+= $(DRIVER_DBGARCHIVES
))
256 # If the compiler supports linking, distinguish static from dynamic links,
257 # otherwise set all libraries to static
259 $(eval STLIBS
:= $(filter-out $($($2_C)_LDLIBS
),$($2_LIBS)))\
260 $(eval DLIBS
:= $(filter-out $(STLIBS
),$($2_LIBS))),\
261 $(eval STLIBS
:= $($2_LIBS)))
264 $(eval DRIVER_TARG_DIR
:= $(dir $(1:%.
$2=$(ROOT_DIR
)/%)))
265 $(eval MAKE_DIRS
+= $(DRIVER_TARG_DIR
))
266 $(if
$($2_DBG),$(eval MAKE_DIRS
+= $(DRIVER_TARG_DIR
).
$($2_DBG)))
268 # Setup the sources for this object. A driver build rule does not include any
269 # compile steps at all, and so should not contain any sources that are,
270 # themselves, traditional "source files" like a language-compiled 'c' file. The
271 # driver depends on the existence of its equivalent object file (created through
272 # compilation), which is expected to contain a 'main' function (or entry for
273 # '_start'). This driver object is then simply linked to its libraries and
274 # module archives to create an executable binary in the output folder.
275 # ORDER MATTERS HERE, this is for the linker:
276 $(eval DRIVER_SRC
:= $(DRIVER_LFLAGS
) $(DRIVER_DEPS
))
277 $(eval DRIVER_DBGSRC
:= $(DRIVER_LFLAGS
) $(DRIVER_DBGDEPS
))
278 # Iterate through the list of libraries in our language and stack commands from
279 # left to right in the "sources" section of the LD command (or LD section of the
280 # commandline for the compiler), to preserve ordering and also ensure that it is
281 # linked correctly IN THAT ORDER (static vs dynamic).
282 $(foreach lib
,$($2_LIBS),\
283 $(if
$(findstring $(lib
),$(STLIBS
)),
284 # Evaluate static lib $(lib) and add it to the dependencies and sources of the rules we built
285 $(eval STATIC_SRC
:= $(LIB_DIR
)/lib
$(lib
).
$($2_AROBJ))\
286 $(eval STATIC_DBGSRC
:= $(LIB_DIR
)/.
$($2_DBG)/lib
$(lib
).
$($2_AROBJ))\
287 $(eval DRIVER_SRC
:= $(STATIC_SRC
) $(DRIVER_SRC
))\
288 $(eval DRIVER_DBGSRC
:= $(STATIC_DBGSRC
) $(DRIVER_DBGSRC
))\
289 $(eval DRIVER_DEPS
+= $(STATIC_SRC
))\
290 $(eval DRIVER_DBGDEPS
+= $(STATIC_DBGSRC
)),
291 # Add dynamic lib to the sources with a link flag, in its correct ordering
292 $(eval DRIVER_SRC
:= -l
$(lib
) $(DRIVER_SRC
))\
293 $(eval DRIVER_DBGSRC
:= -l
$(lib
) $(DRIVER_DBGSRC
))\
296 # Add the driver target to this language's targets for cleaning
297 $(eval SCRUB_TARGETS
+= $(DRIVER_TARG
))
298 $(DRIVER_TARG
): $(DRIVER_DEPS
) |
$(DRIVER_TARG_DIR
)
299 $($2_C) $($2_FLAGS) $(DRIVER_SRC
) -o
$$@
300 # Output a rule for building with debug flags
301 $(eval DRIVER_DBGTARG
:= $(DRIVER_TARG_DIR
)$(basename $(notdir $(DRIVER_TARG
)))-d
$($2_OUT))
302 $(eval SCRUB_TARGETS
+= $(DRIVER_DBGTARG
))
303 $(eval MAKE_DIRS
+= $(DRIVER_TARG_DIR
).
$($2_DBG)/)
304 $(DRIVER_DBGTARG
): $(DRIVER_DBGDEPS
) |
$(DRIVER_TARG_DIR
)
305 $($2_C) $($2_FLAGS) $(DRIVER_DBGSRC
) -o
$$@
306 # Make a rule to run this driver after building
307 $(DRIVER_NAME
)-run
: $(DRIVER_TARG
)
309 $(DRIVER_NAME
)-d
: $(DRIVER_DBGTARG
)
311 #/SRC_LANG_DRVRULE###############################################################
314 #MODULE_ARCRULE##################################################################
315 # generate rule for turning an entire module's collection of binary objects into
316 # a single locally-linked (no external -L libs) object (for simplified linking
317 # modules as static libs).#######################################################
318 define MODULE_ARCRULE
=
319 $(eval ARCDEPS
:= $(filter $1/%.
$(c_OBJ
),$(foreach lang
,$(LANGS
),$($(lang
)_MOD_TRG
))))\
320 $(eval MAKE_TARGETS
+= $1.
$(c_AROBJ
))\
322 $1.
$(c_AROBJ
): $(ARCDEPS
)
325 $(eval undefine DBGARCDEPS
)
326 $(foreach arcdep
,$(ARCDEPS
),$(eval DBGARCDEPS
+= $(dir $(arcdep
)).
$(c_DBG
)/$(notdir $(arcdep
))))
327 $(eval MAKE_TARGETS
+= .
$(c_DBG
)/$1.
$(c_AROBJ
))\
329 .
$(c_DBG
)/$1.
$(c_AROBJ
): $(DBGARCDEPS
) | .
$(c_DBG
)/
332 #/MODULE_ARCRULE#################################################################
335 # LANG_LIB_PARENT_BUILDRULE######################################################
336 # define rules for creating unknown libraries in the local build environment for
337 # either binary or bytecode representation given a library name and, optionally,
338 # a parent lib that included it for error reporting #############################
339 define LANG_LIB_PARENT_BUILDRULE
=
340 $(eval CONF_FLAG
:= --prefix=$(abspath
$(LIB_DIR
)).trash
)
341 $(eval CONF_FLAG
+= --includedir=$(abspath
$(LIBINC_DIR
)))
342 # Setup installation rule steps for this lib
344 BUILDSTEPS
:= $(if
$(AUTOGEN
),$(AUTOGEN
) && )
345 BUILDSTEPS
+= $(if
$(CONFIGURE
),$(CONFIGURE
) $(CONF_FLAG
) --libdir=$(abspath
$(LIB_DIR
)) && )
347 BUILDSTEPS
+= $(MKCMD
),
348 $(error
$2.mk requires a valid MKCMD definition
))
350 INSTALLCMD
:= $(MKINSTALL
),\
351 $(error
$2.mk requires a valid MKINSTALL definition
))
352 CLEANCMD
:= $(MKCLEAN
)
354 # Construct the download method, or error if there isn't a valid one
355 $(if
$(GITADDR
),$(eval LIBDL
:= git clone
$(GITADDR
) $2),\
356 $(if
$(HGADDR
),$(eval LIBDL
:= hg clone
$(HGADDR
) $2),\
358 $(eval LIBDL
:= export CVSROOT
=$(CVSADDR
))\
359 $(eval LIBDL
+= && echo
'$(CVSPASS)' > ~
/.cvspass
)\
360 $(eval LIBDL
+= && cvs
$($2_CVSGET))
361 $(eval LIBDL
+= && $(if
$(CVSPASS_BAK
),echo
$(CVSPASS_BAK
) > ~
/.cvspass
,rm ~
/.cvspass
)),\
362 $(if
$(WEBADDR
),$(eval LIBDL
:= wget
-O
$(WEBTARG
) $(WEBADDR
)),\
363 $(eval
$(error No way to download
$2 needed by
$3 for
$1 language
))))))
367 cd
$(LIBDL_DIR
) && $(LIBDL
) $(if
$(WEBINIT
),&& $(WEBINIT
))
368 # '$2' Make and install rule, and ensure it isn't marked "clean"
369 $(LIB_DIR
)/lib
$2.
$($1_AROBJ): $(LIBDEPS
:%=$(LIB_DIR
)/lib
%.
$($1_AROBJ)) |
$(LIBDL_DIR
)/$2
371 cd
$(LIBDL_DIR
)/$2 && export LD_RUN_PATH
=$(LIBLDPATH
) && $(BUILDSTEPS
)
372 cd
$(LIBDL_DIR
)/$2 && $(INSTALLCMD
)
373 @
rm -f
$(LIBDL_DIR
)/$2.
clean
375 # '$2' Rule for building under debug mode
377 $(eval LIBDBG_DIR
:= $(LIB_DIR
)/.
$($1_DBG))\
378 $(eval
$(if
$(findstring $(LIBDBG_DIR
),$(MAKE_DIRS
)),,MAKE_DIRS
+= $(LIBDBG_DIR
)))\
379 $(eval LIBDBG_TARG
:= $(LIBDBG_DIR
)/lib
$2.
$($1_AROBJ))\
380 $(eval LIBDBG_DEPS
:= $(LIBDEPS
:%=$(LIB_DIR
)/.
$($1_DBG)/lib
%.
$($1_AROBJ)))\
381 $(eval DBGBUILDSTEPS
:= $(if
$(AUTOGEN
),$(if
$(AUTOGENDBG
),$(AUTOGENDBG
),$(AUTOGEN
)) && ))\
382 $(eval DBGBUILDSTEPS
+= $(if
$(CONFIGURE
),$(if
$(CONFIGUREDBG
),$(CONFIGUREDBG
),$(CONFIGURE
)) $(CONF_FLAG
) --libdir=$(abspath
$(LIB_DIR
))/.
$($1_DBG)))\
383 $(eval DBGBUILDSTEPS
+= $(if
$(MKDBGCMD
),$(MKDBGCDM
),$(MKCMD
)))\
385 # '$2' Make and install rule for debugging
386 $(LIBDBG_TARG
): $(LIBDBG_DEPS
) |
$(LIBDL_DIR
)
387 @mkdir
-p
$(LIBDBG_DIR
)
388 cd
$(LIBDL_DIR
)/$2 && export LD_RUN_PATH
=$(LIBLDPATH
:$(LIB_DIR
)%=$(LIBDBG_DIR
)) && $(DBGBUILDSTEPS
)
389 cd
$(LIBDL_DIR
)/$2 && $(INSTALLCMD
)
390 @
rm -f
$(LIBDL_DIR
)/$2.
clean
393 # '$2' Clean rule - drop a marker in the directory so we don't bother cleaning
394 # things that are clean... by default there is no real way of tracking this in a
397 $$(if
$$(wildcard $(LIBDL_DIR
)/$2),$$(if
$$(wildcard $(LIBDL_DIR
)/$2.
clean),,\
398 cd
$(LIBDL_DIR
)/$2 && $(CLEANCMD
) && touch
$(abspath
$(LIBDL_DIR
))/$2.
clean))
400 #/LANG_LIB_PARENT_BUILDRULE######################################################
404 #LANG_LIB_INIT###################################################################
405 # For every library in this language, we need to determine how, and if, to link
406 # its data. This could potentially be done recursively as libraries have
407 # dependencies upon each other. We call a recursive macro here to expand out
408 # all the libs for this language, and then flatten the tree by removing
409 # duplicates with shell commands. We can't use make's 'sort' function because
410 # we actually care about the ordering so that interdependent libraries can link
411 # back up the tree ##############################################################
412 define LANG_LIB_INIT
=
413 # Initialize per-library data, then import data from an accompanying file that
414 # can overwrite these vars
416 # Implicit defaults for lib.mk files
417 AUTOGEN
:= .
/autogen.sh
418 AUTOGENDBG
:= .
/autogen.sh
--enable-debug
419 CONFIGURE
:= .
/configure
421 MKINSTALL
:= make
-k
install
422 MKCLEAN
:= make
clean
426 LIBLDPATH
:= $(if
$($($1_C)_LD
),$(if
$(LD_LIBRARY_PATH
),$(LD_LIBRARY_PATH
):,/usr
/lib
:)$(abspath
$(LIB_DIR
)))
427 # One of these must be defined in the .mk file imported
428 $(foreach var
, GITADDR WEBADDR HGADDR CVSADDR
,\
429 $(eval undefine
$(var
)))
430 # Other, optional, variables to clear
431 $(foreach var
, WEBTARG WEBINIT CVSGET CVSPASS CONFIGUREDBG
,
432 $(eval undefine
$(var
)))
434 # Include and evaluate such that it may be called and evaluated for immediate
435 # evaluation of its contents
437 $(eval
include $2.mk
)
439 # If there is a .mk file for this lib, import it. Otherwise, we'll try to link
440 # it later, unless there is no linker for the language's compiler.
441 $(if
$(wildcard $2.mk
),$(eval
$(file
<$2.mk
)),\
443 $(warning No
$2.mk for lib
$2, attempting to link from the system
),\
444 $(error No
$2.mk for lib
$2 and
'$($1_C)' is not configured for linking
)))\
446 # Add dependencies we found (potentially recursively, ignore repeats until later
447 # so we can preserve ordering information for interdependency build ordering)
449 $(eval
$1_LIBS += $(LIBDEPS
)))
450 # languages that can link this library (typically any lang that shares the
451 # object format (ELF by default, but this is compiler-dependent (e.g. emcc uses
452 # llvm bitcode, which is just an intermediary representation of data that can
453 # link to anything else statically, or as an archive)))
454 $(if
$(findstring $1,$(LIBLANGS
)),,\
455 $(error
$2.mk must specifically set LIBLANGS
:= $1 if it is compatible with
$1))
456 # Mark this lib as available for all its other compatible languages. This list
457 # is assumed to be sorted later, so we will ignore repeats for speed
458 $(foreach lang
,$(LIBLANGS
),\
459 $(if
$(findstring $2,$($(lang
)_LIBS
)),,\
460 $(eval
$(lang
)_LIBS
+= $2)))
461 # Generate a build rule for this lib, unless it's already built
462 $(if
$(findstring $2,$(BUILTLIBS
)),,\
463 $(eval
$(call LANG_LIB_PARENT_BUILDRULE
,$1,$2,$3))\
464 $(eval BUILTLIBS
+= $2))
465 # Recurse this function for every libdep encountered
466 $(foreach libdep
,$(LIBDEPS
),\
467 $(call LANG_LIB_INIT
,$1,$(libdep
),$2))
468 #/LANG_LIB_INIT#################################################################
471 # Initialize data for supported lanaguages ######################################
474 $1_OBJ := $($($1_C)_OBJ
)
475 $1_OUT := $($($1_C)_OUT
)
476 $1_AROBJ := $($($1_C)_AROBJ
)
477 $1_SOURCES := $(subst .
/,,$(shell find
-name
"*.$1" -not
-name
".*"))
478 $1_DBG := $($($1_C)_DBG
)
479 $1_AR := $($($1_C)_AR
)
481 # Find save language-specific driver and module sources and targets
482 $(eval
$1_DRV_SRC := $(filter $(DRIVER_DIR
)/%,$($1_SOURCES)))
483 $(eval
$1_DRV_TRG := $(patsubst %.
$1,$(ROOT_DIR
)/%$($1_OUT),$($1_DRV_SRC)))
484 $(eval
$1_MOD_SRC := $(filter-out $(DRIVER_DIR
)/%,$($1_SOURCES)))
485 $(eval
$1_MOD_TRG := $(subst .
$1,.
$($1_OBJ),$($1_MOD_SRC)))
486 # Add those sources and targets to a language-irreverant var
488 DRV_SRC
+= $($1_DRV_SRC)
489 DRV_TRG
+= $($1_DRV_TRG)
490 MOD_SRC
+= $($1_MOD_SRC)
491 MOD_TRG
+= $($1_MOD_TRG)
493 # First, initialize language-module, module, and language relative data. Then,
494 # filter out each source file from the module and generate Dependency Rules and
495 # Module Object Rules for each of them.
496 $(foreach module
,$(MODULES
),
497 # Evaluate the module sources for this language
498 $(eval
$(module
)_
$1_SRC := $(filter $(module
)/%,$($1_MOD_SRC)))
499 # Evaluate module targets for this language
500 $(eval
$(module
)_
$1_TRG := $($(module
)_
$1_SRC:%.
$1=%.
$($1_OBJ)))
501 # Add these language-specific sources and targets to the module's src/targs
503 $(module
)_SRC
+= $($(module
)_
$1_SRC)
504 $(module
)_TRG
+= $($(module
)_
$1_TRG)
506 # For every source file, create a build rule for it for our language
507 $(foreach src
,$(filter $(module
)/%,$($1_MOD_SRC)),\
508 $(eval
$(call SRC_LANG_RULE
,$(src
),$1))\
510 # For all the listed libraries, initialize the library, which will set up all of
511 # its local ($1_LIBS, etc) data
512 $(foreach lib
,$($1_LIBS),\
513 $(eval
$(call LANG_LIB_INIT
,$1,$(lib
),$1)))
514 # The initializer produces an ordered list of interlinked dependencies in groups
515 # by depth, so when reading backwards and compressing repeats we are left with a
516 # reliable build order for library interlinking. this awk program just looks
517 # through a list of space separated words backwards failing to print when it
518 # encounters a repeat
519 $1_LIBS := $(shell echo
"$($1_LIBS)" |
$(call AWK_REVERSE_SQUASH
))
520 #/LANG_INIT######################################################################
523 # The recursive library dependecy traversal constructs a tree ordered by nested
524 # dependency depth. when linking, this order is reversed.
525 # Initialize each language and look for its files
526 $(eval
$(foreach lang
,$(LANGS
),\
527 $(eval
$(call LANG_INIT
,$(lang
)))\
528 $(eval
$(lang
)_LIBS
:= $(shell echo
$($(lang
)_LIBS
) |
$(call AWK_REVERSE_SQUASH
)))\
531 # Create module object rules for each module
532 $(foreach module
,$(MODULES
),\
533 $(eval
$(call MODULE_ARCRULE
,$(module
)))\
535 $(if
$(c_DBG
),$(eval MAKE_DIRS
+= .
$(c_DBG
)/))
537 # Create lang-specific rules for producing final (linked) targets
538 $(foreach lang
,$(LANGS
),\
539 $(foreach drvsrc
,$($(lang
)_DRV_SRC
),\
540 $(eval
$(call SRC_LANG_DRVRULE
,$(drvsrc
),$(lang
)))\
543 # Create driver rules for each driver we found, let users call make specifying
544 # the basename of the driver file, and just route to the correct linkrule we
545 # made in the last paragraph
546 DRV_FNAMES
:= $(notdir $(DRV_SRC
))
547 .PHONY
: all $(basename $(DRV_NAMES
))
548 $(foreach fname
,$(DRV_FNAMES
),\
549 $(eval
$(basename $(fname
)): \
550 $(filter %/$(basename $(fname
))$($(lastword
$(subst .
, ,$(fname
))_OUT
)),$(DRV_TRG
)))\
552 all: $(basename $(DRV_FNAMES
))
555 # Rule to make any dirs that we're in charge of
556 $(sort $(MAKE_DIRS
)):
560 # Clean destroys all object files and internal module archives.
561 CLEAN_TARGETS
:= $(sort $(foreach mtarg
,$(MAKE_TARGETS
),$(if
$(wildcard $(mtarg
)),$(mtarg
))))
562 clean: $(BUILTLIBS
:%=clean_
%)
563 $(if
$(CLEAN_TARGETS
),rm $(CLEAN_TARGETS
))
565 # Scrub destroys all distributable binaries
566 SCRUB_TARGETS
:= $(sort $(foreach starg
,$(SCRUB_TARGETS
),$(if
$(wildcard $(starg
)),$(starg
))))
568 $(if
$(SCRUB_TARGETS
),rm $(SCRUB_TARGETS
))
570 # Purge destroys all distributables created by a distributable binary
573 # Uninstall destroys all objects and directories that cannot be recreated by