UseSWIG.cmake 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. # set_source_files_properties( ${swig_generated_file_fullname}
  27. #
  28. # ::
  29. #
  30. # PROPERTIES COMPILE_FLAGS "-bla")
  31. #=============================================================================
  32. # Copyright 2004-2009 Kitware, Inc.
  33. # Copyright 2009 Mathieu Malaterre <[email protected]>
  34. #
  35. # Distributed under the OSI-approved BSD License (the "License");
  36. # see accompanying file Copyright.txt for details.
  37. #
  38. # This software is distributed WITHOUT ANY WARRANTY; without even the
  39. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  40. # See the License for more information.
  41. #=============================================================================
  42. # (To distribute this file outside of CMake, substitute the full
  43. # License text for the above reference.)
  44. set(SWIG_CXX_EXTENSION "cxx")
  45. set(SWIG_EXTRA_LIBRARIES "")
  46. set(SWIG_PYTHON_EXTRA_FILE_EXTENSION "py")
  47. #
  48. # For given swig module initialize variables associated with it
  49. #
  50. macro(SWIG_MODULE_INITIALIZE name language)
  51. string(TOUPPER "${language}" swig_uppercase_language)
  52. string(TOLOWER "${language}" swig_lowercase_language)
  53. set(SWIG_MODULE_${name}_LANGUAGE "${swig_uppercase_language}")
  54. set(SWIG_MODULE_${name}_SWIG_LANGUAGE_FLAG "${swig_lowercase_language}")
  55. set(SWIG_MODULE_${name}_REAL_NAME "${name}")
  56. if("${SWIG_MODULE_${name}_LANGUAGE}" STREQUAL "UNKNOWN")
  57. message(FATAL_ERROR "SWIG Error: Language \"${language}\" not found")
  58. elseif("${SWIG_MODULE_${name}_LANGUAGE}" STREQUAL "PYTHON")
  59. # when swig is used without the -interface it will produce in the module.py
  60. # a 'import _modulename' statement, which implies having a corresponding
  61. # _modulename.so (*NIX), _modulename.pyd (Win32).
  62. set(SWIG_MODULE_${name}_REAL_NAME "_${name}")
  63. elseif("${SWIG_MODULE_${name}_LANGUAGE}" STREQUAL "PERL")
  64. set(SWIG_MODULE_${name}_EXTRA_FLAGS "-shadow")
  65. elseif("${SWIG_MODULE_${name}_LANGUAGE}" STREQUAL "CSHARP")
  66. # This makes sure that the name used in the generated DllImport
  67. # matches the library name created by CMake
  68. set(SWIG_MODULE_${name}_EXTRA_FLAGS "-dllimport;${name}")
  69. endif()
  70. endmacro()
  71. #
  72. # For a given language, input file, and output file, determine extra files that
  73. # will be generated. This is internal swig macro.
  74. #
  75. macro(SWIG_GET_EXTRA_OUTPUT_FILES language outfiles generatedpath infile)
  76. set(${outfiles} "")
  77. get_source_file_property(SWIG_GET_EXTRA_OUTPUT_FILES_module_basename
  78. ${infile} SWIG_MODULE_NAME)
  79. if(SWIG_GET_EXTRA_OUTPUT_FILES_module_basename STREQUAL "NOTFOUND")
  80. get_filename_component(SWIG_GET_EXTRA_OUTPUT_FILES_module_basename "${infile}" NAME_WE)
  81. endif()
  82. foreach(it ${SWIG_${language}_EXTRA_FILE_EXTENSION})
  83. set(${outfiles} ${${outfiles}}
  84. "${generatedpath}/${SWIG_GET_EXTRA_OUTPUT_FILES_module_basename}.${it}")
  85. endforeach()
  86. endmacro()
  87. #
  88. # Take swig (*.i) file and add proper custom commands for it
  89. #
  90. macro(SWIG_ADD_SOURCE_TO_MODULE name outfiles infile)
  91. set(swig_full_infile ${infile})
  92. get_filename_component(swig_source_file_name_we "${infile}" NAME_WE)
  93. get_source_file_property(swig_source_file_generated ${infile} GENERATED)
  94. get_source_file_property(swig_source_file_cplusplus ${infile} CPLUSPLUS)
  95. get_source_file_property(swig_source_file_flags ${infile} SWIG_FLAGS)
  96. if("${swig_source_file_flags}" STREQUAL "NOTFOUND")
  97. set(swig_source_file_flags "")
  98. endif()
  99. get_filename_component(swig_source_file_fullname "${infile}" ABSOLUTE)
  100. # If CMAKE_SWIG_OUTDIR was specified then pass it to -outdir
  101. if(CMAKE_SWIG_OUTDIR)
  102. set(swig_outdir ${CMAKE_SWIG_OUTDIR})
  103. else()
  104. set(swig_outdir ${CMAKE_CURRENT_BINARY_DIR})
  105. endif()
  106. SWIG_GET_EXTRA_OUTPUT_FILES(${SWIG_MODULE_${name}_LANGUAGE}
  107. swig_extra_generated_files
  108. "${swig_outdir}"
  109. "${infile}")
  110. set(swig_generated_file_fullname
  111. "${swig_outdir}/${swig_source_file_name_we}")
  112. # add the language into the name of the file (i.e. TCL_wrap)
  113. # this allows for the same .i file to be wrapped into different languages
  114. set(swig_generated_file_fullname
  115. "${swig_generated_file_fullname}${SWIG_MODULE_${name}_LANGUAGE}_wrap")
  116. if(swig_source_file_cplusplus)
  117. set(swig_generated_file_fullname
  118. "${swig_generated_file_fullname}.${SWIG_CXX_EXTENSION}")
  119. else()
  120. set(swig_generated_file_fullname
  121. "${swig_generated_file_fullname}.c")
  122. endif()
  123. #message("Full path to source file: ${swig_source_file_fullname}")
  124. #message("Full path to the output file: ${swig_generated_file_fullname}")
  125. get_directory_property(cmake_include_directories INCLUDE_DIRECTORIES)
  126. list(REMOVE_DUPLICATES cmake_include_directories)
  127. set(swig_include_dirs)
  128. foreach(it ${cmake_include_directories})
  129. set(swig_include_dirs ${swig_include_dirs} "-I${it}")
  130. endforeach()
  131. set(swig_special_flags)
  132. # default is c, so add c++ flag if it is c++
  133. if(swig_source_file_cplusplus)
  134. set(swig_special_flags ${swig_special_flags} "-c++")
  135. endif()
  136. set(swig_extra_flags)
  137. if(SWIG_MODULE_${name}_EXTRA_FLAGS)
  138. set(swig_extra_flags ${swig_extra_flags} ${SWIG_MODULE_${name}_EXTRA_FLAGS})
  139. endif()
  140. add_custom_command(
  141. OUTPUT "${swig_generated_file_fullname}" ${swig_extra_generated_files}
  142. # Let's create the ${swig_outdir} at execution time, in case dir contains $(OutDir)
  143. COMMAND ${CMAKE_COMMAND} -E make_directory ${swig_outdir}
  144. COMMAND "${SWIG_EXECUTABLE}"
  145. ARGS "-${SWIG_MODULE_${name}_SWIG_LANGUAGE_FLAG}"
  146. ${swig_source_file_flags}
  147. ${CMAKE_SWIG_FLAGS}
  148. -outdir ${swig_outdir}
  149. ${swig_special_flags}
  150. ${swig_extra_flags}
  151. ${swig_include_dirs}
  152. -o "${swig_generated_file_fullname}"
  153. "${swig_source_file_fullname}"
  154. MAIN_DEPENDENCY "${swig_source_file_fullname}"
  155. DEPENDS ${SWIG_MODULE_${name}_EXTRA_DEPS}
  156. COMMENT "Swig source")
  157. set_source_files_properties("${swig_generated_file_fullname}" ${swig_extra_generated_files}
  158. PROPERTIES GENERATED 1)
  159. set(${outfiles} "${swig_generated_file_fullname}" ${swig_extra_generated_files})
  160. endmacro()
  161. #
  162. # Create Swig module
  163. #
  164. macro(SWIG_ADD_MODULE name language)
  165. SWIG_MODULE_INITIALIZE(${name} ${language})
  166. set(swig_dot_i_sources)
  167. set(swig_other_sources)
  168. foreach(it ${ARGN})
  169. if(${it} MATCHES ".*\\.i$")
  170. set(swig_dot_i_sources ${swig_dot_i_sources} "${it}")
  171. else()
  172. set(swig_other_sources ${swig_other_sources} "${it}")
  173. endif()
  174. endforeach()
  175. set(swig_generated_sources)
  176. foreach(it ${swig_dot_i_sources})
  177. SWIG_ADD_SOURCE_TO_MODULE(${name} swig_generated_source ${it})
  178. set(swig_generated_sources ${swig_generated_sources} "${swig_generated_source}")
  179. endforeach()
  180. get_directory_property(swig_extra_clean_files ADDITIONAL_MAKE_CLEAN_FILES)
  181. set_directory_properties(PROPERTIES
  182. ADDITIONAL_MAKE_CLEAN_FILES "${swig_extra_clean_files};${swig_generated_sources}")
  183. add_library(${SWIG_MODULE_${name}_REAL_NAME}
  184. MODULE
  185. ${swig_generated_sources}
  186. ${swig_other_sources})
  187. string(TOLOWER "${language}" swig_lowercase_language)
  188. if ("${swig_lowercase_language}" STREQUAL "octave")
  189. set_target_properties(${SWIG_MODULE_${name}_REAL_NAME} PROPERTIES PREFIX "")
  190. set_target_properties(${SWIG_MODULE_${name}_REAL_NAME} PROPERTIES SUFFIX ".oct")
  191. elseif ("${swig_lowercase_language}" STREQUAL "java")
  192. if (APPLE)
  193. # In java you want:
  194. # System.loadLibrary("LIBRARY");
  195. # then JNI will look for a library whose name is platform dependent, namely
  196. # MacOS : libLIBRARY.jnilib
  197. # Windows: LIBRARY.dll
  198. # Linux : libLIBRARY.so
  199. set_target_properties (${SWIG_MODULE_${name}_REAL_NAME} PROPERTIES SUFFIX ".jnilib")
  200. endif ()
  201. endif ()
  202. if ("${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. endif ()
  217. endmacro()
  218. #
  219. # Like TARGET_LINK_LIBRARIES but for swig modules
  220. #
  221. macro(SWIG_LINK_LIBRARIES name)
  222. if(SWIG_MODULE_${name}_REAL_NAME)
  223. target_link_libraries(${SWIG_MODULE_${name}_REAL_NAME} ${ARGN})
  224. else()
  225. message(SEND_ERROR "Cannot find Swig library \"${name}\".")
  226. endif()
  227. endmacro()