Skip to content
Snippets Groups Projects
Makefile 6.63 KiB
Newer Older
# Copyright 2016-2020
# 
# Timo Kortbrae,
# Jan Heitmann,
# Bernd-Christian Renner, and
# Hamburg University of Technology (TUHH).
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# 3. Neither the name of the copyright holder nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# GENERAL SETUP
#

# program setup
PLATFORM        ?= arm-none-eabi-
CC              := $(PLATFORM)gcc
LD              := $(PLATFORM)gcc
NM              := $(PLATFORM)nm
OBJCOPY         := $(PLATFORM)objcopy
OBJDUMP         := $(PLATFORM)objdump
SIZE            := $(PLATFORM)size

RM              := rm
RMDIR           := rmdir -p --ignore-fail-on-non-empty

PROGRAM         := st-flash

# Strings for beautifying output
MSG_CLEAN_FILES         = "RM      *.o *.d"
MSG_CLEAN_DIRS          = "RMDIR   $(strip $(clean-dirs))"
MSG_MKDIR               = "MKDIR   $(dir $@)"

MSG_BINARY_IMAGE        = "OBJCOPY $@"
MSG_COMPILING           = "CC      $@"
MSG_EXTENDED_LISTING    = "OBJDUMP $@"
MSG_IHEX_IMAGE          = "OBJCOPY $@"
MSG_LINKING             = "LN      $@"
MSG_SIZE                = "SIZE    $@"
MSG_SYMBOL_TABLE        = "NM      $@"

MSG_PROGRAMMING         = "ST-LINK program $(TARGET)"
# reverse order of a list
reverse = $(if $(1),$(call reverse,$(wordlist 2,$(words $(1)),$(1)))) $(firstword $(1))

# flags
cflags            := $(CFLAGS)
libflags-gnu-y    := $(foreach LIB,$(LIBS),-l$(LIB))
ldflags           := $(LDFLAGS)

# Use pipes instead of temporary files for communication between processes
cflags    += -pipe
ldflags   += -pipe

# warnings
cflags    += -Wall -Wstrict-prototypes -Wmissing-prototypes -Werror-implicit-function-declaration

# void pointer arithmetic warning
cflags    += -Wpointer-arith

# preprocessor flags
cflags    += $(foreach INC,$(addprefix $(PRJ_PATH)/,$(INC_PATH)),-I$(INC))

# dep file flags
depflags   = -MD -MP -MQ $@

# GNU standard
cflags    += -std=gnu99

# use unsigned char by default
cflags    += -funsigned-char

# no strict aliasing
cflags    += -fno-strict-aliasing

# allow garbage collection for unused sections
cflags    += -ffunction-sections -fdata-sections

# remove unreferred sections when linking
ldflags   += -Wl,--gc-sections

# output a link map file and a cross reference table
ldflags   += -Wl,-Map,"$(BUILD_DIR)/$(PROJECT).map",--cref

# add library search paths relative to top level directory
ldflags   += $(foreach _LIB_PATH,$(addprefix $(PRJ_PATH)/,$(LIB_PATH)),-L$(_LIB_PATH))

# finally put it all together for the linker and compiler
cc_flags  = $(depflags) $(OPTIMIZATION) $(cflags)
ld_flags  = $(OPTIMIZATION) $(ldflags)

# force trailing slash (/) in dir name
build-dir       := $(dir $(BUILD_DIR))$(if $(notdir $(BUILD_DIR)),$(notdir $(BUILD_DIR))/)

# assemble object and dependency file list from source file list
obj-files       := $(addprefix $(build-dir), $(addsuffix .o,$(basename $(CSRCS) $(ASSRCS))))
dep-files       := $(wildcard $(foreach f,$(obj-files),$(basename $(f)).d))

# files and dirs to be cleaned
clean-files     :=
clean-files     += $(wildcard $(BUILD_DIR)/$(TARGET) $(BUILD_DIR)/$(PROJECT).map)
clean-files     += $(wildcard $(BUILD_DIR)/$(PROJECT).hex $(BUILD_DIR)/$(PROJECT).bin)
clean-files     += $(wildcard $(BUILD_DIR)/$(PROJECT).lss $(BUILD_DIR)/$(PROJECT).sym)
clean-files     += $(wildcard $(obj-files)) $(dep-files)

clean-dirs      :=
clean-dirs      += $(call reverse,$(sort $(wildcard $(dir $(obj-files)))))



#
# ACTUAL BUILD PROCESS
#

# default
.PHONY: all
all: $(BUILD_DIR)/$(TARGET) $(BUILD_DIR)/$(PROJECT).lss $(BUILD_DIR)/$(PROJECT).sym $(BUILD_DIR)/$(PROJECT).hex $(BUILD_DIR)/$(PROJECT).bin

# clean up
.PHONY: clean
clean:
	@$(if $(strip $(clean-files)),echo $(MSG_CLEAN_FILES))
	$(if $(strip $(clean-files)),$(Q)$(RM) $(clean-files),)
	@$(if $(strip $(clean-dirs)),echo $(MSG_CLEAN_DIRS))
	$(Q)$(if $(strip $(clean-dirs)),                        \
		for directory in $(strip $(clean-dirs)); do     \
			if [ -d "$$directory" ]; then           \
				$(RMDIR) $$directory;           \
			fi                                      \
		done                                            \
	)

.PHONY: objfiles
# build object files from source files
$(build-dir)%.o: %.c $(MAKEFILE_PATH) $(MAKEFILE_CONF)
	$(Q)test -d $(dir $@) || echo $(MSG_MKDIR)
	$(Q)test -d $(dir $@) || mkdir -p $(dir $@)
	@echo $(MSG_COMPILING)
include $(dep-files)

# create ELF from object files
$(BUILD_DIR)/$(TARGET): $(PRJ_PATH)/$(LINKER_SCRIPT) $(MAKEFILE_PATH) $(MAKEFILE_CONF) $(obj-files)
	@echo $(MSG_LINKING)
	$(Q)$(LD) $(ld_flags) $(obj-files) -Wl,--start-group $(libflags-gnu-y) -Wl,--end-group -o $@
	@echo $(MSG_SIZE)
	$(Q)$(SIZE) -Ax $@
	$(Q)$(SIZE) -Bx $@

# build extended function listing
%.lss: $(BUILD_DIR)/$(TARGET)
	@echo $(MSG_EXTENDED_LISTING)
	$(Q)$(OBJDUMP) -h -S $< > $@

# build symbol table
%.sym: $(BUILD_DIR)/$(TARGET)
	@echo $(MSG_SYMBOL_TABLE)
	$(Q)$(NM) -n $< > $@

	@echo $(MSG_IHEX_IMAGE)
	$(Q)$(OBJCOPY) -O ihex $< $@

	@echo $(MSG_BINARY_IMAGE)
	$(Q)$(OBJCOPY) -O binary $< $@

.PHONY: program
program:
	@echo $(MSG_PROGRAMMING)
	$(Q)$(PROGRAM) write $(BUILD_DIR)/$(PROJECT).bin $(FLASH)