Skip to content

Commit

Permalink
Use FindPython3 instead of FindPythonInterp (#355)
Browse files Browse the repository at this point in the history
* First pass using FindPython3, no testing done

Signed-off-by: Shane Loretz <sloretz@osrfoundation.org>

* Use config-time python to avoid CMP0087

Signed-off-by: Shane Loretz <sloretz@osrfoundation.org>

* Set ament_package_PYTHON_EXECUTABLE

Signed-off-by: Shane Loretz <sloretz@osrfoundation.org>

* move getting executable path out of if block

Signed-off-by: Shane Loretz <sloretz@osrfoundation.org>

* Fatal error if Python3::Interpreter not present

Signed-off-by: Shane Loretz <sloretz@osrfoundation.org>

* Remove Python 2 logic

Signed-off-by: Shane Loretz <sloretz@osrfoundation.org>
  • Loading branch information
sloretz committed Aug 12, 2021
1 parent 5a5b57c commit 0998713
Show file tree
Hide file tree
Showing 35 changed files with 80 additions and 70 deletions.
2 changes: 1 addition & 1 deletion ament_cmake/CMakeLists.txt
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.5)
cmake_minimum_required(VERSION 3.12)

project(ament_cmake NONE)

Expand Down
2 changes: 1 addition & 1 deletion ament_cmake_auto/CMakeLists.txt
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.5)
cmake_minimum_required(VERSION 3.12)

project(ament_cmake_auto NONE)

Expand Down
2 changes: 1 addition & 1 deletion ament_cmake_core/CMakeLists.txt
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.5)
cmake_minimum_required(VERSION 3.12)

project(ament_cmake_core NONE)

Expand Down
8 changes: 4 additions & 4 deletions ament_cmake_core/ament_cmake_package_templates-extras.cmake
Expand Up @@ -13,10 +13,9 @@
# limitations under the License.

# extract information from ament_package.templates
if(NOT PYTHON_EXECUTABLE)
if(NOT TARGET Python3::Interpreter)
message(FATAL_ERROR
"ament_cmake_package_templates: variable 'PYTHON_EXECUTABLE' must not be "
"empty")
"ament_cmake_package_templates: target 'Python3::Interpreter' must exist")
endif()

# stamp script to generate CMake code
Expand All @@ -27,8 +26,9 @@ stamp("${_generator}")
# invoke generator script
set(_generated_file
"${CMAKE_CURRENT_BINARY_DIR}/ament_cmake_package_templates/templates.cmake")
get_executable_path(_python_interpreter Python3::Interpreter CONFIGURE)
set(_cmd
"${PYTHON_EXECUTABLE}"
"${_python_interpreter}"
"${_generator}"
"${_generated_file}"
)
Expand Down
7 changes: 4 additions & 3 deletions ament_cmake_core/cmake/core/ament_package_xml.cmake
Expand Up @@ -75,12 +75,13 @@ macro(_ament_package_xml dest_dir)

# extract information from package.xml
file(MAKE_DIRECTORY ${dest_dir})
if(NOT PYTHON_EXECUTABLE)
if(NOT TARGET Python3::Interpreter)
message(FATAL_ERROR
"ament_package_xml() variable 'PYTHON_EXECUTABLE' must not be empty")
"ament_package_xml() target 'Python3::Interpreter' must exist")
endif()
get_executable_path(_python_interpreter Python3::Interpreter CONFIGURE)
set(_cmd
"${PYTHON_EXECUTABLE}"
"${_python_interpreter}"
"${ament_cmake_core_DIR}/core/package_xml_2_cmake.py"
"${PACKAGE_XML_DIRECTORY}/package.xml"
"${dest_dir}/package.cmake"
Expand Down
15 changes: 7 additions & 8 deletions ament_cmake_core/cmake/core/python.cmake
Expand Up @@ -12,12 +12,11 @@
# See the License for the specific language governing permissions and
# limitations under the License.

set(PYTHON_VERSION "" CACHE STRING
"Specify specific Python version to use ('major.minor' or 'major')")
# if not specified otherwise use Python 3
if(NOT PYTHON_VERSION)
set(PYTHON_VERSION "3")
# ament_cmake needs a Python 3 interpreter
# If a specific Python version is required then find it before finding ament_cmake
# Example:
# find_package(Python3 3.8 REQUIRED)
# find_package(ament_cmake REQUIRED)
if (NOT TARGET Python3::Interpreter)
find_package(Python3 REQUIRED COMPONENTS Interpreter)
endif()

find_package(PythonInterp ${PYTHON_VERSION} REQUIRED)
message(STATUS "Using PYTHON_EXECUTABLE: ${PYTHON_EXECUTABLE}")
Expand Up @@ -23,6 +23,14 @@ function(ament_generate_environment)
"ament_generate_environment() called with unused arguments: ${ARGN}")
endif()

