# Linker
LD ?= ld
LDFLAGS ?=
-LDLIBS ?= -lunistring
+LDLIBS ?= -lc -lunistring
apcLIBS ?=
apc-dLIBS ?=
-LDCMD = $(strip $(LD) $(LDFLAGS) $(LDLIBS) $($1LIBS) $(if $2,-o $2) $1)
+LDCMD = $(strip $(LD) $(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')
cGEN := $(strip $(ySRC:%.y=%.tab.c) $(rlSRC:%.rl=%.c))
# Filter all other driver objects out of each driver's link commands.
+OBJ := $(patsubst %.c,%.o,$(cSRC) $(cGEN))
$(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))))
+$(eval OTHERS := $(filter-out $(drv),$(DRIVERS)))\
+$(eval $(drv)SRC := $(filter-out $(OTHERS:%=\%/%.o),$(OBJ)))\
+$(eval $(drv)-dSRC := $(filter-out $(OTHERS:%=\%/%-d.o),$(OBJ:%.o=%-d.o))))
# Unless cleaning, deps should be generated for each source file
ifeq (,$(filter clean,$(MAKECMDGOALS)))
%.tab.c: %.y $$(call S2S,YCMD,%.y,$$@) ;
%.c: %.rl $$(call S2S,RLCMD,%.rl,$$@) ;
-clean: $(wildcard $(cGEN) $(hGEN) $(apcSRC) $(apc-dSRC))
+clean: $(wildcard $(cGEN) $(hGEN) $(foreach drv,$(DRIVERS),$($(drv)SRC) $($(drv)-dSRC)))
$(if $^,rm $^)