2d1796155eaafa42f0e2ab69ce31b03b6746641f
[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 # 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
19 c_SRCL := y
20 cpp_C := $(strip $(notdir $(CXX)))
21 cpp_FLAGS := $(strip $(CXXFLAGS)) $(c_FLAGS)
22 cpp_LIBS := $(c_LIBS)
23 go_C := gccgo
24 go_FLAGS := $(c_FLAGS)
25 # Source to source languages
26 y_C := bison
27 y_FLAGS := -d
28 y_STEM := tab
29 y_DUP := h
30 y_CHDIR := t
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).
34 cc_OBJ := o
35 cc_LD := $(LD)
36 cc_AR := $(AR)
37 cc_AROBJ := a
38 $(cpp_C)_LD := $(cc_LD)
39 gcc_OBJ := $(cc_OBJ)
40 gcc_LD := $(cc_LD)
41 gcc_AR := $(cc_AR)
42 gcc_AROBJ := $(cc_AROBJ)
43 emcc_OBJ := bc
44 emcc_AR := emar
45 emcc_AROBJ := $(emcc_OBJ) #emar is just a python script that reparses shit for emcc
46 emcc_OUT := .js
47 g++_OBJ := $(cc_OBJ)
48 em++_OBJ := $(emcc_OBJ)
49 em++_AR := $(emcc_AR)
50 em++_AROBJ := $(emcc_AROBJ)
51 em++_OUT := $(emcc_OUT)
52 gccgo_OBJ := o
53 gccgo_LD := $(cc_LD)
54 gccgo_AR := $(cc_AR)
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)
59 emcc_LDLIBS :=
60 g++_LDLIBS := $(cc_LDLIBS)
61 em++_LDLIBS :=
62 go_LDLIBS := $(cc_LDLIBS)
63 # Directories
64 # The directory in './' containing app drivers and compilation info
65 DRIVER_DIR := bin
66 ROOT_DIR := ..
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
78 # binaries.
79 MODULES := $(filter-out $(DRIVER_DIR),$(subst /,,$(shell ls -d */)))
80
81 # Given a source and a language, generate a rule to make the object. The second
82 # invocation of gcc per file lists only its local includes, and filters out
83 # anything already caught. Anything remaining should be considered to be
84 # available in the current working directory of the source file(i.e. if you
85 # '#include "something.h"' in 'mymodule/horseradish.c', you would expect to
86 # include 'mymodule/something.h'. this check lets that happen), otherwise it is
87 # some kind of user error
88 define SRC_LANG_RULE =
89 $(eval MOD := $(firstword $(subst /, ,$(dir $1))))\
90 $(eval DEPS := $(filter $(MODULES:%=%/%),$(shell $($2_C) -I$(MOD) -M -MG $1 2> /dev/null)))\
91 $(eval LOST := $(filter-out %: $(MODULES:%=%/%),$(shell $($2_C) -I$(MOD) -MM -MG $1 2> /dev/null)))\
92 $(eval DEPS += $(LOST:%=$(dir $1)%))\
93 $(if $($1),,$(eval $1 := t)\
94 $(basename $1).$($2_OBJ): $(DEPS)
95 $($2_C) $$($2_FLAGS) -I$(MOD) -c -o $$@ $1
96 )
97 endef
98
99 # establish a build and link rule given a source driver and language ############
100 define SRC_LANG_DRVRULE =
101 # turn the non-existent third argument into an identifier for our link data
102 $(eval 3 := $(notdir $(basename $1)))
103 # generate the dependecies list of the driver itself
104 $(eval $3_DRV_OBJ := $(1:%.$2=%.$($2_OBJ)))
105 $(eval $3_DRV_DEP := $(filter-out $3:,$(shell $($2_C) -M -MG $1)))
106 # Implicit and Manual LD Options to allow .ld files to specify manual linking to
107 # one of the built-in modules. Implicit LDOs are ones we can surmise from
108 # include rules generated with 'SRC_LANG_DEPS', but these are only caught if
109 # there was an explicitly included header, or some other file, within the source
110 # file of the driver to one of the internal modules. This practice is a
111 # reliable way to automatically link an internal module, but if desired an
112 # associated '.ld' file for the driver can contain a list of whitespace
113 # separated module names to ensure they link properly
114 $(eval MODULE_FILES := $(filter-out .% $(DRIVER_DIR)/%,$($3_DEP)))
115 $(eval MODULE_FILES := $(basename $(MODULE_FILES)))
116 $(foreach mfile,$(MODULE_FILES),\
117 $(eval $3_ILDO += $(firstword $(subst /, ,$(mfile)))))
118 # Find the .ld files, if present, and include their 'links' to our internal libs
119 $(if $(wildcard $(1:.$2=.ld)),\
120 $(eval $3_MLDO := $(basename $(strip $(file <$(1:.$2=.ld))))))
121 # Combine the link info into a single list '$3_LDO'
122 $(eval $3_LDO := $(patsubst %,%.$($($2_C)_AROBJ),$(sort $($3_ILDO) $($3_MLDO))))
123 # If the compiler supports linking, distinguish static from dynamic linking
124 $(if $($($2_C)_LD),\
125 $(eval $3_STLIBS := $(filter-out $($($2_C)_LDLIBS),$($2_LIBS)))\
126 $(eval $3_DLIBS := $(filter-out $($3_STLIBS),$($2_LIBS))),\
127 $(eval $3_STLIBS := $($2_LIBS)))
128 # Directory setup
129 $(eval $3_DIR := $(dir $(1:%.$2=$(ROOT_DIR)/%)))
130 $(eval MAKE_DIRS += $($3_DIR))
131 # Setup the sources for this object
132 $(eval $3_SRC := $($3_DRV_OBJ))
133 # Find the dependencies
134 $(eval $3_DEP := $(filter-out $1,$($3_DRV_DEPS)))
135 $(eval $3_DEP += $($3_SRC))
136 # Preserver ordering and build out the linking order (don't use -Wstatic/dynamic)
137 $(foreach lib,$($2_LIBS),\
138 $(if $(findstring $(lib),$($3_STLIBS)),\
139 $(eval $3_SRC := $(LIB_DIR)/lib$(lib).$($($2_C)_AROBJ) $($3_SRC)),\
140 $(eval $3_SRC := -l$(lib) $($3_SRC))))
141 $(eval $3_SRC += $($3_LDO))
142 # Output the driver object file rule
143 $(eval $2_TARGETS += $($3_DRV_OBJ))
144 # Filter the dependencies of the object to only include deps inside our modules
145 $($3_DRV_OBJ): $(filter $(MODULES:%=%/%),$($3_DRV_DEP))
146 $($2_C) $($2_FLAGS) -c -o $$@ $1
147 # Output the driver link rule
148 $(eval $2_TARGETS += $($3_DIR)$3$($2_OUT))
149 $($3_DIR)$3$($2_OUT): $($3_DEP) $($3_SRC) | $($3_DIR)
150 $($2_C) $($2_FLAGS) $($3_SRC) -o $$@
151 #/SRC_LANG_DRVRULE##############################################################
152 endef
153
154 # generate rule for turning an entire module's collection of binary objects into
155 # a single locally-linked (no external -L libs) object (for simplified linking
156 # modules as static libs).
157 define MODULE_ARCRULE =
158 $(eval c_TARGETS += $1.$($(c_C)_AROBJ))\
159 $(eval $1_ARCDEPS := $(filter $1/%,$(foreach lang,$(LANGS),$($(lang)_MOD_TRG))))\
160 $1.$($(c_C)_AROBJ): $($1_ARCDEPS) | $(LCLLIB_DIR)
161 $($(c_C)_AR) cr $$@ $$^
162 endef
163
164 # LANG_LIB_PARENT_BUILDRULE######################################################
165 # define rules for creating unknown libraries in the local build environment for
166 # either binary or bytecode representation given a library name and, optionally,
167 # a parent lib that included it for error reporting #############################
168 define LANG_LIB_PARENT_BUILDRULE =
169 # Setup installation rule steps for this lib
170 $(eval
171 BUILDSTEPS := $(if $(AUTOGEN),$(AUTOGEN) && )
172 BUILDSTEPS += $(if $(CONFIGURE),$(CONFIGURE) && )
173 $(if $(MKCMD),\
174 BUILDSTEPS += $(MKCMD),
175 $(error $2.mk requires a valid MKCMD definition))
176 $(if $(MKINSTALL),\
177 INSTALLCMD := $(MKINSTALL),\
178 $(error $2.mk requires a valid MKINSTALL definition))
179 CLEANCMD := $(MKCLEAN)
180 )
181 # Construct the download method, or error if there isn't a valid one
182 $(if $(GITADDR),$(eval LIBDL := git clone $(GITADDR) $2),\
183 $(if $(HGADDR),$(eval LIBDL := hg clone $(HGADDR) $2),\
184 $(if $(CVSADDR),$(shell echo $(CVSPASS) > ~/.cvspass)\
185 $(eval LIBDL := export CVSROOT=$(CVSADDR) && cvs $($2_CVSGET)),\
186 $(if $(WEBADDR),$(eval LIBDL := wget -O $(WEBTARG) $(WEBADDR)),\
187 $(eval $(error No way to download $2 needed by $3 for $1 language ))))))
188 # '$2' Download Rule
189 $(LIBDL_DIR)/$2:
190 mkdir -p $$@
191 cd $(LIBDL_DIR) && $(LIBDL) $(if $(WEBINIT),&& $(WEBINIT))
192 # '$2' Make and install rule, and ensure it isn't marked "clean"
193 $(LIB_DIR)/lib$2.$($1_AROBJ): $(LIBDEPS:%=$(LIB_DIR)/lib%.$($1_AROBJ)) | $(LIBDL_DIR)/$2
194 @mkdir -p $(LIB_DIR)
195 cd $(LIBDL_DIR)/$2 && export LD_RUN_PATH=$(LIBLDPATH) && $(BUILDSTEPS)
196 cd $(LIBDL_DIR)/$2 && $(INSTALLCMD)
197 @rm -f $(LIBDL_DIR)/$2.clean
198 # '$2' Clean rule - drop a marker in the directory so we don't bother cleaning
199 # things that are clean... by default there is no real way of tracking this in a
200 # makefile
201 clean_$2:
202 $$(if $$(wildcard $(LIBDL_DIR)/$2),$$(if $$(wildcard $(LIBDL_DIR)/$2.clean),,\
203 cd $(LIBDL_DIR)/$2 && $(CLEANCMD) && touch $(abspath $(LIBDL_DIR))/$2.clean))
204 #/LANG_LIB_PARENT_BUILDRULE######################################################
205 endef
206
207
208 #LANG_LIB_INIT###################################################################
209 # For every library in this language, we need to determine how, and if, to link
210 # its data. This could potentially be done recursively as libraries have
211 # dependencies upon each other. We call a recursive macro here to expand out
212 # all the libs for this language, and then flatten the tree by removing
213 # duplicates with shell commands. We can't use make's 'sort' function because
214 # we actually care about the ordering so that interdependent libraries can link
215 # back up the tree ##############################################################
216 define LANG_LIB_INIT =
217 # Initialize per-library data, then import data from an accompanying file that
218 # can overwrite these vars
219 $(eval
220 # Implicit defaults for lib.mk files
221 AUTOGEN := ./autogen.sh
222 CONFIGURE := ./configure
223 CONFIGURE += --prefix=$(abspath $(LIB_DIR)/.trash)
224 CONFIGURE += --includedir=$(abspath $(LIBINC_DIR))
225 CONFIGURE += --libdir=$(abspath $(LIB_DIR))
226 MKCMD := make -k
227 MKINSTALL := make -k install
228 MKCLEAN := make clean
229 LIBDEPS :=
230 LIBLANGS := c cpp go
231 LIBLDPATH := $(if $($($1_C)_LD),$(if $(LD_LIBRARY_PATH),$(LD_LIBRARY_PATH):,/usr/lib:)$(abspath $(LIB_DIR)))
232 # One of these must be defined in the .mk file imported
233 $(foreach var, GITADDR WEBADDR HGADDR CVSADDR,\
234 $(eval undefine $(var)))
235 # Other, optional, variables to clear
236 $(foreach var, WEBTARG WEBINIT CVSGET CVSPASS,
237 $(eval undefine $(var)))
238 )
239 # Include and evaluate such that it may be called and evaluated for immediate
240 # evaluation of its contents
241 define EVALINCLUDE =
242 $(eval include $2.mk)
243 endef
244 # If there is a .mk file for this lib, import it. Otherwise, we'll try to link
245 # it later, unless there is no linker for the language's compiler.
246 $(if $(wildcard $2.mk),$(eval $(call EVALINCLUDE)),\
247 $(if $($($1_C)_LD),\
248 $(warning No $2.mk for lib$2, linking from the system),\
249 $(error No $2.mk for lib$2 and '$($1_C)' is not configured for linking)))\
250
251 # Add dependencies we found (potentially recursively, ignore repeats until later
252 # so we can preserve ordering information for interdependency build ordering)
253 $(if $(LIBDEPS),\
254 $(eval $1_LIBS += $(LIBDEPS)))
255 # languages that can link this library (typically any lang that shares the
256 # object format (ELF by default, but this is compiler-dependent (e.g. emcc uses
257 # llvm bitcode, which is just an intermediary representation of data that can
258 # link to anything else statically, or as an archive)))
259 $(if $(findstring $1,$(LIBLANGS)),,\
260 $(error $2.mk must specifically set LIBLANGS := $1 if it is compatible with $1))
261 # Mark this lib as available for all its other compatible languages. This list
262 # is assumed to be sorted later, so we will ignore repeats for speed
263 $(foreach lang,$(LIBLANGS),\
264 $(if $(findstring $2,$($(lang)_LIBS)),,\
265 $(eval $(lang)_LIBS += $2)))
266 # Generate a build rule for this lib, unless it's already built
267 $(if $(findstring $2,$(BUILTLIBS)),,\
268 $(eval $(call LANG_LIB_PARENT_BUILDRULE,$1,$2,$3))\
269 $(eval BUILTLIBS += $2))
270 # Recurse this function for every libdep encountered
271 $(foreach libdep,$(LIBDEPS),\
272 $(call LANG_LIB_INIT,$1,$(libdep),$2))
273 #/LANG_LIB_INIT#################################################################
274 endef
275
276 # Initialize data for supported lanaguages ######################################
277 define LANG_INIT =
278 $(eval
279 # Initialize all relevant (although not necessarily used) language relative data
280 define $1_VARS_INIT =
281 $(eval
282 $1_OBJ := $($($1_C)_OBJ)
283 $1_OUT := $($($1_C)_OUT)
284 $1_AROBJ := $($($1_C)_AROBJ)
285 $1_SOURCES := $(subst ./,,$(shell find -name "*.$1"))
286 )
287 # For each source language that can compile to this language, add the expected
288 # result of such a transformation to the sources of this language. Then, if the
289 # source-to-source compiler also creates duplicate files of other formats
290 # (i.e. yacc/bison also produce accompanying .h files), add them to the list of
291 # source files that this makefile can generate ('$1_TARGETS'), to later be deleted
292 # during the clean rule.
293 $(foreach srcl,$($1_SRCL),\
294 $(eval $(srcl)_SOURCES := $(shell find -name "*.$(srcl)" | \
295 sed -e 's@^\(.*\).$(srcl)@\1$($(srcl)_STEM:%=.%).$1@g' -e 's@\./@@'))\
296 $1_SOURCES += $($(srcl)_SOURCES)
297 $(srcl)_TARGETS += $($(srcl)_SOURCES:%.$(srcl)=.$1)
298 $(foreach dup,$($(srcl)_DUP),\
299 $(srcl)_TARGETS += $($(srcl)_SOURCES:%.$1=%.$(dup))
300 ))
301 endef
302 )\
303 $(eval $($1_VARS_INIT))\
304 # Find save language-specific driver and module sources and targets
305 $(eval $1_DRV_SRC := $(filter $(DRIVER_DIR)/%,$($1_SOURCES)))
306 $(eval $1_DRV_TRG := $(patsubst %.$1,$(ROOT_DIR)/%$($1_OUT),$($1_DRV_SRC)))
307 $(eval $1_MOD_SRC := $(filter-out $(DRIVER_DIR)/%,$($1_SOURCES)))
308 $(eval $1_MOD_TRG := $(subst .$1,.$($1_OBJ),$($1_MOD_SRC)))
309 # Add those sources and targets to a language-irreverant var
310 $(eval
311 DRV_SRC += $($1_DRV_SRC)
312 DRV_TRG += $($1_DRV_TRG)
313 MOD_SRC += $($1_MOD_SRC)
314 MOD_TRG += $($1_MOD_TRG)
315 )\
316 # First, initialize language-module, module, and language relative data. Then,
317 # filter out each source file from the module and generate Dependency Rules and
318 # Module Object Rules for each of them.
319 $(foreach module,$(MODULES),
320 # Evaluate the module sources for this language
321 $(eval $(module)_$1_SRC := $(filter $(module)/%,$($1_MOD_SRC)))
322 # Evaluate module targets for this language
323 $(eval $(module)_$1_TRG := $($(module)_$1_SRC:%.$1=%.$($1_OBJ)))
324 # Add these language-specific sources and targets to the module's src/targs
325 $(eval
326 $(module)_SRC += $($(module)_$1_SRC)
327 $(module)_TRG += $($(module)_$1_TRG)
328 )
329 # Add these targest to the language's target list
330 $(eval
331 $1_TARGETS += $($(module)_TRG)
332 )
333 # For every source file, create a build rule for it for our language
334 $(foreach src,$(filter $(module)/%,$($1_MOD_SRC)),\
335 $(eval $(call SRC_LANG_RULE,$(src),$1))\
336 ))
337 # For all the listed libraries, initialize the library, which will set up all of
338 # its local ($1_LIBS, etc) data
339 $(foreach lib,$($1_LIBS),\
340 $(eval $(call LANG_LIB_INIT,$1,$(lib),$1)))
341 # The initializer produces an ordered list of interlinked dependencies in groups
342 # by depth, so when reading backwards and compressing repeats we are left with a
343 # reliable build order for library interlinking. this awk program just looks
344 # through a list of space separated words backwards failing to print when it
345 # encounters a repeat (which is suitable as a kind of ad-hoc make lisp function)
346 $1_LIBS := $(shell echo "$($1_LIBS)" | awk \
347 '
348 { for(i=NF;i>0;i--)
349 printf (!a[$$i]++) ? $$i FS : "";
350 i=split("",a);
351 print ""
352 }
353 '
354 )
355 #/LANG_INIT######################################################################
356 endef
357
358 # The following awk program reverses the order of a list while also removing
359 # duplicate entries. The effect of this when run on the dependency tree is that
360 # it will remove duplicates occurring in reverse order, allowing the most deeply
361 # nested libraries to be built, and linked, first.
362 define AWK_REVERSE_SQUASH =
363 awk \
364 '
365 BEGIN { OFS = " "; ORS = " " }
366 { for (i=NF; i>1; i--)
367 printf("%s ",$$i);
368 print $$1;
369 }
370 '
371 endef
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)))))
378
379 # Create module object rules for each module
380 $(foreach module,$(MODULES),\
381 $(eval $(call MODULE_ARCRULE,$(module)))\
382 )
383
384 # Create lang-specific rules for producing final (linked) targets
385 $(foreach lang,$(LANGS),\
386 $(foreach drvsrc,$($(lang)_DRV_SRC),\
387 $(eval $(call SRC_LANG_DRVRULE,$(drvsrc),$(lang)))\
388 ))
389
390 # Make any dirs that we're in charge of
391 $(MAKE_DIRS):
392 @mkdir -p $@
393
394 # Source-to-source build rules
395 # Compilers that don't specify an output location can be made to change
396 # directories with 'cd' to the target file's directory, update the command line
397 # source files, and allow the compiler to output in its default current working
398 # directory 'cwd'. this is done by setting the $1_CHDIR flag to 't'
399 define SRCLANG_TRGLANG_BUILDRULE =
400 %$($1_STEM:%=.%).$2$(if $($1_DUP), %$($1_STEM:%=.%).$($1_DUP)): %.$1
401 $(if $($1_CHDIR),cd $$(shell dirname $$@) && )$$($1_C) $$($1_FLAGS)$(if $($1_CHDIR),\
402 $$(shell echo $$@ | sed -e 's@^.*/\([^\.]*\).*@\1@').$1,\
403 $$<)
404 endef
405 # Create rules for all the source-to-source compilation, and poll all of our
406 # make targets while we're at it
407 $(foreach lang,$(LANGS),\
408 $(foreach srcl,$($(lang)_SRCL),\
409 $(eval $(call SRCLANG_TRGLANG_BUILDRULE,$(srcl),$(lang)))\
410 $(eval MAKE_TARGETS += $($(srcl)_TARGETS))\
411 )\
412 $(eval MAKE_TARGETS += $($(lang)_TARGETS))\
413 )
414
415 # Create driver rules for each driver we found, let users call make specifying
416 # the basename of the driver, and just route to the correct linkrule we made in
417 # the last paragraph
418 DRV_FNAMES := $(notdir $(DRV_SRC))
419 .PHONY: all $(basename $(DRV_NAMES))
420 $(foreach fname,$(DRV_FNAMES),\
421 $(eval $(basename $(fname)): \
422 $(filter %/$(basename $(fname))$($(lastword $(subst ., ,$(fname))_OUT)),$(DRV_TRG)))\
423 )
424 all: $(basename $(DRV_FNAMES))
425 @echo Build Complete
426
427 clean: $(BUILTLIBS:%=clean_%)
428 rm -f $(MAKE_TARGETS)
429
430 scrub: clean
431 rm -Rf $(MAKE_DIRS)