Makefile v0.4 driver support added
[henge/apc.git] / Makefile
index d37a162..7968b66 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -21,10 +21,12 @@ CFLAGS ?= -Wall
 CCMD    = $(CC) $(CFLAGS) $(CPPFLAGS) -c -o $@ $<
 
 # Linker
-LD      ?= ld
-LDFLAGS ?= 
-LDLIBS  ?= -lunistring
-LDCMD    = $(LD) $(LDFLAGS) $(LDLIBS) -o $@ $^
+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')
@@ -35,17 +37,19 @@ cSRC  := $(shell find ./src -type f -name '*.c')
 hGEN  := $(ySRC:%.y=%.tab.h)
 cGEN  := $(strip $(ySRC:%.y=%.tab.c) $(rlSRC:%.rl=%.c))
 
-# Functions
-cGENDEP = $(if $(wildcard $1),$(subst src/,,$(filter-out $1 \ %:,$(shell $(CC) -MM -MG $1))),$(info <$1>))
-ldFLAGS = $(strip $(LDFLAGS) $(LDLIBS) $(VA_ARGS))
+# Deps generation function
+cGENDEP = $(if $(wildcard $1),$(subst $(dir $1),,$(filter-out $1 \ %:,$(shell $(CC) -MM -MG $1))),$(info <$1>))
 
-# Rules
-apcSRC   := $(patsubst %.c,%.o,$(cSRC) $(cGEN))
-apc-dSRC := $(patsubst %.o,%-d.o,$(apcSRC))
+# 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
 .SECONDEXPANSION:
-apc-d apc: $$($$@SRC) | $(hGEN)
-       $(strip $(LDCMD))
+$(DRIVERS:%=%-d) $(DRIVERS): $$($$@SRC) | $(hGEN)
+       $(strip $(call LDCMD,$@))
 
 %-d.o: CFLAGS+= -Og -ggdb
 %.o %-d.o: %.c $$(call cGENDEP,$$(dir $$@)%.c)
@@ -59,4 +63,4 @@ apc-d apc: $$($$@SRC) | $(hGEN)
        $(strip $(RLCMD))
 
 clean: $(wildcard $(cGEN) $(hGEN) $(apcSRC) $(apc-dSRC))
-       rm $^
+       $(if $^,rm $^)