-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMakefile
More file actions
175 lines (142 loc) · 5.67 KB
/
Makefile
File metadata and controls
175 lines (142 loc) · 5.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
SHELL := /bin/bash
############################################################
# Standard Configuration
############################################################
mkfile_path := $(abspath $(lastword $(MAKEFILE_LIST)))
current_path := $(dir $(mkfile_path))
current_dir := $(notdir $(patsubst %/,%,$(dir $(mkfile_path))))
build_dir ?= $(abspath $(lastword $(MAKEFILE_LIST))/../../..)
CCACHE_DIR := $(build_dir)/.ccache
ros_source_file := /bin/ros_setup.sh
ifeq ("$(wildcard /opt/ros/jazzy/setup.bash)","")
ifeq ("$(wildcard $(ros_source_file))","")
ros_source_file := /opt/ros/humble/setup.bash
endif
else
ifeq ("$(wildcard $(ros_source_file))","")
ros_source_file := /opt/ros/jazzy/setup.bash
endif
endif
LINKER_FLAGS = "$(shell python3-config --ldflags --embed)"
# Find ROS2 packages in a given directory, two levels deep, and return only the package name
define find_ros2_packages
$(shell \
for dir in $$(find $(1) -mindepth 1 -maxdepth 2 -type d); do \
if [ -f "$$dir/CMakeLists.txt" ] && [ -f "$$dir/package.xml" ]; then \
basename $$dir; \
fi; \
done)
endef
define find_ros2_python_packages
$(shell \
for dir in $$(find $(1) -mindepth 1 -maxdepth 2 -type d); do \
if [ -f "$$dir/setup.py" ] && [ -f "$$dir/package.xml" ] && [[ "$$(basename $$dir)" == *_py ]]; then \
basename $$dir; \
fi; \
done)
endef
# Individual package lists from specific subdirectories
PACKAGES := $(call find_ros2_packages,$(current_path)/motorium)
############################################################
# Customizable Configuration - User can override these
############################################################
BUILD_TYPE ?= Release
BUILD_TESTING ?= ON
BUILD_WITH_NINJA ?= ON
PARALLEL_JOBS ?= 6
CPP_VERSION ?= -std=c++23
CLANG_THREAD_SAFETY_FLAGS := -Wthread-safety -Werror=thread-safety-analysis
############################################################
# Set flags based on configuration
############################################################
COMMON_CMAKE_ARGS ?= \
-DCMAKE_EXPORT_COMPILE_COMMANDS=ON \
-DCMAKE_BUILD_TYPE=$(BUILD_TYPE) \
-DBUILD_TESTING=$(BUILD_TESTING) \
-DCMAKE_SHARED_LINKER_FLAGS=$(LINKER_FLAGS) \
-DCMAKE_C_COMPILER=clang \
-DCMAKE_CXX_COMPILER=clang++ \
"-DCMAKE_CXX_FLAGS=$(CPP_VERSION) $(CLANG_THREAD_SAFETY_FLAGS)"
# Conditionally add flags specific for the Ninja build system
ifeq ($(BUILD_WITH_NINJA), ON)
BUILD_SYSTEM=Ninja
EVENT_HANDLERS=--event-handlers=console_cohesion+
# Include ccache specific flags for Ninja builds
COMMON_CMAKE_ARGS += \
-G${BUILD_SYSTEM} \
-DCMAKE_C_COMPILER_LAUNCHER=ccache \
-DCMAKE_CXX_COMPILER_LAUNCHER=ccache
else
BUILD_SYSTEM="Unix Makefiles"
# Just specify the generator for non-Ninja builds
COMMON_CMAKE_ARGS += \
-G${BUILD_SYSTEM}
endif
COMMON_COLCON_BUILD_FLAGS ?= \
--parallel-workers=${PARALLEL_JOBS} \
${EVENT_HANDLERS} \
--symlink-install \
--build-base $(build_dir)/build \
--install-base $(build_dir)/install \
--base-paths $(current_path)/motorium $(current_path)/lib
############################################################
# Define build and test targets
############################################################
define default-build-package
cd ${build_dir} && \
export MAKEFLAGS="-j ${PARALLEL_JOBS} -d" && \
source ${ros_source_file} && \
colcon build ${COMMON_COLCON_BUILD_FLAGS} --packages-up-to $(1) \
--cmake-args ${COMMON_CMAKE_ARGS} $(EXTRA_CMAKE_ARGS) && \
source $(build_dir)/install/setup.bash
endef
define default-build-python-package
cd ${build_dir} && \
source ${ros_source_file} && \
colcon build ${COMMON_COLCON_BUILD_FLAGS} --packages-up-to $(1)
endef
define default-test-package
cd ${build_dir} && \
source ${ros_source_file} && \
source $(build_dir)/install/setup.bash && \
colcon test --base-paths $(current_path)/motorium $(current_path)/lib --packages-select $(1) --event-handlers console_direct+ --return-code-on-test-failure
endef
############################################################
# Command Line Interface
############################################################
.PHONY: build-all build-debug build-release build-relwithdebinfo build \
test-all test $(addprefix build-,$(PACKAGES)) $(addprefix test-,$(PACKAGES))
build-all:
$(call default-build-package,$(PACKAGES))
$(addprefix build-,$(PACKAGES)):
$(call default-build-package,$(patsubst build-%,%,$@))
build:
@$(if $(PKG),$(call default-build-package,$(PKG)),@echo "Please specify a package to build by setting the PKG variable. Example: make build PKG=package_name")
build-debug:
@$(MAKE) BUILD_TYPE=Debug $(if $(PKG),build PKG=$(PKG),build-all)
build-release:
@$(MAKE) BUILD_TYPE=Release BUILD_TESTING=OFF $(if $(PKG),build PKG=$(PKG),build-all)
build-relwithdebinfo:
@$(MAKE) BUILD_TYPE=RelWithDebInfo $(if $(PKG),build PKG=$(PKG),build-all)
test-all: $(addprefix test-,$(PACKAGES))
@echo ""
@echo "=========================================="
@echo "Test Results Summary:"
@echo "=========================================="
@cd ${build_dir} && source ${ros_source_file} && colcon test-result --all
$(addprefix test-,$(PACKAGES)):
$(call default-test-package,$(patsubst test-%,%,$@))
test:
@$(if $(PKG),$(call default-test-package,$(PKG)),@echo "Please specify a package to test by setting the PKG variable. Example: make test PKG=package_name")
echo-packages:
@echo "Packages to be built: $(PACKAGES)"
update-submodules:
git submodule update --init --recursive
git-lfs:
git lfs install && git lfs pull
clean-ws:
cd ${build_dir} && \
rm -rf build install log .ccache
format:
find . -name "lib" -prune -o \( -name "*.cpp" -o -name "*.h" -o -name "*.hpp" \) -print | xargs clang-format -i && \
black . --exclude="lib/"