UseSWIG.cmake 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file Copyright.txt or https://cmake.org/licensing for details.
  3. #[=======================================================================[.rst:
  4. UseSWIG
  5. -------
  6. Defines the following macros for use with SWIG:
  7. .. command:: swig_add_library
  8. Define swig module with given name and specified language::
  9. swig_add_library(<name>
  10. [TYPE <SHARED|MODULE|STATIC|USE_BUILD_SHARED_LIBS>]
  11. LANGUAGE <language>
  12. SOURCES <file>...
  13. )
  14. The variable ``SWIG_MODULE_<name>_REAL_NAME`` will be set to the name
  15. of the swig module target library.
  16. .. command:: swig_link_libraries
  17. Link libraries to swig module::
  18. swig_link_libraries(<name> [ libraries ])
  19. Source file properties on module files can be set before the invocation
  20. of the ``swig_add_library`` macro to specify special behavior of SWIG:
  21. ``CPLUSPLUS``
  22. Call SWIG in c++ mode. For example:
  23. .. code-block:: cmake
  24. set_property(SOURCE mymod.i PROPERTY CPLUSPLUS ON)
  25. swig_add_library(mymod LANGUAGE python SOURCES mymod.i)
  26. ``SWIG_FLAGS``
  27. Add custom flags to the SWIG executable.
  28. ``SWIG_MODULE_NAME``
  29. Specify the actual import name of the module in the target language.
  30. This is required if it cannot be scanned automatically from source
  31. or different from the module file basename. For example:
  32. .. code-block:: cmake
  33. set_property(SOURCE mymod.i PROPERTY SWIG_MODULE_NAME mymod_realname)
  34. Some variables can be set to specify special behavior of SWIG:
  35. ``CMAKE_SWIG_FLAGS``
  36. Add flags to all swig calls.
  37. ``CMAKE_SWIG_OUTDIR``
  38. Specify where to write the language specific files (swig ``-outdir`` option).
  39. ``SWIG_OUTFILE_DIR``
  40. Specify an output directory name where the generated source file will be
  41. placed. If not specified, ``CMAKE_SWIG_OUTDIR`` is used.
  42. ``SWIG_MODULE_<name>_EXTRA_DEPS``
  43. Specify extra dependencies for the generated module for ``<name>``.
  44. #]=======================================================================]
  45. set(SWIG_CXX_EXTENSION "cxx")
  46. set(SWIG_EXTRA_LIBRARIES "")
  47. set(SWIG_PYTHON_EXTRA_FILE_EXTENSIONS ".py")
  48. set(SWIG_JAVA_EXTRA_FILE_EXTENSIONS ".java" "JNI.java")
  49. #
  50. # For given swig module initialize variables associated with it
  51. #
  52. macro(SWIG_MODULE_INITIALIZE name language)
  53. string(TOUPPER "${language}" swig_uppercase_language)
  54. string(TOLOWER "${language}" swig_lowercase_language)
  55. set(SWIG_MODULE_${name}_LANGUAGE "${swig_uppercase_language}")
  56. set(SWIG_MODULE_${name}_SWIG_LANGUAGE_FLAG "${swig_lowercase_language}")
  57. set(SWIG_MODULE_${name}_REAL_NAME "${name}")
  58. if (";${CMAKE_SWIG_FLAGS};" MATCHES ";-noproxy;")
  59. set (SWIG_MODULE_${name}_NOPROXY TRUE)
  60. endif ()
  61. if("x${SWIG_MODULE_${name}_LANGUAGE}" STREQUAL "xUNKNOWN")
  62. message(FATAL_ERROR "SWIG Error: Language \"${language}\" not found")
  63. elseif("x${SWIG_MODULE_${name}_LANGUAGE}" STREQUAL "xPYTHON" AND NOT SWIG_MODULE_${name}_NOPROXY)
  64. # swig will produce a module.py containing an 'import _modulename' statement,
  65. # which implies having a corresponding _modulename.so (*NIX), _modulename.pyd (Win32),
  66. # unless the -noproxy flag is used
  67. set(SWIG_MODULE_${name}_REAL_NAME "_${name}")
  68. elseif("x${SWIG_MODULE_${name}_LANGUAGE}" STREQUAL "xPERL")
  69. set(SWIG_MODULE_${name}_EXTRA_FLAGS "-shadow")
  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_EXTENSIONS})
  103. set(extra_file "${generatedpath}/${SWIG_GET_EXTRA_OUTPUT_FILES_module_basename}${it}")
  104. list(APPEND ${outfiles} ${extra_file})
  105. # Treat extra outputs as plain files regardless of language.
  106. set_property(SOURCE "${extra_file}" PROPERTY LANGUAGE "")
  107. endforeach()
  108. endmacro()
  109. #
  110. # Take swig (*.i) file and add proper custom commands for it
  111. #
  112. macro(SWIG_ADD_SOURCE_TO_MODULE name outfiles infile)
  113. set(swig_full_infile ${infile})
  114. get_filename_component(swig_source_file_name_we "${infile}" NAME_WE)
  115. get_source_file_property(swig_source_file_generated ${infile} GENERATED)
  116. get_source_file_property(swig_source_file_cplusplus ${infile} CPLUSPLUS)
  117. get_source_file_property(swig_source_file_flags ${infile} SWIG_FLAGS)
  118. if("${swig_source_file_flags}" STREQUAL "NOTFOUND")
  119. set(swig_source_file_flags "")
  120. endif()
  121. get_filename_component(swig_source_file_fullname "${infile}" ABSOLUTE)
  122. # If CMAKE_SWIG_OUTDIR was specified then pass it to -outdir
  123. if(CMAKE_SWIG_OUTDIR)
  124. set(swig_outdir ${CMAKE_SWIG_OUTDIR})
  125. else()
  126. set(swig_outdir ${CMAKE_CURRENT_BINARY_DIR})
  127. endif()
  128. if(SWIG_OUTFILE_DIR)
  129. set(swig_outfile_dir ${SWIG_OUTFILE_DIR})
  130. else()
  131. set(swig_outfile_dir ${swig_outdir})
  132. endif()
  133. if (NOT SWIG_MODULE_${name}_NOPROXY)
  134. SWIG_GET_EXTRA_OUTPUT_FILES(${SWIG_MODULE_${name}_LANGUAGE}
  135. swig_extra_generated_files
  136. "${swig_outdir}"
  137. "${swig_source_file_fullname}")
  138. endif()
  139. set(swig_generated_file_fullname
  140. "${swig_outfile_dir}/${swig_source_file_name_we}")
  141. # add the language into the name of the file (i.e. TCL_wrap)
  142. # this allows for the same .i file to be wrapped into different languages
  143. string(APPEND swig_generated_file_fullname
  144. "${SWIG_MODULE_${name}_LANGUAGE}_wrap")
  145. if(swig_source_file_cplusplus)
  146. string(APPEND swig_generated_file_fullname
  147. ".${SWIG_CXX_EXTENSION}")
  148. else()
  149. string(APPEND swig_generated_file_fullname
  150. ".c")
  151. endif()
  152. #message("Full path to source file: ${swig_source_file_fullname}")
  153. #message("Full path to the output file: ${swig_generated_file_fullname}")
  154. get_directory_property(cmake_include_directories INCLUDE_DIRECTORIES)
  155. list(REMOVE_DUPLICATES cmake_include_directories)
  156. set(swig_include_dirs)
  157. foreach(it ${cmake_include_directories})
  158. set(swig_include_dirs ${swig_include_dirs} "-I${it}")
  159. endforeach()
  160. set(swig_special_flags)
  161. # default is c, so add c++ flag if it is c++
  162. if(swig_source_file_cplusplus)
  163. set(swig_special_flags ${swig_special_flags} "-c++")
  164. endif()
  165. if("x${SWIG_MODULE_${name}_LANGUAGE}" STREQUAL "xCSHARP")
  166. if(NOT ";${swig_source_file_flags};${CMAKE_SWIG_FLAGS};" MATCHES ";-dllimport;")
  167. # This makes sure that the name used in the generated DllImport
  168. # matches the library name created by CMake
  169. set(SWIG_MODULE_${name}_EXTRA_FLAGS "-dllimport;${name}")
  170. endif()
  171. endif()
  172. set(swig_extra_flags)
  173. if(SWIG_MODULE_${name}_EXTRA_FLAGS)
  174. set(swig_extra_flags ${swig_extra_flags} ${SWIG_MODULE_${name}_EXTRA_FLAGS})
  175. endif()
  176. # IMPLICIT_DEPENDS below can not handle situations where a dependent file is
  177. # removed. We need an extra step with timestamp and custom target, see #16830
  178. # As this is needed only for Makefile generator do it conditionally
  179. if(CMAKE_GENERATOR MATCHES "Make")
  180. get_filename_component(swig_generated_timestamp
  181. "${swig_generated_file_fullname}" NAME_WE)
  182. set(swig_gen_target gen_${name}_${swig_generated_timestamp})
  183. set(swig_generated_timestamp
  184. "${swig_outdir}/${swig_generated_timestamp}.stamp")
  185. set(swig_custom_output ${swig_generated_timestamp})
  186. set(swig_custom_products
  187. BYPRODUCTS "${swig_generated_file_fullname}" ${swig_extra_generated_files})
  188. set(swig_timestamp_command
  189. COMMAND ${CMAKE_COMMAND} -E touch ${swig_generated_timestamp})
  190. else()
  191. set(swig_custom_output
  192. "${swig_generated_file_fullname}" ${swig_extra_generated_files})
  193. set(swig_custom_products)
  194. set(swig_timestamp_command)
  195. endif()
  196. add_custom_command(
  197. OUTPUT ${swig_custom_output}
  198. ${swig_custom_products}
  199. # Let's create the ${swig_outdir} at execution time, in case dir contains $(OutDir)
  200. COMMAND ${CMAKE_COMMAND} -E make_directory ${swig_outdir}
  201. ${swig_timestamp_command}
  202. COMMAND "${SWIG_EXECUTABLE}"
  203. ARGS "-${SWIG_MODULE_${name}_SWIG_LANGUAGE_FLAG}"
  204. ${swig_source_file_flags}
  205. ${CMAKE_SWIG_FLAGS}
  206. -outdir ${swig_outdir}
  207. ${swig_special_flags}
  208. ${swig_extra_flags}
  209. ${swig_include_dirs}
  210. -o "${swig_generated_file_fullname}"
  211. "${swig_source_file_fullname}"
  212. MAIN_DEPENDENCY "${swig_source_file_fullname}"
  213. DEPENDS ${SWIG_MODULE_${name}_EXTRA_DEPS}
  214. IMPLICIT_DEPENDS CXX "${swig_source_file_fullname}"
  215. COMMENT "Swig source")
  216. if(CMAKE_GENERATOR MATCHES "Make")
  217. add_custom_target(${swig_gen_target} DEPENDS ${swig_generated_timestamp})
  218. endif()
  219. unset(swig_generated_timestamp)
  220. unset(swig_custom_output)
  221. unset(swig_custom_products)
  222. unset(swig_timestamp_command)
  223. set_source_files_properties("${swig_generated_file_fullname}" ${swig_extra_generated_files}
  224. PROPERTIES GENERATED 1)
  225. set(${outfiles} "${swig_generated_file_fullname}" ${swig_extra_generated_files})
  226. endmacro()
  227. #
  228. # Create Swig module
  229. #
  230. macro(SWIG_ADD_MODULE name language)
  231. message(DEPRECATION "SWIG_ADD_MODULE is deprecated. Use SWIG_ADD_LIBRARY instead.")
  232. swig_add_library(${name}
  233. LANGUAGE ${language}
  234. TYPE MODULE
  235. SOURCES ${ARGN})
  236. endmacro()
  237. macro(SWIG_ADD_LIBRARY name)
  238. set(options "")
  239. set(oneValueArgs LANGUAGE
  240. TYPE)
  241. set(multiValueArgs SOURCES)
  242. cmake_parse_arguments(_SAM "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
  243. if(NOT DEFINED _SAM_LANGUAGE)
  244. message(FATAL_ERROR "SWIG_ADD_LIBRARY: Missing LANGUAGE argument")
  245. endif()
  246. if(NOT DEFINED _SAM_SOURCES)
  247. message(FATAL_ERROR "SWIG_ADD_LIBRARY: Missing SOURCES argument")
  248. endif()
  249. if(NOT DEFINED _SAM_TYPE)
  250. set(_SAM_TYPE MODULE)
  251. elseif("${_SAM_TYPE}" STREQUAL "USE_BUILD_SHARED_LIBS")
  252. unset(_SAM_TYPE)
  253. endif()
  254. swig_module_initialize(${name} ${_SAM_LANGUAGE})
  255. set(swig_dot_i_sources)
  256. set(swig_other_sources)
  257. foreach(it ${_SAM_SOURCES})
  258. if(${it} MATCHES "\\.i$")
  259. set(swig_dot_i_sources ${swig_dot_i_sources} "${it}")
  260. else()
  261. set(swig_other_sources ${swig_other_sources} "${it}")
  262. endif()
  263. endforeach()
  264. set(swig_generated_sources)
  265. set(swig_generated_targets)
  266. foreach(it ${swig_dot_i_sources})
  267. SWIG_ADD_SOURCE_TO_MODULE(${name} swig_generated_source ${it})
  268. set(swig_generated_sources ${swig_generated_sources} "${swig_generated_source}")
  269. list(APPEND swig_generated_targets "${swig_gen_target}")
  270. endforeach()
  271. get_directory_property(swig_extra_clean_files ADDITIONAL_MAKE_CLEAN_FILES)
  272. set_directory_properties(PROPERTIES
  273. ADDITIONAL_MAKE_CLEAN_FILES "${swig_extra_clean_files};${swig_generated_sources}")
  274. add_library(${SWIG_MODULE_${name}_REAL_NAME}
  275. ${_SAM_TYPE}
  276. ${swig_generated_sources}
  277. ${swig_other_sources})
  278. if(CMAKE_GENERATOR MATCHES "Make")
  279. # see IMPLICIT_DEPENDS above
  280. add_dependencies(${SWIG_MODULE_${name}_REAL_NAME} ${swig_generated_targets})
  281. endif()
  282. if("${_SAM_TYPE}" STREQUAL "MODULE")
  283. set_target_properties(${SWIG_MODULE_${name}_REAL_NAME} PROPERTIES NO_SONAME ON)
  284. endif()
  285. string(TOLOWER "${_SAM_LANGUAGE}" swig_lowercase_language)
  286. if ("${swig_lowercase_language}" STREQUAL "octave")
  287. set_target_properties(${SWIG_MODULE_${name}_REAL_NAME} PROPERTIES PREFIX "")
  288. set_target_properties(${SWIG_MODULE_${name}_REAL_NAME} PROPERTIES SUFFIX ".oct")
  289. elseif ("${swig_lowercase_language}" STREQUAL "go")
  290. set_target_properties(${SWIG_MODULE_${name}_REAL_NAME} PROPERTIES PREFIX "")
  291. elseif ("${swig_lowercase_language}" STREQUAL "java")
  292. if (APPLE)
  293. # In java you want:
  294. # System.loadLibrary("LIBRARY");
  295. # then JNI will look for a library whose name is platform dependent, namely
  296. # MacOS : libLIBRARY.jnilib
  297. # Windows: LIBRARY.dll
  298. # Linux : libLIBRARY.so
  299. set_target_properties (${SWIG_MODULE_${name}_REAL_NAME} PROPERTIES SUFFIX ".jnilib")
  300. endif ()
  301. elseif ("${swig_lowercase_language}" STREQUAL "lua")
  302. if("${_SAM_TYPE}" STREQUAL "MODULE")
  303. set_target_properties(${SWIG_MODULE_${name}_REAL_NAME} PROPERTIES PREFIX "")
  304. endif()
  305. elseif ("${swig_lowercase_language}" STREQUAL "python")
  306. # this is only needed for the python case where a _modulename.so is generated
  307. set_target_properties(${SWIG_MODULE_${name}_REAL_NAME} PROPERTIES PREFIX "")
  308. # Python extension modules on Windows must have the extension ".pyd"
  309. # instead of ".dll" as of Python 2.5. Older python versions do support
  310. # this suffix.
  311. # http://docs.python.org/whatsnew/ports.html#SECTION0001510000000000000000
  312. # <quote>
  313. # Windows: .dll is no longer supported as a filename extension for extension modules.
  314. # .pyd is now the only filename extension that will be searched for.
  315. # </quote>
  316. if(WIN32 AND NOT CYGWIN)
  317. set_target_properties(${SWIG_MODULE_${name}_REAL_NAME} PROPERTIES SUFFIX ".pyd")
  318. endif()
  319. elseif ("${swig_lowercase_language}" STREQUAL "r")
  320. set_target_properties(${SWIG_MODULE_${name}_REAL_NAME} PROPERTIES PREFIX "")
  321. elseif ("${swig_lowercase_language}" STREQUAL "ruby")
  322. # In ruby you want:
  323. # require 'LIBRARY'
  324. # then ruby will look for a library whose name is platform dependent, namely
  325. # MacOS : LIBRARY.bundle
  326. # Windows: LIBRARY.dll
  327. # Linux : LIBRARY.so
  328. set_target_properties (${SWIG_MODULE_${name}_REAL_NAME} PROPERTIES PREFIX "")
  329. if (APPLE)
  330. set_target_properties (${SWIG_MODULE_${name}_REAL_NAME} PROPERTIES SUFFIX ".bundle")
  331. endif ()
  332. else()
  333. # assume empty prefix because we expect the module to be dynamically loaded
  334. set_target_properties (${SWIG_MODULE_${name}_REAL_NAME} PROPERTIES PREFIX "")
  335. endif ()
  336. endmacro()
  337. #
  338. # Like TARGET_LINK_LIBRARIES but for swig modules
  339. #
  340. macro(SWIG_LINK_LIBRARIES name)
  341. if(SWIG_MODULE_${name}_REAL_NAME)
  342. target_link_libraries(${SWIG_MODULE_${name}_REAL_NAME} ${ARGN})
  343. else()
  344. message(SEND_ERROR "Cannot find Swig library \"${name}\".")
  345. endif()
  346. endmacro()