if(NOT TARGET Python3::Interpreter)
message(FATAL_ERROR
"ament_generate_environment() target 'Python3::Interpreter' must exist")
endif()

# Default python used in local_setup.* scripts
get_executable_path(ament_package_PYTHON_EXECUTABLE Python3::Interpreter CONFIGURE)

# configure and install setup files
foreach(file ${ament_cmake_package_templates_PREFIX_LEVEL})
# check if the file is a template
Expand Down
2 changes: 1 addition & 1 deletion ament_cmake_export_definitions/CMakeLists.txt
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.5)
cmake_minimum_required(VERSION 3.12)

project(ament_cmake_export_definitions NONE)

Expand Down
2 changes: 1 addition & 1 deletion ament_cmake_export_dependencies/CMakeLists.txt
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.5)
cmake_minimum_required(VERSION 3.12)

project(ament_cmake_export_dependencies NONE)

Expand Down
2 changes: 1 addition & 1 deletion ament_cmake_export_include_directories/CMakeLists.txt
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.5)
cmake_minimum_required(VERSION 3.12)

project(ament_cmake_export_include_directories NONE)

Expand Down
2 changes: 1 addition & 1 deletion ament_cmake_export_interfaces/CMakeLists.txt
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.5)
cmake_minimum_required(VERSION 3.12)

project(ament_cmake_export_interfaces NONE)

Expand Down
2 changes: 1 addition & 1 deletion ament_cmake_export_libraries/CMakeLists.txt
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.5)
cmake_minimum_required(VERSION 3.12)

project(ament_cmake_export_libraries NONE)

Expand Down
2 changes: 1 addition & 1 deletion ament_cmake_export_link_flags/CMakeLists.txt
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.5)
cmake_minimum_required(VERSION 3.12)

project(ament_cmake_export_link_flags NONE)

Expand Down
2 changes: 1 addition & 1 deletion ament_cmake_export_targets/CMakeLists.txt
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.5)
cmake_minimum_required(VERSION 3.12)

project(ament_cmake_export_targets NONE)

Expand Down
2 changes: 1 addition & 1 deletion ament_cmake_gmock/CMakeLists.txt
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.5)
cmake_minimum_required(VERSION 3.12)

project(ament_cmake_gmock NONE)

Expand Down
2 changes: 1 addition & 1 deletion ament_cmake_google_benchmark/CMakeLists.txt
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.5)
cmake_minimum_required(VERSION 3.12)
project(ament_cmake_google_benchmark NONE)

# find dependencies
Expand Down
Expand Up @@ -66,11 +66,13 @@ function(ament_add_google_benchmark_test target)
set(OVERLAY_ARG "--result-file-overlay" "${AMENT_CMAKE_GOOGLE_BENCHMARK_OVERLAY}")
endif()

get_executable_path(python_interpreter Python3::Interpreter BUILD)

set(executable "$<TARGET_FILE:${target}>")
set(benchmark_out "${AMENT_TEST_RESULTS_DIR}/${PROJECT_NAME}/${target}.google_benchmark.json")
set(common_out "${AMENT_TEST_RESULTS_DIR}/${PROJECT_NAME}/${target}.benchmark.json")
set(cmd
"${PYTHON_EXECUTABLE}" "-u" "${ament_cmake_google_benchmark_DIR}/run_and_convert.py"
"${python_interpreter}" "-u" "${ament_cmake_google_benchmark_DIR}/run_and_convert.py"
"${benchmark_out}" "${common_out}" "--package-name" "${PROJECT_NAME}"
${OVERLAY_ARG}
"--command" "${executable}"
Expand Down
2 changes: 1 addition & 1 deletion ament_cmake_gtest/CMakeLists.txt
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.5)
cmake_minimum_required(VERSION 3.12)

