Buildsys2
authorksg <ken@mihrtec.com>
Thu, 14 Jul 2016 23:55:49 +0000 (16:55 -0700)
committerksg <ken@mihrtec.com>
Thu, 14 Jul 2016 23:55:49 +0000 (16:55 -0700)
src/.make/build.mk
src/.make/buildc.mk
src/.make/buildc_emcc.mk
src/.make/buildclibs.mk [new file with mode: 0644]
src/.make/buildhtml.mk [new file with mode: 0644]
src/.make/libwolfssl.mk
src/Makefile

index 86e518d..12affc7 100644 (file)
 ##################################################
 #Environment Setup
 ##################################################
-#Extra modules to build (core is built by default)
-MODULES  :=
-#Libraries to include
-LIBS     := wolfssl SDL2 SDL2_image SDL2_ttf
-
-#Build system directory management
-REPOROOT := ..
-DISTDIR  := $(REPOROOT)/dist
-BINDIR   := $(DISTDIR)/bin
-EXEDIR   := $(BINDIR)/$(shell uname -m)
-XTDIR    := $(REPOROOT)/extern
-XTLIBDIR := /usr/local/lib/mihrtec
+#Project root
+PROJDIR  := ..
+#Output directories for distributables
+DISTDIR  := $(PROJDIR)/dist
 
 #Build rules for each source type
 include .make/buildc.mk
+include .make/buildhtml.mk
 
 #Set the final target, flags, and objects for app compile
-APPDEPS  := $(CTARG) $(APPDEPS)
-APPTARG  := $(EXEDIR)/$(APPNAME)$(APPSUFFIX)
-APPFLAGS := $(CXXFLAGS) $(CFLAGS) $(APPFLAGS)
-APPDIRS  := $(sort $(dir $(APPTARG) $(WEBTARG)))
+APPC     := $(CC)
+APPDEPS  := $(strip $(CDEPS) $(CTARG) $(APPDEPS))
+APPSRC   := $(filter %$(COBJSUF),$(APPDEPS))
+APPTARG  := $(CBINDIR)/$(APPNAME)$(APPSUF)
+APPFLAGS := $(strip $(CXXFLAGS) $(CFLAGS) $(APPFLAGS))
+APPDIRS  := $(sort $(dir $(APPTARG) $(APPDEPS)))
 
index a1768bb..48e9d0a 100644 (file)
@@ -8,64 +8,70 @@
 #compile C files either to native bc through gcc,
 #or web through emscripten
 ##################################################
