]> cloudbase.mooo.com Git - z180-stamp.git/blob - mk/def_rules.mk
9197b03f2135f30753b29fc770f11a2afb0f3f2e
[z180-stamp.git] / mk / def_rules.mk
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
24 COLOR := \033[33;40m
25 NOCOLOR := \033[0m
26
27 ifndef COLOR_TTY
28 ifdef TERM
29 COLOR_TTY := $(shell [ `tput colors` -gt 2 ] && echo true)
30 endif
31 endif
32
33 ifneq ($(VERBOSE),true)
34 ifneq ($(strip $(TOP_BUILD_DIR)),)
35 strip_top = $(subst $(TOP)/,,$(subst $(TOP_BUILD_DIR)/,,$(1)))
36 else
37 strip_top = $(subst $(TOP)/,,$(1))
38 endif
39 ifeq ($(COLOR_TTY),true)
40 echo_prog := $(shell if echo -e | grep -q -- -e; then echo echo; else echo echo -e; fi)
41 echo_cmd = @$(echo_prog) "$(COLOR)$(call strip_top,$(1))$(NOCOLOR)";
42 else
43 echo_cmd = @echo "$(call strip_top,$(1))";
44 endif
45 else # Verbose output
46 echo_cmd =
47 endif
48
49 COMPILE.c = $(call echo_cmd,CC $<) $(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c
50 COMPILE.cc = $(call echo_cmd,CXX $<) $(CXX) $(CXXFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c
51 LINK.c = $(call echo_cmd,LINK $@) $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH)
52 LINK.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")
83 AUTO_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.
90 COMPILECMD = $(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
109 COMPILECMD_TD = $(COMPILE$(suffix $@)$(suffix $<)) -o $@ $<
110
111 # Definition of variable ${\n} containing just new-line character
112 define \n
113
114
115 endef
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)