Newer
Older
build_scripts / build.mk
ifndef OUTDIR
OUTDIR = bin
endif


ifndef OBJDIR
OBJDIR = $(OUTDIR)/obj
endif


#definition of various tools
AWK = awk
CAT = cat
CD = cd
CP = cp
CPIO = cpio
ECHO = echo
MV = mv
RM = rm
LS = ls
SED = sed
MKDIR = mkdir
CHMOD = chmod
SREC_CAT = srec_cat
BIN2C = bin2c
INSTALL = install
TAR = tar


ifeq ($(RELEASE), yes)

$(info *******************************  Release build  *******************************)

else

$(info *******************************  Debug build  *******************************)

endif


#update CFLAGS and LDFLAGS
CFLAGS += $(PLATFORM_CFLAGS) $(ARCH_CFLAGS) $(OPT_CFLAGS) -O$(OPTLEVEL) -g$(DBGLEVEL) $(addprefix -I, $(INCDIRS)) $(addprefix -D, $(CPPDEFS))
CXXFLAGS += $(PLATFORM_CXXFLAGS) $(ARCH_CFLAGS) $(OPT_CFLAGS) -O$(OPTLEVEL) -g$(DBGLEVEL) $(addprefix -I, $(INCDIRS)) $(addprefix -D, $(CPPDEFS))
LDFLAGS += $(PLATFORM_LDFLAGS) $(ARCH_CFLAGS) $(OPT_CFLAGS) -O$(OPTLEVEL) -g$(DBGLEVEL) -Wl,-Map=$(OUTDIR)/$(TARGET).map,--gc-sections,--cref

#create list of all objects
ALLOBJS = $(addprefix $(OBJDIR)/, $(patsubst %.s, %.o, $(notdir $(ASRCS)))) $(addprefix $(OBJDIR)/, $(patsubst %.c, %.o, $(notdir $(CSRCS)))) $(addprefix $(OBJDIR)/, $(patsubst %.cpp, %.o, $(notdir $(CXXSRCS))))

#create list of all object directories
NEWDIRS += $(sort $(dir $(ALLOBJS)))


ifdef BUILD_VERSION

BUILD_VERSION_BCD = 0x$(subst .,,$(BUILD_VERSION))

CFLAGS += -D_BUILD_VERSION_=\""v$(BUILD_VERSION)"\"
CXXFLAGS += -D_BUILD_VERSION_=\""v$(BUILD_VERSION)"\"
CFLAGS += -D_BUILD_VERSION_BCD_=$(BUILD_VERSION_BCD)
CXXFLAGS += -D_BUILD_VERSION_BCD_=$(BUILD_VERSION_BCD)

TARGET_VER = $(addsuffix _v$(BUILD_VERSION), $(TARGET))

else

TARGET_VER = $(TARGET)
BUILD_VERSION_BCD = 0xFFFF

endif


ifeq ($(VERBOSE), yes)
	PREFIX :=
	EMPTY_LINE := @$(ECHO);
else
	PREFIX := @
	EMPTY_LINE :=	
endif


#(1) = target
#(2) = source(s)
#(3) = extra flags
define COMPILE_ASM_SOURCE
$(1): $(MAKEFILE_LIST) $(2) | $(dir $(1))
	@$(ECHO) Compiling $(2) ...;
	$(PREFIX)$(CC) -c $(3) -o $(1) $(2);
	$(EMPTY_LINE)
endef



#(1) = target
#(2) = source(s)
#(3) = extra flags
define COMPILE_C_SOURCE
$(1): $(MAKEFILE_LIST) $(2) | $(dir $(1))
	@$(ECHO) Compiling $(2) ...;
	$(PREFIX)$(CC) -c $(3) -MD -pipe -o $(1) $(2);
	$(EMPTY_LINE)
	@$(CP) -R $(patsubst %.o, %.d, $(1)) $(patsubst %.o, %.dtemp, $(1));
	@$(SED) -e 's/#.*//' -e 's/^[^:]*: *//' -e 's/ *\\$$$$//' -e '/^$$$$/ d' -e 's/$$$$/ :/' <$(patsubst %.o,%.d,$(1)) >>$(patsubst %.o,%.dtemp,$(1));
	@$(MV) $(patsubst %.o, %.dtemp, $(1)) $(patsubst %.o, %.d, $(1));
endef


#(1) = target
#(2) = source(s)
#(3) = extra flags
define COMPILE_CXX_SOURCE
$(1): $(MAKEFILE_LIST) $(2) | $(dir $(1))
	@$(ECHO) Compiling $(2) ...;
	$(PREFIX)$(CC) -c $(3) -MD -pipe -o $(1) $(2);
	$(EMPTY_LINE)
	@$(CP) -R $(patsubst %.o, %.d, $(1)) $(patsubst %.o, %.dtemp, $(1));
	@$(SED) -e 's/#.*//' -e 's/^[^:]*: *//' -e 's/ *\\$$$$//' -e '/^$$$$/ d' -e 's/$$$$/ :/' <$(patsubst %.o,%.d,$(1)) >>$(patsubst %.o,%.dtemp,$(1));
	@$(MV) $(patsubst %.o, %.dtemp, $(1)) $(patsubst %.o, %.d, $(1));
