]> rtime.felk.cvut.cz Git - omk/sssa.git/blob - omk-devel/sysless/Makefile.rules.gcc
daf805cc27d50f10b8bd3e9dcce1677027e9a343
[omk/sssa.git] / omk-devel / sysless / Makefile.rules.gcc
1 # -*- makefile -*-
2
3 # Rules for compilation of C, C++ and assembler sources using FNU
4 # toolchain. These rules are meant to be used with GNU toolchain and
5 # are included from OMK when it is necessary to compile something for
6 # target.
7
8 -include $(USER_OBJS_DIR)/*.d
9
10 #%.lo: %.c
11 #       $(CC) -o $@ $(LCFLAGS) -c $<
12
13 c_o_COMPILE = $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) \
14         $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS)
15
16 cc_o_COMPILE = $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) \
17         $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS)
18
19 S_o_COMPILE = $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) \
20         $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) $(ASFLAGS)
21
22
23 # Check GCC version for user build
24 ifndef CC_MAJOR_VERSION
25 CC_MAJOR_VERSION := $(shell $(CC) -dumpversion | sed -e 's/\([^.]\)\..*/\1/')
26 endif
27 # Prepare suitable define for dependency building
28 ifeq ($(CC_MAJOR_VERSION),2)
29 CC_DEPFLAGS = -Wp,-MD,"$@.d.tmp"
30 else
31 CC_DEPFLAGS = -MT $@ -MD -MP -MF "$@.d.tmp"
32 endif
33
34
35
36 # Syntax: $(call COMPILE_c_o_template,<source>,<target>,<additional c-flags>)
37 define COMPILE_c_o_template
38 $(2): $(1) $(LOCAL_CONFIG_H) $$(GEN_HEADERS)
39         @$(QUIET_CMD_ECHO) "  CC      $$@"
40         $(Q) if $$(c_o_COMPILE) $$(CC_DEPFLAGS) $(3) -o $$@ -c $$< ; \
41         then mv -f "$$@.d.tmp" "$$@.d" ; \
42         else rm -f "$$@.d.tmp" ; exit 1; \
43         fi
44 endef
45
46
47
48 # Syntax: $(call COMPILE_cc_o_template,<source>,<target>,<additional c-flags>)
49 define COMPILE_cc_o_template
50 $(2): $(1) $(LOCAL_CONFIG_H) $$(GEN_HEADERS)
51         @$(QUIET_CMD_ECHO) "  CXX     $$@"
52         $(Q) if $$(cc_o_COMPILE) $$(CC_DEPFLAGS) $(3) -o $$@ -c $$< ; \
53         then mv -f "$$@.d.tmp" "$$@.d" ; \
54         else rm -f "$$@.d.tmp" ; exit 1; \
55         fi
56 endef
57
58 # Syntax: $(call COMPILE_S_o_template,<source>,<target>,<additional c-flags>)
59 define COMPILE_S_o_template
60 $(2): $(1) $(LOCAL_CONFIG_H) $$(GEN_HEADERS)
61         @$(QUIET_CMD_ECHO) "  AS      $$@"
62         $(Q) if $$(S_o_COMPILE) -D__ASSEMBLY__ $$(CC_DEPFLAGS) $(3) -o $$@ -c $$< ; \
63         then if [ -e "$$@.d.tmp" ] ; then mv -f "$$@.d.tmp" "$$@.d" ; fi ; \
64         else rm -f "$$@.d.tmp" ; exit 1; \
65         fi
66 endef
67
68 # Syntax: $(call CMETRIC_o_h_template,<object_file>,<target_header>)
69 define CMETRIC_o_h_template
70 $(2): $(1)
71         @$(QUIET_CMD_ECHO) "  CMETRIC $$@"
72         $(Q)if [ -n `dirname $$@` ] ; then \
73               if [ ! -e `dirname $$@` ] ; then \
74                 mkdir -p `dirname $$@` ; fi ; fi
75         $(Q)echo >$$@ '/* Automatically generated from $$< */'
76         $(Q)echo >>$$@ '/* Conditionals to control compilation */'
77         $(Q)set -o pipefail ; $(NM) $$< \
78                 | sed -n 's/^ *0*\(0[0-9A-Fa-f]*\) *A *_cmetric2cond_\([A-Za-z_0-9]*\) */#define \2 0x\1/p' \
79                 | sort >>$$@
80         $(Q)echo >>$$@ '/* Defines from the values defined to symbols */'
81         $(Q)set -o pipefail ; $(NM) $$< \
82                 | sed -n 's/^ *0*\(0[0-9A-Fa-f]*\) *A *_cmetric2def_\([A-Za-z_0-9]*\) */#define \2 0x\1/p' \
83                 | sort >>$$@
84 endef
85
86 # Syntax: $(call PROGRAM_template,<dir>,<executable-name>,<link-variant>)
87 define PROGRAM_template
88 $(1)_OBJS += $$(filter %.o,$$($(1)_SOURCES:%.c=%.o))
89 $(1)_OBJS += $$(filter %.o,$$($(1)_SOURCES:%.cc=%.o))
90 $(1)_OBJS += $$(filter %.o,$$($(1)_SOURCES:%.cxx=%.o))
91 $(1)_OBJS += $$(filter %.o,$$($(1)_SOURCES:%.S=%.o))
92 $(1)_OBJS += $$(filter %.o,$(1)_SOURCES)
93 $(1)_OBJS := $$(sort $$($(1)_OBJS))
94
95 SOURCES += $$($(1)_SOURCES)
96
97 ifneq ($$(LD_SCRIPT),)
98 $(1)$$(addprefix -,$(3))_LDFLAGS = -Wl,-T,$$(LD_SCRIPT).ld$$(addprefix -,$(3))
99 endif
100
101
102 $(2)/$(1)$(addprefix -,$(3)): $$($(1)_OBJS)
103         @$(QUIET_CMD_ECHO) "  LINK    $$@"
104         $(Q) $$(shell if [ -z "$$(filter %.cc,$$($(1)_SOURCES))" ] ; \
105           then echo $$(CC)  $$(CPPFLAGS) $$(AM_CPPFLAGS) $$(AM_CFLAGS)   $$(CFLAGS) ; \
106           else echo $$(CXX) $$(CPPFLAGS) $$(AM_CPPFLAGS) $$(AM_CXXFLAGS) $$(CXXFLAGS) ; fi) \
107           $$(AM_LDFLAGS) $$(LDFLAGS) $$($(1)$$(addprefix -,$(3))_LDFLAGS) -Wl,-Map,$(1)$$(addprefix -,$(3)).map \
108           $$($(1)_OBJS) $$(LOADLIBES) $$($(1)_MOREOBJS) $$($(1)_LIBS:%=-l%) \
109           -o $$@
110         @echo "$(2)/$(1)$(addprefix -,$(3)): \\" >$(USER_OBJS_DIR)/$(1)$(addprefix -,$(3)).exe.d
111         @if [ -n "$(LD_SCRIPT)" ]; then \
112           echo "  $(USER_LIB_DIR)/$(LD_SCRIPT).ld$(addprefix -,$(3)) \\" >>$(USER_OBJS_DIR)/$(1)$(addprefix -,$(3)).exe.d; fi
113         @sed -n -e 's/^LOAD \(.*\)$$$$/  \1  \\/p' $(USER_OBJS_DIR)/$(1)$(addprefix -,$(3)).map  >>$(USER_OBJS_DIR)/$(1)$(addprefix -,$(3)).exe.d
114         @echo >>$(USER_OBJS_DIR)/$(1).exe.d
115 endef
116
117 # Rules for other output formats (can be specified by OUTPUT_FORMATS)
118 %.bin: %
119         @$(QUIET_CMD_ECHO) "  OBJCOPY $@"
120         $(Q) $(OBJCOPY)  --output-target=binary -S $< $@
121
122 %.hex: %
123         @$(QUIET_CMD_ECHO) "  OBJCOPY $@"
124         $(Q) $(OBJCOPY)  --output-target=ihex -S $< $@
125
126 %.srec: %
127         @$(QUIET_CMD_ECHO) "  OBJCOPY $@"
128         $(Q) $(OBJCOPY)  --output-target=srec -S $< $@
129
130 # Syntax: $(call LIBRARY_template,<library-name>)
131 define LIBRARY_template
132 $(1)_OBJS += $$(filter %.o,$$($(1)_SOURCES:%.c=%.o))
133 $(1)_OBJS += $$(filter %.o,$$($(1)_SOURCES:%.cc=%.o))
134 $(1)_OBJS += $$(filter %.o,$$($(1)_SOURCES:%.cxx=%.o))
135 $(1)_OBJS += $$(filter %.o,$$($(1)_SOURCES:%.S=%.o))
136 $(1)_OBJS := $$(sort $$($(1)_OBJS))
137
138 SOURCES += $$($(1)_SOURCES)
139
140 $(USER_LIB_DIR)/lib$(1).a: $$($(1)_OBJS)
141         @$(QUIET_CMD_ECHO) "  AR      $$@"
142         $(Q) $(AR) rcs $$@ $$^
143 endef
144
145
146 # Syntax: $(call SOLIB_template,<library-name>)
147 define SOLIB_template
148 $(1)_OBJSLO += $$(filter %.lo,$$($(1)_SOURCES:%.c=%.lo))
149 $(1)_OBJSLO += $$(filter %.lo,$$($(1)_SOURCES:%.cc=%.lo))
150 $(1)_OBJSLO += $$(filter %.lo,$$($(1)_SOURCES:%.cxx=%.lo))
151 $(1)_OBJSLO += $$(filter %.lo,$$($(1)_SOURCES:%.S=%.lo))
152 $(1)_OBJSLO := $$(sort $$($(1)_OBJSLO))
153
154 SOLIB_OBJS  += $$($(1)_OBJSLO)
155 SOLIB_SOURCES += $$($(1)_SOURCES)
156
157 $(USER_LIB_DIR)/lib$(1).so: $$($(1)_OBJSLO)
158         @$(QUIET_CMD_ECHO) "  LINK    $$@"
159         $(Q) $(LD) --shared --soname=lib$(1).so -o $$@ $$^
160 endef
161
162
163 # lib_obj_SOURCES handling
164 lib_OBJS = $(addsuffix .o,$(basename $(lib_obj_SOURCES)))
165 SOURCES += $(filter-out %.o,$(lib_obj_SOURCES))
166 #$(warning lib_OBJS = $(lib_OBJS))
167
168 # # Syntax: $(call LIBOBJ_template,<object-name>)
169 # define LIBOBJ_template
170 # $(USER_LIB_DIR)/$(1): $(1)
171 #       @cp $(CP_FLAGS) $$< $$@
172 # endef
173 $(USER_LIB_DIR)/%.o: %.o
174         @cp $(CP_FLAGS) $< $@
175