Home | History | Annotate | Download | only in app
      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 += -fshort-double
    162 CFLAGS += -fvisibility=hidden
    163 CFLAGS += -fno-unwind-tables
    164 CFLAGS += -fstack-reuse=all
    165 CFLAGS += -ffunction-sections
    166 CFLAGS += -fdata-sections
    167 
    168 # Linker Configuration #########################################################
    169 
    170 LD := $(PREFIX)g++
    171 
    172 LDFLAGS := -T $(NANOHUB_DIR)/os/platform/$(PLATFORM)/lkr/app.lkr
    173 LDFLAGS += -nostartfiles
    174 LDFLAGS += -Wl,--gc-sections
    175 LDFLAGS += -Wl,-Map,$(OUT)/$(BIN).map
    176 LDFLAGS += -Wl,--cref
    177 STATIC_LIBS += -lgcc
    178 ifeq ($(BIN_MODE),static)
    179 LDFLAGS += -Bstatic
    180 LDFLAGS += -Wl,--emit-relocs
    181 else
    182 LDFLAGS += -Bdynamic
    183 LDFLAGS += -Wl,--no-undefined
    184 LDFLAGS += -Wl,--no-allow-shlib-undefined
    185 endif
    186 
    187 # Build Rules ##################################################################
    188 
    189 AS_SRCS := $(filter %.S, $(SRCS))
    190 C_SRCS := $(filter %.c, $(SRCS))
    191 CXX_SRCS := $(filter %.cc, $(SRCS))
    192 CPP_SRCS := $(filter %.cpp, $(SRCS))
    193 
    194 OBJS := $(patsubst %.S, $(OUT)/%.o, $(AS_SRCS))
    195 OBJS += $(patsubst %.c, $(OUT)/%.o, $(C_SRCS))
    196 OBJS += $(patsubst %.cc, $(OUT)/%.o, $(CXX_SRCS))
    197 OBJS += $(patsubst %.cpp, $(OUT)/%.o, $(CPP_SRCS))
    198 
    199 UNSIGNED_BIN := $(BIN).unsigned.napp
    200 
    201 NANOHUB_KEY_PATH := $(NANOHUB_DIR)/os/platform/$(PLATFORM)/misc
    202 
    203 .PHONY: all clean sync
    204 all: $(OUT)/$(BIN).S $(OUT)/$(BIN).napp $(IMAGE_TARGET_OUT)
    205 
    206 $(OUT)/$(BIN).napp : $(OUT)/$(UNSIGNED_BIN) $(NANOAPP_SIGN)
    207 	@mkdir -p $(dir $@)
    208 	$(NANOAPP_SIGN) -e $(NANOHUB_KEY_PATH)/debug.privkey \
    209 		-m $(NANOHUB_KEY_PATH)/debug.pubkey -s $< $@
    210 ifdef IMAGE_TARGET_OUT
    211 $(IMAGE_TARGET_OUT): $(OUT)/$(BIN).napp
    212 	@mkdir -p $(dir $@)
    213 	cp $< $(IMAGE_TARGET_OUT)
    214 endif
    215 
    216 ifeq ($(APP_VERSION),)
    217 APP_VERSION := 0
    218 endif
    219 
    220 ifeq ($(BIN_MODE),static)
    221 $(OUT)/$(UNSIGNED_BIN) : $(OUT)/$(BIN).elf $(NANOAPP_POSTPROCESS)
    222 	@mkdir -p $(dir $@)
    223 	$(NANOAPP_POSTPROCESS) -s -a $(APP_ID) -e $(APP_VERSION) -v $(BIN_POSTPROCESS_ARGS) $< $@
    224 else
    225 $(OUT)/$(UNSIGNED_BIN) : $(OUT)/$(BIN).bin $(NANOAPP_POSTPROCESS)
    226 	@mkdir -p $(dir $@)
    227 	$(NANOAPP_POSTPROCESS) -a $(APP_ID) -e $(APP_VERSION) -v $(BIN_POSTPROCESS_ARGS) $< $@
    228 
    229 $(OUT)/$(BIN).bin : $(OUT)/$(BIN).elf
    230 	@mkdir -p $(dir $@)
    231 	$(OBJCOPY) -j.relocs -j.flash -j.data -j.dynsym -O binary $< $@
    232 endif
    233 
    234 $(OUT)/$(BIN).S : $(OUT)/$(BIN).elf
    235 	@mkdir -p $(dir $@)
    236 	$(OBJDUMP) $< -DS > $@
    237 
    238 $(OUT)/$(BIN).elf : $(OBJS)
    239 	@mkdir -p $(dir $@)
    240 	$(LD) $(CFLAGS) $(CXX_FLAGS) $(LDFLAGS) $(OBJS) $(STATIC_LIBS) -o $@
    241 
    242 $(OUT)/%.o : %.S
    243 	@mkdir -p $(dir $@)
    244 	$(AS) $(AS_FLAGS) $(CFLAGS) -DSRC_FILENAME=\"$(notdir $<)\" -c $< -o $@
    245 
    246 $(OUT)/%.o : %.c
    247 	@mkdir -p $(dir $@)
    248 	$(CC) $(C_CFLAGS) $(CFLAGS) -DSRC_FILENAME=\"$(notdir $<)\" -c $< -o $@
    249 
    250 $(OUT)/%.o : %.cc
    251 	@mkdir -p $(dir $@)
    252 	$(CXX) $(CXX_CFLAGS) $(CFLAGS) -DSRC_FILENAME=\"$(notdir $<)\" -c $< -o $@
    253 
    254 $(OUT)/%.o : %.cpp
    255 	@mkdir -p $(dir $@)
    256 	$(CXX) $(CXX_CFLAGS) $(CFLAGS) -DSRC_FILENAME=\"$(notdir $<)\" -c $< -o $@
    257 
    258 # Automatic dependency resolution ##############################################
    259 
    260 DEPS_AS = $(OUT)/deps_as
    261 DEPS_C = $(OUT)/deps_c
    262 DEPS_CXX = $(OUT)/deps_cxx
    263 
    264 $(DEPS_AS) : $(AS_SRCS)
    265 	@mkdir -p $(dir $@)
    266 	$(AS) $(AS_CFLAGS) $(CFLAGS) -MM $^ > $@
    267 
    268 $(DEPS_C) : $(C_SRCS)
    269 	@mkdir -p $(dir $@)
    270 	$(CC) $(C_CFLAGS) $(CFLAGS) -MM $^ > $@
    271 
    272 $(DEPS_CXX) : $(CXX_SRCS) $(CPP_SRCS)
    273 	@mkdir -p $(dir $@)
    274 	$(CXX) $(CXX_CFLAGS) $(CFLAGS) -MM $^ > $@
    275 
    276 NOAUTODEPTARGETS = clean
    277 
    278 ifeq ($(words $(findstring $(MAKECMDGOALS), $(NOAUTODEPTARGETS))), 0)
    279 
    280 ifneq ($(AS_SRCS), )
    281 -include $(DEPS_AS)
    282 endif
    283 
    284 ifneq ($(C_SRCS), )
    285 -include $(DEPS_C)
    286 endif
    287 
    288 ifneq ($(CXX_SRCS)$(CPP_SRCS),)
    289 -include $(DEPS_CXX)
    290 endif
    291 
    292 endif
    293 
    294 $(NANOAPP_POSTPROCESS): $(wildcard $(dir $(NANOAPP_POSTPROCESS))/*.c* $(dir $(NANOAPP_POSTPROCESS))/*.h)
    295 	echo DEPS [$@]: $^
    296 	make -C $(dir $@)
    297 
    298 $(NANOAPP_SIGN): $(wildcard $(dir $(NANOAPP_SIGN))/*.c* $(dir $(NANOAPP_SIGN))/*.h)
    299 	echo DEPS [$@]: $^
    300 	make -C $(dir $@)
    301 
    302 # Clean targets ################################################################
    303 
    304 clean :
    305 	rm -rf $(OUT)
    306 
    307 sync:   $(OUT)/$(BIN).napp
    308 	adb push $< /vendor/firmware/$(BIN).napp
    309