################################################################################
# This makefile builds APC, the Asset Package Compiler for Henge, on the system.
################################################################################
+# Driver sources
+DRIVERS ?= apc testapc
+
# Yacc
YACC := bison
YFLAGS ?= -v -d -Wall
-YCMD = $(YACC) $(YFLAGS) $<
+YCMD = $(strip $(YACC) $(YFLAGS) $(if $2,$(dir $2))$1)
+YCMD += $(if $2,&& mv $(notdir $(1:%.y=%.tab.[ch])) $(dir $2))
# Ragel
RLC ?= ragel
RLFALGS ?= -C
-RLCMD = $(RLC) $(RLFLAGS) -o $@ $<
+RLCMD = $(strip $(RLC) $(RLFLAGS) $(if $2,-o $2 $(dir $2))$1)
# C
CC ?= gcc
CFLAGS ?= -Wall
-CCMD = $(CC) $(CFLAGS) $(CPPFLAGS) -c -o $@ $<
+CCMD = $(strip $(CC) $(CFLAGS) $(CPPFLAGS) -c $(if $2,-o $2) $1)
# Linker
LD ?= ld
LDLIBS ?= -lunistring
apcLIBS ?=
apc-dLIBS ?=
-LDCMD = $(LD) $(LDFLAGS) $(LDLIBS) $($1LIBS) -o $@ $^
+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')
# 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))
-# Deps generation function
-cGENDEP = $(if $(wildcard $1),$(subst $(dir $1),,$(filter-out $1 \ %:,$(shell $(CC) -MM -MG $1))),$(info <$1>))
+# 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)) $(if $(filter %-d,$1),$(ldSRC:%.o=%-d.o),$(ldSRC))
-# Driver sources
-DRIVERS := apc testapc
-$(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))))
+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]))
+# S2S will print the command necessary to create a file when called
+S2S = $(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 += $(shell $(call $1,$2,$3))
+endif
+endif
+
+# Clean targets
+cleanCMD = $(if $(wildcard $1),rm $(wildcard $1))
# Rules
.SECONDEXPANSION:
-$(DRIVERS:%=%-d) $(DRIVERS): $$($$@SRC) | $(hGEN)
- $(strip $(call LDCMD,$@))
-
+$(ldTRG): $$(call ldDEP,$$@) | $(hGEN) ; $(call LDCMD,$^,$@)
%-d.o: CFLAGS+= -Og -ggdb
-%.o %-d.o: %.c $$(call cGENDEP,$$(dir $$@)%.c)
- $(strip $(CCMD))
-
-%.tab.h %.tab.c: %.y
- $(strip $(YCMD))
- mv $(notdir $(<:%.y=%.tab.[ch])) $(dir $@)
-
-%.c: %.rl
- $(strip $(RLCMD))
-
-clean: $(wildcard $(cGEN) $(hGEN) $(apcSRC) $(apc-dSRC))
- $(if $^,rm $^)
+%.o %-d.o: %.c $$(call cGENDEP,$$(dir $$@)%.c) ; $(call CCMD,$<,$@)
+%.tab.h: %.tab.c ;
+%.tab.c: %.y $$(call S2S,YCMD,%.y,$$@) ;
+%.fsm.c: %.rl $$(call S2S,RLCMD,%.rl,$$@) ;
+clean: ; $(call cleanCMD,$(cGEN) $(hGEN) $(cTRG))
+distclean: clean ; $(call cleanCMD,$(ldTRG))