UseSWIG.cmake 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. #.rst:
  2. # UseSWIG
  3. # -------
  4. #
  5. # SWIG module for CMake
  6. #
  7. # Defines the following macros:
  8. #
  9. # ::
  10. #
  11. # SWIG_ADD_MODULE(name language [ files ])
  12. # - Define swig module with given name and specified language
  13. # SWIG_LINK_LIBRARIES(name [ libraries ])
  14. # - Link libraries to swig module
  15. #
  16. # All other macros are for internal use only. To get the actual name of
  17. # the swig module, use: ${SWIG_MODULE_${name}_REAL_NAME}. Set Source
  18. # files properties such as CPLUSPLUS and SWIG_FLAGS to specify special
  19. # behavior of SWIG. Also global CMAKE_SWIG_FLAGS can be used to add
  20. # special flags to all swig calls. Another special variable is
  21. # CMAKE_SWIG_OUTDIR, it allows one to specify where to write all the
  22. # swig generated module (swig -outdir option) The name-specific variable
  23. # SWIG_MODULE_<name>_EXTRA_DEPS may be used to specify extra
  24. # dependencies for the generated modules. If the source file generated
  25. # by swig need some special flag you can use::
  26. #
  27. # set_source_files_properties( ${swig_generated_file_fullname}
  28. # PROPERTIES COMPILE_FLAGS "-bla")
  29. #=============================================================================
  30. # Copyright 2004-2009 Kitware, Inc.
  31. # Copyright 2009 Mathieu Malaterre <[email protected]>
  32. #
  33. # Distributed under the OSI-approved BSD License (the "License");
  34. # see accompanying file Copyright.txt for details.
  35. #
  36. # This software is distributed WITHOUT ANY WARRANTY; without even the
  37. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  38. # See the License for more information.
  39. #=============================================================================
  40. # (To distribute this file outside of CMake, substitute the full
  41. # License text for the above reference.)
  42. set(SWIG_CXX_EXTENSION "cxx")
  43. set(SWIG_EXTRA_LIBRARIES "")
  44. set(SWIG_PYTHON_EXTRA_FILE_EXTENSION "py")
  45. #
  46. # For given swig module initialize variables associated with it
  47. #
  48. macro(SWIG_MODULE_INITIALIZE name language)
  49. string(TOUPPER "${language}" swig_uppercase_language)
  50. string(TOLOWER "${language}" swig_lowercase_language)
  51. set(SWIG_MODULE_${name}_LANGUAGE "${swig_uppercase_language}")
  52. set(SWIG_MODULE_${name}_SWIG_LANGUAGE_FLAG "${swig_lowercase_language}")
  53. set(SWIG_MODULE_${name}_REAL_NAME "${name}")
  54. if (CMAKE_SWIG_FLAGS MATCHES "-noproxy")
  55. set (SWIG_MODULE_${name}_NOPROXY TRUE)
  56. endif ()
  57. if("${SWIG_MODULE_${name}_LANGUAGE}" STREQUAL "UNKNOWN")
  58. message(FATAL_ERROR "SWIG Error: Language \"${language}\" not found")
  59. elseif("${SWIG_MODULE_${name}_LANGUAGE}" STREQUAL "PYTHON" AND NOT SWIG_MODULE_${name}_NOPROXY)
  60. # swig will produce a module.py containing an 'import _modulename' statement,
  61. # which implies having a corresponding _modulename.so (*NIX), _modulename.pyd (Win32),
  62. # unless the -noproxy flag is used
  63. set(SWIG_MODULE_${name}_REAL_NAME "_${name}")
  64. elseif("${SWIG_MODULE_${name}_LANGUAGE}" STREQUAL "PERL")
  65. set(SWIG_MODULE_${name}_EXTRA_FLAGS "-shadow")
  66. elseif("${SWIG_MODULE_${name}_LANGUAGE}" STREQUAL "CSHARP")
  67. # This makes sure that the name used in the generated DllImport
  68. # matches the library name created by CMake
  69. set(SWIG_MODULE_${name}_EXTRA_FLAGS "-dllimport;${name}")
  70. endif()
  71. endmacro()
  72. #
  73. # For a given language, input file, and output file, determine extra files that
  74. # will be generated. This is internal swig macro.
  75. #
  76. macro(SWIG_GET_EXTRA_OUTPUT_FILES language outfiles generatedpath infile)
  77. set(${outfiles} "")
  78. get_source_file_property(SWIG_GET_EXTRA_OUTPUT_FILES_module_basename
  79. ${infile} SWIG_MODULE_NAME)
  80. if(SWIG_GET_EXTRA_OUTPUT_FILES_module_basename STREQUAL "NOTFOUND")
  81. # try to get module name from "%module foo" syntax
  82. if ( EXISTS ${infile} )
  83. file ( STRINGS ${infile} _MODULE_NAME REGEX "[ ]*%module[ ]*[a-zA-Z0-9_]+.*" )
  84. endif ()
  85. if ( _MODULE_NAME )
  86. string ( REGEX REPLACE "[ ]*%module[ ]*([a-zA-Z0-9_]+).*" "\\1" _MODULE_NAME "${_MODULE_NAME}" )
  87. set(SWIG_GET_EXTRA_OUTPUT_FILES_module_basename "${_MODULE_NAME}")
  88. else ()
  89. # try to get module name from "%module (options=...) foo" syntax
  90. if ( EXISTS ${infile} )
  91. file ( STRINGS ${infile} _MODULE_NAME REGEX "[ ]*%module[ ]*\\(.*\\)[ ]*[a-zA-Z0-9_]+.*" )
  92. endif ()
  93. if ( _MODULE_NAME )
  94. string ( REGEX REPLACE "[ ]*%module[ ]*\\(.*\\)[ ]*([a-zA-Z0-9_]+).*" "\\1" _MODULE_NAME "${_MODULE_NAME}" )
  95. set(SWIG_GET_EXTRA_OUTPUT_FILES_module_basename "${_MODULE_NAME}")
  96. else ()
  97. # fallback to file basename
  98. get_filename_component(SWIG_GET_EXTRA_OUTPUT_FILES_module_basename ${infile} NAME_WE)
  99. endif ()
  100. endif ()
  101. endif()
  102. foreach(it ${SWIG_${language}_EXTRA_FILE_EXTENSION})
  103. set(${outfiles} ${${outfiles}}
  104. "${generatedpath}/${SWIG_GET_EXTRA_OUTPUT_FILES_module_basename}.${it}")
  105. endforeach()
  106. endmacro()
  107. #
  108. # Take swig (*.i) file and add proper custom commands for it
  109. #
  110. macro(SWIG_ADD_SOURCE_TO_MODULE name outfiles infile)
  111. set(swig_full_infile ${infile})
  112. get_filename_component(swig_source_file_name_we "${infile}" NAME_WE)
  113. get_source_file_property(swig_source_file_generated ${infile} GENERATED)
  114. get_source_file_property(swig_source_file_cplusplus ${infile} CPLUSPLUS)
  115. get_source_file_property(swig_source_file_flags ${infile} SWIG_FLAGS)
  116. if("${swig_source_file_flags}" STREQUAL "NOTFOUND")
  117. set(swig_source_file_flags "")
  118. endif()
  119. get_filename_component(swig_source_file_fullname "${infile}" ABSOLUTE)
  120. # If CMAKE_SWIG_OUTDIR was specified then pass it to -outdir
  121. if(CMAKE_SWIG_OUTDIR)
  122. set(swig_outdir ${CMAKE_SWIG_OUTDIR})
  123. else()
  124. set(swig_outdir ${CMAKE_CURRENT_BINARY_DIR})
  125. endif()
  126. SWIG_GET_EXTRA_OUTPUT_FILES(${SWIG_MODULE_${name}_LANGUAGE}
  127. swig_extra_generated_files
  128. "${swig_outdir}"
  129. "${infile}")
  130. set(swig_generated_file_fullname
  131. "${swig_outdir}/${swig_source_file_name_we}")
  132. # add the language into the name of the file (i.e. TCL_wrap)
  133. # this allows for the same .i file to be wrapped into different languages
  134. set(swig_generated_file_fullname
  135. "${swig_generated_file_fullname}${SWIG_MODULE_${name}_LANGUAGE}_wrap")
  136. if(swig_source_file_cplusplus)
  137. set(swig_generated_file_fullname
  138. "${swig_generated_file_fullname}.${SWIG_CXX_EXTENSION}")
  139. else()
  140. set(swig_generated_file_fullname
  141. "${swig_generated_file_fullname}.c")
  142. endif()
  143. #message("Full path to source file: ${swig_source_file_fullname}")
  144. #message("Full path to the output file: ${swig_generated_file_fullname}")
  145. get_directory_property(cmake_include_directories INCLUDE_DIRECTORIES)
  146. list(REMOVE_DUPLICATES cmake_include_directories)
  147. set(swig_include_dirs)
  148. foreach(it ${cmake_include_directories})
  149. set(swig_include_dirs ${swig_include_dirs} "-I${it}")
  150. endforeach()
  151. set(swig_special_flags)
  152. # default is c, so add c++ flag if it is c++
  153. if(swig_source_file_cplusplus)
  154. set(swig_special_flags ${swig_special_flags} "-c++")
  155. endif()
  156. set(swig_extra_flags)
  157. if(SWIG_MODULE_${name}_EXTRA_FLAGS)
  158. set(swig_extra_flags ${swig_extra_flags} ${SWIG_MODULE_${name}_EXTRA_FLAGS})
  159. endif()
  160. add_custom_command(
  161. OUTPUT "${swig_generated_file_fullname}" ${swig_extra_generated_files}
  162. # Let's create the ${swig_outdir} at execution time, in case dir contains $(OutDir)
  163. COMMAND ${CMAKE_COMMAND} -E make_directory ${swig_outdir}
  164. COMMAND "${SWIG_EXECUTABLE}"
  165. ARGS "-${SWIG_MODULE_${name}_SWIG_LANGUAGE_FLAG}"
  166. ${swig_source_file_flags}
  167. ${CMAKE_SWIG_FLAGS}
  168. -outdir ${swig_outdir}
  169. ${swig_special_flags}
  170. ${swig_extra_flags}
  171. ${swig_include_dirs}
  172. -o "${swig_generated_file_fullname}"
  173. "${swig_source_file_fullname}"
  174. MAIN_DEPENDENCY "${swig_source_file_fullname}"
  175. DEPENDS ${SWIG_MODULE_${name}_EXTRA_DEPS}
  176. COMMENT "Swig source")
  177. set_source_files_properties("${swig_generated_file_fullname}" ${swig_extra_generated_files}
  178. PROPERTIES GENERATED 1)
  179. set(${outfiles} "${swig_generated_file_fullname}" ${swig_extra_generated_files})
  180. endmacro()
  181. #
  182. # Create Swig module
  183. #
  184. macro(SWIG_ADD_MODULE name language)
  185. SWIG_MODULE_INITIALIZE(${name} ${language})
  186. set(swig_dot_i_sources)
  187. set(swig_other_sources)
  188. foreach(it ${ARGN})
  189. if(${it} MATCHES ".*\\.i$")
  190. set(swig_dot_i_sources ${swig_dot_i_sources} "${it}")
  191. else()
  192. set(swig_other_sources ${swig_other_sources} "${it}")
  193. endif()
  194. endforeach()
  195. set(swig_generated_sources)
  196. foreach(it ${swig_dot_i_sources})
  197. SWIG_ADD_SOURCE_TO_MODULE(${name} swig_generated_source ${it})
  198. set(swig_generated_sources ${swig_generated_sources} "${swig_generated_source}")
  199. endforeach()
  200. get_directory_property(swig_extra_clean_files ADDITIONAL_MAKE_CLEAN_FILES)
  201. set_directory_properties(PROPERTIES
  202. ADDITIONAL_MAKE_CLEAN_FILES "${swig_extra_clean_files};${swig_generated_sources}")
  203. add_library(${SWIG_MODULE_${name}_REAL_NAME}
  204. MODULE
  205. ${swig_generated_sources}
  206. ${swig_other_sources})
  207. string(TOLOWER "${language}" swig_lowercase_language)
  208. if ("${swig_lowercase_language}" STREQUAL "octave")
  209. set_target_properties(${SWIG_MODULE_${name}_REAL_NAME} PROPERTIES PREFIX "")
  210. set_target_properties(${SWIG_MODULE_${name}_REAL_NAME} PROPERTIES SUFFIX ".oct")
  211. elseif ("${swig_lowercase_language}" STREQUAL "java")
  212. if (APPLE)
  213. # In java you want:
  214. # System.loadLibrary("LIBRARY");
  215. # then JNI will look for a library whose name is platform dependent, namely
  216. # MacOS : libLIBRARY.jnilib
  217. # Windows: LIBRARY.dll
  218. # Linux : libLIBRARY.so
  219. set_target_properties (${SWIG_MODULE_${name}_REAL_NAME} PROPERTIES SUFFIX ".jnilib")
  220. endif ()
  221. elseif ("${swig_lowercase_language}" STREQUAL "python")
  222. # this is only needed for the python case where a _modulename.so is generated
  223. set_target_properties(${SWIG_MODULE_${name}_REAL_NAME} PROPERTIES PREFIX "")
  224. # Python extension modules on Windows must have the extension ".pyd"
  225. # instead of ".dll" as of Python 2.5. Older python versions do support
  226. # this suffix.
  227. # http://docs.python.org/whatsnew/ports.html#SECTION0001510000000000000000
  228. # <quote>
  229. # Windows: .dll is no longer supported as a filename extension for extension modules.
  230. # .pyd is now the only filename extension that will be searched for.
  231. # </quote>
  232. if(WIN32 AND NOT CYGWIN)
  233. set_target_properties(${SWIG_MODULE_${name}_REAL_NAME} PROPERTIES SUFFIX ".pyd")
  234. endif()
  235. elseif ("${swig_lowercase_language}" STREQUAL "ruby")
  236. # In ruby you want:
  237. # require 'LIBRARY'
  238. # then ruby will look for a library whose name is platform dependent, namely
  239. # MacOS : LIBRARY.bundle
  240. # Windows: LIBRARY.dll
  241. # Linux : LIBRARY.so
  242. set_target_properties (${SWIG_MODULE_${name}_REAL_NAME} PROPERTIES PREFIX "")
  243. if (APPLE)
  244. set_target_properties (${SWIG_MODULE_${name}_REAL_NAME} PROPERTIES SUFFIX ".bundle")
  245. endif ()
  246. endif ()
  247. endmacro()
  248. #
  249. # Like TARGET_LINK_LIBRARIES but for swig modules
  250. #
  251. macro(SWIG_LINK_LIBRARIES name)
  252. if(SWIG_MODULE_${name}_REAL_NAME)
  253. target_link_libraries(${SWIG_MODULE_${name}_REAL_NAME} ${ARGN})
  254. else()
  255. message(SEND_ERROR "Cannot find Swig library \"${name}\".")
  256. endif()
  257. endmacro()