UseSWIG.cmake 10 KB

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