62bf1193ccb17282f4c383ae321a64a51ac9bfe6
[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 # Debug Level
12 DEBUG ?= 1
13
14 # Yacc
15 YACC := bison
16 YFLAGS ?= -v -d -Wall
17 YCMD = $(strip $(YACC) $(YFLAGS) $(if $2,$(dir $2))$1)
18 YCMD += $(if $2,&& mv $(notdir $(1:%.y=%.tab.[ch])) $(dir $2))
19
20 # Ragel
21 RLC ?= ragel
22 RLFLAGS ?= -C
23 RLCMD = $(strip $(RLC) $(RLFLAGS) $(if $2,-o $2 $(dir $2))$1)
24
25 # C
26 CC ?= gcc
27 CFLAGS ?= -Wall
28 CCMD = $(strip $(CC) $(CFLAGS) $(CPPFLAGS) -c $(if $2,-o $2) $1)
29
30 # Linker
31 LD ?= ld
32 LDFLAGS ?=
33 LDLIBS ?= -lunistring
34 apcLIBS ?=
35 apc-dLIBS ?=
36 LDCMD = $(strip $(CC) $(LDFLAGS) $(if $2,-o $2) $1) $(LDLIBS) $($1LIBS)
37
38 # APC is built from Ragel, Bison and C source code only.
39 ySRC := $(shell find src/ -type f -name '*.y')
40 rlSRC := $(shell find src/ -type f -name '*.rl')
41 cSRC := $(shell find src/ -type f -name '*.c')
42
43 # Generated files from Yacc/Bison and Ragel
44 hGEN := $(ySRC:%.y=%.tab.h)
45 cGEN := $(strip $(ySRC:%.y=%.tab.c) $(rlSRC:%.rl=%.fsm.c))
46 .SECONDARY: $(cGEN)
47
48 # Determine binary/ir targets (object files and driver binaries)
49 cTRG := $(patsubst %.c,%.o,$(cSRC) $(cGEN))
50 ldSRC := $(filter-out $(DRIVERS:%=\%/%.o),$(cTRG))
51 cTRG += $(cTRG:%.o=%-d.o)
52 ldTRG := $(DRIVERS:%=%-d) $(DRIVERS)
53 ldDEP = $(filter %/$1.o,$(cTRG)) $(if $(filter %-d,$1),$(ldSRC:%.o=%-d.o),$(ldSRC))
54
55 # Determine if '1' is newer than '2'
56 TSTAMP = $(if $(wildcard $1),$(shell stat -c %Y $1),0$(info nots: $1))
57 NEWER = $(eval 4 := $(call TSTAMP,$(dir $2)$1))
58 NEWER += $(eval 5 := $(call TSTAMP,$2))
59 NEWER += $(if $(filter $5,$(firstword $(sort $4 $5))),$1,$2)
60
61 ifeq (,$(filter clean,$(MAKECMDGOALS)))
62 # Deps should be generated for each source file, when not cleaning
63 cGENDEP = $(if $(wildcard $1),$(subst $(dir $1),,$(filter-out $1 \ %:,$(shell $(CC) -MM -MG $1))),\
64 $(info [<$1>: no deps - file not found]))
65 # Filter only missing deps
66 cMISDEP = $(strip $(foreach dep,$(call cGENDEP,$1),$(if $(wildcard src/$(dep)),,$(dep))))
67 # S2S will print the command necessary to create a file when called
68 S2S = $(if $(filter $2,$(call NEWER,$2,$3)),$(eval 4 := t),$(eval 4 :=))
69 S2S += $(if $4,$(info $(call $1,$2,$3)))
70 ifeq (,$(filter n,$(MAKEFLAGS)))
71 # Unless we're in -n mode, S2S should also invoke the command on the shell
72 S2S += $(if $4,$(shell $(call $1,$2,$3)))
73 endif
74 endif
75
76 # Clean targets
77 cleanCMD = $(if $(wildcard $1),rm $(wildcard $(sort $1)))
78
79 # Rules
80 .SECONDEXPANSION:
81 $(ldTRG): $$(call ldDEP,$$@) | $(hGEN) ; $(call LDCMD,$^,$@)
82 %-d.o: CFLAGS+= -Og -ggdb -DDEBUG=$(DEBUG)
83 %.o %-d.o: %.c $$(call cGENDEP,$$(dir $$@)%.c); $(call CCMD,$<,$@)
84 %.o %-d.o: %.c; $(error Missing dependencies for $<: $(call cMISDEP, $<))
85 %.tab.h: %.tab.c ;
86 %.tab.c: %.y $$(call S2S,YCMD,%.y,$$@) ;
87 %.fsm.c: %.rl $$(call S2S,RLCMD,%.rl,$$@) ;
88 clean: ; $(call cleanCMD,$(cGEN) $(hGEN) $(cTRG))
89 distclean: clean ; $(call cleanCMD,$(ldTRG))