| giuliomoro@301 | 1 # Bela | 
| giuliomoro@64 | 2 # Low-latency, real-time audio and sensor processing on BeagleBone Black | 
| l@260 | 3 # (c) 2016 Andrew McPherson, Victor Zappi, Giulio Moro, Liam Donovan | 
| giuliomoro@64 | 4 # Centre for Digital Music, Queen Mary University of London | 
| giuliomoro@64 | 5 | 
| giuliomoro@64 | 6 # This Makefile is intended for use on the BeagleBone Black itself | 
| giuliomoro@64 | 7 # and not for cross-compiling | 
| giuliomoro@64 | 8 | 
| l@260 | 9 # set the project to be compiled by calling: make all PROJECT=<project_name> | 
| l@260 | 10 | 
| l@260 | 11 # if the PROJECT variable is not set, throw an error and exit | 
| giuliomoro@265 | 12 # otherwise, we could have unexpected data loss when calling clean without it | 
| l@260 | 13 ifndef PROJECT | 
| giuliomoro@287 | 14   ifndef EXAMPLE | 
| giuliomoro@302 | 15     $(warning PROJECT or EXAMPLE should be set in order to build) | 
| giuliomoro@287 | 16   endif | 
| l@260 | 17 endif | 
| l@260 | 18 | 
| giuliomoro@287 | 19 ifndef EXAMPLE | 
| giuliomoro@287 | 20   PROJECT_DIR := $(abspath projects/$(PROJECT)) | 
| giuliomoro@287 | 21 endif | 
| giuliomoro@287 | 22 | 
| giuliomoro@287 | 23 ifdef EXAMPLE | 
| giuliomoro@287 | 24   PROJECT?=exampleTempProject | 
| giuliomoro@287 | 25   PROJECT_DIR?=$(abspath projects/$(PROJECT)) | 
| giuliomoro@287 | 26   $(shell rm -rf $(PROJECT_DIR)) | 
| giuliomoro@287 | 27   $(shell cp -r examples/$(EXAMPLE) $(PROJECT_DIR)) | 
| giuliomoro@287 | 28 endif | 
| giuliomoro@326 | 29 OUTPUT_FILE=$(PROJECT_DIR)/$(PROJECT) | 
| giuliomoro@326 | 30 COMMAND_LINE_OPTIONS=$(CL) | 
| giuliomoro@326 | 31 RUN_COMMAND=$(OUTPUT_FILE) $(COMMAND_LINE_OPTIONS) | 
| giuliomoro@287 | 32 | 
| giuliomoro@302 | 33 SCREEN_NAME?=BeagleRT | 
| giuliomoro@302 | 34 | 
| giuliomoro@304 | 35 #TODO: run these lines only if the command is not syntax or | 
| giuliomoro@267 | 36 $(shell mkdir -p $(PROJECT_DIR)/build) | 
| giuliomoro@64 | 37 RM := rm -rf | 
| giuliomoro@64 | 38 STATIC_LIBS := ./libprussdrv.a ./libNE10.a | 
| l@260 | 39 LIBS := -lrt -lnative -lxenomai -lsndfile | 
| l@260 | 40 | 
| giuliomoro@244 | 41 # refresh library cache and check if libpd is there | 
| giuliomoro@326 | 42 #TEST_LIBPD := $(shell ldconfig; ldconfig -p | grep "libpd\.so")  # safest but slower way of checking | 
| giuliomoro@326 | 43 LIBPD_PATH = /usr/lib/libpd.so | 
| giuliomoro@326 | 44 TEST_LIBPD := $(shell which $(LIBPD_PATH)) | 
| giuliomoro@326 | 45 ifneq ($(strip $(TEST_LIBPD)), ) | 
| giuliomoro@244 | 46 # if libpd is there, link it in | 
| giuliomoro@244 | 47   LIBS += -lpd -lpthread_rt | 
| giuliomoro@244 | 48 endif | 
| giuliomoro@64 | 49 | 
| giuliomoro@241 | 50 CPP_FLAGS := -O3 -march=armv7-a -mtune=cortex-a8 -mfloat-abi=hard -mfpu=neon -ftree-vectorize | 
| giuliomoro@241 | 51 C_FLAGS := $(CPP_FLAGS) | 
| giuliomoro@241 | 52 | 
| giuliomoro@241 | 53 ifndef COMPILER | 
| giuliomoro@241 | 54 # check whether clang is installed | 
| giuliomoro@295 | 55 #    TEST_COMPILER := $(shell which clang) | 
| giuliomoro@302 | 56   ifneq ($(strip $(TEST_COMPILER)), ) | 
| giuliomoro@302 | 57     if it is installed, use it | 
| giuliomoro@302 | 58     COMPILER := clang | 
| giuliomoro@302 | 59   else | 
| giuliomoro@241 | 60     COMPILER := gcc | 
| giuliomoro@302 | 61   endif | 
| giuliomoro@241 | 62 endif | 
| giuliomoro@241 | 63 | 
| giuliomoro@241 | 64 ifeq ($(COMPILER), clang) | 
| giuliomoro@306 | 65   CC=clang | 
| giuliomoro@306 | 66   CXX=clang++ | 
| giuliomoro@302 | 67   CPP_FLAGS += -DNDEBUG | 
| giuliomoro@302 | 68   C_FLAGS += -DNDEBUG | 
| giuliomoro@241 | 69 else | 
| giuliomoro@241 | 70   CC=gcc | 
| giuliomoro@241 | 71   CXX=g++ | 
| giuliomoro@241 | 72   CPP_FLAGS += --fast-math | 
| giuliomoro@241 | 73   C_FLAGS += --fast-math | 
| giuliomoro@241 | 74 endif | 
| giuliomoro@241 | 75 | 
| l@260 | 76 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 | 77 | 
| l@260 | 78 ASM_SRCS := $(wildcard $(PROJECT_DIR)/*.S) | 
| l@260 | 79 ASM_OBJS := $(addprefix $(PROJECT_DIR)/build/,$(notdir $(ASM_SRCS:.S=.o))) | 
| l@260 | 80 ASM_DEPS := $(addprefix $(PROJECT_DIR)/build/,$(notdir $(ASM_SRCS:.S=.d))) | 
| giuliomoro@64 | 81 | 
| l@260 | 82 C_SRCS := $(wildcard $(PROJECT_DIR)/*.c) | 
| l@260 | 83 C_OBJS := $(addprefix $(PROJECT_DIR)/build/,$(notdir $(C_SRCS:.c=.o))) | 
| l@260 | 84 C_DEPS := $(addprefix $(PROJECT_DIR)/build/,$(notdir $(C_SRCS:.c=.d))) | 
| giuliomoro@64 | 85 | 
| l@260 | 86 CPP_SRCS := $(wildcard $(PROJECT_DIR)/*.cpp) | 
| l@260 | 87 CPP_OBJS := $(addprefix $(PROJECT_DIR)/build/,$(notdir $(CPP_SRCS:.cpp=.o))) | 
| l@260 | 88 CPP_DEPS := $(addprefix $(PROJECT_DIR)/build/,$(notdir $(CPP_SRCS:.cpp=.d))) | 
| giuliomoro@64 | 89 | 
| giuliomoro@301 | 90 # Core Bela sources | 
| giuliomoro@186 | 91 CORE_CPP_SRCS = $(filter-out core/default_main.cpp, $(wildcard core/*.cpp)) | 
| giuliomoro@186 | 92 CORE_OBJS := $(addprefix build/core/,$(notdir $(CORE_CPP_SRCS:.cpp=.o))) | 
| giuliomoro@186 | 93 CORE_CPP_DEPS := $(addprefix build/core/,$(notdir $(CORE_CPP_SRCS:.cpp=.d))) | 
| giuliomoro@64 | 94 | 
| andrewm@318 | 95 CORE_ASM_SRCS := $(wildcard core/*.S) | 
| andrewm@318 | 96 CORE_ASM_OBJS := $(addprefix build/core/,$(notdir $(CORE_ASM_SRCS:.S=.o))) | 
| andrewm@318 | 97 CORE_ASM_DEPS := $(addprefix build/core/,$(notdir $(CORE_ASM_SRCS:.S=.d))) | 
| andrewm@318 | 98 | 
| andrewm@318 | 99 | 
| giuliomoro@64 | 100 # Objects for a system-supplied default main() file, if the user | 
| giuliomoro@64 | 101 # only wants to provide the render functions. | 
| giuliomoro@64 | 102 DEFAULT_MAIN_CPP_SRCS := ./core/default_main.cpp | 
| giuliomoro@64 | 103 DEFAULT_MAIN_OBJS := ./build/core/default_main.o | 
| giuliomoro@64 | 104 DEFAULT_MAIN_CPP_DEPS := ./build/core/default_main.d | 
| andrewm@68 | 105 | 
| giuliomoro@301 | 106 # all = build Bela | 
| andrewm@69 | 107 all: SYNTAX_FLAG := | 
| giuliomoro@301 | 108 all: Bela | 
| andrewm@68 | 109 | 
| giuliomoro@301 | 110 # debug = buildBela debug | 
| giuliomoro@209 | 111 debug: CPP_FLAGS=-g | 
| giuliomoro@209 | 112 debug: C_FLAGS=-g | 
| giuliomoro@209 | 113 debug: all | 
| giuliomoro@209 | 114 | 
| andrewm@69 | 115 # syntax = check syntax | 
| andrewm@69 | 116 syntax: SYNTAX_FLAG := -fsyntax-only | 
| l@260 | 117 syntax: SYNTAX | 
| andrewm@69 | 118 | 
| giuliomoro@302 | 119 | 
| giuliomoro@302 | 120 | 
| giuliomoro@301 | 121 # Rule for Bela core C++ files | 
| giuliomoro@64 | 122 build/core/%.o: ./core/%.cpp | 
| l@260 | 123 	@echo 'Building $(notdir $<)...' | 
| l@260 | 124 #	@echo 'Invoking: C++ Compiler' | 
| l@260 | 125 	@$(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 | 126 	@echo ' ...done' | 
| giuliomoro@64 | 127 	@echo ' ' | 
| andrewm@68 | 128 | 
| andrewm@318 | 129 # Rule for Bela core ASM files | 
| andrewm@318 | 130 build/core/%.o: ./core/%.S | 
| andrewm@318 | 131 	@echo 'Building $(notdir $<)...' | 
| andrewm@318 | 132 #	@echo 'Invoking: GCC Assembler' | 
| andrewm@318 | 133 	@as  -o "$@" "$<" | 
| andrewm@318 | 134 	@echo ' ...done' | 
| andrewm@318 | 135 	@echo ' ' | 
| andrewm@318 | 136 | 
| giuliomoro@64 | 137 # Rule for user-supplied C++ files | 
| l@260 | 138 $(PROJECT_DIR)/build/%.o: $(PROJECT_DIR)/%.cpp | 
| l@260 | 139 	@echo 'Building $(notdir $<)...' | 
| l@260 | 140 #	@echo 'Invoking: C++ Compiler' | 
| l@260 | 141 	@$(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 | 142 	@echo ' ...done' | 
| giuliomoro@64 | 143 	@echo ' ' | 
| andrewm@68 | 144 | 
| giuliomoro@64 | 145 # Rule for user-supplied C files | 
| l@260 | 146 $(PROJECT_DIR)/build/%.o: $(PROJECT_DIR)/%.c | 
| l@260 | 147 	@echo 'Building $(notdir $<)...' | 
| l@260 | 148 #	@echo 'Invoking: C Compiler' | 
| giuliomoro@287 | 149 	$(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 | 150 	@echo ' ...done' | 
| giuliomoro@64 | 151 	@echo ' ' | 
| andrewm@68 | 152 | 
| giuliomoro@64 | 153 # Rule for user-supplied assembly files | 
| l@260 | 154 $(PROJECT_DIR)/build/%.o: $(PROJECT_DIR)/%.S | 
| l@260 | 155 	@echo 'Building $(notdir $<)...' | 
| l@260 | 156 #	@echo 'Invoking: GCC Assembler' | 
| l@260 | 157 	@as  -o "$@" "$<" | 
| l@260 | 158 	@echo ' ...done' | 
| giuliomoro@64 | 159 	@echo ' ' | 
| giuliomoro@64 | 160 | 
| giuliomoro@64 | 161 # This is a nasty kludge: we want to be able to optionally link in a default | 
| giuliomoro@64 | 162 # main file if the user hasn't supplied one. We check for the presence of the main() | 
| giuliomoro@64 | 163 # function, and conditionally call one of two recursive make targets depending on whether | 
| giuliomoro@64 | 164 # we want to link in the default main file or not. The kludge is the mess of a shell script | 
| giuliomoro@64 | 165 # line below. Surely there's a better way to do this? | 
| giuliomoro@326 | 166 $(OUTPUT_FILE): $(CORE_ASM_OBJS) $(CORE_OBJS) $(ASM_OBJS) $(C_OBJS) $(CPP_OBJS) $(STATIC_LIBS) $(DEFAULT_MAIN_OBJS) | 
| giuliomoro@287 | 167 	$(eval DEFAULT_MAIN_CONDITIONAL := $(shell bash -c 'if [ `nm $(PROJECT_DIR)/build/*.o | grep -w T | grep -w main | wc -l` == '0' ]; then echo "$(DEFAULT_MAIN_OBJS)"; else echo ""; fi')) | 
| giuliomoro@287 | 168 	@echo 'Invoking: C++ linker' | 
| andrewm@318 | 169 	@$(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_ASM_OBJS) $(CORE_OBJS) $(DEFAULT_MAIN_CONDITIONAL) $(ASM_OBJS) $(C_OBJS) $(CPP_OBJS) $(STATIC_LIBS) $(LIBS) | 
| giuliomoro@287 | 170 	@echo 'Finished building target: $@' | 
| andrewm@68 | 171 | 
| l@260 | 172 # Other Targets: | 
| l@260 | 173 # This rule compiles c and c++ source files without output or linking | 
| l@260 | 174 SYNTAX: $(C_OBJS) $(CPP_OBJS) | 
| giuliomoro@64 | 175 | 
| l@260 | 176 # Remove the project's build objects & binary | 
| l@260 | 177 projectclean: | 
| giuliomoro@326 | 178 	-$(RM) $(PROJECT_DIR)/build/* $(OUTPUT_FILE) | 
| giuliomoro@64 | 179 	-@echo ' ' | 
| giuliomoro@64 | 180 | 
| giuliomoro@301 | 181 # Remove all the built objects, including the core Bela objects | 
| giuliomoro@64 | 182 distclean: | 
| giuliomoro@326 | 183 	-$(RM) build/source/* $(CORE_OBJS) $(CORE_CPP_DEPS) $(DEFAULT_MAIN_OBJS) $(DEFAULT_MAIN_CPP_DEPS) $(OUTPUT_FILE) | 
| giuliomoro@64 | 184 	-@echo ' ' | 
| giuliomoro@64 | 185 | 
| giuliomoro@326 | 186 Bela: $(OUTPUT_FILE) | 
| giuliomoro@326 | 187 | 
| giuliomoro@302 | 188 runfg: run | 
| giuliomoro@326 | 189 run: stop Bela | 
| giuliomoro@313 | 190 	echo "Running $(RUN_COMMAND)" | 
| giuliomoro@313 | 191 	$(RUN_COMMAND) | 
| giuliomoro@302 | 192 runscreen: stop $(OUTPUT_FILE) | 
| giuliomoro@313 | 193 	echo "Running $(RUN_COMMAND) in a screen" | 
| giuliomoro@313 | 194 	screen -S $(SCREEN_NAME) -d -m $(RUN_COMMAND) | 
| giuliomoro@313 | 195 runscreenfg: stop $(OUTPUT_FILE) | 
| giuliomoro@313 | 196 	echo "Running $(RUN_COMMAND) in a screen" | 
| giuliomoro@313 | 197 	screen -S $(SCREEN_NAME) -m $(RUN_COMMAND) | 
| giuliomoro@313 | 198 FIFO_NAME=/tmp/belafifo | 
| giuliomoro@313 | 199 runscreenfifo: stop $(OUTPUT_FILE) | 
| giuliomoro@313 | 200 	@echo "Running $(RUN_COMMAND), piping output to $(FIFO_NAME)" | 
| giuliomoro@313 | 201 	@rm -rf $(FIFO_NAME) | 
| giuliomoro@313 | 202 	@mkfifo $(FIFO_NAME) | 
| giuliomoro@315 | 203 	@screen -S $(SCREEN_NAME) -d -m stdbuf -e 0 -i 0 -o 0 bash -c "$(RUN_COMMAND) &>  $(FIFO_NAME)" | 
| giuliomoro@315 | 204 	@cat /tmp/belafifo | 
| giuliomoro@302 | 205 | 
| giuliomoro@302 | 206 BELA_AUDIO_THREAD_NAME=beaglert-audio | 
| giuliomoro@302 | 207 stop: | 
| giuliomoro@302 | 208 	@PID=`grep $(BELA_AUDIO_THREAD_NAME) /proc/xenomai/stat | cut -d " " -f 5 | sed s/\s//g`; if [ -z $$PID ]; then echo "No process to kill"; else echo "Killing old Bela process $$PID"; kill -2 $$PID; fi; screen -X -S $(SCREEN_NAME) quit > /dev/null; exit 0; | 
| giuliomoro@64 | 209 # Remove only the user-generated objects | 
| l@260 | 210 #clean: | 
| giuliomoro@301 | 211 #	-$(RM) build/source/* Bela | 
| l@260 | 212 #	-@echo ' ' | 
| giuliomoro@64 | 213 | 
| giuliomoro@64 | 214 post-build: | 
| giuliomoro@64 | 215 # Nothing to do here (for now) | 
| giuliomoro@64 | 216 | 
| giuliomoro@326 | 217 .PHONY: all clean distclean projectclean dependents debug run runfg runscreen stop | 
| giuliomoro@64 | 218 .SECONDARY: post-build |