[Bf-blender-cvs] [2c23b4e0bff] master: Python: on macOS, stop requiring framework for building bpy module

Brecht Van Lommel noreply at git.blender.org
Sat Sep 10 13:50:36 CEST 2022


Commit: 2c23b4e0bff0ddd7219f4d23bcbe95d2da921fed
Author: Brecht Van Lommel
Date:   Fri Sep 9 03:12:56 2022 +0200
Branches: master
https://developer.blender.org/rB2c23b4e0bff0ddd7219f4d23bcbe95d2da921fed

Python: on macOS, stop requiring framework for building bpy module

Build against Python from precompiled libraries by default, instead of
requiring framework from python.org package install. The resulting bpy module
can still be used with any Python install of the same version.

Use the same CMake find module as Linux. This simplifies code, and makes it
possible to manually set PYTHON_* variables in CMake configuration.

Remove WITH_PYTHON_FRAMEWORK option for regular Blender build, as this doesn't
work well due to missing required Python packages. Advanced users can still
set PYTHON_ROOT_DIR=/Library/Frameworks/Python.framework/Versions/3.10 for
the same result.

===================================================================

M	CMakeLists.txt
M	build_files/cmake/Modules/FindPythonLibsUnix.cmake
M	build_files/cmake/macros.cmake
M	build_files/cmake/platform/platform_apple.cmake
M	source/creator/CMakeLists.txt

===================================================================

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 53859196cfb..5a08e7d167c 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -157,9 +157,6 @@ mark_as_advanced(WITH_PYTHON_SECURITY)  # some distributions see this as a secur
 option(WITH_PYTHON_SAFETY "Enable internal API error checking to track invalid data to prevent crash on access (at the expense of some efficiency, only enable for development)." OFF)
 mark_as_advanced(WITH_PYTHON_SAFETY)
 option(WITH_PYTHON_MODULE "Enable building as a python module which runs without a user interface, like running regular blender in background mode (experimental, only enable for development), installs to PYTHON_SITE_PACKAGES (or CMAKE_INSTALL_PREFIX if WITH_INSTALL_PORTABLE is enabled)." OFF)
-if(APPLE)
-  option(WITH_PYTHON_FRAMEWORK "Enable building using the Python available in the framework (OSX only)" OFF)
-endif()
 
 option(WITH_BUILDINFO     "Include extra build details (only disable for development & faster builds)" ON)
 set(BUILDINFO_OVERRIDE_DATE "" CACHE STRING "Use instead of the current date for reproducible builds (empty string disables this option)")
@@ -1628,8 +1625,8 @@ if(WITH_PYTHON)
     )
   endif()
 
-  if(WIN32 OR APPLE)
-    # Windows and macOS have this bundled with Python libraries.
+  if(WIN32)
+    # Always use numpy bundled in precompiled libs.
   elseif((WITH_PYTHON_INSTALL AND WITH_PYTHON_INSTALL_NUMPY) OR WITH_PYTHON_NUMPY)
     if(("${PYTHON_NUMPY_PATH}" STREQUAL "") OR (${PYTHON_NUMPY_PATH} MATCHES NOTFOUND))
       find_python_package(numpy "core/include")
@@ -1637,13 +1634,13 @@ if(WITH_PYTHON)
   endif()
 
   if(WIN32 OR APPLE)
-    # pass, we have this in lib/python/site-packages
+    # Always copy from precompiled libs.
   elseif(WITH_PYTHON_INSTALL_REQUESTS)
     find_python_package(requests "")
   endif()
 
   if(WIN32 OR APPLE)
-    # pass, we have this in lib/python/site-packages
+    # Always copy from precompiled libs.
   elseif(WITH_PYTHON_INSTALL_ZSTANDARD)
     find_python_package(zstandard "")
   endif()
@@ -1908,9 +1905,6 @@ if(FIRST_RUN)
   info_cfg_option(WITH_LZO)
 
   info_cfg_text("Python:")
-  if(APPLE)
-    info_cfg_option(WITH_PYTHON_FRAMEWORK)
-  endif()
   info_cfg_option(WITH_PYTHON_INSTALL)
   info_cfg_option(WITH_PYTHON_INSTALL_NUMPY)
   info_cfg_option(WITH_PYTHON_INSTALL_ZSTANDARD)
