Commit 2526082c authored by Luca Beltrame's avatar Luca Beltrame
Browse files

Adjust Python CMake macros in kdelibs:

- Rewrite to use more standard CMake macros
- Use FindPythonInterp and FindPythonLibrary from upstream CMake
- Properly support Python byte-compiled files from Python 3.2 or higher
  (namely, __pycache__ directory containing *.pyc files)

REVIEW: 107148
parents a6d50e9a 9b5c1dc4
......@@ -12,7 +12,7 @@
# PYTHON_LONG_VERSION - The version of the Python interpreter found as a human
# readable string.
#
# PYTHON_SITE_PACKAGES_INSTALL_DIR - this cache variable can be used for installing
# PYTHON_SITE_PACKAGES_INSTALL_DIR - this cache variable can be used for installing
# own python modules. You may want to adjust this to be the
# same as ${PYTHON_SITE_PACKAGES_DIR}, but then admin
# privileges may be required for installation.
......@@ -24,83 +24,39 @@
# PYTHON_LIBRARY, PYTHON_LIBRARIES- Location of the Python library.
# Copyright (c) 2007, Simon Edwards <simon@simonzone.com>
# Copyright (c) 2012, Luca Beltrame <lbeltrame@kde.org>
# Redistribution and use is allowed according to the terms of the BSD license.
# For details see the accompanying COPYING-CMAKE-SCRIPTS file.
include(FindPackageHandleStandardArgs)
find_package(PythonInterp)
include(CMakeFindFrameworks)
if (PYTHONINTERP_FOUND)
if(EXISTS PYTHON_LIBRARY)
# Already in cache, be silent
set(PYTHONLIBRARY_FOUND TRUE)
else(EXISTS PYTHON_LIBRARY)
# Set the Python libraries to what we actually found for interpreters
set(Python_ADDITIONAL_VERSIONS "${PYTHON_VERSION_MAJOR}.${PYTHON_VERSION_MINOR}")
# These are kept for compatibility
set(PYTHON_SHORT_VERSION "${PYTHON_VERSION_MAJOR}.${PYTHON_VERSION_MINOR}")
set(PYTHON_LONG_VERSION ${PYTHON_VERSION_STRING})
find_package(PythonInterp)
find_package(PythonLibs QUIET)
if(PYTHONINTERP_FOUND)
if(PYTHONLIBS_FOUND)
set(PYTHON_LIBRARY ${PYTHON_LIBRARIES})
endif(PYTHONLIBS_FOUND)
# get the directory of the current file, used later on in the file
get_filename_component( _py_cmake_module_dir ${CMAKE_CURRENT_LIST_FILE} PATH)
if(NOT EXISTS "${_py_cmake_module_dir}/FindLibPython.py")
message(FATAL_ERROR "The file FindLibPython.py does not exist in ${_py_cmake_module_dir} (the directory where FindPythonLibrary.cmake is located). Check your installation.")
endif(NOT EXISTS "${_py_cmake_module_dir}/FindLibPython.py")
# Auto detect Python site-packages directory
execute_process(COMMAND ${PYTHON_EXECUTABLE} -c "from distutils.sysconfig import get_python_lib; print(get_python_lib(True))" OUTPUT_VARIABLE PYTHON_SITE_PACKAGES_DIR OUTPUT_STRIP_TRAILING_WHITESPACE)
execute_process(COMMAND ${PYTHON_EXECUTABLE} "${_py_cmake_module_dir}/FindLibPython.py" OUTPUT_VARIABLE python_config)
if(python_config)
string(REGEX REPLACE ".*exec_prefix:([^\n]+).*$" "\\1" PYTHON_PREFIX ${python_config})
string(REGEX REPLACE ".*\nshort_version:([^\n]+).*$" "\\1" PYTHON_SHORT_VERSION ${python_config})
string(REGEX REPLACE ".*\nlong_version:([^\n]+).*$" "\\1" PYTHON_LONG_VERSION ${python_config})
# Set the destination install directory to be the same as the site-packages dir if not supplied
if(NOT DEFINED PYTHON_SITE_PACKAGES_INSTALL_DIR)
set(PYTHON_SITE_PACKAGES_INSTALL_DIR "${PYTHON_SITE_PACKAGES_DIR}")
endif(NOT DEFINED PYTHON_SITE_PACKAGES_INSTALL_DIR)
string(REGEX REPLACE ".*\npy_inc_dir:([^\n]+).*$" "\\1" _TMP_PYTHON_INCLUDE_PATH ${python_config})
string(REGEX REPLACE ".*\nsite_packages_dir:([^\n]+).*$" "\\1" _TMP_PYTHON_SITE_PACKAGES_DIR ${python_config})
message (STATUS "Python system site-packages directory: ${PYTHON_SITE_PACKAGES_DIR}")
message(STATUS "Installing Python modules to: ${PYTHON_SITE_PACKAGES_INSTALL_DIR}")
# Put these two variables in the cache so they are visible for the user, but read-only:
set(PYTHON_INCLUDE_PATH "${_TMP_PYTHON_INCLUDE_PATH}" CACHE PATH "The python include directory" FORCE)
set(PYTHON_SITE_PACKAGES_DIR "${_TMP_PYTHON_SITE_PACKAGES_DIR}" CACHE PATH "The python site packages dir" FORCE)
endif(PYTHONINTERP_FOUND)
# This one is intended to be used and changed by the user for installing own modules:
set(PYTHON_SITE_PACKAGES_INSTALL_DIR ${CMAKE_INSTALL_PREFIX}/lib${LIB_SUFFIX}/python${PYTHON_SHORT_VERSION}/site-packages CACHE PATH "The directory where python modules will be installed to.")
string(REGEX REPLACE "([0-9]+).([0-9]+)" "\\1\\2" PYTHON_SHORT_VERSION_NO_DOT ${PYTHON_SHORT_VERSION})
set(PYTHON_LIBRARY_NAMES python${PYTHON_SHORT_VERSION} python${PYTHON_SHORT_VERSION_NO_DOT})
if(WIN32)
string(REPLACE "\\" "/" PYTHON_SITE_PACKAGES_DIR ${PYTHON_SITE_PACKAGES_DIR})
endif(WIN32)
find_library(PYTHON_LIBRARY NAMES ${PYTHON_LIBRARY_NAMES} PATHS ${PYTHON_PREFIX}/lib ${PYTHON_PREFIX}/libs NO_DEFAULT_PATH)
set(PYTHONLIBRARY_FOUND TRUE)
endif(python_config)
# adapted from cmake's builtin FindPythonLibs
if(APPLE)
cmake_find_frameworks(Python)
set(PYTHON_FRAMEWORK_INCLUDES)
if(Python_FRAMEWORKS)
# If a framework has been selected for the include path,
# make sure "-framework" is used to link it.
if("${PYTHON_INCLUDE_PATH}" MATCHES "Python\\.framework")
set(PYTHON_LIBRARY "")
set(PYTHON_DEBUG_LIBRARY "")
endif("${PYTHON_INCLUDE_PATH}" MATCHES "Python\\.framework")
if(NOT PYTHON_LIBRARY)
set (PYTHON_LIBRARY "-framework Python" CACHE FILEPATH "Python Framework" FORCE)
endif(NOT PYTHON_LIBRARY)
set(PYTHONLIBRARY_FOUND TRUE)
endif(Python_FRAMEWORKS)
endif(APPLE)
endif(PYTHONINTERP_FOUND)
if(PYTHONLIBRARY_FOUND)
set(PYTHON_LIBRARIES ${PYTHON_LIBRARY})
if(NOT PYTHONLIBRARY_FIND_QUIETLY)
message(STATUS "Found Python executable: ${PYTHON_EXECUTABLE}")
message(STATUS "Found Python version: ${PYTHON_LONG_VERSION}")
message(STATUS "Found Python library: ${PYTHON_LIBRARY}")
endif(NOT PYTHONLIBRARY_FIND_QUIETLY)
else(PYTHONLIBRARY_FOUND)
if(PYTHONLIBRARY_FIND_REQUIRED)
message(FATAL_ERROR "Could not find Python")
endif(PYTHONLIBRARY_FIND_REQUIRED)
endif(PYTHONLIBRARY_FOUND)
endif (EXISTS PYTHON_LIBRARY)
find_package_handle_standard_args(PythonLibrary DEFAULT_MSG PYTHON_LIBRARY)
# Python macros
# ~~~~~~~~~~~~~
# Copyright (c) 2007, Simon Edwards <simon@simonzone.com>
# Copyright (c) 2012, Luca Beltrame <lbeltrame@kde.org>
# Copyright (c) 2012, Rolf Eike Beer <eike@sf-mail.de>
#
# Redistribution and use is allowed according to the terms of the BSD license.
# For details see the accompanying COPYING-CMAKE-SCRIPTS file.
......@@ -12,64 +14,65 @@
# destination directory during install. The file will be byte compiled
# and both the .py file and .pyc file will be installed.
GET_FILENAME_COMPONENT(PYTHON_MACROS_MODULE_PATH ${CMAKE_CURRENT_LIST_FILE} PATH)
set(PYTHON_MACROS_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR})
MACRO(PYTHON_INSTALL SOURCE_FILE DESTINATION_DIR)
macro(PYTHON_INSTALL SOURCE_FILE DESTINATION_DIR)
FIND_FILE(_python_compile_py PythonCompile.py PATHS ${CMAKE_MODULE_PATH})
find_file(_python_compile_py PythonCompile.py PATHS ${CMAKE_MODULE_PATH})
ADD_CUSTOM_TARGET(compile_python_files ALL)
add_custom_target(compile_python_files ALL)
# Install the source file.
INSTALL(FILES ${SOURCE_FILE} DESTINATION ${DESTINATION_DIR})
install(FILES ${SOURCE_FILE} DESTINATION ${DESTINATION_DIR})
# Byte compile and install the .pyc file.
GET_FILENAME_COMPONENT(_absfilename ${SOURCE_FILE} ABSOLUTE)
GET_FILENAME_COMPONENT(_filename ${SOURCE_FILE} NAME)
GET_FILENAME_COMPONENT(_filenamebase ${SOURCE_FILE} NAME_WE)
GET_FILENAME_COMPONENT(_basepath ${SOURCE_FILE} PATH)
# Byte compile and install the .pyc file.
get_filename_component(_absfilename ${SOURCE_FILE} ABSOLUTE)
get_filename_component(_filename ${SOURCE_FILE} NAME)
get_filename_component(_filenamebase ${SOURCE_FILE} NAME_WE)
get_filename_component(_basepath ${SOURCE_FILE} PATH)
if(WIN32)
string(REGEX REPLACE ".:/" "/" _basepath "${_basepath}")
# remove drive letter
string(REGEX REPLACE "^[a-zA-Z]:/" "/" _basepath "${_basepath}")
endif(WIN32)
SET(_bin_py ${CMAKE_CURRENT_BINARY_DIR}/${_basepath}/${_filename})
set(_bin_py ${CMAKE_CURRENT_BINARY_DIR}/${_basepath}/${_filename})
# Python 3.2 changed the pyc file location
IF(PYTHON_SHORT_VERSION GREATER 3.1)
if(PYTHON_VERSION_STRING VERSION_GREATER 3.1)
# To get the right version for suffix
STRING(REPLACE "." "" _suffix ${PYTHON_SHORT_VERSION})
SET(_bin_pyc ${CMAKE_CURRENT_BINARY_DIR}/${_basepath}/__pycache__/${_filenamebase}.cpython-${_suffix}.pyc)
ELSE(PYTHON_SHORT_VERSION GREATER 3.1)
SET(_bin_pyc ${CMAKE_CURRENT_BINARY_DIR}/${_basepath}/${_filenamebase}.pyc)
ENDIF(PYTHON_SHORT_VERSION GREATER 3.1)
set(_bin_pyc "${CMAKE_CURRENT_BINARY_DIR}/${_basepath}/__pycache__/${_filenamebase}.cpython-${PYTHON_VERSION_MAJOR}${PYTHON_VERSION_MINOR}.pyc")
set(_py_install_dir "${DESTINATION_DIR}/__pycache__/")
else()
set(_bin_pyc "${CMAKE_CURRENT_BINARY_DIR}/${_basepath}/${_filenamebase}.pyc")
set(_py_install_dir "${DESTINATION_DIR}")
endif()
FILE(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/${_basepath})
SET(_message "Byte-compiling ${_bin_py}")
# Setting because it will be displayed later, in compile_python_files
set(_message "Byte-compiling ${_bin_py} to ${_bin_pyc}")
GET_FILENAME_COMPONENT(_abs_bin_py ${_bin_py} ABSOLUTE)
IF(_abs_bin_py STREQUAL ${_absfilename}) # Don't copy the file onto itself.
ADD_CUSTOM_COMMAND(
get_filename_component(_abs_bin_py ${_bin_py} ABSOLUTE)
if(_abs_bin_py STREQUAL _absfilename) # Don't copy the file onto itself.
add_custom_command(
TARGET compile_python_files
COMMAND ${CMAKE_COMMAND} -E echo ${_message}
COMMAND ${PYTHON_EXECUTABLE} ${_python_compile_py} ${_bin_py}
DEPENDS ${_absfilename}
COMMAND "${CMAKE_COMMAND}" -E echo "${_message}"
COMMAND "${PYTHON_EXECUTABLE}" "${_python_compile_py}" "${_bin_py}"
DEPENDS "${_absfilename}"
)
ELSE(_abs_bin_py STREQUAL ${_absfilename})
ADD_CUSTOM_COMMAND(
else()
add_custom_command(
TARGET compile_python_files
COMMAND ${CMAKE_COMMAND} -E echo ${_message}
COMMAND ${CMAKE_COMMAND} -E copy ${_absfilename} ${_bin_py}
COMMAND ${PYTHON_EXECUTABLE} ${_python_compile_py} ${_bin_py}
DEPENDS ${_absfilename}
COMMAND "${CMAKE_COMMAND}" -E echo "${_message}"
COMMAND "${CMAKE_COMMAND}" -E copy "${_absfilename}" "${_bin_py}"
COMMAND "${PYTHON_EXECUTABLE}" "${_python_compile_py}" "${_bin_py}"
DEPENDS "${_absfilename}"
)
ENDIF(_abs_bin_py STREQUAL ${_absfilename})
endif()
IF(PYTHON_SHORT_VERSION GREATER 3.1)
INSTALL(FILES ${_bin_pyc} DESTINATION ${DESTINATION_DIR}/__pycache__/)
ELSE (PYTHON_SHORT_VERSION GREATER 3.1)
INSTALL(FILES ${_bin_pyc} DESTINATION ${DESTINATION_DIR})
ENDIF (PYTHON_SHORT_VERSION GREATER 3.1)
install(FILES ${_bin_pyc} DESTINATION "${_py_install_dir}")
unset(_py_install_dir)
unset(_message)
ENDMACRO(PYTHON_INSTALL)
endmacro(PYTHON_INSTALL)
Supports Markdown
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