Makefile v0.4 driver support added
[henge/apc.git] / Makefile
index 50fd45d..7968b66 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -5,37 +5,62 @@
 ################################################################################
 # 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)
+# Yacc
+YACC   := bison
+YFLAGS ?= -v -d -Wall
+YCMD    = $(YACC) $(YFLAGS) $<
 
-# 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')
+# Ragel
+RLC     ?= ragel
+RLFALGS ?= -C
+RLCMD    = $(RLC) $(RLFLAGS) -o $@ $<
+
+# C
+CC     ?= gcc
+CFLAGS ?= -Wall
+CCMD    = $(CC) $(CFLAGS) $(CPPFLAGS) -c -o $@ $<
 
-# Specify the linker (allow env var override)
-LD    := $(if $(LD),$(LD),ld)
+# Linker
+LD        ?= ld
+LDFLAGS   ?= 
+LDLIBS    ?= -lunistring
+apcLIBS   ?= 
+apc-dLIBS ?= 
+LDCMD      = $(LD) $(LDFLAGS) $(LDLIBS) $($1LIBS) -o $@ $^
+
+# 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))
 
+# Deps generation function
+cGENDEP = $(if $(wildcard $1),$(subst $(dir $1),,$(filter-out $1 \ %:,$(shell $(CC) -MM -MG $1))),$(info <$1>))
+
+# 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))))
+
 # Rules
-apc-d: $(patsubst %.c,%-d.o,$(cSRC) $(cGEN)) | $(hGEN)
-       $(LD) $(LDFLAGS) $(LDLIBS) $<
+.SECONDEXPANSION:
+$(DRIVERS:%=%-d) $(DRIVERS): $$($$@SRC) | $(hGEN)
+       $(strip $(call LDCMD,$@))
 
-apc: $(patsubst %.c,%.o,$(cSRC) $(cGEN)) | $(hGEN)
-       $(LD) $(LDFLAGS) $(LDLIBS) $<
+%-d.o: CFLAGS+= -Og -ggdb
+%.o %-d.o: %.c $$(call cGENDEP,$$(dir $$@)%.c)
+       $(strip $(CCMD))
 
 %.tab.h %.tab.c: %.y
-       bison -d $(YFLAGS) $<
+       $(strip $(YCMD))
+       mv $(notdir $(<:%.y=%.tab.[ch])) $(dir $@)
 
-cFLAGS = $(strip $(CFLAGS) $(CPPFLAGS) $1 $2 $3 $4 $5 $6 $7 $8 $9)
-GENDEP = $(filter-out $1 \ %:,$(shell $(CC) -MM -MG $1))
-.SECONDEXPANSION:
-%.o: %.c $$(info $$(call GENDEP,$$(dir $$@)%.c))
-       $(CC) $(call cFLAGS,-c,-o $@) $<
+%.c: %.rl
+       $(strip $(RLCMD))
 
-%-d.o: %.c $$(info $$@DEPS- $$(call GENDEP,$$(dir $$@)%.c)) $$(call GENDEP,$$(dir $$@)%.c)
-       $(CC) $(call cFLAGS,-c,-Og,-ggdb,-o $@) $<
+clean: $(wildcard $(cGEN) $(hGEN) $(apcSRC) $(apc-dSRC))
+       $(if $^,rm $^)