cleanup pass 1
authorken <ken@mihrtec.com>
Sat, 24 Sep 2016 22:01:05 +0000 (15:01 -0700)
committerken <ken@mihrtec.com>
Sat, 24 Sep 2016 22:01:05 +0000 (15:01 -0700)
src/Makefile

index 6f4a660..2d17961 100644 (file)
@@ -15,7 +15,7 @@ LANGS      := c cpp go
 # Language-specific compilers and flags passed in from environment
 c_C        := $(strip $(notdir $(CC)))
 c_FLAGS    := $(strip $(CFLAGS)) -I.
-c_LIBS     := SDL2_ttf SDL2_net SDL2_image SDL2 wolfssl
+c_LIBS     := SDL2 wolfssl
 c_SRCL     := y
 cpp_C      := $(strip $(notdir $(CXX)))
 cpp_FLAGS  := $(strip $(CXXFLAGS)) $(c_FLAGS)
@@ -54,7 +54,7 @@ gccgo_LD   := $(cc_LD)
 gccgo_AR   := $(cc_AR)
 gccgo_AROBJ := $(cc_AROBJ)
 # Shell functions to determine what libraries can be linked by the compiler
-cc_LDLIBS   := $(shell ls /usr/lib | grep ".so" | sed -e 's@^.*lib\([_\+a-zA-Z0-9\-]*\)\..*@\1@g')
+cc_LDLIBS   := $(shell ls /usr/lib | grep ".o" | sed -e 's@^.*lib\([_\+a-zA-Z0-9\-]*\)\..*@\1@g')
 gcc_LDLIBS  := $(cc_LDLIBS)
 emcc_LDLIBS := 
 g++_LDLIBS  := $(cc_LDLIBS)
@@ -78,39 +78,31 @@ c_OBJ      := $($(c_C)_OBJ)
 # binaries.
 MODULES    := $(filter-out $(DRIVER_DIR),$(subst /,,$(shell ls -d */)))
 
-# determine the dependencies of a given source and language, but route link
-# issues to /dev/null since we will generate them later
-# can pass an optional third argument for additional opts
-define SRC_LANG_DEPS =
-$(shell $($2_C) -MM -MG $(if $3,-I$3) $($2_FLAGS) $1 2> /dev/null\
-| sed -e 's@^.*\.o: @@' -e 's@\\@@')
-endef
-
-# create a list of module object archives associated with the given files,
-# if any, filtering out any drivers or non-module objects
-define FLIST_MODULEOBJS =
-$(sort $(shell echo $(filter-out .%,$(filter-out $(DRIVER_DIR)/%,$1))\
- | sed -e 's@^\([a-Z\-\+]*\)/*@\1.$(c_OBJ)@g' ))
-endef
-
-# establish a dependency rule given a source and language.  if passed a
-# directory name as the third argument, will prefix all header files in the
-# dependency with the local lib
-define SRC_LANG_DEPRULE =
-$(eval $1_$2_DEPS := $(call SRC_LANG_DEPS,$1,$2,$3))
-$(foreach hdr,$(filter %.h,$($1_$2_DEPS)),\
-$(if $(word 2,$(subst /, ,$(hdr))),,\
-$(eval $1_$2_DEPS := $($1_$2_DEPS:$(hdr)=$3/$(hdr)))\
-))
-$(basename $1).$($2_OBJ): $($1_$2_DEPS)
+# Given a source and a language, generate a rule to make the object.  The second
+# invocation of gcc per file lists only its local includes, and filters out
+# anything already caught.  Anything remaining should be considered to be
+# available in the current working directory of the source file(i.e. if you
+# '#include "something.h"' in 'mymodule/horseradish.c', you would expect to
+# include 'mymodule/something.h'.  this check lets that happen), otherwise it is
+# some kind of user error
+define SRC_LANG_RULE =
+$(eval MOD  := $(firstword $(subst /, ,$(dir $1))))\
+$(eval DEPS := $(filter $(MODULES:%=%/%),$(shell $($2_C) -I$(MOD) -M -MG $1 2> /dev/null)))\
+$(eval LOST := $(filter-out %: $(MODULES:%=%/%),$(shell $($2_C) -I$(MOD) -MM -MG $1 2> /dev/null)))\
+$(eval DEPS += $(LOST:%=$(dir $1)%))\
+$(if $($1),,$(eval $1 := t)\
+$(basename $1).$($2_OBJ): $(DEPS)
+       $($2_C) $$($2_FLAGS) -I$(MOD) -c -o $$@ $1
+)
 endef
 
 # establish a build and link rule given a source driver and language ############
