]> cloudbase.mooo.com Git - z180-stamp.git/blob - mk/skel.mk
99547ea08eb138653c0cadfe35e9adedbc220489
[z180-stamp.git] / mk / skel.mk
1 # For the reference here are some automatic variables defined by make.
2 # There are also their D/F variants e.g. $(<D) - check the manual.
3 #
4 # $@ - file name of the target of the rule
5 # $% - target member name when the target is archive member
6 # $< - the name of the first dependency
7 # $? - the names of all dependencies that are newer then the target
8 # $^ - the names of all dependencies
9
10 ########################################################################
11 # User defined variables #
12 ########################################################################
13
14 # VERB_VARS is a list of variables that you'd like to record on per
15 # directory level. So if you set it to say AUTOTEST then each each
16 # directory will have it's own AUTOTEST_$(dir) variable with value taken
17 # from appropriate Rules.mk
18 VERB_VARS :=
19
20 # OBJ_VARS - like VERB_VARS but all values taken from Rules.mk have
21 # $(OBJPATH) prepended so instead of saying:
22 # INSTALL_$d := $(OBJPATH)/some_target
23 # you can say simply
24 # INSTALL := some_target
25 OBJ_VARS := INSTALL_BIN INSTALL_LIB
26
27 # DIR_VARS - like VERB_VARS but all values taken from Rules.mk have $(d)
28 # prepended (unless value is an absolute path) so you can say:
29 # INSTALL_DOC := Readme.txt
30 # instead of:
31 # INSTALL_DOC_$d := $(d)/Readme.txt
32 DIR_VARS := INSTALL_DOC INSTALL_INC
33
34 # NOTE: There is generic macro defined below with which you can get all
35 # values of given variable from some subtree e.g.:
36 # $(call get_subtree,INSTALL,dir)
37 # will give you value of all INSTALL variables from tree starting at
38 # 'dir'
39
40 ########################################################################
41 # Directory specific flags #
42 ########################################################################
43
44 # You just define in Rules.mk say
45 # INCLUDES_$(d) := ....
46 # and this will get expanded properly during compilation (see e.g. COMPILE.c)
47 # Of course you can still use the target specific variables if you want
48 # to have special setting for just one target and not the whole
49 # directory. See below for definition of @RD variable.
50 DIR_INCLUDES = $(addprefix -I,$(INCLUDES_$(@RD)))
51 DIR_CPPFLAGS = $(CPPFLAGS_$(@RD))
52 DIR_CFLAGS = $(CFLAGS_$(@RD))
53 DIR_CXXFLAGS = $(CXXFLAGS_$(@RD))
54
55 ########################################################################
56 # Global flags/settings #
57 ########################################################################
58
59 CFLAGS = -g -W -Wall $(DIR_CFLAGS)
60 CXXFLAGS = -g -W -Wall $(DIR_CXXFLAGS)
61
62 OPT_FLAGS := -O3
63
64 # List of includes that all (or at least majority) needs
65 INCLUDES :=
66
67 # Here's an example of settings for preprocessor. -MMD is to
68 # automatically build dependency files as a side effect of compilation.
69 # This has some drawbacks (e.g. when you move/rename a file) but it is
70 # good enough for me. You can improve this by using a special script
71 # that builds the dependency files (one can find examples on the web).
72 # Note that I'm adding DIR_INCLUDES before INCLUDES so that they have
73 # precedence.
74 CPPFLAGS = -MMD -D_REENTRANT -D_POSIX_C_SOURCE=200112L -D__EXTENSIONS__ \
75 $(DIR_CPPFLAGS) $(DIR_INCLUDES) $(addprefix -I,$(INCLUDES))
76
77 # Linker flags. The values below will use what you've specified for
78 # particular target or directory but if you have some flags or libraries
79 # that should be used for all targets/directories just append them at end.
80 LDFLAGS = $(LDFLAGS_$(@)) $(addprefix -L,$(LIBDIRS_$(@RD)))
81
82 # List of libraries that all targets need (either with specific command
83 # generated by this makefile system or for which make has built in rules
84 # since LDLIBS is a variable that implicit make rules are using).
85 # LDLIBS can be either simple or recursive, but simpler version is
86 # suggested :).
87 LDLIBS :=
88
89 ########################################################################
90 # The end of generic flags #
91 ########################################################################
92
93 # Now we suck in configuration ...
94 include $(MK)/config.mk
95
96 # ... optional build mode specific flags ...
97 ifdef BUILD_MODE
98 -include $(MK)/build-$(BUILD_MODE).mk
99 endif
100
101 # ... host and build specific settings ...
102 ifneq ($(wildcard $(MK)/config-$(BUILD_ARCH)_$(HOST_ARCH).mk),)
103 include $(MK)/config-$(BUILD_ARCH)_$(HOST_ARCH).mk
104 else
105 include $(MK)/config-default.mk
106 endif
107
108 # ... and here's a good place to translate some of these settings into
109 # compilation flags/variables. As an example a preprocessor macro for
110 # target endianess
111 ifeq ($(ENDIAN),big)
112 CPPFLAGS += -DBIG_ENDIAN
113 else
114 CPPFLAGS += -DLITTLE_ENDIAN
115 endif
116
117 # Use host/build specific config files to override default extension
118 # for shared libraries
119 SOEXT := $(or $(SOEXT),so)
120
121 ########################################################################
122 # A more advanced part - if you change anything below #
123 # you should have at least vague idea how this works :D #
124 ########################################################################
125
126 # I define these for convenience - you can use them in your command for
127 # updating the target.
128 DEP_OBJS = $(filter %.o, $^)
129 DEP_ARCH = $(filter %.a, $^)
130 DEP_LIBS = $(addprefix -L,$(dir $(filter %.$(SOEXT), $^))) $(patsubst lib%.$(SOEXT),-l%,$(notdir $(filter %.$(SOEXT), $^)))
131
132 # Kept for backward compatibility - you should stop using these since
133 # I'm now not dependent on $(OBJDIR)/.fake_file any more
134 ?R = $?
135 ^R = $^
136
137 # Targets that match this pattern (make pattern) will use rules defined
138 # in:
139 # - def_rules.mk included below (explicit or via `skeleton' macro)
140 # - built in make rules
141 # Other targets will have to use _DEPS (and so on) variables which are
142 # saved in `save_vars' and used in `tgt_rule' (see below).
143 AUTO_TGTS := %.o
144
145 # Where to put the compiled objects. You can e.g. make it different
146 # depending on the target platform (e.g. for cross-compilation a good
147 # choice would be OBJDIR := obj/$(HOST_ARCH)) or debugging being on/off.
148 OBJDIR := $(if $(BUILD_MODE),obj/$(BUILD_MODE),obj)
149
150 # Convenience function to convert from a build directory back to the
151 # "real directory" of a target
152 define build_to_real_dir
153 $(if $(strip $(TOP_BUILD_DIR)),$(patsubst $(TOP_BUILD_DIR)%/$(OBJDIR),$(TOP)%,$(1)),$(patsubst %/$(OBJDIR),%,$(1)))
154 endef
155
156 # Convenience function to convert from the "real directory" to the build
157 # directory
158 define real_to_build_dir
159 $(if $(strip $(TOP_BUILD_DIR)),$(TOP_BUILD_DIR)$(subst $(TOP),,$(1))/$(OBJDIR),$(1)/$(OBJDIR))
160 endef
161
162 # By default OBJDIR is relative to the directory of the corresponding Rules.mk
163 # however you can use TOP_BUILD_DIR to build all objects outside of your
164 # project tree. This should be an absolute path. Note that it can be
165 # also inside your project like example below.
166 #TOP_BUILD_DIR := $(TOP)/build_dir
167 OBJPATH = $(call real_to_build_dir,$(d))
168 CLEAN_DIR = $(call real_to_build_dir,$(subst clean_,,$@))
169 DIST_CLEAN_DIR = $(patsubst %/$(OBJDIR),%/$(firstword $(subst /, ,$(OBJDIR))),\
170 $(call real_to_build_dir,$(subst dist_clean_,,$@)))
171
172 # This variable contains a list of subdirectories where to look for
173 # sources. That is if you have some/dir/Rules.mk where you name object
174 # say client.o this object will be created in some/dir/$(OBJDIR)/ and
175 # corresponding source file will be searched in some/dir and in
176 # some/dir/{x,y,z,...} where "x y z ..." is value of this variable.
177 SRCS_VPATH := src
178
179 # Target "real directory" - this is used above already and is most
180 # reliable way to refer to "per directory flags". In theory one could
181 # use automatic variable already defined by make "<D" but this will not
182 # work well when somebody uses SRCS_VPATH variable.
183 @RD = $(call build_to_real_dir,$(@D))
184
185 # These are commands that are used to update the target. If you have
186 # a target that make handles with built in rules just add its pattern to
187 # the AUTO_TGTS below. Otherwise you have to supply the command and you
188 # can either do it explicitly with _CMD variable or based on the
189 # target's suffix and corresponding MAKECMD variable. For example %.a
190 # are # updated by MAKECMD.a (exemplary setting below). If the target
191 # is not filtered out by AUTO_TGTS and there's neither _CMD nor suffix
192 # specific command to build the target DEFAULT_MAKECMD is used.
193 MAKECMD.a = $(call echo_cmd,AR $@) $(AR) $(ARFLAGS) $@ $(DEP_OBJS) && $(RANLIB) $@
194 MAKECMD.$(SOEXT) = $(LINK.cc) $(DEP_OBJS) $(DEP_ARCH) $(DEP_LIBS) $(LIBS_$(@)) $(LDLIBS) -shared -o $@
195 DEFAULT_MAKECMD = $(LINK.cc) $(DEP_OBJS) $(DEP_ARCH) $(DEP_LIBS) $(LIBS_$(@)) $(LDLIBS) -o $@
196
197 ########################################################################
198 # Below is a "Blood sugar sex^H^H^Hmake magik" :) - don't touch it #
199 # unless you know what you are doing. #
200 ########################################################################
201
202 # This can be useful. E.g. if you want to set INCLUDES_$(d) for given
203 # $(d) to the same value as includes for its parent directory plus some
204 # add ons then: INCLUDES_$(d) := $(INCLUDES_$(parent_dir)) ...
205 parent_dir = $(patsubst %/,%,$(dir $(d)))
206
207 define include_subdir_rules
208 dir_stack := $(d) $(dir_stack)
209 d := $(d)/$(1)
210 $$(eval $$(value HEADER))
211 include $(addsuffix /Rules.mk,$$(d))
212 $$(eval $$(value FOOTER))
213 d := $$(firstword $$(dir_stack))
214 dir_stack := $$(wordlist 2,$$(words $$(dir_stack)),$$(dir_stack))
215 endef
216
217 define save_vars
218 DEPS_$(1)$(2) = $(value $(2)_DEPS)
219 LIBS_$(1)$(2) = $(value $(2)_LIBS)
220 LDFLAGS_$(1)$(2) = $(value $(2)_LDFLAGS)
221 CMD_$(1)$(2) = $(value $(2)_CMD)
222 $(2)_DEPS =
223 $(2)_LIBS =
224 $(2)_LDFLAGS =
225 $(2)_CMD =
226 endef
227
228 define tgt_rule
229 abs_deps := $$(foreach dep,$$(DEPS_$(1)),$$(if $$(or $$(filter /%,$$(dep)),$$(filter $$$$%,$$(dep))),$$(dep),$$(addprefix $(OBJPATH)/,$$(dep))))
230 -include $$(addsuffix .d,$$(basename $$(abs_deps)))
231 $(1): $$(abs_deps) $(if $(findstring $(OBJDIR),$(1)),| $(OBJPATH),)
232 $$(or $$(CMD_$(1)),$$(MAKECMD$$(suffix $$@)),$$(DEFAULT_MAKECMD))
233 endef
234
235 # subtree_tgts is now just a special case of a more general get_subtree
236 # macro since $(call get_subtree,TARGETS,dir) has the same effect but
237 # I'm keeping it for backward compatibility
238 define subtree_tgts
239 $(TARGETS_$(1)) $(foreach sd,$(SUBDIRS_$(1)),$(call subtree_tgts,$(sd)))
240 endef
241
242 define get_subtree
243 $($(1)_$(2)) $(foreach sd,$(SUBDIRS_$(2)),$(call get_subtree,$(1),$(sd)))
244 endef
245
246 # if we are using out of project build tree then there is no need to
247 # have dist_clean on per directory level and the one below is enough
248 ifneq ($(strip $(TOP_BUILD_DIR)),)
249 dist_clean :
250 rm -rf $(TOP_BUILD_DIR)
251 endif
252
253 # Suck in the default rules
254 include $(MK)/def_rules.mk