view Makefile @ 453:4cccb13bf406 prerelease

Minor edits to Makefile
author Giulio Moro <giuliomoro@yahoo.it>
date Sun, 19 Jun 2016 22:36:38 +0100
parents fdb30affab1c
children 0567af952cfa
line wrap: on
line source
# Bela
# Low-latency, real-time audio and sensor processing on BeagleBone Black
# (c) 2016 Andrew McPherson, Victor Zappi, Giulio Moro, Liam Donovan
# Centre for Digital Music, Queen Mary University of London

# This Makefile is intended for use on the BeagleBone Black itself
# and not for cross-compiling

.DEFAULT_GOAL := Bela

NO_PROJECT_TARGETS=help coreclean distclean stop nostartup idestart idestop idestartup idenostartup connect ideconnect update checkupdate
NO_PROJECT_TARGETS_MESSAGE=PROJECT or EXAMPLE should be set for all targets except: $(NO_PROJECT_TARGETS)
# list of targets that automatically activate the QUIET=true flag
QUIET_TARGETS=runide

# Type `$ make help` to get a description of the functionalities of this Makefile.
help: ## Show this help
	@echo 'Usage: make [target] CL=[command line options] [PROJECT=[projectName] | EXAMPLE=[exampleName]]'
	@printf "\n$(NO_PROJECT_TARGETS_MESSAGE)\n\n"
	@echo 'Targets: (default: $(.DEFAULT_GOAL))'
	@fgrep -h "##" $(MAKEFILE_LIST) | fgrep -v fgrep | sed -e  's/^\(.*\): .*##\(.*\)/\1:#\2/' | column -t -c 2 -s '#'

# PROJECT or EXAMPLE must be set for targets that are not included in NO_PROJECT_TARGETS
ifeq (,$(filter $(NO_PROJECT_TARGETS),$(MAKECMDGOALS)))
  ifndef PROJECT
    ifndef EXAMPLE
      $(error $(NO_PROJECT_TARGETS_MESSAGE))
    endif
  endif
endif

# if we are building an example, just copy it to the projects/ folder
# and then treat it as a project
ifdef EXAMPLE
  #you can alternatively specify PROJECT= along with EXAMPLE=
  PROJECT?=exampleTempProject
  PROJECT_DIR?=$(abspath projects/$(PROJECT))
  $(shell mkdir -p $(abspath projects))
  $(shell rm -rf $(PROJECT_DIR))
  $(shell cp -r examples/$(EXAMPLE) $(PROJECT_DIR))
else
  PROJECT_DIR := $(abspath projects/$(PROJECT))
endif

ifdef PROJECT
  $(shell mkdir -p $(PROJECT_DIR)/build build/core)
endif

OUTPUT_FILE?=$(PROJECT_DIR)/$(PROJECT)
COMMAND_LINE_OPTIONS?=$(CL)
RUN_FROM?=$(PROJECT_DIR)
RUN_COMMAND?=$(OUTPUT_FILE) $(COMMAND_LINE_OPTIONS)
RUN_IDE_COMMAND?=stdbuf -i0 -o0 -e0 $(RUN_COMMAND)
BELA_STARTUP_SCRIPT?=/root/Bela_startup.sh
BELA_AUDIO_THREAD_NAME?=bela-audio 
SCREEN_NAME?=Bela
BELA_IDE_STARTUP_SCRIPT?=/root/Bela_node.sh
BELA_IDE_HOME?=/root/Bela/IDE
# 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)
BELA_IDE_SCREEN_NAME?=IDE-Bela
BELA_IDE_RUN_COMMAND?=cd $(BELA_IDE_HOME) && screen -S $(BELA_IDE_SCREEN_NAME) -d -m bash -c "while true; do /usr/local/bin/node index.js; sleep 0.5; done"
BELA_IDE_STOP_COMMAND?=screen -X -S $(BELA_IDE_SCREEN_NAME) quit > /dev/null 

ifneq (,$(filter $(QUIET_TARGETS),$(MAKECMDGOALS)))
  QUIET=true
endif
QUIET?=false

RM := rm -rf
STATIC_LIBS := ./libprussdrv.a ./libNE10.a
LIBS := -lrt -lnative -lxenomai -lsndfile

