Makefile v0.4 driver support added
[henge/apc.git] / Makefile
1 ################################################################################
2 # Desc: APC make script
3 # Author: kgr
4 # Date: 2016
5 ################################################################################
6 # This makefile builds APC, the Asset Package Compiler for Henge, on the system.
7 ################################################################################
8 # Yacc
9 YACC := bison
10 YFLAGS ?= -v -d -Wall
11 YCMD = $(YACC) $(YFLAGS) $<
12
13 # Ragel
14 RLC ?= ragel
15 RLFALGS ?= -C
16 RLCMD = $(RLC) $(RLFLAGS) -o $@ $<
17
18 # C
19 CC ?= gcc
20 CFLAGS ?= -Wall
21 CCMD = $(CC) $(CFLAGS) $(CPPFLAGS) -c -o $@ $<
22
23 # Linker
24 LD ?= ld
25 LDFLAGS ?=
26 LDLIBS ?= -lunistring
27 apcLIBS ?=
28 apc-dLIBS ?=
29 LDCMD = $(LD) $(LDFLAGS) $(LDLIBS) $($1LIBS) -o $@ $^
30
31 # APC is built from Ragel, Bison and C source code only.
32 ySRC := $(shell find ./src -type f -name '*.y')
33 rlSRC := $(shell find ./src -type f -name '*.rl')
34 cSRC := $(shell find ./src -type f -name '*.c')
35
36 # Generated files from Yacc/Bison and Ragel
37 hGEN := $(ySRC:%.y=%.tab.h)
38 cGEN := $(strip $(ySRC:%.y=%.tab.c) $(rlSRC:%.rl=%.c))
39
40 # Deps generation function
41 cGENDEP = $(if $(wildcard $1),$(subst $(dir $1),,$(filter-out $1 \ %:,$(shell $(CC) -MM -MG $1))),$(info <$1>))
42
43 # Driver sources
44 DRIVERS := apc testapc
45 $(foreach drv,$(DRIVERS),\
46 $(eval $(drv)SRC := $(patsubst %.c,%.o,$(filter-out $(patsubst %,src/%.c,$(filter-out $(drv),$(DRIVERS))),$(cSRC) $(cGEN))))\
47 $(eval $(drv)-dSRC := $(patsubst %.o,%-d.o,$($(drv)SRC))))
48
49 # Rules
50 .SECONDEXPANSION:
51 $(DRIVERS:%=%-d) $(DRIVERS): $$($$@SRC) | $(hGEN)
52 $(strip $(call LDCMD,$@))
53
54 %-d.o: CFLAGS+= -Og -ggdb
55 %.o %-d.o: %.c $$(call cGENDEP,$$(dir $$@)%.c)
56 $(strip $(CCMD))
57
58 %.tab.h %.tab.c: %.y
59 $(strip $(YCMD))
60 mv $(notdir $(<:%.y=%.tab.[ch])) $(dir $@)
61
62 %.c: %.rl
63 $(strip $(RLCMD))
64
65 clean: $(wildcard $(cGEN) $(hGEN) $(apcSRC) $(apc-dSRC))
66 $(if $^,rm $^)