Warning, /frameworks/kdelibs4support/cmake/modules/PythonMacros.cmake is written in an unsupported language. File is not indexed.

0001 # Python macros
0002 # ~~~~~~~~~~~~~
0003 # Copyright (c) 2007, Simon Edwards <simon@simonzone.com>
0004 # Copyright (c) 2012, Luca Beltrame <lbeltrame@kde.org>
0005 # Copyright (c) 2012, Rolf Eike Beer <eike@sf-mail.de>
0006 #
0007 # Redistribution and use is allowed according to the terms of the BSD license.
0008 # For details see the accompanying COPYING-CMAKE-SCRIPTS file.
0009 #
0010 # This file defines the following macros:
0011 #
0012 # PYTHON_INSTALL (SOURCE_FILE DESTINATION_DIR)
0013 #     Install the SOURCE_FILE, which is a Python .py file, into the
0014 #     destination directory during install. The file will be byte compiled
0015 #     and both the .py file and .pyc file will be installed.
0016 
0017 set(PYTHON_MACROS_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR})
0018 
0019 macro(PYTHON_INSTALL SOURCE_FILE DESTINATION_DIR)
0020 
0021   find_file(_python_compile_py PythonCompile.py PATHS ${CMAKE_MODULE_PATH})
0022 
0023   # Install the source file.
0024   install(FILES ${SOURCE_FILE} DESTINATION ${DESTINATION_DIR})
0025 
0026   # Byte compile and install the .pyc file, unless explicitly prevented by env..
0027   if("$ENV{PYTHONDONTWRITEBYTECODE}" STREQUAL "")
0028     get_filename_component(_absfilename ${SOURCE_FILE} ABSOLUTE)
0029     get_filename_component(_filename ${SOURCE_FILE} NAME)
0030     get_filename_component(_filenamebase ${SOURCE_FILE} NAME_WE)
0031     get_filename_component(_basepath ${SOURCE_FILE} PATH)
0032 
0033     if(WIN32)
0034       # remove drive letter
0035       string(REGEX REPLACE "^[a-zA-Z]:/" "/" _basepath "${_basepath}")
0036     endif(WIN32)
0037 
0038     set(_bin_py ${CMAKE_CURRENT_BINARY_DIR}/${_basepath}/${_filename})
0039 
0040     # Python 3.2 changed the pyc file location
0041     if(PYTHON_VERSION_STRING VERSION_GREATER 3.1)
0042       # To get the right version for suffix
0043       set(_bin_pyc "${CMAKE_CURRENT_BINARY_DIR}/${_basepath}/__pycache__/${_filenamebase}.cpython-${PYTHON_VERSION_MAJOR}${PYTHON_VERSION_MINOR}.pyc")
0044       set(_py_install_dir "${DESTINATION_DIR}/__pycache__/")
0045     else()
0046       set(_bin_pyc "${CMAKE_CURRENT_BINARY_DIR}/${_basepath}/${_filenamebase}.pyc")
0047       set(_py_install_dir "${DESTINATION_DIR}")
0048     endif()
0049 
0050     file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/${_basepath})
0051 
0052     # Setting because it will be displayed later, in compile_python_files
0053     set(_message "Byte-compiling ${_bin_py} to ${_bin_pyc}")
0054 
0055     string(REPLACE "/" "_" _rule_name "${_basepath}/${_bin_pyc}")
0056     add_custom_target("${_rule_name}" ALL)
0057 
0058     get_filename_component(_abs_bin_py ${_bin_py} ABSOLUTE)
0059     if(_abs_bin_py STREQUAL _absfilename)    # Don't copy the file onto itself.
0060       add_custom_command(
0061         TARGET "${_rule_name}"
0062         COMMAND "${CMAKE_COMMAND}" -E echo "${_message}"
0063         COMMAND "${PYTHON_EXECUTABLE}" "${_python_compile_py}" "${_bin_py}"
0064         DEPENDS "${_absfilename}"
0065       )
0066     else()
0067       add_custom_command(
0068         TARGET "${_rule_name}"
0069         COMMAND "${CMAKE_COMMAND}" -E echo "${_message}"
0070         COMMAND "${CMAKE_COMMAND}" -E copy "${_absfilename}" "${_bin_py}"
0071         COMMAND "${PYTHON_EXECUTABLE}" "${_python_compile_py}" "${_bin_py}"
0072         DEPENDS "${_absfilename}"
0073       )
0074     endif()
0075 
0076     install(FILES ${_bin_pyc} DESTINATION "${_py_install_dir}")
0077     unset(_py_install_dir)
0078     unset(_message)
0079 
0080   endif("$ENV{PYTHONDONTWRITEBYTECODE}" STREQUAL "")
0081 
0082 endmacro(PYTHON_INSTALL)