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