giuliomoro@301: # Bela 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: giuliomoro@386: .DEFAULT_GOAL := Bela giuliomoro@386: giuliomoro@423: NO_PROJECT_TARGETS=help coreclean distclean stop nostartup idestart idestop idestartup idenostartup connect ideconnect giuliomoro@385: NO_PROJECT_TARGETS_MESSAGE=PROJECT or EXAMPLE should be set for all targets except: $(NO_PROJECT_TARGETS) giuliomoro@424: # list of targets that automatically activate the QUIET=true flag giuliomoro@424: QUIET_TARGETS=runide giuliomoro@424: giuliomoro@385: # Type `$ make help` to get a description of the functionalities of this Makefile. giuliomoro@385: help: ## Show this help giuliomoro@385: @echo 'Usage: make [target] CL=[command line options] [PROJECT=[projectName] | EXAMPLE=[exampleName]]' giuliomoro@386: @printf "\n$(NO_PROJECT_TARGETS_MESSAGE)\n\n" giuliomoro@386: @echo 'Targets: (default: $(.DEFAULT_GOAL))' giuliomoro@385: @fgrep -h "##" $(MAKEFILE_LIST) | fgrep -v fgrep | sed -e 's/^\(.*\): .*##\(.*\)/\1:#\2/' | column -t -c 2 -s '#' l@260: giuliomoro@385: # PROJECT or EXAMPLE must be set for targets that are not included in NO_PROJECT_TARGETS giuliomoro@374: ifeq (,$(filter $(NO_PROJECT_TARGETS),$(MAKECMDGOALS))) giuliomoro@374: ifndef PROJECT giuliomoro@374: ifndef EXAMPLE giuliomoro@385: $(error $(NO_PROJECT_TARGETS_MESSAGE)) giuliomoro@374: endif giuliomoro@287: endif l@260: endif l@260: giuliomoro@374: # if we are building an example, just copy it to the projects/ folder giuliomoro@386: # and then treat it as a project giuliomoro@374: ifdef EXAMPLE giuliomoro@394: #you can alternatively specify PROJECT= along with EXAMPLE= giuliomoro@374: PROJECT?=exampleTempProject giuliomoro@374: PROJECT_DIR?=$(abspath projects/$(PROJECT)) andrewm@380: $(shell mkdir -p $(abspath projects)) giuliomoro@374: $(shell rm -rf $(PROJECT_DIR)) giuliomoro@374: $(shell cp -r examples/$(EXAMPLE) $(PROJECT_DIR)) giuliomoro@374: else giuliomoro@287: PROJECT_DIR := $(abspath projects/$(PROJECT)) giuliomoro@287: endif giuliomoro@287: giuliomoro@374: ifdef PROJECT giuliomoro@393: $(shell mkdir -p $(PROJECT_DIR)/build build/core) giuliomoro@287: endif giuliomoro@374: giuliomoro@331: OUTPUT_FILE?=$(PROJECT_DIR)/$(PROJECT) giuliomoro@331: COMMAND_LINE_OPTIONS?=$(CL) giuliomoro@331: RUN_COMMAND?=$(OUTPUT_FILE) $(COMMAND_LINE_OPTIONS) giuliomoro@396: RUN_IDE_COMMAND?=stdbuf -i0 -o0 -e0 $(RUN_COMMAND) giuliomoro@401: BELA_STARTUP_SCRIPT?=/root/Bela_startup.sh giuliomoro@359: BELA_AUDIO_THREAD_NAME?=bela-audio giuliomoro@377: SCREEN_NAME?=Bela giuliomoro@399: BELA_IDE_STARTUP_SCRIPT?=/root/Bela_node.sh giuliomoro@393: BELA_IDE_HOME?=/root/Bela/IDE giuliomoro@400: # A bug in this version of screen forces us to use two screen names which beginning substrings do not match (Bela, Bela-IDE would cause problems) giuliomoro@400: BELA_IDE_SCREEN_NAME?=IDE-Bela giuliomoro@401: BELA_IDE_RUN_COMMAND?=cd $(BELA_IDE_HOME) && screen -S $(BELA_IDE_SCREEN_NAME) -d -m /usr/local/bin/node index.js giuliomoro@393: BELA_IDE_STOP_COMMAND?=screen -X -S $(BELA_IDE_SCREEN_NAME) quit > /dev/null giuliomoro@374: giuliomoro@424: ifneq (,$(filter $(QUIET_TARGETS),$(MAKECMDGOALS))) giuliomoro@424: QUIET=true giuliomoro@424: endif giuliomoro@424: QUIET?=false giuliomoro@424: 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@326: #TEST_LIBPD := $(shell ldconfig; ldconfig -p | grep "libpd\.so") # safest but slower way of checking giuliomoro@326: LIBPD_PATH = /usr/lib/libpd.so giuliomoro@326: TEST_LIBPD := $(shell which $(LIBPD_PATH)) giuliomoro@326: ifneq ($(strip $(TEST_LIBPD)), ) 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@385: TEST_COMPILER := $(shell which clang) giuliomoro@302: ifneq ($(strip $(TEST_COMPILER)), ) giuliomoro@385: #if it is installed, use it giuliomoro@302: COMPILER := clang giuliomoro@302: else giuliomoro@241: COMPILER := gcc giuliomoro@302: endif giuliomoro@241: endif giuliomoro@241: giuliomoro@241: ifeq ($(COMPILER), clang) giuliomoro@306: CC=clang giuliomoro@306: CXX=clang++ giuliomoro@302: CPP_FLAGS += -DNDEBUG giuliomoro@302: C_FLAGS += -DNDEBUG 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@383: PROJECT_OBJS = $(ASM_OBJS) $(C_OBJS) $(CPP_OBJS) giuliomoro@383: giuliomoro@301: # Core Bela 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: andrewm@318: CORE_ASM_SRCS := $(wildcard core/*.S) andrewm@318: CORE_ASM_OBJS := $(addprefix build/core/,$(notdir $(CORE_ASM_SRCS:.S=.o))) andrewm@318: CORE_ASM_DEPS := $(addprefix build/core/,$(notdir $(CORE_ASM_SRCS:.S=.d))) andrewm@318: andrewm@318: 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@334: Bela: ## Builds the Bela program with all the opimizations giuliomoro@334: Bela: $(OUTPUT_FILE) giuliomoro@334: giuliomoro@301: # all = build Bela giuliomoro@334: all: ## Same as Bela andrewm@69: all: SYNTAX_FLAG := giuliomoro@301: all: Bela andrewm@68: giuliomoro@301: # debug = buildBela debug giuliomoro@334: debug: ## Same as Bela but with debug flags and no optimizations giuliomoro@209: debug: CPP_FLAGS=-g giuliomoro@209: debug: C_FLAGS=-g giuliomoro@209: debug: all giuliomoro@209: andrewm@69: # syntax = check syntax giuliomoro@334: syntax: ## Only checks syntax andrewm@69: syntax: SYNTAX_FLAG := -fsyntax-only l@260: syntax: SYNTAX andrewm@69: giuliomoro@301: # Rule for Bela core C++ files giuliomoro@64: build/core/%.o: ./core/%.cpp l@260: @echo 'Building $(notdir $<)...' giuliomoro@386: # @echo 'Invoking: C++ Compiler $(CXX)' 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: andrewm@318: # Rule for Bela core ASM files andrewm@318: build/core/%.o: ./core/%.S andrewm@318: @echo 'Building $(notdir $<)...' andrewm@318: # @echo 'Invoking: GCC Assembler' andrewm@318: @as -o "$@" "$<" andrewm@318: @echo ' ...done' andrewm@318: @echo ' ' andrewm@318: giuliomoro@64: # Rule for user-supplied C++ files l@260: $(PROJECT_DIR)/build/%.o: $(PROJECT_DIR)/%.cpp l@260: @echo 'Building $(notdir $<)...' l@389: # @echo 'Invoking: C++ Compiler $(CXX)' 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 $<)...' giuliomoro@386: # @echo 'Invoking: C Compiler $(CC)' giuliomoro@287: $(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@383: $(OUTPUT_FILE): $(CORE_ASM_OBJS) $(CORE_OBJS) $(PROJECT_OBJS) $(STATIC_LIBS) $(DEFAULT_MAIN_OBJS) giuliomoro@383: $(eval DEFAULT_MAIN_CONDITIONAL := $(shell bash -c 'if [ `nm $(PROJECT_OBJS) | grep -w T | grep -w main | wc -l` == '0' ]; then echo "$(DEFAULT_MAIN_OBJS)"; else echo ""; fi')) l@387: @echo 'Linking...' andrewm@318: @$(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) l@387: @echo ' ...done' l@387: 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: giuliomoro@417: projectclean:## Remove the PROJECT's build objects & binary giuliomoro@326: -$(RM) $(PROJECT_DIR)/build/* $(OUTPUT_FILE) giuliomoro@64: -@echo ' ' giuliomoro@64: giuliomoro@386: clean: ## Same as projectclean giuliomoro@386: clean: projectclean giuliomoro@386: giuliomoro@386: coreclean: ## Remove the core's build objects giuliomoro@394: -$(RM) build/core/* giuliomoro@386: giuliomoro@386: prompt: giuliomoro@386: @printf "Warning: you are about to DELETE the projects/ folder and its content. This operation cannot be undone. Continue? (y/N) " giuliomoro@386: @read REPLY; if [ $$REPLY != y ] && [ $$REPLY != Y ]; then echo "Aborting..."; exit 1; fi giuliomoro@386: giuliomoro@386: distclean: ## Restores the Bela folder to a pristine state: remove all the projects source and the built objects, including the core Bela objects. giuliomoro@386: distclean: prompt distcleannoprompt giuliomoro@386: giuliomoro@386: distcleannoprompt: ## Same as distclean, but does not prompt for confirmation. Use with care. giuliomoro@326: -$(RM) build/source/* $(CORE_OBJS) $(CORE_CPP_DEPS) $(DEFAULT_MAIN_OBJS) $(DEFAULT_MAIN_CPP_DEPS) $(OUTPUT_FILE) giuliomoro@64: -@echo ' ' giuliomoro@64: giuliomoro@302: runfg: run giuliomoro@334: run: ## Run PROJECT in the foreground giuliomoro@326: run: stop Bela giuliomoro@327: @echo "Running $(RUN_COMMAND)" giuliomoro@404: @sync& cd $(PROJECT_DIR) && $(RUN_COMMAND) giuliomoro@424: runide: ## Run PROJECT for IDE (foreground, no buffering) giuliomoro@424: runide: stop Bela giuliomoro@404: @sync& cd $(PROJECT_DIR) && $(RUN_IDE_COMMAND) giuliomoro@334: runscreen: ## Run PROJECT in the background (detached screen) giuliomoro@302: runscreen: stop $(OUTPUT_FILE) giuliomoro@327: @echo "Running $(RUN_COMMAND) in a screen" giuliomoro@327: @cd $(PROJECT_DIR) && screen -S $(SCREEN_NAME) -d -m $(RUN_COMMAND) giuliomoro@334: runscreenfg: ## Run PROJECT in a screen in the foreground (can detach with ctrl-a ctrl-d) giuliomoro@313: runscreenfg: stop $(OUTPUT_FILE) giuliomoro@327: @echo "Running $(RUN_COMMAND) in a screen" giuliomoro@327: @cd $(PROJECT_DIR) && screen -S $(SCREEN_NAME) -m $(RUN_COMMAND) giuliomoro@313: FIFO_NAME=/tmp/belafifo giuliomoro@334: runscreenfifo: ## Same as runscreen, but stdout and stderr are piped to the foreground through a fifo giuliomoro@313: runscreenfifo: stop $(OUTPUT_FILE) giuliomoro@313: @echo "Running $(RUN_COMMAND), piping output to $(FIFO_NAME)" giuliomoro@313: @rm -rf $(FIFO_NAME) giuliomoro@313: @mkfifo $(FIFO_NAME) giuliomoro@327: @cd $(PROJECT_DIR) giuliomoro@315: @screen -S $(SCREEN_NAME) -d -m stdbuf -e 0 -i 0 -o 0 bash -c "$(RUN_COMMAND) &> $(FIFO_NAME)" giuliomoro@315: @cat /tmp/belafifo giuliomoro@302: giuliomoro@331: STARTUP_COMMAND=printf "\#!/bin/sh\n\#\n\# This file is autogenerated by Bela. Do not edit!\n\necho Running Bela...\nscreen -S $(SCREEN_NAME) -d -m %s $(RUN_COMMAND) %s\n" giuliomoro@334: nostartup: ## No Bela project runs at startup giuliomoro@331: nostartup: andrewm@391: @echo "Disabling Bela at startup..." giuliomoro@344: @printf "#!/bin/sh\n#\n\n# This file is autogenerated by Bela. Do not edit!\n\n# Run on startup disabled -- nothing to do here\n" > $(BELA_STARTUP_SCRIPT) giuliomoro@331: giuliomoro@334: startuploop: ## Makes PROJECT run at startup and restarts it if it crashes giuliomoro@331: startuploop: Bela giuliomoro@331: @echo "Enabling Bela at startup in a loop..." giuliomoro@331: @$(STARTUP_COMMAND) 'bash -c "while sleep 0.5 ; do echo Running Bela...;' '; done"' > $(BELA_STARTUP_SCRIPT) giuliomoro@331: giuliomoro@334: startup: ## Makes PROJECT run at startup giuliomoro@331: startup: Bela giuliomoro@331: @echo "Enabling Bela at startup..." giuliomoro@331: @$(STARTUP_COMMAND) > $(BELA_STARTUP_SCRIPT) giuliomoro@426: @chmod +x $(BELA_STARTUP_SCRIPT) giuliomoro@334: stop: ## Stops any Bela program that is currently running giuliomoro@302: stop: giuliomoro@424: @PID=`grep $(BELA_AUDIO_THREAD_NAME) /proc/xenomai/stat | cut -d " " -f 5 | sed s/\s//g`; if [ -z $$PID ]; then [ $(QUIET) = true ] || echo "No process to kill"; else [ $(QUIET) = true ] || echo "Killing old Bela process $$PID"; kill -2 $$PID; fi; screen -X -S $(SCREEN_NAME) quit > /dev/null; exit 0; giuliomoro@64: giuliomoro@417: connect: ## Connects to the running Bela program (if any), can detach with ctrl-a ctrl-d. giuliomoro@417: @screen -r -S $(SCREEN_NAME) giuliomoro@417: giuliomoro@409: idestart: ## Starts the on-board IDE giuliomoro@410: idestart: idestop giuliomoro@410: @printf "Starting IDE..." giuliomoro@410: @$(BELA_IDE_RUN_COMMAND) giuliomoro@410: @printf "done\n" giuliomoro@393: giuliomoro@393: idestop: ## Stops the on-board IDE giuliomoro@410: @printf "Stopping currently running IDE..." giuliomoro@410: @screen -X -S $(BELA_IDE_SCREEN_NAME) quit > /dev/null; exit 0; giuliomoro@410: @printf "done\n" giuliomoro@393: giuliomoro@393: BELA_IDE_STARTUP_COMMAND=printf "\#!/bin/sh\n\#\n\# This file is autogenerated by Bela. Do not edit!\n\necho Running the Bela IDE...\n$(BELA_IDE_RUN_COMMAND)\n" > $(BELA_IDE_STARTUP_SCRIPT) giuliomoro@393: giuliomoro@399: idestartup: ## Enables the IDE at startup giuliomoro@393: @echo "Enabling the IDE at startup" giuliomoro@414: @$(BELA_IDE_STARTUP_COMMAND) giuliomoro@393: @chmod +x $(BELA_IDE_STARTUP_SCRIPT) giuliomoro@393: giuliomoro@393: idenostartup: ## Disables the IDE at startup giuliomoro@393: @echo "Disabling the IDE at startup" giuliomoro@393: @printf "#!/bin/sh\n#\n\n# This file is autogenerated by Bela. Do not edit!\n\n# The Bela IDE is disabled on startup.\n" > $(BELA_IDE_STARTUP_SCRIPT) giuliomoro@417: giuliomoro@417: ideconnect: ## Brings in the foreground the IDE that currently is running in a screen (if any), can detach with ctrl-a ctrl-d. giuliomoro@417: @screen -r -S $(BELA_IDE_SCREEN_NAME) giuliomoro@417: giuliomoro@417: .PHONY: all clean distclean help projectclean nostartup startup startuploop debug run runfg runscreen runscreenfg runscreenfifo stop idestart idestop idestartup idenostartup ideconnect connect