################################################################################
# This makefile builds APC, the Asset Package Compiler for Henge, on the system.
################################################################################
-# Directories (can override with env vars)
-APC_ROOTDIR := $(if $(APC_ROOTDIR),$(APC_ROOTDIR),.)
-APC_SRCDIR := $(if $(APC_SRCDIR),$(APC_SRCDIR),$(APC_ROOTDIR)/src)
+# Driver sources
+DRIVERS := apc testapc
-# APC is built from Ragel, Bison and C source code only.
-ySRC := $(shell find $(APC_SRCDIR) -type f -name '*.y')
-rlSRC := $(shell find $(APC_SRCDIR) -type f -name '*.rl')
-cSRC := $(shell find $(APC_SRCDIR) -type f -name '*.c')
+# Yacc
+YACC := bison
+YFLAGS ?= -v -d -Wall
+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 = $(strip $(RLC) $(RLFLAGS) $(if $2,-o $2 $(dir $2))$1)
+
+# C
+CC ?= gcc
+CFLAGS ?= -Wall
+CCMD = $(strip $(CC) $(CFLAGS) $(CPPFLAGS) -c $(if $2,-o $2) $1)
-# Specify the linker (allow env var override)
-LD := $(if $(LD),$(LD),ld)
+# Linker
+LD ?= ld
+LDFLAGS ?=
+LDLIBS ?= -lunistring
+apcLIBS ?=
+apc-dLIBS ?=
+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')
# 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))
-# Rules
-apc-d: $(patsubst %.c,%-d.o,$(cSRC) $(cGEN)) | $(hGEN)
- $(LD) $(LDFLAGS) $(LDLIBS) $<
-
-apc: $(patsubst %.c,%.o,$(cSRC) $(cGEN)) | $(hGEN)
- $(LD) $(LDFLAGS) $(LDLIBS) $<
+# Filter all other driver objects out of each driver's link commands.
+OBJ := $(patsubst %.c,%.o,$(cSRC) $(cGEN))
+$(foreach drv,$(DRIVERS),\
+$(eval OTHERS := $(filter-out $(drv),$(DRIVERS)))\
+$(eval $(drv)SRC := $(filter-out $(OTHERS:%=\%/%.o),$(OBJ)))\
+$(eval $(drv)-dSRC := $($(drv)SRC:%.o=%-d.o)))
-%.tab.h %.tab.c: %.y
- bison -d $(YFLAGS) $<
+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
-cFLAGS = $(strip $(CFLAGS) $(CPPFLAGS) $1 $2 $3 $4 $5 $6 $7 $8 $9)
-GENDEP = $(filter-out \ %:,$(shell $(CC) -MM -MG $1))
+# Rules
.SECONDEXPANSION:
-%.o: %.c $$(call GENDEP,$$(dir $$@)%.c)
- $(CC) $(call cFLAGS,-c,-o $@) $<
+$(DRIVERS:%=%-d) $(DRIVERS): $$($$@SRC) | $(hGEN)
+ $(call LDCMD,$^,$@)
+
+%-d.o: CFLAGS+= -Og -ggdb
+%.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,$$@) ;
-%-d.o: $$(call GENDEP,$$(dir $$@)%.c) $$(info [DEPS|$$@] $$(call GENDEP,$$(dir $$@)%.c))
- $(CC) $(call cFLAGS,-c,-Og,-ggdb,-o $@) $<
+clean: $(wildcard $(cGEN) $(hGEN) $(foreach drv,$(DRIVERS),$($(drv)SRC) $($(drv)-dSRC)))
+ $(if $^,rm $^)