1 ################################################################################
2 # Desc: APC make script
5 ################################################################################
6 # This makefile builds APC, the Asset Package Compiler for Henge, on the system.
7 ################################################################################
9 DRIVERS ?
= apc testapc testston
17 YCMD
= $(strip $(YACC
) $(YFLAGS
) $(if
$2,$(dir $2))$1)
18 YCMD
+= $(if
$2,&& mv
$(notdir $(1:%.y
=%.tab.
[ch
])) $(dir $2))
23 RLCMD
= $(strip $(RLC
) $(RLFLAGS
) $(if
$2,-o
$2 $(dir $2))$1)
28 CCMD
= $(strip $(CC
) $(CFLAGS
) $(CPPFLAGS
) -c
$(if
$2,-o
$2) $1)
36 LDCMD
= $(strip $(CC
) $(LDFLAGS
) $(if
$2,-o
$2) $1) $(LDLIBS
) $($1LIBS)
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')
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
))
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
))
54 ldDEP
+= $(if
$(filter testston
%,$1),,$(if
$(filter %-d
,$1),$(ldSRC
:%.o
=%-d.o
),$(ldSRC
)))
56 # Determine if '1' is newer than '2'
57 TSTAMP
= $(if
$(wildcard $1),$(shell stat
-c
%Y
$1),)
58 NEWER
= $(eval
4 := $(call TSTAMP
,$(dir $2)$1))
59 NEWER
+= $(eval
5 := $(call TSTAMP
,$2))
60 NEWER
+= $(if
$(filter $5,$(firstword $(sort $4 $5))),$1,$2)
62 ifeq (,$(filter clean,$(MAKECMDGOALS
)))
63 # Deps should be generated for each source file, when not cleaning
64 cGENDEP
= $(if
$(wildcard $1),$(subst $(dir $1),,$(filter-out $1 \
%:,$(shell $(CC
) -MM
-MG
$1))),\
65 $(info [<$1>: no deps
- file not found
]))
66 # Filter only missing deps
67 cMISDEP
= $(strip $(foreach dep
,$(call cGENDEP
,$1),$(if
$(wildcard src
/$(dep
)),,$(dep
))))
68 # S2S will print the command necessary to create a file when called
69 S2S
= $(if
$(filter $2,$(call NEWER
,$2,$3)),$(eval
4 := t
),$(eval
4 :=))
70 S2S
+= $(if
$4,$(info $(call
$1,$2,$3)))
71 ifeq (,$(filter n
,$(MAKEFLAGS
)))
72 # Unless we're in -n mode, S2S should also invoke the command on the shell
73 S2S
+= $(if
$4,$(shell $(call
$1,$2,$3)))
78 cleanCMD
= $(if
$(wildcard $1),rm $(wildcard $(sort $1)))
82 $(ldTRG
): $$(call ldDEP
,$$@
) |
$(hGEN
) ; $(call LDCMD
,$^
,$@
)
83 %-d.o
: CFLAGS
+= -Og
-ggdb
-DDEBUG
=$(DEBUG
)
84 %.o
%-d.o
: %.c
$$(call cGENDEP
,$$(dir $$@
)%.c
); $(call CCMD
,$<,$@
)
85 %.o
%-d.o
: %.c
; $(error Missing dependencies for
$<: $(call cMISDEP
, $<))
87 %.tab.c
: %.y
$$(call S2S
,YCMD
,%.y
,$$@
) ;
88 %.fsm.c
: %.rl
$$(call S2S
,RLCMD
,%.rl
,$$@
) ;
89 clean: ; $(call cleanCMD
,$(cGEN
) $(hGEN
) $(cTRG
))
90 distclean: clean ; $(call cleanCMD
,$(ldTRG
))