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 # PROGRAM_template, LIBRARY_template, SOLIB_template - templates that
34 # create rules for compilation of a program, library and shared
35 # library. The rules can use rules produced by COMPILE_xxx_template.
37 define COMPILER_DEFS_template
43 CC = $(CROSS_COMPILE)gcc
44 LINK = $(CROSS_COMPILE)ld
45 AR = $(CROSS_COMPILE)ar
46 OBJCOPY = $(CROSS_COMPILE)objcopy
47 NM = $(CROSS_COMPILE)nm
49 CFLAGS += $(TARGET_ARCH) $(DEBUG) $(OPTIMIZE)
51 CFLAGS += -I$(SOURCES_DIR)
52 CFLAGS += -I$(INCLUDE_DIR)
54 LOADLIBES += -L$(LIB_DIR)
55 LOADLIBES += $(lib_LOADLIBES:%=-l%)
58 -include $(OBJS_DIR)/*.d
60 SOURCES2OBJS = .o/.c .o/.cc .o/.cxx .o/.S .o/.o
62 SOURCES2OBJSLO = .lo/.c .lo/.cc .lo/.cxx .lo/.S .lo/.lo
65 # $(CC) -o $@ $(LCFLAGS) -c $<
67 c_o_COMPILE = $$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) \
68 $(CPPFLAGS) $(AM_CFLAGS) $$(CFLAGS)
70 cc_o_COMPILE = $$(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) \
71 $(CPPFLAGS) $(CXXFLAGS) $(AM_CFLAGS) $$(CFLAGS)
73 S_o_COMPILE = $$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) \
74 $(CPPFLAGS) $(AM_CFLAGS) $$(CFLAGS) $(ASFLAGS)
77 # Check GCC version for user build
78 ifndef CC_MAJOR_VERSION
79 CC_MAJOR_VERSION = $$(shell $$(CC) -dumpversion | $(SED4OMK) -e 's/\([^.]\)\..*/\1/')
81 # Prepare suitable define for dependency building
82 ifeq ($$(CC_MAJOR_VERSION),2)
83 CC_DEPFLAGS = -Wp,-MD,"$$@.d.tmp"
85 CC_DEPFLAGS = -MT $$@ -MD -MP -MF "$$@.d.tmp"
88 endef # COMPILER_DEFS_template
91 # Syntax: $(call COMPILE_c_o_template,<source>,<target>,<additional c-flags>)
92 define COMPILE_c_o_template
93 $(2): $(1) $$(GEN_HEADERS)
94 @$(QUIET_CMD_ECHO) " CC $$@"
95 $(Q) if $$(c_o_COMPILE) $$(CC_DEPFLAGS) $(3) -o $$@ -c $$< ; \
96 then mv -f "$$@.d.tmp" "$$@.d" ; \
97 else rm -f "$$@.d.tmp" ; exit 1; \
103 # Syntax: $(call COMPILE_cc_o_template,<source>,<target>,<additional c-flags>)
104 define COMPILE_cc_o_template
105 $(2): $(1) $$(GEN_HEADERS)
106 @$(QUIET_CMD_ECHO) " CXX $$@"
107 $(Q) if $$(cc_o_COMPILE) $$(CC_DEPFLAGS) $(3) -o $$@ -c $$< ; \
108 then mv -f "$$@.d.tmp" "$$@.d" ; \
109 else rm -f "$$@.d.tmp" ; exit 1; \
113 # Syntax: $(call COMPILE_S_o_template,<source>,<target>,<additional c-flags>)
114 define COMPILE_S_o_template
115 $(2): $(1) $$(GEN_HEADERS)
116 @$(QUIET_CMD_ECHO) " AS $$@"
117 $(Q) if $$(S_o_COMPILE) -D__ASSEMBLY__ $$(CC_DEPFLAGS) $(3) -o $$@ -c $$< ; \
118 then if [ -e "$$@.d.tmp" ] ; then mv -f "$$@.d.tmp" "$$@.d" ; fi ; \
119 else rm -f "$$@.d.tmp" ; exit 1; \
123 # Syntax: $(call PROGRAM_template,<executable-name>,<dir>,<link-variant>)
124 define PROGRAM_template
126 GEN_SOURCES += $$($(1)_GEN_SOURCES)
128 $(foreach x, $(SOURCES2OBJS),
129 $(1)_OBJS += $$(patsubst %$(notdir $(x)),%$(dir $(x)),$$(filter %$(notdir $(x)),\
130 $$($(1)_SOURCES) $$($(1)_GEN_SOURCES)))
132 $(1)_OBJS := $$(sort $$($(1)_OBJS:%/=%))
134 SOURCES += $$($(1)_SOURCES)
136 ifneq ($(LD_SCRIPT),)
137 $(1)$(3:%=-%)_LDFLAGS = -Wl,-T,$(LD_SCRIPT).ld$(3:%=-%)
140 $(2)/$(1)$(3:%=-%): $$($(1)_OBJS)
141 @$(QUIET_CMD_ECHO) " LINK $$@"
142 $(Q) $$(shell if [ -z "$$(filter %.cc,$$($(1)_SOURCES))" ] ; \
143 then echo $$(CC) $$(CPPFLAGS) $$(AM_CPPFLAGS) $$(AM_CFLAGS) $$(CFLAGS) ; \
144 else echo $$(CXX) $$(CPPFLAGS) $$(AM_CPPFLAGS) $$(AM_CXXFLAGS) $$(CXXFLAGS) ; fi) \
145 $$(AM_LDFLAGS) $$(LDFLAGS) $$($(1)$(3:%=-%)_LDFLAGS) -Wl,-Map,$(1)$(3:%=-%).map \
146 $$($(1)_OBJS) $$($(1)_MOREOBJS) $$(LOADLIBES) $$($(1)_LIBS:%=-l%) \
148 @echo "$(2)/$(1)$(3:%=-%): \\" >$(OBJS_DIR)/$(1)$(3:%=-%).exe.d
149 @if [ -n "$(LD_SCRIPT)" ]; then \
150 echo " $(LIB_DIR)/$(LD_SCRIPT).ld$(3:%=-%) \\" >>$(OBJS_DIR)/$(1)$(3:%=-%).exe.d; fi
151 @$(SED4OMK) -n -e 's|^LOAD \(.*\)$$$$| \1 \&|p' $(OBJS_DIR)/$(1)$(3:%=-%).map|tr '&' '\134' >>$(OBJS_DIR)/$(1)$(3:%=-%).exe.d
152 @echo >>$(OBJS_DIR)/$(1).exe.d
155 # Rules for other output formats (can be specified by OUTPUT_FORMATS)
157 @$(QUIET_CMD_ECHO) " OBJCOPY $@"
158 $(Q) $(OBJCOPY) --output-target=binary -S $< $@
161 @$(QUIET_CMD_ECHO) " OBJCOPY $@"
162 $(Q) $(OBJCOPY) --output-target=ihex -S $< $@
165 @$(QUIET_CMD_ECHO) " OBJCOPY $@"
166 $(Q) $(OBJCOPY) --output-target=srec -S $< $@
168 # Syntax: $(call LIBRARY_template,<library-name>)
169 define LIBRARY_template
171 GEN_SOURCES += $$($(1)_GEN_SOURCES)
173 $(foreach x, $(SOURCES2OBJS),
174 $(1)_OBJS += $$(patsubst %$(notdir $(x)),%$(dir $(x)),$$(filter %$(notdir $(x)),\
175 $$($(1)_SOURCES) $$($(1)_GEN_SOURCES)))
177 $(1)_OBJS := $$(sort $$($(1)_OBJS:%/=%))
179 SOURCES += $$($(1)_SOURCES)
181 $(LIB_DIR)/lib$(1).a: $$($(1)_OBJS)
182 @$(QUIET_CMD_ECHO) " AR $$@"
183 $(Q) $(AR) rcs $$@ $$^
187 # Syntax: $(call SOLIB_template,<library-name>)
188 define SOLIB_template
190 SOLIB_GEN_SOURCES += $$($(1)_GEN_SOURCES)
192 $(foreach x, $(SOURCES2OBJSLO),
193 $(1)_OBJSLO += $$(patsubst %$(notdir $(x)),%$(dir $(x)),$$(filter %$(notdir $(x)),\
194 $$($(1)_SOURCES) $$($(1)_GEN_SOURCES)))
196 $(1)_OBJSLO := $$(sort $$($(1)_OBJSLO:%/=%))
198 SOLIB_OBJS += $$($(1)_OBJSLO)
199 SOLIB_SOURCES += $$($(1)_SOURCES)
201 $(LIB_DIR)/lib$(1).so: $$($(1)_OBJSLO)
202 @$(QUIET_CMD_ECHO) " LINK $$@"
203 $(Q) $(LD) --shared --soname=lib$(1).so -o $$@ $$^