Sfoglia il codice sorgente

FindPython: Add possibility to control scope of artifacts.

Fixes: #20362
Marc Chevrier 6 anni fa
parent
commit
e5b4c74238

+ 6 - 0
Help/release/dev/FindPython-artifacts-interactive.rst

@@ -0,0 +1,6 @@
+FindPython-artifacts-interactive
+--------------------------------
+
+* The :module:`FindPython3`, :module:`FindPython2` and :module:`FindPython`
+  modules gained the possibility to create artifacts cache variables for
+  interactive edition.

+ 16 - 0
Modules/FindPython.cmake

@@ -288,6 +288,22 @@ setting the following variables:
   If more than one artifact is specified, it is the user's responsability to
   If more than one artifact is specified, it is the user's responsability to
   ensure the consistency of the various artifacts.
   ensure the consistency of the various artifacts.
 
 
+By default, this module supports multiple calls in different directories of a
+project with different version/component requirements while providing correct
+and consistent results for each call. To support this behavior, ``CMake`` cache
+is not used in the traditional way which can be problematic for interactive
+specification. So, to enable also interactive specification, module behavior
+can be controled with the following variable:
+
+``Python_ARTIFACTS_INTERACTIVE``
+  Selects the behavior of the module. This is a boolean variable:
+
+  * If set to ``TRUE``: Create CMake cache entries for the above artifact
+    specification variables so that users can edit them interactively.
+    This disables support for multiple version/component requirements.
+  * If set to ``FALSE`` or undefined: Enable multiple version/component
+    requirements.
+
 Commands
 Commands
 ^^^^^^^^
 ^^^^^^^^
 
 

+ 28 - 13
Modules/FindPython/Support.cmake

@@ -75,22 +75,20 @@ macro (_PYTHON_SELECT_LIBRARY_CONFIGURATIONS _PYTHON_BASENAME)
       (_PYTHON_isMultiConfig OR CMAKE_BUILD_TYPE))
       (_PYTHON_isMultiConfig OR CMAKE_BUILD_TYPE))
     # if the generator is multi-config or if CMAKE_BUILD_TYPE is set for
     # if the generator is multi-config or if CMAKE_BUILD_TYPE is set for
     # single-config generators, set optimized and debug libraries
     # single-config generators, set optimized and debug libraries
-    set (${_PYTHON_BASENAME}_LIBRARY "")
-    foreach (_PYTHON_libname IN LISTS ${_PYTHON_BASENAME}_LIBRARY_RELEASE )
-      list( APPEND ${_PYTHON_BASENAME}_LIBRARY optimized "${_PYTHON_libname}" )
+    set (${_PYTHON_BASENAME}_LIBRARIES "")
+    foreach (_PYTHON_libname IN LISTS ${_PYTHON_BASENAME}_LIBRARY_RELEASE)
+      list( APPEND ${_PYTHON_BASENAME}_LIBRARIES optimized "${_PYTHON_libname}")
     endforeach()
     endforeach()
-    foreach (_PYTHON_libname IN LISTS ${_PYTHON_BASENAME}_LIBRARY_DEBUG )
-      list( APPEND ${_PYTHON_BASENAME}_LIBRARY debug "${_PYTHON_libname}" )
+    foreach (_PYTHON_libname IN LISTS ${_PYTHON_BASENAME}_LIBRARY_DEBUG)
+      list( APPEND ${_PYTHON_BASENAME}_LIBRARIES debug "${_PYTHON_libname}")
     endforeach()
     endforeach()
   elseif (${_PYTHON_BASENAME}_LIBRARY_RELEASE)
   elseif (${_PYTHON_BASENAME}_LIBRARY_RELEASE)
-    set (${_PYTHON_BASENAME}_LIBRARY "${${_PYTHON_BASENAME}_LIBRARY_RELEASE}")
+    set (${_PYTHON_BASENAME}_LIBRARIES "${${_PYTHON_BASENAME}_LIBRARY_RELEASE}")
   elseif (${_PYTHON_BASENAME}_LIBRARY_DEBUG)
   elseif (${_PYTHON_BASENAME}_LIBRARY_DEBUG)
