Skip to content

Commit

Permalink
[cmake] Fix finding python
Browse files Browse the repository at this point in the history
(cherry picked from commit 9e329f2)
  • Loading branch information
jslee02 committed Mar 17, 2024
1 parent 475aa1e commit b9e45b5
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 31 deletions.
4 changes: 2 additions & 2 deletions cmake/CodeCoverage.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,9 @@ ENDFUNCTION() # SETUP_TARGET_FOR_COVERAGE
# Pass them in list form, e.g.: "-j;2" for -j 2
FUNCTION(SETUP_TARGET_FOR_COVERAGE_COBERTURA _targetname _testrunner _outputname)

IF(NOT PYTHON_EXECUTABLE)
IF(NOT Python3_EXECUTABLE)
MESSAGE(FATAL_ERROR "Python not found! Aborting...")
ENDIF() # NOT PYTHON_EXECUTABLE
ENDIF() # NOT Python3_EXECUTABLE

IF(NOT GCOVR_PATH)
MESSAGE(FATAL_ERROR "gcovr not found! Aborting...")
Expand Down
2 changes: 2 additions & 0 deletions cmake/DARTFindDependencies.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ endif()
# Optional dependencies
#=======================

find_package(Python3 COMPONENTS Interpreter Development)

option(DART_SKIP_spdlog "If ON, do not use spdlog even if it is found." OFF)
mark_as_advanced(DART_SKIP_spdlog)
dart_find_package(spdlog)
Expand Down
16 changes: 15 additions & 1 deletion python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,20 @@ if(NOT DART_BUILD_DARTPY)
return()
endif()

# Set up pybind11
include(FetchContent)
FetchContent_Declare(
pybind11
GIT_REPOSITORY https://github.com/pybind/pybind11
GIT_TAG v2.11.1
)
FetchContent_MakeAvailable(pybind11)

if(NOT pybind11_FOUND)
message(WARNING "Disabling [dartpy] due to missing pybind11 >= 2.2.0.")
return()
endif()

set(DART_DARTPY_BUILD_DIR "${CMAKE_CURRENT_BINARY_DIR}/dartpy")

add_subdirectory(dartpy)
Expand All @@ -14,6 +28,6 @@ add_subdirectory(tutorials)

message(STATUS "")
message(STATUS "[ dartpy ]")
message(STATUS "- PYTHON_EXECUTABLE : ${PYTHON_EXECUTABLE}")
message(STATUS "- Python3_EXECUTABLE : ${Python3_EXECUTABLE}")
message(STATUS "- PYTHON_SITE_PACKAGES: ${PYTHON_SITE_PACKAGES}")
message(STATUS "- DARTPY_PYTEST_FOUND : ${DARTPY_PYTEST_FOUND}")
18 changes: 1 addition & 17 deletions python/dartpy/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,7 @@
#
# This file is provided under the "BSD-style" License

find_package(Python3 COMPONENTS Interpreter Development)

# Set up pybind11
include(FetchContent)
FetchContent_Declare(
pybind11
GIT_REPOSITORY https://github.com/pybind/pybind11
GIT_TAG v2.11.1
)
FetchContent_MakeAvailable(pybind11)

if(NOT pybind11_FOUND)
message(WARNING "Disabling [dartpy] due to missing pybind11 >= 2.2.0.")
return()
endif()

