1
0

UseSWIG.cmake 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. foreach(it ${SWIG_${language}_EXTRA_FILE_EXTENSION})
  81. set(${outfiles} ${${outfiles}}
  82. "${generatedpath}/${SWIG_GET_EXTRA_OUTPUT_FILES_module_basename}.${it}")
  83. endforeach()
  84. endmacro()
  85. #
  86. # Take swig (*.i) file and add proper custom commands for it
  87. #
  88. macro(SWIG_ADD_SOURCE_TO_MODULE name outfiles infile)
  89. set(swig_full_infile ${infile})
  90. get_filename_component(swig_source_file_name_we "${infile}" NAME_WE)
  91. get_source_file_property(swig_source_file_generated ${infile} GENERATED)
  92. get_source_file_property(swig_source_file_cplusplus ${infile} CPLUSPLUS)
  93. get_source_file_property(swig_source_file_flags ${infile} SWIG_FLAGS)
  94. get_source_file_property(_SWIG_MODULE_NAME ${infile} SWIG_MODULE_NAME)
  95. if ( NOT _SWIG_MODULE_NAME )
  96. set_source_files_properties(${infile} PROPERTIES SWIG_MODULE_NAME ${name})
  97. endif ()
  98. if("${swig_source_file_flags}" STREQUAL "NOTFOUND")
  99. set(swig_source_file_flags "")
  100. endif()
  101. get_filename_component(swig_source_file_fullname "${infile}" ABSOLUTE)
  102. # If CMAKE_SWIG_OUTDIR was specified then pass it to -outdir
  103. if(CMAKE_SWIG_OUTDIR)
  104. set(swig_outdir ${CMAKE_SWIG_OUTDIR})
  105. else()
  106. set(swig_outdir ${CMAKE_CURRENT_BINARY_DIR})
  107. endif()
  108. SWIG_GET_EXTRA_OUTPUT_FILES(${SWIG_MODULE_${name}_LANGUAGE}
  109. swig_extra_generated_files
  110. "${swig_outdir}"
  111. "${infile}")
  112. set(swig_generated_file_fullname
  113. "${swig_outdir}/${swig_source_file_name_we}")
  114. # add the language into the name of the file (i.e. TCL_wrap)
  115. # this allows for the same .i file to be wrapped into different languages
  116. set(swig_generated_file_fullname
  117. "${swig_generated_file_fullname}${SWIG_MODULE_${name}_LANGUAGE}_wrap")
  118. if(swig_source_file_cplusplus)
  119. set(swig_generated_file_fullname
  120. "${swig_generated_file_fullname}.${SWIG_CXX_EXTENSION}")
  121. else()
  122. set(swig_generated_file_fullname
  123. "${swig_generated_file_fullname}.c")
  124. endif()
  125. #message("Full path to source file: ${swig_source_file_fullname}")
  126. #message("Full path to the output file: ${swig_generated_file_fullname}")
  127. get_directory_property(cmake_include_directories INCLUDE_DIRECTORIES)
  128. list(REMOVE_DUPLICATES cmake_include_directories)
  129. set(swig_include_dirs)
  130. foreach(it ${cmake_include_directories})
  131. set(swig_include_dirs ${swig_include_dirs} "-I${it}")
  132. endforeach()
  133. set(swig_special_flags)
  134. # default is c, so add c++ flag if it is c++
  135. if(swig_source_file_cplusplus)
  136. set(swig_special_flags ${swig_special_flags} "-c++")
  137. endif()
  138. set(swig_extra_flags)
  139. if(SWIG_MODULE_${name}_EXTRA_FLAGS)
  140. set(swig_extra_flags ${swig_extra_flags} ${SWIG_MODULE_${name}_EXTRA_FLAGS})
  141. endif()
  142. add_custom_command(
  143. OUTPUT "${swig_generated_file_fullname}" ${swig_extra_generated_files}
  144. # Let's create the ${swig_outdir} at execution time, in case dir contains $(OutDir)
  145. COMMAND ${CMAKE_COMMAND} -E make_directory ${swig_outdir}
  146. COMMAND "${SWIG_EXECUTABLE}"
  147. ARGS "-${SWIG_MODULE_${name}_SWIG_LANGUAGE_FLAG}"
  148. ${swig_source_file_flags}
  149. ${CMAKE_SWIG_FLAGS}
  150. -outdir ${swig_outdir}
  151. ${swig_special_flags}
  152. ${swig_extra_flags}
  153. ${swig_include_dirs}
  154. -o "${swig_generated_file_fullname}"
  155. "${swig_source_file_fullname}"
  156. MAIN_DEPENDENCY "${swig_source_file_fullname}"
  157. DEPENDS ${SWIG_MODULE_${name}_EXTRA_DEPS}
  158. COMMENT "Swig source")
  159. set_source_files_properties("${swig_generated_file_fullname}" ${swig_extra_generated_files}
  160. PROPERTIES GENERATED 1)
  161. set(${outfiles} "${swig_generated_file_fullname}" ${swig_extra_generated_files})
  162. endmacro()
  163. #
  164. # Create Swig module
  165. #
  166. macro(SWIG_ADD_MODULE name language)
  167. SWIG_MODULE_INITIALIZE(${name} ${language})
  168. set(swig_dot_i_sources)
  169. set(swig_other_sources)
  170. foreach(it ${ARGN})
  171. if(${it} MATCHES ".*\\.i$")
  172. set(swig_dot_i_sources ${swig_dot_i_sources} "${it}")
  173. else()
  174. set(swig_other_sources ${swig_other_sources} "${it}")
  175. endif()
  176. endforeach()
  177. set(swig_generated_sources)
  178. foreach(it ${swig_dot_i_sources})
  179. SWIG_ADD_SOURCE_TO_MODULE(${name} swig_generated_source ${it})
  180. set(swig_generated_sources ${swig_generated_sources} "${swig_generated_source}")
  181. endforeach()
  182. get_directory_property(swig_extra_clean_files ADDITIONAL_MAKE_CLEAN_FILES)
  183. set_directory_properties(PROPERTIES
  184. ADDITIONAL_MAKE_CLEAN_FILES "${swig_extra_clean_files};${swig_generated_sources}")
  185. add_library(${SWIG_MODULE_${name}_REAL_NAME}
  186. MODULE
  187. ${swig_generated_sources}
  188. ${swig_other_sources})
  189. string(TOLOWER "${language}" swig_lowercase_language)
  190. if ("${swig_lowercase_language}" STREQUAL "octave")
  191. set_target_properties(${SWIG_MODULE_${name}_REAL_NAME} PROPERTIES PREFIX "")
  192. set_target_properties(${SWIG_MODULE_${name}_REAL_NAME} PROPERTIES SUFFIX ".oct")
  193. elseif ("${swig_lowercase_language}" STREQUAL "java")
  194. if (APPLE)
  195. # In java you want:
  196. # System.loadLibrary("LIBRARY");
  197. # then JNI will look for a library whose name is platform dependent, namely
  198. # MacOS : libLIBRARY.jnilib
  199. # Windows: LIBRARY.dll
  200. # Linux : libLIBRARY.so
  201. set_target_properties (${SWIG_MODULE_${name}_REAL_NAME} PROPERTIES SUFFIX ".jnilib")
  202. endif ()
  203. elseif ("${swig_lowercase_language}" STREQUAL "python")
  204. # this is only needed for the python case where a _modulename.so is generated
  205. set_target_properties(${SWIG_MODULE_${name}_REAL_NAME} PROPERTIES PREFIX "")
  206. # Python extension modules on Windows must have the extension ".pyd"
  207. # instead of ".dll" as of Python 2.5. Older python versions do support
  208. # this suffix.
  209. # http://docs.python.org/whatsnew/ports.html#SECTION0001510000000000000000
  210. # <quote>
  211. # Windows: .dll is no longer supported as a filename extension for extension modules.
  212. # .pyd is now the only filename extension that will be searched for.
  213. # </quote>
  214. if(WIN32 AND NOT CYGWIN)
  215. set_target_properties(${SWIG_MODULE_${name}_REAL_NAME} PROPERTIES SUFFIX ".pyd")
  216. endif()
  217. elseif ("${swig_lowercase_language}" STREQUAL "ruby")
  218. # In ruby you want:
  219. # require 'LIBRARY'
  220. # then ruby will look for a library whose name is platform dependent, namely
  221. # MacOS : LIBRARY.bundle
  222. # Windows: LIBRARY.dll
  223. # Linux : LIBRARY.so
  224. set_target_properties (${SWIG_MODULE_${name}_REAL_NAME} PROPERTIES PREFIX "")
  225. if (APPLE)
  226. set_target_properties (${SWIG_MODULE_${name}_REAL_NAME} PROPERTIES SUFFIX ".bundle")
  227. endif ()
  228. endif ()
  229. endmacro()
  230. #
  231. # Like TARGET_LINK_LIBRARIES but for swig modules
  232. #
  233. macro(SWIG_LINK_LIBRARIES name)
  234. if(SWIG_MODULE_${name}_REAL_NAME)
  235. target_link_libraries(${SWIG_MODULE_${name}_REAL_NAME} ${ARGN})
  236. else()
  237. message(SEND_ERROR "Cannot find Swig library \"${name}\".")
  238. endif()
  239. endmacro()