diff --git a/build_files/cmake/Modules/FindPythonLibsUnix.cmake b/build_files/cmake/Modules/FindPythonLibsUnix.cmake
index 1e88621303f..0afe1299330 100644
--- a/build_files/cmake/Modules/FindPythonLibsUnix.cmake
+++ b/build_files/cmake/Modules/FindPythonLibsUnix.cmake
@@ -34,11 +34,17 @@ SET(PYTHON_VERSION 3.10 CACHE STRING "Python Version (major and minor only)")
 MARK_AS_ADVANCED(PYTHON_VERSION)
 
 
-# See: http://docs.python.org/extending/embedding.html#linking-requirements
-#      for why this is needed
-SET(PYTHON_LINKFLAGS "-Xlinker -export-dynamic" CACHE STRING "Linker flags for python")
-MARK_AS_ADVANCED(PYTHON_LINKFLAGS)
-
+if(APPLE)
+  if(WITH_PYTHON_MODULE)
+    set(PYTHON_LINKFLAGS "-undefined dynamic_lookup")
+  else()
+    set(PYTHON_LINKFLAGS)
+  endif()
+else()
+  # See: http://docs.python.org/extending/embedding.html#linking-requirements
+  SET(PYTHON_LINKFLAGS "-Xlinker -export-dynamic" CACHE STRING "Linker flags for python")
+  MARK_AS_ADVANCED(PYTHON_LINKFLAGS)
+endif()
 
 # if the user passes these defines as args, we don't want to overwrite
 SET(_IS_INC_DEF OFF)
diff --git a/build_files/cmake/macros.cmake b/build_files/cmake/macros.cmake
index 5508e8f2104..d271d8f216f 100644
--- a/build_files/cmake/macros.cmake
+++ b/build_files/cmake/macros.cmake
@@ -1208,16 +1208,8 @@ endmacro()
 
 macro(without_system_libs_begin)
   set(CMAKE_IGNORE_PATH "${CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES};${CMAKE_SYSTEM_INCLUDE_PATH};${CMAKE_C_IMPLICIT_INCLUDE_DIRECTORIES};${CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES}")
-  if(APPLE)
-    # Avoid searching for headers in frameworks (like Mono), and libraries in LIBDIR.
-    set(CMAKE_FIND_FRAMEWORK NEVER)
-  endif()
 endmacro()
 
 macro(without_system_libs_end)
   unset(CMAKE_IGNORE_PATH)
-  if(APPLE)
-    # FIRST is the default.
-    set(CMAKE_FIND_FRAMEWORK FIRST)
-  endif()
 endmacro()
diff --git a/build_files/cmake/platform/platform_apple.cmake b/build_files/cmake/platform/platform_apple.cmake
index bc5baf43530..e428b2abab3 100644
--- a/build_files/cmake/platform/platform_apple.cmake
+++ b/build_files/cmake/platform/platform_apple.cmake
@@ -36,6 +36,9 @@ endmacro()
 # ------------------------------------------------------------------------
 # Find system provided libraries.
 
+# Avoid searching for headers in frameworks (like Mono), and libraries in LIBDIR.
+set(CMAKE_FIND_FRAMEWORK NEVER)
+
 # Find system ZLIB, not the pre-compiled one supplied with OpenCollada.
 set(ZLIB_ROOT /usr)
 find_package(ZLIB REQUIRED)
@@ -75,6 +78,11 @@ if(NOT EXISTS "${LIBDIR}/")
   message(FATAL_ERROR "Mac OSX requires pre-compiled libs at: '${LIBDIR}'")
 endif()
 