-    set (${_PYTHON_BASENAME}_LIBRARY "${${_PYTHON_BASENAME}_LIBRARY_DEBUG}")
+    set (${_PYTHON_BASENAME}_LIBRARIES "${${_PYTHON_BASENAME}_LIBRARY_DEBUG}")
   else()
   else()
-    set (${_PYTHON_BASENAME}_LIBRARY "${_PYTHON_BASENAME}_LIBRARY-NOTFOUND")
+    set (${_PYTHON_BASENAME}_LIBRARIES "${_PYTHON_BASENAME}_LIBRARY-NOTFOUND")
   endif()
   endif()
-
-  set (${_PYTHON_BASENAME}_LIBRARIES "${${_PYTHON_BASENAME}_LIBRARY}")
 endmacro()
 endmacro()
 
 
 
 
@@ -1412,6 +1410,10 @@ if ("Interpreter" IN_LIST ${_PYTHON_PREFIX}_FIND_COMPONENTS)
     endif()
     endif()
   endif()
   endif()
 
 
+  if (${_PYTHON_PREFIX}_ARTIFACTS_INTERACTIVE)
+    set (${_PYTHON_PREFIX}_EXECUTABLE "${_${_PYTHON_PREFIX}_EXECUTABLE}" CACHE FILEPATH "${_PYTHON_PREFIX} Interpreter")
+  endif()
+
   _python_mark_as_internal (_${_PYTHON_PREFIX}_EXECUTABLE
   _python_mark_as_internal (_${_PYTHON_PREFIX}_EXECUTABLE
                             _${_PYTHON_PREFIX}_INTERPRETER_PROPERTIES
                             _${_PYTHON_PREFIX}_INTERPRETER_PROPERTIES
                             _${_PYTHON_PREFIX}_INTERPRETER_SIGNATURE)
                             _${_PYTHON_PREFIX}_INTERPRETER_SIGNATURE)
@@ -1607,6 +1609,10 @@ if ("Compiler" IN_LIST ${_PYTHON_PREFIX}_FIND_COMPONENTS)
     unset (${_PYTHON_PREFIX}_COMPILER_ID)
     unset (${_PYTHON_PREFIX}_COMPILER_ID)
   endif()
   endif()
 
 
+  if (${_PYTHON_PREFIX}_ARTIFACTS_INTERACTIVE)
+    set (${_PYTHON_PREFIX}_COMPILER "${_${_PYTHON_PREFIX}_COMPILER}" CACHE FILEPATH "${_PYTHON_PREFIX} Compiler")
+  endif()
+
   _python_mark_as_internal (_${_PYTHON_PREFIX}_COMPILER
   _python_mark_as_internal (_${_PYTHON_PREFIX}_COMPILER
                             _${_PYTHON_PREFIX}_COMPILER_SIGNATURE)
                             _${_PYTHON_PREFIX}_COMPILER_SIGNATURE)
 endif()
 endif()
