Newer
Older

Bernd-Christian Renner
committed
# 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

Bernd-Christian Renner
committed
# modification, are permitted provided that the following conditions
# are met:

Bernd-Christian Renner
committed
# 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.

Bernd-Christian Renner
committed
# 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.

Bernd-Christian Renner
committed
# 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.

Bernd-Christian Renner
committed
# imports from config.mk
# - TARGET
# - PROJECT

Bernd-Christian Renner
committed
# 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 $@"

Bernd-Christian Renner
committed
MSG_PROGRAMMING = "ST-LINK program $(TARGET)"

Bernd-Christian Renner
committed
# reverse order of a list
reverse = $(if $(1),$(call reverse,$(wordlist 2,$(words $(1)),$(1)))) $(firstword $(1))

Bernd-Christian Renner
committed
# verbose: make Q empty (@otherwise)
#Q =
Q = @

Bernd-Christian Renner
committed
#
# FLAG CONFIG

Bernd-Christian Renner
committed
# flags
cflags := $(CFLAGS)
libflags-gnu-y := $(foreach LIB,$(LIBS),-l$(LIB))
ldflags := $(LDFLAGS)
# Use pipes instead of temporary files for communication between processes

Bernd-Christian Renner
committed
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
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

Bernd-Christian Renner
committed
ldflags += -Wl,-Map,"$(BUILD_DIR)/$(PROJECT).map",--cref

Bernd-Christian Renner
committed
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# 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

Bernd-Christian Renner
committed
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

Bernd-Christian Renner
committed
objfiles: $(obj-files)

Bernd-Christian Renner
committed
# 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)

Bernd-Christian Renner
committed
$(Q)$(CC) $(cc_flags) -c $< -o $@

Bernd-Christian Renner
committed
# add depedency to all header files in use

Bernd-Christian Renner
committed
# create ELF from object files
$(BUILD_DIR)/$(TARGET): $(PRJ_PATH)/$(LINKER_SCRIPT) $(MAKEFILE_PATH) $(MAKEFILE_CONF) $(obj-files)

Bernd-Christian Renner
committed
$(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 $@

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

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

Bernd-Christian Renner
committed
# build intel hex
%.hex: $(BUILD_DIR)/$(TARGET)
@echo $(MSG_IHEX_IMAGE)
$(Q)$(OBJCOPY) -O ihex $< $@

Bernd-Christian Renner
committed
# build binary
%.bin: $(BUILD_DIR)/$(TARGET)
@echo $(MSG_BINARY_IMAGE)
$(Q)$(OBJCOPY) -O binary $< $@
.PHONY: program
program:
@echo $(MSG_PROGRAMMING)

Bernd-Christian Renner
committed
$(Q)$(PROGRAM) write $(BUILD_DIR)/$(PROJECT).bin $(FLASH)