+# Optionally use system Python if PYTHON_ROOT_DIR is specified.
+if(WITH_PYTHON AND (WITH_PYTHON_MODULE AND PYTHON_ROOT_DIR))
+  find_package(PythonLibsUnix REQUIRED)
+endif()
+
 # Prefer lib directory paths
 file(GLOB LIB_SUBDIRS ${LIBDIR}/*)
 set(CMAKE_PREFIX_PATH ${LIB_SUBDIRS})
@@ -123,34 +131,8 @@ if(WITH_CODEC_SNDFILE)
   unset(_sndfile_VORBISENC_LIBRARY)
 endif()
 
-if(WITH_PYTHON)
-  # Use precompiled libraries by default.
-  set(PYTHON_VERSION 3.10)
-  if(NOT WITH_PYTHON_MODULE AND NOT WITH_PYTHON_FRAMEWORK)
-    # Normally cached but not since we include them with blender.
-    set(PYTHON_INCLUDE_DIR "${LIBDIR}/python/include/python${PYTHON_VERSION}")
-    set(PYTHON_EXECUTABLE "${LIBDIR}/python/bin/python${PYTHON_VERSION}")
-    set(PYTHON_LIBRARY ${LIBDIR}/python/lib/libpython${PYTHON_VERSION}.a)
-    set(PYTHON_LIBPATH "${LIBDIR}/python/lib/python${PYTHON_VERSION}")
-  else()
-    # Module must be compiled against Python framework.
-    set(_py_framework "/Library/Frameworks/Python.framework/Versions/${PYTHON_VERSION}")
-    set(PYTHON_INCLUDE_DIR "${_py_framework}/include/python${PYTHON_VERSION}")
-    set(PYTHON_EXECUTABLE "${_py_framework}/bin/python${PYTHON_VERSION}")
-    set(PYTHON_LIBPATH "${_py_framework}/lib/python${PYTHON_VERSION}")
-    unset(_py_framework)
-  endif()
-
-  # uncached vars
-  set(PYTHON_INCLUDE_DIRS "${PYTHON_INCLUDE_DIR}")
-  set(PYTHON_LIBRARIES  "${PYTHON_LIBRARY}")
-
-  # needed for Audaspace, numpy is installed into python site-packages
-  set(PYTHON_NUMPY_INCLUDE_DIRS "${PYTHON_LIBPATH}/site-packages/numpy/core/include")
-
-  if(NOT EXISTS "${PYTHON_EXECUTABLE}")
-    message(FATAL_ERROR "Python executable missing: ${PYTHON_EXECUTABLE}")
-  endif()
+if(WITH_PYTHON AND NOT (WITH_PYTHON_MODULE AND PYTHON_ROOT_DIR))
+  find_package(PythonLibsUnix REQUIRED)
 endif()
 
 if(WITH_FFTW3)
@@ -213,11 +195,6 @@ if(WITH_JACK)
   string(APPEND PLATFORM_LINKFLAGS " -F/Library/Frameworks -weak_framework jackmp")
 endif()
 
-if(WITH_PYTHON_MODULE OR WITH_PYTHON_FRAMEWORK)
-  # force cmake to link right framework
-  string(APPEND PLATFORM_LINKFLAGS " /Library/Frameworks/Python.framework/Versions/${PYTHON_VERSION}/Python")
-endif()
-
 if(WITH_OPENCOLLADA)
   find_package(OpenCOLLADA)
   find_library(PCRE_LIBRARIES NAMES pcre HINTS ${LIBDIR}/opencollada/lib)
diff --git a/source/creator/CMakeLists.txt b/source/creator/CMakeLists.txt
index f8cbb9bc07c..b228c8d9ac1 100644
--- a/source/creator/CMakeLists.txt
+++ b/source/creator/CMakeLists.txt
@@ -362,9 +362,9 @@ elseif(APPLE)
     else()
       # Paths defined in terms of site-packages since the site-packages
       # directory can be a symlink (brew for example).
-      set(TARGETDIR_BPY ${PYTHON_LIBPATH}/site-packages/bpy)
-      set(TARGETDIR_VER ${PYTHON_LIBPATH}/site-packages/bpy/${BLENDER_VERSION})
-      set(TARGETDIR_LIB ${PYTHON_LIBPATH}/site-packages/bpy/lib)
+      set(TARGETDIR_BPY ${PYTHON_SITE_PACKAGES}/bpy)
+      set(TARGETDIR_VER ${PYTHON_SITE_PACKAGES}/bpy/${BLENDER_VERSION})
+      set(TARGETDIR_LIB ${PYTHON_SITE_PACKAGES}/bpy/lib)
     endif()
   else()
     set(TARGETDIR_VER Blender.app/Contents/Resources/${BLENDER_VERSION})



More information about the Bf-blender-cvs mailing list