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 OMK_CFLAGS variable. If the variable is defined
59 # earlier (i.g. in config.omk), it is not overriden here.
60 OMK_CFLAGS ?= -O2 -Wall
61 OMK_CXXFLAGS ?= -O2 -Wall
64 INCLUDES += -I $(USER_INCLUDE_DIR)
66 LOADLIBES += -L$(USER_LIB_DIR)
68 LOADLIBES += $(lib_LOADLIBES:%=-l%)
70 ifeq ($(TARGET_OS),win32)
75 SOLIB_PICFLAGS += -fpic
78 #vpath %.c $(SOURCES_DIR)
79 #vpath %.cc $(SOURCES_DIR)
80 #vpath %.cxx $(SOURCES_DIR)
82 USER_OBJS_DIR = $(USER_BUILD_DIR)/$(RELATIVE_DIR)
83 OMK_WORK_DIR = $(USER_OBJS_DIR)
85 .PHONY: dep subdirs clean clean-custom cleandepend check-dir
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
94 override OMK_SERIALIZE_INCLUDED = y
95 MAKEOVERRIDES := $(filter-out OMK_SERIALIZE_INCLUDED=n,$(MAKEOVERRIDES))
98 #=====================================================================
99 # User-space rules and templates to compile programs, libraries etc.
101 ifdef USER_RULE_TEMPLATES
103 c_o_COMPILE = $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(OMK_CPPFLAGS) \
104 $(CPPFLAGS) $(OMK_CFLAGS) $(CFLAGS) -DOMK_FOR_USER
106 cc_o_COMPILE = $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(OMK_CPPFLAGS) \
107 $(CPPFLAGS) $(OMK_CXXFLAGS) $(CXXFLAGS) -DOMK_FOR_USER
109 S_o_COMPILE = $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(OMK_CPPFLAGS) \
110 $(CPPFLAGS) $(OMK_CFLAGS) $(CFLAGS) $(ASFLAGS) -DOMK_FOR_USER
112 idl_COMPILE = $(IDL_COMPILER)
114 # Check GCC version for user build
115 ifndef CC_MAJOR_VERSION
116 CC_MAJOR_VERSION := $(shell $(CC) -dumpversion | $(SED4OMK) -e 's/\([^.]\)\..*/\1/')
118 # Prepare suitable define for dependency building
119 ifeq ($(CC_MAJOR_VERSION),2)
120 CC_DEPFLAGS = -Wp,-MD,"$@.d.tmp"
122 CC_DEPFLAGS = -MT $@ -MD -MP -MF "$@.d.tmp"
126 # Syntax: $(call COMPILE_c_o_template,<source>,<target>,<additional c-flags>)
127 define COMPILE_c_o_template
128 ifeq ($$($(2)_C_TARGET),)
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; \
140 # Syntax: $(call COMPILE_cc_o_template,<source>,<target>,<additional c-flags>)
141 define COMPILE_cc_o_template
142 ifeq ($$($(2)_CC_TARGET),)
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; \
154 # Syntax: $(call COMPILE_S_o_template,<source>,<target>,<additional c-flags>)
155 define COMPILE_S_o_template
156 ifeq ($$($(2)_S_TARGET),)
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; \
167 # Syntax: $(call COMPILE_s_o_template,<source>,<target>,<additional c-flags>)
168 define COMPILE_s_o_template
169 ifeq ($$($(2)_s_TARGET),)
171 $(2): $(1) $$(GEN_HEADERS)
172 @$(QUIET_CMD_ECHO) " AS $$@"
173 $(Q)$$(S_o_COMPILE) $(3) -o $$@ -c $$<
177 # Syntax: $(call COMPILE_idl_template,</path/to/src.idl>,<basename>)
178 define COMPILE_idl_template
180 ifeq ($$($(2)_IDL_TARGET),)
182 GEN_HEADERS+=$(filter %.h,$(notdir $(1:%.idl=%.h))) # Do we need this global variable?
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)
190 # GCC recognizes files matching this pattern as C++
191 CXX_PATTERN = %.cc %.cp %.cxx %.cpp %.CPP %.c++ %.C
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 $(CXX_PATTERN),$(1)),$(call COMPILE_cc_o_template,$(src),$(3)$(notdir $(basename $(src))$(2)),)
200 $(foreach src,$(filter %.S,$(1)),$(call COMPILE_S_o_template,$(src),$(3)$(notdir $(basename $(src))$(2)),)
202 $(foreach src,$(filter %.s,$(1)),$(call COMPILE_s_o_template,$(src),$(3)$(notdir $(basename $(src))$(2)),)
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))
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=%)))
225 $(call COMPILE_templates,$(TARGET_SOURCES_ABS),.o,$(TARGET_OBJ_PREFIX))
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
238 binary-pass-local: $(2)/$(1)$(3)
241 # Usage: $(call SCRIPT_template,<target-directory>,<script-name>)
242 define SCRIPT_template
243 $(2)/$(1): $$(SOURCES_DIR)/$(1)
244 @$(QUIET_CMD_ECHO) " CP $$@"
247 binary-pass-local: $(2)/$(1)
251 # Syntax: $(call LIBRARY_template,<library-name>)
252 define LIBRARY_template
254 $(foreach idl,$(TARGET_IDLS),$(call COMPILE_idl_template,$(SOURCES_DIR)/$(idl),$(idl:%.idl=%)))
256 $(call COMPILE_templates,$(TARGET_SOURCES_ABS),.o,$(TARGET_OBJ_PREFIX))
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 $$@ $$^
265 library-pass-local: $(USER_LIB_DIR)/lib$(1).a
269 # Syntax: $(call SOLIB_template,<library-name>)
270 define SOLIB_template
272 $(foreach idl,$(TARGET_IDLS),$(call COMPILE_idl_template,$(SOURCES_DIR)/$(idl),$(idl:%.idl=%)))
274 $(call COMPILE_templates,$(TARGET_SOURCES_ABS),.lo,$(TARGET_OBJ_PREFIX))
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
290 library-pass-local: $(OMK_WORK_DIR)/lib$(1).$(SOLIB_EXT).omkvar
293 # Generate rules for compilation of programs and libraries
295 $(foreach prog,$(utils_PROGRAMS),$(eval $(call PROGRAM_template,$(prog),$(USER_UTILS_DIR),$(EXE_SUFFIX))))
297 $(foreach prog,$(test_PROGRAMS),$(eval $(call PROGRAM_template,$(prog),$(USER_TESTS_DIR),$(EXE_SUFFIX))))
299 $(foreach prog,$(bin_PROGRAMS),$(eval $(call PROGRAM_template,$(prog),$(USER_BIN_DIR),$(EXE_SUFFIX))))
301 $(foreach script,$(bin_SCRIPTS),$(eval $(call SCRIPT_template,$(script),$(USER_BIN_DIR))))
304 # $(foreach lib,$(lib_LIBRARIES),$(info $(call LIBRARY_template,$(lib))))
305 $(foreach lib,$(lib_LIBRARIES),$(eval $(call LIBRARY_template,$(lib))))
307 $(foreach lib,$(shared_LIBRARIES),$(eval $(call SOLIB_template,$(lib))))
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,
314 endif # USER_RULE_TEMPLATES
316 .PHONY: link-pseudo-pass
318 $(Q)$(MAKE) $(NO_PRINT_DIRECTORY) -C $(USER_BUILD_DIR) -f $(SOURCESDIR_MAKEFILE) link-shared-libs
320 ifeq ($(MAKECMDGOALS),link-shared-libs)
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)
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))))
337 .PHONY: link-shared-libs
338 link-shared-libs: $(shared_libs:%=$(USER_LIB_DIR)/lib%.$(SOLIB_EXT))
339 endif # link-shared-libs
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)))
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))
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))
356 install-local: # TODO
358 $(eval $(call include-pass-template,$(USER_INCLUDE_DIR),include))
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)/%)
368 include-pass-submakes: extra-rules-subdirs
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
375 default: include-pass library-pass binary-pass