# Rules for compilation of C, C++ and assembler sources using GNU # toolchain. # Interface to other rules: # Input variables: # LIB_DIR - directory where compiled libraries are stored # OBJS_DIR - directory where intermediate files (.o, .map, ...) are stored # INCLUDE_DIR - where includes can be found # from config.omk or Makefile.omk # CROSS_COMPILE - # TARGET_ARCH, DEBUG, OPTIMIZE, DEFS - forms CFLAGS # from base: SOURCES_DIR # from Makefile.omk: lib_LOADLIBES # Output variables: # SOURCES - all the source files that needs to be compiled (except for shared library sources) # SOLIB_SOURCES - all the source files that needs to be compiled for a shared library # OBJ_EXT - extension of object files # LIB_EXT - extension of library files # LIB_PREF - prefix for library files # ASM_EXT - extension of assembler sources # Templates: # COMPILER_DEFS_template - definitions that should be defined before # the following templates can be used. The input variables needs to be # defined before evaluating this template # COMPILE_c_o_template, COMPILE_cc_o_template, COMPILE_S_o_template - # templates that create rules for compilation of sources # CMETRIC_o_h_template - FIXME # PROGRAM_template, LIBRARY_template, SOLIB_template - templates that # create rules for compilation of a program, library and shared # library. The rules can use rules produced by COMPILE_xxx_template. define COMPILER_DEFS_template OBJ_EXT = .o LIB_EXT = .a LIB_PREF = lib ASM_EXT = .S CC = $(CROSS_COMPILE)gcc LINK = $(CROSS_COMPILE)ld AR = $(CROSS_COMPILE)ar OBJCOPY = $(CROSS_COMPILE)objcopy NM = $(CROSS_COMPILE)nm CFLAGS += $(TARGET_ARCH) $(DEBUG) $(OPTIMIZE) CFLAGS += -Wall CFLAGS += -I$(SOURCES_DIR) CFLAGS += -I$(INCLUDE_DIR) LOADLIBES += -L$(LIB_DIR) LOADLIBES += $(lib_LOADLIBES:%=-l%) -include $(OBJS_DIR)/*.d #%.lo: %.c # $(CC) -o $@ $(LCFLAGS) -c $< c_o_COMPILE = $$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) \ $(CPPFLAGS) $(AM_CFLAGS) $$(CFLAGS) cc_o_COMPILE = $$(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) \ $(CPPFLAGS) $(AM_CFLAGS) $$(CFLAGS) S_o_COMPILE = $$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) \ $(CPPFLAGS) $(AM_CFLAGS) $$(CFLAGS) $(ASFLAGS) # Check GCC version for user build ifndef CC_MAJOR_VERSION CC_MAJOR_VERSION = $$(shell $$(CC) -dumpversion | sed -e 's/\([^.]\)\..*/\1/') endif # Prepare suitable define for dependency building ifeq ($$(CC_MAJOR_VERSION),2) CC_DEPFLAGS = -Wp,-MD,"$$@.d.tmp" else CC_DEPFLAGS = -MT $$@ -MD -MP -MF "$$@.d.tmp" endif endef # COMPILER_DEFS_template # Syntax: $(call COMPILE_c_o_template,,,) define COMPILE_c_o_template $(2): $(1) $$(GEN_HEADERS) @$(QUIET_CMD_ECHO) " CC $$@" $(Q) if $$(c_o_COMPILE) $$(CC_DEPFLAGS) $(3) -o $$@ -c $$< ; \ then mv -f "$$@.d.tmp" "$$@.d" ; \ else rm -f "$$@.d.tmp" ; exit 1; \ fi endef # Syntax: $(call COMPILE_cc_o_template,,,) define COMPILE_cc_o_template $(2): $(1) $$(GEN_HEADERS) @$(QUIET_CMD_ECHO) " CXX $$@" $(Q) if $$(cc_o_COMPILE) $$(CC_DEPFLAGS) $(3) -o $$@ -c $$< ; \ then mv -f "$$@.d.tmp" "$$@.d" ; \ else rm -f "$$@.d.tmp" ; exit 1; \ fi endef # Syntax: $(call COMPILE_S_o_template,,,) define COMPILE_S_o_template $(2): $(1) $$(GEN_HEADERS) @$(QUIET_CMD_ECHO) " AS $$@" $(Q) if $$(S_o_COMPILE) -D__ASSEMBLY__ $$(CC_DEPFLAGS) $(3) -o $$@ -c $$< ; \ then if [ -e "$$@.d.tmp" ] ; then mv -f "$$@.d.tmp" "$$@.d" ; fi ; \ else rm -f "$$@.d.tmp" ; exit 1; \ fi endef # Syntax: $(call CMETRIC_o_h_template,,) define CMETRIC_o_h_template $(2): $(1) @$(QUIET_CMD_ECHO) " CMETRIC $$@" $(Q)if [ -n `dirname $$@` ] ; then \ if [ ! -e `dirname $$@` ] ; then \ mkdir -p `dirname $$@` ; fi ; fi $(Q)echo >$$@ '/* Automatically generated from $$< */' $(Q)echo >>$$@ '/* Conditionals to control compilation */' $(Q)set -o pipefail ; $(NM) $$< \ | sed -n 's/^ *0*\(0[0-9A-Fa-f]*\) *A *_cmetric2cond_\([A-Za-z_0-9]*\) */#define \2 0x\1/p' \ | sort >>$$@ $(Q)echo >>$$@ '/* Defines from the values defined to symbols */' $(Q)set -o pipefail ; $(NM) $$< \ | sed -n 's/^ *0*\(0[0-9A-Fa-f]*\) *A *_cmetric2def_\([A-Za-z_0-9]*\) */#define \2 0x\1/p' \ | sort >>$$@ endef # Syntax: $(call PROGRAM_template,,,) define PROGRAM_template $(1)_OBJS += $$(filter %.o,$$($(1)_SOURCES:%.c=%.o)) $(1)_OBJS += $$(filter %.o,$$($(1)_SOURCES:%.cc=%.o)) $(1)_OBJS += $$(filter %.o,$$($(1)_SOURCES:%.cxx=%.o)) $(1)_OBJS += $$(filter %.o,$$($(1)_SOURCES:%.S=%.o)) $(1)_OBJS += $$(filter %.o,$(1)_SOURCES) $(1)_OBJS := $$(sort $$($(1)_OBJS)) SOURCES += $$($(1)_SOURCES) ifneq ($(LD_SCRIPT),) $(1)$(3:%=-%)_LDFLAGS = -Wl,-T,$(LD_SCRIPT).ld$(3:%=-%) endif $(2)/$(1)$(3:%=-%): $$($(1)_OBJS) @$(QUIET_CMD_ECHO) " LINK $$@" $(Q) $$(shell if [ -z "$$(filter %.cc,$$($(1)_SOURCES))" ] ; \ then echo $$(CC) $$(CPPFLAGS) $$(AM_CPPFLAGS) $$(AM_CFLAGS) $$(CFLAGS) ; \ else echo $$(CXX) $$(CPPFLAGS) $$(AM_CPPFLAGS) $$(AM_CXXFLAGS) $$(CXXFLAGS) ; fi) \ $$(AM_LDFLAGS) $$(LDFLAGS) $$($(1)$(3:%=-%)_LDFLAGS) -Wl,-Map,$(1)$(3:%=-%).map \ $$($(1)_OBJS) $$(LOADLIBES) $$($(1)_MOREOBJS) $$($(1)_LIBS:%=-l%) \ -o $$@ @echo "$(2)/$(1)$(3:%=-%): \\" >$(OBJS_DIR)/$(1)$(3:%=-%).exe.d @if [ -n "$(LD_SCRIPT)" ]; then \ echo " $(LIB_DIR)/$(LD_SCRIPT).ld$(3:%=-%) \\" >>$(OBJS_DIR)/$(1)$(3:%=-%).exe.d; fi @sed -n -e 's|^LOAD \(.*\)$$$$| \1 \&|p' $(OBJS_DIR)/$(1)$(3:%=-%).map|tr '&' '\134' >>$(OBJS_DIR)/$(1)$(3:%=-%).exe.d @echo >>$(OBJS_DIR)/$(1).exe.d endef # Rules for other output formats (can be specified by OUTPUT_FORMATS) %.bin: % @$(QUIET_CMD_ECHO) " OBJCOPY $@" $(Q) $(OBJCOPY) --output-target=binary -S $< $@ %.hex: % @$(QUIET_CMD_ECHO) " OBJCOPY $@" $(Q) $(OBJCOPY) --output-target=ihex -S $< $@ %.srec: % @$(QUIET_CMD_ECHO) " OBJCOPY $@" $(Q) $(OBJCOPY) --output-target=srec -S $< $@ # Syntax: $(call LIBRARY_template,) define LIBRARY_template $(1)_OBJS += $$(filter %.o,$$($(1)_SOURCES:%.c=%.o)) $(1)_OBJS += $$(filter %.o,$$($(1)_SOURCES:%.cc=%.o)) $(1)_OBJS += $$(filter %.o,$$($(1)_SOURCES:%.cxx=%.o)) $(1)_OBJS += $$(filter %.o,$$($(1)_SOURCES:%.S=%.o)) $(1)_OBJS := $$(sort $$($(1)_OBJS)) SOURCES += $$($(1)_SOURCES) $(LIB_DIR)/lib$(1).a: $$($(1)_OBJS) @$(QUIET_CMD_ECHO) " AR $$@" $(Q) $(AR) rcs $$@ $$^ endef # Syntax: $(call SOLIB_template,) define SOLIB_template $(1)_OBJSLO += $$(filter %.lo,$$($(1)_SOURCES:%.c=%.lo)) $(1)_OBJSLO += $$(filter %.lo,$$($(1)_SOURCES:%.cc=%.lo)) $(1)_OBJSLO += $$(filter %.lo,$$($(1)_SOURCES:%.cxx=%.lo)) $(1)_OBJSLO += $$(filter %.lo,$$($(1)_SOURCES:%.S=%.lo)) $(1)_OBJSLO := $$(sort $$($(1)_OBJSLO)) SOLIB_OBJS += $$($(1)_OBJSLO) SOLIB_SOURCES += $$($(1)_SOURCES) $(LIB_DIR)/lib$(1).so: $$($(1)_OBJSLO) @$(QUIET_CMD_ECHO) " LINK $$@" $(Q) $(LD) --shared --soname=lib$(1).so -o $$@ $$^ endef # Local Variables: # mode:makefile # End: