]> cloudbase.mooo.com Git - z180-stamp.git/blame - mk/def_rules.mk
libopencm3 update
[z180-stamp.git] / mk / def_rules.mk
CommitLineData
88d31d11
L
1# For now I depend on flex and bison - I want to generate source files
2# in the same directory as their lex/yacc inputs.
3%.c %.h : %.y
4 bison -d -o $*.c $<
5
6%.c : %.l
7 flex -o$@ $<
8
9# "Pretty printing" stuff
10#
11# The value of the variable VERBOSE decides whether to output only
12# a short note what is being done (e.g. "CC foobar.c") or a full
13# command line.
14#
15# Sometimes you have a code that you're not in charge of and which gives
16# a lot of warnings. In that case you can use colors so that you find
17# easily what is being compiled. By default I check terminal
18# capabilities and use colors only when the terminal support them but you
19# can suppress coloring by setting COLOR_TTY to something else than
20# 'true' (see config.mk).
21# Please don't argue about this choice of colors - I'm always using black
22# background so yellow on black it is :-D - background is specified
23# below just for those using bright background :-P
24COLOR := \033[33;40m
25NOCOLOR := \033[0m
26
27ifndef COLOR_TTY
28ifdef TERM
29COLOR_TTY := $(shell [ `tput colors` -gt 2 ] && echo true)
30endif
31endif
32
33ifneq ($(VERBOSE),true)
34ifneq ($(strip $(TOP_BUILD_DIR)),)
35 strip_top = $(subst $(TOP)/,,$(subst $(TOP_BUILD_DIR)/,,$(1)))
36else
37 strip_top = $(subst $(TOP)/,,$(1))
38endif
39ifeq ($(COLOR_TTY),true)
40echo_prog := $(shell if echo -e | grep -q -- -e; then echo echo; else echo echo -e; fi)
41echo_cmd = @$(echo_prog) "$(COLOR)$(call strip_top,$(1))$(NOCOLOR)";
42else
43echo_cmd = @echo "$(call strip_top,$(1))";
44endif
45else # Verbose output
46echo_cmd =
47endif
48
49COMPILE.c = $(call echo_cmd,CC $<) $(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c
50COMPILE.cc = $(call echo_cmd,CXX $<) $(CXX) $(CXXFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c
51LINK.c = $(call echo_cmd,LINK $@) $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH)
52LINK.cc = $(call echo_cmd,LINK $@) $(CXX) $(CXXFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH)
53
54# This rule is just for running C/C++ preprocessor and saving the output
55# into the file with .E appended - sometimes this can be handy.
56# Suffix E comes from -E option to gcc. Make sure you invoke this rule
57# via full path (e.g.: make $(pwd)/foobar.c.E) if you want to have per
58# directory preprocessor flags included.
59%.E : %
60 $(call echo_cmd,CPP $<) $(CPP) $(CPPFLAGS) -o $@ $<
61
62# Special rule to get easily CPP options for given file. This can be
63# handy for your code "assistant" (e.g. clang) that needs to know
64# preprocessor options in order to parse file properly. In that case
65# you can run: make /path/to/foobar.c.CPPFLAGS | tail -1
66# to get effective flags for foobar.c
67%.CPPFLAGS : %
68 @echo $(CPPFLAGS)
69
70# Create the output directory for build targets.
71%/$(OBJDIR):
72 @mkdir -p $@
73
74# Automatic rules. Again, since the output is in different directory
75# than source files I cannot count on the built in make rules. So
76# I keep them in a "macro" (see 'skeleton' below) that is expanded for
77# every directory with Rules.mk (and its SRCS_VPATH subdirectories).
78# Below setting means that %.o are made from %.cpp and the same for .cc
79# and .c - this is just the list of mappings.
80# You can add your own here. For example to add support for Fortran you
81# would just append ".o:.f" and set COMPILE.f (actually make already
82# defines it - just take a look at output of "make -p")
83AUTO_RULES := .o:.cpp .o:.cc .o:.c
84# Additional mapping together with corresponding variables can be specified
85# in user_rules.mk
86-include $(MK)/user_rules.mk
87
88# This compile command should be generic for most compilers - you should
89# just define appropriate COMPILE.suffix variable.
90COMPILECMD = $(COMPILE$(suffix $<)) -o $@ $<
91
92# In cases where from one source different types of objects can be
93# generated I have added COMPILECMD_TD (TD stands for "target
94# dependent"). So e.g. for OCaml one could use:
95# CAML := ocamlc
96# CAMLOPT := ocamlopt
97# AUTO_TGTS += %.cmo %.cmx # or modify its value in skel.mk
98# COMPILE.cmo.ml = $(call echo_cmd,CAML $<) $(CAML) -c
99# COMPILE.cmx.ml = $(call echo_cmd,CAMLOPT $<) $(CAMLOPT) -c
100# AUTO_TD_RULES := .cmo&.cmx:.ml
101# The syntax for AUTO_TD_RULES is similar to AUTO_RULES but instead of
102# single output suffix (e.g. ".o") you have to list them all separated
103# by "&" (sorry, comma gives me problems since it is part of normal make
104# syntax for functions :P). The above setting means that %.cmo and %.cmx
105# can be generated from %.ml via their respective COMPILE commands
106# expanded in COMPILECMD_TD (see below).
107
108# Again this should be generic enough so that you won't have to touch it
109COMPILECMD_TD = $(COMPILE$(suffix $@)$(suffix $<)) -o $@ $<
110
111# Definition of variable ${\n} containing just new-line character
112define \n
113
114
115endef
116
117# Definition of 'skeleton' macro used in generation of recipes. This
118# definition is itself computed so you should not modify it (unless you
119# know what you are doing :D). It should be enough for you to add new
120# mappings to AUTO_RULES and AUTO_TD_RULES above and define/change
121# corresponding COMPILE.suffix and COMPILE.outsuffix.insuffix variables.
122$(eval define skeleton$(foreach rule,$(AUTO_RULES),${\n}$$(OBJPATH)/%$(subst :,: $$(1)/%,$(rule)) | $$(OBJPATH); $$(value COMPILECMD))${\n}\
123 $(foreach rule,$(AUTO_TD_RULES),${\n}$$(OBJPATH)/%$(subst :,: $$(1)/%,$(subst &, $$(OBJPATH)/%,$(rule))) | $$(OBJPATH); $$(value COMPILECMD_TD))${\n}endef)