Commit 34d1c76b authored by Alvin Wong's avatar Alvin Wong

Improve Python libs detection on Windows

- Remove bogus version check functions
- Specify 3.6 EXACT Python version
- Check for Python in 3rdparty also
- Add Python compilation check to make sure the Python lib can be linked

Maniphest Tasks: T1625
parent 9e9789d4
...@@ -126,8 +126,55 @@ if (UNIX AND NOT APPLE) ...@@ -126,8 +126,55 @@ if (UNIX AND NOT APPLE)
set(PATCH_COMMAND patch) set(PATCH_COMMAND patch)
endif () endif ()
function(TestCompileLinkPythonLibs OUTPUT_VARNAME)
include(CheckCXXSourceCompiles)
set(CMAKE_REQUIRED_INCLUDES ${PYTHON_INCLUDE_PATH})
set(CMAKE_REQUIRED_LIBRARIES ${PYTHON_LIBRARIES})
if (MINGW)
set(CMAKE_REQUIRED_DEFINITIONS -D_hypot=hypot)
endif (MINGW)
unset(${OUTPUT_VARNAME} CACHE)
CHECK_CXX_SOURCE_COMPILES("
#include <Python.h>
int main(int argc, char *argv[]) {
Py_InitializeEx(0);
}" ${OUTPUT_VARNAME})
endfunction()
if (MINGW)
option(ENABLE_PYTHON_DEPS "Enable Python deps (sip, pyqt)" ON)
if (ENABLE_PYTHON_DEPS)
# First get the Python library path to make sure the selected Python
# can be used for Building, then try to get the Python interpreter again
find_package(PythonInterp 3.6 EXACT)
find_package(PythonLibs 3.6 EXACT)
if (PYTHONLIBS_FOUND)
# sip and pyqt does not use the CMake variable "PYTHON_LIBRARIES".
# We point to python.exe directly because we want to get the exact
# Python build for the target architecture, so that sip and pyqt
# will find the proper lib
get_filename_component(PYTHON_DIR ${PYTHON_LIBRARIES} DIRECTORY)
get_filename_component(PYTHON_DIR ${PYTHON_DIR} DIRECTORY)
set(PYTHON_EXECUTABLE "${PYTHON_DIR}/python.exe")
message(STATUS "Set Python executable: ${PYTHON_EXECUTABLE}")
find_package(PythonInterp 3.6 EXACT)
endif (PYTHONLIBS_FOUND)
if (PYTHONLIBS_FOUND AND PYTHONINTERP_FOUND)
message(STATUS "Python requirements met.")
else (PYTHONLIBS_FOUND AND PYTHONINTERP_FOUND)
message(FATAL_ERROR "Python requirements not met. To disable Python deps, set ENABLE_PYTHON_DEPS to OFF.")
endif (PYTHONLIBS_FOUND AND PYTHONINTERP_FOUND)
TestCompileLinkPythonLibs(CAN_USE_PYTHON_LIBS)
if (NOT CAN_USE_PYTHON_LIBS)
message(FATAL_ERROR "Compiling with Python library failed, please check whether the architecture is correct!")
endif (NOT CAN_USE_PYTHON_LIBS)
endif (ENABLE_PYTHON_DEPS)
endif (MINGW)
# this list must be dependency-ordered # this list must be dependency-ordered
add_subdirectory( ext_python ) if (ENABLE_PYTHON_DEPS OR NOT MINGW)
add_subdirectory( ext_python )
endif (ENABLE_PYTHON_DEPS OR NOT MINGW)
if (MSVC) if (MSVC)
add_subdirectory( ext_patch ) add_subdirectory( ext_patch )
add_subdirectory( ext_png2ico ) add_subdirectory( ext_png2ico )
...@@ -164,8 +211,10 @@ add_subdirectory( ext_qt ) ...@@ -164,8 +211,10 @@ add_subdirectory( ext_qt )
add_subdirectory( ext_poppler ) add_subdirectory( ext_poppler )
add_subdirectory( ext_libraw ) add_subdirectory( ext_libraw )
add_subdirectory( ext_frameworks ) add_subdirectory( ext_frameworks )
add_subdirectory( ext_sip ) if (ENABLE_PYTHON_DEPS OR NOT MINGW)
add_subdirectory( ext_pyqt ) add_subdirectory( ext_sip )
add_subdirectory( ext_pyqt )
endif (ENABLE_PYTHON_DEPS OR NOT MINGW)
if (MSVC OR MINGW) if (MSVC OR MINGW)
add_subdirectory( ext_drmingw ) add_subdirectory( ext_drmingw )
......
...@@ -34,7 +34,7 @@ elseif(MINGW) ...@@ -34,7 +34,7 @@ elseif(MINGW)
URL_MD5 d978884753df265896eda436d8f4e07b URL_MD5 d978884753df265896eda436d8f4e07b
PATCH_COMMAND ${PATCH_COMMAND} -p1 -i ${CMAKE_CURRENT_SOURCE_DIR}/pyqt-configure-fix.patch PATCH_COMMAND ${PATCH_COMMAND} -p1 -i ${CMAKE_CURRENT_SOURCE_DIR}/pyqt-configure-fix.patch
CONFIGURE_COMMAND python.exe <SOURCE_DIR>/configure.py ${_PYQT_conf} CONFIGURE_COMMAND ${PYTHON_EXECUTABLE} <SOURCE_DIR>/configure.py ${_PYQT_conf}
BUILD_COMMAND mingw32-make -j${SUBMAKE_JOBS} CXXFLAGS=-D_hypot=hypot LDFLAGS=${SECURITY_SHARED_LINKER_FLAGS} BUILD_COMMAND mingw32-make -j${SUBMAKE_JOBS} CXXFLAGS=-D_hypot=hypot LDFLAGS=${SECURITY_SHARED_LINKER_FLAGS}
INSTALL_COMMAND mingw32-make -j${SUBMAKE_JOBS} install INSTALL_COMMAND mingw32-make -j${SUBMAKE_JOBS} install
......
...@@ -27,7 +27,7 @@ elseif (MINGW) ...@@ -27,7 +27,7 @@ elseif (MINGW)
URL https://sourceforge.net/projects/pyqt/files/sip/sip-4.19.3/sip-4.19.3.zip URL https://sourceforge.net/projects/pyqt/files/sip/sip-4.19.3/sip-4.19.3.zip
URL_MD5 1098da9ee1915354fedf38fd6fbe22ce URL_MD5 1098da9ee1915354fedf38fd6fbe22ce
CONFIGURE_COMMAND python.exe <SOURCE_DIR>/configure.py ${_SIP_conf} CONFIGURE_COMMAND ${PYTHON_EXECUTABLE} <SOURCE_DIR>/configure.py ${_SIP_conf}
BUILD_COMMAND mingw32-make -j${SUBMAKE_JOBS} LDFLAGS=${SECURITY_SHARED_LINKER_FLAGS} BUILD_COMMAND mingw32-make -j${SUBMAKE_JOBS} LDFLAGS=${SECURITY_SHARED_LINKER_FLAGS}
INSTALL_COMMAND mingw32-make -j${SUBMAKE_JOBS} install INSTALL_COMMAND mingw32-make -j${SUBMAKE_JOBS} install
......
...@@ -190,15 +190,45 @@ include(MacroJPEG) ...@@ -190,15 +190,45 @@ include(MacroJPEG)
## so we should request the correct version in advance ## ## so we should request the correct version in advance ##
########################################################### ###########################################################
function(TestCompileLinkPythonLibs OUTPUT_VARNAME)
include(CheckCXXSourceCompiles)
set(CMAKE_REQUIRED_INCLUDES ${PYTHON_INCLUDE_PATH})
set(CMAKE_REQUIRED_LIBRARIES ${PYTHON_LIBRARIES})
if (MINGW)
set(CMAKE_REQUIRED_DEFINITIONS -D_hypot=hypot)
endif (MINGW)
unset(${OUTPUT_VARNAME} CACHE)
CHECK_CXX_SOURCE_COMPILES("
#include <Python.h>
int main(int argc, char *argv[]) {
Py_InitializeEx(0);
}" ${OUTPUT_VARNAME})
endfunction()
if(MINGW) if(MINGW)
# Special check: Building on Windows and ext_python is used # First get the Python library path to make sure the selected Python
find_package(PythonInterp 3.6) # can be used for Building, then try to get the Python interpreter again
if(PYTHONINTERP_FOUND) find_package(PythonInterp 3.6 EXACT)
find_package(PythonLibrary 3.6) find_package(PythonLibs 3.6 EXACT)
if(PYTHONLIBS_FOUND) if (PYTHONLIBS_FOUND)
include("${CMAKE_CURRENT_SOURCE_DIR}/PythonWindowsCheck.cmake") # Use the same Python as the library
endif(PYTHONLIBS_FOUND) get_filename_component(PYTHON_DIR ${PYTHON_LIBRARIES} DIRECTORY)
endif(PYTHONINTERP_FOUND) get_filename_component(PYTHON_DIR ${PYTHON_DIR} DIRECTORY)
set(PYTHON_EXECUTABLE "${PYTHON_DIR}/python.exe")
message(STATUS "Set Python executable: ${PYTHON_EXECUTABLE}")
find_package(PythonInterp 3.6 EXACT)
if (PYTHONINTERP_FOUND)
find_package(PythonLibrary 3.6)
else (PYTHONINTERP_FOUND)
# This shouldn't happen on Windows...
message(FATAL_ERROR "Python library found but python.exe not found!")
endif (PYTHONINTERP_FOUND)
TestCompileLinkPythonLibs(CAN_USE_PYTHON_LIBS)
if (NOT CAN_USE_PYTHON_LIBS)
message(WARNING "Compiling with Python library failed, please check whether the architecture is correct. Python will be disabled.")
unset(PYTHONLIBS_FOUND CACHE)
endif (NOT CAN_USE_PYTHON_LIBS)
endif (PYTHONLIBS_FOUND)
else(MINGW) else(MINGW)
find_package(PythonInterp 3.0) find_package(PythonInterp 3.0)
find_package(PythonLibrary 3.0) find_package(PythonLibrary 3.0)
......
# Check whether the found python is the same as ext_python
# HACK: Find pythonxx.dll and compare equality. Probably not the best idea...
# TODO: Check the python version
set(_check_python_dll "python36.dll")
if(NOT ${PYTHONLIBS_VERSION_STRING} VERSION_EQUAL "3.6.2")
message(FATAL_ERROR "Windows build with Python requires Python 3.6.2, found version ${PYTHONLIBS_VERSION_STRING} instead.")
else()
if(EXISTS "${CMAKE_INSTALL_PREFIX}/python/${_check_python_dll}")
message(STATUS "python36.dll is found in \"${CMAKE_INSTALL_PREFIX}/python/\".")
file(SHA1 "${CMAKE_INSTALL_PREFIX}/python/${_check_python_dll}" _ext_python_dll_sha1)
get_filename_component(_found_python_dir ${PYTHON_EXECUTABLE} DIRECTORY)
file(SHA1 "${_found_python_dir}/${_check_python_dll}" _found_python_dll_sha1)
if(NOT ${_ext_python_dll_sha1} STREQUAL ${_found_python_dll_sha1})
message(FATAL_ERROR "The found ${_check_python_dll} is not the same as the ${_check_python_dll} from ext_python.")
endif()
else()
message(FATAL_ERROR "${_check_python_dll} is NOT found in \"${CMAKE_INSTALL_PREFIX}/python/\".")
endif()
endif()
unset(_check_python_dll)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment