A metronome using the FRDM K64F board

Committer:
ram54288
Date:
Sun May 14 18:40:18 2017 +0000
Revision:
0:a7a43371b306
Initial commit

Who changed what in which revision?

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