execute_process(COMMAND ${PYTHON_EXECUTABLE} -c
execute_process(COMMAND ${Python3_EXECUTABLE} -c
"from distutils.sysconfig import get_python_lib;\
print(get_python_lib(plat_specific=True))"
OUTPUT_VARIABLE PYTHON_SITE_PACKAGES
Expand Down
4 changes: 2 additions & 2 deletions python/examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ function(dartpy_add_example example_name) # ARGN for source file
set(source "${example_name}.py")
endif()
add_custom_target(${example_name}
COMMAND ${CMAKE_COMMAND} -E echo "Running pytest by: PYTHONPATH=${DART_DARTPY_BUILD_DIR} ${PYTHON_EXECUTABLE} ${source}"
COMMAND ${CMAKE_COMMAND} -E env "PYTHONPATH=${DART_DARTPY_BUILD_DIR} ${PYTHON_EXECUTABLE} ${source}"
COMMAND ${CMAKE_COMMAND} -E echo "Running pytest by: PYTHONPATH=${DART_DARTPY_BUILD_DIR} ${Python3_EXECUTABLE} ${source}"
COMMAND ${CMAKE_COMMAND} -E env "PYTHONPATH=${DART_DARTPY_BUILD_DIR} ${Python3_EXECUTABLE} ${source}"
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
SOURCES ${source}
)
Expand Down
14 changes: 7 additions & 7 deletions python/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
# Make sure pytest is installed
execute_process(
COMMAND ${PYTHON_EXECUTABLE} -c "import pytest; print(pytest.__version__)"
COMMAND ${Python3_EXECUTABLE} -c "import pytest; print(pytest.__version__)"
RESULT_VARIABLE pytest_not_found
OUTPUT_VARIABLE pytest_version
ERROR_QUIET
)
if(pytest_not_found)
message(WARNING "Running the tests requires pytest. Please install it manually"
" (try: ${PYTHON_EXECUTABLE} -m pip install pytest)"
" (try: ${Python3_EXECUTABLE} -m pip install pytest)"
)
set(DARTPY_PYTEST_FOUND FALSE)
elseif(pytest_version VERSION_LESS 3.0)
message(WARNING "Running the tests requires pytest >= 3.0. Found: ${pytest_version}"
"Please update it (try: ${PYTHON_EXECUTABLE} -m pip install -U pytest)"
"Please update it (try: ${Python3_EXECUTABLE} -m pip install -U pytest)"
)
set(DARTPY_PYTEST_FOUND FALSE)
else()
Expand All @@ -29,8 +29,8 @@ file(GLOB_RECURSE dartpy_test_files "test_*.py")
# Add custom target to run the tests
if(DARTPY_PYTEST_FOUND)
add_custom_target(pytest
COMMAND ${CMAKE_COMMAND} -E echo "Running pytest by: PYTHONPATH=${DART_DARTPY_BUILD_DIR} ${PYTHON_EXECUTABLE} -m pytest [sources]"
COMMAND ${CMAKE_COMMAND} -E env PYTHONPATH="${DART_DARTPY_BUILD_DIR}" "${PYTHON_EXECUTABLE}" -m pytest ${dartpy_test_files} -v
COMMAND ${CMAKE_COMMAND} -E echo "Running pytest by: PYTHONPATH=${DART_DARTPY_BUILD_DIR} ${Python3_EXECUTABLE} -m pytest [sources]"
COMMAND ${CMAKE_COMMAND} -E env PYTHONPATH="${DART_DARTPY_BUILD_DIR}" "${Python3_EXECUTABLE}" -m pytest ${dartpy_test_files} -v
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
SOURCES ${dartpy_test_files} ${dartpy_test_utils}
DEPENDS dartpy
Expand All @@ -53,8 +53,8 @@ function(dartpy_add_test test_name) # ARGN for source file
set(source "${test_name}.py")
endif()
add_custom_target(${test_name}
COMMAND ${CMAKE_COMMAND} -E echo "Running pytest by: PYTHONPATH=${DART_DARTPY_BUILD_DIR} ${PYTHON_EXECUTABLE} ${source}"
COMMAND ${CMAKE_COMMAND} -E env PYTHONPATH=${DART_DARTPY_BUILD_DIR} ${PYTHON_EXECUTABLE} ${source}
COMMAND ${CMAKE_COMMAND} -E echo "Running pytest by: PYTHONPATH=${DART_DARTPY_BUILD_DIR} ${Python3_EXECUTABLE} ${source}"
COMMAND ${CMAKE_COMMAND} -E env PYTHONPATH=${DART_DARTPY_BUILD_DIR} ${Python3_EXECUTABLE} ${source}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
SOURCES ${source}
DEPENDS dartpy
Expand Down
4 changes: 2 additions & 2 deletions python/tutorials/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ function(dartpy_add_tutorial tutorial_name) # ARGN for source file
set(source "${tutorial_name}.py")
endif()
add_custom_target(${tutorial_name}
COMMAND ${CMAKE_COMMAND} -E echo "Running pytest by: PYTHONPATH=${DART_DARTPY_BUILD_DIR} ${PYTHON_EXECUTABLE} ${source}"
COMMAND ${CMAKE_COMMAND} -E env PYTHONPATH=${DART_DARTPY_BUILD_DIR} ${PYTHON_EXECUTABLE} ${source}
COMMAND ${CMAKE_COMMAND} -E echo "Running pytest by: PYTHONPATH=${DART_DARTPY_BUILD_DIR} ${Python3_EXECUTABLE} ${source}"
COMMAND ${CMAKE_COMMAND} -E env PYTHONPATH=${DART_DARTPY_BUILD_DIR} ${Python3_EXECUTABLE} ${source}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
SOURCES ${source}
)
Expand Down

0 comments on commit b9e45b5

Please sign in to comment.