project(ament_cmake_gtest NONE)

Expand Down
2 changes: 1 addition & 1 deletion ament_cmake_include_directories/CMakeLists.txt
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.5)
cmake_minimum_required(VERSION 3.12)

project(ament_cmake_include_directories NONE)

Expand Down
2 changes: 1 addition & 1 deletion ament_cmake_libraries/CMakeLists.txt
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.5)
cmake_minimum_required(VERSION 3.12)

project(ament_cmake_libraries NONE)

Expand Down
2 changes: 1 addition & 1 deletion ament_cmake_nose/CMakeLists.txt
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.5)
cmake_minimum_required(VERSION 3.12)

project(ament_cmake_nose NONE)

Expand Down
18 changes: 7 additions & 11 deletions ament_cmake_nose/ament_cmake_nose-extras.cmake
Expand Up @@ -23,15 +23,16 @@ macro(_ament_cmake_nose_find_nosetests)
find_package(ament_cmake_test QUIET REQUIRED)

find_program(NOSETESTS NAMES
"nosetests${PYTHON_VERSION_MAJOR}.${PYTHON_VERSION_MINOR}"
"nosetests-${PYTHON_VERSION_MAJOR}.${PYTHON_VERSION_MINOR}"
"nosetests${PYTHON_VERSION_MAJOR}"
"nosetests-${PYTHON_VERSION_MAJOR}"
"nosetests${Python3_VERSION_MAJOR}.${Python3_VERSION_MINOR}"
"nosetests-${Python3_VERSION_MAJOR}.${Python3_VERSION_MINOR}"
"nosetests${Python3_VERSION_MAJOR}"
"nosetests-${Python3_VERSION_MAJOR}"
"nosetests")
if(NOSETESTS)
# if Python is located in a path containing spaces the shebang line of nosetests is invalid
# to avoid using the shebang line of nosetests the script is being invoked through Python
set(_cmd "${PYTHON_EXECUTABLE}" "${NOSETESTS}" "--version")
get_executable_path(_python_interpreter Python3::Interpreter CONFIGURE)
set(_cmd "${_python_interpreter}" "${NOSETESTS}" "--version")
execute_process(
COMMAND ${_cmd}
RESULT_VARIABLE _res
Expand All @@ -49,14 +50,9 @@ macro(_ament_cmake_nose_find_nosetests)
list(GET _output_list 1 NOSETESTS_VERSION)
message(STATUS "Using Python nosetests: ${NOSETESTS} (${NOSETESTS_VERSION})")
else()
if(PYTHON_VERSION_MAJOR STREQUAL "3")
set(_python_nosetests_package "python3-nose")
else()
set(_python_nosetests_package "python-nose")
endif()
message(WARNING
"'nosetests' not found, Python nose tests can not be run (e.g. on "
"Ubuntu/Debian install the package '${_python_nosetests_package}')")
"Ubuntu/Debian install the package 'python3-nose')")
endif()
endif()
endmacro()
Expand Down
6 changes: 3 additions & 3 deletions ament_cmake_nose/cmake/ament_add_nose_test.cmake
Expand Up @@ -22,8 +22,8 @@
# :type path: string
# :param SKIP_TEST: if set mark the test as being skipped
# :type SKIP_TEST: option
# :param PYTHON_EXECUTABLE: absolute path to the executable used to run the test,
# default to the CMake variable with the same name returned by FindPythonInterp
# :param PYTHON_EXECUTABLE: Python executable used to run the test.
# It defaults to the CMake executable target Python3::Interpreter.
# :type PYTHON_EXECUTABLE: string
# :param RUNNER: the path to the test runner script (default: see ament_add_test).
# :type RUNNER: string
Expand Down Expand Up @@ -72,7 +72,7 @@ function(_ament_add_nose_test testname path)
"ament_add_nose_test() the path '${path}' does not exist")
endif()
if(NOT ARG_PYTHON_EXECUTABLE)
set(ARG_PYTHON_EXECUTABLE "${PYTHON_EXECUTABLE}")
set(ARG_PYTHON_EXECUTABLE Python3::Interpreter)
endif()

