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