-define SRC_LANG_LINKRULE =
+define SRC_LANG_DRVRULE =
 # turn the non-existent third argument into an identifier for our link data
 $(eval 3 := $(notdir $(basename $1)))
 # generate the dependecies list of the driver itself
-$(eval $3_DRV_DEPS := $(filter-out $1,$(call SRC_LANG_DEPS,$1,$2)))
+$(eval $3_DRV_OBJ := $(1:%.$2=%.$($2_OBJ)))
+$(eval $3_DRV_DEP := $(filter-out $3:,$(shell $($2_C) -M -MG $1)))
 # Implicit and Manual LD Options to allow .ld files to specify manual linking to
 # one of the built-in modules.  Implicit LDOs are ones we can surmise from
 # include rules generated with 'SRC_LANG_DEPS', but these are only caught if
@@ -132,71 +124,83 @@ $(eval $3_LDO  := $(patsubst %,%.$($($2_C)_AROBJ),$(sort $($3_ILDO) $($3_MLDO)))
 $(if $($($2_C)_LD),\
 $(eval $3_STLIBS   := $(filter-out $($($2_C)_LDLIBS),$($2_LIBS)))\
 $(eval $3_DLIBS    := $(filter-out $($3_STLIBS),$($2_LIBS))),\
-$(eval $3_STLIBS   := $(2_LIBS)))
+$(eval $3_STLIBS   := $($2_LIBS)))
 # Directory setup
 $(eval $3_DIR      := $(dir $(1:%.$2=$(ROOT_DIR)/%)))
 $(eval MAKE_DIRS   += $($3_DIR))
 # Setup the sources for this object
-$(eval $3_SRC      := $($3_LDO))
-$(eval $3_SRC      += $1)
+$(eval $3_SRC      := $($3_DRV_OBJ))
 # Find the dependencies
-$(eval $3_DEP      := $($3_DRV_DEPS))
+$(eval $3_DEP      := $(filter-out $1,$($3_DRV_DEPS)))
 $(eval $3_DEP      += $($3_SRC))
 # Preserver ordering and build out the linking order (don't use -Wstatic/dynamic)
 $(foreach lib,$($2_LIBS),\
 $(if $(findstring $(lib),$($3_STLIBS)),\
 $(eval $3_SRC      := $(LIB_DIR)/lib$(lib).$($($2_C)_AROBJ) $($3_SRC)),\
 $(eval $3_SRC      := -l$(lib) $($3_SRC))))
-$($3_DIR)$3$($2_OUT): $($3_DEP) | $($3_DIR)
+$(eval $3_SRC      += $($3_LDO))
+# Output the driver object file rule
+$(eval $2_TARGETS += $($3_DRV_OBJ))
+# Filter the dependencies of the object to only include deps inside our modules
+$($3_DRV_OBJ): $(filter $(MODULES:%=%/%),$($3_DRV_DEP))
+       $($2_C) $($2_FLAGS) -c -o $$@ $1
+# Output the driver link rule
+$(eval $2_TARGETS += $($3_DIR)$3$($2_OUT))
+$($3_DIR)$3$($2_OUT): $($3_DEP) $($3_SRC) | $($3_DIR)
        $($2_C) $($2_FLAGS) $($3_SRC) -o $$@
-#/SRC_LANG_LINKRULE##############################################################
+#/SRC_LANG_DRVRULE##############################################################
 endef
 
 # generate rule for turning an entire module's collection of binary objects into
 # a single locally-linked (no external -L libs) object (for simplified linking
 # modules as static libs).
 define MODULE_ARCRULE =
-$(eval $1_ARCDEPS := $(filter $1/%,$(foreach lang,$(LANGS),$($(lang)_MOD_TRG))))
+$(eval c_TARGETS += $1.$($(c_C)_AROBJ))\
+$(eval $1_ARCDEPS := $(filter $1/%,$(foreach lang,$(LANGS),$($(lang)_MOD_TRG))))\
 $1.$($(c_C)_AROBJ): $($1_ARCDEPS) | $(LCLLIB_DIR)
        $($(c_C)_AR) cr $$@ $$^
 endef
 
-# define an object creation rule given a module and language
-define MODULE_LANG_OBJRULE =
-$1/%.$($2_OBJ): $1/%.$2
-       $$($2_C) -I$1 $$(filter-out -l% %.a,$$($2_FLAGS)) $$< -c -o $$@
-endef
-
 # 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 =