get_executable_path(python_interpreter "${ARG_PYTHON_EXECUTABLE}" BUILD)
Expand Down
2 changes: 1 addition & 1 deletion ament_cmake_pytest/CMakeLists.txt
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.5)
cmake_minimum_required(VERSION 3.12)

project(ament_cmake_pytest NONE)

Expand Down
6 changes: 3 additions & 3 deletions ament_cmake_pytest/cmake/ament_add_pytest_test.cmake
Expand Up @@ -22,8 +22,8 @@
# :type path: string
# :param SKIP_TEST: if set mark the test as being skipped
# :type SKIP_TEST: option
# :param PYTHON_EXECUTABLE: absolute path to the executable used to run the test,
# default to the CMake variable with the same name returned by FindPythonInterp
# :param PYTHON_EXECUTABLE: Python executable used to run the test.
# It defaults to the CMake executable target Python3::Interpreter.
# :type PYTHON_EXECUTABLE: string
# :param RUNNER: the path to the test runner script (default: see ament_add_test).
# :type RUNNER: string
Expand Down Expand Up @@ -68,7 +68,7 @@ function(ament_add_pytest_test testname path)
"ament_add_pytest_test() the path '${path}' does not exist")
endif()
if(NOT ARG_PYTHON_EXECUTABLE)
set(ARG_PYTHON_EXECUTABLE "${PYTHON_EXECUTABLE}")
set(ARG_PYTHON_EXECUTABLE Python3::Interpreter)
endif()

get_executable_path(python_interpreter "${ARG_PYTHON_EXECUTABLE}" BUILD)
Expand Down
7 changes: 3 additions & 4 deletions ament_cmake_pytest/cmake/ament_get_pytest_cov_version.cmake
Expand Up @@ -17,9 +17,8 @@
#
# :param var: the output variable name
# :type var: string
# :param PYTHON_EXECUTABLE: absolute path to the Python interpreter to be used,
# default to the CMake variable with the same name returned by
# FindPythonInterp
# :param PYTHON_EXECUTABLE: Python executable used to check the version
# It defaults to the CMake executable target Python3::Interpreter.
# :type PYTHON_EXECUTABLE: string
#
# @public
Expand All @@ -32,7 +31,7 @@ function(ament_get_pytest_cov_version var)
endif()

if(NOT ARG_PYTHON_EXECUTABLE)
set(ARG_PYTHON_EXECUTABLE "${PYTHON_EXECUTABLE}")
set(ARG_PYTHON_EXECUTABLE Python3::Interpreter)
endif()

get_executable_path(python_interpreter "${ARG_PYTHON_EXECUTABLE}" CONFIGURE)
Expand Down
7 changes: 3 additions & 4 deletions ament_cmake_pytest/cmake/ament_has_pytest.cmake
Expand Up @@ -20,9 +20,8 @@
# :param QUIET: suppress the CMake warning if pytest is not found, if not set
# and pytest was not found a CMake warning is printed
# :type QUIET: option
# :param PYTHON_EXECUTABLE: absolute path to the Python interpreter to be used,
# default to the CMake variable with the same name returned by
# FindPythonInterp
# :param PYTHON_EXECUTABLE: Python executable used to check for pytest
# It defaults to the CMake executable target Python3::Interpreter.
# :type PYTHON_EXECUTABLE: string
#
# @public
Expand All @@ -35,7 +34,7 @@ function(ament_has_pytest var)
endif()

if(NOT ARG_PYTHON_EXECUTABLE)
set(ARG_PYTHON_EXECUTABLE "${PYTHON_EXECUTABLE}")
set(ARG_PYTHON_EXECUTABLE Python3::Interpreter)
endif()