-#expand the app source dir last as a module to build
-CMODULES := $(MODULES) $(APPSRC)
-CLIBS    := $(patsubst %,-l%,$(LIBS))
-CSRC     := $(wildcard $(patsubst %,%/*.c,$(CMODULES)))
-CTARG    := $(CSRC:.c=.o)
-
-##################################################
-#Global CFlags
+#Manually defined resources
 ##################################################
-#Warn Level
-CFLAGS := -Wall
-#Include each module's root directory and src/
-CFLAGS += $(patsubst %,-I%, $(CMODULES)) -I.
-#Precompiler Flags
-CFLAGS += -DAPPNAME=$(APPNAME)
+#Libraries to link for the build
+CLIBS    := wolfssl SDL2 SDL2_image SDL2_ttf
+#Modules for C (directories in src/ with C code)
+CMODULES := $(APPMODULE)
+#CFlag defaults
+CFLAGS   := -Wall
 
-##################################################
-#Missing Lib Handler
-##################################################
-LDPIPE := ld $(CLIBS) 2>&1 >/dev/null
-LDGREP := grep "cannot find"
-LDSED  := sed -e 's@ld:.*-l\([^ ]*\).*@\1@'
-MISSINGLIBS := $(shell $(LDPIPE) | $(LDGREP) | $(LDSED))
-#Automake vars for building libs
-AMGEN  := ./autogen.sh
-AMCONF := ./configure
-AMMAKE := make
 
 ##################################################
-#Override C Environment for compiler-specific
-#or OS specific requirements, if present
+#Allow compiler-specific and OS-specific
+#makefiles to override C environment vars
 ##################################################
 -include .make/buildc_$(strip $(notdir $(CC))).mk
 ifdef OS
 -include .make/buildc_$(strip $(OS)).mk
 endif
 
-#Determine suffix (possibly overridden by includes)
-COBJSUF := $(suffix $(firstword $(CTARG)))
+#Link each CLIB with -l
+CFLAGS += $(patsubst %,-l%,$(CLIBS))
+#Include each module's root directory and .
+CFLAGS += $(patsubst %,-I%, $(CMODULES)) -I.
+#C Preprocessor defines
+CFLAGS += -DAPPNAME=$(APPNAME)
 
 ##################################################
-#C Dependency Generator
+#Conditional (may be set in buildc_*.mk files)
 ##################################################
-CGENDEP := $(strip $(CC) -MM -MG $(CXXFLAGS) $(CFLAGS)) $BASE.c
-CGENDEP += | sed -e 's@^.*\.o:@@'
-CGENDEP += | tr '\\' ' '
+#Sources are found in any CMODULE subdir
+CSRC     ?= $(wildcard $(patsubst %,%/*.c,$(CMODULES)))
+#Output format suffix
+COBJSUF  ?= .o
+#Architecture
+CARCH    ?= $(shell uname -m)
+#Application suffix
+APPSUF   ?= .$(CARCH)
+#C binary distribution directory
+CDISTDIR ?= $(DISTDIR)
+#Directory for C binary/executable output files
+CBINDIR  ?= $(CDISTDIR)/bin
+#Set up the final C build targets
+CTARG    ?= $(CSRC:.c=$(COBJSUF))
+
+#Set of shell functions used to determine dependencies
+#for building each c source file
+#$BASE is a keyword replaced in the main Makefile iteratively
+#for each source file.
+CDPIPE ?= $(strip $(CC)) -MM -MG $(CXXFLAXS) $(CFLAGS) $BASE.c
+CDSED  ?= sed -e 's@^.*\.o:@@'
+CDTR   ?= tr '\\' ' '
+#The shell command to execute and evaluate for deps
+CDEPSCOMMAND ?= $(CDPIPE) | $(CDSED) | $(CDTR)
+
+#Set of shell functions used to determine the
+#name of each library the compiler/LD can't find
+LDPIPE ?= $(CC) $(patsubst %,-l%,$(CLIBS)) 2>&1 >/dev/null
+LDGREP ?= grep "cannot find"
+LDSED  ?= sed -e 's@^.*cannot find -l\([a-zA-Z0-9\.]*\).*@\1@g'
+#Calculate missing libs
+MISSINGLIBS ?= $(strip $(shell $(LDPIPE) | $(LDGREP) | $(LDSED)))
 
-#Set up all missing libs to be made in the source directory
+#Include handler for building missing libs, if any
 ifdef MISSINGLIBS
-$(info Static libraries to be built in $(XTLIBDIR):)
-$(info $(patsubst %,lib%.so,$(MISSINGLIBS)))
-STATICLIBS := $(patsubst %,$(XTLIBDIR)/lib%.so,$(MISSINGLIBS))
-CFLAGS     += -L$(XTLIBDIR) $(patsubst %,-I$(XTDIR)/%,$(MISSINGLIBS))
-#Ensure that C files depend on the creation of these static libs
-CDEPS      += $(STATICLIBS)
-#Modify the dependency generator to find the right directory for
-#missing libs (they are in XTDIR/libname)
-CGENDEP    += $(patsubst %, | sed -e 's@ \(%\)/@$(XTDIR)/\1/\1/@',$(MISSINGLIBS))
+include .make/buildclibs.mk
 endif
 
-CFLAGS += $(CLIBS)
index 800722c..2ac5546 100644 (file)
@@ -5,26 +5,35 @@
 ##################################################
 #Compile Overrides for web
 ##################################################
-#redefine EXE directory for web platform
-EXEDIR    := $(BINDIR)/web/js
-#EMCC targets .js "executables"
-APPSUFFIX := .js
-#EMCC provides ports of each of these, so remove them
-#and recalculate CLIBS
-LIBS     := $(filter-out SDL2 SDL2_image SDL2_ttf, $(LIBS))
-CLIBS    := $(patsubst %,-l%,$(LIBS))
-#Override targets to make .bc files
-CTARG    := $(CTARG:.o=.bc)
+#EMCC compiles with llvm
+CARCH   := llvm
+#EMCC outputs to javascript
+APPSUF  := .js
+#Place JS binary output in dist/js
+CBINDIR := $(DISTDIR)/js
+#Object files are in LLVM bitcode format
+COBJSUF := .bc
 #Expand the list of plain-text functions into what emcc expects
 EXPORT_FUNCS := $(patsubst %,'_%',$(EXPORT_FUNCS))
 EXPORT_LIST := $(shell echo "$(EXPORT_FUNCS)" | sed -e 's@ @, @g')
-#Mark all remaining libs "missing" since they need to be built for emcc
-MISSINGLIBS := $(LIBS)
-#Set XTLibDir to one for bytecodes
-XTLIBDIR := $(XTLIBDIR)/bc
-#Override Automake vars
-AMGEN  := emconfigure $(AMGEN)
-AMCONF := emconfigure $(AMCONF)
+#Override the linker sed script to find our string
+LDSED := sed -e 's!.*rary \"\([a-zA-Z0-9\.]*\)\".*!\1!g'
+##################################################
+#Emscripten Ports
+#To be replaced.  Emscripten's ports are not
+#maintained well.
+##################################################
+EMPORTS := SDL2 SDL2_image SDL2_ttf
+ifdef EMPORTS
+#Remove EMPORTS from the linker
+CLIBS := $(filter-out $(EMPORTS), $(CLIBS))
+#Add supported ports to CFLAGS
+CFLAGS  += -s USE_SDL=2 -s USE_SDL_IMAGE=2 -s USE_SDL_TTF=2
+endif
+
+#Override Automake vars (for building libs)
+AMGEN  := emconfigure ./autogen.sh
+AMCONF := emconfigure ./configure
 AMMAKE := emmake
 
 #Set up emscripten APPFLAGS
@@ -35,28 +44,4 @@ APPFLAGS += -s FORCE_FILESYSTEM=1
 
 #Set up emscripten flags for CC
 CFLAGS   += -s EXPORTED_FUNCTIONS="[$(EXPORT_LIST)]"
-CFLAGS   += -I$(XTDIR) -s USE_SDL=2 -s USE_SDL_IMAGE=2 -s USE_SDL_TTF=2
-
-#Directory structure for web targets
-#Dirs for source files
-HTSRCDIR := $(REPOROOT)/html
-JSSRCDIR := $(HTSRCDIR)/js
-#Dirs for targets
-HTDIR    := $(EXEDIR)
-JSDIR    := $(HTDIR)/js
-
-#Discover each type of object supported
-HTSRC   := $(wildcard $(HTSRCDIR)/*.html)
-JSSRC   := $(wildcard $(JSSRCDIR)/*.js)
-WEBSRC  := $(HTSRC) $(JSSRC)
-
-#Translate sources into their required targets in APPDIR
-WEBTARG := $(patsubst $(HTSRCDIR)/%,$(HTDIR)/%,$(WEBSRC))
-
-#Add APPDEPS for all web objects so they must be created
-#at build time, before compilation of the main executable
-APPDEPS += $(WEBOBJ)
-
-#Later, we will use WEBTARG to define rules for "making"
-#these targets (with a cp action) and cleaning (with rm)
 
diff --git a/src/.make/buildclibs.mk b/src/.make/buildclibs.mk
new file mode 100644 (file)
index 0000000..ccd85ce
--- /dev/null
@@ -0,0 +1,40 @@
+##################################################
+#Desc:   Missing CLib Generator
+#Author: Mihrtec LLC
+#Date:   2016
+##################################################
+#Included from buildc.make
+#Defines vars for automatically downloading,
+#compiling, and linking GNU-like C libraries
+##################################################
+
+#Missing libraries will be downloaded
+#to MLSRCDIR and installed at MLLIBDIR
+MLDIR    := /usr/local
+MLSRCDIR := $(MLDIR)/src
+#If we aren't compiling for our own environment,
+#we need to create a directory for that type of
+#transpiled library
+ARCHLOCAL := $(shell uname -m | grep "$(CARCH)")
+MLLIBDIR  := $(if $(ARCHLOCAL),$(MLDIR)/lib,$(MLDIR)/lib$(CARCH))
+
+#Modify the dependency generator to find the right directory for
+#missing lib includes (they are in MLDIR/src/libname/libname)
+CDEPSCOMMAND += $(patsubst %, | sed -e 's@ \(%\)/@$(MLSRCDIR)/\1/\1/@',$(MISSINGLIBS))
+
+#Automake vars for building libs
+AMGEN  ?= ./autogen.sh
+AMCONF ?= ./configure
+AMMAKE ?= make
+
+#Import the headers for each missing library (XTDIR/LIBNAME)
+CFLAGS += -L$(MLLIBDIR) $(patsubst %,-I$(MLSRCDIR)/%,$(MISSINGLIBS))
+
+#Notify user of libraries that are about to be built
+$(info Libraries to be built in $(MLLIBDIR):)
+$(info $(patsubst %,lib%.so,$(MISSINGLIBS)))
+STATICLIBS := $(patsubst %,$(MLLIBDIR)/lib%.so,$(MISSINGLIBS))
+
+#Ensure that C files depend on the creation of these static libs
+CDEPS  += $(STATICLIBS)
+
diff --git a/src/.make/buildhtml.mk b/src/.make/buildhtml.mk
new file mode 100644 (file)
index 0000000..c5fb66c
--- /dev/null
@@ -0,0 +1,21 @@
+#Directory structure for web targets
+#Dirs for source files
+HTSRCDIR := $(PROJDIR)/html
+JSSRCDIR := $(HTSRCDIR)/js
+
+#Discover each type of object supported
+HTSRC   := $(wildcard $(HTSRCDIR)/*.html)
+JSSRC   := $(wildcard $(JSSRCDIR)/*.js)
+WEBSRC  := $(HTSRC) $(JSSRC)
+
+#Translate sources into their required targets in APPDIR
+WEBTARG := $(patsubst $(HTSRCDIR)/%,$(DISTDIR)/%,$(WEBSRC))
+
+#Add APPDEPS for all web objects so they must be created
+#at build time, before compilation of the main executable
+APPDEPS += $(WEBTARG)
+APPDIRS += $(dir $(WEBTARG))
+
+#In Makefile, we will use WEBTARG to define rules for "making"
+#these targets (with a cp action) and cleaning (with rm)
+
index d9b77e4..a53cb1e 100644 (file)
@@ -9,9 +9,9 @@
 ##################################################
 
 #wolfssl module compilation rules
-WOLFDIR  := $(XTDIR)/wolfssl
+WOLFDIR  := $(MLSRCDIR)/wolfssl
 WOLFSO   := $(WOLFDIR)/src/.libs/libwolfssl.so
-WOLFTARG := $(XTLIBDIR)/libwolfssl.so
+WOLFTARG := $(MLLIBDIR)/libwolfssl.so
 CONFCL   := --enable-singlethreaded=yes
 CONFCL   += --enable-static=yes
 CONFCL   += --enable-fastmath=no
index 4e22f2f..c6262fd 100644 (file)
@@ -8,7 +8,7 @@
 #Executable name, also passed as -DAPPNAME to compiler
 APPNAME   := the_march
 #Directory in which we expect to find main()
-APPSRC    := core
+APPMODULE := core
 #Compiler flags for the final executable
 APPFLAGS  :=
 #Object files statically linked for the executable
@@ -26,17 +26,20 @@ EMFLAGS      :=
 #Import the build system setup
 include .make/build.mk
 
+#Directories to be constructed
+DIRS := $(sort $(dir $(APPDEPS) $(APPTARG)))
+
 #Targets that don't generate a file
 .PHONY: $(APPNAME) run push dox doc clean scrub all default
 #Define default rules to point to APPNAME
 all default: $(APPTARG)
 
 #Build the executable
-$(APPNAME) $(APPTARG): $(APPDEPS) | $(APPDIRS)
-       $(CC) -o $(APPTARG) $(APPFLAGS) $(APPDEPS)
+$(APPNAME) $(APPTARG): $(DIRS) $(APPDEPS)
+       $(APPC) -o $(APPTARG) $(APPFLAGS) $(APPSRC)
 
 #create directories
-$(APPDIRS):
+$(DIRS):
        @mkdir -p $@
 
 #run the game after making it
@@ -67,7 +70,7 @@ scrub: | clean
 
 #Generate and evaluate C dependencies
 $(foreach base,$(basename $(sort $(strip $(CTARG)))),\
-       $(eval $(strip $(base)$(COBJSUF): $(shell $(subst $BASE,$(base),$(CGENDEP))))))
+       $(eval $(strip $(base)$(COBJSUF): $(shell $(subst $BASE,$(base),$(CDEPSCOMMAND)))) $(CDEPS)))
 
 #new default C build rule
 %.bc %.o: %.c $(CDEPS)
@@ -75,11 +78,13 @@ $(foreach base,$(basename $(sort $(strip $(CTARG)))),\
 
 #construct list of cp rules for each web item
 ifdef WEBTARG
-$(WEBTARG): $(HTMLDIR)/$(filter-out $(EXEDIR),$@)
-       cp -f $< $@
+$(WEBTARG): $(subst $(DISTDIR),$(HTSRCDIR),$(WEBTARG))
+       cp -f $(subst $(DISTDIR),$(HTSRCDIR),$@) $@
 endif
 
-#force including rules for making each lib in this case
+#force including rules for making each lib if
+#we have identified any missing libs
+#(fail if no rule exists)
 ifdef MISSINGLIBS
 include $(foreach lib,$(MISSINGLIBS),.make/lib$(lib).mk)
 endif