added distclean, simplified some rules
[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 $(CC) $(LDFLAGS) $(if $2,-o $2) $1) $(LDLIBS) $($1LIBS)
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=%.fsm.c))
43
44 # Determine binary/ir targets (object files and driver binaries)
45 cTRG := $(patsubst %.c,%.o,$(cSRC) $(cGEN))
46 ldSRC := $(filter-out $(DRIVERS:%=\%/%.o),$(cTRG))
47 cTRG += $(cTRG:%.o=%-d.o)
48 ldTRG := $(DRIVERS:%=%-d) $(DRIVERS)
49 ldDEP = $(filter %/$1.o,$(cTRG)) $(if $(filter %-d,$1),$(ldSRC:%.o=%-d.o),$(ldSRC))
50
51 ifeq (,$(filter clean,$(MAKECMDGOALS)))
52 # Deps should be generated for each source file, when not cleaning
53 cGENDEP = $(if $(wildcard $1),$(subst $(dir $1),,$(filter-out $1 \ %:,$(shell $(CC) -MM -MG $1))),\
54 $(info [<$1>: no deps - file not found]))
55 # S2S will print the command necessary to create a file when called
56 S2S = $(info $(call $1,$2,$3))
57 ifeq (,$(filter n,$(MAKEFLAGS)))
58 # Unless we're in -n mode, S2S should also invoke the command on the shell
59 S2S += $(shell $(call $1,$2,$3))
60 endif
61 endif
62
63 # Clean targets
64 cleanCMD = $(if $(wildcard $1),rm $(wildcard $1))
65
66 # Rules
67 .SECONDEXPANSION:
68 $(ldTRG): $$(call ldDEP,$$@) | $(hGEN) ; $(call LDCMD,$^,$@)
69 %-d.o: CFLAGS+= -Og -ggdb
70 %.o %-d.o: %.c $$(call cGENDEP,$$(dir $$@)%.c) ; $(call CCMD,$<,$@)
71 %.tab.h: %.tab.c ;
72 %.tab.c: %.y $$(call S2S,YCMD,%.y,$$@) ;
73 %.fsm.c: %.rl $$(call S2S,RLCMD,%.rl,$$@) ;
74 clean: ; $(call cleanCMD,$(cGEN) $(hGEN) $(cTRG))
75 distclean: clean ; $(call cleanCMD,$(ldTRG))