# 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 # 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 SOURCES2OBJS = .o/.c .o/.cc .o/.cxx .o/.S .o/.o SOURCES2OBJSLO = .lo/.c .lo/.cc .lo/.cxx .lo/.S .lo/.lo #%.lo: %.c # $(CC) -o $@ $(LCFLAGS) -c $< c_o_COMPILE = $$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(OMK_CPPFLAGS) \ $(CPPFLAGS) $(OMK_CFLAGS) $$(CFLAGS) cc_o_COMPILE = $$(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(OMK_CPPFLAGS) \ $(CPPFLAGS) $(CXXFLAGS) $(OMK_CFLAGS) $$(CFLAGS) S_o_COMPILE = $$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) \ $(CPPFLAGS) $(OMK_CFLAGS) $$(CFLAGS) $(ASFLAGS) # Check GCC version for user build ifndef CC_MAJOR_VERSION CC_MAJOR_VERSION = $$(shell $$(CC) -dumpversion | $(SED4OMK) -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 PROGRAM_template,,,) define PROGRAM_template GEN_SOURCES += $$($(1)_GEN_SOURCES) $(foreach x, $(SOURCES2OBJS), $(1)_OBJS += $$(patsubst %$(notdir $(x)),%$(dir $(x)),$$(filter %$(notdir $(x)),\ $$($(1)_SOURCES) $$($(1)_GEN_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) $$(OMK_CPPFLAGS) $$(OMK_CFLAGS) $$(CFLAGS) ; \ else echo $$(CXX) $$(CPPFLAGS) $$(OMK_CPPFLAGS) $$(OMK_CXXFLAGS) $$(CXXFLAGS) ; fi) \ $$(OMK_LDFLAGS) $$(LDFLAGS) $$($(1)$(3:%=-%)_LDFLAGS) -Wl,-Map,$(1)$(3:%=-%).map \ $$($(1)_OBJS) $$($(1)_MOREOBJS) $$(LOADLIBES) $$($(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 @$(SED4OMK) -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 GEN_SOURCES += $$($(1)_GEN_SOURCES) $(foreach x, $(SOURCES2OBJS), $(1)_OBJS += $$(patsubst %$(notdir $(x)),%$(dir $(x)),$$(filter %$(notdir $(x)),\ $$($(1)_SOURCES) $$($(1)_GEN_SOURCES))) ) $(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 SOLIB_GEN_SOURCES += $$($(1)_GEN_SOURCES) $(foreach x, $(SOURCES2OBJSLO), $(1)_OBJSLO += $$(patsubst %$(notdir $(x)),%$(dir $(x)),$$(filter %$(notdir $(x)),\ $$($(1)_SOURCES) $$($(1)_GEN_SOURCES))) ) $(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