+# LANG_LIB_PARENT_BUILDRULE######################################################
+# define rules for creating unknown libraries in the local build environment for
+# either binary or bytecode representation given a library name and, optionally,
+# a parent lib that included it for error reporting #############################
+define LANG_LIB_PARENT_BUILDRULE =
+$(eval CONF_FLAG := --prefix=$(abspath $(LIB_DIR)).trash)
+$(eval CONF_FLAG += --includedir=$(abspath $(LIBINC_DIR)))
+# Setup installation rule steps for this lib
+$(eval
+BUILDSTEPS := $(if $(AUTOGEN),$(AUTOGEN) && )
+BUILDSTEPS += $(if $(CONFIGURE),$(CONFIGURE) $(CONF_FLAG) --libdir=$(abspath $(LIB_DIR)) && )
+$(if $(MKCMD),\
+BUILDSTEPS += $(MKCMD),
+$(error $2.mk requires a valid MKCMD definition))
+$(if $(MKINSTALL),\
+INSTALLCMD := $(MKINSTALL),\
+$(error $2.mk requires a valid MKINSTALL definition))
+CLEANCMD := $(MKCLEAN)
+)
+# Construct the download method, or error if there isn't a valid one
+$(if $(GITADDR),$(eval LIBDL := git clone $(GITADDR) $2),\
+$(if $(HGADDR),$(eval LIBDL := hg clone $(HGADDR) $2),\
+$(if $(CVSADDR),\
+$(eval LIBDL := export CVSROOT=$(CVSADDR))\
+$(eval LIBDL += && echo '$(CVSPASS)' > ~/.cvspass)\
+$(eval LIBDL += && cvs $($2_CVSGET))
+$(eval LIBDL += && $(if $(CVSPASS_BAK),echo $(CVSPASS_BAK) > ~/.cvspass,rm ~/.cvspass)),\
+$(if $(WEBADDR),$(eval LIBDL := wget -O $(WEBTARG) $(WEBADDR)),\
+$(eval $(error No way to download $2 needed by $3 for $1 language ))))))
+# '$2' Download Rule
+$(LIBDL_DIR)/$2:
+ mkdir -p $$@
+ cd $(LIBDL_DIR) && $(LIBDL) $(if $(WEBINIT),&& $(WEBINIT))
+# '$2' Make and install rule, and ensure it isn't marked "clean"
+$(LIB_DIR)/lib$2.$($1_AROBJ): $(LIBDEPS:%=$(LIB_DIR)/lib%.$($1_AROBJ)) | $(LIBDL_DIR)/$2
+ @mkdir -p $(LIB_DIR)
+ cd $(LIBDL_DIR)/$2 && export LD_RUN_PATH=$(LIBLDPATH) && $(BUILDSTEPS)
+ cd $(LIBDL_DIR)/$2 && $(INSTALLCMD)
+ @rm -f $(LIBDL_DIR)/$2.clean
+
+# '$2' Rule for building under debug mode
+$(if $($1_DBG),\
+$(eval LIBDBG_DIR := $(LIB_DIR)/.$($1_DBG))\
+$(eval $(if $(findstring $(LIBDBG_DIR),$(MAKE_DIRS)),,MAKE_DIRS += $(LIBDBG_DIR)))\
+$(eval LIBDBG_TARG := $(LIBDBG_DIR)/lib$2.$($1_AROBJ))\
+$(eval LIBDBG_DEPS := $(LIBDEPS:%=$(LIB_DIR)/.$($1_DBG)/lib%.$($1_AROBJ)))\
+$(eval DBGBUILDSTEPS := $(if $(AUTOGEN),$(if $(AUTOGENDBG),$(AUTOGENDBG),$(AUTOGEN)) && ))\
+$(eval DBGBUILDSTEPS += $(if $(CONFIGURE),$(if $(CONFIGUREDBG),$(CONFIGUREDBG),$(CONFIGURE)) $(CONF_FLAG) --libdir=$(abspath $(LIB_DIR))/.$($1_DBG)))\
+$(eval DBGBUILDSTEPS += $(if $(MKDBGCMD),$(MKDBGCDM),$(MKCMD)))\
+
+# '$2' Make and install rule for debugging
+$(LIBDBG_TARG): $(LIBDBG_DEPS) | $(LIBDL_DIR)
+ @mkdir -p $(LIBDBG_DIR)
+ cd $(LIBDL_DIR)/$2 && export LD_RUN_PATH=$(LIBLDPATH:$(LIB_DIR)%=$(LIBDBG_DIR)) && $(DBGBUILDSTEPS)
+ cd $(LIBDL_DIR)/$2 && $(INSTALLCMD)
+ @rm -f $(LIBDL_DIR)/$2.clean
+)
+
+# '$2' Clean rule - drop a marker in the directory so we don't bother cleaning
+# things that are clean... by default there is no real way of tracking this in a
+# makefile
+clean_$2:
+ $$(if $$(wildcard $(LIBDL_DIR)/$2),$$(if $$(wildcard $(LIBDL_DIR)/$2.clean),,\
+ cd $(LIBDL_DIR)/$2 && $(CLEANCMD) && touch $(abspath $(LIBDL_DIR))/$2.clean))
+
+#/LANG_LIB_PARENT_BUILDRULE######################################################
+endef
+
+
+#LANG_LIB_INIT###################################################################
+# For every library in this language, we need to determine how, and if, to link
+# its data. This could potentially be done recursively as libraries have
+# dependencies upon each other. We call a recursive macro here to expand out
+# all the libs for this language, and then flatten the tree by removing
+# duplicates with shell commands. We can't use make's 'sort' function because
+# we actually care about the ordering so that interdependent libraries can link
+# back up the tree ##############################################################
+define LANG_LIB_INIT =
+# Initialize per-library data, then import data from an accompanying file that
+# can overwrite these vars
+$(eval
+# Implicit defaults for lib.mk files
+AUTOGEN := ./autogen.sh
+AUTOGENDBG := ./autogen.sh --enable-debug
+CONFIGURE := ./configure
+MKCMD := make -k
+MKINSTALL := make -k install
+MKCLEAN := make clean
+MKDBGCMD := make -k
+LIBDEPS :=
+LIBLANGS := c cpp go
+LIBLDPATH := $(if $($($1_C)_LD),$(if $(LD_LIBRARY_PATH),$(LD_LIBRARY_PATH):,/usr/lib:)$(abspath $(LIB_DIR)))
+# One of these must be defined in the .mk file imported
+$(foreach var, GITADDR WEBADDR HGADDR CVSADDR,\
+$(eval undefine $(var)))
+# Other, optional, variables to clear
+$(foreach var, WEBTARG WEBINIT CVSGET CVSPASS CONFIGUREDBG,
+$(eval undefine $(var)))
+)
+# Include and evaluate such that it may be called and evaluated for immediate
+# evaluation of its contents
+define EVALINCLUDE =
+$(eval include $2.mk)
+endef
+# If there is a .mk file for this lib, import it. Otherwise, we'll try to link
+# it later, unless there is no linker for the language's compiler.
+$(if $(wildcard $2.mk),$(eval $(file <$2.mk)),\
+$(if $($($1_C)_LD),\
+$(warning No $2.mk for lib$2, attempting to link from the system),\
+$(error No $2.mk for lib$2 and '$($1_C)' is not configured for linking)))\
+
+# Add dependencies we found (potentially recursively, ignore repeats until later
+# so we can preserve ordering information for interdependency build ordering)
+$(if $(LIBDEPS),\
+$(eval $1_LIBS += $(LIBDEPS)))
+# languages that can link this library (typically any lang that shares the
+# object format (ELF by default, but this is compiler-dependent (e.g. emcc uses
+# llvm bitcode, which is just an intermediary representation of data that can
+# link to anything else statically, or as an archive)))
+$(if $(findstring $1,$(LIBLANGS)),,\
+$(error $2.mk must specifically set LIBLANGS := $1 if it is compatible with $1))
+# Mark this lib as available for all its other compatible languages. This list
+# is assumed to be sorted later, so we will ignore repeats for speed
+$(foreach lang,$(LIBLANGS),\
+$(if $(findstring $2,$($(lang)_LIBS)),,\
+$(eval $(lang)_LIBS += $2)))
+# Generate a build rule for this lib, unless it's already built
+$(if $(findstring $2,$(BUILTLIBS)),,\
+$(eval $(call LANG_LIB_PARENT_BUILDRULE,$1,$2,$3))\
+$(eval BUILTLIBS += $2))
+# Recurse this function for every libdep encountered
+$(foreach libdep,$(LIBDEPS),\
+$(call LANG_LIB_INIT,$1,$(libdep),$2))
+#/LANG_LIB_INIT#################################################################
+endef
+
+# Initialize data for supported lanaguages ######################################
+define LANG_INIT =
+$(eval
+$1_OBJ := $($($1_C)_OBJ)
+$1_OUT := $($($1_C)_OUT)
+$1_AROBJ := $($($1_C)_AROBJ)
+$1_SOURCES := $(subst ./,,$(shell find -name "*.$1"))
+$1_DBG := $($($1_C)_DBG)
+$1_AR := $($($1_C)_AR)
+)
+# Find save language-specific driver and module sources and targets
+$(eval $1_DRV_SRC := $(filter $(DRIVER_DIR)/%,$($1_SOURCES)))
+$(eval $1_DRV_TRG := $(patsubst %.$1,$(ROOT_DIR)/%$($1_OUT),$($1_DRV_SRC)))
+$(eval $1_MOD_SRC := $(filter-out $(DRIVER_DIR)/%,$($1_SOURCES)))
+$(eval $1_MOD_TRG := $(subst .$1,.$($1_OBJ),$($1_MOD_SRC)))
+# Add those sources and targets to a language-irreverant var
+$(eval
+DRV_SRC += $($1_DRV_SRC)
+DRV_TRG += $($1_DRV_TRG)
+MOD_SRC += $($1_MOD_SRC)
+MOD_TRG += $($1_MOD_TRG)
+)\
+# First, initialize language-module, module, and language relative data. Then,
+# filter out each source file from the module and generate Dependency Rules and
+# Module Object Rules for each of them.
+$(foreach module,$(MODULES),
+# Evaluate the module sources for this language
+$(eval $(module)_$1_SRC := $(filter $(module)/%,$($1_MOD_SRC)))
+# Evaluate module targets for this language
+$(eval $(module)_$1_TRG := $($(module)_$1_SRC:%.$1=%.$($1_OBJ)))
+# Add these language-specific sources and targets to the module's src/targs
+$(eval
+$(module)_SRC += $($(module)_$1_SRC)
+$(module)_TRG += $($(module)_$1_TRG)
+)
+# For every source file, create a build rule for it for our language
+$(foreach src,$(filter $(module)/%,$($1_MOD_SRC)),\
+$(eval $(call SRC_LANG_RULE,$(src),$1))\
+))
+# For all the listed libraries, initialize the library, which will set up all of
+# its local ($1_LIBS, etc) data
+$(foreach lib,$($1_LIBS),\
+$(eval $(call LANG_LIB_INIT,$1,$(lib),$1)))
+# The initializer produces an ordered list of interlinked dependencies in groups
+# by depth, so when reading backwards and compressing repeats we are left with a
+# reliable build order for library interlinking. this awk program just looks
+# through a list of space separated words backwards failing to print when it
+# encounters a repeat
+$1_LIBS := $(shell echo "$($1_LIBS)" | $(call AWK_REVERSE_SQUASH))
+#/LANG_INIT######################################################################
+endef
+
+# The recursive library dependecy traversal constructs a tree ordered by nested
+# dependency depth. when linking, this order is reversed.
+# Initialize each language and look for its files
+$(eval $(foreach lang,$(LANGS),\
+$(eval $(call LANG_INIT,$(lang)))\
+$(eval $(lang)_LIBS := $(shell echo $($(lang)_LIBS) | $(call AWK_REVERSE_SQUASH)))\
+))
+
+# Create module object rules for each module
+$(foreach module,$(MODULES),\
+$(eval $(call MODULE_ARCRULE,$(module)))\
+)
+$(if $(c_DBG),$(eval MAKE_DIRS += .$(c_DBG)/))
+
+# Create lang-specific rules for producing final (linked) targets
+$(foreach lang,$(LANGS),\
+$(foreach drvsrc,$($(lang)_DRV_SRC),\
+$(eval $(call SRC_LANG_DRVRULE,$(drvsrc),$(lang)))\
+))
+
+# Create driver rules for each driver we found, let users call make specifying
+# the basename of the driver file, and just route to the correct linkrule we
+# made in the last paragraph
+DRV_FNAMES := $(notdir $(DRV_SRC))
+.PHONY: all $(basename $(DRV_NAMES))
+$(foreach fname,$(DRV_FNAMES),\
+$(eval $(basename $(fname)): \
+$(filter %/$(basename $(fname))$($(lastword $(subst ., ,$(fname))_OUT)),$(DRV_TRG)))\
+)
+all: $(basename $(DRV_FNAMES))
+ @echo Build Complete
+
+# Rule to make any dirs that we're in charge of
+$(MAKE_DIRS):
+ @mkdir -p $@