piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 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(DEFINED __INCLUDED_GR_PYTHON_CMAKE) |
| 21 | return() |
| 22 | endif() |
| 23 | set(__INCLUDED_GR_PYTHON_CMAKE TRUE) |
| 24 | |
| 25 | ######################################################################## |
| 26 | # Setup the python interpreter: |
| 27 | # This allows the user to specify a specific interpreter, |
| 28 | # or finds the interpreter via the built-in cmake module. |
| 29 | ######################################################################## |
| 30 | #this allows the user to override PYTHON_EXECUTABLE |
| 31 | if(PYTHON_EXECUTABLE) |
| 32 | |
| 33 | set(PYTHONINTERP_FOUND TRUE) |
| 34 | |
| 35 | #otherwise if not set, try to automatically find it |
| 36 | else(PYTHON_EXECUTABLE) |
| 37 | |
| 38 | #use the built-in find script |
| 39 | find_package(PythonInterp 2) |
| 40 | |
| 41 | #and if that fails use the find program routine |
| 42 | if(NOT PYTHONINTERP_FOUND) |
| 43 | find_program(PYTHON_EXECUTABLE NAMES python python2 python2.7 python2.6 python2.5) |
| 44 | if(PYTHON_EXECUTABLE) |
| 45 | set(PYTHONINTERP_FOUND TRUE) |
| 46 | endif(PYTHON_EXECUTABLE) |
| 47 | endif(NOT PYTHONINTERP_FOUND) |
| 48 | |
| 49 | endif(PYTHON_EXECUTABLE) |
| 50 | |
| 51 | #make the path to the executable appear in the cmake gui |
| 52 | set(PYTHON_EXECUTABLE ${PYTHON_EXECUTABLE} CACHE FILEPATH "python interpreter") |
| 53 | |
| 54 | #make sure we can use -B with python (introduced in 2.6) |
| 55 | if(PYTHON_EXECUTABLE) |
| 56 | execute_process( |
| 57 | COMMAND ${PYTHON_EXECUTABLE} -B -c "" |
| 58 | OUTPUT_QUIET ERROR_QUIET |
| 59 | RESULT_VARIABLE PYTHON_HAS_DASH_B_RESULT |
| 60 | ) |
| 61 | if(PYTHON_HAS_DASH_B_RESULT EQUAL 0) |
| 62 | set(PYTHON_DASH_B "-B") |
| 63 | endif() |
| 64 | endif(PYTHON_EXECUTABLE) |
| 65 | |
| 66 | ######################################################################## |
| 67 | # Check for the existence of a python module: |
| 68 | # - desc a string description of the check |
| 69 | # - mod the name of the module to import |
| 70 | # - cmd an additional command to run |
| 71 | # - have the result variable to set |
| 72 | ######################################################################## |
| 73 | macro(GR_PYTHON_CHECK_MODULE desc mod cmd have) |
| 74 | message(STATUS "") |
| 75 | message(STATUS "Python checking for ${desc}") |
| 76 | execute_process( |
| 77 | COMMAND ${PYTHON_EXECUTABLE} -c " |
| 78 | ######################################### |
| 79 | try: import ${mod} |
| 80 | except: exit(-1) |
| 81 | try: assert ${cmd} |
| 82 | except: exit(-1) |
| 83 | #########################################" |
| 84 | RESULT_VARIABLE ${have} |
| 85 | ) |
| 86 | if(${have} EQUAL 0) |
| 87 | message(STATUS "Python checking for ${desc} - found") |
| 88 | set(${have} TRUE) |
| 89 | else(${have} EQUAL 0) |
| 90 | message(STATUS "Python checking for ${desc} - not found") |
| 91 | set(${have} FALSE) |
| 92 | endif(${have} EQUAL 0) |
| 93 | endmacro(GR_PYTHON_CHECK_MODULE) |
| 94 | |
| 95 | ######################################################################## |
| 96 | # Sets the python installation directory GR_PYTHON_DIR |
| 97 | ######################################################################## |
| 98 | execute_process(COMMAND ${PYTHON_EXECUTABLE} -c " |
| 99 | from distutils import sysconfig |
| 100 | print sysconfig.get_python_lib(plat_specific=True, prefix='') |
| 101 | " OUTPUT_VARIABLE GR_PYTHON_DIR OUTPUT_STRIP_TRAILING_WHITESPACE |
| 102 | ) |
| 103 | file(TO_CMAKE_PATH ${GR_PYTHON_DIR} GR_PYTHON_DIR) |
| 104 | |
| 105 | ######################################################################## |
| 106 | # Create an always-built target with a unique name |
| 107 | # Usage: GR_UNIQUE_TARGET(<description> <dependencies list>) |
| 108 | ######################################################################## |
| 109 | function(GR_UNIQUE_TARGET desc) |
| 110 | file(RELATIVE_PATH reldir ${CMAKE_BINARY_DIR} ${CMAKE_CURRENT_BINARY_DIR}) |
| 111 | execute_process(COMMAND ${PYTHON_EXECUTABLE} -c "import re, hashlib |
| 112 | unique = hashlib.md5('${reldir}${ARGN}').hexdigest()[:5] |
| 113 | print(re.sub('\\W', '_', '${desc} ${reldir} ' + unique))" |
| 114 | OUTPUT_VARIABLE _target OUTPUT_STRIP_TRAILING_WHITESPACE) |
| 115 | add_custom_target(${_target} ALL DEPENDS ${ARGN}) |
| 116 | endfunction(GR_UNIQUE_TARGET) |
| 117 | |
| 118 | ######################################################################## |
| 119 | # Install python sources (also builds and installs byte-compiled python) |
| 120 | ######################################################################## |
| 121 | function(GR_PYTHON_INSTALL) |
| 122 | include(CMakeParseArgumentsCopy) |
| 123 | CMAKE_PARSE_ARGUMENTS(GR_PYTHON_INSTALL "" "DESTINATION;COMPONENT" "FILES;PROGRAMS" ${ARGN}) |
| 124 | |
| 125 | #################################################################### |
| 126 | if(GR_PYTHON_INSTALL_FILES) |
| 127 | #################################################################### |
| 128 | install(${ARGN}) #installs regular python files |
| 129 | |
| 130 | #create a list of all generated files |
| 131 | unset(pysrcfiles) |
| 132 | unset(pycfiles) |
| 133 | unset(pyofiles) |
| 134 | foreach(pyfile ${GR_PYTHON_INSTALL_FILES}) |
| 135 | get_filename_component(pyfile ${pyfile} ABSOLUTE) |
| 136 | list(APPEND pysrcfiles ${pyfile}) |
| 137 | |
| 138 | #determine if this file is in the source or binary directory |
| 139 | file(RELATIVE_PATH source_rel_path ${CMAKE_CURRENT_SOURCE_DIR} ${pyfile}) |
| 140 | string(LENGTH "${source_rel_path}" source_rel_path_len) |
| 141 | file(RELATIVE_PATH binary_rel_path ${CMAKE_CURRENT_BINARY_DIR} ${pyfile}) |
| 142 | string(LENGTH "${binary_rel_path}" binary_rel_path_len) |
| 143 | |
| 144 | #and set the generated path appropriately |
| 145 | if(${source_rel_path_len} GREATER ${binary_rel_path_len}) |
| 146 | set(pygenfile ${CMAKE_CURRENT_BINARY_DIR}/${binary_rel_path}) |
| 147 | else() |
| 148 | set(pygenfile ${CMAKE_CURRENT_BINARY_DIR}/${source_rel_path}) |
| 149 | endif() |
| 150 | list(APPEND pycfiles ${pygenfile}c) |
| 151 | list(APPEND pyofiles ${pygenfile}o) |
| 152 | |
| 153 | #ensure generation path exists |
| 154 | get_filename_component(pygen_path ${pygenfile} PATH) |
| 155 | file(MAKE_DIRECTORY ${pygen_path}) |
| 156 | |
| 157 | endforeach(pyfile) |
| 158 | |
| 159 | #the command to generate the pyc files |
| 160 | add_custom_command( |
| 161 | DEPENDS ${pysrcfiles} OUTPUT ${pycfiles} |
| 162 | COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_BINARY_DIR}/python_compile_helper.py ${pysrcfiles} ${pycfiles} |
| 163 | ) |
| 164 | |
| 165 | #the command to generate the pyo files |
| 166 | add_custom_command( |
| 167 | DEPENDS ${pysrcfiles} OUTPUT ${pyofiles} |
| 168 | COMMAND ${PYTHON_EXECUTABLE} -O ${CMAKE_BINARY_DIR}/python_compile_helper.py ${pysrcfiles} ${pyofiles} |
| 169 | ) |
| 170 | |
| 171 | #create install rule and add generated files to target list |
| 172 | set(python_install_gen_targets ${pycfiles} ${pyofiles}) |
| 173 | install(FILES ${python_install_gen_targets} |
| 174 | DESTINATION ${GR_PYTHON_INSTALL_DESTINATION} |
| 175 | COMPONENT ${GR_PYTHON_INSTALL_COMPONENT} |
| 176 | ) |
| 177 | |
| 178 | |
| 179 | #################################################################### |
| 180 | elseif(GR_PYTHON_INSTALL_PROGRAMS) |
| 181 | #################################################################### |
| 182 | file(TO_NATIVE_PATH ${PYTHON_EXECUTABLE} pyexe_native) |
| 183 | |
| 184 | foreach(pyfile ${GR_PYTHON_INSTALL_PROGRAMS}) |
| 185 | get_filename_component(pyfile_name ${pyfile} NAME) |
| 186 | get_filename_component(pyfile ${pyfile} ABSOLUTE) |
| 187 | string(REPLACE "${CMAKE_SOURCE_DIR}" "${CMAKE_BINARY_DIR}" pyexefile "${pyfile}.exe") |
| 188 | list(APPEND python_install_gen_targets ${pyexefile}) |
| 189 | |
| 190 | get_filename_component(pyexefile_path ${pyexefile} PATH) |
| 191 | file(MAKE_DIRECTORY ${pyexefile_path}) |
| 192 | |
| 193 | add_custom_command( |
| 194 | OUTPUT ${pyexefile} DEPENDS ${pyfile} |
| 195 | COMMAND ${PYTHON_EXECUTABLE} -c |
| 196 | \"open('${pyexefile}', 'w').write('\#!${pyexe_native}\\n'+open('${pyfile}').read())\" |
| 197 | COMMENT "Shebangin ${pyfile_name}" |
| 198 | ) |
| 199 | |
| 200 | #on windows, python files need an extension to execute |
| 201 | get_filename_component(pyfile_ext ${pyfile} EXT) |
| 202 | if(WIN32 AND NOT pyfile_ext) |
| 203 | set(pyfile_name "${pyfile_name}.py") |
| 204 | endif() |
| 205 | |
| 206 | install(PROGRAMS ${pyexefile} RENAME ${pyfile_name} |
| 207 | DESTINATION ${GR_PYTHON_INSTALL_DESTINATION} |
| 208 | COMPONENT ${GR_PYTHON_INSTALL_COMPONENT} |
| 209 | ) |
| 210 | endforeach(pyfile) |
| 211 | |
| 212 | endif() |
| 213 | |
| 214 | GR_UNIQUE_TARGET("pygen" ${python_install_gen_targets}) |
| 215 | |
| 216 | endfunction(GR_PYTHON_INSTALL) |
| 217 | |
| 218 | ######################################################################## |
| 219 | # Write the python helper script that generates byte code files |
| 220 | ######################################################################## |
| 221 | file(WRITE ${CMAKE_BINARY_DIR}/python_compile_helper.py " |
| 222 | import sys, py_compile |
| 223 | files = sys.argv[1:] |
| 224 | srcs, gens = files[:len(files)/2], files[len(files)/2:] |
| 225 | for src, gen in zip(srcs, gens): |
| 226 | py_compile.compile(file=src, cfile=gen, doraise=True) |
| 227 | ") |