]> rtime.felk.cvut.cz Git - omk.git/blob - snippets/gcc.omk
NuttX: define LD to point to GNU ld, NuttX provided LD points to GCC wrapper now .
[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 CXX = $(CROSS_COMPILE)g++
45 LINK = $(CROSS_COMPILE)ld
46 AR = $(CROSS_COMPILE)ar
47 OBJCOPY = $(CROSS_COMPILE)objcopy
48 NM = $(CROSS_COMPILE)nm
49
50 CFLAGS += $(TARGET_ARCH) $(DEBUG) $(OPTIMIZE)
51 CFLAGS  += -Wall
52 CFLAGS += -I$(SOURCES_DIR)
53 CFLAGS += -I$(INCLUDE_DIR)
54
55 LOADLIBES += -L$(LIB_DIR)
56 LOADLIBES += $(lib_LOADLIBES:%=-l%)
57
58
59 -include $(OBJS_DIR)/*.d
60
61 SOURCES2OBJS = .o/.c .o/.cc .o/.cxx .o/.S .o/.s .o/.o
62
63 SOURCES2OBJSLO = .lo/.c .lo/.cc .lo/.cxx .lo/.S .lo/.s .lo/.lo
64
65 #%.lo: %.c
66 #       $(CC) -o $@ $(LCFLAGS) -c $<
67
68 c_o_COMPILE = $$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(OMK_CPPFLAGS) \
69         $(CPPFLAGS) $(OMK_CFLAGS) $$(CFLAGS)
70
71 cc_o_COMPILE = $$(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(OMK_CPPFLAGS) \
72         $(CPPFLAGS) $(CXXFLAGS) $(OMK_CFLAGS) $$(CFLAGS)
73
74 S_o_COMPILE = $$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) \
75         $(CPPFLAGS) $(OMK_CFLAGS) $$(CFLAGS) $(ASFLAGS)
76
77
78 # Check GCC version for user build
79 ifndef CC_MAJOR_VERSION
80 CC_MAJOR_VERSION = $$(shell $$(CC) -dumpversion | $(SED4OMK) -e 's/\([^.]\)\..*/\1/')
81 endif
82 # Prepare suitable define for dependency building
83 ifeq ($$(CC_MAJOR_VERSION),2)
84 CC_DEPFLAGS = -Wp,-MD,"$$@.d.tmp"
85 else
86 CC_DEPFLAGS = -MT $$@ -MD -MP -MF "$$@.d.tmp"
87 endif
88
89 endef # COMPILER_DEFS_template
90
91
92 # Syntax: $(call COMPILE_c_o_template,<source>,<target>,<additional c-flags>)
93 define COMPILE_c_o_template
94 $(2): $(1) $$(GEN_HEADERS)
95         @$(QUIET_CMD_ECHO) "  CC      $$@"
96         $(Q) if $$(c_o_COMPILE) $$(CC_DEPFLAGS) $(3) -o $$@ -c $$< ; \
97         then mv -f "$$@.d.tmp" "$$@.d" ; \
98         else rm -f "$$@.d.tmp" ; exit 1; \
99         fi
100 endef
101
102
103
104 # Syntax: $(call COMPILE_cc_o_template,<source>,<target>,<additional c-flags>)
105 define COMPILE_cc_o_template
106 $(2): $(1) $$(GEN_HEADERS)
107         @$(QUIET_CMD_ECHO) "  CXX     $$@"
108         $(Q) if $$(cc_o_COMPILE) $$(CC_DEPFLAGS) $(3) -o $$@ -c $$< ; \
109         then mv -f "$$@.d.tmp" "$$@.d" ; \
110         else rm -f "$$@.d.tmp" ; exit 1; \
111         fi
112 endef
113
114 # Syntax: $(call COMPILE_S_o_template,<source>,<target>,<additional c-flags>)
115 define COMPILE_S_o_template
116 $(2): $(1) $$(GEN_HEADERS)
117         @$(QUIET_CMD_ECHO) "  AS      $$@"
118         $(Q) if $$(S_o_COMPILE) -D__ASSEMBLY__ $$(CC_DEPFLAGS) $(3) -o $$@ -c $$< ; \
119         then if [ -e "$$@.d.tmp" ] ; then mv -f "$$@.d.tmp" "$$@.d" ; fi ; \
120         else rm -f "$$@.d.tmp" ; exit 1; \
121         fi
122 endef
123
124 # Syntax: $(call PROGRAM_template,<executable-name>,<dir>,<link-variant>)
125 define PROGRAM_template
126
127 GEN_SOURCES += $$($(1)_GEN_SOURCES)
128
129 $(foreach x, $(SOURCES2OBJS),
130 $(1)_OBJS += $$(patsubst %$(notdir $(x)),%$(dir $(x)),$$(filter %$(notdir $(x)),\
131                 $$($(1)_SOURCES) $$($(1)_GEN_SOURCES)))
132 )
133 $(1)_OBJS := $$(sort $$($(1)_OBJS:%/=%))
134
135 SOURCES += $$($(1)_SOURCES)
136
137 ifneq ($(LD_SCRIPT),)
138 $(1)$(3:%=-%)_LDFLAGS = -Wl,-T,$(LD_SCRIPT).ld$(3:%=-%)
139 endif
140
141 $(2)/$(1)$(3:%=-%): $$($(1)_OBJS)
142         @$(QUIET_CMD_ECHO) "  LINK    $$@"
143         $(Q) $$(if $$(filter %.cc,$$($(1)_SOURCES)) , \
144           $$(CXX) $$(CPPFLAGS) $$(OMK_CPPFLAGS) $$(OMK_CXXFLAGS) $$(CXXFLAGS) , \
145           $$(CC)  $$(CPPFLAGS) $$(OMK_CPPFLAGS) $$(OMK_CFLAGS)   $$(CFLAGS) ) \
146           $$(OMK_LDFLAGS) $$(LDFLAGS) $$($(1)$(3:%=-%)_LDFLAGS) -Wl,-Map,$(1)$(3:%=-%).map \
147           $$($(1)_OBJS) $$($(1)_MOREOBJS) $$(LOADLIBES) $$($(1)_LIBS:%=-l%) \
148           -o $$@
149         @echo "$(2)/$(1)$(3:%=-%): \\" >$(OBJS_DIR)/$(1)$(3:%=-%).exe.d
150         @if [ -n "$(LD_SCRIPT)" ]; then \
151           echo "  $(LIB_DIR)/$(LD_SCRIPT).ld$(3:%=-%) \\" >>$(OBJS_DIR)/$(1)$(3:%=-%).exe.d; fi
152         @$(SED4OMK) -n -e 's|^LOAD \(.*\)$$$$|  \1  \&|p' $(OBJS_DIR)/$(1)$(3:%=-%).map|tr '&' '\134'  >>$(OBJS_DIR)/$(1)$(3:%=-%).exe.d
153         @echo >>$(OBJS_DIR)/$(1).exe.d
154 endef
155
156 # Rules for other output formats (can be specified by OUTPUT_FORMATS)
157 %.bin: %
158         @$(QUIET_CMD_ECHO) "  OBJCOPY $@"
159         $(Q) $(OBJCOPY)  --output-target=binary -S $< $@
160
161 %.hex: %
162         @$(QUIET_CMD_ECHO) "  OBJCOPY $@"
163         $(Q) $(OBJCOPY)  --output-target=ihex -S $< $@
164
165 %.srec: %
166         @$(QUIET_CMD_ECHO) "  OBJCOPY $@"
167         $(Q) $(OBJCOPY)  --output-target=srec -S $< $@
168
169 # Syntax: $(call LIBRARY_template,<library-name>)
170 define LIBRARY_template
171
172 GEN_SOURCES += $$($(1)_GEN_SOURCES)
173
174 $(foreach x, $(SOURCES2OBJS),
175 $(1)_OBJS += $$(patsubst %$(notdir $(x)),%$(dir $(x)),$$(filter %$(notdir $(x)),\
176                 $$($(1)_SOURCES) $$($(1)_GEN_SOURCES)))
177 )
178 $(1)_OBJS := $$(sort $$($(1)_OBJS:%/=%))
179
180 SOURCES += $$($(1)_SOURCES)
181
182 $(LIB_DIR)/lib$(1).a: $$($(1)_OBJS)
183         @$(QUIET_CMD_ECHO) "  AR      $$@"
184         $(Q) $(AR) rcs $$@ $$^
185 endef
186
187
188 # Syntax: $(call SOLIB_template,<library-name>)
189 define SOLIB_template
190
191 SOLIB_GEN_SOURCES += $$($(1)_GEN_SOURCES)
192
193 $(foreach x, $(SOURCES2OBJSLO),
194 $(1)_OBJSLO += $$(patsubst %$(notdir $(x)),%$(dir $(x)),$$(filter %$(notdir $(x)),\
195                 $$($(1)_SOURCES) $$($(1)_GEN_SOURCES)))
196 )
197 $(1)_OBJSLO := $$(sort $$($(1)_OBJSLO:%/=%))
198
199 SOLIB_OBJS  += $$($(1)_OBJSLO)
200 SOLIB_SOURCES += $$($(1)_SOURCES)
201
202 $(LIB_DIR)/lib$(1).so: $$($(1)_OBJSLO)
203         @$(QUIET_CMD_ECHO) "  LINK    $$@"
204         $(Q) $(LD) --shared --soname=lib$(1).so -o $$@ $$^
205 endef