FRDM K64F Metronome

Committer:
ram54288
Date:
Sun May 14 18:37:05 2017 +0000
Revision:
0:dbad57390bd1
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ram54288 0:dbad57390bd1 1 #
ram54288 0:dbad57390bd1 2 # Toolchain rules for building libraries
ram54288 0:dbad57390bd1 3 #
ram54288 0:dbad57390bd1 4 # Support multi-platform builds
ram54288 0:dbad57390bd1 5 # You may define PLATFORM=.. prefix for compiler
ram54288 0:dbad57390bd1 6 # if PLATFORM is defined it will owerride CC and AR
ram54288 0:dbad57390bd1 7 # You may also specify CC and AR separately.
ram54288 0:dbad57390bd1 8 # example: Compile with KEIL
ram54288 0:dbad57390bd1 9 # make CC=ArmCC AR=ArmAR
ram54288 0:dbad57390bd1 10 #
ram54288 0:dbad57390bd1 11 # example: Cross Compile with Gnu toolchain, for embedded Linux
ram54288 0:dbad57390bd1 12 # make PLATFORM=arm-linux-gnueabi-
ram54288 0:dbad57390bd1 13 #
ram54288 0:dbad57390bd1 14 # Special rules for IAR-ARM toolchains added, AR tool on that does not follow common arquements.
ram54288 0:dbad57390bd1 15 # make CC=iccarm uses iarchive.exe for creating a archive.
ram54288 0:dbad57390bd1 16 #
ram54288 0:dbad57390bd1 17 # Special rules for IAR-RL78 toolchain added, uses own AR and compiler requires specific flags
ram54288 0:dbad57390bd1 18 # make CC=iccrl78
ram54288 0:dbad57390bd1 19 #
ram54288 0:dbad57390bd1 20 # When using ArmCC, Select CPU by defining one of following supported values
ram54288 0:dbad57390bd1 21 # CPU=Cortex-M0
ram54288 0:dbad57390bd1 22 # CPU=Cortex-M3
ram54288 0:dbad57390bd1 23 # CPU=Cortex-M4
ram54288 0:dbad57390bd1 24 #
ram54288 0:dbad57390bd1 25 # example:
ram54288 0:dbad57390bd1 26 # make CC=armcc CPU=Cortex-M4
ram54288 0:dbad57390bd1 27
ram54288 0:dbad57390bd1 28 #
ram54288 0:dbad57390bd1 29 # If PLATFORM prefix is defined,
ram54288 0:dbad57390bd1 30 # generate CC and AR accordingly
ram54288 0:dbad57390bd1 31 #
ram54288 0:dbad57390bd1 32 ifneq ($(strip $(PLATFORM)),)
ram54288 0:dbad57390bd1 33 CC:=$(PLATFORM)gcc
ram54288 0:dbad57390bd1 34 AR:=$(PLATFORM)ar
ram54288 0:dbad57390bd1 35 endif
ram54288 0:dbad57390bd1 36
ram54288 0:dbad57390bd1 37 #
ram54288 0:dbad57390bd1 38 # Windows specific tweaks for echo and mkdir to work
ram54288 0:dbad57390bd1 39 #
ram54288 0:dbad57390bd1 40 ifeq ($(OS),Windows_NT)
ram54288 0:dbad57390bd1 41 SHELL := cmd.exe
ram54288 0:dbad57390bd1 42 mkdir = if not exist $(1) md $(subst /,\,$(1))
ram54288 0:dbad57390bd1 43 else
ram54288 0:dbad57390bd1 44 mkdir = mkdir -p $(1)
ram54288 0:dbad57390bd1 45 endif
ram54288 0:dbad57390bd1 46
ram54288 0:dbad57390bd1 47 #
ram54288 0:dbad57390bd1 48 # Append CC and CPU to library names
ram54288 0:dbad57390bd1 49 #
ram54288 0:dbad57390bd1 50 LIB:=$(LIB:%.a=%_$(CC).a)
ram54288 0:dbad57390bd1 51 ifneq (,$(CPU))
ram54288 0:dbad57390bd1 52 LIB:=$(LIB:%.a=%_$(CPU).a)
ram54288 0:dbad57390bd1 53 endif
ram54288 0:dbad57390bd1 54
ram54288 0:dbad57390bd1 55 ##########################################################
ram54288 0:dbad57390bd1 56 # TOOLCHAIN SPECIFIC RULES AND FLAGS #
ram54288 0:dbad57390bd1 57 ##########################################################
ram54288 0:dbad57390bd1 58
ram54288 0:dbad57390bd1 59 #
ram54288 0:dbad57390bd1 60 # GCC toolchains
ram54288 0:dbad57390bd1 61 #
ram54288 0:dbad57390bd1 62 ifneq (,$(findstring gcc,$(CC)))
ram54288 0:dbad57390bd1 63 # Flags for common toolchain, usually GCC or CC
ram54288 0:dbad57390bd1 64 AROPTS=-rsc $@ $^
ram54288 0:dbad57390bd1 65 override CFLAGS += -Wall -pedantic-errors
ram54288 0:dbad57390bd1 66 # Dependency generation
ram54288 0:dbad57390bd1 67 override CFLAGS += -MMD -MP
ram54288 0:dbad57390bd1 68 ifneq (,$(CPU))
ram54288 0:dbad57390bd1 69 # CPU must be lowercase on GCC
ram54288 0:dbad57390bd1 70 CPU_L:=$(shell echo $(CPU) | tr A-Z a-z)
ram54288 0:dbad57390bd1 71 override CFLAGS += -mcpu=$(CPU_L)
ram54288 0:dbad57390bd1 72 # Use THUMB instructions of Cortex-M cores
ram54288 0:dbad57390bd1 73 ifeq (cortex-m,$(findstring cortex-m,$(CPU_L)))
ram54288 0:dbad57390bd1 74 override CFLAGS += -mthumb
ram54288 0:dbad57390bd1 75 endif
ram54288 0:dbad57390bd1 76 endif
ram54288 0:dbad57390bd1 77 # Debug
ram54288 0:dbad57390bd1 78 ifeq ($(DEBUG),1)
ram54288 0:dbad57390bd1 79 override CFLAGS += -g -O0
ram54288 0:dbad57390bd1 80 else
ram54288 0:dbad57390bd1 81 override CFLAGS += -O2
ram54288 0:dbad57390bd1 82 endif
ram54288 0:dbad57390bd1 83 # Enable Coverage generation
ram54288 0:dbad57390bd1 84 ifeq ($(COVERAGE),1)
ram54288 0:dbad57390bd1 85 override CFLAGS += -ftest-coverage -fprofile-arcs
ram54288 0:dbad57390bd1 86 override LDFLAGS += -ftest-coverage -fprofile-arcs
ram54288 0:dbad57390bd1 87 endif
ram54288 0:dbad57390bd1 88 COMPILE = $(CC) -std=gnu99 -c -o $@
ram54288 0:dbad57390bd1 89 CXXCOMPILE = $(CC) -std=c++11 -c -o $@
ram54288 0:dbad57390bd1 90
ram54288 0:dbad57390bd1 91 #
ram54288 0:dbad57390bd1 92 # IAR-ARM toolchain
ram54288 0:dbad57390bd1 93 #
ram54288 0:dbad57390bd1 94 else ifneq (,$(findstring iccarm,$(CC)))
ram54288 0:dbad57390bd1 95 AR:=iarchive
ram54288 0:dbad57390bd1 96 AROPTS=$^ --create -o $@
ram54288 0:dbad57390bd1 97 DLIB_FILE=$(subst bin\iccarm.exe,inc\c\DLib_Config_Full.h,$(shell where iccarm))
ram54288 0:dbad57390bd1 98 override CFLAGS += --dlib_config '$(DLIB_FILE)' --cpu Cortex-M4 --vla --diag_suppress Pa50
ram54288 0:dbad57390bd1 99 # Dependency generation
ram54288 0:dbad57390bd1 100 override CFLAGS += --dependencies=m $(basename $@).d
ram54288 0:dbad57390bd1 101 # Debug
ram54288 0:dbad57390bd1 102 ifeq ($(DEBUG),1)
ram54288 0:dbad57390bd1 103 override CFLAGS += --debug -On
ram54288 0:dbad57390bd1 104 else
ram54288 0:dbad57390bd1 105 override CFLAGS += -Om
ram54288 0:dbad57390bd1 106 endif
ram54288 0:dbad57390bd1 107 COMPILE = $(CC) -c -o $@
ram54288 0:dbad57390bd1 108
ram54288 0:dbad57390bd1 109 #
ram54288 0:dbad57390bd1 110 # ArmCC toolchain (Used by Keil)
ram54288 0:dbad57390bd1 111 #
ram54288 0:dbad57390bd1 112 else ifneq (,$(findstring armcc,$(CC)))
ram54288 0:dbad57390bd1 113 AR:=armar
ram54288 0:dbad57390bd1 114 AROPTS=-rsc $@ $^
ram54288 0:dbad57390bd1 115 override CFLAGS += --c99 --no_wrap_diagnostics
ram54288 0:dbad57390bd1 116 # Dependency generation
ram54288 0:dbad57390bd1 117 override CFLAGS += --depend $(basename $@).d --phony_targets
ram54288 0:dbad57390bd1 118 LIB:=$(LIB:%.a=%.lib)
ram54288 0:dbad57390bd1 119 ifneq (,$(CPU))
ram54288 0:dbad57390bd1 120 override CFLAGS += --cpu=$(CPU)
ram54288 0:dbad57390bd1 121 endif
ram54288 0:dbad57390bd1 122 # Debug
ram54288 0:dbad57390bd1 123 ifeq ($(DEBUG),1)
ram54288 0:dbad57390bd1 124 override CFLAGS += -g -O0
ram54288 0:dbad57390bd1 125 else
ram54288 0:dbad57390bd1 126 override CFLAGS += -O2
ram54288 0:dbad57390bd1 127 endif
ram54288 0:dbad57390bd1 128 COMPILE = $(CC) -c -o $@
ram54288 0:dbad57390bd1 129
ram54288 0:dbad57390bd1 130 #
ram54288 0:dbad57390bd1 131 # IAR Renesas78 toolchain
ram54288 0:dbad57390bd1 132 #
ram54288 0:dbad57390bd1 133 else ifneq (,$(findstring iccrl78,$(CC)))
ram54288 0:dbad57390bd1 134 AR:=xar
ram54288 0:dbad57390bd1 135 AROPTS=$@ $^
ram54288 0:dbad57390bd1 136 # Hack to get \lib include path which contains dlib config headers for compiler
ram54288 0:dbad57390bd1 137 DIR=$(subst iccrl78.exe,..\lib\,$(shell where iccrl78))
ram54288 0:dbad57390bd1 138 override CFLAGS += --core rl78_1 --code_model far --data_model far --dlib_config full --vla --no_wrap_diagnostics -I'$(DIR)'
ram54288 0:dbad57390bd1 139 # Dependency generation
ram54288 0:dbad57390bd1 140 LIB:=$(LIB:%.a=%.lib)
ram54288 0:dbad57390bd1 141 override CFLAGS += --dependencies=m $(basename $@).d
ram54288 0:dbad57390bd1 142 # Debug
ram54288 0:dbad57390bd1 143 ifeq ($(DEBUG),1)
ram54288 0:dbad57390bd1 144 override CFLAGS += --debug -Oh
ram54288 0:dbad57390bd1 145 else
ram54288 0:dbad57390bd1 146 override CFLAGS += -Ohz
ram54288 0:dbad57390bd1 147 endif
ram54288 0:dbad57390bd1 148 COMPILE = $(CC) -o $@
ram54288 0:dbad57390bd1 149
ram54288 0:dbad57390bd1 150 #
ram54288 0:dbad57390bd1 151 # IAR MSP430 toolchain
ram54288 0:dbad57390bd1 152 #
ram54288 0:dbad57390bd1 153 else ifneq (,$(findstring icc430,$(CC)))
ram54288 0:dbad57390bd1 154 AR:=xar
ram54288 0:dbad57390bd1 155 AROPTS=$@ $^
ram54288 0:dbad57390bd1 156 # Hack to get \lib include path which contains dlib config headers for compiler
ram54288 0:dbad57390bd1 157 DIR=$(subst bin\icc430.exe,lib,$(shell where icc430))
ram54288 0:dbad57390bd1 158 DLIB_FILE=$(subst bin\icc430.exe,lib\dlib\dl430xllff.h,$(shell where icc430))
ram54288 0:dbad57390bd1 159 override CFLAGS += --dlib_config '$(DLIB_FILE)' --vla --core 430X --data_model large --no_wrap_diagnostics -I'$(DIR)'
ram54288 0:dbad57390bd1 160 # Dependency generation
ram54288 0:dbad57390bd1 161 LIB:=$(LIB:%.a=%.lib)
ram54288 0:dbad57390bd1 162 override CFLAGS += --dependencies=m $(basename $@).d
ram54288 0:dbad57390bd1 163 # Debug
ram54288 0:dbad57390bd1 164 ifeq ($(DEBUG),1)
ram54288 0:dbad57390bd1 165 override CFLAGS += --debug -On
ram54288 0:dbad57390bd1 166 else
ram54288 0:dbad57390bd1 167 override CFLAGS += -Ohz
ram54288 0:dbad57390bd1 168 endif
ram54288 0:dbad57390bd1 169 COMPILE = $(CC) -o $@
ram54288 0:dbad57390bd1 170
ram54288 0:dbad57390bd1 171 #
ram54288 0:dbad57390bd1 172 # CppCheck toolchain
ram54288 0:dbad57390bd1 173 # This is used only for static testing the code.
ram54288 0:dbad57390bd1 174 # cppcheck is used in place of compiler and linker phase is ignored
ram54288 0:dbad57390bd1 175 else ifneq (,$(findstring cppcheck,$(CC)))
ram54288 0:dbad57390bd1 176 AR = cppcheck -q --enable=warning --enable=style --std=c99 --inline-suppr -DCPPCHECK $(CPPCHECK_OPTS) $(CFLAGS) $(SRCS)
ram54288 0:dbad57390bd1 177 COMPILE = true
ram54288 0:dbad57390bd1 178 CPPCHECK = 1
ram54288 0:dbad57390bd1 179 LIB:= "ignored_with_cppcheck"
ram54288 0:dbad57390bd1 180
ram54288 0:dbad57390bd1 181
ram54288 0:dbad57390bd1 182 ###################################
ram54288 0:dbad57390bd1 183 # End of toolchain specific rules #
ram54288 0:dbad57390bd1 184 ###################################
ram54288 0:dbad57390bd1 185 endif
ram54288 0:dbad57390bd1 186
ram54288 0:dbad57390bd1 187 #
ram54288 0:dbad57390bd1 188 # Generate VERSION tag
ram54288 0:dbad57390bd1 189 #
ram54288 0:dbad57390bd1 190 ifeq (,$(CPPCHECK))
ram54288 0:dbad57390bd1 191 VERSION := $(shell git describe --tags --long --dirty --always)
ram54288 0:dbad57390bd1 192 CFLAGS += -DVERSION='"$(VERSION)"'
ram54288 0:dbad57390bd1 193 endif
ram54288 0:dbad57390bd1 194
ram54288 0:dbad57390bd1 195 ############################################################
ram54288 0:dbad57390bd1 196 # AUTOMATICALLY GENERATE LIBRARY COMPILATION RULES #
ram54288 0:dbad57390bd1 197 # #
ram54288 0:dbad57390bd1 198 # Use: #
ram54288 0:dbad57390bd1 199 # $(eval $(call generate_rules,$(OUTPUT_LIB),$(SRCS))) #
ram54288 0:dbad57390bd1 200 # at the end of your Makefile #
ram54288 0:dbad57390bd1 201 # #
ram54288 0:dbad57390bd1 202 ############################################################
ram54288 0:dbad57390bd1 203
ram54288 0:dbad57390bd1 204 define generate_rules
ram54288 0:dbad57390bd1 205
ram54288 0:dbad57390bd1 206 DEST_LIB := $(1)
ram54288 0:dbad57390bd1 207 LIB_SRCS := $(2)
ram54288 0:dbad57390bd1 208 C_SRCS := $$(filter %.c,$$(LIB_SRCS))
ram54288 0:dbad57390bd1 209 CXX_SRCS := $$(filter %.cpp,$$(LIB_SRCS))
ram54288 0:dbad57390bd1 210 all: $$(DEST_LIB)
ram54288 0:dbad57390bd1 211
ram54288 0:dbad57390bd1 212 #
ram54288 0:dbad57390bd1 213 # Define build dir outside from src folders
ram54288 0:dbad57390bd1 214 #
ram54288 0:dbad57390bd1 215 BUILD_DIR := output/$$(CC)
ram54288 0:dbad57390bd1 216 ifneq (,$$(CPU))
ram54288 0:dbad57390bd1 217 BUILD_DIR := $$(BUILD_DIR)_$$(CPU)
ram54288 0:dbad57390bd1 218 endif
ram54288 0:dbad57390bd1 219 ifneq (generic,$$(CONFIG))
ram54288 0:dbad57390bd1 220 BUILD_DIR := $$(BUILD_DIR)_$$(CONFIG)
ram54288 0:dbad57390bd1 221 endif
ram54288 0:dbad57390bd1 222 OBJS := $$(C_SRCS:%.c=$$(BUILD_DIR)/%.o) $$(CXX_SRCS:%.cpp=$$(BUILD_DIR)/%.o)
ram54288 0:dbad57390bd1 223
ram54288 0:dbad57390bd1 224 # Include auto-dependencies
ram54288 0:dbad57390bd1 225 -include $$(OBJS:.o=.d)
ram54288 0:dbad57390bd1 226
ram54288 0:dbad57390bd1 227 # Create list of output directories.
ram54288 0:dbad57390bd1 228 OBJ_DIRS := $$(sort $$(dir $$(OBJS)))
ram54288 0:dbad57390bd1 229
ram54288 0:dbad57390bd1 230 # requires directories to be created before build.
ram54288 0:dbad57390bd1 231 # '|' means do not care about timestamps of requisities
ram54288 0:dbad57390bd1 232 $$(OBJS): | $$(OBJ_DIRS)
ram54288 0:dbad57390bd1 233
ram54288 0:dbad57390bd1 234 # Need to redefine build rule to find the sources
ram54288 0:dbad57390bd1 235 # from correct folder.
ram54288 0:dbad57390bd1 236 $$(BUILD_DIR)/%.o: %.c
ram54288 0:dbad57390bd1 237 ifeq ($(V),1)
ram54288 0:dbad57390bd1 238 $$(COMPILE) $$(CFLAGS) $$<
ram54288 0:dbad57390bd1 239 else ifeq (1,$(CPPCHECK))
ram54288 0:dbad57390bd1 240 @$$(COMPILE) $$(CFLAGS) $$<
ram54288 0:dbad57390bd1 241 else ifeq (1,$(UNITTEST))
ram54288 0:dbad57390bd1 242 @echo Building unittest $$<
ram54288 0:dbad57390bd1 243 ifneq ($(OS),Windows_NT)
ram54288 0:dbad57390bd1 244 ruby $(UNIT_TEST_BASE)/../auto/generate_test_runner.rb Test.c Test_Runner.c
ram54288 0:dbad57390bd1 245 endif
ram54288 0:dbad57390bd1 246 @gcc $$(CFLAGS) $$(SRCS) -o $$(OUTPUT_LIB)
ram54288 0:dbad57390bd1 247 else
ram54288 0:dbad57390bd1 248 @echo Building $$<
ram54288 0:dbad57390bd1 249 @$$(COMPILE) $$(CFLAGS) $$<
ram54288 0:dbad57390bd1 250 endif
ram54288 0:dbad57390bd1 251
ram54288 0:dbad57390bd1 252 # Same again for C++
ram54288 0:dbad57390bd1 253 $$(BUILD_DIR)/%.o: %.cpp
ram54288 0:dbad57390bd1 254 ifeq ($(V),1)
ram54288 0:dbad57390bd1 255 $$(CXXCOMPILE) $$(CFLAGS) $$<
ram54288 0:dbad57390bd1 256 else ifeq (1,$(CPPCHECK))
ram54288 0:dbad57390bd1 257 @$$(CXXCOMPILE) $$(CFLAGS) $$<
ram54288 0:dbad57390bd1 258 else
ram54288 0:dbad57390bd1 259 @echo Building $$<
ram54288 0:dbad57390bd1 260 @$$(CXXCOMPILE) $$(CFLAGS) $$<
ram54288 0:dbad57390bd1 261 endif
ram54288 0:dbad57390bd1 262
ram54288 0:dbad57390bd1 263 # Rule to create a object directory
ram54288 0:dbad57390bd1 264 $$(OBJ_DIRS):
ram54288 0:dbad57390bd1 265 @-$$(call mkdir,$$@)
ram54288 0:dbad57390bd1 266
ram54288 0:dbad57390bd1 267 $$(DEST_LIB): $$(OBJS)
ram54288 0:dbad57390bd1 268 ifeq ($(V),1)
ram54288 0:dbad57390bd1 269 -$$(addprefix $$(RM) ,$$(wildcard $$@))
ram54288 0:dbad57390bd1 270 $$(AR) $$(AROPTS)
ram54288 0:dbad57390bd1 271 else ifeq (1,$(CPPCHECK))
ram54288 0:dbad57390bd1 272 @$$(AR) $$(AROPTS)
ram54288 0:dbad57390bd1 273 else ifeq (1,$(UNITTEST))
ram54288 0:dbad57390bd1 274 #ifneq ($(OS),Windows_NT)
ram54288 0:dbad57390bd1 275 # lcov -q --capture -i --directory $(CURDIR) --output-file lib_base.info
ram54288 0:dbad57390bd1 276 # ./$$@
ram54288 0:dbad57390bd1 277 # lcov -q --capture --directory $(CURDIR) --output-file lib_test.info
ram54288 0:dbad57390bd1 278 # lcov -a lib_base.info -a lib_test.info -o coverage.info
ram54288 0:dbad57390bd1 279 #endif
ram54288 0:dbad57390bd1 280 else
ram54288 0:dbad57390bd1 281 @echo Linking $$(notdir $$@)
ram54288 0:dbad57390bd1 282 @-$$(addprefix $$(RM) ,$$(wildcard $$@))
ram54288 0:dbad57390bd1 283 @$$(AR) $$(AROPTS)
ram54288 0:dbad57390bd1 284 endif
ram54288 0:dbad57390bd1 285
ram54288 0:dbad57390bd1 286 clean:
ram54288 0:dbad57390bd1 287 @$(RM) -r $$(BUILD_DIR)
ram54288 0:dbad57390bd1 288 @$(RM) $$(DEST_LIB)
ram54288 0:dbad57390bd1 289
ram54288 0:dbad57390bd1 290 clean-all:
ram54288 0:dbad57390bd1 291 @$(RM) -r output *.a
ram54288 0:dbad57390bd1 292
ram54288 0:dbad57390bd1 293 endef