+# Setup installation rule steps for this lib
+$(eval
+BUILDSTEPS := $(if $(AUTOGEN),$(AUTOGEN) && )
+BUILDSTEPS += $(if $(CONFIGURE),$(CONFIGURE) && )
+$(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 $($2_GITADDR),$(eval LIBDL := git clone $($2_GITADDR) $2),\
-$(if $($2_HGADDR),$(eval LIBDL  := hg clone $($2_HGADDR) $2),\
-$(if $($2_CVSADDR),$(shell echo $($2_CVSPASS) >  ~/.cvspass)\
-$(eval LIBDL := export CVSROOT=$($2_CVSADDR) && cvs $($2_CVSGET)),\
-$(if $($2_WEBADDR),$(eval LIBDL := wget -O $($2_WEBTARG) $($2_WEBADDR)),\
+$(if $(GITADDR),$(eval LIBDL := git clone $(GITADDR) $2),\
+$(if $(HGADDR),$(eval LIBDL  := hg clone $(HGADDR) $2),\
+$(if $(CVSADDR),$(shell echo $(CVSPASS) >  ~/.cvspass)\
+$(eval LIBDL := export CVSROOT=$(CVSADDR) && cvs $($2_CVSGET)),\
+$(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 $($2_WEBINIT),&& $($2_WEBINIT))
-# '$2' Make and install rule
-$(LIB_DIR)/lib$2.so: $(LIBDL_DIR)/$2 $($2_LIBDEPS:%=$(LIB_DIR)/lib%.so)
+       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 && $($2_MAKE)
-       cd $(LIBDL_DIR)/$2 && $($2_INSTALL)
-
-# each library generated will be included in the libs available list, removed
-# from the missing libs list, and passed by -l'libname' to the language flags
-$(foreach lang,$(LIBLANGS),\
-$(eval $(lang)_LIBS_AVAILABLE += $2)\
-$(eval $(lang)_MISSING_LIBS   := $(filter-out $2,$($(lang)_MISSING_LIBS)))\
-$(eval $(lang)_SOBS           += $(2:%=$(LIB_DIR)/lib%.so))\
-)
+       cd $(LIBDL_DIR)/$2 && export LD_RUN_PATH=$(LIBLDPATH) && $(BUILDSTEPS)
+       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
 
@@ -210,25 +214,30 @@ endef
 # 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
-$(eval AUTOGEN   := ./autogen.sh)
-$(eval CONFIGURE := ./configure)
-$(eval CONFIGURE += --prefix=$(abspath $(LIB_DIR)/.trash))  
-$(eval CONFIGURE += --includedir=$(abspath $(LIBINC_DIR)))
-$(eval CONFIGURE += --libdir=$(abspath $(LIB_DIR)))
-$(eval MKCMD     := make -k)
-$(eval MKINSTALL := make -k install)
-$(eval MKCLEAN   := make clean)
-$(eval LIBDEPS   :=)
+AUTOGEN   := ./autogen.sh
+CONFIGURE := ./configure
+CONFIGURE += --prefix=$(abspath $(LIB_DIR)/.trash)
+CONFIGURE += --includedir=$(abspath $(LIBINC_DIR))
+CONFIGURE += --libdir=$(abspath $(LIB_DIR))
+MKCMD     := make -k
+MKINSTALL := make -k install
+MKCLEAN   := make clean
+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
-$(eval undefine WEBTARG) # File 'WEBADDR' is stored in
-$(eval undefine WEBINIT) # Method of extracting 'WEBTARG', if necessary
-$(eval undefine CVSGET)  # checkout command
-$(eval undefine CVSPASS) # auto-put into ~/.cvspass to login automatically
-# Sneaky expansion to call a file inclusion with unelevated (eval'ed) text
+$(foreach var, WEBTARG WEBINIT CVSGET CVSPASS,
+$(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
@@ -237,54 +246,44 @@ endef
 $(if $(wildcard $2.mk),$(eval $(call EVALINCLUDE)),\
 $(if $($($1_C)_LD),\
 $(warning No $2.mk for lib$2, linking from the system),\
-$(error No $2.mk for lib$2 and '$($1_C)' is not configured for linking)))
-
-# Store lib specific build info
-$(foreach var,GITADDR WEBADDR HGADDR CVSADDR WEBTARG WEBINIT CVSGET CVSPASS,\
-$(eval $2_$(var)  := $($(var))))
-$(eval $2_MAKE    := $(if $(AUTOGEN),$(AUTOGEN) && ))
-$(eval $2_MAKE    += $(if $(CONFIGURE),$(CONFIGURE) && ))
-$(if $(MKCMD),\
-$(eval $2_MAKE    += $(MKCMD)),\
-$(error $2.mk requires a valid MKCMD definition))
-$(if $(MKINSTALL),\
-$(eval $2_INSTALL := $(MKINSTALL)),\
-$(error $2.mk requires a valid MKINSTALL definition))
-$(eval $2_CLEAN   := $(MKCLEAN))
-$(eval $2_DEPS    := $(LIBDEPS))
+$(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)))
-
+$(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)))
-$(eval LIBLANGS ?= c cpp go)
 $(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)\
-))
-
+$(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),\
-$(eval $(call LANG_LIB_INIT,$1,$(libdep)))\
-)
-#/LANG_LIB_INIT##################################################################
+$(call LANG_LIB_INIT,$1,$(libdep),$2))
+#/LANG_LIB_INIT#################################################################
 endef
 
 # Initialize data for supported lanaguages ######################################
 define LANG_INIT =
+$(eval 
 # Initialize all relevant (although not necessarily used) language relative data
-$(eval $1_OBJ     := $($($1_C)_OBJ))
-$(eval $1_OUT     := $($($1_C)_OUT))
-$(eval $1_SOURCES := $(subst ./,,$(shell find -name "*.$1")))
+define $1_VARS_INIT =
+$(eval
+$1_OBJ     := $($($1_C)_OBJ)
+$1_OUT     := $($($1_C)_OUT)
+$1_AROBJ   := $($($1_C)_AROBJ)
+$1_SOURCES := $(subst ./,,$(shell find -name "*.$1"))
+)
 # For each source language that can compile to this language, add the expected
 # result of such a transformation to the sources of this language.  Then, if the
 # source-to-source compiler also creates duplicate files of other formats
@@ -294,49 +293,57 @@ $(eval $1_SOURCES := $(subst ./,,$(shell find -name "*.$1")))
 $(foreach srcl,$($1_SRCL),\
 $(eval $(srcl)_SOURCES := $(shell find -name "*.$(srcl)" | \
 sed -e 's@^\(.*\).$(srcl)@\1$($(srcl)_STEM:%=.%).$1@g' -e 's@\./@@'))\
-$(eval $1_SOURCES += $($(srcl)_SOURCES))\
-$(eval $(srcl)_TARGETS += $($(srcl)_SOURCES:%.$(srcl)=.$1))\
+$1_SOURCES += $($(srcl)_SOURCES)
+$(srcl)_TARGETS += $($(srcl)_SOURCES:%.$(srcl)=.$1)
 $(foreach dup,$($(srcl)_DUP),\
-$(eval $(srcl)_TARGETS += $($(srcl)_TARGETS:%.$1=%.$(dup)))\
+$(srcl)_TARGETS += $($(srcl)_SOURCES:%.$1=%.$(dup))
 ))
-
+endef
+)\
+$(eval $($1_VARS_INIT))\
+# Find save language-specific driver and module sources and targets
 $(eval $1_DRV_SRC := $(filter $(DRIVER_DIR)/%,$($1_SOURCES)))
-$(eval DRV_SRC    += $($1_DRV_SRC))
 $(eval $1_DRV_TRG := $(patsubst %.$1,$(ROOT_DIR)/%$($1_OUT),$($1_DRV_SRC)))
-$(eval DRV_TRG    += $($1_DRV_TRG))
 $(eval $1_MOD_SRC := $(filter-out $(DRIVER_DIR)/%,$($1_SOURCES)))
-$(eval MOD_SRC    += $($1_MOD_SRC))
 $(eval $1_MOD_TRG := $(subst .$1,.$($1_OBJ),$($1_MOD_SRC)))
-$(eval MOD_TRG    += $($1_MOD_TRG))
-# Generate a list of shared object files this language has access to
-$(eval $1_SOBS    := $(wildcard $(LIB_DIR)/lib*.so))
-
+# 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),\
-$(eval $(module)_$1_SRC := $(filter $(module)/%,$($1_MOD_SRC)))\
-$(eval $(module)_$1_TRG := $(filter $(module)/%,$($1_MOD_TRG)))\
-$(eval $(module)_SRC += $(module)_$1_SRC)\
-$(eval $(module)_TRG += $($(module)_$1_TRG))\
-$(eval $1_TRG        += $($(module)_TRG))\
-$(eval \
+$(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)
+)
+# Add these targest to the language's target list
+$(eval
+$1_TARGETS    += $($(module)_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_DEPRULE,$(src),$1,$(module)))\
-$(eval $(call MODULE_LANG_OBJRULE,$(module),$1)))\
+$(eval $(call SRC_LANG_RULE,$(src),$1))\
 ))
