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
22 # COMPILER_DEFS_template - definitions that should be defined before
23 # the following templates can be used. The input variables needs to be
24 # defined before evaluating this template
26 # COMPILE_c_o_template, COMPILE_cc_o_template, COMPILE_S_o_template -
27 # templates that create rules for compilation of sources
29 # CMETRIC_o_h_template - FIXME
31 # PROGRAM_template, LIBRARY_template, SOLIB_template - templates that
32 # create rules for compilation of a program, library and shared
33 # library. The rules can use rules produced by COMPILE_xxx_template.
35 define COMPILER_DEFS_template
36 CC = $(CROSS_COMPILE)gcc
37 LINK = $(CROSS_COMPILE)ld
38 AR = $(CROSS_COMPILE)ar
39 OBJCOPY = $(CROSS_COMPILE)objcopy
40 NM = $(CROSS_COMPILE)nm
42 CFLAGS += $(TARGET_ARCH) $(DEBUG) $(OPTIMIZE)
44 CFLAGS += -I$(SOURCES_DIR)
45 CFLAGS += -I$(INCLUDE_DIR)
47 LOADLIBES += -L$(LIB_DIR)
48 LOADLIBES += $(lib_LOADLIBES:%=-l%)
51 -include $(OBJS_DIR)/*.d
54 # $(CC) -o $@ $(LCFLAGS) -c $<
56 c_o_COMPILE = $$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) \
57 $(CPPFLAGS) $(AM_CFLAGS) $$(CFLAGS)
59 cc_o_COMPILE = $$(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) \
60 $(CPPFLAGS) $(AM_CFLAGS) $$(CFLAGS)
62 S_o_COMPILE = $$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) \
63 $(CPPFLAGS) $(AM_CFLAGS) $$(CFLAGS) $(ASFLAGS)
66 # Check GCC version for user build
67 ifndef CC_MAJOR_VERSION
68 CC_MAJOR_VERSION = $$(shell $$(CC) -dumpversion | sed -e 's/\([^.]\)\..*/\1/')
70 # Prepare suitable define for dependency building
71 ifeq ($$(CC_MAJOR_VERSION),2)
72 CC_DEPFLAGS = -Wp,-MD,"$$@.d.tmp"
74 CC_DEPFLAGS = -MT $$@ -MD -MP -MF "$$@.d.tmp"
77 # lib_obj_SOURCES handling
79 @cp $$(CP_FLAGS) $$< $$@
80 endef # COMPILER_DEFS_template
83 # Syntax: $(call COMPILE_c_o_template,<source>,<target>,<additional c-flags>)
84 define COMPILE_c_o_template
85 $(2): $(1) $(LOCAL_CONFIG_H) $$(GEN_HEADERS)
86 @$(QUIET_CMD_ECHO) " CC $$@"
87 $(Q) if $$(c_o_COMPILE) $$(CC_DEPFLAGS) $(3) -o $$@ -c $$< ; \
88 then mv -f "$$@.d.tmp" "$$@.d" ; \
89 else rm -f "$$@.d.tmp" ; exit 1; \
95 # Syntax: $(call COMPILE_cc_o_template,<source>,<target>,<additional c-flags>)
96 define COMPILE_cc_o_template
97 $(2): $(1) $(LOCAL_CONFIG_H) $$(GEN_HEADERS)
98 @$(QUIET_CMD_ECHO) " CXX $$@"
99 $(Q) if $$(cc_o_COMPILE) $$(CC_DEPFLAGS) $(3) -o $$@ -c $$< ; \
100 then mv -f "$$@.d.tmp" "$$@.d" ; \
101 else rm -f "$$@.d.tmp" ; exit 1; \
105 # Syntax: $(call COMPILE_S_o_template,<source>,<target>,<additional c-flags>)
106 define COMPILE_S_o_template
107 $(2): $(1) $(LOCAL_CONFIG_H) $$(GEN_HEADERS)
108 @$(QUIET_CMD_ECHO) " AS $$@"
109 $(Q) if $$(S_o_COMPILE) -D__ASSEMBLY__ $$(CC_DEPFLAGS) $(3) -o $$@ -c $$< ; \
110 then if [ -e "$$@.d.tmp" ] ; then mv -f "$$@.d.tmp" "$$@.d" ; fi ; \
111 else rm -f "$$@.d.tmp" ; exit 1; \
115 # Syntax: $(call CMETRIC_o_h_template,<object_file>,<target_header>)
116 define CMETRIC_o_h_template
118 @$(QUIET_CMD_ECHO) " CMETRIC $$@"
119 $(Q)if [ -n `dirname $$@` ] ; then \
120 if [ ! -e `dirname $$@` ] ; then \
121 mkdir -p `dirname $$@` ; fi ; fi
122 $(Q)echo >$$@ '/* Automatically generated from $$< */'
123 $(Q)echo >>$$@ '/* Conditionals to control compilation */'
124 $(Q)set -o pipefail ; $(NM) $$< \
125 | sed -n 's/^ *0*\(0[0-9A-Fa-f]*\) *A *_cmetric2cond_\([A-Za-z_0-9]*\) */#define \2 0x\1/p' \
127 $(Q)echo >>$$@ '/* Defines from the values defined to symbols */'
128 $(Q)set -o pipefail ; $(NM) $$< \
129 | sed -n 's/^ *0*\(0[0-9A-Fa-f]*\) *A *_cmetric2def_\([A-Za-z_0-9]*\) */#define \2 0x\1/p' \
133 # Syntax: $(call PROGRAM_template,<executable-name>,<dir>,<link-variant>)
134 define PROGRAM_template
135 $(1)_OBJS += $$(filter %.o,$$($(1)_SOURCES:%.c=%.o))
136 $(1)_OBJS += $$(filter %.o,$$($(1)_SOURCES:%.cc=%.o))
137 $(1)_OBJS += $$(filter %.o,$$($(1)_SOURCES:%.cxx=%.o))
138 $(1)_OBJS += $$(filter %.o,$$($(1)_SOURCES:%.S=%.o))
139 $(1)_OBJS += $$(filter %.o,$(1)_SOURCES)
140 $(1)_OBJS := $$(sort $$($(1)_OBJS))
142 SOURCES += $$($(1)_SOURCES)
144 ifneq ($(LD_SCRIPT),)
145 $(1)$(3:%=-%)_LDFLAGS = -Wl,-T,$(LD_SCRIPT).ld$(3:%=-%)
148 $(2)/$(1)$(3:%=-%): $$($(1)_OBJS)
149 @$(QUIET_CMD_ECHO) " LINK $$@"
150 $(Q) $$(shell if [ -z "$$(filter %.cc,$$($(1)_SOURCES))" ] ; \
151 then echo $$(CC) $$(CPPFLAGS) $$(AM_CPPFLAGS) $$(AM_CFLAGS) $$(CFLAGS) ; \
152 else echo $$(CXX) $$(CPPFLAGS) $$(AM_CPPFLAGS) $$(AM_CXXFLAGS) $$(CXXFLAGS) ; fi) \
153 $$(AM_LDFLAGS) $$(LDFLAGS) $$($(1)$(3:%=-%)_LDFLAGS) -Wl,-Map,$(1)$(3:%=-%).map \
154 $$($(1)_OBJS) $$(LOADLIBES) $$($(1)_MOREOBJS) $$($(1)_LIBS:%=-l%) \
156 @echo "$(2)/$(1)$(3:%=-%): \\" >$(OBJS_DIR)/$(1)$(3:%=-%).exe.d
157 @if [ -n "$(LD_SCRIPT)" ]; then \
158 echo " $(LIB_DIR)/$(LD_SCRIPT).ld$(3:%=-%) \\" >>$(OBJS_DIR)/$(1)$(3:%=-%).exe.d; fi
159 @sed -n -e 's/^LOAD \(.*\)$$$$/ \1 \\/p' $(OBJS_DIR)/$(1)$(3:%=-%).map >>$(OBJS_DIR)/$(1)$(3:%=-%).exe.d
160 @echo >>$(OBJS_DIR)/$(1).exe.d
163 # Rules for other output formats (can be specified by OUTPUT_FORMATS)
165 @$(QUIET_CMD_ECHO) " OBJCOPY $@"
166 $(Q) $(OBJCOPY) --output-target=binary -S $< $@
169 @$(QUIET_CMD_ECHO) " OBJCOPY $@"
170 $(Q) $(OBJCOPY) --output-target=ihex -S $< $@
173 @$(QUIET_CMD_ECHO) " OBJCOPY $@"
174 $(Q) $(OBJCOPY) --output-target=srec -S $< $@
176 # Syntax: $(call LIBRARY_template,<library-name>)
177 define LIBRARY_template
178 $(1)_OBJS += $$(filter %.o,$$($(1)_SOURCES:%.c=%.o))
179 $(1)_OBJS += $$(filter %.o,$$($(1)_SOURCES:%.cc=%.o))
180 $(1)_OBJS += $$(filter %.o,$$($(1)_SOURCES:%.cxx=%.o))
181 $(1)_OBJS += $$(filter %.o,$$($(1)_SOURCES:%.S=%.o))
182 $(1)_OBJS := $$(sort $$($(1)_OBJS))
184 SOURCES += $$($(1)_SOURCES)
186 $(LIB_DIR)/lib$(1).a: $$($(1)_OBJS)
187 @$(QUIET_CMD_ECHO) " AR $$@"
188 $(Q) $(AR) rcs $$@ $$^
192 # Syntax: $(call SOLIB_template,<library-name>)
193 define SOLIB_template
194 $(1)_OBJSLO += $$(filter %.lo,$$($(1)_SOURCES:%.c=%.lo))
195 $(1)_OBJSLO += $$(filter %.lo,$$($(1)_SOURCES:%.cc=%.lo))
196 $(1)_OBJSLO += $$(filter %.lo,$$($(1)_SOURCES:%.cxx=%.lo))
197 $(1)_OBJSLO += $$(filter %.lo,$$($(1)_SOURCES:%.S=%.lo))
198 $(1)_OBJSLO := $$(sort $$($(1)_OBJSLO))
200 SOLIB_OBJS += $$($(1)_OBJSLO)
201 SOLIB_SOURCES += $$($(1)_SOURCES)
203 $(LIB_DIR)/lib$(1).so: $$($(1)_OBJSLO)
204 @$(QUIET_CMD_ECHO) " LINK $$@"
205 $(Q) $(LD) --shared --soname=lib$(1).so -o $$@ $$^
209 # lib_obj_SOURCES handling
210 lib_OBJS = $(addsuffix .o,$(basename $(lib_obj_SOURCES)))
211 SOURCES += $(filter-out %.o,$(lib_obj_SOURCES))
212 #$(warning lib_OBJS = $(lib_OBJS))
214 # # Syntax: $(call LIBOBJ_template,<object-name>)
215 # define LIBOBJ_template
216 # $(LIB_DIR)/$(1): $(1)
217 # @cp $(CP_FLAGS) $$< $$@