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