| giuliomoro@64 | 1 # BeagleRT | 
| 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@265 | 14   $(error PROJECT is not set) | 
| l@260 | 15 endif | 
| l@260 | 16 | 
| l@260 | 17 PROJECT_DIR := $(abspath projects/$(PROJECT)) | 
| giuliomoro@267 | 18 $(shell mkdir -p $(PROJECT_DIR)/build) | 
| giuliomoro@64 | 19 RM := rm -rf | 
| giuliomoro@64 | 20 STATIC_LIBS := ./libprussdrv.a ./libNE10.a | 
| l@260 | 21 LIBS := -lrt -lnative -lxenomai -lsndfile | 
| l@260 | 22 | 
| giuliomoro@244 | 23 # refresh library cache and check if libpd is there | 
| giuliomoro@244 | 24 TEST_LIBPD := $(shell ldconfig; ldconfig -p | grep "libpd\.so") | 
| giuliomoro@244 | 25 ifeq ($(strip $(TEST_LIBPD)), ) | 
| giuliomoro@244 | 26 else | 
| giuliomoro@244 | 27 # if libpd is there, link it in | 
| giuliomoro@244 | 28   LIBS += -lpd -lpthread_rt | 
| giuliomoro@244 | 29 endif | 
| giuliomoro@64 | 30 | 
| giuliomoro@241 | 31 CPP_FLAGS := -O3 -march=armv7-a -mtune=cortex-a8 -mfloat-abi=hard -mfpu=neon -ftree-vectorize | 
| giuliomoro@241 | 32 C_FLAGS := $(CPP_FLAGS) | 
| giuliomoro@241 | 33 | 
| giuliomoro@241 | 34 ifndef COMPILER | 
| giuliomoro@241 | 35 # check whether clang is installed | 
| giuliomoro@241 | 36     TEST_COMPILER := $(shell which clang) | 
| giuliomoro@241 | 37   ifneq ($(strip $(TEST_COMPILER)), ) | 
| giuliomoro@241 | 38     # if it is installed, use it | 
| giuliomoro@241 | 39     COMPILER := clang | 
| giuliomoro@241 | 40   else | 
| giuliomoro@241 | 41     COMPILER := gcc | 
| giuliomoro@241 | 42   endif | 
| giuliomoro@241 | 43 endif | 
| giuliomoro@241 | 44 | 
| giuliomoro@241 | 45 ifeq ($(COMPILER), clang) | 
| giuliomoro@241 | 46   CC=clang | 
| giuliomoro@241 | 47   CXX=clang++ | 
| giuliomoro@241 | 48   CPP_FLAGS += | 
| giuliomoro@244 | 49   C_FLAGS += | 
| giuliomoro@241 | 50 else | 
| giuliomoro@241 | 51   CC=gcc | 
| giuliomoro@241 | 52   CXX=g++ | 
| giuliomoro@241 | 53   CPP_FLAGS += --fast-math | 
| giuliomoro@241 | 54   C_FLAGS += --fast-math | 
| giuliomoro@241 | 55 endif | 
| giuliomoro@241 | 56 | 
| l@260 | 57 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 | 58 | 
| l@260 | 59 ASM_SRCS := $(wildcard $(PROJECT_DIR)/*.S) | 
| l@260 | 60 ASM_OBJS := $(addprefix $(PROJECT_DIR)/build/,$(notdir $(ASM_SRCS:.S=.o))) | 
| l@260 | 61 ASM_DEPS := $(addprefix $(PROJECT_DIR)/build/,$(notdir $(ASM_SRCS:.S=.d))) | 
| giuliomoro@64 | 62 | 
| l@260 | 63 C_SRCS := $(wildcard $(PROJECT_DIR)/*.c) | 
| l@260 | 64 C_OBJS := $(addprefix $(PROJECT_DIR)/build/,$(notdir $(C_SRCS:.c=.o))) | 
| l@260 | 65 C_DEPS := $(addprefix $(PROJECT_DIR)/build/,$(notdir $(C_SRCS:.c=.d))) | 
| giuliomoro@64 | 66 | 
| l@260 | 67 CPP_SRCS := $(wildcard $(PROJECT_DIR)/*.cpp) | 
| l@260 | 68 CPP_OBJS := $(addprefix $(PROJECT_DIR)/build/,$(notdir $(CPP_SRCS:.cpp=.o))) | 
| l@260 | 69 CPP_DEPS := $(addprefix $(PROJECT_DIR)/build/,$(notdir $(CPP_SRCS:.cpp=.d))) | 
| giuliomoro@64 | 70 | 
| giuliomoro@64 | 71 # Core BeagleRT sources | 
| giuliomoro@186 | 72 CORE_CPP_SRCS = $(filter-out core/default_main.cpp, $(wildcard core/*.cpp)) | 
| giuliomoro@186 | 73 CORE_OBJS := $(addprefix build/core/,$(notdir $(CORE_CPP_SRCS:.cpp=.o))) | 
| giuliomoro@186 | 74 CORE_CPP_DEPS := $(addprefix build/core/,$(notdir $(CORE_CPP_SRCS:.cpp=.d))) | 
| giuliomoro@64 | 75 | 
| giuliomoro@64 | 76 # Objects for a system-supplied default main() file, if the user | 
| giuliomoro@64 | 77 # only wants to provide the render functions. | 
| giuliomoro@64 | 78 DEFAULT_MAIN_CPP_SRCS := ./core/default_main.cpp | 
| giuliomoro@64 | 79 DEFAULT_MAIN_OBJS := ./build/core/default_main.o | 
| giuliomoro@64 | 80 DEFAULT_MAIN_CPP_DEPS := ./build/core/default_main.d | 
| andrewm@68 | 81 | 
| giuliomoro@209 | 82 # all = build BeagleRT | 
| andrewm@69 | 83 all: SYNTAX_FLAG := | 
| giuliomoro@64 | 84 all: BeagleRT | 
| andrewm@68 | 85 | 
| giuliomoro@209 | 86 # debug = buildBeagleRT debug | 
| giuliomoro@209 | 87 debug: CPP_FLAGS=-g | 
| giuliomoro@209 | 88 debug: C_FLAGS=-g | 
| giuliomoro@209 | 89 debug: all | 
| giuliomoro@209 | 90 | 
| andrewm@69 | 91 # syntax = check syntax | 
| andrewm@69 | 92 syntax: SYNTAX_FLAG := -fsyntax-only | 
| l@260 | 93 syntax: SYNTAX | 
| andrewm@69 | 94 | 
| giuliomoro@64 | 95 # Rule for BeagleRT core C++ files | 
| giuliomoro@64 | 96 build/core/%.o: ./core/%.cpp | 
| l@260 | 97 	@echo 'Building $(notdir $<)...' | 
| l@260 | 98 #	@echo 'Invoking: C++ Compiler' | 
| l@260 | 99 	@$(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 | 100 	@echo ' ...done' | 
| giuliomoro@64 | 101 	@echo ' ' | 
| andrewm@68 | 102 | 
| giuliomoro@64 | 103 # Rule for user-supplied C++ files | 
| l@260 | 104 $(PROJECT_DIR)/build/%.o: $(PROJECT_DIR)/%.cpp | 
| l@260 | 105 	@echo 'Building $(notdir $<)...' | 
| l@260 | 106 #	@echo 'Invoking: C++ Compiler' | 
| l@260 | 107 	@$(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 | 108 	@echo ' ...done' | 
| giuliomoro@64 | 109 	@echo ' ' | 
| andrewm@68 | 110 | 
| giuliomoro@64 | 111 # Rule for user-supplied C files | 
| l@260 | 112 $(PROJECT_DIR)/build/%.o: $(PROJECT_DIR)/%.c | 
| l@260 | 113 	@echo 'Building $(notdir $<)...' | 
| l@260 | 114 #	@echo 'Invoking: C Compiler' | 
| l@260 | 115 	@$(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 | 116 	@echo ' ...done' | 
| giuliomoro@64 | 117 	@echo ' ' | 
| andrewm@68 | 118 | 
| giuliomoro@64 | 119 # Rule for user-supplied assembly files | 
| l@260 | 120 $(PROJECT_DIR)/build/%.o: $(PROJECT_DIR)/%.S | 
| l@260 | 121 	@echo 'Building $(notdir $<)...' | 
| l@260 | 122 #	@echo 'Invoking: GCC Assembler' | 
| l@260 | 123 	@as  -o "$@" "$<" | 
| l@260 | 124 	@echo ' ...done' | 
| giuliomoro@64 | 125 	@echo ' ' | 
| giuliomoro@64 | 126 | 
| giuliomoro@64 | 127 # This is a nasty kludge: we want to be able to optionally link in a default | 
| giuliomoro@64 | 128 # main file if the user hasn't supplied one. We check for the presence of the main() | 
| giuliomoro@64 | 129 # function, and conditionally call one of two recursive make targets depending on whether | 
| giuliomoro@64 | 130 # we want to link in the default main file or not. The kludge is the mess of a shell script | 
| giuliomoro@64 | 131 # line below. Surely there's a better way to do this? | 
| giuliomoro@64 | 132 BeagleRT: $(CORE_OBJS) $(ASM_OBJS) $(C_OBJS) $(CPP_OBJS) $(STATIC_LIBS) | 
| l@260 | 133 	$(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 | 134 	@$(MAKE) $(NEXT_TARGET) | 
| l@260 | 135 #	@echo 'Finished building target: $@' | 
| l@260 | 136 #	@echo ' ' | 
| l@260 | 137 #	$(MAKE) --no-print-directory post-build | 
| andrewm@68 | 138 | 
| giuliomoro@64 | 139 # Rule for building BeagleRT including the default main file (no user-supplied main()) | 
| giuliomoro@64 | 140 BeagleRT_with_main: $(CORE_OBJS) $(DEFAULT_MAIN_OBJS) $(ASM_OBJS) $(C_OBJS) $(CPP_OBJS) $(STATIC_LIBS) | 
| l@260 | 141 	@echo 'Linking default main.cpp...' | 
| l@260 | 142 #	@echo 'Invoking: C++ Linker' | 
| l@260 | 143 	@$(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 | 144 	@echo ' ...done' | 
| l@260 | 145 | 
| giuliomoro@64 | 146 # Rule for building BeagleRT without the default main file (user-supplied main()) | 
| giuliomoro@64 | 147 BeagleRT_without_main: $(CORE_OBJS) $(ASM_OBJS) $(C_OBJS) $(CPP_OBJS) $(STATIC_LIBS) | 
| l@260 | 148 	@echo 'Linking main.cpp from project...' | 
| l@260 | 149 #	@echo 'Invoking: C++ Linker' | 
| l@260 | 150 	@$(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 | 151 	@echo ' ...done' | 
| l@260 | 152 | 
| l@260 | 153 # Other Targets: | 
| l@260 | 154 # This rule compiles c and c++ source files without output or linking | 
| l@260 | 155 SYNTAX: $(C_OBJS) $(CPP_OBJS) | 
| giuliomoro@64 | 156 | 
| l@260 | 157 # Remove the project's build objects & binary | 
| l@260 | 158 projectclean: | 
| l@260 | 159 	-$(RM) $(PROJECT_DIR)/build/* $(PROJECT_DIR)/$(PROJECT) | 
| giuliomoro@64 | 160 	-@echo ' ' | 
| giuliomoro@64 | 161 | 
| giuliomoro@64 | 162 # Remove all the built objects, including the core BeagleRT objects | 
| giuliomoro@64 | 163 distclean: | 
| giuliomoro@64 | 164 	-$(RM) build/source/* $(CORE_OBJS) $(CORE_CPP_DEPS) $(DEFAULT_MAIN_OBJS) $(DEFAULT_MAIN_CPP_DEPS) BeagleRT | 
| giuliomoro@64 | 165 	-@echo ' ' | 
| giuliomoro@64 | 166 | 
| giuliomoro@64 | 167 # Remove only the user-generated objects | 
| l@260 | 168 #clean: | 
| l@260 | 169 #	-$(RM) build/source/* BeagleRT | 
| l@260 | 170 #	-@echo ' ' | 
| giuliomoro@64 | 171 | 
| giuliomoro@64 | 172 post-build: | 
| giuliomoro@64 | 173 # Nothing to do here (for now) | 
| giuliomoro@64 | 174 | 
| l@260 | 175 .PHONY: all clean distclean projectclean dependents debug | 
| giuliomoro@64 | 176 .SECONDARY: post-build |