get_executable_path(python_interpreter "${ARG_PYTHON_EXECUTABLE}" CONFIGURE)
Expand Down
2 changes: 1 addition & 1 deletion ament_cmake_python/CMakeLists.txt
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.5)
cmake_minimum_required(VERSION 3.12)

project(ament_cmake_python NONE)

Expand Down
5 changes: 3 additions & 2 deletions ament_cmake_python/ament_cmake_python-extras.cmake
Expand Up @@ -48,9 +48,10 @@ macro(_ament_cmake_python_get_python_install_dir)
"import os"
"print(os.path.relpath(get_python_lib(prefix='${CMAKE_INSTALL_PREFIX}'), start='${CMAKE_INSTALL_PREFIX}').replace(os.sep, '/'))"
)
get_executable_path(_python_interpreter Python3::Interpreter CONFIGURE)
execute_process(
COMMAND
"${PYTHON_EXECUTABLE}"
"${_python_interpreter}"
"-c"
"${_python_code}"
OUTPUT_VARIABLE _output
Expand All @@ -59,7 +60,7 @@ macro(_ament_cmake_python_get_python_install_dir)
)
if(NOT _result EQUAL 0)
message(FATAL_ERROR
"execute_process(${PYTHON_EXECUTABLE} -c '${_python_code}') returned "
"execute_process(${_python_interpreter} -c '${_python_code}') returned "
"error code ${_result}")
endif()

Expand Down
3 changes: 2 additions & 1 deletion ament_cmake_python/cmake/ament_python_install_module.cmake
Expand Up @@ -59,11 +59,12 @@ function(_ament_cmake_python_install_module module_file)
)
get_filename_component(module_file "${module_file}" NAME)
if(NOT ARG_SKIP_COMPILE)
get_executable_path(python_interpreter Python3::Interpreter CONFIGURE)
# compile Python files
install(CODE
"execute_process(
COMMAND
\"${PYTHON_EXECUTABLE}\" \"-m\" \"compileall\"
\"${python_interpreter}\" \"-m\" \"compileall\"
\"${CMAKE_INSTALL_PREFIX}/${destination}/${module_file}\"
)"
)
Expand Down
11 changes: 7 additions & 4 deletions ament_cmake_python/cmake/ament_python_install_package.cmake
Expand Up @@ -120,14 +120,16 @@ setup(
list(APPEND egg_dependencies ament_cmake_python_symlink_${package_name}_setup)
endif()

get_executable_path(python_interpreter Python3::Interpreter BUILD)

add_custom_target(
ament_cmake_python_build_${package_name}_egg ALL
COMMAND ${PYTHON_EXECUTABLE} setup.py egg_info
COMMAND ${python_interpreter} setup.py egg_info
WORKING_DIRECTORY "${build_dir}"
DEPENDS ${egg_dependencies}
)

set(python_version "py${PYTHON_VERSION_MAJOR}.${PYTHON_VERSION_MINOR}")
set(python_version "py${Python3_VERSION_MAJOR}.${Python3_VERSION_MINOR}")

set(egg_name "${package_name}")
set(egg_install_name "${egg_name}-${ARG_VERSION}")
Expand All @@ -143,7 +145,7 @@ setup(

add_custom_target(
ament_cmake_python_build_${package_name}_scripts ALL
COMMAND ${PYTHON_EXECUTABLE} setup.py install_scripts -d scripts
COMMAND ${python_interpreter} setup.py install_scripts -d scripts
WORKING_DIRECTORY "${build_dir}"
DEPENDS ${egg_dependencies}
)
Expand All @@ -168,11 +170,12 @@ setup(
)

if(NOT ARG_SKIP_COMPILE)
get_executable_path(python_interpreter_config Python3::Interpreter CONFIGURE)
# compile Python files
install(CODE
"execute_process(
COMMAND
\"${PYTHON_EXECUTABLE}\" \"-m\" \"compileall\"
\"${python_interpreter_config}\" \"-m\" \"compileall\"
\"${CMAKE_INSTALL_PREFIX}/${ARG_DESTINATION}/${package_name}\"
)"
)
Expand Down

0 comments on commit 0998713

Please sign in to comment.