]> rtime.felk.cvut.cz Git - omk.git/blob - snippets/linux.omk
Add rules for .s assembler files
[omk.git] / snippets / linux.omk
1 #                   Version for Linux/RTLinux builds.
2 #
3 #
4 # input variables
5 # lib_LIBRARIES    .. list of the user-space libraries
6 # shared_LIBRARIES .. list of the user-space shared libraries
7 # include_HEADERS  .. list of the user-space public header files
8 # nobase_include_HEADERS .. public headers copied even with directory part
9 # renamed_include_HEADERS .. public headers copied to the different target name
10 # bin_PROGRAMS     .. list of the require binary programs
11 # utils_PROGRAMS   .. list of the development utility programs
12 # test_PROGRAMS    .. list of the testing programs
13 # bin_SCRIPTS      .. list of scripts to be copied to _compiled/bin
14 # xxx_SOURCES      .. list of specific target sources
15 # xxx_GEN_SOURCES  .. list of specific target sources that are generated in the build directory
16 # xxx_LIBS         .. list of specific target libraries (-l prefix is automatically added)
17 # xxx_LDFLAGS      .. list of specific target LDFLAGS
18 # lib_LOADLIBES    .. list of libraries linked to each executable
19 # INCLUDES         .. additional include directories and defines for user-space
20 #
21 # OMK_CFLAGS        .. C compiler flags
22 # OMK_CXXFLAGS      .. C++ compiler flags
23 # OMK_CPPFLAGS     .. C preprocessor flags
24 # LDFLAGS          .. linker flags for programs linking
25
26 BUILD_DIR_NAME = _build
27 COMPILED_DIR_NAME = _compiled
28 ifndef GROUP_DIR_NAME
29 GROUP_DIR_NAME = nogroup
30 endif
31
32 USER_INCLUDE_DIR := $(OUTPUT_DIR)/$(COMPILED_DIR_NAME)/include
33 USER_LIB_DIR     := $(OUTPUT_DIR)/$(COMPILED_DIR_NAME)/lib
34 USER_UTILS_DIR   := $(OUTPUT_DIR)/$(COMPILED_DIR_NAME)/bin-utils
35 USER_TESTS_DIR   := $(OUTPUT_DIR)/$(COMPILED_DIR_NAME)/bin-tests
36 USER_BIN_DIR     := $(OUTPUT_DIR)/$(COMPILED_DIR_NAME)/bin
37 USER_BUILD_DIR   := $(OUTPUT_DIR)/$(BUILD_DIR_NAME)/user
38
39 ifeq ($(BUILD_OS),)
40   # Check for target
41   ifeq ($(OS),Windows_NT)
42     BUILD_OS := win32
43   else
44     BUILD_OS := $(shell uname | tr '[A-Z]' '[a-z]' )
45     #$(warning BUILD_OS=$(BUILD_OS))
46   endif
47 endif
48
49 ifeq ($(TARGET_OS),)
50   TARGET_OS := $(BUILD_OS)
51 endif
52
53 export TARGET_OS
54 export BUILD_OS
55
56 LOCAL_BUILD_DIR  = $(USER_OBJS_DIR)
57
58 # Assign default values to CFLAGS variable. If the variable is defined
59 # earlier (i.g. in config.omk), it is not overriden here.
60 CFLAGS ?= -O2 -Wall
61 CXXFLAGS ?= -O2 -Wall
62
63
64 CPPFLAGS  += -I $(USER_INCLUDE_DIR)
65
66 LOADLIBES += -L$(USER_LIB_DIR) 
67
68 LOADLIBES += $(lib_LOADLIBES:%=-l%)
69
70 LIB_CPPFLAGS += $(CPPFLAGS)
71 LIB_CFLAGS   += $(CFLAGS)
72
73 ifeq ($(TARGET_OS),win32)
74   EXE_SUFFIX = .exe
75   SOLIB_EXT = dll
76 else
77   SOLIB_EXT = so
78   SOLIB_PICFLAGS += -fpic
79 endif
80
81 #vpath %.c $(SOURCES_DIR)
82 #vpath %.cc $(SOURCES_DIR)
83 #vpath %.cxx $(SOURCES_DIR)
84
85 USER_OBJS_DIR = $(USER_BUILD_DIR)/$(RELATIVE_DIR)
86 OMK_WORK_DIR  = $(USER_OBJS_DIR)
87
88 .PHONY: dep subdirs clean clean-custom cleandepend check-dir
89
90 # Some support to serialize some targets for parallel make
91 ifneq ($(OMK_SERIALIZE_INCLUDED),y)
92 include-pass: check-dir
93 library-pass: include-pass
94 link-pseudo-pass: library-pass
95 binary-pass: link-pseudo-pass
96
97 override OMK_SERIALIZE_INCLUDED = y
98 MAKEOVERRIDES := $(filter-out OMK_SERIALIZE_INCLUDED=n,$(MAKEOVERRIDES))
99 endif
100
101 #=====================================================================
102 # User-space rules and templates to compile programs, libraries etc.
103
104 ifdef USER_RULE_TEMPLATES
105
106 c_o_COMPILE = $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(OMK_CPPFLAGS) \
107         $(CPPFLAGS) $(OMK_CFLAGS) $(CFLAGS) -DOMK_FOR_USER
108
109 cc_o_COMPILE = $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(OMK_CPPFLAGS) \
110         $(CPPFLAGS) $(OMK_CFLAGS) $(CFLAGS) $(OMK_CXXFLAGS) $(CXXFLAGS) -DOMK_FOR_USER
111
112 S_o_COMPILE = $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(OMK_CPPFLAGS) \
113         $(CPPFLAGS) $(OMK_CFLAGS) $(CFLAGS) $(ASFLAGS) -DOMK_FOR_USER
114
115 idl_COMPILE = $(IDL_COMPILER)
116
117 # Check GCC version for user build
118 ifndef CC_MAJOR_VERSION
119 CC_MAJOR_VERSION := $(shell $(CC) -dumpversion | $(SED4OMK) -e 's/\([^.]\)\..*/\1/')
120 endif
121 # Prepare suitable define for dependency building
122 ifeq ($(CC_MAJOR_VERSION),2)
123 CC_DEPFLAGS = -Wp,-MD,"$@.d.tmp"
124 else
125 CC_DEPFLAGS = -MT $@ -MD -MP -MF "$@.d.tmp"
126 endif
127
128
129 # Syntax: $(call COMPILE_c_o_template,<source>,<target>,<additional c-flags>)
130 define COMPILE_c_o_template
131 ifeq ($$($(2)_C_TARGET),)
132 $(2)_C_TARGET=1
133 $(2): $(1) $$(GEN_HEADERS)
134         @$(QUIET_CMD_ECHO) "  CC      $$@"
135         $(Q) if $$(c_o_COMPILE) $$(CC_DEPFLAGS) $(3) -o $$@ -c $$< ; \
136         then mv -f "$$@.d.tmp" "$$@.d" ; \
137         else rm -f "$$@.d.tmp" ; exit 1; \
138         fi
139 endif
140 endef
141
142
143 # Syntax: $(call COMPILE_cc_o_template,<source>,<target>,<additional c-flags>)
144 define COMPILE_cc_o_template
145 ifeq ($$($(2)_CC_TARGET),)
146 $(2)_CC_TARGET=1
147 $(2): $(1) $$(GEN_HEADERS)
148         @$(QUIET_CMD_ECHO) "  CXX     $$@"
149         $(Q) if $$(cc_o_COMPILE) $$(CC_DEPFLAGS) $(3) -o $$@ -c $$< ; \
150         then mv -f "$$@.d.tmp" "$$@.d" ; \
151         else rm -f "$$@.d.tmp" ; exit 1; \
152         fi
153 endif
154 endef
155
156
157 # Syntax: $(call COMPILE_S_o_template,<source>,<target>,<additional c-flags>)
158 define COMPILE_S_o_template
159 ifeq ($$($(2)_S_TARGET),)
160 $(2)_S_TARGET=1
161 $(2): $(1) $$(GEN_HEADERS)
162         @$(QUIET_CMD_ECHO) "  AS      $$@"
163         $(Q) if $$(S_o_COMPILE) -D__ASSEMBLY__ $$(CC_DEPFLAGS) $(3) -o $$@ -c $$< ; \
164         then mv -f "$$@.d.tmp" "$$@.d" ; \
165         else rm -f "$$@.d.tmp" ; exit 1; \
166         fi
167 endif
168 endef
169
170 # Syntax: $(call COMPILE_s_o_template,<source>,<target>,<additional c-flags>)
171 define COMPILE_s_o_template
172 ifeq ($$($(2)_s_TARGET),)
173 $(2)_s_TARGET=1
174 $(2): $(1) $$(GEN_HEADERS)
175         @$(QUIET_CMD_ECHO) "  AS      $$@"
176         $(Q)$$(S_o_COMPILE) $(3) -o $$@ -c $$<
177 endif
178 endef
179
180 # Syntax: $(call COMPILE_idl_template,</path/to/src.idl>,<basename>)
181 define COMPILE_idl_template
182
183 ifeq ($$($(2)_IDL_TARGET),)
184 $(2)_IDL_TARGET=1
185 GEN_HEADERS+=$(filter %.h,$(notdir $(1:%.idl=%.h))) # Do we need this global variable?
186
187 $(2).c $(2)-stubs.c $(2)-skels.c $(2)-common.c $(2).h: $(1) $$(wildcard $$(firstword $$(idl_COMPILE)))
188         @$(QUIET_CMD_ECHO) "  IDL     $$@"
189         $(Q) $$(idl_COMPILE) $$($(2)_IDLFLAGS) $(1)
190 endif
191 endef
192
193 # Syntax: $(call COMPILE_templates,<sources_abs>,<suffix>,<obj-prefix>)
194 # Note: The newlines after $(call ) are IMPORTANT!!!
195 define COMPILE_templates
196 $(foreach src,$(filter %.c,$(1)),$(call COMPILE_c_o_template,$(src),$(3)$(notdir $(src:%.c=%$(2))),)
197 )
198 $(foreach src,$(filter %.cc,$(1)),$(call COMPILE_cc_o_template,$(src),$(3)$(notdir $(src:%.cc=%$(2))),)
199 )
200 $(foreach src,$(filter %.cxx,$(1)),$(call COMPILE_cc_o_template,$(src),$(3)$(notdir $(src:%.cxx=%$(2))),)
201 )
202 $(foreach src,$(filter %.cpp,$(1)),$(call COMPILE_cc_o_template,$(src),$(3)$(notdir $(src:%.cpp=%$(2))),)
203 )
204 $(foreach src,$(filter %.S,$(1)),$(call COMPILE_S_o_template,$(src),$(3)$(notdir $(basename $(src))$(2)),)
205 )
206 $(foreach src,$(filter %.s,$(1)),$(call COMPILE_s_o_template,$(src),$(3)$(notdir $(basename $(src))$(2)),)
207 )
208 endef
209
210 TARGET_IDL_SOURCES =  $(filter %.c,$($(1)_SERVER_IDL:%.idl=%-skels.c)) \
211                        $(filter %.c,$($(1)_SERVER_IDL:%.idl=%-common.c)) \
212                        $(filter %.c,$($(1)_CLIENT_IDL:%.idl=%-stubs.c)) \
213                        $(filter %.c,$($(1)_CLIENT_IDL:%.idl=%-common.c)) \
214                        $(filter %.c,$($(1)_IDL:%.idl=%.c))
215 TARGET_SOURCES = $($(1)_SOURCES) $($(1)_GEN_SOURCES) $(TARGET_IDL_SOURCES)
216 TARGET_SOURCES_ABS = $($(1)_SOURCES:%=$(SOURCES_DIR)/%) $($(1)_GEN_SOURCES) $(TARGET_IDL_SOURCES)
217 TARGET_OBJ_PREFIX = $(if $($(1)_CFLAGS)$($(1)_CXXFLAGS)$($(1)_CPPFLAGS),$(1)-,)
218 TARGET_OBJS  = $(sort $(addprefix $(TARGET_OBJ_PREFIX),$(addsuffix .o,$(basename $(notdir $(TARGET_SOURCES))))))
219 TARGET_LOBJS = $(sort $(addprefix $(TARGET_OBJ_PREFIX),$(addsuffix .lo,$(basename $(notdir $(TARGET_SOURCES))))))
220 TARGET_IDLS = $($(1)_SERVER_IDL) $($(1)_CLIENT_IDL) $($(1)_IDL)
221 TARGET_CFLAGS   = $(if $($(1)_CFLAGS),$($(1)_CFLAGS),$(OMK_CFLAGS))
222 TARGET_CXXFLAGS = $(if $($(1)_CXXFLAGS),$($(1)_CXXFLAGS),$(OMK_CXXFLAGS))
223 TARGET_CPPFLAGS = $(if $($(1)_CPPFLAGS),$($(1)_CPPFLAGS),$(OMK_CPPFLAGS))
224 LINK_WITH_CXX = $(filter %.cc,$(TARGET_SOURCES))$(filter %.cxx,$(TARGET_SOURCES))$(filter %.cpp,$(TARGET_SOURCES))
225
226 # Syntax: $(call PROGRAM_template,<executable-name>,<dir>,<executable-suffix>,<linker-sript>)
227 define PROGRAM_template
228 $(foreach idl,$(TARGET_IDLS),$(call COMPILE_idl_template,$(SOURCES_DIR)/$(idl),$(idl:%.idl=%)))
229
230 $(call COMPILE_templates,$(TARGET_SOURCES_ABS),.o,$(TARGET_OBJ_PREFIX))
231
232 $(2)/$(1)$(3): OMK_CFLAGS=$(TARGET_CFLAGS)
233 $(2)/$(1)$(3): OMK_CXXFLAGS=$(TARGET_CXXFLAGS)
234 $(2)/$(1)$(3): OMK_CPPFLAGS=$(TARGET_CPPFLAGS)
235 $(2)/$(1)$(3): $(TARGET_OBJS)
236         @$(QUIET_CMD_ECHO) "  LINK    $$@"
237         $(Q) $(if $(LINK_WITH_CXX),$$(CXX),$$(CC)) \
238           $(TARGET_OBJS) $$($(1)_LIBS:%=-l%) $$(LOADLIBES) $$(OMK_LDFLAGS) $$(LDFLAGS) $$($(1)_LDFLAGS) -Wl,-rpath-link,$(USER_LIB_DIR) -Wl,-Map,$(USER_OBJS_DIR)/$(1).exe.map -o $$@
239         @echo "$(2)/$(1)$(3): \\" >$(USER_OBJS_DIR)/$(1).exe.d
240         @$(SED4OMK) -n -e 's|^LOAD \(.*\)$$$$|  \1  \&|p' $(USER_OBJS_DIR)/$(1).exe.map|tr '&' '\134'  >>$(USER_OBJS_DIR)/$(1).exe.d
241         @echo >>$(USER_OBJS_DIR)/$(1).exe.d
242
243 binary-pass-local: $(2)/$(1)$(3)
244 endef
245
246 # Usage: $(call SCRIPT_template,<target-directory>,<script-name>)
247 define SCRIPT_template
248 $(2)/$(1): $$(SOURCES_DIR)/$(1)
249         @$(QUIET_CMD_ECHO) "  CP      $$@"
250         $(Q)cp $$^ $$@
251
252 binary-pass-local: $(2)/$(1)
253 endef
254
255
256 # Syntax: $(call LIBRARY_template,<library-name>)
257 define LIBRARY_template
258
259 $(foreach idl,$(TARGET_IDLS),$(call COMPILE_idl_template,$(SOURCES_DIR)/$(idl),$(idl:%.idl=%)))
260
261 $(call COMPILE_templates,$(TARGET_SOURCES_ABS),.o,$(TARGET_OBJ_PREFIX))
262
263 $(USER_LIB_DIR)/lib$(1).a: OMK_CFLAGS=$(TARGET_CFLAGS)
264 $(USER_LIB_DIR)/lib$(1).a: OMK_CXXFLAGS=$(TARGET_CXXFLAGS)
265 $(USER_LIB_DIR)/lib$(1).a: OMK_CPPFLAGS=$(TARGET_CPPFLAGS)
266 $(USER_LIB_DIR)/lib$(1).a: $(TARGET_OBJS)
267         @$(QUIET_CMD_ECHO) "  AR      $$@"
268         $(Q) $(AR) rcs $$@ $$^
269
270 library-pass-local: $(USER_LIB_DIR)/lib$(1).a
271 endef
272
273
274 # Syntax: $(call SOLIB_template,<library-name>)
275 define SOLIB_template
276
277 $(foreach idl,$(TARGET_IDLS),$(call COMPILE_idl_template,$(SOURCES_DIR)/$(idl),$(idl:%.idl=%)))
278
279 $(call COMPILE_templates,$(TARGET_SOURCES_ABS),.lo,$(TARGET_OBJ_PREFIX))
280
281 .PHONY: $(OMK_WORK_DIR)/lib$(1).$(SOLIB_EXT).omkvar
282 $(2)/$(1)$(3): OMK_CFLAGS=$(TARGET_CFLAGS)
283 $(2)/$(1)$(3): OMK_CXXFLAGS=$(TARGET_CXXFLAGS)
284 $(2)/$(1)$(3): OMK_CPPFLAGS=$(TARGET_CPPFLAGS)
285 $(OMK_WORK_DIR)/lib$(1).$(SOLIB_EXT).omkvar: OMK_CFLAGS = $(TARGET_CFLAGS) $(SOLIB_PICFLAGS)
286 $(OMK_WORK_DIR)/lib$(1).$(SOLIB_EXT).omkvar: OMK_CXXFLAGS = $(TARGET_CXXFLAGS) $(SOLIB_PICFLAGS)
287 $(OMK_WORK_DIR)/lib$(1).$(SOLIB_EXT).omkvar: OMK_CPPFLAGS = $(TARGET_CPPFLAGS)
288 $(OMK_WORK_DIR)/lib$(1).$(SOLIB_EXT).omkvar: $(TARGET_LOBJS)
289         $(Q)echo '$(1)_objslo += $$$$(addprefix $(USER_OBJS_DIR)/,$$^)' > $$@.tmp; \
290             echo '$(1)_libs += $$($(1)_LIBS) $$(lib_LOADLIBES)' >> $$@.tmp; \
291             echo '$(1)_ldflags += $$($(1)_LDFLAGS) $$(lib_LDFLAGS)' >> $$@.tmp; \
292             echo 'shared_libs := $$$$(sort $(1) $$$$(shared_libs))' >> $$@.tmp; \
293             if cmp -s $$@.tmp $$@; then rm $$@.tmp; else mv $$@.tmp $$@; fi
294
295 library-pass-local: $(OMK_WORK_DIR)/lib$(1).$(SOLIB_EXT).omkvar
296 endef
297
298 # Generate rules for compilation of programs and libraries
299
300 $(foreach prog,$(utils_PROGRAMS),$(eval $(call PROGRAM_template,$(prog),$(USER_UTILS_DIR),$(EXE_SUFFIX))))
301
302 $(foreach prog,$(test_PROGRAMS),$(eval $(call PROGRAM_template,$(prog),$(USER_TESTS_DIR),$(EXE_SUFFIX))))
303
304 $(foreach prog,$(bin_PROGRAMS),$(eval $(call PROGRAM_template,$(prog),$(USER_BIN_DIR),$(EXE_SUFFIX))))
305
306 $(foreach script,$(bin_SCRIPTS),$(eval $(call SCRIPT_template,$(script),$(USER_BIN_DIR))))
307
308
309 # $(foreach lib,$(lib_LIBRARIES),$(info $(call LIBRARY_template,$(lib))))
310 $(foreach lib,$(lib_LIBRARIES),$(eval $(call LIBRARY_template,$(lib))))
311
312 $(foreach lib,$(shared_LIBRARIES),$(eval $(call SOLIB_template,$(lib))))
313
314 -include $(USER_OBJS_DIR)/*.d
315 #FIXME: This doesn't include dependencies of source files from
316 #subdirectories (i.s. *_SOURCES=dir/file.c. I'm currently not sure,
317 #how to handle this.
318
319 endif # USER_RULE_TEMPLATES
320
321 .PHONY: link-pseudo-pass
322 link-pseudo-pass:
323         $(Q)$(MAKE) $(NO_PRINT_DIRECTORY) -C $(USER_BUILD_DIR) -f $(SOURCESDIR_MAKEFILE) link-shared-libs
324
325 ifeq ($(MAKECMDGOALS),link-shared-libs)
326
327
328
329 # Syntax: $(call solib_link_template,<library-name>)
330 define solib_link_template
331 $(1)_shared_libs = $$(patsubst %,$(USER_LIB_DIR)/lib%.$(SOLIB_EXT),$$(filter $$(shared_libs),$$($(1)_libs)))
332 #$$(warning $(1)_shared_libs = $$($(1)_shared_libs))
333 $(USER_LIB_DIR)/lib$(1).$(SOLIB_EXT): $$($(1)_shared_libs) $$($(1)_objslo)
334         @$(QUIET_CMD_ECHO) "  LINK    $$@"
335         $(Q)$(CC) --shared -Xlinker -soname=lib$(1).$(SOLIB_EXT) -Wl,-Map,$(USER_OBJS_DIR)/lib$(1).$(SOLIB_EXT).map -o $$@ $$($(1)_objslo) $$(LOADLIBES) $$($(1)_libs:%=-l%) $$(lib_ldflags) $$($(1)_ldflags)
336 endef
337
338 -include $(shell true; find $(USER_BUILD_DIR) -name 'lib*.omkvar') # `true' is a hack for MinGW
339 #$(warning $(shared_libs))
340 $(foreach lib,$(shared_libs),$(eval $(call solib_link_template,$(lib))))
341
342 .PHONY: link-shared-libs
343 link-shared-libs: $(shared_libs:%=$(USER_LIB_DIR)/lib%.$(SOLIB_EXT))
344 endif # link-shared-libs
345
346 $(eval $(call omk_pass_template, library-pass,$(USER_OBJS_DIR),USER_RULE_TEMPLATES=y,$(lib_LIBRARIES)$(shared_LIBRARIES)$(cmetric_include_HEADERS)))
347 $(eval $(call omk_pass_template, binary-pass, $(USER_OBJS_DIR),USER_RULE_TEMPLATES=y,$(bin_PROGRAMS)$(utils_PROGRAMS)$(test_PROGRAMS)$(bin_SCRIPTS)))
348
349 $(eval $(call omk_pass_template,clean,$(USER_OBJS_DIR),,always))
350 $(eval $(call omk_pass_template,install,$(USER_OBJS_DIR),,always))
351 $(eval $(call omk_pass_template,include-pass,$(USER_OBJS_DIR),USER_RULE_TEMPLATES=y,always))
352
353 check-dir::
354         @$(call mkdir_def,$(USER_BUILD_DIR))
355         @$(call mkdir_def,$(USER_INCLUDE_DIR))
356         @$(call mkdir_def,$(USER_LIB_DIR))
357         @$(call mkdir_def,$(USER_BIN_DIR))
358         @$(call mkdir_def,$(USER_UTILS_DIR))
359         @$(call mkdir_def,$(USER_TESTS_DIR))
360
361 install-local:                  # TODO
362
363 $(eval $(call include-pass-template,$(USER_INCLUDE_DIR),include))
364
365 clean-local::
366         @echo Cleaning in $(USER_OBJS_DIR)
367         @rm -f $(USER_OBJS_DIR)/*.[och] \
368                $(USER_OBJS_DIR)/*.lo \
369                $(USER_OBJS_DIR)/*.d \
370                $(USER_OBJS_DIR)/*.map \
371                $(LOCAL_CONFIG_H:%=$(USER_OBJS_DIR)/%)
372
373 include-pass-submakes: extra-rules-subdirs
374
375 # We must go to EXTRA_RULES_SUBDIRS before going to any other
376 # directory, since the executables compiled in EXTRA_RULES_SUBDIRS
377 # might be needed there.
378 include-pass-this-dir $(foreach subdir,$(SUBDIRS),include-pass-$(subdir)-subdir): extra-rules-subdirs
379
380 default: include-pass library-pass binary-pass