2 # Rules for compilation of C, C++ and assembler sources using GNU
5 # Interface to other rules:
8 # LIB_DIR - directory where compiled libraries are stored
9 # OBJS_DIR - directory where intermediate files (.o, .map, ...) are stored
10 # INCLUDE_DIR - where includes can be found
11 # from config.omk or Makefile.omk
13 # TARGET_ARCH, DEBUG, OPTIMIZE, DEFS - forms CFLAGS
14 # from base: SOURCES_DIR
15 # from Makefile.omk: lib_LOADLIBES
18 # SOURCES - all the source files that needs to be compiled (except for shared library sources)
19 # SOLIB_SOURCES - all the source files that needs to be compiled for a shared library
20 # OBJ_EXT - extension of object files
21 # LIB_EXT - extension of library files
22 # LIB_PREF - prefix for library files
23 # ASM_EXT - extension of assembler sources
26 # COMPILER_DEFS_template - definitions that should be defined before
27 # the following templates can be used. The input variables needs to be
28 # defined before evaluating this template
30 # COMPILE_c_o_template, COMPILE_cc_o_template, COMPILE_S_o_template -
31 # templates that create rules for compilation of sources
33 # CMETRIC_o_h_template - FIXME
35 # PROGRAM_template, LIBRARY_template, SOLIB_template - templates that
36 # create rules for compilation of a program, library and shared
37 # library. The rules can use rules produced by COMPILE_xxx_template.
39 define COMPILER_DEFS_template
45 CC = $(CROSS_COMPILE)gcc
46 LINK = $(CROSS_COMPILE)ld
47 AR = $(CROSS_COMPILE)ar
48 OBJCOPY = $(CROSS_COMPILE)objcopy
49 NM = $(CROSS_COMPILE)nm
51 CFLAGS += $(TARGET_ARCH) $(DEBUG) $(OPTIMIZE)
53 CFLAGS += -I$(SOURCES_DIR)
54 CFLAGS += -I$(INCLUDE_DIR)
56 LOADLIBES += -L$(LIB_DIR)
57 LOADLIBES += $(lib_LOADLIBES:%=-l%)
60 -include $(OBJS_DIR)/*.d
62 SOURCES2OBJS = .o/.c .o/.cc .o/.cxx .o/.S .o/.o
64 SOURCES2OBJSLO = .lo/.c .lo/.cc .lo/.cxx .lo/.S .lo/.lo
67 # $(CC) -o $@ $(LCFLAGS) -c $<
69 c_o_COMPILE = $$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) \
70 $(CPPFLAGS) $(AM_CFLAGS) $$(CFLAGS)
72 cc_o_COMPILE = $$(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) \
73 $(CPPFLAGS) $(CXXFLAGS) $(AM_CFLAGS) $$(CFLAGS)
75 S_o_COMPILE = $$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) \
76 $(CPPFLAGS) $(AM_CFLAGS) $$(CFLAGS) $(ASFLAGS)
79 # Check GCC version for user build
80 ifndef CC_MAJOR_VERSION
81 CC_MAJOR_VERSION = $$(shell $$(CC) -dumpversion | $(SED4OMK) -e 's/\([^.]\)\..*/\1/')
83 # Prepare suitable define for dependency building
84 ifeq ($$(CC_MAJOR_VERSION),2)
85 CC_DEPFLAGS = -Wp,-MD,"$$@.d.tmp"
87 CC_DEPFLAGS = -MT $$@ -MD -MP -MF "$$@.d.tmp"
90 endef # COMPILER_DEFS_template
93 # Syntax: $(call COMPILE_c_o_template,<source>,<target>,<additional c-flags>)
94 define COMPILE_c_o_template
95 $(2): $(1) $$(GEN_HEADERS)
96 @$(QUIET_CMD_ECHO) " CC $$@"
97 $(Q) if $$(c_o_COMPILE) $$(CC_DEPFLAGS) $(3) -o $$@ -c $$< ; \
98 then mv -f "$$@.d.tmp" "$$@.d" ; \
99 else rm -f "$$@.d.tmp" ; exit 1; \
105 # Syntax: $(call COMPILE_cc_o_template,<source>,<target>,<additional c-flags>)
106 define COMPILE_cc_o_template
107 $(2): $(1) $$(GEN_HEADERS)
108 @$(QUIET_CMD_ECHO) " CXX $$@"
109 $(Q) if $$(cc_o_COMPILE) $$(CC_DEPFLAGS) $(3) -o $$@ -c $$< ; \
110 then mv -f "$$@.d.tmp" "$$@.d" ; \
111 else rm -f "$$@.d.tmp" ; exit 1; \
115 # Syntax: $(call COMPILE_S_o_template,<source>,<target>,<additional c-flags>)
116 define COMPILE_S_o_template
117 $(2): $(1) $$(GEN_HEADERS)
118 @$(QUIET_CMD_ECHO) " AS $$@"
119 $(Q) if $$(S_o_COMPILE) -D__ASSEMBLY__ $$(CC_DEPFLAGS) $(3) -o $$@ -c $$< ; \
120 then if [ -e "$$@.d.tmp" ] ; then mv -f "$$@.d.tmp" "$$@.d" ; fi ; \
121 else rm -f "$$@.d.tmp" ; exit 1; \
125 # Syntax: $(call CMETRIC_o_h_template,<object_file>,<target_header>)
126 define CMETRIC_o_h_template
128 @$(QUIET_CMD_ECHO) " CMETRIC $$@"
129 $(Q)if [ -n `dirname $$@` ] ; then \
130 if [ ! -e `dirname $$@` ] ; then \
131 mkdir -p `dirname $$@` ; fi ; fi
132 $(Q)echo >$$@.tmp '/* Automatically generated from $$< */'
133 $(Q)echo >>$$@.tmp '/* Conditionals to control compilation */'
134 # Bellow, the tricks with redirection are for shells without set -o pipefail
135 # (see http://www.mail-archive.com/dash@vger.kernel.org/msg00149.html)
136 $(Q)exec 3>&1; status=`exec 4>&1 >&3; { $(NM) $$<; echo $$$$? >&4; }\
137 | $(SED4OMK) -n 's/^ *0*\(0[0-9A-Fa-f]*\) *A *_cmetric2cond_\([A-Za-z_0-9]*\) */#define \2 0x\1/p' \
138 | sort >>$$@.tmp` && exit $$$$status
139 $(Q)echo >>$$@.tmp '/* Defines from the values defined to symbols in hexadecimal format */'
140 $(Q)exec 3>&1; status=`exec 4>&1 >&3; { $(NM) $$<; echo $$$$? >&4; }\
141 | $(SED4OMK) -n 's/^ *0*\(0[0-9A-Fa-f]*\) *A *_cmetric2def_\([A-Za-z_0-9]*\) */#define \2 0x\1/p' \
142 | sort >>$$@.tmp` && exit $$$$status
143 $(Q)echo >>$$@.tmp '/* Defines from the values defined to symbols in decimal format */'
144 $(Q)exec 3>&1; status=`exec 4>&1 >&3; { $(NM) -td $$<; echo $$$$? >&4; }\
145 | $(SED4OMK) -n 's/^ *0*\(0\|[1-9][0-9]*\) *A *_cmetric2defdec_\([A-Za-z_0-9]*\) */#define \2 \1/p' \
146 | sort >>$$@.tmp` && exit $$$$status
150 # Syntax: $(call PROGRAM_template,<executable-name>,<dir>,<link-variant>)
151 define PROGRAM_template
153 GEN_SOURCES += $$($(1)_GEN_SOURCES)
155 $(foreach x, $(SOURCES2OBJS),
156 $(1)_OBJS += $$(patsubst %$(notdir $(x)),%$(dir $(x)),$$(filter %$(notdir $(x)),\
157 $$($(1)_SOURCES) $$($(1)_GEN_SOURCES)))
159 $(1)_OBJS := $$(sort $$($(1)_OBJS:%/=%))
161 SOURCES += $$($(1)_SOURCES)
163 ifneq ($(LD_SCRIPT),)
164 $(1)$(3:%=-%)_LDFLAGS = -Wl,-T,$(LD_SCRIPT).ld$(3:%=-%)
167 $(2)/$(1)$(3:%=-%): $$($(1)_OBJS)
168 @$(QUIET_CMD_ECHO) " LINK $$@"
169 $(Q) $$(shell if [ -z "$$(filter %.cc,$$($(1)_SOURCES))" ] ; \
170 then echo $$(CC) $$(CPPFLAGS) $$(AM_CPPFLAGS) $$(AM_CFLAGS) $$(CFLAGS) ; \
171 else echo $$(CXX) $$(CPPFLAGS) $$(AM_CPPFLAGS) $$(AM_CXXFLAGS) $$(CXXFLAGS) ; fi) \
172 $$(AM_LDFLAGS) $$(LDFLAGS) $$($(1)$(3:%=-%)_LDFLAGS) -Wl,-Map,$(1)$(3:%=-%).map \
173 $$($(1)_OBJS) $$($(1)_MOREOBJS) $$(LOADLIBES) $$($(1)_LIBS:%=-l%) \
175 @echo "$(2)/$(1)$(3:%=-%): \\" >$(OBJS_DIR)/$(1)$(3:%=-%).exe.d
176 @if [ -n "$(LD_SCRIPT)" ]; then \
177 echo " $(LIB_DIR)/$(LD_SCRIPT).ld$(3:%=-%) \\" >>$(OBJS_DIR)/$(1)$(3:%=-%).exe.d; fi
178 @$(SED4OMK) -n -e 's|^LOAD \(.*\)$$$$| \1 \&|p' $(OBJS_DIR)/$(1)$(3:%=-%).map|tr '&' '\134' >>$(OBJS_DIR)/$(1)$(3:%=-%).exe.d
179 @echo >>$(OBJS_DIR)/$(1).exe.d
182 # Rules for other output formats (can be specified by OUTPUT_FORMATS)
184 @$(QUIET_CMD_ECHO) " OBJCOPY $@"
185 $(Q) $(OBJCOPY) --output-target=binary -S $< $@
188 @$(QUIET_CMD_ECHO) " OBJCOPY $@"
189 $(Q) $(OBJCOPY) --output-target=ihex -S $< $@
192 @$(QUIET_CMD_ECHO) " OBJCOPY $@"
193 $(Q) $(OBJCOPY) --output-target=srec -S $< $@
195 # Syntax: $(call LIBRARY_template,<library-name>)
196 define LIBRARY_template
198 GEN_SOURCES += $$($(1)_GEN_SOURCES)
200 $(foreach x, $(SOURCES2OBJS),
201 $(1)_OBJS += $$(patsubst %$(notdir $(x)),%$(dir $(x)),$$(filter %$(notdir $(x)),\
202 $$($(1)_SOURCES) $$($(1)_GEN_SOURCES)))
204 $(1)_OBJS := $$(sort $$($(1)_OBJS:%/=%))
206 SOURCES += $$($(1)_SOURCES)
208 $(LIB_DIR)/lib$(1).a: $$($(1)_OBJS)
209 @$(QUIET_CMD_ECHO) " AR $$@"
210 $(Q) $(AR) rcs $$@ $$^
214 # Syntax: $(call SOLIB_template,<library-name>)
215 define SOLIB_template
217 SOLIB_GEN_SOURCES += $$($(1)_GEN_SOURCES)
219 $(foreach x, $(SOURCES2OBJSLO),
220 $(1)_OBJSLO += $$(patsubst %$(notdir $(x)),%$(dir $(x)),$$(filter %$(notdir $(x)),\
221 $$($(1)_SOURCES) $$($(1)_GEN_SOURCES)))
223 $(1)_OBJSLO := $$(sort $$($(1)_OBJSLO:%/=%))
225 SOLIB_OBJS += $$($(1)_OBJSLO)
226 SOLIB_SOURCES += $$($(1)_SOURCES)
228 $(LIB_DIR)/lib$(1).so: $$($(1)_OBJSLO)
229 @$(QUIET_CMD_ECHO) " LINK $$@"
230 $(Q) $(LD) --shared --soname=lib$(1).so -o $$@ $$^