endef


define CREATE_DIRECTORY
$(1)/:
	@$(ECHO) Creating directory $(1) ...
	@$(MKDIR) -p $(1)
endef

.PHONY: all clean clean_first user_target user_clean_target

all: user_target

clean: clean_first user_clean_target

clean_first:
	@$(ECHO) Cleaning application files ...
	-$(RM) -rf $(OUTDIR) $(OBJDIR)


$(OUTDIR)/$(TARGET_VER).elf: $(MAKEFILE_LIST) $(ALLOBJS) $(COMPONENT_LIBS) $(LIBS) $(LINKER_SCRIPT) | $(OUTDIR)/
	@$(ECHO) "Linking ..."
ifdef LINKER_SCRIPT
	$(CC) -o $(OUTDIR)/$(TARGET_VER).elf -T$(LINKER_SCRIPT) $(ALLOBJS) $(COMPONENT_LIBS) $(LIBS) $(LDFLAGS)
else
	$(CC) -o $(OUTDIR)/$(TARGET_VER).elf $(ALLOBJS) $(LIBS) $(LDFLAGS)
endif
ifeq ($(GEN_SYM), yes)
	$(NM) -t d -S --size-sort $(OUTDIR)/$(TARGET_VER).elf > $(OUTDIR)/$(TARGET_VER).sym
endif
ifeq ($(GEN_LST), yes)
	$(OBJDUMP) -xds -S -l $(OUTDIR)/$(TARGET_VER).elf > $(OUTDIR)/$(TARGET_VER).lst
endif
ifeq ($(STRIP_ELF), yes)
ifeq ($(RELEASE), yes)
	$(STRIP) --strip-debug --strip-unneeded $(OUTDIR)/$(TARGET_VER).elf
endif
endif
ifeq ($(GEN_SIZE), yes)
	@$(SIZE) -A -d $(OUTDIR)/$(TARGET_VER).elf
endif
	$(EMPTY_LINE)


$(OUTDIR)/$(TARGET_VER).a: $(MAKEFILE_LIST) $(ALLOBJS) | $(OUTDIR)/
	@$(ECHO) "Creating static library ..."
	$(AR) rcs $(OUTDIR)/$(TARGET_VER).a $(ALLOBJS)
ifeq ($(GEN_SYM), yes)
	$(NM) -t d -S --size-sort $(OUTDIR)/$(TARGET_VER).a > $(OUTDIR)/$(TARGET_VER)_a.sym
endif
ifeq ($(GEN_LST), yes)
	$(OBJDUMP) -xds -S -l $(OUTDIR)/$(TARGET_VER).a > $(OUTDIR)/$(TARGET_VER)_a.lst
endif
ifeq ($(RELEASE), yes)
	$(STRIP) --strip-debug --strip-unneeded $(OUTDIR)/$(TARGET_VER).a
endif
	@$(ECHO)


$(OUTDIR)/$(TARGET_VER).so: $(MAKEFILE_LIST) $(ALLOBJS) | $(OUTDIR)/
	@$(ECHO) "Creating shared library ..."
ifdef LINKER_SCRIPT
	$(CC) -o $(OUTDIR)/$(TARGET_VER).so -T$(LINKER_SCRIPT) $(ALLOBJS) $(COMPONENT_LIBS) $(LIBS) $(LDFLAGS) -rdynamic -shared -Wl,-soname,$(TARGET).so
else
	$(CC) -o $(OUTDIR)/$(TARGET_VER).so $(ALLOBJS) $(LIBS) $(LDFLAGS) -rdynamic -shared -Wl,-soname,$(TARGET).so
endif
ifeq ($(GEN_SYM), yes)
	$(NM) -t d -S --size-sort $(OUTDIR)/$(TARGET_VER).so > $(OUTDIR)/$(TARGET_VER)_so.sym
endif
ifeq ($(GEN_LST), yes)
	$(OBJDUMP) -xds -S -l $(OUTDIR)/$(TARGET_VER).so > $(OUTDIR)/$(TARGET_VER)_so.lst
endif
ifeq ($(RELEASE), yes)
	$(STRIP) --strip-debug --strip-unneeded $(OUTDIR)/$(TARGET_VER).so
endif
	@$(ECHO)


$(foreach dir, $(OBJDIR), $(eval $(call CREATE_DIRECTORY, $(dir))))
$(eval $(call CREATE_DIRECTORY, $(OUTDIR)))

$(foreach source, $(ASRCS), $(eval $(call COMPILE_ASM_SOURCE, $(OBJDIR)/$(notdir $(source:%.s=%.o)), $(source), $(CFLAGS))))
$(foreach source, $(CSRCS), $(eval $(call COMPILE_C_SOURCE, $(OBJDIR)/$(notdir $(source:%.c=%.o)), $(source), $(CFLAGS))))
$(foreach source, $(CXXSRCS), $(eval $(call COMPILE_CXX_SOURCE, $(OBJDIR)/$(notdir $(source:%.cpp=%.o)), $(source), $(CXXFLAGS))))

-include $(ALLOBJS:%.o=%.d)