Skip to content

Commit 0425e86

Browse files
authored
Set hints to find the python version we actually want. (#785)
The comment in the commit explains the reasoning behind it. Signed-off-by: Chris Lalancette <[email protected]>
1 parent 6f6910a commit 0425e86

File tree

5 files changed

+80
-0
lines changed

5 files changed

+80
-0
lines changed

rosidl_generator_c/cmake/rosidl_generator_c_generate_interfaces.cmake

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,22 @@ rosidl_write_generator_arguments(
9292
ROS_INTERFACE_FILES "${_target_sources}"
9393
)
9494

95+
# By default, without the settings below, find_package(Python3) will attempt
96+
# to find the newest python version it can, and additionally will find the
97+
# most specific version. For instance, on a system that has
98+
# /usr/bin/python3.10, /usr/bin/python3.11, and /usr/bin/python3, it will find
99+
# /usr/bin/python3.11, even if /usr/bin/python3 points to /usr/bin/python3.10.
100+
# The behavior we want is to prefer the "system" installed version unless the
101+
# user specifically tells us othewise through the Python3_EXECUTABLE hint.
102+
# Setting CMP0094 to NEW means that the search will stop after the first
103+
# python version is found. Setting Python3_FIND_UNVERSIONED_NAMES means that
104+
# the search will prefer /usr/bin/python3 over /usr/bin/python3.11. And that
105+
# latter functionality is only available in CMake 3.20 or later, so we need
106+
# at least that version.
107+
cmake_minimum_required(VERSION 3.20)
108+
cmake_policy(SET CMP0094 NEW)
109+
set(Python3_FIND_UNVERSIONED_NAMES FIRST)
110+
95111
find_package(Python3 REQUIRED COMPONENTS Interpreter)
96112

97113
set(disable_description_codegen_arg)

rosidl_generator_cpp/cmake/rosidl_generator_cpp_generate_interfaces.cmake

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,22 @@ rosidl_write_generator_arguments(
9494
TYPE_DESCRIPTION_TUPLES "${${rosidl_generate_interfaces_TARGET}__DESCRIPTION_TUPLES}"
9595
)
9696

97+
# By default, without the settings below, find_package(Python3) will attempt
98+
# to find the newest python version it can, and additionally will find the
99+
# most specific version. For instance, on a system that has
100+
# /usr/bin/python3.10, /usr/bin/python3.11, and /usr/bin/python3, it will find
101+
# /usr/bin/python3.11, even if /usr/bin/python3 points to /usr/bin/python3.10.
102+
# The behavior we want is to prefer the "system" installed version unless the
103+
# user specifically tells us othewise through the Python3_EXECUTABLE hint.
104+
# Setting CMP0094 to NEW means that the search will stop after the first
105+
# python version is found. Setting Python3_FIND_UNVERSIONED_NAMES means that
106+
# the search will prefer /usr/bin/python3 over /usr/bin/python3.11. And that
107+
# latter functionality is only available in CMake 3.20 or later, so we need
108+
# at least that version.
109+
cmake_minimum_required(VERSION 3.20)
110+
cmake_policy(SET CMP0094 NEW)
111+
set(Python3_FIND_UNVERSIONED_NAMES FIRST)
112+
97113
find_package(Python3 REQUIRED COMPONENTS Interpreter)
98114

99115
add_custom_command(

rosidl_generator_type_description/cmake/rosidl_generator_type_description_generate_interfaces.cmake

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,22 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
# By default, without the settings below, find_package(Python3) will attempt
16+
# to find the newest python version it can, and additionally will find the
17+
# most specific version. For instance, on a system that has
18+
# /usr/bin/python3.10, /usr/bin/python3.11, and /usr/bin/python3, it will find
19+
# /usr/bin/python3.11, even if /usr/bin/python3 points to /usr/bin/python3.10.
20+
# The behavior we want is to prefer the "system" installed version unless the
21+
# user specifically tells us othewise through the Python3_EXECUTABLE hint.
22+
# Setting CMP0094 to NEW means that the search will stop after the first
23+
# python version is found. Setting Python3_FIND_UNVERSIONED_NAMES means that
24+
# the search will prefer /usr/bin/python3 over /usr/bin/python3.11. And that
25+
# latter functionality is only available in CMake 3.20 or later, so we need
26+
# at least that version.
27+
cmake_minimum_required(VERSION 3.20)
28+
cmake_policy(SET CMP0094 NEW)
29+
set(Python3_FIND_UNVERSIONED_NAMES FIRST)
30+
1531
find_package(Python3 REQUIRED COMPONENTS Interpreter)
1632

1733
set(_output_path "${CMAKE_CURRENT_BINARY_DIR}/rosidl_generator_type_description/${PROJECT_NAME}")

rosidl_typesupport_introspection_c/cmake/rosidl_typesupport_introspection_c_generate_interfaces.cmake

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,22 @@ rosidl_write_generator_arguments(
7676
TARGET_DEPENDENCIES ${target_dependencies}
7777
)
7878

79+
# By default, without the settings below, find_package(Python3) will attempt
80+
# to find the newest python version it can, and additionally will find the
81+
# most specific version. For instance, on a system that has
82+
# /usr/bin/python3.10, /usr/bin/python3.11, and /usr/bin/python3, it will find
83+
# /usr/bin/python3.11, even if /usr/bin/python3 points to /usr/bin/python3.10.
84+
# The behavior we want is to prefer the "system" installed version unless the
85+
# user specifically tells us othewise through the Python3_EXECUTABLE hint.
86+
# Setting CMP0094 to NEW means that the search will stop after the first
87+
# python version is found. Setting Python3_FIND_UNVERSIONED_NAMES means that
88+
# the search will prefer /usr/bin/python3 over /usr/bin/python3.11. And that
89+
# latter functionality is only available in CMake 3.20 or later, so we need
90+
# at least that version.
91+
cmake_minimum_required(VERSION 3.20)
92+
cmake_policy(SET CMP0094 NEW)
93+
set(Python3_FIND_UNVERSIONED_NAMES FIRST)
94+
7995
find_package(Python3 REQUIRED COMPONENTS Interpreter)
8096

8197
add_custom_command(

rosidl_typesupport_introspection_cpp/cmake/rosidl_typesupport_introspection_cpp_generate_interfaces.cmake

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,22 @@ rosidl_write_generator_arguments(
7676
TARGET_DEPENDENCIES ${target_dependencies}
7777
)
7878

79+
# By default, without the settings below, find_package(Python3) will attempt
80+
# to find the newest python version it can, and additionally will find the
81+
# most specific version. For instance, on a system that has
82+
# /usr/bin/python3.10, /usr/bin/python3.11, and /usr/bin/python3, it will find
83+
# /usr/bin/python3.11, even if /usr/bin/python3 points to /usr/bin/python3.10.
84+
# The behavior we want is to prefer the "system" installed version unless the
85+
# user specifically tells us othewise through the Python3_EXECUTABLE hint.
86+
# Setting CMP0094 to NEW means that the search will stop after the first
87+
# python version is found. Setting Python3_FIND_UNVERSIONED_NAMES means that
88+
# the search will prefer /usr/bin/python3 over /usr/bin/python3.11. And that
89+
# latter functionality is only available in CMake 3.20 or later, so we need
90+
# at least that version.
91+
cmake_minimum_required(VERSION 3.20)
92+
cmake_policy(SET CMP0094 NEW)
93+
set(Python3_FIND_UNVERSIONED_NAMES FIRST)
94+
7995
find_package(Python3 REQUIRED COMPONENTS Interpreter)
8096

8197
add_custom_command(

0 commit comments

Comments
 (0)