From 115083ddd1fb474f2a1f61c8744ec8f4c447c942 Mon Sep 17 00:00:00 2001 From: Julien Schueller Date: Tue, 3 Dec 2024 21:22:41 +0100 Subject: [PATCH] CMake: Rewrite NumPy detection --- .github/workflows/build.yml | 4 +- CMakeLists.txt | 10 ++--- cmake/FindNumPy.cmake | 86 ------------------------------------- src/swig/CMakeLists.txt | 7 +-- test/CMakeLists.txt | 2 +- 5 files changed, 11 insertions(+), 98 deletions(-) delete mode 100644 cmake/FindNumPy.cmake diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 33bf39d0..4abae4d3 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -23,7 +23,6 @@ jobs: - uses: actions/checkout@v4 - name: Build run: | - brew install pkgconf --overwrite --force brew install swig guile mkdir build && pushd build cmake -DCMAKE_INSTALL_PREFIX=~/.local -DNLOPT_FORTRAN=OFF -DNLOPT_TESTS=ON .. @@ -33,6 +32,9 @@ jobs: runs-on: windows-latest steps: - uses: actions/checkout@v4 + - uses: actions/setup-python@v5 + with: + python-version: '3.12' - name: Build shell: cmd run: | diff --git a/CMakeLists.txt b/CMakeLists.txt index 53e3aace..22c21f8f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,7 +12,7 @@ # Benoit Scherrer, 2010 CRL, Harvard Medical School # Copyright (c) 2008-2009 Children's Hospital Boston #============================================================================== -cmake_minimum_required (VERSION 3.13) +cmake_minimum_required (VERSION 3.14) set (CMAKE_BUILD_TYPE Release CACHE STRING "Build type") @@ -324,13 +324,9 @@ endif () if (NLOPT_PYTHON) if (CMAKE_VERSION VERSION_LESS 3.26) - find_package (Python 3.6 COMPONENTS Interpreter Development) + find_package (Python 3.6 COMPONENTS Interpreter Development NumPy) else () - find_package (Python 3.6 COMPONENTS Interpreter Development.Module Development.SABIModule) - endif () - - if (Python_FOUND) - find_package (NumPy) + find_package (Python 3.6 COMPONENTS Interpreter Development.Module Development.SABIModule NumPy) endif () endif () diff --git a/cmake/FindNumPy.cmake b/cmake/FindNumPy.cmake deleted file mode 100644 index db9f51d7..00000000 --- a/cmake/FindNumPy.cmake +++ /dev/null @@ -1,86 +0,0 @@ -# - Find the NumPy libraries -# This module finds if NumPy is installed, and sets the following variables -# indicating where it is. -# -# TODO: Update to provide the libraries and paths for linking npymath lib. -# -# NUMPY_FOUND - was NumPy found -# NUMPY_VERSION - the version of NumPy found as a string -# NUMPY_VERSION_MAJOR - the major version number of NumPy -# NUMPY_VERSION_MINOR - the minor version number of NumPy -# NUMPY_VERSION_PATCH - the patch version number of NumPy -# NUMPY_VERSION_DECIMAL - e.g. version 1.6.1 is 10601 -# NUMPY_INCLUDE_DIRS - path to the NumPy include files - -#============================================================================ -# Copyright 2012 Continuum Analytics, Inc. -# -# MIT License -# -# Permission is hereby granted, free of charge, to any person obtaining -# a copy of this software and associated documentation files -# (the "Software"), to deal in the Software without restriction, including -# without limitation the rights to use, copy, modify, merge, publish, -# distribute, sublicense, and/or sell copies of the Software, and to permit -# persons to whom the Software is furnished to do so, subject to -# the following conditions: -# -# The above copyright notice and this permission notice shall be included -# in all copies or substantial portions of the Software. -# -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS -# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL -# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR -# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, -# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR -# OTHER DEALINGS IN THE SOFTWARE. -# -#============================================================================ - -# Finding NumPy involves calling the Python interpreter -if (NOT Python_EXECUTABLE) - message (SEND_ERROR "Python interpreter not found") -endif () - -execute_process(COMMAND "${Python_EXECUTABLE}" "-c" - "import numpy as np; print(np.__version__); print(np.get_include());" - RESULT_VARIABLE _NUMPY_SEARCH_SUCCESS - OUTPUT_VARIABLE _NUMPY_VALUES_OUTPUT - ERROR_VARIABLE _NUMPY_ERROR_VALUE - OUTPUT_STRIP_TRAILING_WHITESPACE) - -if(_NUMPY_SEARCH_SUCCESS MATCHES 0) - # Convert the process output into a list - string(REGEX REPLACE ";" "\\\\;" _NUMPY_VALUES ${_NUMPY_VALUES_OUTPUT}) - string(REGEX REPLACE "\n" ";" _NUMPY_VALUES ${_NUMPY_VALUES}) - list(GET _NUMPY_VALUES 0 NUMPY_VERSION) - list(GET _NUMPY_VALUES 1 NUMPY_INCLUDE_DIRS) - - string(REGEX MATCH "^[0-9]+\\.[0-9]+\\.[0-9]+" _VER_CHECK "${NUMPY_VERSION}") - if("${_VER_CHECK}" STREQUAL "") - # The output from Python was unexpected. Raise an error always - # here, because we found NumPy, but it appears to be corrupted somehow. - message(FATAL_ERROR - "Requested version and include path from NumPy, got instead:\n${_NUMPY_VALUES_OUTPUT}\n") - return() - endif() - - # Make sure all directory separators are '/' - string(REGEX REPLACE "\\\\" "/" NUMPY_INCLUDE_DIRS ${NUMPY_INCLUDE_DIRS}) - - # Get the major and minor version numbers - string(REGEX REPLACE "\\." ";" _NUMPY_VERSION_LIST ${NUMPY_VERSION}) - list(GET _NUMPY_VERSION_LIST 0 NUMPY_VERSION_MAJOR) - list(GET _NUMPY_VERSION_LIST 1 NUMPY_VERSION_MINOR) - list(GET _NUMPY_VERSION_LIST 2 NUMPY_VERSION_PATCH) - string(REGEX MATCH "[0-9]*" NUMPY_VERSION_PATCH ${NUMPY_VERSION_PATCH}) - math(EXPR NUMPY_VERSION_DECIMAL - "(${NUMPY_VERSION_MAJOR} * 10000) + (${NUMPY_VERSION_MINOR} * 100) + ${NUMPY_VERSION_PATCH}") -endif() - - -# handle REQUIRED and QUIET options -include (FindPackageHandleStandardArgs) -find_package_handle_standard_args (NumPy REQUIRED_VARS NUMPY_INCLUDE_DIRS VERSION_VAR NUMPY_VERSION) - diff --git a/src/swig/CMakeLists.txt b/src/swig/CMakeLists.txt index 5e26b613..cccfc6aa 100644 --- a/src/swig/CMakeLists.txt +++ b/src/swig/CMakeLists.txt @@ -19,7 +19,7 @@ endif () include_directories (${NLOPT_PRIVATE_INCLUDE_DIRS}) set_source_files_properties (nlopt.i PROPERTIES CPLUSPLUS ON) -if (NUMPY_FOUND AND Python_FOUND) +if (Python_NumPy_FOUND) set (SWIG_MODULE_nlopt_python_EXTRA_DEPS nlopt-python.i numpy.i generate-cpp) @@ -27,9 +27,10 @@ if (NUMPY_FOUND AND Python_FOUND) swig_add_library (nlopt_python LANGUAGE python SOURCES nlopt.i) target_include_directories (nlopt_python PRIVATE ${Python_INCLUDE_DIRS}) - target_include_directories (nlopt_python PRIVATE ${NUMPY_INCLUDE_DIRS}) + target_include_directories (nlopt_python PRIVATE ${Python_NumPy_INCLUDE_DIRS}) - swig_link_libraries (nlopt_python ${nlopt_lib}) + target_link_libraries (nlopt_python ${nlopt_lib}) + target_link_libraries (nlopt_python Python::NumPy) if (TARGET Python::Module) target_link_libraries (nlopt_python Python::Module) else () diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index cd367e9f..649b24f5 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -62,7 +62,7 @@ foreach (algo_index RANGE 28) # 42 endforeach () endforeach () -if (Python_FOUND AND NUMPY_FOUND AND (SWIG_FOUND OR (EXISTS ${PROJECT_SOURCE_DIR}/src/swig/nlopt-python.cpp))) +if (Python_NumPy_FOUND AND (SWIG_FOUND OR (EXISTS ${PROJECT_SOURCE_DIR}/src/swig/nlopt-python.cpp))) set (PYINSTALLCHECK_ENVIRONMENT "LD_LIBRARY_PATH=${PROJECT_BINARY_DIR}/src/swig" "PYTHONPATH=${PROJECT_BINARY_DIR}/src/swig" )