giuliomoro@64: # BeagleRT
giuliomoro@64: # Low-latency, real-time audio and sensor processing on BeagleBone Black
l@260: # (c) 2016 Andrew McPherson, Victor Zappi, Giulio Moro, Liam Donovan
giuliomoro@64: # Centre for Digital Music, Queen Mary University of London
giuliomoro@64: 
giuliomoro@64: # This Makefile is intended for use on the BeagleBone Black itself
giuliomoro@64: # and not for cross-compiling
giuliomoro@64: 
l@260: # set the project to be compiled by calling: make all PROJECT=<project_name>
l@260: 
l@260: # if the PROJECT variable is not set, throw an error and exit
l@260: # otherwise, calling make clean without setting PROJECT results in entire projects directory being rm -rf'ed
l@260: ifndef PROJECT
l@260: 	$(error PROJECT is not set)
l@260: endif
l@260: 
l@260: PROJECT_DIR := $(abspath projects/$(PROJECT))
l@260: 
giuliomoro@64: RM := rm -rf
giuliomoro@64: STATIC_LIBS := ./libprussdrv.a ./libNE10.a
l@260: LIBS := -lrt -lnative -lxenomai -lsndfile
l@260: 
giuliomoro@244: # refresh library cache and check if libpd is there
giuliomoro@244: TEST_LIBPD := $(shell ldconfig; ldconfig -p | grep "libpd\.so") 
giuliomoro@244: ifeq ($(strip $(TEST_LIBPD)), )
giuliomoro@244: else
giuliomoro@244: # if libpd is there, link it in
giuliomoro@244:   LIBS += -lpd -lpthread_rt
giuliomoro@244: endif
giuliomoro@64: 
giuliomoro@241: CPP_FLAGS := -O3 -march=armv7-a -mtune=cortex-a8 -mfloat-abi=hard -mfpu=neon -ftree-vectorize 
giuliomoro@241: C_FLAGS := $(CPP_FLAGS)
giuliomoro@241: 
giuliomoro@241: ifndef COMPILER
giuliomoro@241: # check whether clang is installed
giuliomoro@241:     TEST_COMPILER := $(shell which clang)
giuliomoro@241:   ifneq ($(strip $(TEST_COMPILER)), )
giuliomoro@241:     # if it is installed, use it
giuliomoro@241:     COMPILER := clang
giuliomoro@241:   else
giuliomoro@241:     COMPILER := gcc
giuliomoro@241:   endif
giuliomoro@241: endif
giuliomoro@241: 
giuliomoro@241: ifeq ($(COMPILER), clang)
giuliomoro@241:   CC=clang
giuliomoro@241:   CXX=clang++
giuliomoro@241:   CPP_FLAGS +=
giuliomoro@244:   C_FLAGS +=
giuliomoro@241: else
giuliomoro@241:   CC=gcc
giuliomoro@241:   CXX=g++
giuliomoro@241:   CPP_FLAGS += --fast-math
giuliomoro@241:   C_FLAGS += --fast-math
giuliomoro@241: endif
giuliomoro@241: 
l@260: INCLUDES := -I$(PROJECT_DIR) -I./include -I/usr/include/ne10 -I/usr/xenomai/include -I/usr/arm-linux-gnueabihf/include/xenomai/include -I/usr/arm-linux-gnueabihf/include/ne10
andrewm@68: 
l@260: ASM_SRCS := $(wildcard $(PROJECT_DIR)/*.S)
l@260: ASM_OBJS := $(addprefix $(PROJECT_DIR)/build/,$(notdir $(ASM_SRCS:.S=.o)))
l@260: ASM_DEPS := $(addprefix $(PROJECT_DIR)/build/,$(notdir $(ASM_SRCS:.S=.d)))
giuliomoro@64: 
l@260: C_SRCS := $(wildcard $(PROJECT_DIR)/*.c)
l@260: C_OBJS := $(addprefix $(PROJECT_DIR)/build/,$(notdir $(C_SRCS:.c=.o)))
l@260: C_DEPS := $(addprefix $(PROJECT_DIR)/build/,$(notdir $(C_SRCS:.c=.d)))
giuliomoro@64: 
l@260: CPP_SRCS := $(wildcard $(PROJECT_DIR)/*.cpp)
l@260: CPP_OBJS := $(addprefix $(PROJECT_DIR)/build/,$(notdir $(CPP_SRCS:.cpp=.o)))
l@260: CPP_DEPS := $(addprefix $(PROJECT_DIR)/build/,$(notdir $(CPP_SRCS:.cpp=.d)))
giuliomoro@64: 
giuliomoro@64: # Core BeagleRT sources
giuliomoro@186: CORE_CPP_SRCS = $(filter-out core/default_main.cpp, $(wildcard core/*.cpp))
giuliomoro@186: CORE_OBJS := $(addprefix build/core/,$(notdir $(CORE_CPP_SRCS:.cpp=.o)))
giuliomoro@186: CORE_CPP_DEPS := $(addprefix build/core/,$(notdir $(CORE_CPP_SRCS:.cpp=.d)))
giuliomoro@64: 
giuliomoro@64: # Objects for a system-supplied default main() file, if the user
giuliomoro@64: # only wants to provide the render functions.
giuliomoro@64: DEFAULT_MAIN_CPP_SRCS := ./core/default_main.cpp
giuliomoro@64: DEFAULT_MAIN_OBJS := ./build/core/default_main.o
giuliomoro@64: DEFAULT_MAIN_CPP_DEPS := ./build/core/default_main.d
andrewm@68: 
giuliomoro@209: # all = build BeagleRT 
andrewm@69: all: SYNTAX_FLAG :=
giuliomoro@64: all: BeagleRT
andrewm@68: 
giuliomoro@209: # debug = buildBeagleRT debug
giuliomoro@209: debug: CPP_FLAGS=-g
giuliomoro@209: debug: C_FLAGS=-g
giuliomoro@209: debug: all
giuliomoro@209: 
andrewm@69: # syntax = check syntax
andrewm@69: syntax: SYNTAX_FLAG := -fsyntax-only
l@260: syntax: SYNTAX
andrewm@69: 
giuliomoro@64: # Rule for BeagleRT core C++ files
giuliomoro@64: build/core/%.o: ./core/%.cpp
l@260: 	@echo 'Building $(notdir $<)...'
l@260: #	@echo 'Invoking: C++ Compiler'
l@260: 	@$(CXX) $(SYNTAX_FLAG) $(INCLUDES) $(CPP_FLAGS) -Wall -c -fmessage-length=0 -U_FORTIFY_SOURCE -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@:%.o=%.d)" -o "$@" "$<"
l@260: 	@echo ' ...done'
giuliomoro@64: 	@echo ' '
andrewm@68: 
giuliomoro@64: # Rule for user-supplied C++ files
l@260: $(PROJECT_DIR)/build/%.o: $(PROJECT_DIR)/%.cpp
l@260: 	@echo 'Building $(notdir $<)...'
l@260: #	@echo 'Invoking: C++ Compiler'
l@260: 	@$(CXX) $(SYNTAX_FLAG) $(INCLUDES) $(CPP_FLAGS) -Wall -c -fmessage-length=0 -U_FORTIFY_SOURCE -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@:%.o=%.d)" -o "$@" "$<"
l@260: 	@echo ' ...done'
giuliomoro@64: 	@echo ' '
andrewm@68: 
giuliomoro@64: # Rule for user-supplied C files
l@260: $(PROJECT_DIR)/build/%.o: $(PROJECT_DIR)/%.c
l@260: 	@echo 'Building $(notdir $<)...'
l@260: #	@echo 'Invoking: C Compiler'
l@260: 	@$(CC) $(SYNTAX_FLAG) $(INCLUDES) $(C_FLAGS) -Wall -c -fmessage-length=0 -U_FORTIFY_SOURCE -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@:%.o=%.d)" -o "$@" "$<" -std=c99 
l@260: 	@echo ' ...done'
giuliomoro@64: 	@echo ' '
andrewm@68: 
giuliomoro@64: # Rule for user-supplied assembly files
l@260: $(PROJECT_DIR)/build/%.o: $(PROJECT_DIR)/%.S
l@260: 	@echo 'Building $(notdir $<)...'
l@260: #	@echo 'Invoking: GCC Assembler'
l@260: 	@as  -o "$@" "$<"
l@260: 	@echo ' ...done'
giuliomoro@64: 	@echo ' '
giuliomoro@64: 
giuliomoro@64: # This is a nasty kludge: we want to be able to optionally link in a default
giuliomoro@64: # main file if the user hasn't supplied one. We check for the presence of the main()
giuliomoro@64: # function, and conditionally call one of two recursive make targets depending on whether
giuliomoro@64: # we want to link in the default main file or not. The kludge is the mess of a shell script
giuliomoro@64: # line below. Surely there's a better way to do this?
giuliomoro@64: BeagleRT: $(CORE_OBJS) $(ASM_OBJS) $(C_OBJS) $(CPP_OBJS) $(STATIC_LIBS)
l@260: 	$(eval NEXT_TARGET := $(shell bash -c 'if [ `nm $(PROJECT_DIR)/build/*.o | grep -w T | grep -w main | wc -l` == '0' ]; then echo "BeagleRT_with_main"; else echo "BeagleRT_without_main"; fi'))
l@260: 	@$(MAKE) $(NEXT_TARGET)
l@260: #	@echo 'Finished building target: $@'
l@260: #	@echo ' '
l@260: #	$(MAKE) --no-print-directory post-build
andrewm@68: 
giuliomoro@64: # Rule for building BeagleRT including the default main file (no user-supplied main())
giuliomoro@64: BeagleRT_with_main: $(CORE_OBJS) $(DEFAULT_MAIN_OBJS) $(ASM_OBJS) $(C_OBJS) $(CPP_OBJS) $(STATIC_LIBS)
l@260: 	@echo 'Linking default main.cpp...'
l@260: #	@echo 'Invoking: C++ Linker'
l@260: 	@$(CXX) $(SYNTAX_FLAG) -L/usr/xenomai/lib -L/usr/arm-linux-gnueabihf/lib -L/usr/arm-linux-gnueabihf/lib/xenomai -L/usr/lib/arm-linux-gnueabihf -pthread -Wpointer-arith -o "$(PROJECT_DIR)/$(PROJECT)" $(CORE_OBJS) $(DEFAULT_MAIN_OBJS) $(ASM_OBJS) $(C_OBJS) $(CPP_OBJS) $(STATIC_LIBS) $(LIBS)
l@260: 	@echo ' ...done'
l@260: 	
giuliomoro@64: # Rule for building BeagleRT without the default main file (user-supplied main())
giuliomoro@64: BeagleRT_without_main: $(CORE_OBJS) $(ASM_OBJS) $(C_OBJS) $(CPP_OBJS) $(STATIC_LIBS) 
l@260: 	@echo 'Linking main.cpp from project...'
l@260: #	@echo 'Invoking: C++ Linker'
l@260: 	@$(CXX) $(SYNTAX_FLAG) -L/usr/xenomai/lib -L/usr/arm-linux-gnueabihf/lib -L/usr/arm-linux-gnueabihf/lib/xenomai -L/usr/lib/arm-linux-gnueabihf -pthread -Wpointer-arith -o "$(PROJECT_DIR)/$(PROJECT)" $(CORE_OBJS) $(ASM_OBJS) $(C_OBJS) $(CPP_OBJS) $(STATIC_LIBS) $(LIBS)
l@260: 	@echo ' ...done'
l@260: 	
l@260: # Other Targets:
l@260: # This rule compiles c and c++ source files without output or linking
l@260: SYNTAX: $(C_OBJS) $(CPP_OBJS)
giuliomoro@64: 
l@260: # Remove the project's build objects & binary
l@260: projectclean:
l@260: 	-$(RM) $(PROJECT_DIR)/build/* $(PROJECT_DIR)/$(PROJECT)
giuliomoro@64: 	-@echo ' '	
giuliomoro@64: 
giuliomoro@64: # Remove all the built objects, including the core BeagleRT objects
giuliomoro@64: distclean:
giuliomoro@64: 	-$(RM) build/source/* $(CORE_OBJS) $(CORE_CPP_DEPS) $(DEFAULT_MAIN_OBJS) $(DEFAULT_MAIN_CPP_DEPS) BeagleRT
giuliomoro@64: 	-@echo ' '
giuliomoro@64: 
giuliomoro@64: # Remove only the user-generated objects
l@260: #clean:
l@260: #	-$(RM) build/source/* BeagleRT
l@260: #	-@echo ' '
giuliomoro@64: 
giuliomoro@64: post-build:
giuliomoro@64: # Nothing to do here (for now)
giuliomoro@64: 
l@260: .PHONY: all clean distclean projectclean dependents debug
giuliomoro@64: .SECONDARY: post-build