Skip to content

Commit f60627c

Browse files
committed
refactor: Integrate compiler option macros and GCC version detection into CMake configuration for improved build settings
1 parent f35f3ea commit f60627c

File tree

3 files changed

+63
-1
lines changed

3 files changed

+63
-1
lines changed

CMakeLists.txt

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,64 @@
1+
# Portions of this file are derived from ros2_control_cmake (Apache 2.0 License)
2+
# Copyright 2025 AIT - Austrian Institute of Technology GmbH
3+
# See the LICENSE file for details.
4+
15
cmake_minimum_required(VERSION 3.16)
26
project(dynamixel_hardware_interface)
37

4-
find_package(ros2_control_cmake REQUIRED)
8+
# ==== Begin inlined content from ros2_control_cmake/cmake/ros2_control.cmake ====
9+
# Macro to extract GCC_MAJOR_VERSION and GCC_MINOR_VERSION
10+
macro(extract_gcc_version)
11+
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
12+
string(REPLACE "." ";" VERSION_LIST ${CMAKE_CXX_COMPILER_VERSION})
13+
list(GET VERSION_LIST 0 GCC_MAJOR_VERSION)
14+
list(GET VERSION_LIST 1 GCC_MINOR_VERSION)
15+
16+
message(STATUS "Detected GCC Version: ${CMAKE_CXX_COMPILER_VERSION} (Major: ${GCC_MAJOR_VERSION}, Minor: ${GCC_MINOR_VERSION})")
17+
18+
# Convert to a number to avoid string comparison issues
19+
if(GCC_MAJOR_VERSION MATCHES "^[0-9]+$")
20+
math(EXPR GCC_MAJOR_VERSION "${GCC_MAJOR_VERSION}")
21+
endif()
22+
if(GCC_MINOR_VERSION MATCHES "^[0-9]+$")
23+
math(EXPR GCC_MINOR_VERSION "${GCC_MINOR_VERSION}")
24+
endif()
25+
endif()
26+
endmacro()
27+
28+
# set compiler options depending on detected compiler
29+
macro(set_compiler_options)
30+
if(CMAKE_CXX_COMPILER_ID MATCHES "(GNU|Clang)")
31+
add_compile_options(-Wall -Wextra -Wpedantic -Werror=conversion -Werror=unused-but-set-variable
32+
-Werror=return-type -Werror=shadow -Werror=format
33+
-Werror=missing-braces)
34+
message(STATUS "Compiler warnings enabled for ${CMAKE_CXX_COMPILER_ID}")
35+
36+
# https://docs.ros.org/en/rolling/How-To-Guides/Ament-CMake-Documentation.html#compiler-and-linker-options
37+
if(NOT CMAKE_C_STANDARD)
38+
set(CMAKE_C_STANDARD 99)
39+
endif()
40+
if(NOT CMAKE_CXX_STANDARD)
41+
set(CMAKE_CXX_STANDARD 17)
42+
endif()
43+
44+
# Extract major version if g++ is used
45+
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
46+
extract_gcc_version()
47+
if(DEFINED GCC_MAJOR_VERSION AND GCC_MAJOR_VERSION GREATER 10)
48+
# GCC 11 introduced -Werror=range-loop-construct
49+
add_compile_options(-Werror=range-loop-construct)
50+
endif()
51+
endif()
52+
endif()
53+
endmacro()
54+
55+
# using this instead of visibility macros
56+
# S1 from https://github.com/ros-controls/ros2_controllers/issues/1053
57+
macro(export_windows_symbols)
58+
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
59+
endmacro()
60+
# ==== End inlined content from ros2_control_cmake/cmake/ros2_control.cmake ====
61+
562
set_compiler_options()
663
export_windows_symbols()
764

NOTICE

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Portions of this package are derived from ros2_control_cmake, which is licensed under the Apache License, Version 2.0.
2+
Copyright 2025 AIT - Austrian Institute of Technology GmbH
3+
4+
See the CMakeLists.txt for details on which code is derived.

ros2_control_cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit d10436128442a805e897b34ce910459ce2159fd2

0 commit comments

Comments
 (0)