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 automatically 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 # Language-specific compilers and flags passed in from environment
16 c_C
:= $(strip $(notdir $(CC
)))
17 c_FLAGS
:= $(strip $(CFLAGS
)) -I.
18 c_LIBS
:= SDL2 wolfssl
20 cpp_C
:= $(strip $(notdir $(CXX
)))
21 cpp_FLAGS
:= $(strip $(CXXFLAGS
)) $(c_FLAGS
)
24 go_FLAGS
:= $(c_FLAGS
)
25 # Source to source languages
31 # Compiler-specific associations. Each compiler has a binary object suffix
32 # (OBJ), an archiver (AR), and an archiver object suffix (AROBJ). Each compiler
33 # may optionally have defined a linker (LD), and a binary output suffix (OUT).
38 $(cpp_C
)_LD
:= $(cc_LD
)
42 gcc_AROBJ
:= $(cc_AROBJ
)
45 emcc_AROBJ
:= $(emcc_OBJ
) #emar is just a python script that reparses shit for emcc
48 em
++_OBJ
:= $(emcc_OBJ
)
50 em
++_AROBJ
:= $(emcc_AROBJ
)
51 em
++_OUT
:= $(emcc_OUT
)
55 gccgo_AROBJ
:= $(cc_AROBJ
)
56 # Shell functions to determine what libraries can be linked by the compiler
57 cc_LDLIBS
:= $(shell ls
/usr
/lib | grep
".o" | sed
-e
's@^.*lib\([_\+a-zA-Z0-9\-]*\)\..*@\1@g')
58 gcc_LDLIBS
:= $(cc_LDLIBS
)
60 g
++_LDLIBS
:= $(cc_LDLIBS
)
62 go_LDLIBS
:= $(cc_LDLIBS
)
64 # The directory in './' containing app drivers and compilation info
67 LIB_DIR
:= $(ROOT_DIR
)/lib
$(shell uname
-m
)/$($(c_C
)_OBJ
)
68 LIBDL_DIR
:= $(LIB_DIR
)/.cache
69 LIBINC_DIR
:= $(ROOT_DIR
)/include
70 # The makefile MUST be capable of generating these directories and all of their
71 # contents. These directories are removed during the 'scrub' rule
72 MAKE_DIRS
:= $(LIB_DIR
) $(LIBDL_DIR
) $(LCLLIB_DIR
)
73 # Set up lib inclusions, and scan for built libs
74 c_FLAGS
+= -I
$(LIBINC_DIR
)
75 c_OBJ
:= $($(c_C
)_OBJ
)
76 # Modules are any directories other than 'DRIVER_MODULE' in './' and produce a
77 # single object file in './' containing all of the module's symbols and
79 MODULES
:= $(filter-out $(DRIVER_DIR
),$(subst /,,$(shell ls
-d
*/)))
81 # Backup the CVS password and CVSROOT environment vars in case we change them
82 $(if
$(wildcard ~
/.cvspass
),\
83 $(eval CVSPASS_BAK
:= $(file
<~
/.cvspass
)))
85 $(eval CVSROOT_BAK
:= $(CVSROOT
)))
87 # The following awk program reverses the order of a list while also removing
88 # duplicate entries. The effect of this when run on the dependency tree is that
89 # it will remove duplicates occurring in reverse order, allowing the most deeply
90 # nested libraries to be built, and linked, first.
91 define AWK_REVERSE_SQUASH
=
95 printf (!a[$$i]++) ? $$i FS : "";
102 # Given a source and a language, generate a rule to make the object. The second
103 # invocation of gcc per file lists only its local includes, and filters out
104 # anything already caught. Anything remaining should be considered to be
105 # available in the current working directory of the source file(i.e. if you
106 # '#include "something.h"' in 'mymodule/horseradish.c', you would expect to
107 # include 'mymodule/something.h'. this check lets that happen), otherwise it is
108 # some kind of user error
109 define SRC_LANG_RULE
=
110 $(eval MOD
:= $(firstword $(subst /, ,$(dir $1))))\
111 $(eval DEPS
:= $(filter $(MODULES
:%=%/%),$(shell $($2_C) -I
$(MOD
) -M
-MG
$1 2> /dev
/null
)))\
112 $(eval LOST
:= $(filter-out %: $(MODULES
:%=%/%),$(shell $($2_C) -I
$(MOD
) -MM
-MG
$1 2> /dev
/null
)))\
113 $(eval DEPS
+= $(LOST
:%=$(dir $1)%))\
114 $(if
$($1),,$(eval
$1 := t
)\
115 $(basename $1).
$($2_OBJ): $(DEPS
)
116 $($2_C) $$($2_FLAGS) -I
$(MOD
) -c
-o
$$@
$1
120 # establish a build and link rule given a source driver and language ############
121 define SRC_LANG_DRVRULE
=
122 # turn the non-existent third argument into an identifier for our link data
123 $(eval
3 := $(notdir $(basename $1)))
124 # generate the dependecies list of the driver itself
125 $(eval
$3_DRV_OBJ := $(1:%.
$2=%.
$($2_OBJ)))
126 $(eval
$3_DRV_DEP := $(filter-out $3:,$(shell $($2_C) -M
-MG
$1)))
127 # Implicit and Manual LD Options to allow .ld files to specify manual linking to
128 # one of the built-in modules. Implicit LDOs are ones we can surmise from
129 # include rules generated with 'SRC_LANG_DEPS', but these are only caught if
130 # there was an explicitly included header, or some other file, within the source
131 # file of the driver to one of the internal modules. This practice is a
132 # reliable way to automatically link an internal module, but if desired an
133 # associated '.ld' file for the driver can contain a list of whitespace
134 # separated module names to ensure they link properly
135 $(foreach mfile
,$(filter $(MODULES
:%=%/%),$($3_DRV_DEP)),\
136 $(eval
$3_MODULES += $(firstword $(subst /, ,$(mfile
)))))
137 # Find the .ld files, if present, and include their 'links' to our internal libs
138 $(if
$(wildcard $(1:.
$2=.
ld)),\
139 $(eval
$3_MODULES += $(basename $(strip $(file
<$(1:.
$2=.
ld))))))
140 # Remove duplicates and sort the module list
141 $(eval
$3_MODULES := $(sort $($3_MODULES)))
142 # If the compiler supports linking, distinguish static from dynamic linking
144 $(eval
$3_STLIBS := $(filter-out $($($2_C)_LDLIBS
),$($2_LIBS)))\
145 $(eval
$3_DLIBS := $(filter-out $($3_STLIBS),$($2_LIBS))),\
146 $(eval
$3_STLIBS := $($2_LIBS)))
148 $(eval
$3_DIR := $(dir $(1:%.
$2=$(ROOT_DIR
)/%)))
149 $(eval MAKE_DIRS
+= $($3_DIR))
150 # Setup the sources for this object
151 $(eval
$3_SRC := $($3_DRV_OBJ))
152 # Find the dependencies
153 $(eval
$3_DEP := $(filter-out $1,$($3_DRV_DEPS)))
154 $(eval
$3_DEP += $($3_SRC))
155 # Preserver ordering and build out the linking order (don't use -Wstatic/dynamic)
156 $(foreach lib
,$($2_LIBS),\
157 $(if
$(findstring $(lib
),$($3_STLIBS)),\
158 $(eval
$3_SRC := $(LIB_DIR
)/lib
$(lib
).
$($($2_C)_AROBJ
) $($3_SRC)),\
159 $(eval
$3_SRC := -l
$(lib
) $($3_SRC))))
160 $(eval
$3_SRC += $($3_MODULES:%=%.
$($2_AROBJ)))
161 # Output the driver object file rule
162 $(eval
$2_TARGETS += $($3_DRV_OBJ))
163 # Filter the dependencies of the object to only include deps inside our modules
164 $($3_DRV_OBJ): $(filter $(MODULES
:%=%/%),$($3_DRV_DEP))
165 $($2_C) $($2_FLAGS) -c
-o
$$@
$1
166 # Output the driver link rule
167 $(eval
$2_TARGETS += $($3_DIR)$3$($2_OUT))
168 $($3_DIR)$3$($2_OUT): $($3_DEP) $($3_SRC) |
$($3_DIR)
169 $($2_C) $($2_FLAGS) $($3_SRC) -o
$$@
170 #/SRC_LANG_DRVRULE##############################################################
173 # generate rule for turning an entire module's collection of binary objects into
174 # a single locally-linked (no external -L libs) object (for simplified linking
175 # modules as static libs).
176 define MODULE_ARCRULE
=
177 $(eval c_TARGETS
+= $1.
$($(c_C
)_AROBJ
))\
178 $(eval
$1_ARCDEPS := $(filter $1/%,$(foreach lang
,$(LANGS
),$($(lang
)_MOD_TRG
))))\
179 $1.
$($(c_C
)_AROBJ
): $($1_ARCDEPS) |
$(LCLLIB_DIR
)
180 $($(c_C
)_AR
) cr
$$@
$$^
183 # LANG_LIB_PARENT_BUILDRULE######################################################
184 # define rules for creating unknown libraries in the local build environment for
185 # either binary or bytecode representation given a library name and, optionally,
186 # a parent lib that included it for error reporting #############################
187 define LANG_LIB_PARENT_BUILDRULE
=
188 # Setup installation rule steps for this lib
190 BUILDSTEPS
:= $(if
$(AUTOGEN
),$(AUTOGEN
) && )
191 BUILDSTEPS
+= $(if
$(CONFIGURE
),$(CONFIGURE
) && )
193 BUILDSTEPS
+= $(MKCMD
),
194 $(error
$2.mk requires a valid MKCMD definition
))
196 INSTALLCMD
:= $(MKINSTALL
),\
197 $(error
$2.mk requires a valid MKINSTALL definition
))
198 CLEANCMD
:= $(MKCLEAN
)
200 # Construct the download method, or error if there isn't a valid one
201 $(if
$(GITADDR
),$(eval LIBDL
:= git clone
$(GITADDR
) $2),\
202 $(if
$(HGADDR
),$(eval LIBDL
:= hg clone
$(HGADDR
) $2),\
204 $(eval LIBDL
:= export CVSROOT
=$(CVSADDR
))\
205 $(eval LIBDL
+= && echo
'$(CVSPASS)' > ~
/.cvspass
)\
206 $(eval LIBDL
+= && cvs
$($2_CVSGET))
207 $(eval LIBDL
+= && $(if
$(CVSPASS_BAK
),echo
$(CVSPASS_BAK
) > ~
/.cvspass
,rm ~
/.cvspass
)),\
208 $(if
$(WEBADDR
),$(eval LIBDL
:= wget
-O
$(WEBTARG
) $(WEBADDR
)),\
209 $(eval
$(error No way to download
$2 needed by
$3 for
$1 language
))))))
213 cd
$(LIBDL_DIR
) && $(LIBDL
) $(if
$(WEBINIT
),&& $(WEBINIT
))
214 # '$2' Make and install rule, and ensure it isn't marked "clean"
215 $(LIB_DIR
)/lib
$2.
$($1_AROBJ): $(LIBDEPS
:%=$(LIB_DIR
)/lib
%.
$($1_AROBJ)) |
$(LIBDL_DIR
)/$2
217 cd
$(LIBDL_DIR
)/$2 && export LD_RUN_PATH
=$(LIBLDPATH
) && $(BUILDSTEPS
)
218 cd
$(LIBDL_DIR
)/$2 && $(INSTALLCMD
)
219 @
rm -f
$(LIBDL_DIR
)/$2.
clean
220 # '$2' Clean rule - drop a marker in the directory so we don't bother cleaning
221 # things that are clean... by default there is no real way of tracking this in a
224 $$(if
$$(wildcard $(LIBDL_DIR
)/$2),$$(if
$$(wildcard $(LIBDL_DIR
)/$2.
clean),,\
225 cd
$(LIBDL_DIR
)/$2 && $(CLEANCMD
) && touch
$(abspath
$(LIBDL_DIR
))/$2.
clean))
226 #/LANG_LIB_PARENT_BUILDRULE######################################################
230 #LANG_LIB_INIT###################################################################
231 # For every library in this language, we need to determine how, and if, to link
232 # its data. This could potentially be done recursively as libraries have
233 # dependencies upon each other. We call a recursive macro here to expand out
234 # all the libs for this language, and then flatten the tree by removing
235 # duplicates with shell commands. We can't use make's 'sort' function because
236 # we actually care about the ordering so that interdependent libraries can link
237 # back up the tree ##############################################################
238 define LANG_LIB_INIT
=
239 # Initialize per-library data, then import data from an accompanying file that
240 # can overwrite these vars
242 # Implicit defaults for lib.mk files
243 AUTOGEN
:= .
/autogen.sh
244 CONFIGURE
:= .
/configure
245 CONFIGURE
+= --prefix=$(abspath
$(LIB_DIR
)/.trash
)
246 CONFIGURE
+= --includedir=$(abspath
$(LIBINC_DIR
))
247 CONFIGURE
+= --libdir=$(abspath
$(LIB_DIR
))
249 MKINSTALL
:= make
-k
install
250 MKCLEAN
:= make
clean
253 LIBLDPATH
:= $(if
$($($1_C)_LD
),$(if
$(LD_LIBRARY_PATH
),$(LD_LIBRARY_PATH
):,/usr
/lib
:)$(abspath
$(LIB_DIR
)))
254 # One of these must be defined in the .mk file imported
255 $(foreach var
, GITADDR WEBADDR HGADDR CVSADDR
,\
256 $(eval undefine
$(var
)))
257 # Other, optional, variables to clear
258 $(foreach var
, WEBTARG WEBINIT CVSGET CVSPASS
,
259 $(eval undefine
$(var
)))
261 # Include and evaluate such that it may be called and evaluated for immediate
262 # evaluation of its contents
264 $(eval
include $2.mk
)
266 # If there is a .mk file for this lib, import it. Otherwise, we'll try to link
267 # it later, unless there is no linker for the language's compiler.
268 $(if
$(wildcard $2.mk
),$(eval
$(call EVALINCLUDE
)),\
270 $(warning No
$2.mk for lib
$2, linking from the system
),\
271 $(error No
$2.mk for lib
$2 and
'$($1_C)' is not configured for linking
)))\
273 # Add dependencies we found (potentially recursively, ignore repeats until later
274 # so we can preserve ordering information for interdependency build ordering)
276 $(eval
$1_LIBS += $(LIBDEPS
)))
277 # languages that can link this library (typically any lang that shares the
278 # object format (ELF by default, but this is compiler-dependent (e.g. emcc uses
279 # llvm bitcode, which is just an intermediary representation of data that can
280 # link to anything else statically, or as an archive)))
281 $(if
$(findstring $1,$(LIBLANGS
)),,\
282 $(error
$2.mk must specifically set LIBLANGS
:= $1 if it is compatible with
$1))
283 # Mark this lib as available for all its other compatible languages. This list
284 # is assumed to be sorted later, so we will ignore repeats for speed
285 $(foreach lang
,$(LIBLANGS
),\
286 $(if
$(findstring $2,$($(lang
)_LIBS
)),,\
287 $(eval
$(lang
)_LIBS
+= $2)))
288 # Generate a build rule for this lib, unless it's already built
289 $(if
$(findstring $2,$(BUILTLIBS
)),,\
290 $(eval
$(call LANG_LIB_PARENT_BUILDRULE
,$1,$2,$3))\
291 $(eval BUILTLIBS
+= $2))
292 # Recurse this function for every libdep encountered
293 $(foreach libdep
,$(LIBDEPS
),\
294 $(call LANG_LIB_INIT
,$1,$(libdep
),$2))
295 #/LANG_LIB_INIT#################################################################
298 # Initialize data for supported lanaguages ######################################
301 # Initialize all relevant (although not necessarily used) language relative data
302 define $1_VARS_INIT =
304 $1_OBJ := $($($1_C)_OBJ
)
305 $1_OUT := $($($1_C)_OUT
)
306 $1_AROBJ := $($($1_C)_AROBJ
)
307 $1_SOURCES := $(subst .
/,,$(shell find
-name
"*.$1"))
309 # For each source language that can compile to this language, add the expected
310 # result of such a transformation to the sources of this language. Then, if the
311 # source-to-source compiler also creates duplicate files of other formats
312 # (i.e. yacc/bison also produce accompanying .h files), add them to the list of
313 # source files that this makefile can generate ('$1_TARGETS'), to later be deleted
314 # during the clean rule.
315 $(foreach srcl
,$($1_SRCL),\
316 $(eval
$(srcl
)_SOURCES
:= $(shell find
-name
"*.$(srcl)" | \
317 sed
-e
's@^\(.*\)\.$(srcl)@\1$($(srcl)_STEM:%=.%).$1@g' -e
's@\./@@'))\
318 $1_SOURCES += $($(srcl
)_SOURCES
)
319 $(srcl
)_TARGETS
+= $($(srcl
)_SOURCES
:%.
$(srcl
)=.
$1)
320 $(foreach dup
,$($(srcl
)_DUP
),\
321 $(srcl
)_TARGETS
+= $($(srcl
)_SOURCES
:%.
$1=%.
$(dup
))
325 $(eval
$($1_VARS_INIT))\
326 # Find save language-specific driver and module sources and targets
327 $(eval
$1_DRV_SRC := $(filter $(DRIVER_DIR
)/%,$($1_SOURCES)))
328 $(eval
$1_DRV_TRG := $(patsubst %.
$1,$(ROOT_DIR
)/%$($1_OUT),$($1_DRV_SRC)))
329 $(eval
$1_MOD_SRC := $(filter-out $(DRIVER_DIR
)/%,$($1_SOURCES)))
330 $(eval
$1_MOD_TRG := $(subst .
$1,.
$($1_OBJ),$($1_MOD_SRC)))
331 # Add those sources and targets to a language-irreverant var
333 DRV_SRC
+= $($1_DRV_SRC)
334 DRV_TRG
+= $($1_DRV_TRG)
335 MOD_SRC
+= $($1_MOD_SRC)
336 MOD_TRG
+= $($1_MOD_TRG)
338 # First, initialize language-module, module, and language relative data. Then,
339 # filter out each source file from the module and generate Dependency Rules and
340 # Module Object Rules for each of them.
341 $(foreach module
,$(MODULES
),
342 # Evaluate the module sources for this language
343 $(eval
$(module
)_
$1_SRC := $(filter $(module
)/%,$($1_MOD_SRC)))
344 # Evaluate module targets for this language
345 $(eval
$(module
)_
$1_TRG := $($(module
)_
$1_SRC:%.
$1=%.
$($1_OBJ)))
346 # Add these language-specific sources and targets to the module's src/targs
348 $(module
)_SRC
+= $($(module
)_
$1_SRC)
349 $(module
)_TRG
+= $($(module
)_
$1_TRG)
351 # Add these targest to the language's target list
353 $1_TARGETS += $($(module
)_TRG
)
355 # For every source file, create a build rule for it for our language
356 $(foreach src
,$(filter $(module
)/%,$($1_MOD_SRC)),\
357 $(eval
$(call SRC_LANG_RULE
,$(src
),$1))\
359 # For all the listed libraries, initialize the library, which will set up all of
360 # its local ($1_LIBS, etc) data
361 $(foreach lib
,$($1_LIBS),\
362 $(eval
$(call LANG_LIB_INIT
,$1,$(lib
),$1)))
363 # The initializer produces an ordered list of interlinked dependencies in groups
364 # by depth, so when reading backwards and compressing repeats we are left with a
365 # reliable build order for library interlinking. this awk program just looks
366 # through a list of space separated words backwards failing to print when it
367 # encounters a repeat
368 $1_LIBS := $(shell echo
"$($1_LIBS)" |
$(call AWK_REVERSE_SQUASH
))
369 #/LANG_INIT######################################################################
372 # The recursive library dependecy traversal constructs a tree ordered by nested
373 # dependency depth. when linking, this order is reversed.
374 # Initialize each language and look for its files
375 $(eval
$(foreach lang
,$(LANGS
),\
376 $(eval
$(call LANG_INIT
,$(lang
)))\
377 $(eval
$(lang
)_LIBS
:= $(shell echo
$($(lang
)_LIBS
) |
$(call AWK_REVERSE_SQUASH
)))\
380 # Create module object rules for each module
381 $(foreach module
,$(MODULES
),\
382 $(eval
$(call MODULE_ARCRULE
,$(module
)))\
385 # Create lang-specific rules for producing final (linked) targets
386 $(foreach lang
,$(LANGS
),\
387 $(foreach drvsrc
,$($(lang
)_DRV_SRC
),\
388 $(eval
$(call SRC_LANG_DRVRULE
,$(drvsrc
),$(lang
)))\
391 # Make any dirs that we're in charge of
395 # Source-to-source build rules
396 # Compilers that don't specify an output location can be made to change
397 # directories with 'cd' to the target file's directory, update the command line
398 # source files, and allow the compiler to output in its default current working
399 # directory 'cwd'. this is done by setting the $1_CHDIR flag to 't'
400 define SRCLANG_TRGLANG_BUILDRULE
=
401 %$($1_STEM:%=.
%).
$2$(if
$($1_DUP), %$($1_STEM:%=.
%).
$($1_DUP)): %.
$1
402 $(if
$($1_CHDIR),cd
$$(shell dirname
$$@
) && )$$($1_C) $$($1_FLAGS)$(if
$($1_CHDIR),\
403 $$(shell echo
$$@ | sed
-e
's@^.*/\([^\.]*\).*@\1@').
$1,\
406 # Create rules for all the source-to-source compilation, and poll all of our
407 # make targets while we're at it
408 $(foreach lang
,$(LANGS
),\
409 $(foreach srcl
,$($(lang
)_SRCL
),\
410 $(eval
$(call SRCLANG_TRGLANG_BUILDRULE
,$(srcl
),$(lang
)))\
411 $(eval MAKE_TARGETS
+= $($(srcl
)_TARGETS
))\
413 $(eval MAKE_TARGETS
+= $($(lang
)_TARGETS
))\
416 # Create driver rules for each driver we found, let users call make specifying
417 # the basename of the driver, and just route to the correct linkrule we made in
419 DRV_FNAMES
:= $(notdir $(DRV_SRC
))
420 .PHONY
: all $(basename $(DRV_NAMES
))
421 $(foreach fname
,$(DRV_FNAMES
),\
422 $(eval
$(basename $(fname
)): \
423 $(filter %/$(basename $(fname
))$($(lastword
$(subst .
, ,$(fname
))_OUT
)),$(DRV_TRG
)))\
425 all: $(basename $(DRV_FNAMES
))
428 clean: $(BUILTLIBS
:%=clean_
%)
429 rm -f
$(MAKE_TARGETS
)