Merge branch 'master' of krull:webcc
[henge/webcc.git] / make / lib / rules.mk
1 ################################################################################
2 # Desc: Make functions for creating rules (Make making Make)
3 # Author: Ken Grimes
4 # Date: 2016
5 ################################################################################
6 # This file consists of library functions for make-making-make functionality
7 ################################################################################
8
9 # A long function definition to grab deps from the source's compiler and route
10 # errors to /dev/null if the rule generator expresses bogus errors
11 define SRC_LANG_FLAGS_OPT_DEPS =
12 $(filter-out \ %:,$(shell $($2_C) $3 $4 -MG $1 2> /dev/null))
13 endef
14
15 #SRC_LANG_RULE###################################################################
16 # Given a source and a language, generate a rule to make the object. The second
17 # invocation of gcc per file lists only its local includes, and filters out
18 # anything already caught. Anything remaining should be considered to be
19 # available in the current working directory of the source file(i.e. if you
20 # '#include "something.h"' in 'mymodule/horseradish.c', you would expect to
21 # include 'mymodule/something.h'. this check lets that happen), otherwise it is
22 # some kind of user error #######################################################
23 define SRC_LANG_RULE =
24 $(if $($1),,$(eval $1 := t)\
25 $(eval MOD := $(filter $(MODULES),$(firstword $(subst /, ,$(subst $(SRC_DIR),,$(dir $1))))))\
26 $(eval FLG := $(MOD:%=-I$(SRC_DIR)/% ))\
27 $(if $(wildcard $1),
28 $(eval DEPS := $(filter-out \ %:,$(shell $($2_C) $(FLG) -M -MG $1)))\
29 $(eval DDEPS := $(filter $(DRIVER_DIR:$(SRC_DIR)/%=%/%),$(DEPS)))\
30 $(eval MDEPS := $(filter $(MODULES:%=%/%),$(DEPS)))\
31 $(eval DEPS := $(filter-out $(DDEPS) $(MDEPS),$(DEPS)))\
32 $(eval MDEPS := $(MDEPS:%=$(SRC_DIR)/%))\
33 $(eval DDEPS := $(DDEPS:%=$(SRC_DIR)/%))\
34 $(eval LOST := $(filter-out \ %: $(DRIVER_DIR:./%=%)/% $(MODULES:%=$(SRC_DIR:./%=%)/%/%),$(shell $($2_C) $(FLG) -MM -MG $1)))\
35 $(eval MDEPS += $(LOST:%=$(dir $1)%))\
36 $(eval MDEPS := $(shell echo $(MDEPS) | sed -e 's@[a-zA-Z0-9\-\+/]*\.\./include@$(LIBINC_DIR)@g'))\
37 $(eval ALLDEPS := $(DDEPS) $(MDEPS) $(DEPS)),\
38 $(error Cannot generate deps for: $1, file not found))\
39 $(eval SRC_OBJ := $(basename $1).$($2_OBJ))\
40 $(eval MAKE_TARGETS += $(SRC_OBJ))
41 # Assume that lost dependencies are in the folder with the source
42 $(foreach lost,$(LOST),
43 $(eval ALLDEPS := $(subst $(lost), $(dir $1)$(lost),$(ALLDEPS))))
44
45 # Find any deps that aren't built yet, which the compiler has flagged
46 # as missing, but which we know the expected location of libs for
47 # this language should have their includes in 'LIBINC_DIR'
48 $(eval BUILDDEPS := $(filter $($2_LIBS:%=%/%),$(ALLDEPS)))
49 $(eval ALLDEPS := $(filter-out $(BUILDDEPS),$(ALLDEPS)) $(BUILDDEPS:%=$(LIBINC_DIR)/%))
50 # Object for $1
51 $(SRC_OBJ): $(ALLDEPS)
52 $($2_C) $$($2_FLAGS) $(FLG) -c -o $$@ $1
53 $(if $($2_DBG),\
54 $(eval DBG_OBJ := $(dir $1).$($2_DBG)/$(basename $(notdir $1)).$($2_OBJ))\
55 $(if $(findstring $(dir $(DBG_OBJ)),$(MAKE_DIRS)),,\
56 $(eval MAKE_DIRS += $(dir $(DBG_OBJ))))
57 $(eval MAKE_TARGETS += $(DBG_OBJ))
58 # Object for $1 with $($2_DBG) symbols
59 $(DBG_OBJ): $(ALLDEPS) | $(dir $(DBG_OBJ))
60 $($2_C) $$($2_FLAGS) $(FLG) -Og -g$($2_DBG) -c -o $$@ $1
61 ))
62 #/SRC_LANG_RULE##################################################################
63 endef
64
65 #SRC_LANG_DRVRULE################################################################
66 # establish a build and link rule given a source driver and language ############
67 define SRC_LANG_DRVRULE =
68 $(eval DRIVER_NAME := $(basename $(notdir $1)))
69 $(eval DRIVER_TARG := $(1:$(SRC_DIR)/%.$2=$(ROOT_DIR)/%$($2_OUT)))
70 $(eval DRIVER_SOB := $(basename $1).$($2_OBJ))
71
72 # Generate a rule for the driver object
73 $(eval $(call SRC_LANG_RULE,$1,$2))
74
75 # Modules can be implicitly specified by a driver if its dependencies include
76 # ANY files from any module directory. The 'MDEPS' var is set by SRC_LANG_RULE
77 # and expresses the dependencies required to build the object file only.
78 $(foreach module_dep,$(MDEPS),
79 $(eval DRIVER_MODULES += $(firstword $(subst /, ,$(module_dep))))
80 )
81 $(eval DRIVER_MODULES := $(filter-out $(DRIVER_DIR) . ..,$(sort $(DRIVER_MODULES))))
82
83 # In the event that the driver developer does not wish to include a header to
84 # any internal modules, the developer may instead create a file with the same
85 # basename as the driver, but with the '.ld' suffix, which contains a space
86 # separated list of internal modules to link together during compilation
87 $(eval DRIVER_LDVALS := $(file <$(1:%.$2=%.ld)))
88 $(eval DRIVER_LFLAGS := $(filter -l%,$(DRIVER_LDVALS)))
89 $(eval DRIVER_MODULES += $(filter-out -l%,$(DRIVER_LDVALS)))
90 $(eval DRIVER_MODULES := $(sort $(DRIVER_MODULES)))
91 # List of module archives to link together during compilation
92 $(eval DRIVER_ARCHIVES := $(DRIVER_MODULES:%=$(SRC_DIR)/%.$($2_AROBJ)))
93 $(eval DRIVER_DEPS := $(DRIVER_SOB) $(DRIVER_ARCHIVES))
94 $(eval DRIVER_DBGARCHIVES := $(DRIVER_MODULES:%=$(SRC_DIR)/.$($2_DBG)/%.$($2_AROBJ)))
95 $(eval DRIVER_DBGDEPS := $(dir $(DRIVER_SOB)).$($2_DBG)/$(notdir $(DRIVER_SOB)))
96 $(eval DRIVER_DBGDEPS += $(DRIVER_DBGARCHIVES))
97
98 # If the compiler supports linking, distinguish static from dynamic links,
99 # otherwise set all libraries to static
100 $(if $($($2_C)_LD),\
101 $(eval STLIBS := $(filter-out $($($2_C)_LDLIBS),$($2_LIBS)))\
102 $(eval DLIBS := $(filter-out $(STLIBS),$($2_LIBS))),\
103 $(eval STLIBS := $($2_LIBS)))
104
105 # Directory setup
106 $(eval DRIVER_TARG_DIR := $(dir $(1:$(SRC_DIR)/%.$2=$(ROOT_DIR)/%)))
107 $(eval MAKE_DIRS += $(DRIVER_TARG_DIR))
108 $(if $($2_DBG),$(eval MAKE_DIRS += $(DRIVER_TARG_DIR).$($2_DBG)))
109
110 # Setup the sources for this object. A driver build rule does not include any
111 # compile steps at all, and so should not contain any sources that are,
112 # themselves, traditional "source files" like a language-compiled 'c' file. The
113 # driver depends on the existence of its equivalent object file (created through
114 # compilation), which is expected to contain a 'main' function (or entry for
115 # '_start'). This driver object is then simply linked to its libraries and
116 # module archives to create an executable binary in the output folder.
117 # ORDER MATTERS HERE, this is for the linker:
118 $(eval DRIVER_SRC := $(DRIVER_LFLAGS) $(DRIVER_DEPS))
119 $(eval DRIVER_DBGSRC := $(DRIVER_LFLAGS) $(DRIVER_DBGDEPS))
120 # Iterate through the list of libraries in our language and stack commands from
121 # left to right in the "sources" section of the LD command (or LD section of the
122 # commandline for the compiler), to preserve ordering and also ensure that it is
123 # linked correctly IN THAT ORDER (static vs dynamic).
124 $(foreach lib,$($2_LIBS),\
125 $(if $(findstring $(lib),$(STLIBS)),
126 # Evaluate static lib $(lib) and add it to the dependencies and sources of the rules we built
127 $(eval STATIC_SRC := $(LIB_DIR)/lib$(lib).$($2_AROBJ))\
128 $(eval STATIC_DBGSRC := $(LIB_DIR)/.$($2_DBG)/lib$(lib).$($2_AROBJ))\
129 $(eval DRIVER_SRC := $(STATIC_SRC) $(DRIVER_SRC))\
130 $(eval DRIVER_DBGSRC := $(STATIC_DBGSRC) $(DRIVER_DBGSRC))\
131 $(eval DRIVER_DEPS += $(STATIC_SRC))\
132 $(eval DRIVER_DBGDEPS += $(STATIC_DBGSRC)),
133 # Add dynamic lib to the sources with a link flag, in its correct ordering
134 $(eval DRIVER_SRC := -l$(lib) $(DRIVER_SRC))\
135 $(eval DRIVER_DBGSRC := -l$(lib) $(DRIVER_DBGSRC))\
136 ))
137
138 # Add the driver target to this language's targets for cleaning
139 $(eval SCRUB_TARGETS += $(DRIVER_TARG))
140 $(DRIVER_TARG): $(DRIVER_DEPS) | $(DRIVER_TARG_DIR)
141 $($2_C) $($2_FLAGS) $(DRIVER_SRC) -o $$@
142 # Output a rule for building with debug flags
143 $(eval DRIVER_DBGTARG := $(DRIVER_TARG_DIR)$(basename $(notdir $(DRIVER_TARG)))-d$($2_OUT))
144 $(eval SCRUB_TARGETS += $(DRIVER_DBGTARG))
145 $(eval MAKE_DIRS += $(DRIVER_TARG_DIR).$($2_DBG)/)
146 $(DRIVER_DBGTARG): $(DRIVER_DBGDEPS) | $(DRIVER_TARG_DIR)
147 $($2_C) $($2_FLAGS) $(DRIVER_DBGSRC) -o $$@
148 # Make a rule to run this driver after building
149 $(DRIVER_NAME)-run: $(DRIVER_TARG)
150 $(DRIVER_TARG)
151 $(DRIVER_NAME)-d: $(DRIVER_DBGTARG)
152
153 #/SRC_LANG_DRVRULE###############################################################
154 endef
155
156 #MODULE_ARCRULE##################################################################
157 # generate rule for turning an entire module's collection of binary objects into
158 # a single locally-linked (no external -L libs) object (for simplified linking
159 # modules as static libs).#######################################################
160 define MODULE_ARCRULE =
161 $(eval ARCDEPS := $(filter $(SRC_DIR)/$1/%.$(c_OBJ),$(foreach lang,$(LANGS),$($(lang)_MOD_TRG))))\
162 $(eval MAKE_TARGETS+= $(SRC_DIR)/$1.$(c_AROBJ))\
163
164 $(SRC_DIR)/$1.$(c_AROBJ): $(ARCDEPS)
165 $(c_AR) cr $$@ $$^
166 $(if $(c_DBG),
167 $(eval undefine DBGARCDEPS)
168 $(foreach arcdep,$(ARCDEPS),$(eval DBGARCDEPS += $(dir $(arcdep)).$(c_DBG)/$(notdir $(arcdep))))
169 $(eval MAKE_TARGETS+= .$(c_DBG)/$1.$(c_AROBJ))\
170
171 $(SRC_DIR)/.$(c_DBG)/$1.$(c_AROBJ): $(DBGARCDEPS) | .$(c_DBG)/
172 $(c_AR) cr $$@ $$^
173 )
174 #/MODULE_ARCRULE#################################################################
175 endef
176
177 # LANG_LIB_PARENT_BUILDRULE######################################################
178 # define rules for creating unknown libraries in the local build environment for
179 # either binary or bytecode representation given a library name and, optionally,
180 # a parent lib that included it for error reporting #############################
181 define LANG_LIB_PARENT_BUILDRULE =
182 $(eval CONF_FLAG := --prefix=$(abspath $(LIB_DIR)).trash)
183 $(eval CONF_FLAG += --includedir=$(abspath $(LIBINC_DIR)))
184 # Setup installation rule steps for this lib
185 $(eval
186 BUILDSTEPS := $(if $(AUTOGEN),$(AUTOGEN) && )
187 BUILDSTEPS += $(if $(CONFIGURE),$(CONFIGURE) $(CONF_FLAG) --libdir=$(abspath $(LIB_DIR)) && )
188 $(if $(MKCMD),\
189 BUILDSTEPS += $(MKCMD),
190 $(error $2.mk requires a valid MKCMD definition))
191 $(if $(MKINSTALL),\
192 INSTALLCMD := $(MKINSTALL),\
193 $(error $2.mk requires a valid MKINSTALL definition))
194 CLEANCMD := $(MKCLEAN)
195 )
196 # Construct the download method, or error if there isn't a valid one
197 $(if $(GITADDR),$(eval LIBDL := git clone $(GITADDR) $2),\
198 $(if $(HGADDR),$(eval LIBDL := hg clone $(HGADDR) $2),\
199 $(if $(CVSADDR),\
200 $(eval LIBDL := export CVSROOT=$(CVSADDR))\
201 $(eval LIBDL += && echo '$(CVSPASS)' > ~/.cvspass)\
202 $(eval LIBDL += && cvs $($2_CVSGET))
203 $(eval LIBDL += && $(if $(CVSPASS_BAK),echo $(CVSPASS_BAK) > ~/.cvspass,rm ~/.cvspass)),\
204 $(if $(WEBADDR),$(eval LIBDL := wget -O $(WEBTARG) $(WEBADDR)),\
205 $(eval $(error No way to download $2 needed by $3 for $1 language ))))))
206 # '$2' Download Rule
207 $(LIBDL_DIR)/$2:
208 mkdir -p $$@
209 cd $(LIBDL_DIR) && $(LIBDL) $(if $(WEBINIT),&& $(WEBINIT))
210 # '$2' Make and install rule, and ensure it isn't marked "clean"
211 $(LIB_DIR)/lib$2.$($1_AROBJ): $(LIBDEPS:%=$(LIB_DIR)/lib%.$($1_AROBJ)) | $(LIBDL_DIR)/$2
212 @mkdir -p $(LIB_DIR)
213 cd $(LIBDL_DIR)/$2 && export LD_RUN_PATH=$(LIBLDPATH) && $(BUILDSTEPS)
214 cd $(LIBDL_DIR)/$2 && $(INSTALLCMD)
215 @rm -f $(LIBDL_DIR)/$2.clean
216
217 # '$2' Rule for building under debug mode
218 $(if $($1_DBG),\
219 $(eval LIBDBG_DIR := $(LIB_DIR)/.$($1_DBG))\
220 $(eval $(if $(findstring $(LIBDBG_DIR),$(MAKE_DIRS)),,MAKE_DIRS += $(LIBDBG_DIR)))\
221 $(eval LIBDBG_TARG := $(LIBDBG_DIR)/lib$2.$($1_AROBJ))\
222 $(eval LIBDBG_DEPS := $(LIBDEPS:%=$(LIB_DIR)/.$($1_DBG)/lib%.$($1_AROBJ)))\
223 $(eval DBGBUILDSTEPS := $(if $(AUTOGEN),$(if $(AUTOGENDBG),$(AUTOGENDBG),$(AUTOGEN)) && ))\
224 $(eval DBGBUILDSTEPS += $(if $(CONFIGURE),$(if $(CONFIGUREDBG),$(CONFIGUREDBG),$(CONFIGURE)) $(CONF_FLAG) --libdir=$(abspath $(LIB_DIR))/.$($1_DBG)))\
225 $(eval DBGBUILDSTEPS += $(if $(MKDBGCMD),$(MKDBGCDM),$(MKCMD)))\
226
227 # '$2' Make and install rule for debugging
228 $(LIBDBG_TARG): $(LIBDBG_DEPS) | $(LIBDL_DIR)
229 @mkdir -p $(LIBDBG_DIR)
230 cd $(LIBDL_DIR)/$2 && export LD_RUN_PATH=$(LIBLDPATH:$(LIB_DIR)%=$(LIBDBG_DIR)) && $(DBGBUILDSTEPS)
231 cd $(LIBDL_DIR)/$2 && $(INSTALLCMD)
232 @rm -f $(LIBDL_DIR)/$2.clean
233 )
234
235 # '$2' Clean rule - drop a marker in the directory so we don't bother cleaning
236 # things that are clean... by default there is no real way of tracking this in a
237 # makefile
238 clean_$2:
239 $$(if $$(wildcard $(LIBDL_DIR)/$2),$$(if $$(wildcard $(LIBDL_DIR)/$2.clean),,\
240 cd $(LIBDL_DIR)/$2 && $(CLEANCMD) && touch $(abspath $(LIBDL_DIR))/$2.clean))
241
242 #/LANG_LIB_PARENT_BUILDRULE######################################################
243 endef
244
245
246 #LANG_LIB_INIT###################################################################
247 # For every library in this language, we need to determine how, and if, to link
248 # its data. This could potentially be done recursively as libraries have
249 # dependencies upon each other. We call a recursive macro here to expand out
250 # all the libs for this language, and then flatten the tree by removing
251 # duplicates with shell commands. We can't use make's 'sort' function because
252 # we actually care about the ordering so that interdependent libraries can link
253 # back up the tree ##############################################################
254 define LANG_LIB_INIT =
255 # Initialize per-library data, then import data from an accompanying file that
256 # can overwrite these vars
257 $(eval
258 # Implicit defaults for lib.mk files
259 AUTOGEN := ./autogen.sh
260 AUTOGENDBG := ./autogen.sh --enable-debug
261 CONFIGURE := ./configure
262 MKCMD := make -k
263 MKINSTALL := make -k install
264 MKCLEAN := make clean
265 MKDBGCMD := make -k
266 LIBDEPS :=
267 LIBLANGS := c cpp go
268 LIBLDPATH := $(if $($($1_C)_LD),$(if $(LD_LIBRARY_PATH),$(LD_LIBRARY_PATH):,/usr/lib:)$(abspath $(LIB_DIR)))
269 # One of these must be defined in the .mk file imported
270 $(foreach var, GITADDR WEBADDR HGADDR CVSADDR,\
271 $(eval undefine $(var)))
272 # Other, optional, variables to clear
273 $(foreach var, WEBTARG WEBINIT CVSGET CVSPASS CONFIGUREDBG,
274 $(eval undefine $(var)))
275 )
276 # If there is a .mk file for this lib, import it. Otherwise, we'll try to link
277 # it later, unless there is no linker for the language's compiler.
278 $(if $(wildcard $(CONF_DIR)/$2.mk),$(eval $(file <$(CONF_DIR)/$2.mk)),\
279 $(if $($($1_C)_LD),\
280 $(warning No $2.mk for lib$2, attempting to link from the system),\
281 $(error No $2.mk for lib$2 and '$($1_C)' is not configured for linking)))\
282
283 # Add dependencies we found (potentially recursively, ignore repeats until later
284 # so we can preserve ordering information for interdependency build ordering)
285 $(if $(LIBDEPS),\
286 $(eval $1_LIBS += $(LIBDEPS)))
287 # languages that can link this library (typically any lang that shares the
288 # object format (ELF by default, but this is compiler-dependent (e.g. emcc uses
289 # llvm bitcode, which is just an intermediary representation of data that can
290 # link to anything else statically, or as an archive)))
291 $(if $(findstring $1,$(LIBLANGS)),,\
292 $(error $2.mk must specifically set LIBLANGS := $1 if it is compatible with $1))
293 # Mark this lib as available for all its other compatible languages. This list
294 # is assumed to be sorted later, so we will ignore repeats for speed
295 $(foreach lang,$(LIBLANGS),\
296 $(if $(findstring $2,$($(lang)_LIBS)),,\
297 $(eval $(lang)_LIBS += $2)))
298 # Generate a build rule for this lib, unless it's already built
299 $(if $(findstring $2,$(BUILTLIBS)),,\
300 $(eval $(call LANG_LIB_PARENT_BUILDRULE,$1,$2,$3))\
301 $(eval BUILTLIBS += $2))
302 # Recurse this function for every libdep encountered
303 $(foreach libdep,$(LIBDEPS),\
304 $(call LANG_LIB_INIT,$1,$(libdep),$2))
305 #/LANG_LIB_INIT#################################################################
306 endef
307
308 # Initialize data for supported lanaguages ######################################
309 define LANG_INIT =
310 $(eval
311 $1_OBJ := $($($1_C)_OBJ)
312 $1_OUT := $($($1_C)_OUT)
313 $1_AROBJ := $($($1_C)_AROBJ)
314 $1_SOURCES := $(shell find $(SRC_DIR) -name "*.$1" -not -name ".*")
315 $1_DBG := $($($1_C)_DBG)
316 $1_AR := $($($1_C)_AR)
317 )
318 # Find save language-specific driver and module sources and targets
319 $(eval $1_DRV_SRC := $(filter $(DRIVER_DIR)/%,$($1_SOURCES)))
320 $(eval $1_DRV_TRG := $(patsubst $(SRC_DIR)/%.$1,$(ROOT_DIR)/%$($1_OUT),$($1_DRV_SRC)))
321 $(eval $1_MOD_SRC := $(filter-out $(DRIVER_DIR)/%,$($1_SOURCES)))
322 $(eval $1_MOD_TRG := $(addsuffix .$($1_OBJ),$(basename $($1_MOD_SRC))))
323 # Add those sources and targets to a language-irreverant var
324 $(eval
325 DRV_SRC += $($1_DRV_SRC)
326 DRV_TRG += $($1_DRV_TRG)
327 MOD_SRC += $($1_MOD_SRC)
328 MOD_TRG += $($1_MOD_TRG)
329 )\
330 # First, initialize language-module, module, and language relative data. Then,
331 # filter out each source file from the module and generate Dependency Rules and
332 # Module Object Rules for each of them.
333 $(foreach module,$(MODULES),
334 # Evaluate the module sources for this language
335 $(eval $(module)_$1_SRC := $(filter $(SRC_DIR)/$(module)/%,$($1_MOD_SRC)))
336 # Evaluate module targets for this language
337 $(eval $(module)_$1_TRG := $($(module)_$1_SRC:%.$1=%.$($1_OBJ)))
338 # Add these language-specific sources and targets to the module's src/targs
339 $(eval
340 $(module)_SRC += $($(module)_$1_SRC)
341 $(module)_TRG += $($(module)_$1_TRG)
342 )
343 # For every source file, create a build rule for it for our language
344 $(foreach src,$(filter $(SRC_DIR)/$(module)/%,$($1_MOD_SRC)),\
345 $(eval $(call SRC_LANG_RULE,$(src),$1))\
346 ))
347 # For all the listed libraries, initialize the library, which will set up all of
348 # its local ($1_LIBS, etc) data
349 $(foreach lib,$($1_LIBS),\
350 $(eval $(call LANG_LIB_INIT,$1,$(lib),$1)))
351 # The initializer produces an ordered list of interlinked dependencies in groups
352 # by depth, so when reading backwards and compressing repeats we are left with a
353 # reliable build order for library interlinking. this awk program just looks
354 # through a list of space separated words backwards failing to print when it
355 # encounters a repeat
356 $1_LIBS := $(shell echo "$($1_LIBS)" | $(call AWK_REVERSE_SQUASH))
357 #/LANG_INIT######################################################################
358 endef