From d054d410ff52ee3c196810a27d885f7799afb9ce Mon Sep 17 00:00:00 2001 From: ken Date: Fri, 14 Oct 2016 15:27:48 -0700 Subject: [PATCH] Make organization++ --- Makefile | 536 +++++--------------------------------------- make/lib/awk.mk | 25 +++ make/lib/premake.mk | 55 +++++ make/lib/rules.mk | 358 +++++++++++++++++++++++++++++ 4 files changed, 499 insertions(+), 475 deletions(-) create mode 100644 make/lib/awk.mk create mode 100644 make/lib/premake.mk create mode 100644 make/lib/rules.mk diff --git a/Makefile b/Makefile index 8ce0adf..d0e4090 100644 --- a/Makefile +++ b/Makefile @@ -11,37 +11,36 @@ ################################################################################ # The default rule to be built if none specified default: all -# Base directories # webcc's root -ROOT_DIR := . +ROOT_DIR := . # source directory to find compilable language files in -SRC_DIR := $(ROOT_DIR)/src +SRC_DIR := $(ROOT_DIR)/src # directory where files with 'main' functions live (can be in SRC_DIR) -DRIVER_DIR := $(SRC_DIR)/bin +DRIVER_DIR := $(SRC_DIR)/bin # make configuration directory -CONF_DIR := $(ROOT_DIR)/make +CONF_DIR := $(ROOT_DIR)/make # Source languages handled by this build system. These languages are capable of # being compiled to an intermediary format for binary output by one of the # provided compilersfor each language -LANGS := c cpp go +LANGS := c cpp go # Source-to-source languages handled by this build system -SLANGS := y rl +SLANGS := y rl # Language-specific compilers and flags passed in from environment -c_C := $(strip $(notdir $(CC))) -c_FLAGS := $(strip $(CFLAGS)) -I$(SRC_DIR) -c_LIBS := SDL2 wolfssl -c_SRCL := y -cpp_C := $(strip $(notdir $(CXX))) -cpp_FLAGS := $(strip $(CXXFLAGS)) $(c_FLAGS) -cpp_LIBS := $(c_LIBS) -go_C := gccgo -go_FLAGS := $(c_FLAGS) +c_C := $(strip $(notdir $(CC))) +c_FLAGS := $(strip $(CFLAGS)) -I$(SRC_DIR) +c_LIBS := SDL2 wolfssl +c_SRCL := y +cpp_C := $(strip $(notdir $(CXX))) +cpp_FLAGS := $(strip $(CXXFLAGS)) $(c_FLAGS) +cpp_LIBS := $(c_LIBS) +go_C := gccgo +go_FLAGS := $(c_FLAGS) # Source to source languages # Bison -y_C := bison -y_FLAGS := -d -y_STEM := tab -y_TRG := c h +y_C := bison +y_FLAGS := -d +y_STEM := tab +y_TRG := c h # Ragel rl_C := ragel rl_FLAGS := -C @@ -49,29 +48,29 @@ rl_TRG := c # Compiler-specific associations. Each compiler has a binary object suffix # (OBJ), an archiver (AR), and an archiver object suffix (AROBJ). Each compiler # may optionally have defined a linker (LD), and a binary output suffix (OUT). -cc_OBJ := o -cc_LD := $(LD) -cc_AR := $(AR) -cc_AROBJ := a -cc_DBG := gdb +cc_OBJ := o +cc_LD := $(LD) +cc_AR := $(AR) +cc_AROBJ := a +cc_DBG := gdb $(cpp_C)_LD := $(cc_LD) -gcc_OBJ := $(cc_OBJ) -gcc_LD := $(cc_LD) +gcc_OBJ := $(cc_OBJ) +gcc_LD := $(cc_LD) ngcc_AR := $(cc_AR) -gcc_AROBJ := $(cc_AROBJ) -gcc_DBG := $(cc_DBG) -emcc_OBJ := bc -emcc_AR := emar -emcc_AROBJ := $(emcc_OBJ) #emar is just a python script that reparses shit for emcc -emcc_OUT := .js -g++_OBJ := $(cc_OBJ) -em++_OBJ := $(emcc_OBJ) -em++_AR := $(emcc_AR) -em++_AROBJ := $(emcc_AROBJ) -em++_OUT := $(emcc_OUT) -gccgo_OBJ := o -gccgo_LD := $(cc_LD) -gccgo_AR := $(cc_AR) +gcc_AROBJ := $(cc_AROBJ) +gcc_DBG := $(cc_DBG) +emcc_OBJ := bc +emcc_AR := emar +emcc_AROBJ := $(emcc_OBJ) #emar is just a python script that reparses shit for emcc +emcc_OUT := .js +g++_OBJ := $(cc_OBJ) +em++_OBJ := $(emcc_OBJ) +em++_AR := $(emcc_AR) +em++_AROBJ := $(emcc_AROBJ) +em++_OUT := $(emcc_OUT) +gccgo_OBJ := o +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 ".o" | sed -e 's@^.*lib\([_\+a-zA-Z0-9\-]*\)\..*@\1@g') @@ -82,94 +81,28 @@ em++_LDLIBS := go_LDLIBS := $(cc_LDLIBS) # location to build cross-compilation libraries in, not intended to be installed # in the host system -LIB_DIR := $(ROOT_DIR)/lib$(shell uname -m)/$($(c_C)_OBJ) -LIBDL_DIR := $(LIB_DIR)/.cache -LIBINC_DIR := $(ROOT_DIR)/include +LIB_DIR := $(ROOT_DIR)/lib$(shell uname -m)/$($(c_C)_OBJ) +LIBDL_DIR := $(LIB_DIR)/.cache +LIBINC_DIR := $(ROOT_DIR)/include # The makefile MUST be capable of generating these directories and all of their # contents. These directories are removed during the 'scrub' rule -MAKE_DIRS := $(LIB_DIR) $(LIBDL_DIR) +MAKE_DIRS := $(LIB_DIR) $(LIBDL_DIR) # Set up lib inclusions, and scan for built libs -c_FLAGS += -I$(LIBINC_DIR) -c_OBJ := $($(c_C)_OBJ) +c_FLAGS += -I$(LIBINC_DIR) +c_OBJ := $($(c_C)_OBJ) # Modules are any directories other than 'DRIVER_MODULE' in 'SRC_DIR' and # produce a single archive in 'SRC_DIR' containing all of the module's symbols -MODULES := $(patsubst $(SRC_DIR)/%/,%,$(filter-out $(DRIVER_DIR)/%,$(shell ls -d $(SRC_DIR)/*/))) +MODULES := $(patsubst $(SRC_DIR)/%/,%,$(filter-out $(DRIVER_DIR)/%,$(shell ls -d $(SRC_DIR)/*/))) -#AWK_REVERSE_SQUASH############################################################## -# The following awk program reverses the order of a list while also removing -# 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 \ -' -{ for(i=NF;i>0;i--) - printf (!a[$$i]++) ? $$i FS : ""; - i=split("",a); - print "" -} -' -#/AWK_REVERSE_SQUASH############################################################# -endef +# Include the make libraries +include make/lib/*.mk -#PREMAKE_SOURCE_COMPILER######################################################### -# Compile any source-to-source languages before executing the majority of make ## -################################################################################# -define PREMAKE_SOURCE_COMPILER = -# Find the sources for each source-to-source language -$(foreach slang,$(SLANGS), -$(eval SLANG_SRC := $(shell find $(SRC_DIR) -name "*.$(slang)" -not -name ".*")) -# Foreach target type in the source-to-source language, add this source's -# targets to the list of the current source-to-source language targets -$(eval undefine SLANG_TRG) -$(foreach trg,$($(slang)_TRG), -$(eval SLANG_TRG += $(SLANG_SRC:%.$(slang)=%.$(if $($(slang)_STEM),$($(slang)_STEM).)$(trg)))) -# Stat the source file's last-modified time to the var SRC_TIME -$(foreach src,$(SLANG_SRC), -# Establish a command to run for compiling this file -$(eval SHELL_CMD := cd $(dir $(src)) && $($(slang)_C) $($(slang)_FLAGS) $(notdir $(src))) -# Evaluate missing targets -$(eval FOUND_TRG := $(shell find $(dir $(src)) -name "$(basename $(notdir $(src))).*" -not -name ".*")) -$(eval MISSING_TRG := $(filter-out $(FOUND_TRG), $(SLANG_TRG))) -# Check timings of existing files -$(eval SRC_TIME := $(shell stat -c %Y $(src))) -# For each of the targets created by this source language, evaluate the -# last-modified times of each potential target. If the file does not exist, set -# the time to '0', otherwise set it to the -c %Y result from statting the file. -$(foreach srctrg,$(filter $(basename $(src))%, $(SLANG_TRG)), -$(eval TRG_TIMES += $(if $(wildcard $(srctrg)),$(shell stat -c %Y $(srctrg)),0)==$(srctrg))) -# Sort TRG_TIMES, which will produce a list of oldest-first files and timestamps -# of the form [last-modified-deltatime]==[filename] -$(eval TRG_TIMES := $(shell echo $(sort $(TRG_TIMES)) | $(call AWK_REVERSE_SQUASH))) -# Evaluate the newest time from our ordered list by splitting the word up by its -# '==' connectors and looking at the first word -$(eval NEWEST_TRG_TIME := $(word 1,$(subst ==, ,$(TRG_TIMES)))) -# Find the older of the two times (between SRC_TIME and NEWEST_TRG_TIME) -$(eval OLDER_TIME := $(firstword $(sort $(NEWEST_TRG_TIME) $(SRC_TIME)))) -# If the older of the two times is the newest target time found, then we need to -# rebuild, but only if our build rule intends to actually make something. If it -# instead does not intend to actually make a target, drop a functional rule that -# could hypothetically make the target for debugging and analysis purposes -$(if $(MAKECMDGOALS), -$(eval BUILDGOALS := $(filter-out clean scrub purge uninstall,$(MAKECMDGOALS))), -$(eval BUILDGOALS := all)) -$(if $(and $(BUILDGOALS),$(or $(MISSING_TRG), $(filter $(OLDER_TIME),$(NEWEST_TRG_TIME)))), -$(if $(findstring n,$(MAKEFLAGS)), -$(SLANG_TRG): - $(SHELL_CMD) -, -$(info $(SHELL_CMD) $(eval $(shell $(SHELL_CMD))))) -)) -# Put these targets on the MAKE_TARGETS list to be removed during "clean", -# regardless of whether or not they were built just now. -$(eval MAKE_TARGETS += $(SLANG_TRG)) -) -#/PREMAKE_SOURCE_COMPILER######################################################## -endef -# Compile a source language to a language that compiles to binary, but only if -# our build rule intends to build some kind of target +# If available, invoke the premake system +ifdef PREMAKE_SOURCE_COMPILER $(eval $(call PREMAKE_SOURCE_COMPILER,)) - +else +$(info Warning: No premake system found, source-to-source languages (e.g. bison) not functional) +endif # Backup the CVS password and CVSROOT environment vars in case we change them $(if $(wildcard ~/.cvspass),\ @@ -177,364 +110,17 @@ $(eval CVSPASS_BAK := $(file <~/.cvspass))) $(if $(CVSROOT),\ $(eval CVSROOT_BAK := $(CVSROOT))) -# A long function definition to grab deps from the source's compiler and route -# errors to /dev/null if the rule generator expresses bogus errors -define SRC_LANG_FLAGS_OPT_DEPS = -$(filter-out \ %:,$(shell $($2_C) $3 $4 -MG $1 2> /dev/null)) -endef - -#SRC_LANG_RULE################################################################### -# 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 = -$(if $($1),,$(eval $1 := t)\ -$(eval MOD := $(filter $(MODULES),$(firstword $(subst /, ,$(subst $(SRC_DIR),,$(dir $1))))))\ -$(eval FLG := $(MOD:%=-I$(SRC_DIR)/% ))\ -$(if $(wildcard $1), -$(eval DEPS := $(filter-out \ %:,$(shell $($2_C) $(FLG) -M -MG $1)))\ -$(eval DDEPS := $(filter $(DRIVER_DIR:$(SRC_DIR)/%=%/%),$(DEPS)))\ -$(eval MDEPS := $(filter $(MODULES:%=%/%),$(DEPS)))\ -$(eval DEPS := $(filter-out $(DDEPS) $(MDEPS),$(DEPS)))\ -$(eval MDEPS := $(MDEPS:%=$(SRC_DIR)/%))\ -$(eval DDEPS := $(DDEPS:%=$(SRC_DIR)/%))\ -$(eval LOST := $(filter-out \ %: $(DRIVER_DIR:./%=%)/% $(MODULES:%=$(SRC_DIR:./%=%)/%/%),$(shell $($2_C) $(FLG) -MM -MG $1)))\ -$(eval MDEPS += $(LOST:%=$(dir $1)%))\ -$(eval MDEPS := $(shell echo $(MDEPS) | sed -e 's@[a-zA-Z0-9\-\+/]*\.\./include@$(LIBINC_DIR)@g'))\ -$(eval ALLDEPS := $(DDEPS) $(MDEPS) $(DEPS)),\ -$(error Cannot generate deps for: $1, file not found))\ -$(eval SRC_OBJ := $(basename $1).$($2_OBJ))\ -$(eval MAKE_TARGETS += $(SRC_OBJ)) -# Assume that lost dependencies are in the folder with the source -$(foreach lost,$(LOST), -$(eval ALLDEPS := $(subst $(lost), $(dir $1)$(lost),$(ALLDEPS)))) - -# Find any deps that aren't built yet, which the compiler has flagged -# as missing, but which we know the expected location of libs for -# this language should have their includes in 'LIBINC_DIR' -$(eval BUILDDEPS := $(filter $($2_LIBS:%=%/%),$(ALLDEPS))) -$(eval ALLDEPS := $(filter-out $(BUILDDEPS),$(ALLDEPS)) $(BUILDDEPS:%=$(LIBINC_DIR)/%)) -# Object for $1 -$(SRC_OBJ): $(ALLDEPS) - $($2_C) $$($2_FLAGS) $(FLG) -c -o $$@ $1 -$(if $($2_DBG),\ -$(eval DBG_OBJ := $(dir $1).$($2_DBG)/$(basename $(notdir $1)).$($2_OBJ))\ -$(if $(findstring $(dir $(DBG_OBJ)),$(MAKE_DIRS)),,\ -$(eval MAKE_DIRS += $(dir $(DBG_OBJ)))) -$(eval MAKE_TARGETS += $(DBG_OBJ)) -# Object for $1 with $($2_DBG) symbols -$(DBG_OBJ): $(ALLDEPS) | $(dir $(DBG_OBJ)) - $($2_C) $$($2_FLAGS) $(FLG) -Og -g$($2_DBG) -c -o $$@ $1 -)) -#/SRC_LANG_RULE################################################################## -endef - -#SRC_LANG_DRVRULE################################################################ -# establish a build and link rule given a source driver and language ############ -define SRC_LANG_DRVRULE = -$(eval DRIVER_NAME := $(basename $(notdir $1))) -$(eval DRIVER_TARG := $(1:$(SRC_DIR)/%.$2=$(ROOT_DIR)/%$($2_OUT))) -$(eval DRIVER_SOB := $(basename $1).$($2_OBJ)) - -# Generate a rule for the driver object -$(eval $(call SRC_LANG_RULE,$1,$2)) - -# Modules can be implicitly specified by a driver if its dependencies include -# ANY files from any module directory. The 'MDEPS' var is set by SRC_LANG_RULE -# and expresses the dependencies required to build the object file only. -$(foreach module_dep,$(MDEPS), -$(eval DRIVER_MODULES += $(firstword $(subst /, ,$(module_dep)))) -) -$(eval DRIVER_MODULES := $(filter-out $(DRIVER_DIR) . ..,$(sort $(DRIVER_MODULES)))) - -# In the event that the driver developer does not wish to include a header to -# any internal modules, the developer may instead create a file with the same -# basename as the driver, but with the '.ld' suffix, which contains a space -# separated list of internal modules to link together during compilation -$(eval DRIVER_LDVALS := $(file <$(1:%.$2=%.ld))) -$(eval DRIVER_LFLAGS := $(filter -l%,$(DRIVER_LDVALS))) -$(eval DRIVER_MODULES += $(filter-out -l%,$(DRIVER_LDVALS))) -$(eval DRIVER_MODULES := $(sort $(DRIVER_MODULES))) -# List of module archives to link together during compilation -$(eval DRIVER_ARCHIVES := $(DRIVER_MODULES:%=$(SRC_DIR)/%.$($2_AROBJ))) -$(eval DRIVER_DEPS := $(DRIVER_SOB) $(DRIVER_ARCHIVES)) -$(eval DRIVER_DBGARCHIVES := $(DRIVER_MODULES:%=$(SRC_DIR)/.$($2_DBG)/%.$($2_AROBJ))) -$(eval DRIVER_DBGDEPS := $(dir $(DRIVER_SOB)).$($2_DBG)/$(notdir $(DRIVER_SOB))) -$(eval DRIVER_DBGDEPS += $(DRIVER_DBGARCHIVES)) - -# If the compiler supports linking, distinguish static from dynamic links, -# otherwise set all libraries to static -$(if $($($2_C)_LD),\ -$(eval STLIBS := $(filter-out $($($2_C)_LDLIBS),$($2_LIBS)))\ -$(eval DLIBS := $(filter-out $(STLIBS),$($2_LIBS))),\ -$(eval STLIBS := $($2_LIBS))) - -# Directory setup -$(eval DRIVER_TARG_DIR := $(dir $(1:$(SRC_DIR)/%.$2=$(ROOT_DIR)/%))) -$(eval MAKE_DIRS += $(DRIVER_TARG_DIR)) -$(if $($2_DBG),$(eval MAKE_DIRS += $(DRIVER_TARG_DIR).$($2_DBG))) - -# Setup the sources for this object. A driver build rule does not include any -# compile steps at all, and so should not contain any sources that are, -# themselves, traditional "source files" like a language-compiled 'c' file. The -# driver depends on the existence of its equivalent object file (created through -# compilation), which is expected to contain a 'main' function (or entry for -# '_start'). This driver object is then simply linked to its libraries and -# module archives to create an executable binary in the output folder. -# ORDER MATTERS HERE, this is for the linker: -$(eval DRIVER_SRC := $(DRIVER_LFLAGS) $(DRIVER_DEPS)) -$(eval DRIVER_DBGSRC := $(DRIVER_LFLAGS) $(DRIVER_DBGDEPS)) -# Iterate through the list of libraries in our language and stack commands from -# left to right in the "sources" section of the LD command (or LD section of the -# commandline for the compiler), to preserve ordering and also ensure that it is -# linked correctly IN THAT ORDER (static vs dynamic). -$(foreach lib,$($2_LIBS),\ -$(if $(findstring $(lib),$(STLIBS)), -# Evaluate static lib $(lib) and add it to the dependencies and sources of the rules we built -$(eval STATIC_SRC := $(LIB_DIR)/lib$(lib).$($2_AROBJ))\ -$(eval STATIC_DBGSRC := $(LIB_DIR)/.$($2_DBG)/lib$(lib).$($2_AROBJ))\ -$(eval DRIVER_SRC := $(STATIC_SRC) $(DRIVER_SRC))\ -$(eval DRIVER_DBGSRC := $(STATIC_DBGSRC) $(DRIVER_DBGSRC))\ -$(eval DRIVER_DEPS += $(STATIC_SRC))\ -$(eval DRIVER_DBGDEPS += $(STATIC_DBGSRC)), -# Add dynamic lib to the sources with a link flag, in its correct ordering -$(eval DRIVER_SRC := -l$(lib) $(DRIVER_SRC))\ -$(eval DRIVER_DBGSRC := -l$(lib) $(DRIVER_DBGSRC))\ -)) - -# Add the driver target to this language's targets for cleaning -$(eval SCRUB_TARGETS += $(DRIVER_TARG)) -$(DRIVER_TARG): $(DRIVER_DEPS) | $(DRIVER_TARG_DIR) - $($2_C) $($2_FLAGS) $(DRIVER_SRC) -o $$@ -# Output a rule for building with debug flags -$(eval DRIVER_DBGTARG := $(DRIVER_TARG_DIR)$(basename $(notdir $(DRIVER_TARG)))-d$($2_OUT)) -$(eval SCRUB_TARGETS += $(DRIVER_DBGTARG)) -$(eval MAKE_DIRS += $(DRIVER_TARG_DIR).$($2_DBG)/) -$(DRIVER_DBGTARG): $(DRIVER_DBGDEPS) | $(DRIVER_TARG_DIR) - $($2_C) $($2_FLAGS) $(DRIVER_DBGSRC) -o $$@ -# Make a rule to run this driver after building -$(DRIVER_NAME)-run: $(DRIVER_TARG) - $(DRIVER_TARG) -$(DRIVER_NAME)-d: $(DRIVER_DBGTARG) - -#/SRC_LANG_DRVRULE############################################################### -endef - -#MODULE_ARCRULE################################################################## -# 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 ARCDEPS := $(filter $(SRC_DIR)/$1/%.$(c_OBJ),$(foreach lang,$(LANGS),$($(lang)_MOD_TRG))))\ -$(eval MAKE_TARGETS+= $(SRC_DIR)/$1.$(c_AROBJ))\ - -$(SRC_DIR)/$1.$(c_AROBJ): $(ARCDEPS) - $(c_AR) cr $$@ $$^ -$(if $(c_DBG), -$(eval undefine DBGARCDEPS) -$(foreach arcdep,$(ARCDEPS),$(eval DBGARCDEPS += $(dir $(arcdep)).$(c_DBG)/$(notdir $(arcdep)))) -$(eval MAKE_TARGETS+= .$(c_DBG)/$1.$(c_AROBJ))\ - -$(SRC_DIR)/.$(c_DBG)/$1.$(c_AROBJ): $(DBGARCDEPS) | .$(c_DBG)/ - $(c_AR) cr $$@ $$^ -) -#/MODULE_ARCRULE################################################################# -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 = -$(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))) -) -# 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 $(CONF_DIR)/$2.mk),$(eval $(file <$(CONF_DIR)/$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 := $(shell find $(SRC_DIR) -name "*.$1" -not -name ".*") -$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 $(SRC_DIR)/%.$1,$(ROOT_DIR)/%$($1_OUT),$($1_DRV_SRC))) -$(eval $1_MOD_SRC := $(filter-out $(DRIVER_DIR)/%,$($1_SOURCES))) -$(eval $1_MOD_TRG := $(addsuffix .$($1_OBJ),$(basename $($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 $(SRC_DIR)/$(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 $(SRC_DIR)/$(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),\ +ifdef LANG_INIT +$(foreach lang,$(LANGS),\ $(eval $(call LANG_INIT,$(lang)))\ -$(eval $(lang)_LIBS := $(shell echo $($(lang)_LIBS) | $(call AWK_REVERSE_SQUASH)))\ -)) +$(eval $(lang)_LIBS := $(shell echo $($(lang)_LIBS | $(call AWK_REVERSE_SQUASH))))\ +) +else +$(error No LANG_INIT available, no languages can be compiled) +endif # Create module object rules for each module $(foreach module,$(MODULES),\ diff --git a/make/lib/awk.mk b/make/lib/awk.mk new file mode 100644 index 0000000..81b57ca --- /dev/null +++ b/make/lib/awk.mk @@ -0,0 +1,25 @@ +################################################################################ +# Desc: Named functions for awk +# Author: Ken Grimes +# Date: 2016 +################################################################################ +# This makefile consists of awk programs that may come in handy as an extension +# to the basic lisp make capabilities without having to invoke guile +################################################################################ + +# AWK_REVERSE_SQUASH############################################################ +# The following awk program reverses the order of a list while also removing +# 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 \ +' +{ for(i=NF;i>0;i--) + printf (!a[$$i]++) ? $$i FS : ""; + i=split("",a); + print "" +} +' +#/AWK_REVERSE_SQUASH############################################################ +endef diff --git a/make/lib/premake.mk b/make/lib/premake.mk new file mode 100644 index 0000000..f5aca93 --- /dev/null +++ b/make/lib/premake.mk @@ -0,0 +1,55 @@ +#PREMAKE_SOURCE_COMPILER######################################################### +# Compile any source-to-source languages before executing the majority of make ## +################################################################################# +define PREMAKE_SOURCE_COMPILER = +# Find the sources for each source-to-source language +$(foreach slang,$(SLANGS), +$(eval SLANG_SRC := $(shell find $(SRC_DIR) -name "*.$(slang)" -not -name ".*")) +# Foreach target type in the source-to-source language, add this source's +# targets to the list of the current source-to-source language targets +$(eval undefine SLANG_TRG) +$(foreach trg,$($(slang)_TRG), +$(eval SLANG_TRG += $(SLANG_SRC:%.$(slang)=%.$(if $($(slang)_STEM),$($(slang)_STEM).)$(trg)))) +# Stat the source file's last-modified time to the var SRC_TIME +$(foreach src,$(SLANG_SRC), +# Establish a command to run for compiling this file +$(eval SHELL_CMD := cd $(dir $(src)) && $($(slang)_C) $($(slang)_FLAGS) $(notdir $(src))) +# Evaluate missing targets +$(eval FOUND_TRG := $(shell find $(dir $(src)) -name "$(basename $(notdir $(src))).*" -not -name ".*")) +$(eval MISSING_TRG := $(filter-out $(FOUND_TRG), $(SLANG_TRG))) +# Check timings of existing files +$(eval SRC_TIME := $(shell stat -c %Y $(src))) +# For each of the targets created by this source language, evaluate the +# last-modified times of each potential target. If the file does not exist, set +# the time to '0', otherwise set it to the -c %Y result from statting the file. +$(foreach srctrg,$(filter $(basename $(src))%, $(SLANG_TRG)), +$(eval TRG_TIMES += $(if $(wildcard $(srctrg)),$(shell stat -c %Y $(srctrg)),0)==$(srctrg))) +# Sort TRG_TIMES, which will produce a list of oldest-first files and timestamps +# of the form [last-modified-deltatime]==[filename] +$(eval TRG_TIMES := $(shell echo $(sort $(TRG_TIMES)) | $(call AWK_REVERSE_SQUASH))) +# Evaluate the newest time from our ordered list by splitting the word up by its +# '==' connectors and looking at the first word +$(eval NEWEST_TRG_TIME := $(word 1,$(subst ==, ,$(TRG_TIMES)))) +# Find the older of the two times (between SRC_TIME and NEWEST_TRG_TIME) +$(eval OLDER_TIME := $(firstword $(sort $(NEWEST_TRG_TIME) $(SRC_TIME)))) +# If the older of the two times is the newest target time found, then we need to +# rebuild, but only if our build rule intends to actually make something. If it +# instead does not intend to actually make a target, drop a functional rule that +# could hypothetically make the target for debugging and analysis purposes +$(if $(MAKECMDGOALS), +$(eval BUILDGOALS := $(filter-out clean scrub purge uninstall,$(MAKECMDGOALS))), +$(eval BUILDGOALS := all)) +$(if $(and $(BUILDGOALS),$(or $(MISSING_TRG), $(filter $(OLDER_TIME),$(NEWEST_TRG_TIME)))), +$(if $(findstring n,$(MAKEFLAGS)), +$(SLANG_TRG): + $(SHELL_CMD) +, +$(info $(SHELL_CMD) $(eval $(shell $(SHELL_CMD))))) +)) +# Put these targets on the MAKE_TARGETS list to be removed during "clean", +# regardless of whether or not they were built just now. +$(eval MAKE_TARGETS += $(SLANG_TRG)) +) +#/PREMAKE_SOURCE_COMPILER######################################################## +endef + diff --git a/make/lib/rules.mk b/make/lib/rules.mk new file mode 100644 index 0000000..50ca62b --- /dev/null +++ b/make/lib/rules.mk @@ -0,0 +1,358 @@ +################################################################################ +# Desc: Make functions for creating rules (Make making Make) +# Author: Ken Grimes +# Date: 2016 +################################################################################ +# This file consists of library functions for make-making-make functionality +################################################################################ + +# A long function definition to grab deps from the source's compiler and route +# errors to /dev/null if the rule generator expresses bogus errors +define SRC_LANG_FLAGS_OPT_DEPS = +$(filter-out \ %:,$(shell $($2_C) $3 $4 -MG $1 2> /dev/null)) +endef + +#SRC_LANG_RULE################################################################### +# 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 = +$(if $($1),,$(eval $1 := t)\ +$(eval MOD := $(filter $(MODULES),$(firstword $(subst /, ,$(subst $(SRC_DIR),,$(dir $1))))))\ +$(eval FLG := $(MOD:%=-I$(SRC_DIR)/% ))\ +$(if $(wildcard $1), +$(eval DEPS := $(filter-out \ %:,$(shell $($2_C) $(FLG) -M -MG $1)))\ +$(eval DDEPS := $(filter $(DRIVER_DIR:$(SRC_DIR)/%=%/%),$(DEPS)))\ +$(eval MDEPS := $(filter $(MODULES:%=%/%),$(DEPS)))\ +$(eval DEPS := $(filter-out $(DDEPS) $(MDEPS),$(DEPS)))\ +$(eval MDEPS := $(MDEPS:%=$(SRC_DIR)/%))\ +$(eval DDEPS := $(DDEPS:%=$(SRC_DIR)/%))\ +$(eval LOST := $(filter-out \ %: $(DRIVER_DIR:./%=%)/% $(MODULES:%=$(SRC_DIR:./%=%)/%/%),$(shell $($2_C) $(FLG) -MM -MG $1)))\ +$(eval MDEPS += $(LOST:%=$(dir $1)%))\ +$(eval MDEPS := $(shell echo $(MDEPS) | sed -e 's@[a-zA-Z0-9\-\+/]*\.\./include@$(LIBINC_DIR)@g'))\ +$(eval ALLDEPS := $(DDEPS) $(MDEPS) $(DEPS)),\ +$(error Cannot generate deps for: $1, file not found))\ +$(eval SRC_OBJ := $(basename $1).$($2_OBJ))\ +$(eval MAKE_TARGETS += $(SRC_OBJ)) +# Assume that lost dependencies are in the folder with the source +$(foreach lost,$(LOST), +$(eval ALLDEPS := $(subst $(lost), $(dir $1)$(lost),$(ALLDEPS)))) + +# Find any deps that aren't built yet, which the compiler has flagged +# as missing, but which we know the expected location of libs for +# this language should have their includes in 'LIBINC_DIR' +$(eval BUILDDEPS := $(filter $($2_LIBS:%=%/%),$(ALLDEPS))) +$(eval ALLDEPS := $(filter-out $(BUILDDEPS),$(ALLDEPS)) $(BUILDDEPS:%=$(LIBINC_DIR)/%)) +# Object for $1 +$(SRC_OBJ): $(ALLDEPS) + $($2_C) $$($2_FLAGS) $(FLG) -c -o $$@ $1 +$(if $($2_DBG),\ +$(eval DBG_OBJ := $(dir $1).$($2_DBG)/$(basename $(notdir $1)).$($2_OBJ))\ +$(if $(findstring $(dir $(DBG_OBJ)),$(MAKE_DIRS)),,\ +$(eval MAKE_DIRS += $(dir $(DBG_OBJ)))) +$(eval MAKE_TARGETS += $(DBG_OBJ)) +# Object for $1 with $($2_DBG) symbols +$(DBG_OBJ): $(ALLDEPS) | $(dir $(DBG_OBJ)) + $($2_C) $$($2_FLAGS) $(FLG) -Og -g$($2_DBG) -c -o $$@ $1 +)) +#/SRC_LANG_RULE################################################################## +endef + +#SRC_LANG_DRVRULE################################################################ +# establish a build and link rule given a source driver and language ############ +define SRC_LANG_DRVRULE = +$(eval DRIVER_NAME := $(basename $(notdir $1))) +$(eval DRIVER_TARG := $(1:$(SRC_DIR)/%.$2=$(ROOT_DIR)/%$($2_OUT))) +$(eval DRIVER_SOB := $(basename $1).$($2_OBJ)) + +# Generate a rule for the driver object +$(eval $(call SRC_LANG_RULE,$1,$2)) + +# Modules can be implicitly specified by a driver if its dependencies include +# ANY files from any module directory. The 'MDEPS' var is set by SRC_LANG_RULE +# and expresses the dependencies required to build the object file only. +$(foreach module_dep,$(MDEPS), +$(eval DRIVER_MODULES += $(firstword $(subst /, ,$(module_dep)))) +) +$(eval DRIVER_MODULES := $(filter-out $(DRIVER_DIR) . ..,$(sort $(DRIVER_MODULES)))) + +# In the event that the driver developer does not wish to include a header to +# any internal modules, the developer may instead create a file with the same +# basename as the driver, but with the '.ld' suffix, which contains a space +# separated list of internal modules to link together during compilation +$(eval DRIVER_LDVALS := $(file <$(1:%.$2=%.ld))) +$(eval DRIVER_LFLAGS := $(filter -l%,$(DRIVER_LDVALS))) +$(eval DRIVER_MODULES += $(filter-out -l%,$(DRIVER_LDVALS))) +$(eval DRIVER_MODULES := $(sort $(DRIVER_MODULES))) +# List of module archives to link together during compilation +$(eval DRIVER_ARCHIVES := $(DRIVER_MODULES:%=$(SRC_DIR)/%.$($2_AROBJ))) +$(eval DRIVER_DEPS := $(DRIVER_SOB) $(DRIVER_ARCHIVES)) +$(eval DRIVER_DBGARCHIVES := $(DRIVER_MODULES:%=$(SRC_DIR)/.$($2_DBG)/%.$($2_AROBJ))) +$(eval DRIVER_DBGDEPS := $(dir $(DRIVER_SOB)).$($2_DBG)/$(notdir $(DRIVER_SOB))) +$(eval DRIVER_DBGDEPS += $(DRIVER_DBGARCHIVES)) + +# If the compiler supports linking, distinguish static from dynamic links, +# otherwise set all libraries to static +$(if $($($2_C)_LD),\ +$(eval STLIBS := $(filter-out $($($2_C)_LDLIBS),$($2_LIBS)))\ +$(eval DLIBS := $(filter-out $(STLIBS),$($2_LIBS))),\ +$(eval STLIBS := $($2_LIBS))) + +# Directory setup +$(eval DRIVER_TARG_DIR := $(dir $(1:$(SRC_DIR)/%.$2=$(ROOT_DIR)/%))) +$(eval MAKE_DIRS += $(DRIVER_TARG_DIR)) +$(if $($2_DBG),$(eval MAKE_DIRS += $(DRIVER_TARG_DIR).$($2_DBG))) + +# Setup the sources for this object. A driver build rule does not include any +# compile steps at all, and so should not contain any sources that are, +# themselves, traditional "source files" like a language-compiled 'c' file. The +# driver depends on the existence of its equivalent object file (created through +# compilation), which is expected to contain a 'main' function (or entry for +# '_start'). This driver object is then simply linked to its libraries and +# module archives to create an executable binary in the output folder. +# ORDER MATTERS HERE, this is for the linker: +$(eval DRIVER_SRC := $(DRIVER_LFLAGS) $(DRIVER_DEPS)) +$(eval DRIVER_DBGSRC := $(DRIVER_LFLAGS) $(DRIVER_DBGDEPS)) +# Iterate through the list of libraries in our language and stack commands from +# left to right in the "sources" section of the LD command (or LD section of the +# commandline for the compiler), to preserve ordering and also ensure that it is +# linked correctly IN THAT ORDER (static vs dynamic). +$(foreach lib,$($2_LIBS),\ +$(if $(findstring $(lib),$(STLIBS)), +# Evaluate static lib $(lib) and add it to the dependencies and sources of the rules we built +$(eval STATIC_SRC := $(LIB_DIR)/lib$(lib).$($2_AROBJ))\ +$(eval STATIC_DBGSRC := $(LIB_DIR)/.$($2_DBG)/lib$(lib).$($2_AROBJ))\ +$(eval DRIVER_SRC := $(STATIC_SRC) $(DRIVER_SRC))\ +$(eval DRIVER_DBGSRC := $(STATIC_DBGSRC) $(DRIVER_DBGSRC))\ +$(eval DRIVER_DEPS += $(STATIC_SRC))\ +$(eval DRIVER_DBGDEPS += $(STATIC_DBGSRC)), +# Add dynamic lib to the sources with a link flag, in its correct ordering +$(eval DRIVER_SRC := -l$(lib) $(DRIVER_SRC))\ +$(eval DRIVER_DBGSRC := -l$(lib) $(DRIVER_DBGSRC))\ +)) + +# Add the driver target to this language's targets for cleaning +$(eval SCRUB_TARGETS += $(DRIVER_TARG)) +$(DRIVER_TARG): $(DRIVER_DEPS) | $(DRIVER_TARG_DIR) + $($2_C) $($2_FLAGS) $(DRIVER_SRC) -o $$@ +# Output a rule for building with debug flags +$(eval DRIVER_DBGTARG := $(DRIVER_TARG_DIR)$(basename $(notdir $(DRIVER_TARG)))-d$($2_OUT)) +$(eval SCRUB_TARGETS += $(DRIVER_DBGTARG)) +$(eval MAKE_DIRS += $(DRIVER_TARG_DIR).$($2_DBG)/) +$(DRIVER_DBGTARG): $(DRIVER_DBGDEPS) | $(DRIVER_TARG_DIR) + $($2_C) $($2_FLAGS) $(DRIVER_DBGSRC) -o $$@ +# Make a rule to run this driver after building +$(DRIVER_NAME)-run: $(DRIVER_TARG) + $(DRIVER_TARG) +$(DRIVER_NAME)-d: $(DRIVER_DBGTARG) + +#/SRC_LANG_DRVRULE############################################################### +endef + +#MODULE_ARCRULE################################################################## +# 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 ARCDEPS := $(filter $(SRC_DIR)/$1/%.$(c_OBJ),$(foreach lang,$(LANGS),$($(lang)_MOD_TRG))))\ +$(eval MAKE_TARGETS+= $(SRC_DIR)/$1.$(c_AROBJ))\ + +$(SRC_DIR)/$1.$(c_AROBJ): $(ARCDEPS) + $(c_AR) cr $$@ $$^ +$(if $(c_DBG), +$(eval undefine DBGARCDEPS) +$(foreach arcdep,$(ARCDEPS),$(eval DBGARCDEPS += $(dir $(arcdep)).$(c_DBG)/$(notdir $(arcdep)))) +$(eval MAKE_TARGETS+= .$(c_DBG)/$1.$(c_AROBJ))\ + +$(SRC_DIR)/.$(c_DBG)/$1.$(c_AROBJ): $(DBGARCDEPS) | .$(c_DBG)/ + $(c_AR) cr $$@ $$^ +) +#/MODULE_ARCRULE################################################################# +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 = +$(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))) +) +# 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 $(CONF_DIR)/$2.mk),$(eval $(file <$(CONF_DIR)/$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 := $(shell find $(SRC_DIR) -name "*.$1" -not -name ".*") +$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 $(SRC_DIR)/%.$1,$(ROOT_DIR)/%$($1_OUT),$($1_DRV_SRC))) +$(eval $1_MOD_SRC := $(filter-out $(DRIVER_DIR)/%,$($1_SOURCES))) +$(eval $1_MOD_TRG := $(addsuffix .$($1_OBJ),$(basename $($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 $(SRC_DIR)/$(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 $(SRC_DIR)/$(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 -- 2.18.0