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
63 # $(CC) -o $@ $(LCFLAGS) -c $<
65 c_o_COMPILE = $$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) \
66 $(CPPFLAGS) $(AM_CFLAGS) $$(CFLAGS)
68 cc_o_COMPILE = $$(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) \
69 $(CPPFLAGS) $(AM_CFLAGS) $$(CFLAGS)
71 S_o_COMPILE = $$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) \
72 $(CPPFLAGS) $(AM_CFLAGS) $$(CFLAGS) $(ASFLAGS)
75 # Check GCC version for user build
76 ifndef CC_MAJOR_VERSION
77 CC_MAJOR_VERSION = $$(shell $$(CC) -dumpversion | sed -e 's/\([^.]\)\..*/\1/')
79 # Prepare suitable define for dependency building
80 ifeq ($$(CC_MAJOR_VERSION),2)
81 CC_DEPFLAGS = -Wp,-MD,"$$@.d.tmp"
83 CC_DEPFLAGS = -MT $$@ -MD -MP -MF "$$@.d.tmp"
86 endef # COMPILER_DEFS_template
89 # Syntax: $(call COMPILE_c_o_template,<source>,<target>,<additional c-flags>)
90 define COMPILE_c_o_template
91 $(2): $(1) $$(GEN_HEADERS)
92 @$(QUIET_CMD_ECHO) " CC $$@"
93 $(Q) if $$(c_o_COMPILE) $$(CC_DEPFLAGS) $(3) -o $$@ -c $$< ; \
94 then mv -f "$$@.d.tmp" "$$@.d" ; \
95 else rm -f "$$@.d.tmp" ; exit 1; \
101 # Syntax: $(call COMPILE_cc_o_template,<source>,<target>,<additional c-flags>)
102 define COMPILE_cc_o_template
103 $(2): $(1) $$(GEN_HEADERS)
104 @$(QUIET_CMD_ECHO) " CXX $$@"
105 $(Q) if $$(cc_o_COMPILE) $$(CC_DEPFLAGS) $(3) -o $$@ -c $$< ; \
106 then mv -f "$$@.d.tmp" "$$@.d" ; \
107 else rm -f "$$@.d.tmp" ; exit 1; \
111 # Syntax: $(call COMPILE_S_o_template,<source>,<target>,<additional c-flags>)
112 define COMPILE_S_o_template
113 $(2): $(1) $$(GEN_HEADERS)
114 @$(QUIET_CMD_ECHO) " AS $$@"
115 $(Q) if $$(S_o_COMPILE) -D__ASSEMBLY__ $$(CC_DEPFLAGS) $(3) -o $$@ -c $$< ; \
116 then if [ -e "$$@.d.tmp" ] ; then mv -f "$$@.d.tmp" "$$@.d" ; fi ; \
117 else rm -f "$$@.d.tmp" ; exit 1; \
121 # Syntax: $(call CMETRIC_o_h_template,<object_file>,<target_header>)
122 define CMETRIC_o_h_template
124 @$(QUIET_CMD_ECHO) " CMETRIC $$@"
125 $(Q)if [ -n `dirname $$@` ] ; then \
126 if [ ! -e `dirname $$@` ] ; then \
127 mkdir -p `dirname $$@` ; fi ; fi
128 $(Q)echo >$$@ '/* Automatically generated from $$< */'
129 $(Q)echo >>$$@ '/* Conditionals to control compilation */'
130 $(Q)set -o pipefail ; $(NM) $$< \
131 | sed -n 's/^ *0*\(0[0-9A-Fa-f]*\) *A *_cmetric2cond_\([A-Za-z_0-9]*\) */#define \2 0x\1/p' \
133 $(Q)echo >>$$@ '/* Defines from the values defined to symbols */'
134 $(Q)set -o pipefail ; $(NM) $$< \
135 | sed -n 's/^ *0*\(0[0-9A-Fa-f]*\) *A *_cmetric2def_\([A-Za-z_0-9]*\) */#define \2 0x\1/p' \
139 # Syntax: $(call PROGRAM_template,<executable-name>,<dir>,<link-variant>)
140 define PROGRAM_template
141 $(1)_OBJS += $$(filter %.o,$$($(1)_SOURCES:%.c=%.o))
142 $(1)_OBJS += $$(filter %.o,$$($(1)_SOURCES:%.cc=%.o))
143 $(1)_OBJS += $$(filter %.o,$$($(1)_SOURCES:%.cxx=%.o))
144 $(1)_OBJS += $$(filter %.o,$$($(1)_SOURCES:%.S=%.o))
145 $(1)_OBJS += $$(filter %.o,$(1)_SOURCES)
146 $(1)_OBJS := $$(sort $$($(1)_OBJS))
148 SOURCES += $$($(1)_SOURCES)
150 ifneq ($(LD_SCRIPT),)
151 $(1)$(3:%=-%)_LDFLAGS = -Wl,-T,$(LD_SCRIPT).ld$(3:%=-%)
154 $(2)/$(1)$(3:%=-%): $$($(1)_OBJS)
155 @$(QUIET_CMD_ECHO) " LINK $$@"
156 $(Q) $$(shell if [ -z "$$(filter %.cc,$$($(1)_SOURCES))" ] ; \
157 then echo $$(CC) $$(CPPFLAGS) $$(AM_CPPFLAGS) $$(AM_CFLAGS) $$(CFLAGS) ; \
158 else echo $$(CXX) $$(CPPFLAGS) $$(AM_CPPFLAGS) $$(AM_CXXFLAGS) $$(CXXFLAGS) ; fi) \
159 $$(AM_LDFLAGS) $$(LDFLAGS) $$($(1)$(3:%=-%)_LDFLAGS) -Wl,-Map,$(1)$(3:%=-%).map \
160 $$($(1)_OBJS) $$(LOADLIBES) $$($(1)_MOREOBJS) $$($(1)_LIBS:%=-l%) \
162 @echo "$(2)/$(1)$(3:%=-%): \\" >$(OBJS_DIR)/$(1)$(3:%=-%).exe.d
163 @if [ -n "$(LD_SCRIPT)" ]; then \
164 echo " $(LIB_DIR)/$(LD_SCRIPT).ld$(3:%=-%) \\" >>$(OBJS_DIR)/$(1)$(3:%=-%).exe.d; fi
165 @sed -n -e 's|^LOAD \(.*\)$$$$| \1 \&|p' $(OBJS_DIR)/$(1)$(3:%=-%).map|tr '&' '\134' >>$(OBJS_DIR)/$(1)$(3:%=-%).exe.d
166 @echo >>$(OBJS_DIR)/$(1).exe.d
169 # Rules for other output formats (can be specified by OUTPUT_FORMATS)
171 @$(QUIET_CMD_ECHO) " OBJCOPY $@"
172 $(Q) $(OBJCOPY) --output-target=binary -S $< $@
175 @$(QUIET_CMD_ECHO) " OBJCOPY $@"
176 $(Q) $(OBJCOPY) --output-target=ihex -S $< $@
179 @$(QUIET_CMD_ECHO) " OBJCOPY $@"
180 $(Q) $(OBJCOPY) --output-target=srec -S $< $@
182 # Syntax: $(call LIBRARY_template,<library-name>)
183 define LIBRARY_template
184 $(1)_OBJS += $$(filter %.o,$$($(1)_SOURCES:%.c=%.o))
185 $(1)_OBJS += $$(filter %.o,$$($(1)_SOURCES:%.cc=%.o))
186 $(1)_OBJS += $$(filter %.o,$$($(1)_SOURCES:%.cxx=%.o))
187 $(1)_OBJS += $$(filter %.o,$$($(1)_SOURCES:%.S=%.o))
188 $(1)_OBJS := $$(sort $$($(1)_OBJS))
190 SOURCES += $$($(1)_SOURCES)
192 $(LIB_DIR)/lib$(1).a: $$($(1)_OBJS)
193 @$(QUIET_CMD_ECHO) " AR $$@"
194 $(Q) $(AR) rcs $$@ $$^
198 # Syntax: $(call SOLIB_template,<library-name>)
199 define SOLIB_template
200 $(1)_OBJSLO += $$(filter %.lo,$$($(1)_SOURCES:%.c=%.lo))
201 $(1)_OBJSLO += $$(filter %.lo,$$($(1)_SOURCES:%.cc=%.lo))
202 $(1)_OBJSLO += $$(filter %.lo,$$($(1)_SOURCES:%.cxx=%.lo))
203 $(1)_OBJSLO += $$(filter %.lo,$$($(1)_SOURCES:%.S=%.lo))
204 $(1)_OBJSLO := $$(sort $$($(1)_OBJSLO))
206 SOLIB_OBJS += $$($(1)_OBJSLO)
207 SOLIB_SOURCES += $$($(1)_SOURCES)
209 $(LIB_DIR)/lib$(1).so: $$($(1)_OBJSLO)
210 @$(QUIET_CMD_ECHO) " LINK $$@"
211 $(Q) $(LD) --shared --soname=lib$(1).so -o $$@ $$^