]> rtime.felk.cvut.cz Git - omk.git/blob - snippets/gcc.omk
Move cmetric rules to a separate snippet
[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 # 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.
36
37 define COMPILER_DEFS_template
38 OBJ_EXT = .o
39 LIB_EXT = .a
40 LIB_PREF = lib
41 ASM_EXT = .S
42
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
48
49 CFLAGS += $(TARGET_ARCH) $(DEBUG) $(OPTIMIZE)
50 CFLAGS  += -Wall
51 CFLAGS += -I$(SOURCES_DIR)
52 CFLAGS += -I$(INCLUDE_DIR)
53
54 LOADLIBES += -L$(LIB_DIR)
55 LOADLIBES += $(lib_LOADLIBES:%=-l%)
56
57
58 -include $(OBJS_DIR)/*.d
59
60 SOURCES2OBJS = .o/.c .o/.cc .o/.cxx .o/.S .o/.o
61
62 SOURCES2OBJSLO = .lo/.c .lo/.cc .lo/.cxx .lo/.S .lo/.lo
63
64 #%.lo: %.c
65 #       $(CC) -o $@ $(LCFLAGS) -c $<
66
67 c_o_COMPILE = $$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) \
68         $(CPPFLAGS) $(AM_CFLAGS) $$(CFLAGS)
69
70 cc_o_COMPILE = $$(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) \
71         $(CPPFLAGS) $(CXXFLAGS) $(AM_CFLAGS) $$(CFLAGS)
72
73 S_o_COMPILE = $$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) \
74         $(CPPFLAGS) $(AM_CFLAGS) $$(CFLAGS) $(ASFLAGS)
75
76
77 # Check GCC version for user build
78 ifndef CC_MAJOR_VERSION
79 CC_MAJOR_VERSION = $$(shell $$(CC) -dumpversion | $(SED4OMK) -e 's/\([^.]\)\..*/\1/')
80 endif
81 # Prepare suitable define for dependency building
82 ifeq ($$(CC_MAJOR_VERSION),2)
83 CC_DEPFLAGS = -Wp,-MD,"$$@.d.tmp"
84 else
85 CC_DEPFLAGS = -MT $$@ -MD -MP -MF "$$@.d.tmp"
86 endif
87
88 endef # COMPILER_DEFS_template
89
90
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; \
98         fi
99 endef
100
101
102
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; \
110         fi
111 endef
112
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; \
120         fi
121 endef
122
123 # Syntax: $(call PROGRAM_template,<executable-name>,<dir>,<link-variant>)
124 define PROGRAM_template
125
126 GEN_SOURCES += $$($(1)_GEN_SOURCES)
127
128 $(foreach x, $(SOURCES2OBJS),
129 $(1)_OBJS += $$(patsubst %$(notdir $(x)),%$(dir $(x)),$$(filter %$(notdir $(x)),\
130                 $$($(1)_SOURCES) $$($(1)_GEN_SOURCES)))
131 )
132 $(1)_OBJS := $$(sort $$($(1)_OBJS:%/=%))
133
134 SOURCES += $$($(1)_SOURCES)
135
136 ifneq ($(LD_SCRIPT),)
137 $(1)$(3:%=-%)_LDFLAGS = -Wl,-T,$(LD_SCRIPT).ld$(3:%=-%)
138 endif
139
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%) \
147           -o $$@
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
153 endef
154
155 # Rules for other output formats (can be specified by OUTPUT_FORMATS)
156 %.bin: %
157         @$(QUIET_CMD_ECHO) "  OBJCOPY $@"
158         $(Q) $(OBJCOPY)  --output-target=binary -S $< $@
159
160 %.hex: %
161         @$(QUIET_CMD_ECHO) "  OBJCOPY $@"
162         $(Q) $(OBJCOPY)  --output-target=ihex -S $< $@
163
164 %.srec: %
165         @$(QUIET_CMD_ECHO) "  OBJCOPY $@"
166         $(Q) $(OBJCOPY)  --output-target=srec -S $< $@
167
168 # Syntax: $(call LIBRARY_template,<library-name>)
169 define LIBRARY_template
170
171 GEN_SOURCES += $$($(1)_GEN_SOURCES)
172
173 $(foreach x, $(SOURCES2OBJS),
174 $(1)_OBJS += $$(patsubst %$(notdir $(x)),%$(dir $(x)),$$(filter %$(notdir $(x)),\
175                 $$($(1)_SOURCES) $$($(1)_GEN_SOURCES)))
176 )
177 $(1)_OBJS := $$(sort $$($(1)_OBJS:%/=%))
178
179 SOURCES += $$($(1)_SOURCES)
180
181 $(LIB_DIR)/lib$(1).a: $$($(1)_OBJS)
182         @$(QUIET_CMD_ECHO) "  AR      $$@"
183         $(Q) $(AR) rcs $$@ $$^
184 endef
185
186
187 # Syntax: $(call SOLIB_template,<library-name>)
188 define SOLIB_template
189
190 SOLIB_GEN_SOURCES += $$($(1)_GEN_SOURCES)
191
192 $(foreach x, $(SOURCES2OBJSLO),
193 $(1)_OBJSLO += $$(patsubst %$(notdir $(x)),%$(dir $(x)),$$(filter %$(notdir $(x)),\
194                 $$($(1)_SOURCES) $$($(1)_GEN_SOURCES)))
195 )
196 $(1)_OBJSLO := $$(sort $$($(1)_OBJSLO:%/=%))
197
198 SOLIB_OBJS  += $$($(1)_OBJSLO)
199 SOLIB_SOURCES += $$($(1)_SOURCES)
200
201 $(LIB_DIR)/lib$(1).so: $$($(1)_OBJSLO)
202         @$(QUIET_CMD_ECHO) "  LINK    $$@"
203         $(Q) $(LD) --shared --soname=lib$(1).so -o $$@ $$^
204 endef