annotate Makefile @ 383:e42bc9ba7550 prerelease

Fixed when rebuilding non-main() project after main() project: the 'nasty kludge' was looking in ALL the .o files in projectFolder/build/ instead of only those that have a corresponding .cpp/.c/.S file. An even better fix is make sure that object files associated with source files deleted by rsync are removed as well
author Giulio Moro <giuliomoro@yahoo.it>
date Mon, 13 Jun 2016 00:44:47 +0100
parents 9dc5a0ccad25
children fb5547fe6d99
rev   line source
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
giuliomoro@374 13 NO_PROJECT_TARGETS=distclean stop
giuliomoro@374 14 ifeq (,$(filter $(NO_PROJECT_TARGETS),$(MAKECMDGOALS)))
giuliomoro@374 15 ifndef PROJECT
giuliomoro@374 16 ifndef EXAMPLE
giuliomoro@374 17 $(error PROJECT or EXAMPLE should be set when the target is not one of: `$(NO_PROJECT_TARGETS)`)
giuliomoro@374 18 endif
giuliomoro@287 19 endif
l@260 20 endif
l@260 21
giuliomoro@374 22 # if we are building an example, just copy it to the projects/ folder
giuliomoro@374 23 # and tehn treat it as a project
giuliomoro@374 24 ifdef EXAMPLE
giuliomoro@374 25 PROJECT?=exampleTempProject
giuliomoro@374 26 PROJECT_DIR?=$(abspath projects/$(PROJECT))
andrewm@380 27 $(shell mkdir -p $(abspath projects))
giuliomoro@374 28 $(shell rm -rf $(PROJECT_DIR))
giuliomoro@374 29 $(shell cp -r examples/$(EXAMPLE) $(PROJECT_DIR))
giuliomoro@374 30 else
giuliomoro@287 31 PROJECT_DIR := $(abspath projects/$(PROJECT))
giuliomoro@287 32 endif
giuliomoro@287 33
giuliomoro@374 34 ifdef PROJECT
giuliomoro@374 35 $(shell mkdir -p $(PROJECT_DIR)/build)
giuliomoro@287 36 endif
giuliomoro@374 37
giuliomoro@331 38 OUTPUT_FILE?=$(PROJECT_DIR)/$(PROJECT)
giuliomoro@331 39 COMMAND_LINE_OPTIONS?=$(CL)
giuliomoro@331 40 RUN_COMMAND?=$(OUTPUT_FILE) $(COMMAND_LINE_OPTIONS)
giuliomoro@331 41 BELA_STARTUP_SCRIPT?=/root/BeagleRT_startup.sh
giuliomoro@359 42 BELA_AUDIO_THREAD_NAME?=bela-audio
giuliomoro@377 43 SCREEN_NAME?=Bela
giuliomoro@302 44
giuliomoro@374 45
giuliomoro@64 46 RM := rm -rf
giuliomoro@64 47 STATIC_LIBS := ./libprussdrv.a ./libNE10.a
l@260 48 LIBS := -lrt -lnative -lxenomai -lsndfile
l@260 49
giuliomoro@244 50 # refresh library cache and check if libpd is there
giuliomoro@326 51 #TEST_LIBPD := $(shell ldconfig; ldconfig -p | grep "libpd\.so") # safest but slower way of checking
giuliomoro@326 52 LIBPD_PATH = /usr/lib/libpd.so
giuliomoro@326 53 TEST_LIBPD := $(shell which $(LIBPD_PATH))
giuliomoro@326 54 ifneq ($(strip $(TEST_LIBPD)), )
giuliomoro@244 55 # if libpd is there, link it in
giuliomoro@244 56 LIBS += -lpd -lpthread_rt
giuliomoro@244 57 endif
giuliomoro@64 58
giuliomoro@241 59 CPP_FLAGS := -O3 -march=armv7-a -mtune=cortex-a8 -mfloat-abi=hard -mfpu=neon -ftree-vectorize
giuliomoro@241 60 C_FLAGS := $(CPP_FLAGS)
giuliomoro@241 61
giuliomoro@241 62 ifndef COMPILER
giuliomoro@241 63 # check whether clang is installed
giuliomoro@295 64 # TEST_COMPILER := $(shell which clang)
giuliomoro@302 65 ifneq ($(strip $(TEST_COMPILER)), )
giuliomoro@302 66 if it is installed, use it
giuliomoro@302 67 COMPILER := clang
giuliomoro@302 68 else
giuliomoro@241 69 COMPILER := gcc
giuliomoro@302 70 endif
giuliomoro@241 71 endif
giuliomoro@241 72
giuliomoro@241 73 ifeq ($(COMPILER), clang)
giuliomoro@306 74 CC=clang
giuliomoro@306 75 CXX=clang++
giuliomoro@302 76 CPP_FLAGS += -DNDEBUG
giuliomoro@302 77 C_FLAGS += -DNDEBUG
giuliomoro@241 78 else
giuliomoro@241 79 CC=gcc
giuliomoro@241 80 CXX=g++
giuliomoro@241 81 CPP_FLAGS += --fast-math
giuliomoro@241 82 C_FLAGS += --fast-math
giuliomoro@241 83 endif
giuliomoro@241 84
l@260 85 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 86
l@260 87 ASM_SRCS := $(wildcard $(PROJECT_DIR)/*.S)
l@260 88 ASM_OBJS := $(addprefix $(PROJECT_DIR)/build/,$(notdir $(ASM_SRCS:.S=.o)))
l@260 89 ASM_DEPS := $(addprefix $(PROJECT_DIR)/build/,$(notdir $(ASM_SRCS:.S=.d)))
giuliomoro@64 90
l@260 91 C_SRCS := $(wildcard $(PROJECT_DIR)/*.c)
l@260 92 C_OBJS := $(addprefix $(PROJECT_DIR)/build/,$(notdir $(C_SRCS:.c=.o)))
l@260 93 C_DEPS := $(addprefix $(PROJECT_DIR)/build/,$(notdir $(C_SRCS:.c=.d)))
giuliomoro@64 94
l@260 95 CPP_SRCS := $(wildcard $(PROJECT_DIR)/*.cpp)
l@260 96 CPP_OBJS := $(addprefix $(PROJECT_DIR)/build/,$(notdir $(CPP_SRCS:.cpp=.o)))
l@260 97 CPP_DEPS := $(addprefix $(PROJECT_DIR)/build/,$(notdir $(CPP_SRCS:.cpp=.d)))
giuliomoro@64 98
giuliomoro@383 99 PROJECT_OBJS = $(ASM_OBJS) $(C_OBJS) $(CPP_OBJS)
giuliomoro@383 100
giuliomoro@301 101 # Core Bela sources
giuliomoro@186 102 CORE_CPP_SRCS = $(filter-out core/default_main.cpp, $(wildcard core/*.cpp))
giuliomoro@186 103 CORE_OBJS := $(addprefix build/core/,$(notdir $(CORE_CPP_SRCS:.cpp=.o)))
giuliomoro@186 104 CORE_CPP_DEPS := $(addprefix build/core/,$(notdir $(CORE_CPP_SRCS:.cpp=.d)))
giuliomoro@64 105
andrewm@318 106 CORE_ASM_SRCS := $(wildcard core/*.S)
andrewm@318 107 CORE_ASM_OBJS := $(addprefix build/core/,$(notdir $(CORE_ASM_SRCS:.S=.o)))
andrewm@318 108 CORE_ASM_DEPS := $(addprefix build/core/,$(notdir $(CORE_ASM_SRCS:.S=.d)))
andrewm@318 109
andrewm@318 110
giuliomoro@64 111 # Objects for a system-supplied default main() file, if the user
giuliomoro@64 112 # only wants to provide the render functions.
giuliomoro@64 113 DEFAULT_MAIN_CPP_SRCS := ./core/default_main.cpp
giuliomoro@64 114 DEFAULT_MAIN_OBJS := ./build/core/default_main.o
giuliomoro@64 115 DEFAULT_MAIN_CPP_DEPS := ./build/core/default_main.d
andrewm@68 116
giuliomoro@334 117 Bela: ## Builds the Bela program with all the opimizations
giuliomoro@334 118 Bela: $(OUTPUT_FILE)
giuliomoro@334 119
giuliomoro@301 120 # all = build Bela
giuliomoro@334 121 all: ## Same as Bela
andrewm@69 122 all: SYNTAX_FLAG :=
giuliomoro@301 123 all: Bela
andrewm@68 124
giuliomoro@301 125 # debug = buildBela debug
giuliomoro@334 126 debug: ## Same as Bela but with debug flags and no optimizations
giuliomoro@209 127 debug: CPP_FLAGS=-g
giuliomoro@209 128 debug: C_FLAGS=-g
giuliomoro@209 129 debug: all
giuliomoro@209 130
andrewm@69 131 # syntax = check syntax
giuliomoro@334 132 syntax: ## Only checks syntax
andrewm@69 133 syntax: SYNTAX_FLAG := -fsyntax-only
l@260 134 syntax: SYNTAX
andrewm@69 135
giuliomoro@301 136 # Rule for Bela core C++ files
giuliomoro@64 137 build/core/%.o: ./core/%.cpp
l@260 138 @echo 'Building $(notdir $<)...'
l@260 139 # @echo 'Invoking: C++ Compiler'
l@260 140 @$(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 141 @echo ' ...done'
giuliomoro@64 142 @echo ' '
andrewm@68 143
andrewm@318 144 # Rule for Bela core ASM files
andrewm@318 145 build/core/%.o: ./core/%.S
andrewm@318 146 @echo 'Building $(notdir $<)...'
andrewm@318 147 # @echo 'Invoking: GCC Assembler'
andrewm@318 148 @as -o "$@" "$<"
andrewm@318 149 @echo ' ...done'
andrewm@318 150 @echo ' '
andrewm@318 151
giuliomoro@64 152 # Rule for user-supplied C++ files
l@260 153 $(PROJECT_DIR)/build/%.o: $(PROJECT_DIR)/%.cpp
l@260 154 @echo 'Building $(notdir $<)...'
l@260 155 # @echo 'Invoking: C++ Compiler'
l@260 156 @$(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 157 @echo ' ...done'
giuliomoro@64 158 @echo ' '
andrewm@68 159
giuliomoro@64 160 # Rule for user-supplied C files
l@260 161 $(PROJECT_DIR)/build/%.o: $(PROJECT_DIR)/%.c
l@260 162 @echo 'Building $(notdir $<)...'
l@260 163 # @echo 'Invoking: C Compiler'
giuliomoro@287 164 $(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 165 @echo ' ...done'
giuliomoro@64 166 @echo ' '
andrewm@68 167
giuliomoro@64 168 # Rule for user-supplied assembly files
l@260 169 $(PROJECT_DIR)/build/%.o: $(PROJECT_DIR)/%.S
l@260 170 @echo 'Building $(notdir $<)...'
l@260 171 # @echo 'Invoking: GCC Assembler'
l@260 172 @as -o "$@" "$<"
l@260 173 @echo ' ...done'
giuliomoro@64 174 @echo ' '
giuliomoro@64 175
giuliomoro@64 176 # This is a nasty kludge: we want to be able to optionally link in a default
giuliomoro@64 177 # main file if the user hasn't supplied one. We check for the presence of the main()
giuliomoro@64 178 # function, and conditionally call one of two recursive make targets depending on whether
giuliomoro@64 179 # we want to link in the default main file or not. The kludge is the mess of a shell script
giuliomoro@64 180 # line below. Surely there's a better way to do this?
giuliomoro@383 181 $(OUTPUT_FILE): $(CORE_ASM_OBJS) $(CORE_OBJS) $(PROJECT_OBJS) $(STATIC_LIBS) $(DEFAULT_MAIN_OBJS)
giuliomoro@383 182 $(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'))
giuliomoro@287 183 @echo 'Invoking: C++ linker'
andrewm@318 184 @$(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 185 @echo 'Finished building target: $@'
giuliomoro@333 186 @sync
l@260 187 # Other Targets:
l@260 188 # This rule compiles c and c++ source files without output or linking
l@260 189 SYNTAX: $(C_OBJS) $(CPP_OBJS)
giuliomoro@64 190
l@260 191 # Remove the project's build objects & binary
l@260 192 projectclean:
giuliomoro@326 193 -$(RM) $(PROJECT_DIR)/build/* $(OUTPUT_FILE)
giuliomoro@64 194 -@echo ' '
giuliomoro@64 195
giuliomoro@301 196 # Remove all the built objects, including the core Bela objects
giuliomoro@64 197 distclean:
giuliomoro@326 198 -$(RM) build/source/* $(CORE_OBJS) $(CORE_CPP_DEPS) $(DEFAULT_MAIN_OBJS) $(DEFAULT_MAIN_CPP_DEPS) $(OUTPUT_FILE)
giuliomoro@64 199 -@echo ' '
giuliomoro@64 200
giuliomoro@326 201
giuliomoro@302 202 runfg: run
giuliomoro@334 203 run: ## Run PROJECT in the foreground
giuliomoro@326 204 run: stop Bela
giuliomoro@327 205 @echo "Running $(RUN_COMMAND)"
giuliomoro@327 206 @cd $(PROJECT_DIR) && $(RUN_COMMAND)
giuliomoro@334 207 runscreen: ## Run PROJECT in the background (detached screen)
giuliomoro@302 208 runscreen: stop $(OUTPUT_FILE)
giuliomoro@327 209 @echo "Running $(RUN_COMMAND) in a screen"
giuliomoro@327 210 @cd $(PROJECT_DIR) && screen -S $(SCREEN_NAME) -d -m $(RUN_COMMAND)
giuliomoro@334 211 runscreenfg: ## Run PROJECT in a screen in the foreground (can detach with ctrl-a ctrl-d)
giuliomoro@313 212 runscreenfg: stop $(OUTPUT_FILE)
giuliomoro@327 213 @echo "Running $(RUN_COMMAND) in a screen"
giuliomoro@327 214 @cd $(PROJECT_DIR) && screen -S $(SCREEN_NAME) -m $(RUN_COMMAND)
giuliomoro@313 215 FIFO_NAME=/tmp/belafifo
giuliomoro@334 216 runscreenfifo: ## Same as runscreen, but stdout and stderr are piped to the foreground through a fifo
giuliomoro@313 217 runscreenfifo: stop $(OUTPUT_FILE)
giuliomoro@313 218 @echo "Running $(RUN_COMMAND), piping output to $(FIFO_NAME)"
giuliomoro@313 219 @rm -rf $(FIFO_NAME)
giuliomoro@313 220 @mkfifo $(FIFO_NAME)
giuliomoro@327 221 @cd $(PROJECT_DIR)
giuliomoro@315 222 @screen -S $(SCREEN_NAME) -d -m stdbuf -e 0 -i 0 -o 0 bash -c "$(RUN_COMMAND) &> $(FIFO_NAME)"
giuliomoro@315 223 @cat /tmp/belafifo
giuliomoro@302 224
giuliomoro@331 225 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 226 nostartup: ## No Bela project runs at startup
giuliomoro@331 227 nostartup:
giuliomoro@331 228 @echo "Disabling BeagleRT at startup..."
giuliomoro@344 229 @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 230
giuliomoro@334 231 startuploop: ## Makes PROJECT run at startup and restarts it if it crashes
giuliomoro@331 232 startuploop: Bela
giuliomoro@331 233 @echo "Enabling Bela at startup in a loop..."
giuliomoro@331 234 @$(STARTUP_COMMAND) 'bash -c "while sleep 0.5 ; do echo Running Bela...;' '; done"' > $(BELA_STARTUP_SCRIPT)
giuliomoro@331 235
giuliomoro@334 236 startup: ## Makes PROJECT run at startup
giuliomoro@331 237 startup: Bela
giuliomoro@331 238 @echo "Enabling Bela at startup..."
giuliomoro@331 239 @$(STARTUP_COMMAND) > $(BELA_STARTUP_SCRIPT)
giuliomoro@331 240
giuliomoro@334 241 stop: ## Stops any Bela program that is currently running
giuliomoro@302 242 stop:
giuliomoro@302 243 @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 244 # Remove only the user-generated objects
l@260 245 #clean:
giuliomoro@301 246 # -$(RM) build/source/* Bela
l@260 247 # -@echo ' '
giuliomoro@64 248
giuliomoro@64 249 post-build:
giuliomoro@64 250 # Nothing to do here (for now)
giuliomoro@334 251 help: ## Show this help
giuliomoro@334 252 @echo 'Usage: make [target] CL=[command line options] [PROJECT=[projectName] | EXAMPLE=[exampleName]]'
giuliomoro@334 253 @echo 'Targets: (default: Bela)'
giuliomoro@334 254 @fgrep -h "##" $(MAKEFILE_LIST) | fgrep -v fgrep | sed -e 's/^\(.*\): .*##\(.*\)/\1:#\2/' | column -t -c 2 -s '#'
giuliomoro@64 255
giuliomoro@326 256 .PHONY: all clean distclean projectclean dependents debug run runfg runscreen stop
giuliomoro@64 257 .SECONDARY: post-build