@@ -2298,6 +2304,11 @@ if ("Development" IN_LIST ${_PYTHON_PREFIX}_FIND_COMPONENTS
     set (CMAKE_FIND_LIBRARY_SUFFIXES ${_${_PYTHON_PREFIX}_CMAKE_FIND_LIBRARY_SUFFIXES})
     set (CMAKE_FIND_LIBRARY_SUFFIXES ${_${_PYTHON_PREFIX}_CMAKE_FIND_LIBRARY_SUFFIXES})
   endif()
   endif()
 
 
+  if (${_PYTHON_PREFIX}_ARTIFACTS_INTERACTIVE)
+    set (${_PYTHON_PREFIX}_LIBRARY "${_${_PYTHON_PREFIX}_LIBRARY_RELEASE}" CACHE FILEPATH "${_PYTHON_PREFIX} Library")
+    set (${_PYTHON_PREFIX}_INCLUDE_DIR "${_${_PYTHON_PREFIX}_INCLUDE_DIR}" CACHE FILEPATH "${_PYTHON_PREFIX} Include Directory")
+  endif()
+
   _python_mark_as_internal (_${_PYTHON_PREFIX}_LIBRARY_RELEASE
   _python_mark_as_internal (_${_PYTHON_PREFIX}_LIBRARY_RELEASE
                             _${_PYTHON_PREFIX}_LIBRARY_DEBUG
                             _${_PYTHON_PREFIX}_LIBRARY_DEBUG
                             _${_PYTHON_PREFIX}_RUNTIME_LIBRARY_RELEASE
                             _${_PYTHON_PREFIX}_RUNTIME_LIBRARY_RELEASE
@@ -2376,6 +2387,10 @@ if ("NumPy" IN_LIST ${_PYTHON_PREFIX}_FIND_COMPONENTS AND ${_PYTHON_PREFIX}_Inte
     unset (_${_PYTHON_PREFIX}_NUMPY_SIGNATURE CACHE)
     unset (_${_PYTHON_PREFIX}_NUMPY_SIGNATURE CACHE)
   endif()
   endif()
 
 
+  if (${_PYTHON_PREFIX}_ARTIFACTS_INTERACTIVE)
+    set (${_PYTHON_PREFIX}_NumPy_INCLUDE_DIR "${_${_PYTHON_PREFIX}_NumPy_INCLUDE_DIR}" CACHE FILEPATH "${_PYTHON_PREFIX} NumPy Include Directory")
+  endif()
+
   _python_mark_as_internal (_${_PYTHON_PREFIX}_NumPy_INCLUDE_DIR
   _python_mark_as_internal (_${_PYTHON_PREFIX}_NumPy_INCLUDE_DIR
                             _${_PYTHON_PREFIX}_NUMPY_SIGNATURE)
                             _${_PYTHON_PREFIX}_NUMPY_SIGNATURE)
 endif()
 endif()
@@ -2451,8 +2466,8 @@ if(_${_PYTHON_PREFIX}_CMAKE_ROLE STREQUAL "PROJECT")
         else()
         else()
           set_target_properties (${__name}
           set_target_properties (${__name}
                                  PROPERTIES IMPORTED_LINK_INTERFACE_LANGUAGES "C"
                                  PROPERTIES IMPORTED_LINK_INTERFACE_LANGUAGES "C"
-                                            IMPORTED_IMPLIB "${${_PYTHON_PREFIX}_LIBRARY}"
-                                            IMPORTED_LOCATION "${${_PYTHON_PREFIX}_RUNTIME_LIBRARY}")
+                                            IMPORTED_IMPLIB "${${_PYTHON_PREFIX}_LIBRARIES}"
+                                            IMPORTED_LOCATION "${${_PYTHON_PREFIX}_RUNTIME_LIBRARY_RELEASE}")
         endif()
         endif()
       else()
       else()
         if (${_PYTHON_PREFIX}_LIBRARY_RELEASE AND ${_PYTHON_PREFIX}_LIBRARY_DEBUG)
         if (${_PYTHON_PREFIX}_LIBRARY_RELEASE AND ${_PYTHON_PREFIX}_LIBRARY_DEBUG)
@@ -2466,7 +2481,7 @@ if(_${_PYTHON_PREFIX}_CMAKE_ROLE STREQUAL "PROJECT")
         else()
         else()
           set_target_properties (${__name}
           set_target_properties (${__name}
                                  PROPERTIES IMPORTED_LINK_INTERFACE_LANGUAGES "C"
                                  PROPERTIES IMPORTED_LINK_INTERFACE_LANGUAGES "C"
-                                            IMPORTED_LOCATION "${${_PYTHON_PREFIX}_LIBRARY}")
+                                            IMPORTED_LOCATION "${${_PYTHON_PREFIX}_LIBRARY_RELEASE}")
         endif()
         endif()
       endif()
       endif()
 
 

+ 16 - 0
Modules/FindPython2.cmake

@@ -237,6 +237,22 @@ setting the following variables:
   If more than one artifact is specified, it is the user's responsability to
   If more than one artifact is specified, it is the user's responsability to
   ensure the consistency of the various artifacts.
   ensure the consistency of the various artifacts.
 
 
+By default, this module supports multiple calls in different directories of a
+project with different version/component requirements while providing correct
+and consistent results for each call. To support this behavior, ``CMake`` cache
+is not used in the traditional way which can be problematic for interactive
+specification. So, to enable also interactive specification, module behavior
+can be controled with the following variable:
+
+``Python2_ARTIFACTS_INTERACTIVE``
+  Selects the behavior of the module. This is a boolean variable:
+
+  * If set to ``TRUE``: Create CMake cache entries for the above artifact
+    specification variables so that users can edit them interactively.
+    This disables support for multiple version/component requirements.
+  * If set to ``FALSE`` or undefined: Enable multiple version/component
+    requirements.
+
 Commands
 Commands
 ^^^^^^^^
 ^^^^^^^^
 
 

+ 16 - 0
Modules/FindPython3.cmake

@@ -285,6 +285,22 @@ setting the following variables:
   If more than one artifact is specified, it is the user's responsability to
   If more than one artifact is specified, it is the user's responsability to
   ensure the consistency of the various artifacts.
   ensure the consistency of the various artifacts.
 
 
+By default, this module supports multiple calls in different directories of a
+project with different version/component requirements while providing correct
+and consistent results for each call. To support this behavior, ``CMake`` cache
+is not used in the traditional way which can be problematic for interactive
+specification. So, to enable also interactive specification, module behavior
+can be controled with the following variable:
+
+``Python3_ARTIFACTS_INTERACTIVE``
+  Selects the behavior of the module. This is a boolean variable:
+
+  * If set to ``TRUE``: Create CMake cache entries for the above artifact
+    specification variables so that users can edit them interactively.
+    This disables support for multiple version/component requirements.
+  * If set to ``FALSE`` or undefined: Enable multiple version/component
+    requirements.
+
 Commands
 Commands
 ^^^^^^^^
 ^^^^^^^^
 
 

+ 24 - 0
Tests/FindPython/ArtifactsInteractive/CMakeLists.txt

@@ -0,0 +1,24 @@
+cmake_minimum_required(VERSION 3.1)
+
+project(TestArtifactsInteractive LANGUAGES C)
+
+set (components Interpreter Development)
+if (CMake_TEST_FindPython_NumPy)
+  list (APPEND components NumPy)
+endif()
+
+find_package(Python3 REQUIRED COMPONENTS ${components})
+
+if (Python3_ARTIFACTS_INTERACTIVE)
+  if (NOT DEFINED CACHE{Python3_EXECUTABLE}
+      OR NOT DEFINED CACHE{Python3_LIBRARY} OR NOT DEFINED CACHE{Python3_INCLUDE_DIR}
+      OR (CMake_TEST_FindPython_NumPy AND NOT DEFINED CACHE{Python3_NumPy_INCLUDE_DIR}))
+    message (FATAL_ERROR "Python3_ARTIFACTS_INTERACTIVE=ON Failed.")
+  endif()
+else()
+  if (DEFINED CACHE{Python3_EXECUTABLE}
+      OR DEFINED CACHE{Python3_LIBRARY} OR DEFINED CACHE{Python3_INCLUDE_DIR}
+      OR (CMake_TEST_FindPython_NumPy AND DEFINED CACHE{Python3_NumPy_INCLUDE_DIR}))
+    message (FATAL_ERROR "Python3_ARTIFACTS_INTERACTIVE=OFF Failed.")
+  endif()
+endif()

+ 29 - 0
Tests/FindPython/CMakeLists.txt

@@ -134,6 +134,35 @@ if(CMake_TEST_FindPython)
     --test-command ${CMAKE_CTEST_COMMAND} -V -C $<CONFIGURATION>
     --test-command ${CMAKE_CTEST_COMMAND} -V -C $<CONFIGURATION>
     )
     )
 
 
+  add_test(NAME FindPython.ArtifactsInteractive.ON COMMAND
+    ${CMAKE_CTEST_COMMAND} -C $<CONFIGURATION>
+    --build-and-test
+    "${CMake_SOURCE_DIR}/Tests/FindPython/ArtifactsInteractive"
+    "${CMake_BINARY_DIR}/Tests/FindPython/ArtifactsInteractive.ON"
+    ${build_generator_args}
+    --build-project TestArtifactsScope
+    --build-options ${build_options} "-Dbuild_generator_args=${build_generator_args}"
+    "-DCMake_SOURCE_DIR=${CMake_SOURCE_DIR}"
+    "-DCMake_BINARY_DIR=${CMake_BINARY_DIR}"
+    "-DCMake_TEST_FindPython_NumPy=${CMake_TEST_FindPython_NumPy}"
+    "-DPython3_ARTIFACTS_INTERACTIVE=ON"
+    --test-command ${CMAKE_CTEST_COMMAND} -V -C $<CONFIGURATION>
+    )
+  add_test(NAME FindPython.ArtifactsInteractive.OFF COMMAND
+    ${CMAKE_CTEST_COMMAND} -C $<CONFIGURATION>
+    --build-and-test
+    "${CMake_SOURCE_DIR}/Tests/FindPython/ArtifactsInteractive"
+    "${CMake_BINARY_DIR}/Tests/FindPython/ArtifactsInteractive.OFF"
+    ${build_generator_args}
+    --build-project TestArtifactsScope
+    --build-options ${build_options} "-Dbuild_generator_args=${build_generator_args}"
+    "-DCMake_SOURCE_DIR=${CMake_SOURCE_DIR}"
+    "-DCMake_BINARY_DIR=${CMake_BINARY_DIR}"
+    "-DCMake_TEST_FindPython_NumPy=${CMake_TEST_FindPython_NumPy}"
+    "-DPython3_ARTIFACTS_INTERACTIVE=OFF"
+    --test-command ${CMAKE_CTEST_COMMAND} -V -C $<CONFIGURATION>
+    )
+
   add_test(NAME FindPython.CustomFailureMessage COMMAND
   add_test(NAME FindPython.CustomFailureMessage COMMAND
     ${CMAKE_CTEST_COMMAND} -C $<CONFIGURATION>
     ${CMAKE_CTEST_COMMAND} -C $<CONFIGURATION>
     --build-and-test
     --build-and-test

+ 3 - 3
Tests/FindPython/Python3/CMakeLists.txt

@@ -15,14 +15,14 @@ if (NOT Python3_FOUND)
 endif()
 endif()
 
 
 if(NOT TARGET Python3::Interpreter)
 if(NOT TARGET Python3::Interpreter)
-  message(SEND_ERROR "Python2::Interpreter not found")
+  message(SEND_ERROR "Python3::Interpreter not found")
 endif()
 endif()
 
 
 if(NOT TARGET Python3::Python)
 if(NOT TARGET Python3::Python)
-  message(SEND_ERROR "Python2::Python not found")
+  message(SEND_ERROR "Python3::Python not found")
 endif()
 endif()
 if(NOT TARGET Python3::Module)
 if(NOT TARGET Python3::Module)
-  message(SEND_ERROR "Python2::Module not found")
+  message(SEND_ERROR "Python3::Module not found")
 endif()
 endif()
 
 
 Python3_add_library (spam3 MODULE ../spam.c)
 Python3_add_library (spam3 MODULE ../spam.c)