Browse Source

Merge topic 'python-android-link'

1507491c25 FindPython: link modules against libpython on Android

Acked-by: Kitware Robot <[email protected]>
Tested-by: buildbot <[email protected]>
Merge-request: !11153
Brad King 1 month ago
parent
commit
c4fe74ba3d

+ 3 - 3
Modules/FindPython/Support.cmake

@@ -1519,13 +1519,13 @@ unset (_${_PYTHON_PREFIX}_FIND_DEVELOPMENT_MODULE_ARTIFACTS)
 unset (_${_PYTHON_PREFIX}_FIND_DEVELOPMENT_SABIMODULE_ARTIFACTS)
 unset (_${_PYTHON_PREFIX}_FIND_DEVELOPMENT_EMBED_ARTIFACTS)
 if ("Development.Module" IN_LIST ${_PYTHON_BASE}_FIND_COMPONENTS)
-  if (CMAKE_SYSTEM_NAME MATCHES "^(Windows.*|CYGWIN|MSYS)$")
+  if (CMAKE_SYSTEM_NAME MATCHES "^(Windows.*|CYGWIN|MSYS|Android)$")
     list (APPEND _${_PYTHON_PREFIX}_FIND_DEVELOPMENT_MODULE_ARTIFACTS "LIBRARY")
   endif()
   list (APPEND _${_PYTHON_PREFIX}_FIND_DEVELOPMENT_MODULE_ARTIFACTS "INCLUDE_DIR")
 endif()
 if ("Development.SABIModule" IN_LIST ${_PYTHON_BASE}_FIND_COMPONENTS)
-  if (CMAKE_SYSTEM_NAME MATCHES "^(Windows.*|CYGWIN|MSYS)$")
+  if (CMAKE_SYSTEM_NAME MATCHES "^(Windows.*|CYGWIN|MSYS|Android)$")
     list (APPEND _${_PYTHON_PREFIX}_FIND_DEVELOPMENT_SABIMODULE_ARTIFACTS "SABI_LIBRARY")
   endif()
   list (APPEND _${_PYTHON_PREFIX}_FIND_DEVELOPMENT_SABIMODULE_ARTIFACTS "INCLUDE_DIR")
@@ -4351,7 +4351,7 @@ if(_${_PYTHON_PREFIX}_CMAKE_ROLE STREQUAL "PROJECT")
 
     if (${_PYTHON_PREFIX}_Development.Module_FOUND)
       if ("LIBRARY" IN_LIST _${_PYTHON_PREFIX}_FIND_DEVELOPMENT_MODULE_ARTIFACTS)
-        # On Windows/CYGWIN/MSYS, Python::Module is the same as Python::Python
+        # On Windows/CYGWIN/MSYS/Android, Python::Module is the same as Python::Python
         # but ALIAS cannot be used because the imported library is not GLOBAL.
         __python_import_library (${_PYTHON_PREFIX}::Module)
       else()

+ 51 - 0
Tests/RunCMake/FindPython/Android.cmake

@@ -0,0 +1,51 @@
+enable_language(C)
+
+set(components Development.Embed Development.Module Development.SABIModule)
+find_package(Python REQUIRED COMPONENTS ${components})
+
+foreach(component ${components})
+  set(found_var Python_${component}_FOUND)
+  if(NOT ${found_var})
+    message(FATAL_ERROR "${found_var} is not set")
+  endif()
+endforeach()
+
+set(android_root "${CMAKE_SOURCE_DIR}/android_root")
+if(NOT Python_INCLUDE_DIRS STREQUAL "${android_root}/include/python3.13")
+  message(FATAL_ERROR "Python_INCLUDE_DIRS=${Python_INCLUDE_DIRS}")
+endif()
+if(NOT Python_LIBRARIES STREQUAL "${android_root}/lib/libpython3.13.so")
+  message(FATAL_ERROR "Python_LIBRARIES=${Python_LIBRARIES}")
+endif()
+if(NOT Python_SABI_LIBRARIES STREQUAL "${android_root}/lib/libpython3.so")
+  message(FATAL_ERROR "Python_SABI_LIBRARIES=${Python_SABI_LIBRARIES}")
+endif()
+
+foreach(target Python::Python Python::Module Python::SABIModule)
+  if(NOT TARGET ${target})
+    message(FATAL_ERROR "Target ${target} does not exist")
+  endif()
+
+  # The Module and SABIModule targets will be SHARED_LIBRARY if Python modules should
+  # link against libpython (as on Android), and INTERFACE_LIBRARY if they should not (as
+  # on Linux).
+  get_target_property(target_type ${target} TYPE)
+  if(NOT target_type STREQUAL SHARED_LIBRARY)
+    message(FATAL_ERROR "${target} TYPE=${target_type}")
+  endif()
+
+  get_target_property(target_location ${target} LOCATION)
+  if(target STREQUAL "Python::SABIModule")
+    set(expected "${android_root}/lib/libpython3.so")
+  else()
+    set(expected "${android_root}/lib/libpython3.13.so")
+  endif()
+  if(NOT target_location STREQUAL expected)
+    message(FATAL_ERROR "${target} LOCATION=${target_location}")
+  endif()
+
+  get_target_property(target_include ${target} INTERFACE_INCLUDE_DIRECTORIES)
+  if(NOT target_include STREQUAL "${android_root}/include/python3.13")
+    message(FATAL_ERROR "${target} INTERFACE_INCLUDE_DIRECTORIES=${target_include}")
+  endif()
+endforeach()

+ 1 - 0
Tests/RunCMake/FindPython/RunCMakeTest.cmake

@@ -322,6 +322,7 @@ if(CMake_TEST_FindPython_Various)
     run_python(ArtifactsInteractive VARIANT "OFF"
                                     OPTIONS -DCMake_TEST_FindPython3_NumPy=${CMake_TEST_FindPython3_NumPy}
                                             -DPython3_ARTIFACTS_INTERACTIVE=OFF)
+    run_python(Android OPTIONS "-DCMAKE_TOOLCHAIN_FILE=${RunCMake_SOURCE_DIR}/android_toolchain.cmake")
   endif()
 
   if(CMake_TEST_FindPython2 OR CMake_TEST_FindPython3)

+ 1 - 0
Tests/RunCMake/FindPython/android_root/README.txt

@@ -0,0 +1 @@
+This directory contains a dummy Python installation tree for the Android test.

+ 0 - 0
Tests/RunCMake/FindPython/android_root/include/python3.13/Python.h


+ 1 - 0
Tests/RunCMake/FindPython/android_root/include/python3.13/patchlevel.h

@@ -0,0 +1 @@
+#define PY_VERSION "3.13.0"

+ 0 - 0
Tests/RunCMake/FindPython/android_root/include/python3.13/pyconfig.h


+ 0 - 0
Tests/RunCMake/FindPython/android_root/lib/libpython3.13.so


+ 0 - 0
Tests/RunCMake/FindPython/android_root/lib/libpython3.so


+ 8 - 0
Tests/RunCMake/FindPython/android_toolchain.cmake

@@ -0,0 +1,8 @@
+set(CMAKE_SYSTEM_NAME Android)
+
+# This test doesn't require the NDK, so inhibit CMake's NDK handling code.
+set(CMAKE_SYSTEM_VERSION 1)
+
+set(CMAKE_FIND_ROOT_PATH "${CMAKE_SOURCE_DIR}/android_root")
+set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
+set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)