root / cmake / Modules / GrPython.cmake @ accb9f2f
History | View | Annotate | Download (6.6 kB)
| 1 | # Copyright 2010-2011 Free Software Foundation, Inc. |
|---|---|
| 2 | # |
| 3 | # This file is part of GNU Radio |
| 4 | # |
| 5 | # GNU Radio is free software; you can redistribute it and/or modify |
| 6 | # it under the terms of the GNU General Public License as published by |
| 7 | # the Free Software Foundation; either version 3, or (at your option) |
| 8 | # any later version. |
| 9 | # |
| 10 | # GNU Radio is distributed in the hope that it will be useful, |
| 11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | # GNU General Public License for more details. |
| 14 | # |
| 15 | # You should have received a copy of the GNU General Public License |
| 16 | # along with GNU Radio; see the file COPYING. If not, write to |
| 17 | # the Free Software Foundation, Inc., 51 Franklin Street, |
| 18 | # Boston, MA 02110-1301, USA. |
| 19 | |
| 20 | IF(NOT DEFINED INCLUDED_GR_PYTHON_CMAKE) |
| 21 | SET(INCLUDED_GR_PYTHON_CMAKE TRUE) |
| 22 | |
| 23 | ######################################################################## |
| 24 | # Setup the python interpreter: |
| 25 | # This allows the user to specify a specific interpreter, |
| 26 | # or finds the interpreter via the built-in cmake module. |
| 27 | ######################################################################## |
| 28 | #this allows the user to override PYTHON_EXECUTABLE |
| 29 | IF(PYTHON_EXECUTABLE) |
| 30 | |
| 31 | SET(PYTHONINTERP_FOUND TRUE) |
| 32 | |
| 33 | #otherwise if not set, try to automatically find it |
| 34 | ELSE(PYTHON_EXECUTABLE) |
| 35 | |
| 36 | #use the built-in find script |
| 37 | FIND_PACKAGE(PythonInterp) |
| 38 | |
| 39 | #and if that fails use the find program routine |
| 40 | IF(NOT PYTHONINTERP_FOUND) |
| 41 | FIND_PROGRAM(PYTHON_EXECUTABLE NAMES python python2.7 python2.6 python2.5) |
| 42 | IF(PYTHON_EXECUTABLE) |
| 43 | SET(PYTHONINTERP_FOUND TRUE) |
| 44 | ENDIF(PYTHON_EXECUTABLE) |
| 45 | ENDIF(NOT PYTHONINTERP_FOUND) |
| 46 | |
| 47 | ENDIF(PYTHON_EXECUTABLE) |
| 48 | |
| 49 | #make the path to the executable appear in the cmake gui |
| 50 | SET(PYTHON_EXECUTABLE ${PYTHON_EXECUTABLE} CACHE FILEPATH "python interpreter")
|
| 51 | |
| 52 | IF(NOT PYTHONINTERP_FOUND) |
| 53 | MESSAGE(FATAL_ERROR "Error: Python interpretor required by the build system.") |
| 54 | ENDIF(NOT PYTHONINTERP_FOUND) |
| 55 | |
| 56 | ######################################################################## |
| 57 | # Check for the existence of a python module: |
| 58 | # - desc a string description of the check |
| 59 | # - mod the name of the module to import |
| 60 | # - cmd an additional command to run |
| 61 | # - have the result variable to set |
| 62 | ######################################################################## |
| 63 | MACRO(GR_PYTHON_CHECK_MODULE desc mod cmd have) |
| 64 | MESSAGE(STATUS "") |
| 65 | MESSAGE(STATUS "Python checking for ${desc}")
|
| 66 | EXECUTE_PROCESS( |
| 67 | COMMAND ${PYTHON_EXECUTABLE} -c "
|
| 68 | ######################################### |
| 69 | try: import ${mod}
|
| 70 | except: exit(-1) |
| 71 | try: assert ${cmd}
|
| 72 | except: exit(-1) |
| 73 | #########################################" |
| 74 | RESULT_VARIABLE ${have}
|
| 75 | ) |
| 76 | IF(${have} EQUAL 0)
|
| 77 | MESSAGE(STATUS "Python checking for ${desc} - found")
|
| 78 | SET(${have} TRUE)
|
| 79 | ELSE(${have} EQUAL 0)
|
| 80 | MESSAGE(STATUS "Python checking for ${desc} - not found")
|
| 81 | SET(${have} FALSE)
|
| 82 | ENDIF(${have} EQUAL 0)
|
| 83 | ENDMACRO(GR_PYTHON_CHECK_MODULE) |
| 84 | |
| 85 | ######################################################################## |
| 86 | # Sets the python installation directory GR_PYTHON_DIR |
| 87 | ######################################################################## |
| 88 | EXECUTE_PROCESS(COMMAND ${PYTHON_EXECUTABLE} -c "
|
| 89 | from distutils import sysconfig |
| 90 | print sysconfig.get_python_lib(prefix='') |
| 91 | " OUTPUT_VARIABLE GR_PYTHON_DIR OUTPUT_STRIP_TRAILING_WHITESPACE |
| 92 | ) |
| 93 | FILE(TO_CMAKE_PATH ${GR_PYTHON_DIR} GR_PYTHON_DIR)
|
| 94 | |
| 95 | ######################################################################## |
| 96 | # Install python sources (also builds and installs byte-compiled python) |
| 97 | ######################################################################## |
| 98 | FUNCTION(GR_PYTHON_INSTALL) |
| 99 | INCLUDE(CMakeParseArgumentsCopy) |
| 100 | CMAKE_PARSE_ARGUMENTS(GR_PYTHON_INSTALL "" "DESTINATION;COMPONENT" "FILES;PROGRAMS" ${ARGN})
|
| 101 | |
| 102 | #################################################################### |
| 103 | IF(GR_PYTHON_INSTALL_FILES) |
| 104 | #################################################################### |
| 105 | INSTALL(${ARGN}) #installs regular python files
|
| 106 | |
| 107 | FOREACH(pyfile ${GR_PYTHON_INSTALL_FILES})
|
| 108 | GET_FILENAME_COMPONENT(pyfile_name ${pyfile} NAME)
|
| 109 | GET_FILENAME_COMPONENT(pyfile ${pyfile} ABSOLUTE)
|
| 110 | STRING(REPLACE "${CMAKE_SOURCE_DIR}" "${CMAKE_BINARY_DIR}" pycfile "${pyfile}c")
|
| 111 | LIST(APPEND python_install_gen_targets ${pycfile})
|
| 112 | |
| 113 | GET_FILENAME_COMPONENT(pycfile_path ${pycfile} PATH)
|
| 114 | FILE(MAKE_DIRECTORY ${pycfile_path})
|
| 115 | |
| 116 | #create a command to generate the byte-compiled pyc file |
| 117 | ADD_CUSTOM_COMMAND( |
| 118 | OUTPUT ${pycfile} DEPENDS ${pyfile}
|
| 119 | COMMAND ${PYTHON_EXECUTABLE} -c
|
| 120 | \"import py_compile\; py_compile.compile(file='${pyfile}', cfile='${pycfile}', doraise=True)\"
|
| 121 | COMMENT "Byte-compiling ${pyfile_name}"
|
| 122 | ) |
| 123 | INSTALL(FILES ${pycfile}
|
| 124 | DESTINATION ${GR_PYTHON_INSTALL_DESTINATION}
|
| 125 | COMPONENT ${GR_PYTHON_INSTALL_COMPONENT}
|
| 126 | ) |
| 127 | ENDFOREACH(pyfile) |
| 128 | |
| 129 | #################################################################### |
| 130 | ELSEIF(GR_PYTHON_INSTALL_PROGRAMS) |
| 131 | #################################################################### |
| 132 | FILE(TO_NATIVE_PATH ${PYTHON_EXECUTABLE} pyexe_native)
|
| 133 | |
| 134 | FOREACH(pyfile ${GR_PYTHON_INSTALL_PROGRAMS})
|
| 135 | GET_FILENAME_COMPONENT(pyfile_name ${pyfile} NAME)
|
| 136 | GET_FILENAME_COMPONENT(pyfile ${pyfile} ABSOLUTE)
|
| 137 | STRING(REPLACE "${CMAKE_SOURCE_DIR}" "${CMAKE_BINARY_DIR}" pyexefile "${pyfile}.exe")
|
| 138 | LIST(APPEND python_install_gen_targets ${pyexefile})
|
| 139 | |
| 140 | GET_FILENAME_COMPONENT(pyexefile_path ${pyexefile} PATH)
|
| 141 | FILE(MAKE_DIRECTORY ${pyexefile_path})
|
| 142 | |
| 143 | ADD_CUSTOM_COMMAND( |
| 144 | OUTPUT ${pyexefile} DEPENDS ${pyfile}
|
| 145 | COMMAND ${PYTHON_EXECUTABLE} -c
|
| 146 | \"open('${pyexefile}', 'w').write('\#!${pyexe_native}\\n'+open('${pyfile}').read())\"
|
| 147 | COMMENT "Shebangin ${pyfile_name}"
|
| 148 | ) |
| 149 | |
| 150 | #on windows, python files need an extension to execute |
| 151 | GET_FILENAME_COMPONENT(pyfile_ext ${pyfile} EXT)
|
| 152 | IF(WIN32 AND NOT pyfile_ext) |
| 153 | SET(pyfile_name "${pyfile_name}.py")
|
| 154 | ENDIF() |
| 155 | |
| 156 | INSTALL(PROGRAMS ${pyexefile} RENAME ${pyfile_name}
|
| 157 | DESTINATION ${GR_PYTHON_INSTALL_DESTINATION}
|
| 158 | COMPONENT ${GR_PYTHON_INSTALL_COMPONENT}
|
| 159 | ) |
| 160 | ENDFOREACH(pyfile) |
| 161 | |
| 162 | ENDIF() |
| 163 | |
| 164 | INCLUDE(GrMiscUtils) #unique target |
| 165 | GR_UNIQUE_TARGET("pygen" ${python_install_gen_targets})
|
| 166 | |
| 167 | ENDFUNCTION(GR_PYTHON_INSTALL) |
| 168 | |
| 169 | ENDIF(NOT DEFINED INCLUDED_GR_PYTHON_CMAKE) |