Makefile v1.0
[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 # Driver sources
9 DRIVERS := apc testapc
10
11 # Yacc
12 YACC := bison
13 YFLAGS ?= -v -d -Wall
14 YCMD = $(strip $(YACC) $(YFLAGS) $(if $2,$(dir $2))$1)
15 YCMD += $(if $2,&& mv $(notdir $(1:%.y=%.tab.[ch])) $(dir $2))
16
17 # Ragel
18 RLC ?= ragel
19 RLFALGS ?= -C
20 RLCMD = $(strip $(RLC) $(RLFLAGS) $(if $2,-o $2 $(dir $2))$1)
21
22 # C
23 CC ?= gcc
24 CFLAGS ?= -Wall
25 CCMD = $(strip $(CC) $(CFLAGS) $(CPPFLAGS) -c $(if $2,-o $2) $1)
26
27 # Linker
28 LD ?= ld
29 LDFLAGS ?=
30 LDLIBS ?= -lunistring
31 apcLIBS ?=
32 apc-dLIBS ?=
33 LDCMD = $(strip $(LD) $(LDFLAGS) $(LDLIBS) $($1LIBS) $(if $2,-o $2) $1)
34
35 # APC is built from Ragel, Bison and C source code only.
36 ySRC := $(shell find ./src -type f -name '*.y')
37 rlSRC := $(shell find ./src -type f -name '*.rl')
38 cSRC := $(shell find ./src -type f -name '*.c')
39
40 # Generated files from Yacc/Bison and Ragel
41 hGEN := $(ySRC:%.y=%.tab.h)
42 cGEN := $(strip $(ySRC:%.y=%.tab.c) $(rlSRC:%.rl=%.c))
43
44 # Filter all other driver objects out of each driver's link commands.
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 # Unless cleaning, deps should be generated for each source file
50 ifeq (,$(filter clean,$(MAKECMDGOALS)))
51 cGENDEP = $(if $(wildcard $1),$(subst $(dir $1),,$(filter-out $1 \ %:,$(shell $(CC) -MM -MG $1))),\
52 $(info [<$1>: no deps - file not found]))
53 endif
54
55 # Construct the S2S function for generating source files during prerequisite
56 # expansion:
57 # If we are cleaning, stop. Else print the result of calling '1'. Then, unless
58 # we are in -n mode, invoke the result of calling '1' in the shell
59 $(if $(filter clean,$(MAKECMDGOALS)),,$(eval S2S += $$(info $$(call $$1,$$2,$$3)))\
60 $(if $(filter n,$(MAKEFLAGS)),,$(eval S2S += $$(shell $$(call $$1,$$2,$$3)))))
61
62 # Rules
63 .SECONDEXPANSION:
64 $(DRIVERS:%=%-d) $(DRIVERS): $$($$@SRC) | $(hGEN)
65 $(strip $(call LDCMD,$^,$@))
66
67 %-d.o: CFLAGS+= -Og -ggdb
68 %.o %-d.o: %.c $$(call cGENDEP,$$(dir $$@)%.c)
69 $(call CCMD,$<,$@)
70
71 %.tab.h: %.tab.c ;
72 %.tab.c: %.y $$(call S2S,YCMD,%.y,$$@) ;
73 %.c: %.rl $$(call S2S,RLCMD,%.rl,$$@) ;
74
75 clean: $(wildcard $(cGEN) $(hGEN) $(apcSRC) $(apc-dSRC))
76 $(if $^,rm $^)