# refresh library cache and check if libpd is there
#TEST_LIBPD := $(shell ldconfig; ldconfig -p | grep "libpd\.so")  # safest but slower way of checking
LIBPD_PATH = /usr/lib/libpd.so
TEST_LIBPD := $(shell which $(LIBPD_PATH))
ifneq ($(strip $(TEST_LIBPD)), )
# if libpd is there, link it in
  LIBS += -lpd -lpthread_rt
endif

CPP_FLAGS := -O3 -march=armv7-a -mtune=cortex-a8 -mfloat-abi=hard -mfpu=neon -ftree-vectorize 
C_FLAGS := $(CPP_FLAGS)

ifndef COMPILER
# check whether clang is installed
  TEST_COMPILER := $(shell which clang)
  ifneq ($(strip $(TEST_COMPILER)), )
    #if it is installed, use it
    COMPILER := clang
  else
    COMPILER := gcc
  endif
endif

ifeq ($(COMPILER), clang)
  CC=clang
  CXX=clang++
  CPP_FLAGS += -DNDEBUG 
  C_FLAGS += -DNDEBUG
else
  CC=gcc
  CXX=g++
  CPP_FLAGS += --fast-math
  C_FLAGS += --fast-math
endif

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

ASM_SRCS := $(wildcard $(PROJECT_DIR)/*.S)
ASM_OBJS := $(addprefix $(PROJECT_DIR)/build/,$(notdir $(ASM_SRCS:.S=.o)))
ASM_DEPS := $(addprefix $(PROJECT_DIR)/build/,$(notdir $(ASM_SRCS:.S=.d)))

C_SRCS := $(wildcard $(PROJECT_DIR)/*.c)
C_OBJS := $(addprefix $(PROJECT_DIR)/build/,$(notdir $(C_SRCS:.c=.o)))
C_DEPS := $(addprefix $(PROJECT_DIR)/build/,$(notdir $(C_SRCS:.c=.d)))

CPP_SRCS := $(wildcard $(PROJECT_DIR)/*.cpp)
CPP_OBJS := $(addprefix $(PROJECT_DIR)/build/,$(notdir $(CPP_SRCS:.cpp=.o)))
CPP_DEPS := $(addprefix $(PROJECT_DIR)/build/,$(notdir $(CPP_SRCS:.cpp=.d)))

PROJECT_OBJS = $(ASM_OBJS) $(C_OBJS) $(CPP_OBJS)

# Core Bela sources
CORE_CPP_SRCS = $(filter-out core/default_main.cpp, $(wildcard core/*.cpp))
CORE_OBJS := $(addprefix build/core/,$(notdir $(CORE_CPP_SRCS:.cpp=.o)))
CORE_CPP_DEPS := $(addprefix build/core/,$(notdir $(CORE_CPP_SRCS:.cpp=.d)))

CORE_ASM_SRCS := $(wildcard core/*.S)
CORE_ASM_OBJS := $(addprefix build/core/,$(notdir $(CORE_ASM_SRCS:.S=.o)))
CORE_ASM_DEPS := $(addprefix build/core/,$(notdir $(CORE_ASM_SRCS:.S=.d)))


# Objects for a system-supplied default main() file, if the user
# only wants to provide the render functions.
DEFAULT_MAIN_CPP_SRCS := ./core/default_main.cpp
DEFAULT_MAIN_OBJS := ./build/core/default_main.o
DEFAULT_MAIN_CPP_DEPS := ./build/core/default_main.d

Bela: ## Builds the Bela program with all the opimizations
Bela: $(OUTPUT_FILE)

# all = build Bela 
all: ## Same as Bela
all: SYNTAX_FLAG :=
all: Bela

# debug = buildBela debug
debug: ## Same as Bela but with debug flags and no optimizations
debug: CPP_FLAGS=-g
debug: C_FLAGS=-g
debug: all

# syntax = check syntax
syntax: ## Only checks syntax
syntax: SYNTAX_FLAG := -fsyntax-only
syntax: SYNTAX

# Rule for Bela core C++ files
build/core/%.o: ./core/%.cpp
	@echo 'Building $(notdir $<)...'
#	@echo 'Invoking: C++ Compiler $(CXX)'
	@$(CXX) $(SYNTAX_FLAG) $(INCLUDES) $(CPP_FLAGS) -Wall -c -fmessage-length=0 -U_FORTIFY_SOURCE -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@:%.o=%.d)" -o "$@" "$<"
	@echo ' ...done'
	@echo ' '

# Rule for Bela core ASM files
build/core/%.o: ./core/%.S
	@echo 'Building $(notdir $<)...'
#	@echo 'Invoking: GCC Assembler'
	@as  -o "$@" "$<"
	@echo ' ...done'
	@echo ' '

# Rule for user-supplied C++ files
$(PROJECT_DIR)/build/%.o: $(PROJECT_DIR)/%.cpp
	@echo 'Building $(notdir $<)...'
#	@echo 'Invoking: C++ Compiler $(CXX)'
	@$(CXX) $(SYNTAX_FLAG) $(INCLUDES) $(CPP_FLAGS) -Wall -c -fmessage-length=0 -U_FORTIFY_SOURCE -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@:%.o=%.d)" -o "$@" "$<"
	@echo ' ...done'
	@echo ' '

# Rule for user-supplied C files
$(PROJECT_DIR)/build/%.o: $(PROJECT_DIR)/%.c
	@echo 'Building $(notdir $<)...'
#	@echo 'Invoking: C Compiler $(CC)'
	@$(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 
	@echo ' ...done'
	@echo ' '

# Rule for user-supplied assembly files
$(PROJECT_DIR)/build/%.o: $(PROJECT_DIR)/%.S
	@echo 'Building $(notdir $<)...'
#	@echo 'Invoking: GCC Assembler'
	@as  -o "$@" "$<"
	@echo ' ...done'
	@echo ' '

# This is a nasty kludge: we want to be able to optionally link in a default
# main file if the user hasn't supplied one. We check for the presence of the main()
# function, and conditionally call one of two recursive make targets depending on whether
# we want to link in the default main file or not. The kludge is the mess of a shell script
# line below. Surely there's a better way to do this?
$(OUTPUT_FILE): $(CORE_ASM_OBJS) $(CORE_OBJS) $(PROJECT_OBJS) $(STATIC_LIBS) $(DEFAULT_MAIN_OBJS)
	$(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'))
	@echo 'Linking...'
	@$(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)
	@echo ' ...done'
	
# Other Targets:
# This rule compiles c and c++ source files without output or linking
SYNTAX: $(C_OBJS) $(CPP_OBJS)

projectclean:## Remove the PROJECT's build objects & binary
	-$(RM) $(PROJECT_DIR)/build/* $(OUTPUT_FILE)
	-@echo ' '	

clean: ## Same as projectclean
clean: projectclean

coreclean: ## Remove the core's build objects
	-$(RM) build/core/*

prompt:
	@printf "Warning: you are about to DELETE the projects/ folder and its content. This operation cannot be undone. Continue? (y/N) "
	@read REPLY; if [ $$REPLY !=  y ] && [ $$REPLY != Y ]; then echo "Aborting..."; exit 1; fi
	
distclean: ## Restores the Bela folder to a pristine state: remove all the projects source and the built objects, including the core Bela objects.
distclean: prompt distcleannoprompt
	
distcleannoprompt: ## Same as distclean, but does not prompt for confirmation. Use with care.
	-$(RM) build/source/* $(CORE_OBJS) $(CORE_CPP_DEPS) $(DEFAULT_MAIN_OBJS) $(DEFAULT_MAIN_CPP_DEPS) $(OUTPUT_FILE)
	-@echo ' '

runfg: run
run: ## Run PROJECT in the foreground
run: stop Bela
	@echo "Running $(RUN_COMMAND)"
	@sync& cd $(RUN_FROM) && $(RUN_COMMAND)
runide: ## Run PROJECT for IDE (foreground, no buffering)
runide: stop Bela
	@sync& cd $(RUN_FROM) && $(RUN_IDE_COMMAND)
runscreen: ## Run PROJECT in the background (detached screen)
runscreen: stop $(OUTPUT_FILE)
	@echo "Running $(RUN_COMMAND) in a screen"
	@cd $(RUN_FROM) && screen -S $(SCREEN_NAME) -d -m $(RUN_COMMAND)
runscreenfg: ## Run PROJECT in a screen in the foreground (can detach with ctrl-a ctrl-d)
runscreenfg: stop $(OUTPUT_FILE)
	@echo "Running $(RUN_COMMAND) in a screen"
	@cd $(RUN_FROM) && screen -S $(SCREEN_NAME) -m $(RUN_COMMAND)
FIFO_NAME=/tmp/belafifo
runscreenfifo: ## Same as runscreen, but stdout and stderr are piped to the foreground through a fifo
runscreenfifo: stop $(OUTPUT_FILE)
	@echo "Running $(RUN_COMMAND), piping output to $(FIFO_NAME)"
	@rm -rf $(FIFO_NAME)
	@mkfifo $(FIFO_NAME)
	@cd $(RUN_FROM) && screen -S $(SCREEN_NAME) -d -m stdbuf -e 0 -i 0 -o 0 bash -c "$(RUN_COMMAND) &>  $(FIFO_NAME)"
	@cat /tmp/belafifo

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"
nostartup: ## No Bela project runs at startup 
nostartup:
	@echo "Disabling Bela at startup..."
	@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)

startuploop: ## Makes PROJECT run at startup and restarts it if it crashes
startuploop: Bela
	@echo "Enabling Bela at startup in a loop..."
	@$(STARTUP_COMMAND) 'bash -c "while sleep 0.5 ; do echo Running Bela...;' '; done"' > $(BELA_STARTUP_SCRIPT)

startup: ## Makes PROJECT run at startup
startup: Bela
	@echo "Enabling Bela at startup..."
	@$(STARTUP_COMMAND) > $(BELA_STARTUP_SCRIPT)
	@chmod +x $(BELA_STARTUP_SCRIPT)
stop: ## Stops any Bela program that is currently running
stop:
	@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;

connect: ## Connects to the running Bela program (if any), can detach with ctrl-a ctrl-d.
	@screen -r -S $(SCREEN_NAME)
	
idestart: ## Starts the on-board IDE
idestart: idestop
	@printf "Starting IDE..."
	@$(BELA_IDE_RUN_COMMAND)
	@printf "done\n"

idestop: ## Stops the on-board IDE
	@printf "Stopping currently running IDE..."
	@screen -X -S $(BELA_IDE_SCREEN_NAME) quit > /dev/null; exit 0;
	@printf "done\n"

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)

idestartup: ## Enables the IDE at startup
	@echo "Enabling the IDE at startup"
	@$(BELA_IDE_STARTUP_COMMAND)
	@chmod +x $(BELA_IDE_STARTUP_SCRIPT)

idenostartup: ## Disables the IDE at startup
	@echo "Disabling the IDE at startup"
	@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)

ideconnect: ## Brings in the foreground the IDE that currently is running in a screen (if any), can detach with ctrl-a ctrl-d.
	@screen -r -S $(BELA_IDE_SCREEN_NAME)

BELA_DIR:=$(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))
UPDATES_DIR?=/root/Bela/updates
UPDATE_SOURCE_DIR_BASE?=/tmp/belaUpdate
UPDATE_SOURCE_DIR=$(UPDATE_SOURCE_DIR_BASE)/Bela
UPDATE_REQUIRED_PATHS?=scripts include core scripts/update_board 
UPDATE_BELA_PATCH?=/tmp/belaPatch
UPDATE_BELA_BACKUP?=/tmp/belaBak
UPDATE_BELA_MV_BACKUP?=/tmp/belaMvBak

updateclean: ## Cleans the $(UPDATES_DIR) folder
	@[ -n $(UPDATE_DIR) ] && rm -rf $(UPDATE_DIR) && mkdir -p $(UPDATE_DIR)

checkupdate: ## Unzips the zip file in $(UPDATES_DIR) and checks that it contains a valid
	# Check that exactly one zip file exists
	@cd $(UPDATES_DIR) && COUNT=`ls -l *.zip | wc -l` && [ $$COUNT -eq 1 ] && rm -rf `ls | grep -v "\.zip$$"`
	@#TODO: heuristics on available space. Use unzip -l and df
	@echo uncompressed size: `unzip -l \`ls $(UPDATES_DIR)/*.zip\` | tail -n1 | awk '{print $$1}'`
	# Delete and re-create the temp directory (first, make sure it is not an empty string!)
	@[ -n $(UPDATE_SOURCE_DIR_BASE) ] && rm -rf $(UPDATE_SOURCE_DIR_BASE) && mkdir -p $(UPDATE_SOURCE_DIR_BASE)
	# Unzip the contents to the temp folder
	@cd $(UPDATE_SOURCE_DIR_BASE) && unzip -qq $(UPDATES_DIR)/*zip
# Strip the top-level folder ( if there is only one )
#@DIR=`ls -d $(UPDATE_SOURCE_DIR)` && COUNT=`echo $$DIR | wc -l` &&\
	  [ $$COUNT -eq 1 ] && mv $(UPDATE_SOURCE_DIR)/* /tmp/supertemp && rm -rf $(UPDATE_SOURCE_DIR) && mv /tmp/supertemp $(UPDATE_SOURCE_DIR)
	# Now actually check if some key-files and folders are there
	@cd $(UPDATE_SOURCE_DIR) && FAIL=0 && for path in $(UPDATE_REQUIRED_PATHS); do `ls $$path >/dev/null 2>&1` || { FAIL=1; break; }; done;\
	  [ $$FAIL -eq 0 ] || { echo "$$path was not found in the zip archive. Maybe it is corrupted?"; exit 1; }
	# Success. You can continue the install with "make update"
UPDATE_LOG?=~/update.log
LOG=>> $(UPDATE_LOG) 2>&1
update: ## Installs the update from $(UPDATES_DIR)
update: stop
	# Truncate the log file
	@echo > $(UPDATE_LOG)
	# Re-perform the check, just in case ...	
	# Now actually check if some key-files and folders are there
	@cd $(UPDATE_SOURCE_DIR) && FAIL=0 && for path in $(UPDATE_REQUIRED_PATHS); do `ls $$path >/dev/null 2>&1` || { FAIL=1; break; }; done;\
	  [ $$FAIL -eq 0 ] || { echo "$$path was not found in the zip archive. Maybe it is corrupted?"; exit 1; }
	# Duplicate the Bela folder $(BELA_DIR) to $(UPDATE_BELA_PATCH) ...
	@[ -n $(UPDATE_BELA_PATCH) ] && mkdir -p $(UPDATE_BELA_PATCH)
	@#TODO: this would allow to trim trailing slashes in case we want to be safer: a="`pwd`/" ; target=${a%/} ; echo $target
	@rsync -a --delete-before $(BELA_DIR)/ $(UPDATE_BELA_PATCH)
	# Also backing it up in $(UPDATE_BELA_BACKUP)...
	@[ -n $(UPDATE_BELA_BACKUP) ] && mkdir -p $(UPDATE_BELA_BACKUP)
	@rsync -a $(BELA_DIR)/ $(UPDATE_BELA_BACKUP)
	# Here's the trick: we run "update_board" ssh'ing into the BeagleBone itself! 
	@cd $(UPDATE_SOURCE_DIR)/scripts && BBB_ADDRESS=root@127.0.0.1 BBB_BELA_HOME=$(UPDATE_BELA_PATCH) ./update_board -y --no-frills
	# If everything went ok, we now have the updated version of $(BELA_DIR) in $(UPDATE_BELA_PATCH) and a backup of $(BELA_DIR) in $(UPDATE_BELA_BACKUP)
	# So let's operate the magic swap. $(BELA_DIR) is moved to $(UPDATE_BELA_MV_BACKUP) and $(UPDATE_BELA_PATCH) is moved to $(BELA_DIR).
	# If something goes wrong at thie stage, you can always find your old $(BELA_DIR) folder at $(UPDATE_BELA_BACKUP)
	# The fun part is that this Makefile is moved as well...
	# We are about to kill the IDE, so just in case you are running this from within the IDE, we run the remainder of this update in a screen.
	# Output will be logged to $(UPDATE_LOG)
	@screen -S update-Bela -d -m bash -c '\
	  [ -n $(UPDATE_BELA_MV_BACKUP) ] $(LOG) && rm -rf $(UPDATE_BELA_MV_BACKUP) $(LOG) &&\
	  echo Kill the IDE $(LOG) && \
	  $(MAKE) --no-print-directory idestop $(LOG) &&\
	  mv $(BELA_DIR) $(UPDATE_BELA_MV_BACKUP) $(LOG) && mv $(UPDATE_BELA_PATCH) $(BELA_DIR) $(LOG) &&\
	  echo Hope we are still alive here $(LOG) &&\
	  echo Restart the IDE $(LOG) &&\
	  make --no-print-directory -C $(BELA_DIR) idestart $(LOG) &&\
	  echo Update succesful $(LOG); \
	  ' $(LOG)

.PHONY: all clean distclean help projectclean nostartup startup startuploop debug run runfg runscreen runscreenfg runscreenfifo stop idestart idestop idestartup idenostartup ideconnect connect update checkupdate