-$(eval $1_TARGETS += $($1_TRG))
-
 # For all the listed libraries, initialize the library, which will set up all of
 # its local ($1_LIBS, etc) data
 $(foreach lib,$($1_LIBS),\
-$(call LANG_LIB_INIT,$1,$(lib)))
+$(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 (which is suitable as a kind of ad-hoc make lisp function)
-$(eval $1_LIBS := $(shell echo "$($1_LIBS)" | awk \
+$1_LIBS := $(shell echo "$($1_LIBS)" | awk \
 '
 { for(i=NF;i>0;i--)
     printf (!a[$$i]++) ? $$i FS : "";
@@ -344,12 +351,14 @@ $(eval $1_LIBS := $(shell echo "$($1_LIBS)" | awk \
   print ""
 }
 '
-))
+)
 #/LANG_INIT######################################################################
 endef
 
 # The following awk program reverses the order of a list while also removing
-# duplicate entries.
+# duplicate entries.  The effect of this when run on the dependency tree is that
+# it will remove duplicates occurring in reverse order, allowing the most deeply
+# nested libraries to be built, and linked, first.
 define AWK_REVERSE_SQUASH =
 awk \
 '
@@ -363,18 +372,11 @@ 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
-$(foreach lang,$(LANGS),\
+$(eval $(foreach lang,$(LANGS),\
 $(eval $(call LANG_INIT,$(lang)))\
-$(eval $(lang)_LIBS := $(shell echo $($(lang)_LIBS) | $(call AWK_REVERSE_SQUASH))))
-
-# Generate rules for making missing libs
-$(foreach lang,$(LANGS),\
-$(foreach mlib,$($(lang)_MISSING_LIBS),\
-$(eval $(call LANG_LIB_PARENT_BUILDRULE,$(lang),$(mlib),root))\
-))
+$(eval $(lang)_LIBS := $(shell echo $($(lang)_LIBS) | $(call AWK_REVERSE_SQUASH)))))
 
-# Create module object rules for each module and scan for interpreted code
-# sources (yacc, etc)
+# Create module object rules for each module
 $(foreach module,$(MODULES),\
 $(eval $(call MODULE_ARCRULE,$(module)))\
 )
@@ -382,26 +384,13 @@ $(eval $(call MODULE_ARCRULE,$(module)))\
 # Create lang-specific rules for producing final (linked) targets
 $(foreach lang,$(LANGS),\
 $(foreach drvsrc,$($(lang)_DRV_SRC),\
-$(eval $(call SRC_LANG_DEPRULE,$(drvsrc),$(lang)))\
-$(eval $(call SRC_LANG_LINKRULE,$(drvsrc),$(lang)))\
+$(eval $(call SRC_LANG_DRVRULE,$(drvsrc),$(lang)))\
 ))
 
 # Make any dirs that we're in charge of
 $(MAKE_DIRS):
        @mkdir -p $@
 
-# Create driver rules for each driver we found, let users call make specifying
-# the basename of the driver, 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
-
 # Source-to-source build rules
 # Compilers that don't specify an output location can be made to change
 # directories with 'cd' to the target file's directory, update the command line
@@ -413,13 +402,30 @@ define SRCLANG_TRGLANG_BUILDRULE =
 $$(shell echo $$@ | sed -e 's@^.*/\([^\.]*\).*@\1@').$1,\
 $$<)
 endef
-$(foreach lang,$(LANGS),$(if $($(lang)_SRCL),$(foreach srcl,$($(lang)_SRCL),\
-$(eval $(call SRCLANG_TRGLANG_BUILDRULE,$(srcl),$(lang))))))
+# Create rules for all the source-to-source compilation, and poll all of our
+# make targets while we're at it
+$(foreach lang,$(LANGS),\
+$(foreach srcl,$($(lang)_SRCL),\
+$(eval $(call SRCLANG_TRGLANG_BUILDRULE,$(srcl),$(lang)))\
+$(eval MAKE_TARGETS += $($(srcl)_TARGETS))\
+)\
+$(eval MAKE_TARGETS += $($(lang)_TARGETS))\
+)
 
+# Create driver rules for each driver we found, let users call make specifying
+# the basename of the driver, 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
 
-CLEANABLES := $(foreach lang,$(LANGS),$(foreach srclang,$($(lang)_SRCL), $($(srclang)_TARGETS)) $($(lang)_TARGETS))
-clean: 
-       rm -f $(CLEANABLES)
+clean: $(BUILTLIBS:%=clean_%)
+       rm -f $(MAKE_TARGETS)
 
 scrub: clean
        rm -Rf $(MAKE_DIRS)