1 # Version for Linux/RTLinux builds.
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
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
26 BUILD_DIR_NAME = _build
27 COMPILED_DIR_NAME = _compiled
29 GROUP_DIR_NAME = nogroup
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
41 ifeq ($(OS),Windows_NT)
44 BUILD_OS := $(shell uname | tr '[A-Z]' '[a-z]' )
45 #$(warning BUILD_OS=$(BUILD_OS))
50 TARGET_OS := $(BUILD_OS)
56 LOCAL_BUILD_DIR = $(USER_OBJS_DIR)
58 # Assign default values to CFLAGS variable. If the variable is defined
59 # earlier (i.g. in config.omk), it is not overriden here.
64 CPPFLAGS += -I $(USER_INCLUDE_DIR)
66 LOADLIBES += -L$(USER_LIB_DIR)
68 LOADLIBES += $(lib_LOADLIBES:%=-l%)
70 LIB_CPPFLAGS += $(CPPFLAGS)
71 LIB_CFLAGS += $(CFLAGS)
73 ifeq ($(TARGET_OS),win32)
78 SOLIB_PICFLAGS += -fpic
81 #vpath %.c $(SOURCES_DIR)
82 #vpath %.cc $(SOURCES_DIR)
83 #vpath %.cxx $(SOURCES_DIR)
85 USER_OBJS_DIR = $(USER_BUILD_DIR)/$(RELATIVE_DIR)
86 OMK_WORK_DIR = $(USER_OBJS_DIR)
88 .PHONY: dep subdirs clean clean-custom cleandepend check-dir
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
97 override OMK_SERIALIZE_INCLUDED = y
98 MAKEOVERRIDES := $(filter-out OMK_SERIALIZE_INCLUDED=n,$(MAKEOVERRIDES))
101 #=====================================================================
102 # User-space rules and templates to compile programs, libraries etc.
104 ifdef USER_RULE_TEMPLATES
106 c_o_COMPILE = $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(OMK_CPPFLAGS) \
107 $(CPPFLAGS) $(OMK_CFLAGS) $(CFLAGS) -DOMK_FOR_USER
109 cc_o_COMPILE = $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(OMK_CPPFLAGS) \
110 $(CPPFLAGS) $(OMK_CFLAGS) $(CFLAGS) $(OMK_CXXFLAGS) $(CXXFLAGS) -DOMK_FOR_USER
112 S_o_COMPILE = $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(OMK_CPPFLAGS) \
113 $(CPPFLAGS) $(OMK_CFLAGS) $(CFLAGS) $(ASFLAGS) -DOMK_FOR_USER
115 idl_COMPILE = $(IDL_COMPILER)
117 # Check GCC version for user build
118 ifndef CC_MAJOR_VERSION
119 CC_MAJOR_VERSION := $(shell $(CC) -dumpversion | $(SED4OMK) -e 's/\([^.]\)\..*/\1/')
121 # Prepare suitable define for dependency building
122 ifeq ($(CC_MAJOR_VERSION),2)
123 CC_DEPFLAGS = -Wp,-MD,"$@.d.tmp"
125 CC_DEPFLAGS = -MT $@ -MD -MP -MF "$@.d.tmp"
129 # Syntax: $(call COMPILE_c_o_template,<source>,<target>,<additional c-flags>)
130 define COMPILE_c_o_template
131 ifeq ($$($(2)_C_TARGET),)
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; \
143 # Syntax: $(call COMPILE_cc_o_template,<source>,<target>,<additional c-flags>)
144 define COMPILE_cc_o_template
145 ifeq ($$($(2)_CC_TARGET),)
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; \
157 # Syntax: $(call COMPILE_S_o_template,<source>,<target>,<additional c-flags>)
158 define COMPILE_S_o_template
159 ifeq ($$($(2)_S_TARGET),)
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; \
170 # Syntax: $(call COMPILE_s_o_template,<source>,<target>,<additional c-flags>)
171 define COMPILE_s_o_template
172 ifeq ($$($(2)_s_TARGET),)
174 $(2): $(1) $$(GEN_HEADERS)
175 @$(QUIET_CMD_ECHO) " AS $$@"
176 $(Q)$$(S_o_COMPILE) $(3) -o $$@ -c $$<
180 # Syntax: $(call COMPILE_idl_template,</path/to/src.idl>,<basename>)
181 define COMPILE_idl_template
183 ifeq ($$($(2)_IDL_TARGET),)
185 GEN_HEADERS+=$(filter %.h,$(notdir $(1:%.idl=%.h))) # Do we need this global variable?
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)
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))),)
198 $(foreach src,$(filter %.cc,$(1)),$(call COMPILE_cc_o_template,$(src),$(3)$(notdir $(src:%.cc=%$(2))),)
200 $(foreach src,$(filter %.cxx,$(1)),$(call COMPILE_cc_o_template,$(src),$(3)$(notdir $(src:%.cxx=%$(2))),)
202 $(foreach src,$(filter %.cpp,$(1)),$(call COMPILE_cc_o_template,$(src),$(3)$(notdir $(src:%.cpp=%$(2))),)
204 $(foreach src,$(filter %.S,$(1)),$(call COMPILE_S_o_template,$(src),$(3)$(notdir $(basename $(src))$(2)),)
206 $(foreach src,$(filter %.s,$(1)),$(call COMPILE_s_o_template,$(src),$(3)$(notdir $(basename $(src))$(2)),)
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))
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=%)))
230 $(call COMPILE_templates,$(TARGET_SOURCES_ABS),.o,$(TARGET_OBJ_PREFIX))
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
243 binary-pass-local: $(2)/$(1)$(3)
246 # Usage: $(call SCRIPT_template,<target-directory>,<script-name>)
247 define SCRIPT_template
248 $(2)/$(1): $$(SOURCES_DIR)/$(1)
249 @$(QUIET_CMD_ECHO) " CP $$@"
252 binary-pass-local: $(2)/$(1)
256 # Syntax: $(call LIBRARY_template,<library-name>)
257 define LIBRARY_template
259 $(foreach idl,$(TARGET_IDLS),$(call COMPILE_idl_template,$(SOURCES_DIR)/$(idl),$(idl:%.idl=%)))
261 $(call COMPILE_templates,$(TARGET_SOURCES_ABS),.o,$(TARGET_OBJ_PREFIX))
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 $$@ $$^
270 library-pass-local: $(USER_LIB_DIR)/lib$(1).a
274 # Syntax: $(call SOLIB_template,<library-name>)
275 define SOLIB_template
277 $(foreach idl,$(TARGET_IDLS),$(call COMPILE_idl_template,$(SOURCES_DIR)/$(idl),$(idl:%.idl=%)))
279 $(call COMPILE_templates,$(TARGET_SOURCES_ABS),.lo,$(TARGET_OBJ_PREFIX))
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
295 library-pass-local: $(OMK_WORK_DIR)/lib$(1).$(SOLIB_EXT).omkvar
298 # Generate rules for compilation of programs and libraries
300 $(foreach prog,$(utils_PROGRAMS),$(eval $(call PROGRAM_template,$(prog),$(USER_UTILS_DIR),$(EXE_SUFFIX))))
302 $(foreach prog,$(test_PROGRAMS),$(eval $(call PROGRAM_template,$(prog),$(USER_TESTS_DIR),$(EXE_SUFFIX))))
304 $(foreach prog,$(bin_PROGRAMS),$(eval $(call PROGRAM_template,$(prog),$(USER_BIN_DIR),$(EXE_SUFFIX))))
306 $(foreach script,$(bin_SCRIPTS),$(eval $(call SCRIPT_template,$(script),$(USER_BIN_DIR))))
309 # $(foreach lib,$(lib_LIBRARIES),$(info $(call LIBRARY_template,$(lib))))
310 $(foreach lib,$(lib_LIBRARIES),$(eval $(call LIBRARY_template,$(lib))))
312 $(foreach lib,$(shared_LIBRARIES),$(eval $(call SOLIB_template,$(lib))))
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,
319 endif # USER_RULE_TEMPLATES
321 .PHONY: link-pseudo-pass
323 $(Q)$(MAKE) $(NO_PRINT_DIRECTORY) -C $(USER_BUILD_DIR) -f $(SOURCESDIR_MAKEFILE) link-shared-libs
325 ifeq ($(MAKECMDGOALS),link-shared-libs)
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)
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))))
342 .PHONY: link-shared-libs
343 link-shared-libs: $(shared_libs:%=$(USER_LIB_DIR)/lib%.$(SOLIB_EXT))
344 endif # link-shared-libs
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)))
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))
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))
361 install-local: # TODO
363 $(eval $(call include-pass-template,$(USER_INCLUDE_DIR),include))
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)/%)
373 include-pass-submakes: extra-rules-subdirs
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
380 default: include-pass library-pass binary-pass