]> rtime.felk.cvut.cz Git - omk.git/blob - snippets/gcc.omk
fc16cdb55c3cbbe9205598239fa15c6764a13a1c
[omk.git] / snippets / gcc.omk
1
2 # Rules for compilation of C, C++ and assembler sources using GNU
3 # toolchain.
4
5 # Interface to other rules:
6
7 # Input variables:
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
12 # CROSS_COMPILE - 
13 # TARGET_ARCH, DEBUG, OPTIMIZE, DEFS - forms CFLAGS
14 # from base: SOURCES_DIR
15 # from Makefile.omk: lib_LOADLIBES
16
17 # Output variables:
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
24
25 # Templates:
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
29
30 # COMPILE_c_o_template, COMPILE_cc_o_template, COMPILE_S_o_template -
31 # templates that create rules for compilation of sources
32
33 # CMETRIC_o_h_template - FIXME
34
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.
38
39 define COMPILER_DEFS_template
40 OBJ_EXT = .o
41 LIB_EXT = .a
42 LIB_PREF = lib
43 ASM_EXT = .S
44
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
50
51 CFLAGS += $(TARGET_ARCH) $(DEBUG) $(OPTIMIZE)
52 CFLAGS  += -Wall
53 CFLAGS += -I$(SOURCES_DIR)
54 CFLAGS += -I$(INCLUDE_DIR)
55
56 LOADLIBES += -L$(LIB_DIR)
57 LOADLIBES += $(lib_LOADLIBES:%=-l%)
58
59
60 -include $(OBJS_DIR)/*.d
61
62 SOURCES2OBJS = .o/.c .o/.cc .o/.cxx .o/.S .o/.o
63
64 SOURCES2OBJSLO = .lo/.c .lo/.cc .lo/.cxx .lo/.S .lo/.lo
65
66 #%.lo: %.c
67 #       $(CC) -o $@ $(LCFLAGS) -c $<
68
69 c_o_COMPILE = $$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) \
70         $(CPPFLAGS) $(AM_CFLAGS) $$(CFLAGS)
71
72 cc_o_COMPILE = $$(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) \
73         $(CPPFLAGS) $(CXXFLAGS) $(AM_CFLAGS) $$(CFLAGS)
74
75 S_o_COMPILE = $$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) \
76         $(CPPFLAGS) $(AM_CFLAGS) $$(CFLAGS) $(ASFLAGS)
77
78
79 # Check GCC version for user build
80 ifndef CC_MAJOR_VERSION
81 CC_MAJOR_VERSION = $$(shell $$(CC) -dumpversion | $(SED4OMK) -e 's/\([^.]\)\..*/\1/')
82 endif
83 # Prepare suitable define for dependency building
84 ifeq ($$(CC_MAJOR_VERSION),2)
85 CC_DEPFLAGS = -Wp,-MD,"$$@.d.tmp"
86 else
87 CC_DEPFLAGS = -MT $$@ -MD -MP -MF "$$@.d.tmp"
88 endif
89
90 endef # COMPILER_DEFS_template
91
92
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; \
100         fi
101 endef
102
103
104
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; \
112         fi
113 endef
114
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; \
122         fi
123 endef
124
125 # Syntax: $(call CMETRIC_o_h_template,<object_file>,<target_header>)
126 define CMETRIC_o_h_template
127 $(2): $(1)
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
147         $(Q)mv $$@.tmp $$@
148 endef
149
150 # Syntax: $(call PROGRAM_template,<executable-name>,<dir>,<link-variant>)
151 define PROGRAM_template
152
153 GEN_SOURCES += $$($(1)_GEN_SOURCES)
154
155 $(foreach x, $(SOURCES2OBJS),
156 $(1)_OBJS += $$(patsubst %$(notdir $(x)),%$(dir $(x)),$$(filter %$(notdir $(x)),\
157                 $$($(1)_SOURCES) $$($(1)_GEN_SOURCES)))
158 )
159 $(1)_OBJS := $$(sort $$($(1)_OBJS:%/=%))
160
161 SOURCES += $$($(1)_SOURCES)
162
163 ifneq ($(LD_SCRIPT),)
164 $(1)$(3:%=-%)_LDFLAGS = -Wl,-T,$(LD_SCRIPT).ld$(3:%=-%)
165 endif
166
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%) \
174           -o $$@
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
180 endef
181
182 # Rules for other output formats (can be specified by OUTPUT_FORMATS)
183 %.bin: %
184         @$(QUIET_CMD_ECHO) "  OBJCOPY $@"
185         $(Q) $(OBJCOPY)  --output-target=binary -S $< $@
186
187 %.hex: %
188         @$(QUIET_CMD_ECHO) "  OBJCOPY $@"
189         $(Q) $(OBJCOPY)  --output-target=ihex -S $< $@
190
191 %.srec: %
192         @$(QUIET_CMD_ECHO) "  OBJCOPY $@"
193         $(Q) $(OBJCOPY)  --output-target=srec -S $< $@
194
195 # Syntax: $(call LIBRARY_template,<library-name>)
196 define LIBRARY_template
197
198 GEN_SOURCES += $$($(1)_GEN_SOURCES)
199
200 $(foreach x, $(SOURCES2OBJS),
201 $(1)_OBJS += $$(patsubst %$(notdir $(x)),%$(dir $(x)),$$(filter %$(notdir $(x)),\
202                 $$($(1)_SOURCES) $$($(1)_GEN_SOURCES)))
203 )
204 $(1)_OBJS := $$(sort $$($(1)_OBJS:%/=%))
205
206 SOURCES += $$($(1)_SOURCES)
207
208 $(LIB_DIR)/lib$(1).a: $$($(1)_OBJS)
209         @$(QUIET_CMD_ECHO) "  AR      $$@"
210         $(Q) $(AR) rcs $$@ $$^
211 endef
212
213
214 # Syntax: $(call SOLIB_template,<library-name>)
215 define SOLIB_template
216
217 SOLIB_GEN_SOURCES += $$($(1)_GEN_SOURCES)
218
219 $(foreach x, $(SOURCES2OBJSLO),
220 $(1)_OBJSLO += $$(patsubst %$(notdir $(x)),%$(dir $(x)),$$(filter %$(notdir $(x)),\
221                 $$($(1)_SOURCES) $$($(1)_GEN_SOURCES)))
222 )
223 $(1)_OBJSLO := $$(sort $$($(1)_OBJSLO:%/=%))
224
225 SOLIB_OBJS  += $$($(1)_OBJSLO)
226 SOLIB_SOURCES += $$($(1)_SOURCES)
227
228 $(LIB_DIR)/lib$(1).so: $$($(1)_OBJSLO)
229         @$(QUIET_CMD_ECHO) "  LINK    $$@"
230         $(Q) $(LD) --shared --soname=lib$(1).so -o $$@ $$^
231 endef