1# Copyright 2018 Oticon A/S
2# SPDX-License-Identifier: Apache-2.0
3#
4# Top level BabbleSim Makefile:
5# it calls make for each component to be compiled
6
7# In case of trouble with making, this may help: make -n --no-builtin-rules --warn-undefined-variables -d
8# or limit with --debug=b  (see https://www.gnu.org/software/make/manual/html_node/Options-Summary.html)
9# you can also install and run with remake or if need be remake -d
10define LF
11
12
13endef
14
15SHELL:=bash
16
17BSIM_OUT_PATH?=./#by default all the output of compiling the simulator is placed under this same folder. If you want the output in some other place set this environment variable
18export BSIM_OUT_PATH:=$(abspath ${BSIM_OUT_PATH})
19
20no_default:
21	@echo "There is no default rule, please specify which component you want to build"
22
23BSIM_COMPONENTS_PATH?=$(abspath components/)
24ifeq ("$(wildcard $(BSIM_COMPONENTS_PATH))","")
25$(error BSIM_COMPONENTS_PATH (${BSIM_COMPONENTS_PATH}) does not point to a folder${LF}	Do not run me directly inside the common folder; or set BSIM_COMPONENTS_PATH first)
26endif
27export BSIM_COMPONENTS_PATH
28
29COMPONENTS:=$(shell ls ${BSIM_COMPONENTS_PATH} --hide=common)
30
31DEPENDFILES:=$(addprefix components/,$(addsuffix /Depends,${COMPONENTS}))
32
33.PHONY: $(COMPONENTS) all clean clean_results ultraclean help no_default everything
34
35Makefile: ;
36
37${BSIM_OUT_PATH}:
38	@mkdir ${BSIM_OUT_PATH} -p
39${BSIM_OUT_PATH}/bin/: | ${BSIM_OUT_PATH}
40	mkdir ${BSIM_OUT_PATH}/bin/
41${BSIM_OUT_PATH}/lib/: | ${BSIM_OUT_PATH}
42	@mkdir ${BSIM_OUT_PATH}/lib/
43${BSIM_OUT_PATH}/results/: | ${BSIM_OUT_PATH}
44	@mkdir ${BSIM_OUT_PATH}/results/
45
46all: everything
47
48everything: ${COMPONENTS}
49
50include ${DEPENDFILES}
51#we complain if the depend file is missing (and we complain more below, but we dont break)
52
53$(COMPONENTS): | ${BSIM_OUT_PATH} ${BSIM_OUT_PATH}/bin/ ${BSIM_OUT_PATH}/lib/ ${BSIM_OUT_PATH}/results/
54	@if [ ! -d "${BSIM_OUT_PATH}/components/$@" ];then mkdir -p  ${BSIM_OUT_PATH}/components/$@ ; fi #let's create the output mirror directory of each component if it doesnt exist
55	@export COMPONENT_OUTPUT_DIR=$(abspath ${BSIM_OUT_PATH}/components/$@) ; ${MAKE} --no-builtin-rules --warn-undefined-variables -C components/$@; \
56	( if [ $$? -ne 0 ]; then \
57	  echo -n "*** NOTE: $@ could NOT be built, " ;\
58	  if [ -z $$BSIM_BUILD_FAIL_ASAP ]; then \
59	   echo "continuing ***" ;\
60	   true; \
61	  else \
62	   echo "aborting ***" ;\
63	   false; \
64	  fi; \
65	fi; )
66
67${DEPENDFILES}:
68	$(warning $@ does not exist. Either you have a left over component folder or you forgot to add a depend file to a new component, either way please fix it (remove the empty component or create a proper Depend file) )
69
70clean_results:
71	@rm ${BSIM_OUT_PATH}/results/* -r ; true
72
73clean:
74	@echo "Deleting intermediate compilation results + libraries + executables (*.d .o .a .so & bin/bs_*)"
75	@find -L ${BSIM_OUT_PATH} -path *fftw-* -prune -o \( -name "*.a" -o -name "*.o" -o -name "*.so" -o -name "*~" -o -name "*.d" -o -name "*.Tsymbols" \)  | grep -v fftw  | grep -i -v libCrypto | xargs rm -f  \;
76	@#avoid looking into the library directories we dont want to need to rebuild each time somebody goes and runs make clean
77	@cd ${BSIM_OUT_PATH}/bin/ ; rm bs_* >& /dev/null ; true
78
79clean_coverage:
80	@find $(BSIM_OUT_PATH) -name "*.gcda" -or -name "*.gcno" | xargs rm -f ; true
81
82clean_all: clean clean_coverage
83
84help:
85	@echo "********************************"
86	@echo "* BabbleSim top level makefile *"
87	@echo "********************************"
88	@echo "Provided rules:"
89	@echo " all             : everything (see below)"
90	@echo " clean           : clean all components and their installed output"
91	@echo " clean_coverage  : clean gcov input files"
92	@echo " clean_all       : clean & clean_coverage"
93	@echo " clean_results   : clean results folder"
94	@echo " <component>     : compile that component and its dependencies"
95	@echo " everything      : compile all components present in the components folder"
96	@echo "Note that you can use TAB to autocomplete rules in the command line in modern OSs"
97
98#.SILENT:; #let's not print entering and leaving directory messages
99