Skip to content

Commit 12b79a0

Browse files
Use CMake features to find NumPy (#333)
The current CMake script that is used to find NumPy does not always work for me, and sometimes causes problems on the different systems I build on. Given we have recently updated the minimum CMake version and the minimum pybind11 version, these changes now use the standard CMake tools to find Python3 and NumPy, and makes the links with the standard CMake targets. I specifically use Python3 in the CMake as the indicated pybind11 version no longer supports Python 2.7.
1 parent 74f1160 commit 12b79a0

6 files changed

Lines changed: 10 additions & 110 deletions

File tree

‎CMakeLists.txt‎

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
cmake_minimum_required(VERSION 3.29)
1111
project(xtensor-python)
1212

13-
set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake ${CMAKE_MODULE_PATH})
1413
set(XTENSOR_PYTHON_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/include)
1514

1615
# Versioning
@@ -46,7 +45,7 @@ else()
4645
message(STATUS "Found xtensor: ${xtensor_INCLUDE_DIRS}/xtensor")
4746
endif()
4847

49-
find_package(Python COMPONENTS Interpreter REQUIRED)
48+
find_package(Python3 COMPONENTS Development.Embed NumPy REQUIRED)
5049

5150
set(pybind11_REQUIRED_VERSION 3.0.0)
5251
if (NOT TARGET pybind11::headers)
@@ -60,14 +59,6 @@ else ()
6059
message(STATUS "Found pybind11 v${pybind11_VERSION}")
6160
endif ()
6261

63-
# Look for NumPy headers, except if NUMPY_INCLUDE_DIRS is passed,
64-
# which is required under some circumstances (such as wasm, where
65-
# there is no real python executable)
66-
if(NOT NUMPY_INCLUDE_DIRS)
67-
find_package(NumPy REQUIRED)
68-
endif()
69-
message(STATUS "Found numpy: ${NUMPY_INCLUDE_DIRS}")
70-
7162
# Build
7263
# =====
7364

@@ -84,10 +75,8 @@ set(XTENSOR_PYTHON_HEADERS
8475
)
8576

8677
add_library(xtensor-python INTERFACE)
87-
target_include_directories(xtensor-python INTERFACE
88-
"$<BUILD_INTERFACE:${XTENSOR_PYTHON_INCLUDE_DIR};${pybind11_INCLUDE_DIRS};${NUMPY_INCLUDE_DIRS}>"
89-
$<INSTALL_INTERFACE:include>)
90-
target_link_libraries(xtensor-python INTERFACE xtensor)
78+
target_include_directories(xtensor-python INTERFACE "$<BUILD_INTERFACE:${XTENSOR_PYTHON_INCLUDE_DIR}>" $<INSTALL_INTERFACE:include>)
79+
target_link_libraries(xtensor-python INTERFACE xtensor pybind11::pybind11 Python3::NumPy)
9180
get_target_property(inc_dir xtensor-python INTERFACE_INCLUDE_DIRECTORIES)
9281

9382
OPTION(BUILD_TESTS "xtensor test suite" OFF)

‎cmake/FindNumPy.cmake‎

Lines changed: 0 additions & 89 deletions
This file was deleted.

‎docs/source/examples/copy_cast/CMakeLists.txt‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ project(mymodule)
55
find_package(pybind11 CONFIG REQUIRED)
66
find_package(xtensor REQUIRED)
77
find_package(xtensor-python REQUIRED)
8-
find_package(Python REQUIRED COMPONENTS NumPy)
8+
find_package(Python3 REQUIRED COMPONENTS NumPy)
99

1010
pybind11_add_module(mymodule main.cpp)
11-
target_link_libraries(mymodule PUBLIC pybind11::module xtensor-python Python::NumPy)
11+
target_link_libraries(mymodule PUBLIC pybind11::module xtensor-python Python3::NumPy)
1212

1313
target_compile_definitions(mymodule PRIVATE VERSION_INFO=0.1.0)

‎docs/source/examples/readme_example_1/CMakeLists.txt‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ cmake_minimum_required(VERSION 3.29)
22

33
project(mymodule)
44

5-
find_package(Python REQUIRED COMPONENTS Interpreter Development NumPy)
5+
find_package(Python3 REQUIRED COMPONENTS Interpreter Development NumPy)
66
find_package(pybind11 REQUIRED CONFIG)
77
find_package(xtensor REQUIRED)
88
find_package(xtensor-python REQUIRED)
99

1010
pybind11_add_module(mymodule main.cpp)
11-
target_link_libraries(mymodule PUBLIC pybind11::module xtensor-python Python::NumPy)
11+
target_link_libraries(mymodule PUBLIC pybind11::module xtensor-python Python3::NumPy)
1212

1313
target_compile_definitions(mymodule PRIVATE VERSION_INFO=0.1.0)

‎docs/source/examples/sfinae/CMakeLists.txt‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ cmake_minimum_required(VERSION 3.29)
22

33
project(mymodule)
44

5-
find_package(Python REQUIRED COMPONENTS Interpreter Development NumPy)
5+
find_package(Python3 REQUIRED COMPONENTS Interpreter Development NumPy)
66
find_package(pybind11 REQUIRED CONFIG)
77
find_package(xtensor REQUIRED)
88
find_package(xtensor-python REQUIRED)
99

1010
pybind11_add_module(mymodule python.cpp)
11-
target_link_libraries(mymodule PUBLIC pybind11::module xtensor-python Python::NumPy)
11+
target_link_libraries(mymodule PUBLIC pybind11::module xtensor-python Python3::NumPy)
1212

1313
target_compile_definitions(mymodule PRIVATE VERSION_INFO=0.1.0)
1414

‎test/CMakeLists.txt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ set(XTENSOR_PYTHON_TESTS
8484
)
8585

8686
add_executable(test_xtensor_python ${XTENSOR_PYTHON_TESTS} ${XTENSOR_PYTHON_HEADERS})
87-
target_link_libraries(test_xtensor_python xtensor-python ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} ${PYTHON_LIBRARIES})
87+
target_link_libraries(test_xtensor_python xtensor-python ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} Python3::Python)
8888

8989
if(DOWNLOAD_GTEST OR GTEST_SRC_DIR)
9090
add_dependencies(test_xtensor_python gtest_main)

0 commit comments

Comments
 (0)