UseSWIG.cmake 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  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. This file provides support for ``SWIG``. It is assumed that :module:`FindSWIG`
  7. module has already been loaded.
  8. Defines the following command for use with ``SWIG``:
  9. .. command:: swig_add_library
  10. Define swig module with given name and specified language::
  11. swig_add_library(<name>
  12. [TYPE <SHARED|MODULE|STATIC|USE_BUILD_SHARED_LIBS>]
  13. LANGUAGE <language>
  14. [NO_PROXY]
  15. [OUTPUT_DIR <directory>]
  16. [OUTFILE_DIR <directory>]
  17. SOURCES <file>...
  18. )
  19. Targets created with the ``swig_add_library`` command have the same
  20. capabilities as targets created with the :command:`add_library` command, so
  21. those targets can be used with any command expecting a target (e.g.
  22. :command:`target_link_libraries`).
  23. .. note::
  24. For multi-config generators, this module does not support
  25. configuration-specific files generated by ``SWIG``. All build
  26. configurations must result in the same generated source file.
  27. ``TYPE``
  28. ``SHARED``, ``MODULE`` and ``STATIC`` have the same semantic as for the
  29. :command:`add_library` command. If ``USE_BUILD_SHARED_LIBS`` is specified,
  30. the library type will be ``STATIC`` or ``SHARED`` based on whether the
  31. current value of the :variable:`BUILD_SHARED_LIBS` variable is ``ON``. If
  32. no type is specified, ``MODULE`` will be used.
  33. ``LANGUAGE``
  34. Specify the target language.
  35. ``NO_PROXY``
  36. Prevent the generation of the wrapper layer (swig ``-noproxy`` option).
  37. ``OUTPUT_DIR``
  38. Specify where to write the language specific files (swig ``-outdir``
  39. option). If not given, the ``CMAKE_SWIG_OUTDIR`` variable will be used.
  40. If neither is specified, the default depends on the value of the
  41. ``UseSWIG_MODULE_VERSION`` variable as follows:
  42. * If ``UseSWIG_MODULE_VERSION`` is 1 or is undefined, output is written to
  43. the :variable:`CMAKE_CURRENT_BINARY_DIR` directory.
  44. * If ``UseSWIG_MODULE_VERSION`` is 2, a dedicated directory will be used.
  45. The path of this directory can be retrieved from the
  46. ``SWIG_SUPPORT_FILES_DIRECTORY`` target property.
  47. ``OUTFILE_DIR``
  48. Specify an output directory name where the generated source file will be
  49. placed (swig -o option). If not specified, the ``SWIG_OUTFILE_DIR`` variable
  50. will be used. If neither is specified, ``OUTPUT_DIR`` or
  51. ``CMAKE_SWIG_OUTDIR`` is used instead.
  52. ``SOURCES``
  53. List of sources for the library. Files with extension ``.i`` will be
  54. identified as sources for the ``SWIG`` tool. Other files will be handled in
  55. the standard way.
  56. .. note::
  57. If ``UseSWIG_MODULE_VERSION`` is set to 2, it is **strongly** recommended
  58. to use a dedicated directory unique to the target when either the
  59. ``OUTPUT_DIR`` option or the ``CMAKE_SWIG_OUTDIR`` variable are specified.
  60. The output directory contents are erased as part of the target build, so
  61. to prevent interference between targets or losing other important files, each
  62. target should have its own dedicated output directory.
  63. Source file properties on module files **must** be set before the invocation
  64. of the ``swig_add_library`` command to specify special behavior of SWIG and
  65. ensure generated files will receive the required settings.
  66. ``CPLUSPLUS``
  67. Call SWIG in c++ mode. For example:
  68. .. code-block:: cmake
  69. set_property(SOURCE mymod.i PROPERTY CPLUSPLUS ON)
  70. swig_add_library(mymod LANGUAGE python SOURCES mymod.i)
  71. ``INCLUDE_DIRECTORIES``, ``COMPILE_DEFINITIONS`` and ``COMPILE_OPTIONS``
  72. Add custom flags to SWIG compiler and have same semantic as properties
  73. :prop_sf:`INCLUDE_DIRECTORIES`, :prop_sf:`COMPILE_DEFINITIONS` and
  74. :prop_sf:`COMPILE_OPTIONS`.
  75. ``GENERATED_INCLUDE_DIRECTORIES``, ``GENERATED_COMPILE_DEFINITIONS`` and ``GENERATED_COMPILE_OPTIONS``
  76. Add custom flags to the C/C++ generated source. They will fill, respectively,
  77. properties :prop_sf:`INCLUDE_DIRECTORIES`, :prop_sf:`COMPILE_DEFINITIONS` and
  78. :prop_sf:`COMPILE_OPTIONS` of generated C/C++ file.
  79. ``DEPENDS``
  80. Specify additional dependencies to the source file.
  81. ``SWIG_MODULE_NAME``
  82. Specify the actual import name of the module in the target language.
  83. This is required if it cannot be scanned automatically from source
  84. or different from the module file basename. For example:
  85. .. code-block:: cmake
  86. set_property(SOURCE mymod.i PROPERTY SWIG_MODULE_NAME mymod_realname)
  87. Target library properties can be set to apply same configuration to all SWIG
  88. input files.
  89. ``SWIG_INCLUDE_DIRECTORIES``, ``SWIG_COMPILE_DEFINITIONS`` and ``SWIG_COMPILE_OPTIONS``
  90. These properties will be applied to all SWIG input files and have same
  91. semantic as target properties :prop_tgt:`INCLUDE_DIRECTORIES`,
  92. :prop_tgt:`COMPILE_DEFINITIONS` and :prop_tgt:`COMPILE_OPTIONS`.
  93. .. code-block:: cmake
  94. swig_add_library(mymod LANGUAGE python SOURCES mymod.i)
  95. set_property(TARGET mymod PROPERTY SWIG_COMPILE_DEFINITIONS MY_DEF1 MY_DEF2)
  96. set_property(TARGET mymod PROPERTY SWIG_COMPILE_OPTIONS -bla -blb)
  97. ``SWIG_GENERATED_INCLUDE_DIRECTORIES``, ``SWIG_GENERATED_COMPILE_DEFINITIONS`` and ``SWIG_GENERATED_COMPILE_OPTIONS``
  98. These properties will populate, respectively, properties
  99. :prop_sf:`INCLUDE_DIRECTORIES`, :prop_sf:`COMPILE_DEFINITIONS` and
  100. :prop_sf:`COMPILE_FLAGS` of all generated C/C++ files.
  101. ``SWIG_DEPENDS``
  102. Add dependencies to all SWIG input files.
  103. The following target properties are output properties and can be used to get
  104. information about support files generated by ``SWIG`` interface compilation.
  105. ``SWIG_SUPPORT_FILES``
  106. This output property list of wrapper files generated during SWIG compilation.
  107. .. code-block:: cmake
  108. swig_add_library(mymod LANGUAGE python SOURCES mymod.i)
  109. get_property(support_files TARGET mymod PROPERTY SWIG_SUPPORT_FILES)
  110. .. note::
  111. Only most principal support files are listed. In case some advanced
  112. features of ``SWIG`` are used (for example ``%template``), associated
  113. support files may not be listed. Prefer to use the
  114. ``SWIG_SUPPORT_FILES_DIRECTORY`` property to handle support files.
  115. ``SWIG_SUPPORT_FILES_DIRECTORY``
  116. This output property specifies the directory where support files will be
  117. generated.
  118. Some variables can be set to customize the behavior of ``swig_add_library``
  119. as well as ``SWIG``:
  120. ``UseSWIG_MODULE_VERSION``
  121. Specify different behaviors for ``UseSWIG`` module.
  122. * Set to 1 or undefined: Legacy behavior is applied.
  123. * Set to 2: A new strategy is applied regarding support files: the output
  124. directory of support files is erased before ``SWIG`` interface compilation.
  125. ``CMAKE_SWIG_FLAGS``
  126. Add flags to all swig calls.
  127. ``CMAKE_SWIG_OUTDIR``
  128. Specify where to write the language specific files (swig ``-outdir`` option).
  129. ``SWIG_OUTFILE_DIR``
  130. Specify an output directory name where the generated source file will be
  131. placed. If not specified, ``CMAKE_SWIG_OUTDIR`` is used.
  132. ``SWIG_MODULE_<name>_EXTRA_DEPS``
  133. Specify extra dependencies for the generated module for ``<name>``.
  134. #]=======================================================================]
  135. cmake_policy (VERSION 3.12)
  136. set(SWIG_CXX_EXTENSION "cxx")
  137. set(SWIG_EXTRA_LIBRARIES "")
  138. set(SWIG_PYTHON_EXTRA_FILE_EXTENSIONS ".py")
  139. set(SWIG_JAVA_EXTRA_FILE_EXTENSIONS ".java" "JNI.java")
  140. ##
  141. ## PRIVATE functions
  142. ##
  143. function (__SWIG_COMPUTE_TIMESTAMP name language infile workingdir __timestamp)
  144. get_filename_component(filename "${infile}" NAME_WE)
  145. set(${__timestamp}
  146. "${workingdir}/${filename}${language}.stamp" PARENT_SCOPE)
  147. # get_filename_component(filename "${infile}" ABSOLUTE)
  148. # string(UUID uuid NAMESPACE 9735D882-D2F8-4E1D-88C9-A0A4F1F6ECA4
  149. # NAME ${name}-${language}-${filename} TYPE SHA1)
  150. # set(${__timestamp} "${workingdir}/${uuid}.stamp" PARENT_SCOPE)
  151. endfunction()
  152. #
  153. # For given swig module initialize variables associated with it
  154. #
  155. macro(SWIG_MODULE_INITIALIZE name language)
  156. string(TOUPPER "${language}" SWIG_MODULE_${name}_LANGUAGE)
  157. string(TOLOWER "${language}" SWIG_MODULE_${name}_SWIG_LANGUAGE_FLAG)
  158. set(SWIG_MODULE_${name}_EXTRA_FLAGS)
  159. if (NOT DEFINED SWIG_MODULE_${name}_NOPROXY)
  160. set (SWIG_MODULE_${name}_NOPROXY FALSE)
  161. endif()
  162. if ("-noproxy" IN_LIST CMAKE_SWIG_FLAGS)
  163. set (SWIG_MODULE_${name}_NOPROXY TRUE)
  164. endif ()
  165. if (SWIG_MODULE_${name}_NOPROXY AND NOT "-noproxy" IN_LIST CMAKE_SWIG_FLAGS)
  166. list (APPEND SWIG_MODULE_${name}_EXTRA_FLAGS "-noproxy")
  167. endif()
  168. if(SWIG_MODULE_${name}_LANGUAGE STREQUAL "UNKNOWN")
  169. message(FATAL_ERROR "SWIG Error: Language \"${language}\" not found")
  170. elseif(SWIG_MODULE_${name}_LANGUAGE STREQUAL "PERL")
  171. list(APPEND SWIG_MODULE_${name}_EXTRA_FLAGS "-shadow")
  172. endif()
  173. endmacro()
  174. #
  175. # For a given language, input file, and output file, determine extra files that
  176. # will be generated. This is internal swig macro.
  177. #
  178. function(SWIG_GET_EXTRA_OUTPUT_FILES language outfiles generatedpath infile)
  179. set(files)
  180. get_source_file_property(module_basename
  181. "${infile}" SWIG_MODULE_NAME)
  182. if(NOT module_basename)
  183. # try to get module name from "%module foo" syntax
  184. if ( EXISTS "${infile}" )
  185. file ( STRINGS "${infile}" module_basename REGEX "[ ]*%module[ ]*[a-zA-Z0-9_]+.*" )
  186. endif ()
  187. if ( module_basename )
  188. string ( REGEX REPLACE "[ ]*%module[ ]*([a-zA-Z0-9_]+).*" "\\1" module_basename "${module_basename}" )
  189. else ()
  190. # try to get module name from "%module (options=...) foo" syntax
  191. if ( EXISTS "${infile}" )
  192. file ( STRINGS "${infile}" module_basename REGEX "[ ]*%module[ ]*\\(.*\\)[ ]*[a-zA-Z0-9_]+.*" )
  193. endif ()
  194. if ( module_basename )
  195. string ( REGEX REPLACE "[ ]*%module[ ]*\\(.*\\)[ ]*([a-zA-Z0-9_]+).*" "\\1" module_basename "${module_basename}" )
  196. else ()
  197. # fallback to file basename
  198. get_filename_component(module_basename "${infile}" NAME_WE)
  199. endif ()
  200. endif ()
  201. endif()
  202. foreach(it ${SWIG_${language}_EXTRA_FILE_EXTENSIONS})
  203. set(extra_file "${generatedpath}/${module_basename}${it}")
  204. list(APPEND files "${extra_file}")
  205. endforeach()
  206. # Treat extra outputs as plain files regardless of language.
  207. set_source_files_properties(${files} PROPERTIES LANGUAGE "")
  208. set (${outfiles} ${files} PARENT_SCOPE)
  209. endfunction()
  210. #
  211. # Take swig (*.i) file and add proper custom commands for it
  212. #
  213. function(SWIG_ADD_SOURCE_TO_MODULE name outfiles infile)
  214. get_filename_component(swig_source_file_name_we "${infile}" NAME_WE)
  215. get_source_file_property(swig_source_file_cplusplus "${infile}" CPLUSPLUS)
  216. # If CMAKE_SWIG_OUTDIR was specified then pass it to -outdir
  217. if(CMAKE_SWIG_OUTDIR)
  218. set(outdir ${CMAKE_SWIG_OUTDIR})
  219. else()
  220. set(outdir ${CMAKE_CURRENT_BINARY_DIR})
  221. endif()
  222. if(SWIG_OUTFILE_DIR)
  223. set(outfiledir ${SWIG_OUTFILE_DIR})
  224. else()
  225. set(outfiledir ${outdir})
  226. endif()
  227. if(SWIG_WORKING_DIR)
  228. set (workingdir "${SWIG_WORKING_DIR}")
  229. else()
  230. set(workingdir "${outdir}")
  231. endif()
  232. set (swig_source_file_flags ${CMAKE_SWIG_FLAGS})
  233. # handle various swig compile flags properties
  234. get_source_file_property (include_directories "${infile}" INCLUDE_DIRECTORIES)
  235. if (include_directories)
  236. list (APPEND swig_source_file_flags "$<$<BOOL:${include_directories}>:-I$<JOIN:${include_directories},$<SEMICOLON>-I>>")
  237. endif()
  238. set (property "$<TARGET_PROPERTY:${name},SWIG_INCLUDE_DIRECTORIES>")
  239. list (APPEND swig_source_file_flags "$<$<BOOL:${property}>:-I$<JOIN:$<TARGET_GENEX_EVAL:${name},${property}>,$<SEMICOLON>-I>>")
  240. set (property "$<TARGET_PROPERTY:${name},SWIG_COMPILE_DEFINITIONS>")
  241. list (APPEND swig_source_file_flags "$<$<BOOL:${property}>:-D$<JOIN:$<TARGET_GENEX_EVAL:${name},${property}>,$<SEMICOLON>-D>>")
  242. get_source_file_property (compile_definitions "${infile}" COMPILE_DEFINITIONS)
  243. if (compile_definitions)
  244. list (APPEND swig_source_file_flags "$<$<BOOL:${compile_definitions}>:-D$<JOIN:${compile_definitions},$<SEMICOLON>-D>>")
  245. endif()
  246. list (APPEND swig_source_file_flags "$<TARGET_GENEX_EVAL:${name},$<TARGET_PROPERTY:${name},SWIG_COMPILE_OPTIONS>>")
  247. get_source_file_property (compile_options "${infile}" COMPILE_OPTIONS)
  248. if (compile_options)
  249. list (APPEND swig_source_file_flags ${compile_options})
  250. endif()
  251. # legacy support
  252. get_source_file_property (swig_flags "${infile}" SWIG_FLAGS)
  253. if (swig_flags)
  254. list (APPEND swig_source_file_flags ${swig_flags})
  255. endif()
  256. get_filename_component(swig_source_file_fullname "${infile}" ABSOLUTE)
  257. if (NOT SWIG_MODULE_${name}_NOPROXY)
  258. SWIG_GET_EXTRA_OUTPUT_FILES(${SWIG_MODULE_${name}_LANGUAGE}
  259. swig_extra_generated_files
  260. "${outdir}"
  261. "${swig_source_file_fullname}")
  262. endif()
  263. set(swig_generated_file_fullname
  264. "${outfiledir}/${swig_source_file_name_we}")
  265. # add the language into the name of the file (i.e. TCL_wrap)
  266. # this allows for the same .i file to be wrapped into different languages
  267. string(APPEND swig_generated_file_fullname
  268. "${SWIG_MODULE_${name}_LANGUAGE}_wrap")
  269. if(swig_source_file_cplusplus)
  270. string(APPEND swig_generated_file_fullname
  271. ".${SWIG_CXX_EXTENSION}")
  272. else()
  273. string(APPEND swig_generated_file_fullname
  274. ".c")
  275. endif()
  276. get_directory_property (cmake_include_directories INCLUDE_DIRECTORIES)
  277. list (REMOVE_DUPLICATES cmake_include_directories)
  278. set (swig_include_dirs)
  279. if (cmake_include_directories)
  280. set (swig_include_dirs "$<$<BOOL:${cmake_include_directories}>:-I$<JOIN:${cmake_include_directories},$<SEMICOLON>-I>>")
  281. endif()
  282. set(swig_special_flags)
  283. # default is c, so add c++ flag if it is c++
  284. if(swig_source_file_cplusplus)
  285. list (APPEND swig_special_flags "-c++")
  286. endif()
  287. set (swig_extra_flags)
  288. if(SWIG_MODULE_${name}_LANGUAGE STREQUAL "CSHARP")
  289. if(NOT ("-dllimport" IN_LIST swig_source_file_flags OR "-dllimport" IN_LIST SWIG_MODULE_${name}_EXTRA_FLAGS))
  290. # This makes sure that the name used in the generated DllImport
  291. # matches the library name created by CMake
  292. list (APPEND SWIG_MODULE_${name}_EXTRA_FLAGS "-dllimport" "${name}")
  293. endif()
  294. endif()
  295. list (APPEND swig_extra_flags ${SWIG_MODULE_${name}_EXTRA_FLAGS})
  296. # dependencies
  297. set (swig_dependencies ${SWIG_MODULE_${name}_EXTRA_DEPS} $<TARGET_PROPERTY:${name},SWIG_DEPENDS>)
  298. get_source_file_property(file_depends "${infile}" DEPENDS)
  299. if (file_depends)
  300. list (APPEND swig_dependencies ${file_depends})
  301. endif()
  302. if (UseSWIG_MODULE_VERSION VERSION_GREATER 1)
  303. # as part of custom command, start by removing old generated files
  304. # to ensure obsolete files do not stay
  305. set (swig_cleanup_command COMMAND "${CMAKE_COMMAND}" -E remove_directory "${outdir}")
  306. else()
  307. unset (swig_cleanup_command)
  308. endif()
  309. # IMPLICIT_DEPENDS below can not handle situations where a dependent file is
  310. # removed. We need an extra step with timestamp and custom target, see #16830
  311. # As this is needed only for Makefile generator do it conditionally
  312. if(CMAKE_GENERATOR MATCHES "Make")
  313. __swig_compute_timestamp(${name} ${SWIG_MODULE_${name}_LANGUAGE}
  314. "${infile}" "${workingdir}" swig_generated_timestamp)
  315. set(swig_custom_output "${swig_generated_timestamp}")
  316. set(swig_custom_products
  317. BYPRODUCTS "${swig_generated_file_fullname}" ${swig_extra_generated_files})
  318. set(swig_timestamp_command
  319. COMMAND ${CMAKE_COMMAND} -E touch "${swig_generated_timestamp}")
  320. else()
  321. set(swig_custom_output
  322. "${swig_generated_file_fullname}" ${swig_extra_generated_files})
  323. set(swig_custom_products)
  324. set(swig_timestamp_command)
  325. endif()
  326. add_custom_command(
  327. OUTPUT ${swig_custom_output}
  328. ${swig_custom_products}
  329. ${swig_cleanup_command}
  330. # Let's create the ${outdir} at execution time, in case dir contains $(OutDir)
  331. COMMAND "${CMAKE_COMMAND}" -E make_directory ${outdir} ${outfiledir}
  332. ${swig_timestamp_command}
  333. COMMAND "${CMAKE_COMMAND}" -E env "SWIG_LIB=${SWIG_DIR}" "${SWIG_EXECUTABLE}"
  334. "-${SWIG_MODULE_${name}_SWIG_LANGUAGE_FLAG}"
  335. "${swig_source_file_flags}"
  336. -outdir "${outdir}"
  337. ${swig_special_flags}
  338. ${swig_extra_flags}
  339. "${swig_include_dirs}"
  340. -o "${swig_generated_file_fullname}"
  341. "${swig_source_file_fullname}"
  342. MAIN_DEPENDENCY "${swig_source_file_fullname}"
  343. DEPENDS ${swig_dependencies}
  344. IMPLICIT_DEPENDS CXX "${swig_source_file_fullname}"
  345. COMMENT "Swig source"
  346. COMMAND_EXPAND_LISTS)
  347. set_source_files_properties("${swig_generated_file_fullname}" ${swig_extra_generated_files}
  348. PROPERTIES GENERATED 1)
  349. ## add all properties for generated file to various properties
  350. get_property (include_directories SOURCE "${infile}" PROPERTY GENERATED_INCLUDE_DIRECTORIES)
  351. set_property (SOURCE "${swig_generated_file_fullname}" PROPERTY INCLUDE_DIRECTORIES ${include_directories} $<TARGET_GENEX_EVAL:${name},$<TARGET_PROPERTY:${name},SWIG_GENERATED_INCLUDE_DIRECTORIES>>)
  352. get_property (compile_definitions SOURCE "${infile}" PROPERTY GENERATED_COMPILE_DEFINITIONS)
  353. set_property (SOURCE "${swig_generated_file_fullname}" PROPERTY COMPILE_DEFINITIONS $<TARGET_GENEX_EVAL:${name},$<TARGET_PROPERTY:${name},SWIG_GENERATED_COMPILE_DEFINITIONS>> ${compile_definitions})
  354. get_property (compile_options SOURCE "${infile}" PROPERTY GENERATED_COMPILE_OPTIONS)
  355. set_property (SOURCE "${swig_generated_file_fullname}" PROPERTY COMPILE_OPTIONS $<TARGET_GENEX_EVAL:${name},$<TARGET_PROPERTY:${name},SWIG_GENERATED_COMPILE_OPTIONS>> ${compile_options})
  356. set(${outfiles} "${swig_generated_file_fullname}" ${swig_extra_generated_files} PARENT_SCOPE)
  357. # legacy support
  358. set (swig_generated_file_fullname "${swig_generated_file_fullname}" PARENT_SCOPE)
  359. endfunction()
  360. #
  361. # Create Swig module
  362. #
  363. macro(SWIG_ADD_MODULE name language)
  364. message(DEPRECATION "SWIG_ADD_MODULE is deprecated. Use SWIG_ADD_LIBRARY instead.")
  365. swig_add_library(${name}
  366. LANGUAGE ${language}
  367. TYPE MODULE
  368. SOURCES ${ARGN})
  369. endmacro()
  370. function(SWIG_ADD_LIBRARY name)
  371. set(options NO_PROXY)
  372. set(oneValueArgs LANGUAGE
  373. TYPE
  374. OUTPUT_DIR
  375. OUTFILE_DIR)
  376. set(multiValueArgs SOURCES)
  377. cmake_parse_arguments(_SAM "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
  378. if (TARGET ${name})
  379. # a target with same name is already defined.
  380. # call NOW add_library command to raise the most useful error message
  381. add_library(${name})
  382. return()
  383. endif()
  384. if (_SAM_UNPARSED_ARGUMENTS)
  385. message(FATAL_ERROR "SWIG_ADD_LIBRARY: ${_SAM_UNPARSED_ARGUMENTS}: unexpected arguments")
  386. endif()
  387. if(NOT DEFINED _SAM_LANGUAGE)
  388. message(FATAL_ERROR "SWIG_ADD_LIBRARY: Missing LANGUAGE argument")
  389. endif()
  390. if(NOT DEFINED _SAM_SOURCES)
  391. message(FATAL_ERROR "SWIG_ADD_LIBRARY: Missing SOURCES argument")
  392. endif()
  393. if(NOT DEFINED _SAM_TYPE)
  394. set(_SAM_TYPE MODULE)
  395. elseif(_SAM_TYPE STREQUAL "USE_BUILD_SHARED_LIBS")
  396. unset(_SAM_TYPE)
  397. endif()
  398. if (NOT DEFINED UseSWIG_MODULE_VERSION)
  399. set (UseSWIG_MODULE_VERSION 1)
  400. elseif (NOT UseSWIG_MODULE_VERSION MATCHES "^(1|2)$")
  401. message (FATAL_ERROR "UseSWIG_MODULE_VERSION: ${UseSWIG_MODULE_VERSION}: invalid value. 1 or 2 is expected.")
  402. endif()
  403. set (workingdir "${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/${name}.dir")
  404. # set special variable to pass extra information to command SWIG_ADD_SOURCE_TO_MODULE
  405. # which cannot be changed due to legacy compatibility
  406. set (SWIG_WORKING_DIR "${workingdir}")
  407. set (outputdir "${_SAM_OUTPUT_DIR}")
  408. if (NOT _SAM_OUTPUT_DIR)
  409. if (CMAKE_SWIG_OUTDIR)
  410. set (outputdir "${CMAKE_SWIG_OUTDIR}")
  411. else()
  412. if (UseSWIG_MODULE_VERSION VERSION_GREATER 1)
  413. set (outputdir "${workingdir}/${_SAM_LANGUAGE}.files")
  414. else()
  415. set (outputdir "${CMAKE_CURRENT_BINARY_DIR}")
  416. endif()
  417. endif()
  418. endif()
  419. set (outfiledir "${_SAM_OUTFILE_DIR}")
  420. if(NOT _SAM_OUTFILE_DIR)
  421. if (SWIG_OUTFILE_DIR)
  422. set (outfiledir "${SWIG_OUTFILE_DIR}")
  423. else()
  424. if (_SAM_OUTPUT_DIR OR CMAKE_SWIG_OUTDIR)
  425. set (outfiledir "${outputdir}")
  426. else()
  427. set (outfiledir "${workingdir}")
  428. endif()
  429. endif()
  430. endif()
  431. # set again, locally, predefined variables to ensure compatibility
  432. # with command SWIG_ADD_SOURCE_TO_MODULE
  433. set(CMAKE_SWIG_OUTDIR "${outputdir}")
  434. set(SWIG_OUTFILE_DIR "${outfiledir}")
  435. set (SWIG_MODULE_${name}_NOPROXY ${_SAM_NO_PROXY})
  436. swig_module_initialize(${name} ${_SAM_LANGUAGE})
  437. set(swig_dot_i_sources ${_SAM_SOURCES})
  438. list(FILTER swig_dot_i_sources INCLUDE REGEX "\\.i$")
  439. if (NOT swig_dot_i_sources)
  440. message(FATAL_ERROR "SWIG_ADD_LIBRARY: no SWIG interface files specified")
  441. endif()
  442. set(swig_other_sources ${_SAM_SOURCES})
  443. list(REMOVE_ITEM swig_other_sources ${swig_dot_i_sources})
  444. set(swig_generated_sources)
  445. set(swig_generated_timestamps)
  446. foreach(swig_it IN LISTS swig_dot_i_sources)
  447. SWIG_ADD_SOURCE_TO_MODULE(${name} swig_generated_source "${swig_it}")
  448. list (APPEND swig_generated_sources "${swig_generated_source}")
  449. if(CMAKE_GENERATOR MATCHES "Make")
  450. __swig_compute_timestamp(${name} ${SWIG_MODULE_${name}_LANGUAGE} "${swig_it}"
  451. "${workingdir}" swig_timestamp)
  452. list (APPEND swig_generated_timestamps "${swig_timestamp}")
  453. endif()
  454. endforeach()
  455. set_property (DIRECTORY APPEND PROPERTY
  456. ADDITIONAL_MAKE_CLEAN_FILES ${swig_generated_sources} ${swig_generated_timestamps})
  457. if (UseSWIG_MODULE_VERSION VERSION_GREATER 1)
  458. set_property (DIRECTORY APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES "${outputdir}")
  459. endif()
  460. add_library(${name}
  461. ${_SAM_TYPE}
  462. ${swig_generated_sources}
  463. ${swig_other_sources})
  464. if(CMAKE_GENERATOR MATCHES "Make")
  465. # see IMPLICIT_DEPENDS above
  466. add_custom_target(${name}_swig_compilation DEPENDS ${swig_generated_timestamps})
  467. add_dependencies(${name} ${name}_swig_compilation)
  468. endif()
  469. if(_SAM_TYPE STREQUAL "MODULE")
  470. set_target_properties(${name} PROPERTIES NO_SONAME ON)
  471. endif()
  472. string(TOLOWER "${_SAM_LANGUAGE}" swig_lowercase_language)
  473. if (swig_lowercase_language STREQUAL "octave")
  474. set_target_properties(${name} PROPERTIES PREFIX "")
  475. set_target_properties(${name} PROPERTIES SUFFIX ".oct")
  476. elseif (swig_lowercase_language STREQUAL "go")
  477. set_target_properties(${name} PROPERTIES PREFIX "")
  478. elseif (swig_lowercase_language STREQUAL "java")
  479. # In java you want:
  480. # System.loadLibrary("LIBRARY");
  481. # then JNI will look for a library whose name is platform dependent, namely
  482. # MacOS : libLIBRARY.jnilib
  483. # Windows: LIBRARY.dll
  484. # Linux : libLIBRARY.so
  485. if (APPLE)
  486. set_target_properties (${name} PROPERTIES SUFFIX ".jnilib")
  487. endif()
  488. if ((WIN32 AND MINGW) OR CYGWIN OR CMAKE_SYSTEM_NAME STREQUAL MSYS)
  489. set_target_properties(${name} PROPERTIES PREFIX "")
  490. endif()
  491. elseif (swig_lowercase_language STREQUAL "lua")
  492. if(_SAM_TYPE STREQUAL "MODULE")
  493. set_target_properties(${name} PROPERTIES PREFIX "")
  494. endif()
  495. elseif (swig_lowercase_language STREQUAL "python")
  496. if (SWIG_MODULE_${name}_NOPROXY)
  497. set_target_properties(${name} PROPERTIES PREFIX "")
  498. else()
  499. # swig will produce a module.py containing an 'import _modulename' statement,
  500. # which implies having a corresponding _modulename.so (*NIX), _modulename.pyd (Win32),
  501. # unless the -noproxy flag is used
  502. set_target_properties(${name} PROPERTIES PREFIX "_")
  503. endif()
  504. # Python extension modules on Windows must have the extension ".pyd"
  505. # instead of ".dll" as of Python 2.5. Older python versions do support
  506. # this suffix.
  507. # http://docs.python.org/whatsnew/ports.html#SECTION0001510000000000000000
  508. # <quote>
  509. # Windows: .dll is no longer supported as a filename extension for extension modules.
  510. # .pyd is now the only filename extension that will be searched for.
  511. # </quote>
  512. if(WIN32 AND NOT CYGWIN)
  513. set_target_properties(${name} PROPERTIES SUFFIX ".pyd")
  514. endif()
  515. elseif (swig_lowercase_language STREQUAL "r")
  516. set_target_properties(${name} PROPERTIES PREFIX "")
  517. elseif (swig_lowercase_language STREQUAL "ruby")
  518. # In ruby you want:
  519. # require 'LIBRARY'
  520. # then ruby will look for a library whose name is platform dependent, namely
  521. # MacOS : LIBRARY.bundle
  522. # Windows: LIBRARY.dll
  523. # Linux : LIBRARY.so
  524. set_target_properties (${name} PROPERTIES PREFIX "")
  525. if (APPLE)
  526. set_target_properties (${name} PROPERTIES SUFFIX ".bundle")
  527. endif ()
  528. elseif (swig_lowercase_language STREQUAL "perl")
  529. # assume empty prefix because we expect the module to be dynamically loaded
  530. set_target_properties (${name} PROPERTIES PREFIX "")
  531. if (APPLE)
  532. set_target_properties (${name} PROPERTIES SUFFIX ".dylib")
  533. endif ()
  534. else()
  535. # assume empty prefix because we expect the module to be dynamically loaded
  536. set_target_properties (${name} PROPERTIES PREFIX "")
  537. endif ()
  538. # target property SWIG_SUPPORT_FILES_DIRECTORY specify output directory of support files
  539. set_property (TARGET ${name} PROPERTY SWIG_SUPPORT_FILES_DIRECTORY "${outputdir}")
  540. # target property SWIG_SUPPORT_FILES lists principal proxy support files
  541. if (NOT SWIG_MODULE_${name}_NOPROXY)
  542. string(TOUPPER "${_SAM_LANGUAGE}" swig_uppercase_language)
  543. set(swig_all_support_files)
  544. foreach (swig_it IN LISTS SWIG_${swig_uppercase_language}_EXTRA_FILE_EXTENSIONS)
  545. set (swig_support_files ${swig_generated_sources})
  546. list (FILTER swig_support_files INCLUDE REGEX ".*${swig_it}$")
  547. list(APPEND swig_all_support_files ${swig_support_files})
  548. endforeach()
  549. if (swig_all_support_files)
  550. list(REMOVE_DUPLICATES swig_all_support_files)
  551. endif()
  552. set_property (TARGET ${name} PROPERTY SWIG_SUPPORT_FILES ${swig_all_support_files})
  553. endif()
  554. # to ensure legacy behavior, export some variables
  555. set (SWIG_MODULE_${name}_LANGUAGE "${SWIG_MODULE_${name}_LANGUAGE}" PARENT_SCOPE)
  556. set (SWIG_MODULE_${name}_SWIG_LANGUAGE_FLAG "${SWIG_MODULE_${name}_SWIG_LANGUAGE_FLAG}" PARENT_SCOPE)
  557. set (SWIG_MODULE_${name}_REAL_NAME "${name}" PARENT_SCOPE)
  558. set (SWIG_MODULE_${name}_NOPROXY "${SWIG_MODULE_${name}_NOPROXY}" PARENT_SCOPE)
  559. set (SWIG_MODULE_${name}_EXTRA_FLAGS "${SWIG_MODULE_${name}_EXTRA_FLAGS}" PARENT_SCOPE)
  560. # the last one is a bit crazy but it is documented, so...
  561. # NOTA: works as expected if only ONE input file is specified
  562. set (swig_generated_file_fullname "${swig_generated_file_fullname}" PARENT_SCOPE)
  563. endfunction()
  564. #
  565. # Like TARGET_LINK_LIBRARIES but for swig modules
  566. #
  567. function(SWIG_LINK_LIBRARIES name)
  568. message(DEPRECATION "SWIG_LINK_LIBRARIES is deprecated. Use TARGET_LINK_LIBRARIES instead.")
  569. if(SWIG_MODULE_${name}_REAL_NAME)
  570. target_link_libraries(${name} ${ARGN})
  571. else()
  572. message(SEND_ERROR "Cannot find Swig library \"${name}\".")
  573. endif()
  574. endfunction()