Makefile 5.59 KB
# Configuration section
#
# Use environment variables if found, otherwise fallback to sane defaults

# If not explicitely selected (with 'make FPU=1'), compile for FPU emulation
ifeq ($(FPU),)
FPU=0
endif

# To be able to properly handle any combination of (FPU, LEON, release) 
# options, create a SUFFIX (see below) to differentiate output folders
ifeq ($(FPU),1)
FPU_SUFFIX=FPU
else
FPU_SUFFIX=NONFPU
endif

# Build up our settings from our inputs and our environment
LEON ?= leon3
RTEMS ?= /opt/rtems-4.11-2016.04.01.${FPU_SUFFIX}
RTEMS_MAKEFILE_PATH = ${RTEMS}/sparc-rtems4.11/${LEON}
RTEMS_LIB=${RTEMS_MAKEFILE_PATH}/lib
CROSS_PREFIX=sparc-rtems4.11

# If not selected, compile debug version of binary (no optimizations)
#ifeq ($(CFG),)
#CFG=debug
#endif

# The directories containing the source files, separated by ':'
#
# DEAR USER, YOU MUST EDIT THIS AND ADD YOUR SOURCE FOLDERS
VPATH=src

# Your source files: regardless of where they reside in the source tree,
# VPATH will locate them...
#
# DEAR USER, YOU MUST EDIT THIS
SRC= \
	init.c \
	common.c \
	task1.c \
	task2.c

ifeq ($(COVERAGE),1)
SRC += \
	base.c \
	gcc_4_7.c
COVERAGE_SUFFIX=WITH_COVERAGE
else
COVERAGE_SUFFIX=NO_COVERAGE
endif

SUFFIX=$(CFG).$(FPU_SUFFIX).$(LEON).$(COVERAGE_SUFFIX)

# Build a Dependency list and an Object list, by replacing the .c
# extension to .d for dependency files, and .o for object files.
DEP = $(patsubst %.c, deps.$(SUFFIX)/%.d, ${SRC})
OBJ = $(patsubst %.c, objs.$(SUFFIX)/%.o, ${SRC})

# Your final binary
#
# DEAR USER, YOU MUST EDIT THIS
TARGET=fputest

# What compiler to use for generating dependencies: 
# it will be invoked with -MM -MP
CC = ${RTEMS}/bin/${CROSS_PREFIX}-gcc
CDEP = ${CC}

# What include flags to pass to the compiler
INCLUDEFLAGS= -I src

# Compilation flags (common)
COMMON += -B${RTEMS_LIB} -specs bsp_specs -qrtems \
	  -mcpu=cypress -DBSP_${LEON} \
	  -ffunction-sections -fdata-sections -Wall \
	  -Wmissing-prototypes -Wimplicit-function-declaration \
	  -Wstrict-prototypes -Wnested-externs

# Separate compile options per configuration
ifeq ($(CFG),debug)
CFLAGS += ${COMMON} -g -Wall -D_DEBUG ${INCLUDEFLAGS}
else
CFLAGS += ${COMMON} -g -O2 -Wall ${INCLUDEFLAGS}
endif

ifeq ($(COVERAGE),1)
CFLAGS += -DCOVERAGE_ENABLED
CFLAGS += -fprofile-arcs -ftest-coverage
ifneq ($(CFG),debug)
$(error You are not supposed to use coverage in release builds - read about it e.g. inlining issues, etc)
endif
endif

# Should we generate native FPU instructions for the SRC or not?
ifeq ($(FPU),0)
CFLAGS += -msoft-float
LDFLAGS += -msoft-float
endif

# A common link flag for all configurations
LDFLAGS += ${COMMON} -Wl,--gc-sections

all:	inform bin.$(SUFFIX)/${TARGET}

inform:
ifneq ($(CFG),release)
ifneq ($(CFG),debug)
	@echo " "
	@echo "Invalid or missing configuration (CFG) "$(CFG)" specified."
	@echo " "
	@echo "You must specify a configuration when running make, e.g."
	@echo " "
	@echo "    make CFG=debug LEON=leon3 FPU=1 V=1"
	@echo " "
	@echo "- Possible choices for CFG are 'release' and 'debug'"
	@echo "- Possible choices for LEON are 'leon2' and 'leon3' (default)"
	@echo "- Possible choices for FPU are '1' (native) and '0' (emulated) (default)"
	@echo "- Possible choices for V are '1' (show commands) and '0' (silent) (default)"
	@echo " "
	@exit 1
endif
endif


bin.$(SUFFIX)/${TARGET}: ${OBJ} | inform
	@mkdir -p $(dir $@)
ifeq ($(V),1)
	$(CC) -g -o $@ $^ ${LDFLAGS}
else
	@echo [LD] $@
	@$(CC) -g -o $@ $^ ${LDFLAGS}
endif
ifeq ($(CFG),release)
	@${CROSS_PREFIX}-objcopy --only-keep-debug $@ ${@}.debug
	@${CROSS_PREFIX}-strip $@
endif
	@echo Built with RTEMS at ${RTEMS_LIB} for ${LEON}.

objs.$(SUFFIX)/%.o: %.c
	@mkdir -p $(dir $@)
ifeq ($(V),1)
	$(CC) -c $(CFLAGS) -o $@ $<
else
	@echo [CC] $@
	@$(CC) -c $(CFLAGS) -o $@ $<
endif

deps.$(SUFFIX)/%.d: %.c
	@mkdir -p $(dir $@)
	@echo Generating dependencies for $<
	@set -e ; $(CDEP) -MM -MP $(INCLUDEFLAGS) $< > $@.$$$$; \
	sed 's,\($*\)\.o[ :]*,objs.$(SUFFIX)\/\1.o $@ : ,g' < $@.$$$$ > $@; \
	rm -f $@.$$$$

clean:
	@rm -rf deps.* objs.* bin.* coverage coverage.info

coverage-common:
	$(MAKE) clean
	$(MAKE) CFG=debug LEON=leon3 COVERAGE=1 FPU=1
	# Spawning QEMU with our coverage enabled binary
	~/tool-src/misc/qemu-Leon3/qemu-system-sparc -no-reboot    \
	  -nographic -M leon3_generic -m 64M -kernel  \
	  bin.debug.FPU.leon3.WITH_COVERAGE/fputest -gdb tcp::9976 -S &
	# Spawning GDB and running the coverage script
	sleep 2 ; sparc-rtems4.11-gdb -x contrib/coverage.gdb </dev/null

coverage:	coverage-common
	# Reporting the results
	mkdir -p coverage ;                                             \
	cd coverage ;                                                   \
	ln -s ../src ;                                                  \
	sparc-rtems4.11-gcov ../objs.debug.FPU.leon3.WITH_COV*/*gcda
	@echo
	@echo ==================================================
	@echo The coverage-annotated sources are under coverage/
	@echo ==================================================

coverage-html:	coverage-common
	lcov --gcov-tool sparc-rtems4.11-gcov --capture --directory .   \
	    --output-file coverage.info
	genhtml coverage.info --output-directory coverage
	@echo
	@echo "===================================================="
	@echo "Open coverage/index.html in your browser - e.g. via:"
	@echo
	@echo "    xdg-open coverage/index.html"
	@echo "===================================================="

.PHONY:	clean coverage coverage-html coverage-common

# Unless "make clean" is called, include the dependency files
# which are auto-generated. Don't fail if they are missing
# (-include), since they will be missing in the first invocation!
ifneq ($(MAKECMDGOALS),clean)
ifneq ($(CFG),)
-include ${DEP}
endif
endif