PageRenderTime 45ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/blocks/gr-doppler/cmake/Modules/GrSwig.cmake

https://gitlab.com/jostikas/Thesis_final
CMake | 251 lines | 142 code | 35 blank | 74 comment | 17 complexity | 7646fabe3f730f41c145acc013c26fda MD5 | raw file
  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. if(DEFINED __INCLUDED_GR_SWIG_CMAKE)
  20. return()
  21. endif()
  22. set(__INCLUDED_GR_SWIG_CMAKE TRUE)
  23. include(GrPython)
  24. ########################################################################
  25. # Builds a swig documentation file to be generated into python docstrings
  26. # Usage: GR_SWIG_MAKE_DOCS(output_file input_path input_path....)
  27. #
  28. # Set the following variable to specify extra dependent targets:
  29. # - GR_SWIG_DOCS_SOURCE_DEPS
  30. # - GR_SWIG_DOCS_TARGET_DEPS
  31. ########################################################################
  32. function(GR_SWIG_MAKE_DOCS output_file)
  33. if(ENABLE_DOXYGEN)
  34. #setup the input files variable list, quote formated
  35. set(input_files)
  36. unset(INPUT_PATHS)
  37. foreach(input_path ${ARGN})
  38. if(IS_DIRECTORY ${input_path}) #when input path is a directory
  39. file(GLOB input_path_h_files ${input_path}/*.h)
  40. else() #otherwise its just a file, no glob
  41. set(input_path_h_files ${input_path})
  42. endif()
  43. list(APPEND input_files ${input_path_h_files})
  44. set(INPUT_PATHS "${INPUT_PATHS} \"${input_path}\"")
  45. endforeach(input_path)
  46. #determine the output directory
  47. get_filename_component(name ${output_file} NAME_WE)
  48. get_filename_component(OUTPUT_DIRECTORY ${output_file} PATH)
  49. set(OUTPUT_DIRECTORY ${OUTPUT_DIRECTORY}/${name}_swig_docs)
  50. make_directory(${OUTPUT_DIRECTORY})
  51. #generate the Doxyfile used by doxygen
  52. configure_file(
  53. ${CMAKE_SOURCE_DIR}/docs/doxygen/Doxyfile.swig_doc.in
  54. ${OUTPUT_DIRECTORY}/Doxyfile
  55. @ONLY)
  56. #Create a dummy custom command that depends on other targets
  57. include(GrMiscUtils)
  58. GR_GEN_TARGET_DEPS(_${name}_tag tag_deps ${GR_SWIG_DOCS_TARGET_DEPS})
  59. #call doxygen on the Doxyfile + input headers
  60. add_custom_command(
  61. OUTPUT ${OUTPUT_DIRECTORY}/xml/index.xml
  62. DEPENDS ${input_files} ${GR_SWIG_DOCS_SOURCE_DEPS} ${tag_deps}
  63. COMMAND ${DOXYGEN_EXECUTABLE} ${OUTPUT_DIRECTORY}/Doxyfile
  64. COMMENT "Generating doxygen xml for ${name} docs"
  65. )
  66. #call the swig_doc script on the xml files
  67. add_custom_command(
  68. OUTPUT ${output_file}
  69. DEPENDS ${input_files} ${stamp-file} ${OUTPUT_DIRECTORY}/xml/index.xml
  70. COMMAND ${PYTHON_EXECUTABLE} ${PYTHON_DASH_B}
  71. ${CMAKE_SOURCE_DIR}/docs/doxygen/swig_doc.py
  72. ${OUTPUT_DIRECTORY}/xml
  73. ${output_file}
  74. COMMENT "Generating python docstrings for ${name}"
  75. WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/docs/doxygen
  76. )
  77. else(ENABLE_DOXYGEN)
  78. file(WRITE ${output_file} "\n") #no doxygen -> empty file
  79. endif(ENABLE_DOXYGEN)
  80. endfunction(GR_SWIG_MAKE_DOCS)
  81. ########################################################################
  82. # Build a swig target for the common gnuradio use case. Usage:
  83. # GR_SWIG_MAKE(target ifile ifile ifile...)
  84. #
  85. # Set the following variables before calling:
  86. # - GR_SWIG_FLAGS
  87. # - GR_SWIG_INCLUDE_DIRS
  88. # - GR_SWIG_LIBRARIES
  89. # - GR_SWIG_SOURCE_DEPS
  90. # - GR_SWIG_TARGET_DEPS
  91. # - GR_SWIG_DOC_FILE
  92. # - GR_SWIG_DOC_DIRS
  93. ########################################################################
  94. macro(GR_SWIG_MAKE name)
  95. set(ifiles ${ARGN})
  96. # Shimming this in here to take care of a SWIG bug with handling
  97. # vector<size_t> and vector<unsigned int> (on 32-bit machines) and
  98. # vector<long unsigned int> (on 64-bit machines). Use this to test
  99. # the size of size_t, then set SIZE_T_32 if it's a 32-bit machine
  100. # or not if it's 64-bit. The logic in gr_type.i handles the rest.
  101. INCLUDE(CheckTypeSize)
  102. CHECK_TYPE_SIZE("size_t" SIZEOF_SIZE_T)
  103. CHECK_TYPE_SIZE("unsigned int" SIZEOF_UINT)
  104. if(${SIZEOF_SIZE_T} EQUAL ${SIZEOF_UINT})
  105. list(APPEND GR_SWIG_FLAGS -DSIZE_T_32)
  106. endif(${SIZEOF_SIZE_T} EQUAL ${SIZEOF_UINT})
  107. #do swig doc generation if specified
  108. if(GR_SWIG_DOC_FILE)
  109. set(GR_SWIG_DOCS_SOURCE_DEPS ${GR_SWIG_SOURCE_DEPS})
  110. list(APPEND GR_SWIG_DOCS_TARGET_DEPS ${GR_SWIG_TARGET_DEPS})
  111. GR_SWIG_MAKE_DOCS(${GR_SWIG_DOC_FILE} ${GR_SWIG_DOC_DIRS})
  112. add_custom_target(${name}_swig_doc DEPENDS ${GR_SWIG_DOC_FILE})
  113. list(APPEND GR_SWIG_TARGET_DEPS ${name}_swig_doc ${GR_RUNTIME_SWIG_DOC_FILE})
  114. endif()
  115. #append additional include directories
  116. find_package(PythonLibs 2)
  117. list(APPEND GR_SWIG_INCLUDE_DIRS ${PYTHON_INCLUDE_PATH}) #deprecated name (now dirs)
  118. list(APPEND GR_SWIG_INCLUDE_DIRS ${PYTHON_INCLUDE_DIRS})
  119. #prepend local swig directories
  120. list(INSERT GR_SWIG_INCLUDE_DIRS 0 ${CMAKE_CURRENT_SOURCE_DIR})
  121. list(INSERT GR_SWIG_INCLUDE_DIRS 0 ${CMAKE_CURRENT_BINARY_DIR})
  122. #determine include dependencies for swig file
  123. execute_process(
  124. COMMAND ${PYTHON_EXECUTABLE}
  125. ${CMAKE_BINARY_DIR}/get_swig_deps.py
  126. "${ifiles}" "${GR_SWIG_INCLUDE_DIRS}"
  127. OUTPUT_STRIP_TRAILING_WHITESPACE
  128. OUTPUT_VARIABLE SWIG_MODULE_${name}_EXTRA_DEPS
  129. WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
  130. )
  131. #Create a dummy custom command that depends on other targets
  132. include(GrMiscUtils)
  133. GR_GEN_TARGET_DEPS(_${name}_swig_tag tag_deps ${GR_SWIG_TARGET_DEPS})
  134. set(tag_file ${CMAKE_CURRENT_BINARY_DIR}/${name}.tag)
  135. add_custom_command(
  136. OUTPUT ${tag_file}
  137. DEPENDS ${GR_SWIG_SOURCE_DEPS} ${tag_deps}
  138. COMMAND ${CMAKE_COMMAND} -E touch ${tag_file}
  139. )
  140. #append the specified include directories
  141. include_directories(${GR_SWIG_INCLUDE_DIRS})
  142. list(APPEND SWIG_MODULE_${name}_EXTRA_DEPS ${tag_file})
  143. #setup the swig flags with flags and include directories
  144. set(CMAKE_SWIG_FLAGS -fvirtual -modern -keyword -w511 -module ${name} ${GR_SWIG_FLAGS})
  145. foreach(dir ${GR_SWIG_INCLUDE_DIRS})
  146. list(APPEND CMAKE_SWIG_FLAGS "-I${dir}")
  147. endforeach(dir)
  148. #set the C++ property on the swig .i file so it builds
  149. set_source_files_properties(${ifiles} PROPERTIES CPLUSPLUS ON)
  150. #setup the actual swig library target to be built
  151. include(UseSWIG)
  152. SWIG_ADD_MODULE(${name} python ${ifiles})
  153. SWIG_LINK_LIBRARIES(${name} ${PYTHON_LIBRARIES} ${GR_SWIG_LIBRARIES})
  154. if(${name} STREQUAL "runtime_swig")
  155. SET_TARGET_PROPERTIES(${SWIG_MODULE_runtime_swig_REAL_NAME} PROPERTIES DEFINE_SYMBOL "gnuradio_runtime_EXPORTS")
  156. endif(${name} STREQUAL "runtime_swig")
  157. endmacro(GR_SWIG_MAKE)
  158. ########################################################################
  159. # Install swig targets generated by GR_SWIG_MAKE. Usage:
  160. # GR_SWIG_INSTALL(
  161. # TARGETS target target target...
  162. # [DESTINATION destination]
  163. # [COMPONENT component]
  164. # )
  165. ########################################################################
  166. macro(GR_SWIG_INSTALL)
  167. include(CMakeParseArgumentsCopy)
  168. CMAKE_PARSE_ARGUMENTS(GR_SWIG_INSTALL "" "DESTINATION;COMPONENT" "TARGETS" ${ARGN})
  169. foreach(name ${GR_SWIG_INSTALL_TARGETS})
  170. install(TARGETS ${SWIG_MODULE_${name}_REAL_NAME}
  171. DESTINATION ${GR_SWIG_INSTALL_DESTINATION}
  172. COMPONENT ${GR_SWIG_INSTALL_COMPONENT}
  173. )
  174. include(GrPython)
  175. GR_PYTHON_INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/${name}.py
  176. DESTINATION ${GR_SWIG_INSTALL_DESTINATION}
  177. COMPONENT ${GR_SWIG_INSTALL_COMPONENT}
  178. )
  179. GR_LIBTOOL(
  180. TARGET ${SWIG_MODULE_${name}_REAL_NAME}
  181. DESTINATION ${GR_SWIG_INSTALL_DESTINATION}
  182. )
  183. endforeach(name)
  184. endmacro(GR_SWIG_INSTALL)
  185. ########################################################################
  186. # Generate a python file that can determine swig dependencies.
  187. # Used by the make macro above to determine extra dependencies.
  188. # When you build C++, CMake figures out the header dependencies.
  189. # This code essentially performs that logic for swig includes.
  190. ########################################################################
  191. file(WRITE ${CMAKE_BINARY_DIR}/get_swig_deps.py "
  192. import os, sys, re
  193. i_include_matcher = re.compile('%(include|import)\\s*[<|\"](.*)[>|\"]')
  194. h_include_matcher = re.compile('#(include)\\s*[<|\"](.*)[>|\"]')
  195. include_dirs = sys.argv[2].split(';')
  196. def get_swig_incs(file_path):
  197. if file_path.endswith('.i'): matcher = i_include_matcher
  198. else: matcher = h_include_matcher
  199. file_contents = open(file_path, 'r').read()
  200. return matcher.findall(file_contents, re.MULTILINE)
  201. def get_swig_deps(file_path, level):
  202. deps = [file_path]
  203. if level == 0: return deps
  204. for keyword, inc_file in get_swig_incs(file_path):
  205. for inc_dir in include_dirs:
  206. inc_path = os.path.join(inc_dir, inc_file)
  207. if not os.path.exists(inc_path): continue
  208. deps.extend(get_swig_deps(inc_path, level-1))
  209. break #found, we dont search in lower prio inc dirs
  210. return deps
  211. if __name__ == '__main__':
  212. ifiles = sys.argv[1].split(';')
  213. deps = sum([get_swig_deps(ifile, 3) for ifile in ifiles], [])
  214. #sys.stderr.write(';'.join(set(deps)) + '\\n\\n')
  215. print(';'.join(set(deps)))
  216. ")