Makefile v0.31 fixed object compilation
[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 LDCMD = $(LD) $(LDFLAGS) $(LDLIBS) -o $@ $^
28
29 # APC is built from Ragel, Bison and C source code only.
30 ySRC := $(shell find ./src -type f -name '*.y')
31 rlSRC := $(shell find ./src -type f -name '*.rl')
32 cSRC := $(shell find ./src -type f -name '*.c')
33
34 # Generated files from Yacc/Bison and Ragel
35 hGEN := $(ySRC:%.y=%.tab.h)
36 cGEN := $(strip $(ySRC:%.y=%.tab.c) $(rlSRC:%.rl=%.c))
37
38 # Functions
39 cGENDEP = $(if $(wildcard $1),$(subst src/,,$(filter-out $1 \ %:,$(shell $(CC) -MM -MG $1))),$(info <$1>))
40 ldFLAGS = $(strip $(LDFLAGS) $(LDLIBS) $(VA_ARGS))
41
42 # Rules
43 apcSRC := $(patsubst %.c,%.o,$(cSRC) $(cGEN))
44 apc-dSRC := $(patsubst %.o,%-d.o,$(apcSRC))
45
46 .SECONDEXPANSION:
47 apc-d apc: $$($$@SRC) | $(hGEN)
48 $(strip $(LDCMD))
49
50 %-d.o: CFLAGS+= -Og -ggdb
51 %.o %-d.o: %.c $$(call cGENDEP,$$(dir $$@)%.c)
52 $(strip $(CCMD))
53
54 %.tab.h %.tab.c: %.y
55 $(strip $(YCMD))
56 mv $(notdir $(<:%.y=%.tab.[ch])) $(dir $@)
57
58 %.c: %.rl
59 $(strip $(RLCMD))
60
61 clean: $(wildcard $(cGEN) $(hGEN) $(apcSRC) $(apc-dSRC))
62 rm $^