1 # 2 # Copyright (C) 2016 The Android Open Source Project 3 # 4 # Licensed under the Apache License, Version 2.0 (the "License"); 5 # you may not use this file except in compliance with the License. 6 # You may obtain a copy of the License at 7 # 8 # http://www.apache.org/licenses/LICENSE-2.0 9 # 10 # Unless required by applicable law or agreed to in writing, software 11 # distributed under the License is distributed on an "AS IS" BASIS, 12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 # See the License for the specific language governing permissions and 14 # limitations under the License. 15 # 16 ################################################################################ 17 # 18 # NanoApp C/C++ Makefile Utils 19 # 20 ################################################################################ 21 22 # Configuration ################################################################ 23 24 # Toolchain Prefix 25 ifndef CROSS_COMPILE 26 $(error Please set the environment variable CROSS_COMPILE to the complete \ 27 path to the toolchain directory plus the binary prefix, e.g. export \ 28 CROSS_COMPILE=~/bin/gcc-arm-none-eabi-4_8-2014q3/bin/arm-none-eabi-) 29 endif 30 31 PREFIX = $(CROSS_COMPILE) 32 TOOLCHAIN_DIR = $(shell dirname `which $(CROSS_COMPILE)gcc`)/.. 33 34 # NANOHUB_DIR is relative to CWD (which is always APP Makefile dir) 35 36 NANOAPP_POSTPROCESS := $(NANOHUB_DIR)/../util/nanoapp_postprocess/nanoapp_postprocess 37 NANOAPP_SIGN := $(NANOHUB_DIR)/../util/nanoapp_sign/nanoapp_sign 38 39 # TOP_RELPATH is ANDROID_TOP relative to NANOHUB_DIR 40 TOP_RELPATH := ../../../.. 41 TOP_ABSPATH := $(realpath $(NANOHUB_DIR)/$(TOP_RELPATH)) 42 43 # for local variants there is always a path; for out-of-tree variants there may be 44 # - a $(VARIANT) soft link under firmware/variant subdir, or 45 # - VARIANT_PATH is found as vendor/<vendor_name>/<variant_name>/nanohub, or 46 # - explicitly provided in VARIANT_CONFIG_PATH (path, where <variant>_conf.mk is located) 47 # 48 ifeq ($(VARIANT_CONFIG_PATH),) 49 variant_conf := $(wildcard $(TOP_ABSPATH)/vendor/*/*/nanohub/$(VARIANT)_conf.mk) 50 ifeq ($(words $(variant_conf)),1) 51 VARIANT_CONFIG_PATH := $(patsubst $(TOP_ABSPATH)/%/$(VARIANT)_conf.mk,%,$(variant_conf)) 52 else 53 VARIANT_CONFIG_PATH := device/google/contexthub/firmware/variant/$(VARIANT) 54 endif 55 endif 56 57 include $(TOP_ABSPATH)/$(VARIANT_CONFIG_PATH)/$(VARIANT)_conf.mk 58 59 # $(VARIANT)_conf.mk defines VARIANT_PATH, PLATFORM, CHIP, CPU, VARIANT 60 # VARIANT_PATH from $(VARIANT)_conf.mk is ANDROID_TOP relative 61 62 # change VARIANT_PATH to become CWD-relative 63 VARIANT_PATH := $(NANOHUB_DIR)/$(TOP_RELPATH)/$(VARIANT_PATH) 64 65 # all output goes here 66 ifndef OUT 67 OUT:=out/nanohub/$(VARIANT)/app/$(BIN) 68 else 69 ifneq ($(filter $(TOP_ABSPATH)/out/target/product/%,$(OUT)),) 70 # this looks like Android OUT env var; update it 71 IMAGE_TARGET_OUT:=$(OUT)/vendor/firmware/$(BIN).napp 72 OUT:=$(OUT)/nanohub/$(VARIANT)/app/$(BIN) 73 endif 74 endif 75 76 ################################################################################ 77 # 78 # Nanoapp Libc/Libm Utils 79 # 80 ################################################################################ 81 82 include $(NANOHUB_DIR)/lib/lib.mk 83 84 # Tools ######################################################################## 85 86 AS := $(PREFIX)gcc 87 CC := $(PREFIX)gcc 88 CXX := $(PREFIX)g++ 89 OBJCOPY := $(PREFIX)objcopy 90 OBJDUMP := $(PREFIX)objdump 91 92 # Assembly Flags ############################################################### 93 94 AS_FLAGS += 95 96 # C++ Flags #################################################################### 97 98 CXX_CFLAGS += -std=c++11 99 CXX_CFLAGS += -fno-exceptions 100 CXX_CFLAGS += -fno-rtti 101 102 # C Flags ###################################################################### 103 104 C_CFLAGS += 105 106 # Common Flags ################################################################# 107 108 # Defines 109 CFLAGS += -DAPP_ID=$(APP_ID) 110 CFLAGS += -DAPP_VERSION=$(APP_VERSION) 111 CFLAGS += -D__NANOHUB__ 112 113 # Optimization/debug 114 CFLAGS += -Os 115 CFLAGS += -g 116 117 # Include paths 118 CFLAGS += -I$(NANOHUB_DIR)/os/inc 119 CFLAGS += -I$(NANOHUB_DIR)/os/platform/$(PLATFORM)/inc 120 CFLAGS += -I$(NANOHUB_DIR)/os/cpu/$(CPU)/inc 121 CFLAGS += -I$(VARIANT_PATH)/inc 122 CFLAGS += -I$(NANOHUB_DIR)/../lib/include 123 124 # Warnings/error configuration. 125 CFLAGS += -Wall 126 CFLAGS += -Werror 127 CFLAGS += -Wmissing-declarations 128 CFLAGS += -Wlogical-op 129 CFLAGS += -Waddress 130 CFLAGS += -Wempty-body 131 CFLAGS += -Wpointer-arith 132 CFLAGS += -Wenum-compare 133 CFLAGS += -Wdouble-promotion 134 CFLAGS += -Wshadow 135 CFLAGS += -Wno-attributes 136 137 # Produce position independent code. 138 CFLAGS += -fpic 139 CFLAGS += -mno-pic-data-is-text-relative 140 CFLAGS += -msingle-pic-base 141 CFLAGS += -mpic-register=r9 142 143 # Code generation options for Cortex-M4F 144 CFLAGS += -mthumb 145 CFLAGS += -mcpu=cortex-m4 146 CFLAGS += -march=armv7e-m 147 CFLAGS += -mfloat-abi=softfp 148 CFLAGS += -mfpu=fpv4-sp-d16 149 CFLAGS += -mno-thumb-interwork 150 CFLAGS += -ffast-math 151 CFLAGS += -fsingle-precision-constant 152 153 # Platform defines 154 CFLAGS += -DARM 155 CFLAGS += -DUSE_NANOHUB_FLOAT_RUNTIME 156 CFLAGS += -DARM_MATH_CM4 157 CFLAGS += -D__FPU_PRESENT 158 159 # Miscellaneous 160 CFLAGS += -fno-strict-aliasing 161 CFLAGS += -fvisibility=hidden 162 CFLAGS += -fno-unwind-tables 163 CFLAGS += -fstack-reuse=all 164 CFLAGS += -ffunction-sections 165 CFLAGS += -fdata-sections 166 167 # Linker Configuration ######################################################### 168 169 LD := $(PREFIX)g++ 170 171 LDFLAGS := -T $(NANOHUB_DIR)/os/platform/$(PLATFORM)/lkr/app.lkr 172 LDFLAGS += -nostartfiles 173 LDFLAGS += -nostdlib 174 LDFLAGS += -Wl,--gc-sections 175 LDFLAGS += -Wl,-Map,$(OUT)/$(BIN).map 176 LDFLAGS += -Wl,--cref 177 ifeq ($(BIN_MODE),static) 178 LDFLAGS += -Bstatic 179 LDFLAGS += -Wl,--emit-relocs 180 else 181 LDFLAGS += -Bdynamic 182 LDFLAGS += -Wl,--no-undefined 183 LDFLAGS += -Wl,--no-allow-shlib-undefined 184 endif 185 186 # Build Rules ################################################################## 187 188 AS_SRCS := $(filter %.S, $(SRCS)) 189 C_SRCS := $(filter %.c, $(SRCS)) 190 CXX_SRCS := $(filter %.cc, $(SRCS)) 191 CPP_SRCS := $(filter %.cpp, $(SRCS)) 192 193 OBJS := $(patsubst %.S, $(OUT)/%.o, $(AS_SRCS)) 194 OBJS += $(patsubst %.c, $(OUT)/%.o, $(C_SRCS)) 195 OBJS += $(patsubst %.cc, $(OUT)/%.o, $(CXX_SRCS)) 196 OBJS += $(patsubst %.cpp, $(OUT)/%.o, $(CPP_SRCS)) 197 198 UNSIGNED_BIN := $(BIN).unsigned.napp 199 200 NANOHUB_KEY_PATH := $(NANOHUB_DIR)/os/platform/$(PLATFORM)/misc 201 202 .PHONY: all clean sync 203 all: $(OUT)/$(BIN).S $(OUT)/$(BIN).napp $(IMAGE_TARGET_OUT) 204 205 $(OUT)/$(BIN).napp : $(OUT)/$(UNSIGNED_BIN) $(NANOAPP_SIGN) 206 @mkdir -p $(dir $@) 207 $(NANOAPP_SIGN) -e $(NANOHUB_KEY_PATH)/debug.privkey \ 208 -m $(NANOHUB_KEY_PATH)/debug.pubkey -s $< $@ 209 ifdef IMAGE_TARGET_OUT 210 $(IMAGE_TARGET_OUT): $(OUT)/$(BIN).napp 211 @mkdir -p $(dir $@) 212 cp $< $(IMAGE_TARGET_OUT) 213 endif 214 215 ifeq ($(APP_VERSION),) 216 APP_VERSION := 0 217 endif 218 219 ifeq ($(BIN_MODE),static) 220 $(OUT)/$(UNSIGNED_BIN) : $(OUT)/$(BIN).elf $(NANOAPP_POSTPROCESS) 221 @mkdir -p $(dir $@) 222 $(NANOAPP_POSTPROCESS) -s -a $(APP_ID) -e $(APP_VERSION) -v $(BIN_POSTPROCESS_ARGS) $< $@ 223 else 224 $(OUT)/$(UNSIGNED_BIN) : $(OUT)/$(BIN).bin $(NANOAPP_POSTPROCESS) 225 @mkdir -p $(dir $@) 226 $(NANOAPP_POSTPROCESS) -a $(APP_ID) -e $(APP_VERSION) -v $(BIN_POSTPROCESS_ARGS) $< $@ 227 228 $(OUT)/$(BIN).bin : $(OUT)/$(BIN).elf 229 @mkdir -p $(dir $@) 230 $(OBJCOPY) -j.relocs -j.flash -j.data -j.dynsym -O binary $< $@ 231 endif 232 233 $(OUT)/$(BIN).S : $(OUT)/$(BIN).elf 234 @mkdir -p $(dir $@) 235 $(OBJDUMP) $< -DS > $@ 236 237 $(OUT)/$(BIN).elf : $(OBJS) 238 @mkdir -p $(dir $@) 239 $(LD) $(CFLAGS) $(CXX_FLAGS) $(LDFLAGS) $(OBJS) $(STATIC_LIBS) -o $@ 240 241 $(OUT)/%.o : %.S 242 @mkdir -p $(dir $@) 243 $(AS) $(AS_FLAGS) $(CFLAGS) -DSRC_FILENAME=\"$(notdir $<)\" -c $< -o $@ 244 245 $(OUT)/%.o : %.c 246 @mkdir -p $(dir $@) 247 $(CC) $(C_CFLAGS) $(CFLAGS) -DSRC_FILENAME=\"$(notdir $<)\" -c $< -o $@ 248 249 $(OUT)/%.o : %.cc 250 @mkdir -p $(dir $@) 251 $(CXX) $(CXX_CFLAGS) $(CFLAGS) -DSRC_FILENAME=\"$(notdir $<)\" -c $< -o $@ 252 253 $(OUT)/%.o : %.cpp 254 @mkdir -p $(dir $@) 255 $(CXX) $(CXX_CFLAGS) $(CFLAGS) -DSRC_FILENAME=\"$(notdir $<)\" -c $< -o $@ 256 257 # Automatic dependency resolution ############################################## 258 259 DEPS_AS = $(OUT)/deps_as 260 DEPS_C = $(OUT)/deps_c 261 DEPS_CXX = $(OUT)/deps_cxx 262 263 $(DEPS_AS) : $(AS_SRCS) 264 @mkdir -p $(dir $@) 265 $(AS) $(AS_CFLAGS) $(CFLAGS) -MM $^ > $@ 266 267 $(DEPS_C) : $(C_SRCS) 268 @mkdir -p $(dir $@) 269 $(CC) $(C_CFLAGS) $(CFLAGS) -MM $^ > $@ 270 271 $(DEPS_CXX) : $(CXX_SRCS) $(CPP_SRCS) 272 @mkdir -p $(dir $@) 273 $(CXX) $(CXX_CFLAGS) $(CFLAGS) -MM $^ > $@ 274 275 NOAUTODEPTARGETS = clean 276 277 ifeq ($(words $(findstring $(MAKECMDGOALS), $(NOAUTODEPTARGETS))), 0) 278 279 ifneq ($(AS_SRCS), ) 280 -include $(DEPS_AS) 281 endif 282 283 ifneq ($(C_SRCS), ) 284 -include $(DEPS_C) 285 endif 286 287 ifneq ($(CXX_SRCS)$(CPP_SRCS),) 288 -include $(DEPS_CXX) 289 endif 290 291 endif 292 293 $(NANOAPP_POSTPROCESS): $(wildcard $(dir $(NANOAPP_POSTPROCESS))/*.c* $(dir $(NANOAPP_POSTPROCESS))/*.h) 294 echo DEPS [$@]: $^ 295 make -C $(dir $@) 296 297 $(NANOAPP_SIGN): $(wildcard $(dir $(NANOAPP_SIGN))/*.c* $(dir $(NANOAPP_SIGN))/*.h) 298 echo DEPS [$@]: $^ 299 make -C $(dir $@) 300 301 # Clean targets ################################################################ 302 303 clean : 304 rm -rf $(OUT) 305 306 sync: $(OUT)/$(BIN).napp 307 adb push $< /vendor/firmware/$(BIN).napp 308