1# Copyright 2023 Nordic Semiconductor ASA 2# SPDX-License-Identifier: Apache-2.0 3 4# Native Simulator (NSI) Makefile. 5# It builds the simulator runner itself, and produces the final 6# Linux executable by linking it to the the embedded cpu library 7 8# By default all the build output is placed under the _build folder, but the user can override it 9# setting the NSI_BUILD_PATH 10# 11# The caller can provide an optional configuration file and point to it with NSI_CONFIG_FILE 12# See "Configurable/user overridible variables" below. 13# 14# By default only the core of the runner will be built, but the caller can set NSI_NATIVE 15# to also build the components in native/src/ 16 17NSI_CONFIG_FILE?=nsi_config 18-include ${NSI_CONFIG_FILE} 19#If the file does not exist, we don't use it as a build dependency 20NSI_CONFIG_FILE:=$(wildcard ${NSI_CONFIG_FILE}) 21 22# Configurable/user overridible variables: 23# Path to the native_simulator (this folder): 24NSI_PATH?=./ 25# Folder where the build output will be placed 26NSI_BUILD_PATH?=$(abspath _build/) 27EXE_NAME?=native_simulator.exe 28# Final executable path/file_name which will be produced 29NSI_EXE?=${NSI_BUILD_PATH}/${EXE_NAME} 30# Number of embedded CPUs/MCUs 31NSI_N_CPUS?=1 32# Path to all CPUs embedded SW which will be linked with the final executable 33NSI_EMBEDDED_CPU_SW?= 34# Host architecture configuration switch 35NSI_ARCH?=-m32 36# Coverage switch (GCOV coverage is enabled by default) 37NSI_COVERAGE?=--coverage 38NSI_LOCALIZE_OPTIONS?= 39NSI_BUILD_OPTIONS?=${NSI_ARCH} ${NSI_COVERAGE} 40NSI_LINK_OPTIONS?=${NSI_ARCH} ${NSI_COVERAGE} 41# Extra source files to be built in the runner context 42NSI_EXTRA_SRCS?= 43# Extra include directories to be used while building in the runner context 44NSI_EXTRA_INCLUDES?= 45# Extra libraries to be linked to the final executable 46NSI_EXTRA_LIBS?= 47 48SHELL?=bash 49# Compiler 50NSI_CC?=gcc 51# Archive program (it is unlikely you'll need to change this) 52NSI_AR?=ar 53# Objcopy program (it is unlikely you'll need to change this) 54NSI_OBJCOPY?=objcopy 55 56# Build debug switch (by default enabled) 57NSI_DEBUG?=-g 58# Build optimization level (by default disabled to ease debugging) 59NSI_OPT?=-O0 60# Warnings switches (for the runner itself) 61NSI_WARNINGS?=-Wall -Wpedantic 62# Preprocessor flags 63NSI_CPPFLAGS?=-D_POSIX_C_SOURCE=200809 -D_XOPEN_SOURCE=600 -D_XOPEN_SOURCE_EXTENDED 64 65NO_PIE_CO:=-fno-pie -fno-pic 66DEPENDFLAGS:=-MMD -MP 67CFLAGS:=${NSI_DEBUG} ${NSI_WARNINGS} ${NSI_OPT} ${NO_PIE_CO} -DNSI_N_CPUS=${NSI_N_CPUS} \ 68 -ffunction-sections -fdata-sections ${DEPENDFLAGS} -std=c11 ${NSI_BUILD_OPTIONS} 69FINALLINK_FLAGS:=${NO_PIE_CO} -no-pie ${NSI_WARNINGS} \ 70 -Wl,--gc-sections -ldl -pthread \ 71 ${NSI_LINK_OPTIONS} -lm 72 73no_default: 74 @echo "There is no default rule, please specify what you want to build,\ 75 or run make help for more info" 76 77RUNNER_LIB:=runner.a 78 79SRCS:=$(shell ls ${NSI_PATH}common/src/*.c) 80ifdef NSI_NATIVE 81 SRCS+=$(shell ls ${NSI_PATH}native/src/*.c) 82endif 83 84INCLUDES:=-I${NSI_PATH}common/src/include/ \ 85 -I${NSI_PATH}common/src \ 86 ${NSI_EXTRA_INCLUDES} 87 88ifdef NSI_NATIVE 89 INCLUDES+=-I${NSI_PATH}native/src/include/ 90endif 91 92EXTRA_OBJS:=$(abspath $(addprefix $(NSI_BUILD_PATH)/,$(sort ${NSI_EXTRA_SRCS:%.c=%.o}))) 93OBJS:=$(abspath $(addprefix $(NSI_BUILD_PATH)/,${SRCS:${NSI_PATH}%.c=%.o})) ${EXTRA_OBJS} 94 95DEPENDFILES:=$(addsuffix .d,$(basename ${OBJS})) 96 97-include ${DEPENDFILES} 98 99LOCALIZED_EMBSW:=$(abspath $(addprefix $(NSI_BUILD_PATH)/,$(addsuffix .loc_cpusw.o,${NSI_EMBEDDED_CPU_SW}))) 100 101${NSI_BUILD_PATH}: 102 @if [ ! -d ${NSI_BUILD_PATH} ]; then mkdir -p ${NSI_BUILD_PATH}; fi 103 104#Extra sources build: 105${NSI_BUILD_PATH}/%.o: /%.c ${NSI_PATH}Makefile ${NSI_CONFIG_FILE} 106 @if [ ! -d $(dir $@) ]; then mkdir -p $(dir $@); fi 107 ${NSI_CC} ${NSI_CPPFLAGS} ${INCLUDES} ${CFLAGS} -c $< -o $@ 108 109${NSI_BUILD_PATH}/%.o: ${NSI_PATH}/%.c ${NSI_PATH}Makefile ${NSI_CONFIG_FILE} 110 @if [ ! -d $(dir $@) ]; then mkdir -p $(dir $@); fi 111 ${NSI_CC} ${NSI_CPPFLAGS} ${INCLUDES} ${CFLAGS} -c $< -o $@ 112 113${NSI_BUILD_PATH}/linker_script.ld : ${NSI_PATH}/common/other/linker_script.pre.ld | ${NSI_BUILD_PATH} 114 ${NSI_CC} -x c -E -P $< -o $@ ${DEPENDFLAGS} 115 116${NSI_BUILD_PATH}/${RUNNER_LIB}: ${OBJS} 117 if [ -f $@ ]; then rm $@ ; fi 118 ${NSI_AR} -cr $@ ${OBJS} 119 120${NSI_BUILD_PATH}/%.loc_cpusw.o: /% ${NSI_CONFIG_FILE} 121 @if [ -z $< ] || [ ! -f $< ]; then \ 122 echo "Error: Input embedded CPU SW ($<) not found \ 123(NSI_EMBEDDED_CPU_SW=${NSI_EMBEDDED_CPU_SW} )"; \ 124 false; \ 125 fi 126 @if [ ! -d $(dir $@) ]; then mkdir -p $(dir $@); fi 127 ${NSI_OBJCOPY} --localize-hidden $< $@ -w --localize-symbol=_* ${NSI_LOCALIZE_OPTIONS} 128 129${NSI_EXE}: ${NSI_BUILD_PATH}/${RUNNER_LIB} ${LOCALIZED_EMBSW} ${NSI_EXTRA_LIBS} \ 130 ${NSI_BUILD_PATH}/linker_script.ld 131 ${NSI_CC} -Wl,--whole-archive ${LOCALIZED_EMBSW} ${NSI_BUILD_PATH}/${RUNNER_LIB} \ 132 ${NSI_EXTRA_LIBS} -Wl,--no-whole-archive \ 133 -o $@ ${FINALLINK_FLAGS} -T ${NSI_BUILD_PATH}/linker_script.ld 134 135Makefile: ; 136 137link_with_esw: ${NSI_EXE}; 138 139runner_lib: ${NSI_BUILD_PATH}/${RUNNER_LIB} 140 141all: link_with_esw 142 143clean: 144 @echo "Deleting intermediate compilation results + libraries + executables (*.d .o .a .exe)" 145 find $(NSI_BUILD_PATH) -name "*.o" -or -name "*.exe" -or -name "*.a" -or -name "*.d" | xargs rm -f 146 147clean_coverage: 148 find $(NSI_BUILD_PATH) -name "*.gcda" -or -name "*.gcno" | xargs rm -f ; true 149 150clean_all: clean clean_coverage ; 151 152.PHONY: clean clean_coverage clean_all link_with_esw runner_lib no_default all ${DEPENDFILES} 153 154ifndef NSI_BUILD_VERBOSE 155.SILENT: 156endif 157 158help: 159 @echo "*******************************" 160 @echo "* Native Simulator makefile *" 161 @echo "*******************************" 162 @echo "Provided rules:" 163 @echo " clean : clean all build output" 164 @echo " clean_coverage : clean all coverage files" 165 @echo " clean_all : clean + clean_coverage" 166 @echo " link_with_esw : Link the runner with the CPU embedded sw" 167 @echo " runner_lib : Build the runner itself (pending the embedded SW)" 168 @echo " all : link_with_esw" 169 @echo "Note that you can use TAB to autocomplete rules in the command line in modern OSs" 170