comments updated
[henge/apc.git] / Makefile
index e10f81c..7b8934d 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -6,7 +6,10 @@
 # This makefile builds APC, the Asset Package Compiler for Henge, on the system.
 ################################################################################
 # Driver sources
-DRIVERS  := apc testapc
+DRIVERS ?= apc testapc testston
+
+# Debug Level
+DEBUG   ?= 1
 
 # Yacc
 YACC   := bison
@@ -16,7 +19,7 @@ YCMD   += $(if $2,&& mv $(notdir $(1:%.y=%.tab.[ch])) $(dir $2))
 
 # Ragel
 RLC     ?= ragel
-RLFALGS ?= -C
+RLFLAGS ?= -C
 RLCMD    = $(strip $(RLC) $(RLFLAGS) $(if $2,-o $2 $(dir $2))$1)
 
 # C
@@ -30,47 +33,58 @@ LDFLAGS   ?=
 LDLIBS    ?= -lunistring
 apcLIBS   ?= 
 apc-dLIBS ?= 
-LDCMD      = $(strip $(LD) $(LDFLAGS) $(LDLIBS) $($1LIBS) $(if $2,-o $2) $1)
+LDCMD      = $(strip $(CC) $(LDFLAGS) $(if $2,-o $2) $1) $(LDLIBS) $($1LIBS) 
 
 # APC is built from Ragel, Bison and C source code only.
-ySRC  := $(shell find ./src -type f -name '*.y')
-rlSRC := $(shell find ./src -type f -name '*.rl')
-cSRC  := $(shell find ./src -type f -name '*.c')
+ySRC  := $(shell find src/ -type f -name '*.y')
+rlSRC := $(shell find src/ -type f -name '*.rl')
+cSRC  := $(shell find src/ -type f -name '*.c')
 
 # Generated files from Yacc/Bison and Ragel
 hGEN  := $(ySRC:%.y=%.tab.h)
-cGEN  := $(strip $(ySRC:%.y=%.tab.c) $(rlSRC:%.rl=%.c))
+cGEN  := $(strip $(ySRC:%.y=%.tab.c) $(rlSRC:%.rl=%.fsm.c))
+.SECONDARY: $(cGEN)
+
+# Determine binary/ir targets (object files and driver binaries)
+cTRG  := $(patsubst %.c,%.o,$(cSRC) $(cGEN))
+ldSRC := $(filter-out $(DRIVERS:%=\%/%.o),$(cTRG))
+cTRG  += $(cTRG:%.o=%-d.o)
+ldTRG := $(DRIVERS:%=%-d) $(DRIVERS)
+ldDEP  = $(filter %/$1.o,$(cTRG))
+ldDEP += $(if $(filter testston%,$1),,$(if $(filter %-d,$1),$(ldSRC:%.o=%-d.o),$(ldSRC)))
 
-# Filter all other driver objects out of each driver's link commands.
-$(foreach drv,$(DRIVERS),\
-$(eval $(drv)SRC   := $(patsubst %.c,%.o,$(filter-out $(patsubst %,src/%.c,$(filter-out $(drv),$(DRIVERS))),$(cSRC) $(cGEN))))\
-$(eval $(drv)-dSRC := $(patsubst %.o,%-d.o,$($(drv)SRC))))
+# Determine if '1' is newer than '2'
+TSTAMP = $(if $(wildcard $1),$(shell stat -c %Y $1),)
+NEWER  = $(eval 4 := $(call TSTAMP,$(dir $2)$1))
+NEWER += $(eval 5 := $(call TSTAMP,$2))
+NEWER += $(if $(filter $5,$(firstword $(sort $4 $5))),$1,$2)
 
-# Unless cleaning, deps should be generated for each source file
 ifeq (,$(filter clean,$(MAKECMDGOALS)))
+# Deps should be generated for each source file, when not cleaning
 cGENDEP = $(if $(wildcard $1),$(subst $(dir $1),,$(filter-out $1 \ %:,$(shell $(CC) -MM -MG $1))),\
 $(info [<$1>: no deps - file not found]))
+# Filter only missing deps
+cMISDEP = $(strip $(foreach dep,$(call cGENDEP,$1),$(if $(wildcard src/$(dep)),,$(dep))))
+# S2S will print the command necessary to create a file when called
+S2S  = $(if $(filter $2,$(call NEWER,$2,$3)),$(eval 4 := t),$(eval 4 :=))
+S2S += $(if $4,$(info $(call $1,$2,$3)))
+ifeq (,$(filter n,$(MAKEFLAGS)))
+# Unless we're in -n mode, S2S should also invoke the command on the shell
+S2S += $(if $4,$(shell $(call $1,$2,$3)))
+endif
 endif
 
-# Construct the S2S function for generating source files during prerequisite
-# expansion:
-# If we are cleaning, stop.  Else print the result of calling '1'.  Then, unless
-# we are in -n mode, invoke the result of calling '1' in the shell
-$(if $(filter clean,$(MAKECMDGOALS)),,$(eval S2S += $$(info $$(call $$1,$$2,$$3)))\
-$(if $(filter n,$(MAKEFLAGS)),,$(eval S2S += $$(shell $$(call $$1,$$2,$$3)))))
+# Clean targets
+cleanCMD = $(if $(wildcard $1),rm $(wildcard $(sort $1)))
 
 # Rules
 .SECONDEXPANSION:
-$(DRIVERS:%=%-d) $(DRIVERS): $$($$@SRC) | $(hGEN)
-       $(strip $(call LDCMD,$^,$@))
-
-%-d.o: CFLAGS+= -Og -ggdb
-%.o %-d.o: %.c $$(call cGENDEP,$$(dir $$@)%.c)
-       $(call CCMD,$<,$@)
-
+$(ldTRG): $$(call ldDEP,$$@) | $(hGEN) ; $(call LDCMD,$^,$@)
+%-d.o: CFLAGS+= -Og -ggdb -DDEBUG=$(DEBUG)
+%.o %-d.o: %.c $$(call cGENDEP,$$(dir $$@)%.c); $(call CCMD,$<,$@)
+%.o %-d.o: %.c; $(error Missing dependencies for $<: $(call cMISDEP, $<))
 %.tab.h: %.tab.c ;
 %.tab.c: %.y $$(call S2S,YCMD,%.y,$$@) ;
-%.c: %.rl $$(call S2S,RLCMD,%.rl,$$@) ;
-
-clean: $(wildcard $(cGEN) $(hGEN) $(apcSRC) $(apc-dSRC))
-       $(if $^,rm $^)
+%.fsm.c: %.rl $$(call S2S,RLCMD,%.rl,$$@) ;
+clean: ; $(call cleanCMD,$(cGEN) $(hGEN) $(cTRG))
+distclean: clean ; $(call cleanCMD,$(ldTRG))