UseSWIG.cmake 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084
  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. .. only:: html
  9. .. contents::
  10. CMake Commands
  11. ^^^^^^^^^^^^^^
  12. The following command is defined for use with ``SWIG``:
  13. .. command:: swig_add_library
  14. .. versionadded:: 3.8
  15. Define swig module with given name and specified language::
  16. swig_add_library(<name>
  17. [TYPE <SHARED|MODULE|STATIC|USE_BUILD_SHARED_LIBS>]
  18. LANGUAGE <language>
  19. [NO_PROXY]
  20. [OUTPUT_DIR <directory>]
  21. [OUTFILE_DIR <directory>]
  22. SOURCES <file>...
  23. )
  24. Targets created with the ``swig_add_library`` command have the same
  25. capabilities as targets created with the :command:`add_library` command, so
  26. those targets can be used with any command expecting a target (e.g.
  27. :command:`target_link_libraries`).
  28. .. versionchanged:: 3.13
  29. This command creates a target with the specified ``<name>`` when
  30. policy :policy:`CMP0078` is set to ``NEW``. Otherwise, the legacy
  31. behavior will choose a different target name and store it in the
  32. ``SWIG_MODULE_<name>_REAL_NAME`` variable.
  33. .. versionchanged:: 3.15
  34. Alternate library name (set with the :prop_tgt:`OUTPUT_NAME` property,
  35. for example) will be passed on to ``Python`` and ``CSharp`` wrapper
  36. libraries.
  37. .. versionchanged:: 3.21
  38. Generated library use standard naming conventions for ``CSharp`` language
  39. when policy :policy:`CMP0122` is set to ``NEW``. Otherwise, the legacy
  40. behavior is applied.
  41. .. note::
  42. For multi-config generators, this module does not support
  43. configuration-specific files generated by ``SWIG``. All build
  44. configurations must result in the same generated source file.
  45. .. note::
  46. For :ref:`Makefile Generators`, if, for some sources, the
  47. ``USE_SWIG_DEPENDENCIES`` property is ``FALSE``, ``swig_add_library`` does
  48. not track file dependencies, so depending on the ``<name>_swig_compilation``
  49. custom target is required for targets which require the ``swig``-generated
  50. files to exist. Other generators may depend on the source files that would
  51. be generated by SWIG.
  52. ``TYPE``
  53. ``SHARED``, ``MODULE`` and ``STATIC`` have the same semantic as for the
  54. :command:`add_library` command. If ``USE_BUILD_SHARED_LIBS`` is specified,
  55. the library type will be ``STATIC`` or ``SHARED`` based on whether the
  56. current value of the :variable:`BUILD_SHARED_LIBS` variable is ``ON``. If
  57. no type is specified, ``MODULE`` will be used.
  58. ``LANGUAGE``
  59. Specify the target language.
  60. .. versionadded:: 3.1
  61. Go and Lua language support.
  62. .. versionadded:: 3.2
  63. R language support.
  64. .. versionadded:: 3.18
  65. Fortran language support.
  66. ``NO_PROXY``
  67. .. versionadded:: 3.12
  68. Prevent the generation of the wrapper layer (swig ``-noproxy`` option).
  69. ``OUTPUT_DIR``
  70. .. versionadded:: 3.12
  71. Specify where to write the language specific files (swig ``-outdir``
  72. option). If not given, the ``CMAKE_SWIG_OUTDIR`` variable will be used.
  73. If neither is specified, the default depends on the value of the
  74. ``UseSWIG_MODULE_VERSION`` variable as follows:
  75. * If ``UseSWIG_MODULE_VERSION`` is 1 or is undefined, output is written to
  76. the :variable:`CMAKE_CURRENT_BINARY_DIR` directory.
  77. * If ``UseSWIG_MODULE_VERSION`` is 2, a dedicated directory will be used.
  78. The path of this directory can be retrieved from the
  79. ``SWIG_SUPPORT_FILES_DIRECTORY`` target property.
  80. ``OUTFILE_DIR``
  81. .. versionadded:: 3.12
  82. Specify an output directory name where the generated source file will be
  83. placed (swig ``-o`` option). If not specified, the ``SWIG_OUTFILE_DIR``
  84. variable will be used. If neither is specified, ``OUTPUT_DIR`` or
  85. ``CMAKE_SWIG_OUTDIR`` is used instead.
  86. ``SOURCES``
  87. List of sources for the library. Files with extension ``.i`` will be
  88. identified as sources for the ``SWIG`` tool. Other files will be handled in
  89. the standard way.
  90. .. versionadded:: 3.14
  91. This behavior can be overridden by specifying the variable
  92. ``SWIG_SOURCE_FILE_EXTENSIONS``.
  93. .. note::
  94. If ``UseSWIG_MODULE_VERSION`` is set to 2, it is **strongly** recommended
  95. to use a dedicated directory unique to the target when either the
  96. ``OUTPUT_DIR`` option or the ``CMAKE_SWIG_OUTDIR`` variable are specified.
  97. The output directory contents are erased as part of the target build, so
  98. to prevent interference between targets or losing other important files,
  99. each target should have its own dedicated output directory.
  100. Properties on Source Files
  101. ^^^^^^^^^^^^^^^^^^^^^^^^^^
  102. Source file properties on module files **must** be set before the invocation
  103. of the ``swig_add_library`` command to specify special behavior of SWIG and
  104. ensure generated files will receive the required settings.
  105. ``CPLUSPLUS``
  106. Call SWIG in c++ mode. For example:
  107. .. code-block:: cmake
  108. set_property(SOURCE mymod.i PROPERTY CPLUSPLUS ON)
  109. swig_add_library(mymod LANGUAGE python SOURCES mymod.i)
  110. ``SWIG_FLAGS``
  111. .. deprecated:: 3.12
  112. Replaced with the fine-grained properties that follow.
  113. Pass custom flags to the SWIG executable.
  114. ``INCLUDE_DIRECTORIES``, ``COMPILE_DEFINITIONS`` and ``COMPILE_OPTIONS``
  115. .. versionadded:: 3.12
  116. Add custom flags to SWIG compiler and have same semantic as properties
  117. :prop_sf:`INCLUDE_DIRECTORIES`, :prop_sf:`COMPILE_DEFINITIONS` and
  118. :prop_sf:`COMPILE_OPTIONS`.
  119. ``USE_TARGET_INCLUDE_DIRECTORIES``
  120. .. versionadded:: 3.13
  121. If set to ``TRUE``, contents of target property
  122. :prop_tgt:`INCLUDE_DIRECTORIES` will be forwarded to ``SWIG`` compiler.
  123. If set to ``FALSE`` target property :prop_tgt:`INCLUDE_DIRECTORIES` will be
  124. ignored. If not set, target property ``SWIG_USE_TARGET_INCLUDE_DIRECTORIES``
  125. will be considered.
  126. ``GENERATED_INCLUDE_DIRECTORIES``, ``GENERATED_COMPILE_DEFINITIONS`` and ``GENERATED_COMPILE_OPTIONS``
  127. .. versionadded:: 3.12
  128. Add custom flags to the C/C++ generated source. They will fill, respectively,
  129. properties :prop_sf:`INCLUDE_DIRECTORIES`, :prop_sf:`COMPILE_DEFINITIONS` and
  130. :prop_sf:`COMPILE_OPTIONS` of generated C/C++ file.
  131. ``DEPENDS``
  132. .. versionadded:: 3.12
  133. Specify additional dependencies to the source file.
  134. ``USE_SWIG_DEPENDENCIES``
  135. .. versionadded:: 3.20
  136. If set to ``TRUE``, implicit dependencies are generated by the ``swig`` tool
  137. itself. This property is only meaningful for
  138. :ref:`Makefile <Makefile Generators>`,
  139. :ref:`Ninja <Ninja Generators>`, :generator:`Xcode`, and
  140. :ref:`Visual Studio <Visual Studio Generators>`
  141. (:generator:`Visual Studio 12 2013` and above) generators. Default value is
  142. ``FALSE``.
  143. .. versionadded:: 3.21
  144. Added the support of :generator:`Xcode` generator.
  145. .. versionadded:: 3.22
  146. Added the support of :ref:`Visual Studio Generators`.
  147. ``SWIG_MODULE_NAME``
  148. Specify the actual import name of the module in the target language.
  149. This is required if it cannot be scanned automatically from source
  150. or different from the module file basename. For example:
  151. .. code-block:: cmake
  152. set_property(SOURCE mymod.i PROPERTY SWIG_MODULE_NAME mymod_realname)
  153. .. versionchanged:: 3.14
  154. If policy :policy:`CMP0086` is set to ``NEW``, ``-module <module_name>``
  155. is passed to ``SWIG`` compiler.
  156. ``OUTPUT_DIR``
  157. .. versionadded:: 3.19
  158. Specify where to write the language specific files (swig ``-outdir`` option)
  159. for the considered source file. If not specified, the other ways to define
  160. the output directory applies (see ``OUTPUT_DIR`` option of
  161. ``swig_add_library()`` command).
  162. ``OUTFILE_DIR``
  163. .. versionadded:: 3.19
  164. Specify an output directory where the generated source file will be placed
  165. (swig ``-o`` option) for the considered source file. If not specified,
  166. ``OUTPUT_DIR`` source property will be used. If neither are specified, the
  167. other ways to define output file directory applies (see ``OUTFILE_DIR``
  168. option of ``swig_add_library()`` command).
  169. Properties on Targets
  170. ^^^^^^^^^^^^^^^^^^^^^
  171. Target library properties can be set to apply same configuration to all SWIG
  172. input files.
  173. ``SWIG_INCLUDE_DIRECTORIES``, ``SWIG_COMPILE_DEFINITIONS`` and ``SWIG_COMPILE_OPTIONS``
  174. .. versionadded:: 3.12
  175. These properties will be applied to all SWIG input files and have same
  176. semantic as target properties :prop_tgt:`INCLUDE_DIRECTORIES`,
  177. :prop_tgt:`COMPILE_DEFINITIONS` and :prop_tgt:`COMPILE_OPTIONS`.
  178. .. code-block:: cmake
  179. set (UseSWIG_TARGET_NAME_PREFERENCE STANDARD)
  180. swig_add_library(mymod LANGUAGE python SOURCES mymod.i)
  181. set_property(TARGET mymod PROPERTY SWIG_COMPILE_DEFINITIONS MY_DEF1 MY_DEF2)
  182. set_property(TARGET mymod PROPERTY SWIG_COMPILE_OPTIONS -bla -blb)
  183. ``SWIG_USE_TARGET_INCLUDE_DIRECTORIES``
  184. .. versionadded:: 3.13
  185. If set to ``TRUE``, contents of target property
  186. :prop_tgt:`INCLUDE_DIRECTORIES` will be forwarded to ``SWIG`` compiler.
  187. If set to ``FALSE`` or not defined, target property
  188. :prop_tgt:`INCLUDE_DIRECTORIES` will be ignored. This behavior can be
  189. overridden by specifying source property ``USE_TARGET_INCLUDE_DIRECTORIES``.
  190. ``SWIG_GENERATED_INCLUDE_DIRECTORIES``, ``SWIG_GENERATED_COMPILE_DEFINITIONS`` and ``SWIG_GENERATED_COMPILE_OPTIONS``
  191. .. versionadded:: 3.12
  192. These properties will populate, respectively, properties
  193. :prop_sf:`INCLUDE_DIRECTORIES`, :prop_sf:`COMPILE_DEFINITIONS` and
  194. :prop_sf:`COMPILE_FLAGS` of all generated C/C++ files.
  195. ``SWIG_DEPENDS``
  196. .. versionadded:: 3.12
  197. Add dependencies to all SWIG input files.
  198. Read-only Target Properties
  199. """""""""""""""""""""""""""
  200. The following target properties are output properties and can be used to get
  201. information about support files generated by ``SWIG`` interface compilation.
  202. ``SWIG_SUPPORT_FILES``
  203. .. versionadded:: 3.12
  204. This output property list of wrapper files generated during SWIG compilation.
  205. .. code-block:: cmake
  206. set (UseSWIG_TARGET_NAME_PREFERENCE STANDARD)
  207. swig_add_library(mymod LANGUAGE python SOURCES mymod.i)
  208. get_property(support_files TARGET mymod PROPERTY SWIG_SUPPORT_FILES)
  209. .. note::
  210. Only most principal support files are listed. In case some advanced
  211. features of ``SWIG`` are used (for example ``%template``), associated
  212. support files may not be listed. Prefer to use the
  213. ``SWIG_SUPPORT_FILES_DIRECTORY`` property to handle support files.
  214. ``SWIG_SUPPORT_FILES_DIRECTORY``
  215. .. versionadded:: 3.12
  216. This output property specifies the directory where support files will be
  217. generated.
  218. .. note::
  219. When source property ``OUTPUT_DIR`` is defined, multiple directories can be
  220. specified as part of ``SWIG_SUPPORT_FILES_DIRECTORY``.
  221. CMake Variables
  222. ^^^^^^^^^^^^^^^
  223. Some variables can be set to customize the behavior of ``swig_add_library``
  224. as well as ``SWIG``:
  225. ``UseSWIG_MODULE_VERSION``
  226. .. versionadded:: 3.12
  227. Specify different behaviors for ``UseSWIG`` module.
  228. * Set to 1 or undefined: Legacy behavior is applied.
  229. * Set to 2: A new strategy is applied regarding support files: the output
  230. directory of support files is erased before ``SWIG`` interface compilation.
  231. ``CMAKE_SWIG_FLAGS``
  232. Add flags to all swig calls.
  233. ``CMAKE_SWIG_OUTDIR``
  234. Specify where to write the language specific files (swig ``-outdir`` option).
  235. ``SWIG_OUTFILE_DIR``
  236. .. versionadded:: 3.8
  237. Specify an output directory name where the generated source file will be
  238. placed. If not specified, ``CMAKE_SWIG_OUTDIR`` is used.
  239. ``SWIG_MODULE_<name>_EXTRA_DEPS``
  240. Specify extra dependencies for the generated module for ``<name>``.
  241. ``SWIG_SOURCE_FILE_EXTENSIONS``
  242. .. versionadded:: 3.14
  243. Specify a list of source file extensions to override the default
  244. behavior of considering only ``.i`` files as sources for the ``SWIG``
  245. tool. For example:
  246. .. code-block:: cmake
  247. set(SWIG_SOURCE_FILE_EXTENSIONS ".i" ".swg")
  248. ``SWIG_USE_SWIG_DEPENDENCIES``
  249. .. versionadded:: 3.20
  250. If set to ``TRUE``, implicit dependencies are generated by the ``swig`` tool
  251. itself. This variable is only meaningful for
  252. :ref:`Makefile <Makefile Generators>`,
  253. :ref:`Ninja <Ninja Generators>`, :generator:`Xcode`, and
  254. :ref:`Visual Studio <Visual Studio Generators>`
  255. (:generator:`Visual Studio 12 2013` and above) generators. Default value is
  256. ``FALSE``.
  257. Source file property ``USE_SWIG_DEPENDENCIES``, if not defined, will be
  258. initialized with the value of this variable.
  259. .. versionadded:: 3.21
  260. Added the support of :generator:`Xcode` generator.
  261. .. versionadded:: 3.22
  262. Added the support of :ref:`Visual Studio Generators`.
  263. Deprecated Commands
  264. ^^^^^^^^^^^^^^^^^^^
  265. .. command:: swig_link_libraries
  266. .. deprecated:: 3.13
  267. Use :command:`target_link_libraries` with the standard target name,
  268. or with ``${SWIG_MODULE_<name>_REAL_NAME}`` for legacy target naming.
  269. Link libraries to swig module::
  270. swig_link_libraries(<name> <item>...)
  271. This command has same capabilities as :command:`target_link_libraries`
  272. command.
  273. .. note::
  274. When policy :policy:`CMP0078` is set to ``NEW``,
  275. :command:`swig_add_library` creates a standard target with the
  276. specified ``<name>`` and :command:`target_link_libraries` must be used
  277. instead of this command.
  278. With the legacy behavior (when :policy:`CMP0078` is set to ``OLD`` and
  279. the ``UseSWIG_TARGET_NAME_PREFERENCE`` variable is set to ``"LEGACY"``,
  280. or in CMake versions prior to 3.12), it is preferable to use
  281. ``target_link_libraries(${SWIG_MODULE_<name>_REAL_NAME} ...)``
  282. instead of this command.
  283. #]=======================================================================]
  284. cmake_policy(PUSH)
  285. # numbers and boolean constants
  286. cmake_policy (SET CMP0012 NEW)
  287. # IN_LIST operator
  288. cmake_policy (SET CMP0057 NEW)
  289. # Ninja generator normalizes custom command depfile paths
  290. cmake_policy (SET CMP0116 NEW)
  291. set(SWIG_CXX_EXTENSION "cxx")
  292. set(SWIG_EXTRA_LIBRARIES "")
  293. set(SWIG_PYTHON_EXTRA_FILE_EXTENSIONS ".py")
  294. set(SWIG_JAVA_EXTRA_FILE_EXTENSIONS ".java" "JNI.java")
  295. set(SWIG_CSHARP_EXTRA_FILE_EXTENSIONS ".cs" "PINVOKE.cs")
  296. set(SWIG_PERL_EXTRA_FILE_EXTENSIONS ".pm")
  297. set(SWIG_PERL5_EXTRA_FILE_EXTENSIONS ".pm")
  298. set(SWIG_MANAGE_SUPPORT_FILES_SCRIPT "${CMAKE_CURRENT_LIST_DIR}/UseSWIG/ManageSupportFiles.cmake")
  299. ##
  300. ## PRIVATE functions
  301. ##
  302. function (__SWIG_COMPUTE_TIMESTAMP name language infile workingdir __timestamp)
  303. get_filename_component(filename "${infile}" NAME_WE)
  304. set(${__timestamp}
  305. "${workingdir}/${filename}${language}.stamp" PARENT_SCOPE)
  306. # get_filename_component(filename "${infile}" ABSOLUTE)
  307. # string(UUID uuid NAMESPACE 9735D882-D2F8-4E1D-88C9-A0A4F1F6ECA4
  308. # NAME ${name}-${language}-${filename} TYPE SHA1)
  309. # set(${__timestamp} "${workingdir}/${uuid}.stamp" PARENT_SCOPE)
  310. endfunction()
  311. #
  312. # For given swig module initialize variables associated with it
  313. #
  314. macro(SWIG_MODULE_INITIALIZE name language)
  315. string(TOUPPER "${language}" SWIG_MODULE_${name}_LANGUAGE)
  316. string(TOLOWER "${language}" SWIG_MODULE_${name}_SWIG_LANGUAGE_FLAG)
  317. if (NOT DEFINED SWIG_MODULE_${name}_NOPROXY)
  318. set (SWIG_MODULE_${name}_NOPROXY FALSE)
  319. endif()
  320. if ("-noproxy" IN_LIST CMAKE_SWIG_FLAGS)
  321. set (SWIG_MODULE_${name}_NOPROXY TRUE)
  322. endif ()
  323. if (SWIG_MODULE_${name}_NOPROXY AND
  324. NOT ("-noproxy" IN_LIST CMAKE_SWIG_FLAGS OR "-noproxy" IN_LIST SWIG_MODULE_${name}_EXTRA_FLAGS))
  325. list (APPEND SWIG_MODULE_${name}_EXTRA_FLAGS "-noproxy")
  326. endif()
  327. if(SWIG_MODULE_${name}_LANGUAGE STREQUAL "UNKNOWN")
  328. message(FATAL_ERROR "SWIG Error: Language \"${language}\" not found")
  329. elseif((SWIG_MODULE_${name}_LANGUAGE STREQUAL "PERL" OR SWIG_MODULE_${name}_LANGUAGE STREQUAL "PERL5")
  330. AND NOT "-shadow" IN_LIST SWIG_MODULE_${name}_EXTRA_FLAGS)
  331. list(APPEND SWIG_MODULE_${name}_EXTRA_FLAGS "-shadow")
  332. endif()
  333. endmacro()
  334. #
  335. # For a given language, input file, and output file, determine extra files that
  336. # will be generated. This is internal swig macro.
  337. #
  338. function(SWIG_GET_EXTRA_OUTPUT_FILES language outfiles generatedpath infile)
  339. set(files)
  340. get_source_file_property(module_basename
  341. "${infile}" SWIG_MODULE_NAME)
  342. if(NOT module_basename)
  343. # try to get module name from "%module foo" syntax
  344. if ( EXISTS "${infile}" )
  345. cmake_policy(PUSH)
  346. cmake_policy(SET CMP0159 NEW) # file(STRINGS) with REGEX updates CMAKE_MATCH_<n>
  347. file ( STRINGS "${infile}" module_basename REGEX "[ ]*%module[ ]*[a-zA-Z0-9_]+.*" )
  348. cmake_policy(POP)
  349. endif ()
  350. if ( module_basename )
  351. string ( REGEX REPLACE "[ ]*%module[ ]*([a-zA-Z0-9_]+).*" "\\1" module_basename "${module_basename}" )
  352. else ()
  353. # try to get module name from "%module (options=...) foo" syntax
  354. if ( EXISTS "${infile}" )
  355. cmake_policy(PUSH)
  356. cmake_policy(SET CMP0159 NEW) # file(STRINGS) with REGEX updates CMAKE_MATCH_<n>
  357. file ( STRINGS "${infile}" module_basename REGEX "[ ]*%module[ ]*\\(.*\\)[ ]*[a-zA-Z0-9_]+.*" )
  358. cmake_policy(POP)
  359. endif ()
  360. if ( module_basename )
  361. string ( REGEX REPLACE "[ ]*%module[ ]*\\(.*\\)[ ]*([a-zA-Z0-9_]+).*" "\\1" module_basename "${module_basename}" )
  362. else ()
  363. # fallback to file basename
  364. get_filename_component(module_basename "${infile}" NAME_WE)
  365. endif ()
  366. endif ()
  367. endif()
  368. foreach(it ${SWIG_${language}_EXTRA_FILE_EXTENSIONS})
  369. set(extra_file "${generatedpath}/${module_basename}${it}")
  370. if (extra_file MATCHES "\\.cs$" AND CMAKE_CSharp_COMPILER_LOADED)
  371. set_source_files_properties(${extra_file} PROPERTIES LANGUAGE "CSharp")
  372. else()
  373. # Treat extra outputs as plain files regardless of language.
  374. set_source_files_properties(${extra_file} PROPERTIES LANGUAGE "")
  375. endif()
  376. list(APPEND files "${extra_file}")
  377. endforeach()
  378. if (language STREQUAL "FORTRAN" AND CMAKE_Fortran_COMPILER_LOADED)
  379. # Process possible user-supplied extension in flags (obtained via parent
  380. # scope variable) to determine the source file name.
  381. list(FIND SWIG_COMPILATION_FLAGS "-fext" fext_idx)
  382. if (fext_idx EQUAL -1)
  383. # Default Fortran generated extension
  384. set(fext "f90")
  385. else()
  386. # Get extension from user-provided flag
  387. math(EXPR fext_idx "${fext_idx} + 1")
  388. list(GET SWIG_COMPILATION_FLAGS "${fext_idx}" fext)
  389. endif()
  390. set(extra_file "${generatedpath}/${module_basename}.${fext}")
  391. set_source_files_properties("${extra_file}" PROPERTIES LANGUAGE "Fortran")
  392. list(APPEND files "${extra_file}")
  393. endif()
  394. set (${outfiles} ${files} PARENT_SCOPE)
  395. endfunction()
  396. #
  397. # Take swig (*.i) file and add proper custom commands for it
  398. #
  399. function(SWIG_ADD_SOURCE_TO_MODULE name outfiles infile)
  400. get_filename_component(swig_source_file_name_we "${infile}" NAME_WE)
  401. get_source_file_property(swig_source_file_cplusplus "${infile}" CPLUSPLUS)
  402. get_source_file_property(swig_source_file_outdir "${infile}" OUTPUT_DIR)
  403. get_source_file_property(swig_source_file_outfiledir "${infile}" OUTFILE_DIR)
  404. if (swig_source_file_outdir)
  405. # use source file property
  406. set(outdir "${swig_source_file_outdir}")
  407. if (NOT swig_source_file_outfiledir)
  408. set (swig_source_file_outfiledir "${outdir}")
  409. endif()
  410. elseif(CMAKE_SWIG_OUTDIR)
  411. set(outdir ${CMAKE_SWIG_OUTDIR})
  412. else()
  413. set(outdir ${CMAKE_CURRENT_BINARY_DIR})
  414. endif()
  415. if (swig_source_file_outfiledir)
  416. set (outfiledir "${swig_source_file_outfiledir}")
  417. elseif(SWIG_OUTFILE_DIR)
  418. set(outfiledir ${SWIG_OUTFILE_DIR})
  419. else()
  420. set(outfiledir ${outdir})
  421. endif()
  422. if(SWIG_WORKING_DIR)
  423. set (workingdir "${SWIG_WORKING_DIR}")
  424. else()
  425. set(workingdir "${outdir}")
  426. endif()
  427. if(SWIG_TARGET_NAME)
  428. set(target_name ${SWIG_TARGET_NAME})
  429. else()
  430. set(target_name ${name})
  431. endif()
  432. set (use_swig_dependencies ${SWIG_USE_SWIG_DEPENDENCIES})
  433. if (CMAKE_GENERATOR MATCHES "Make|Ninja|Xcode|Visual Studio (1[1-9]|[2-9][0-9])")
  434. get_property(use_swig_dependencies_set SOURCE "${infile}" PROPERTY USE_SWIG_DEPENDENCIES SET)
  435. if (use_swig_dependencies_set)
  436. get_property(use_swig_dependencies SOURCE "${infile}" PROPERTY USE_SWIG_DEPENDENCIES)
  437. endif()
  438. endif()
  439. set (swig_source_file_flags ${CMAKE_SWIG_FLAGS})
  440. # handle various swig compile flags properties
  441. get_source_file_property (include_directories "${infile}" INCLUDE_DIRECTORIES)
  442. if (include_directories)
  443. list (APPEND swig_source_file_flags "$<$<BOOL:${include_directories}>:-I$<JOIN:${include_directories},$<SEMICOLON>-I>>")
  444. endif()
  445. set (property "$<TARGET_PROPERTY:${target_name},SWIG_INCLUDE_DIRECTORIES>")
  446. list (APPEND swig_source_file_flags "$<$<BOOL:${property}>:-I$<JOIN:$<TARGET_GENEX_EVAL:${target_name},${property}>,$<SEMICOLON>-I>>")
  447. set (property "$<REMOVE_DUPLICATES:$<TARGET_PROPERTY:${target_name},INCLUDE_DIRECTORIES>>")
  448. get_source_file_property(use_target_include_dirs "${infile}" USE_TARGET_INCLUDE_DIRECTORIES)
  449. if (use_target_include_dirs)
  450. list (APPEND swig_source_file_flags "$<$<BOOL:${property}>:-I$<JOIN:${property},$<SEMICOLON>-I>>")
  451. elseif(use_target_include_dirs STREQUAL "NOTFOUND")
  452. # not defined at source level, rely on target level
  453. list (APPEND swig_source_file_flags "$<$<AND:$<BOOL:$<TARGET_PROPERTY:${target_name},SWIG_USE_TARGET_INCLUDE_DIRECTORIES>>,$<BOOL:${property}>>:-I$<JOIN:${property},$<SEMICOLON>-I>>")
  454. endif()
  455. set (property "$<TARGET_PROPERTY:${target_name},SWIG_COMPILE_DEFINITIONS>")
  456. list (APPEND swig_source_file_flags "$<$<BOOL:${property}>:-D$<JOIN:$<TARGET_GENEX_EVAL:${target_name},${property}>,$<SEMICOLON>-D>>")
  457. get_source_file_property (compile_definitions "${infile}" COMPILE_DEFINITIONS)
  458. if (compile_definitions)
  459. list (APPEND swig_source_file_flags "$<$<BOOL:${compile_definitions}>:-D$<JOIN:${compile_definitions},$<SEMICOLON>-D>>")
  460. endif()
  461. list (APPEND swig_source_file_flags "$<TARGET_GENEX_EVAL:${target_name},$<TARGET_PROPERTY:${target_name},SWIG_COMPILE_OPTIONS>>")
  462. get_source_file_property (compile_options "${infile}" COMPILE_OPTIONS)
  463. if (compile_options)
  464. list (APPEND swig_source_file_flags ${compile_options})
  465. endif()
  466. # legacy support
  467. get_source_file_property (swig_flags "${infile}" SWIG_FLAGS)
  468. if (swig_flags)
  469. list (APPEND swig_source_file_flags ${swig_flags})
  470. endif()
  471. get_filename_component(swig_source_file_fullname "${infile}" ABSOLUTE)
  472. if (NOT SWIG_MODULE_${name}_NOPROXY)
  473. set(SWIG_COMPILATION_FLAGS ${swig_source_file_flags})
  474. SWIG_GET_EXTRA_OUTPUT_FILES(${SWIG_MODULE_${name}_LANGUAGE}
  475. swig_extra_generated_files
  476. "${outdir}"
  477. "${swig_source_file_fullname}")
  478. endif()
  479. set(swig_generated_file_fullname
  480. "${outfiledir}/${swig_source_file_name_we}")
  481. # add the language into the name of the file (i.e. TCL_wrap)
  482. # this allows for the same .i file to be wrapped into different languages
  483. string(APPEND swig_generated_file_fullname
  484. "${SWIG_MODULE_${name}_LANGUAGE}_wrap")
  485. if(swig_source_file_cplusplus)
  486. string(APPEND swig_generated_file_fullname
  487. ".${SWIG_CXX_EXTENSION}")
  488. else()
  489. string(APPEND swig_generated_file_fullname
  490. ".c")
  491. endif()
  492. get_directory_property (cmake_include_directories INCLUDE_DIRECTORIES)
  493. list (REMOVE_DUPLICATES cmake_include_directories)
  494. set (swig_include_dirs)
  495. if (cmake_include_directories)
  496. set (swig_include_dirs "$<$<BOOL:${cmake_include_directories}>:-I$<JOIN:${cmake_include_directories},$<SEMICOLON>-I>>")
  497. endif()
  498. set(swig_special_flags)
  499. # default is c, so add c++ flag if it is c++
  500. if(swig_source_file_cplusplus)
  501. list (APPEND swig_special_flags "-c++")
  502. endif()
  503. cmake_policy(GET CMP0086 module_name_policy)
  504. if (module_name_policy STREQUAL "NEW")
  505. get_source_file_property(module_name "${infile}" SWIG_MODULE_NAME)
  506. if (module_name)
  507. list (APPEND swig_special_flags "-module" "${module_name}")
  508. endif()
  509. else()
  510. if (NOT module_name_policy)
  511. cmake_policy(GET_WARNING CMP0086 _cmp0086_warning)
  512. message(AUTHOR_WARNING "${_cmp0086_warning}\n")
  513. endif()
  514. endif()
  515. set (swig_extra_flags)
  516. if(SWIG_MODULE_${name}_LANGUAGE STREQUAL "CSHARP")
  517. if(NOT ("-dllimport" IN_LIST swig_source_file_flags OR "-dllimport" IN_LIST SWIG_MODULE_${name}_EXTRA_FLAGS))
  518. # This makes sure that the name used in the generated DllImport
  519. # matches the library name created by CMake
  520. list (APPEND SWIG_MODULE_${name}_EXTRA_FLAGS "-dllimport" "$<TARGET_FILE_BASE_NAME:${target_name}>")
  521. endif()
  522. endif()
  523. if (SWIG_MODULE_${name}_LANGUAGE STREQUAL "PYTHON" AND NOT SWIG_MODULE_${name}_NOPROXY)
  524. if(SWIG_USE_INTERFACE AND
  525. NOT ("-interface" IN_LIST swig_source_file_flags OR "-interface" IN_LIST SWIG_MODULE_${name}_EXTRA_FLAGS))
  526. # This makes sure that the name used in the proxy code
  527. # matches the library name created by CMake
  528. list (APPEND SWIG_MODULE_${name}_EXTRA_FLAGS "-interface" "$<TARGET_FILE_PREFIX:${target_name}>$<TARGET_FILE_BASE_NAME:${target_name}>")
  529. endif()
  530. endif()
  531. list (APPEND swig_extra_flags ${SWIG_MODULE_${name}_EXTRA_FLAGS})
  532. # dependencies
  533. set (swig_dependencies DEPENDS ${SWIG_MODULE_${name}_EXTRA_DEPS} $<TARGET_PROPERTY:${target_name},SWIG_DEPENDS>)
  534. get_source_file_property(file_depends "${infile}" DEPENDS)
  535. if (file_depends)
  536. list (APPEND swig_dependencies ${file_depends})
  537. endif()
  538. if (UseSWIG_MODULE_VERSION VERSION_GREATER 1)
  539. # as part of custom command, start by removing old generated files
  540. # to ensure obsolete files do not stay
  541. set (swig_file_outdir "${workingdir}/${swig_source_file_name_we}.files")
  542. set (swig_cleanup_command COMMAND "${CMAKE_COMMAND}" "-DSUPPORT_FILES_WORKING_DIRECTORY=${swig_file_outdir}" "-DSUPPORT_FILES_OUTPUT_DIRECTORY=${outdir}" -DACTION=CLEAN -P "${SWIG_MANAGE_SUPPORT_FILES_SCRIPT}")
  543. set (swig_copy_command COMMAND "${CMAKE_COMMAND}" "-DSUPPORT_FILES_WORKING_DIRECTORY=${swig_file_outdir}" "-DSUPPORT_FILES_OUTPUT_DIRECTORY=${outdir}" -DACTION=COPY -P "${SWIG_MANAGE_SUPPORT_FILES_SCRIPT}")
  544. else()
  545. set (swig_file_outdir "${outdir}")
  546. unset (swig_cleanup_command)
  547. unset (swig_copy_command)
  548. endif()
  549. set(swig_depends_flags)
  550. if(NOT use_swig_dependencies AND CMAKE_GENERATOR MATCHES "Make")
  551. # IMPLICIT_DEPENDS can not handle situations where a dependent file is
  552. # removed. We need an extra step with timestamp and custom target, see #16830
  553. # As this is needed only for Makefile generator do it conditionally
  554. __swig_compute_timestamp(${name} ${SWIG_MODULE_${name}_LANGUAGE}
  555. "${infile}" "${workingdir}" swig_generated_timestamp)
  556. set(swig_custom_output "${swig_generated_timestamp}")
  557. set(swig_custom_products
  558. BYPRODUCTS "${swig_generated_file_fullname}" ${swig_extra_generated_files})
  559. set(swig_timestamp_command
  560. COMMAND ${CMAKE_COMMAND} -E touch "${swig_generated_timestamp}")
  561. list(APPEND swig_dependencies IMPLICIT_DEPENDS CXX "${swig_source_file_fullname}")
  562. else()
  563. set(swig_generated_timestamp)
  564. set(swig_custom_output
  565. "${swig_generated_file_fullname}" ${swig_extra_generated_files})
  566. set(swig_custom_products)
  567. set(swig_timestamp_command)
  568. if (use_swig_dependencies)
  569. cmake_path(GET infile FILENAME swig_depends_filename)
  570. set(swig_depends_filename "${workingdir}/${swig_depends_filename}.d")
  571. list(APPEND swig_dependencies DEPFILE "${swig_depends_filename}")
  572. set(swig_depends_flags -MF "${swig_depends_filename}" -MD)
  573. endif()
  574. endif()
  575. add_custom_command(
  576. OUTPUT ${swig_custom_output}
  577. ${swig_custom_products}
  578. ${swig_cleanup_command}
  579. # Let's create the ${outdir} at execution time, in case dir contains $(OutDir)
  580. COMMAND "${CMAKE_COMMAND}" -E make_directory "${workingdir}" "${outdir}" "${outfiledir}"
  581. ${swig_timestamp_command}
  582. COMMAND "${CMAKE_COMMAND}" -E env "SWIG_LIB=${SWIG_DIR}" "${SWIG_EXECUTABLE}"
  583. "-${SWIG_MODULE_${name}_SWIG_LANGUAGE_FLAG}"
  584. "${swig_source_file_flags}"
  585. -outdir "${swig_file_outdir}"
  586. ${swig_special_flags}
  587. ${swig_extra_flags}
  588. ${swig_depends_flags}
  589. "${swig_include_dirs}"
  590. -o "${swig_generated_file_fullname}"
  591. "${swig_source_file_fullname}"
  592. ${swig_copy_command}
  593. MAIN_DEPENDENCY "${swig_source_file_fullname}"
  594. ${swig_dependencies}
  595. COMMENT "Swig compile ${infile} for ${SWIG_MODULE_${name}_SWIG_LANGUAGE_FLAG}"
  596. COMMAND_EXPAND_LISTS)
  597. set_source_files_properties("${swig_generated_file_fullname}" ${swig_extra_generated_files}
  598. PROPERTIES GENERATED 1)
  599. ## add all properties for generated file to various properties
  600. get_property (include_directories SOURCE "${infile}" PROPERTY GENERATED_INCLUDE_DIRECTORIES)
  601. set_property (SOURCE "${swig_generated_file_fullname}" PROPERTY INCLUDE_DIRECTORIES ${include_directories} $<TARGET_GENEX_EVAL:${target_name},$<TARGET_PROPERTY:${target_name},SWIG_GENERATED_INCLUDE_DIRECTORIES>>)
  602. get_property (compile_definitions SOURCE "${infile}" PROPERTY GENERATED_COMPILE_DEFINITIONS)
  603. set_property (SOURCE "${swig_generated_file_fullname}" PROPERTY COMPILE_DEFINITIONS $<TARGET_GENEX_EVAL:${target_name},$<TARGET_PROPERTY:${target_name},SWIG_GENERATED_COMPILE_DEFINITIONS>> ${compile_definitions})
  604. get_property (compile_options SOURCE "${infile}" PROPERTY GENERATED_COMPILE_OPTIONS)
  605. set_property (SOURCE "${swig_generated_file_fullname}" PROPERTY COMPILE_OPTIONS $<TARGET_GENEX_EVAL:${target_name},$<TARGET_PROPERTY:${target_name},SWIG_GENERATED_COMPILE_OPTIONS>> ${compile_options})
  606. if (SWIG_MODULE_${name}_SWIG_LANGUAGE_FLAG MATCHES "php")
  607. set_property (SOURCE "${swig_generated_file_fullname}" APPEND PROPERTY INCLUDE_DIRECTORIES "${outdir}")
  608. endif()
  609. set(${outfiles} "${swig_generated_file_fullname}" ${swig_extra_generated_files} PARENT_SCOPE)
  610. set(swig_timestamp "${swig_generated_timestamp}" PARENT_SCOPE)
  611. # legacy support
  612. set (swig_generated_file_fullname "${swig_generated_file_fullname}" PARENT_SCOPE)
  613. endfunction()
  614. #
  615. # Create Swig module
  616. #
  617. macro(SWIG_ADD_MODULE name language)
  618. message(DEPRECATION "SWIG_ADD_MODULE is deprecated. Use SWIG_ADD_LIBRARY instead.")
  619. swig_add_library(${name}
  620. LANGUAGE ${language}
  621. TYPE MODULE
  622. SOURCES ${ARGN})
  623. endmacro()
  624. function(SWIG_ADD_LIBRARY name)
  625. set(options NO_PROXY)
  626. set(oneValueArgs LANGUAGE
  627. TYPE
  628. OUTPUT_DIR
  629. OUTFILE_DIR)
  630. set(multiValueArgs SOURCES)
  631. cmake_parse_arguments(_SAM "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
  632. if (_SAM_UNPARSED_ARGUMENTS)
  633. message(FATAL_ERROR "SWIG_ADD_LIBRARY: ${_SAM_UNPARSED_ARGUMENTS}: unexpected arguments")
  634. endif()
  635. if(NOT DEFINED _SAM_LANGUAGE)
  636. message(FATAL_ERROR "SWIG_ADD_LIBRARY: Missing LANGUAGE argument")
  637. endif()
  638. if(NOT DEFINED _SAM_SOURCES)
  639. message(FATAL_ERROR "SWIG_ADD_LIBRARY: Missing SOURCES argument")
  640. endif()
  641. if(NOT DEFINED _SAM_TYPE)
  642. set(_SAM_TYPE MODULE)
  643. elseif(_SAM_TYPE STREQUAL "USE_BUILD_SHARED_LIBS")
  644. unset(_SAM_TYPE)
  645. endif()
  646. cmake_policy(GET CMP0078 target_name_policy)
  647. if (target_name_policy STREQUAL "NEW")
  648. set (UseSWIG_TARGET_NAME_PREFERENCE STANDARD)
  649. else()
  650. if (NOT target_name_policy)
  651. cmake_policy(GET_WARNING CMP0078 _cmp0078_warning)
  652. message(AUTHOR_WARNING "${_cmp0078_warning}\n")
  653. endif()
  654. if (NOT DEFINED UseSWIG_TARGET_NAME_PREFERENCE)
  655. set (UseSWIG_TARGET_NAME_PREFERENCE LEGACY)
  656. elseif (NOT UseSWIG_TARGET_NAME_PREFERENCE MATCHES "^(LEGACY|STANDARD)$")
  657. message (FATAL_ERROR "UseSWIG_TARGET_NAME_PREFERENCE: ${UseSWIG_TARGET_NAME_PREFERENCE}: invalid value. 'LEGACY' or 'STANDARD' is expected.")
  658. endif()
  659. endif()
  660. if (NOT DEFINED UseSWIG_MODULE_VERSION)
  661. set (UseSWIG_MODULE_VERSION 1)
  662. elseif (NOT UseSWIG_MODULE_VERSION MATCHES "^(1|2)$")
  663. message (FATAL_ERROR "UseSWIG_MODULE_VERSION: ${UseSWIG_MODULE_VERSION}: invalid value. 1 or 2 is expected.")
  664. endif()
  665. set (SWIG_MODULE_${name}_NOPROXY ${_SAM_NO_PROXY})
  666. swig_module_initialize(${name} ${_SAM_LANGUAGE})
  667. # compute real target name.
  668. if (UseSWIG_TARGET_NAME_PREFERENCE STREQUAL "LEGACY" AND
  669. SWIG_MODULE_${name}_LANGUAGE STREQUAL "PYTHON" AND NOT SWIG_MODULE_${name}_NOPROXY)
  670. # swig will produce a module.py containing an 'import _modulename' statement,
  671. # which implies having a corresponding _modulename.so (*NIX), _modulename.pyd (Win32),
  672. # unless the -noproxy flag is used
  673. set(target_name "_${name}")
  674. else()
  675. set(target_name "${name}")
  676. endif()
  677. if (TARGET ${target_name})
  678. # a target with same name is already defined.
  679. # call NOW add_library command to raise the most useful error message
  680. add_library(${target_name})
  681. return()
  682. endif()
  683. set (workingdir "${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/${target_name}.dir")
  684. # set special variable to pass extra information to command SWIG_ADD_SOURCE_TO_MODULE
  685. # which cannot be changed due to legacy compatibility
  686. set (SWIG_WORKING_DIR "${workingdir}")
  687. set (SWIG_TARGET_NAME "${target_name}")
  688. set (outputdir "${_SAM_OUTPUT_DIR}")
  689. if (NOT _SAM_OUTPUT_DIR)
  690. if (CMAKE_SWIG_OUTDIR)
  691. set (outputdir "${CMAKE_SWIG_OUTDIR}")
  692. else()
  693. if (UseSWIG_MODULE_VERSION VERSION_GREATER 1)
  694. set (outputdir "${workingdir}/${_SAM_LANGUAGE}.files")
  695. else()
  696. set (outputdir "${CMAKE_CURRENT_BINARY_DIR}")
  697. endif()
  698. endif()
  699. endif()
  700. set (outfiledir "${_SAM_OUTFILE_DIR}")
  701. if(NOT _SAM_OUTFILE_DIR)
  702. if (SWIG_OUTFILE_DIR)
  703. set (outfiledir "${SWIG_OUTFILE_DIR}")
  704. else()
  705. if (_SAM_OUTPUT_DIR OR CMAKE_SWIG_OUTDIR)
  706. set (outfiledir "${outputdir}")
  707. else()
  708. set (outfiledir "${workingdir}")
  709. endif()
  710. endif()
  711. endif()
  712. # set again, locally, predefined variables to ensure compatibility
  713. # with command SWIG_ADD_SOURCE_TO_MODULE
  714. set(CMAKE_SWIG_OUTDIR "${outputdir}")
  715. set(SWIG_OUTFILE_DIR "${outfiledir}")
  716. # See if the user has specified source extensions for swig files?
  717. if (NOT DEFINED SWIG_SOURCE_FILE_EXTENSIONS)
  718. # Assume the default (*.i) file extension for Swig source files
  719. set(SWIG_SOURCE_FILE_EXTENSIONS ".i")
  720. endif()
  721. if (CMAKE_GENERATOR MATCHES "Make|Ninja|Xcode|Visual Studio (1[1-9]|[2-9][0-9])")
  722. # For Makefiles, Ninja, Xcode and Visual Studio generators,
  723. # use SWIG generated dependencies if requested
  724. if (NOT DEFINED SWIG_USE_SWIG_DEPENDENCIES)
  725. set (SWIG_USE_SWIG_DEPENDENCIES OFF)
  726. endif()
  727. else()
  728. set (SWIG_USE_SWIG_DEPENDENCIES OFF)
  729. endif()
  730. # Generate a regex out of file extensions.
  731. string(REGEX REPLACE "([$^.*+?|()-])" "\\\\\\1" swig_source_ext_regex "${SWIG_SOURCE_FILE_EXTENSIONS}")
  732. list (JOIN swig_source_ext_regex "|" swig_source_ext_regex)
  733. string (PREPEND swig_source_ext_regex "(")
  734. string (APPEND swig_source_ext_regex ")$")
  735. set(swig_dot_i_sources ${_SAM_SOURCES})
  736. list(FILTER swig_dot_i_sources INCLUDE REGEX ${swig_source_ext_regex})
  737. if (NOT swig_dot_i_sources)
  738. message(FATAL_ERROR "SWIG_ADD_LIBRARY: no SWIG interface files specified")
  739. endif()
  740. set(swig_other_sources ${_SAM_SOURCES})
  741. list(REMOVE_ITEM swig_other_sources ${swig_dot_i_sources})
  742. set(swig_generated_sources)
  743. set(swig_generated_timestamps)
  744. set(swig_generated_outdirs "${outputdir}")
  745. list(LENGTH swig_dot_i_sources swig_sources_count)
  746. if (swig_sources_count GREATER "1")
  747. # option -interface cannot be used
  748. set(SWIG_USE_INTERFACE FALSE)
  749. else()
  750. set(SWIG_USE_INTERFACE TRUE)
  751. endif()
  752. foreach(swig_it IN LISTS swig_dot_i_sources)
  753. SWIG_ADD_SOURCE_TO_MODULE(${name} swig_generated_source "${swig_it}")
  754. list (APPEND swig_generated_sources "${swig_generated_source}")
  755. if(swig_timestamp)
  756. list (APPEND swig_generated_timestamps "${swig_timestamp}")
  757. endif()
  758. get_source_file_property(swig_source_file_outdir "${swig_it}" OUTPUT_DIR)
  759. if (swig_source_file_outdir)
  760. list (APPEND swig_generated_outdirs "${swig_source_file_outdir}")
  761. endif()
  762. endforeach()
  763. list(REMOVE_DUPLICATES swig_generated_outdirs)
  764. set_property (DIRECTORY APPEND PROPERTY
  765. ADDITIONAL_CLEAN_FILES ${swig_generated_sources} ${swig_generated_timestamps})
  766. if (UseSWIG_MODULE_VERSION VERSION_GREATER 1)
  767. set_property (DIRECTORY APPEND PROPERTY ADDITIONAL_CLEAN_FILES ${swig_generated_outdirs})
  768. endif()
  769. add_library(${target_name}
  770. ${_SAM_TYPE}
  771. ${swig_generated_sources}
  772. ${swig_other_sources})
  773. if(swig_generated_timestamps)
  774. # see IMPLICIT_DEPENDS above
  775. add_custom_target(${name}_swig_compilation DEPENDS ${swig_generated_timestamps})
  776. add_dependencies(${target_name} ${name}_swig_compilation)
  777. endif()
  778. if(_SAM_TYPE STREQUAL "MODULE")
  779. set_target_properties(${target_name} PROPERTIES NO_SONAME ON)
  780. endif()
  781. string(TOLOWER "${_SAM_LANGUAGE}" swig_lowercase_language)
  782. if (swig_lowercase_language STREQUAL "octave")
  783. set_target_properties(${target_name} PROPERTIES PREFIX "")
  784. set_target_properties(${target_name} PROPERTIES SUFFIX ".oct")
  785. elseif (swig_lowercase_language STREQUAL "go")
  786. set_target_properties(${target_name} PROPERTIES PREFIX "")
  787. elseif (swig_lowercase_language STREQUAL "java")
  788. # In java you want:
  789. # System.loadLibrary("LIBRARY");
  790. # then JNI will look for a library whose name is platform dependent, namely
  791. # MacOS : libLIBRARY.jnilib
  792. # Windows: LIBRARY.dll
  793. # Linux : libLIBRARY.so
  794. if (APPLE)
  795. set_target_properties (${target_name} PROPERTIES SUFFIX ".jnilib")
  796. endif()
  797. if ((WIN32 AND MINGW) OR CYGWIN OR CMAKE_SYSTEM_NAME STREQUAL "MSYS")
  798. set_target_properties(${target_name} PROPERTIES PREFIX "")
  799. endif()
  800. elseif (swig_lowercase_language STREQUAL "lua")
  801. if(_SAM_TYPE STREQUAL "MODULE")
  802. set_target_properties(${target_name} PROPERTIES PREFIX "")
  803. endif()
  804. elseif (swig_lowercase_language STREQUAL "python")
  805. if (UseSWIG_TARGET_NAME_PREFERENCE STREQUAL "STANDARD" AND NOT SWIG_MODULE_${name}_NOPROXY)
  806. # swig will produce a module.py containing an 'import _modulename' statement,
  807. # which implies having a corresponding _modulename.so (*NIX), _modulename.pyd (Win32),
  808. # unless the -noproxy flag is used
  809. set_target_properties(${target_name} PROPERTIES PREFIX "_")
  810. else()
  811. set_target_properties(${target_name} PROPERTIES PREFIX "")
  812. endif()
  813. # Python extension modules on Windows must have the extension ".pyd"
  814. # instead of ".dll" as of Python 2.5. Older python versions do support
  815. # this suffix.
  816. # http://docs.python.org/whatsnew/ports.html#SECTION0001510000000000000000
  817. # <quote>
  818. # Windows: .dll is no longer supported as a filename extension for extension modules.
  819. # .pyd is now the only filename extension that will be searched for.
  820. # </quote>
  821. if(WIN32 AND NOT CYGWIN)
  822. set_target_properties(${target_name} PROPERTIES SUFFIX ".pyd")
  823. endif()
  824. elseif (swig_lowercase_language STREQUAL "r")
  825. set_target_properties(${target_name} PROPERTIES PREFIX "")
  826. elseif (swig_lowercase_language STREQUAL "ruby")
  827. # In ruby you want:
  828. # require 'LIBRARY'
  829. # then ruby will look for a library whose name is platform dependent, namely
  830. # MacOS : LIBRARY.bundle
  831. # Windows: LIBRARY.dll
  832. # Linux : LIBRARY.so
  833. set_target_properties (${target_name} PROPERTIES PREFIX "")
  834. if (APPLE)
  835. set_target_properties (${target_name} PROPERTIES SUFFIX ".bundle")
  836. endif ()
  837. elseif (swig_lowercase_language STREQUAL "perl" OR swig_lowercase_language STREQUAL "perl5")
  838. # assume empty prefix because we expect the module to be dynamically loaded
  839. set_target_properties (${target_name} PROPERTIES PREFIX "")
  840. if (APPLE)
  841. set_target_properties (${target_name} PROPERTIES SUFFIX ".dylib")
  842. endif ()
  843. elseif (swig_lowercase_language STREQUAL "fortran")
  844. # Do *not* override the target's library prefix
  845. elseif (swig_lowercase_language STREQUAL "csharp")
  846. cmake_policy(GET CMP0122 csharp_naming_policy)
  847. if (csharp_naming_policy STREQUAL "NEW")
  848. # Do *not* override the target's library prefix
  849. else()
  850. if (NOT csharp_naming_policy)
  851. cmake_policy(GET_WARNING CMP0122 _cmp0122_warning)
  852. message(AUTHOR_WARNING "${_cmp0122_warning}\n")
  853. endif()
  854. set_target_properties (${target_name} PROPERTIES PREFIX "")
  855. endif()
  856. if (APPLE)
  857. set_target_properties (${target_name} PROPERTIES SUFFIX ".dylib")
  858. endif ()
  859. else()
  860. # assume empty prefix because we expect the module to be dynamically loaded
  861. set_target_properties (${target_name} PROPERTIES PREFIX "")
  862. endif ()
  863. # target property SWIG_SUPPORT_FILES_DIRECTORY specify output directories of support files
  864. set_property (TARGET ${target_name} PROPERTY SWIG_SUPPORT_FILES_DIRECTORY ${swig_generated_outdirs})
  865. # target property SWIG_SUPPORT_FILES lists principal proxy support files
  866. if (NOT SWIG_MODULE_${name}_NOPROXY)
  867. string(TOUPPER "${_SAM_LANGUAGE}" swig_uppercase_language)
  868. set(swig_all_support_files)
  869. foreach (swig_it IN LISTS SWIG_${swig_uppercase_language}_EXTRA_FILE_EXTENSIONS)
  870. set (swig_support_files ${swig_generated_sources})
  871. list (FILTER swig_support_files INCLUDE REGEX ".*${swig_it}$")
  872. list(APPEND swig_all_support_files ${swig_support_files})
  873. endforeach()
  874. if (swig_all_support_files)
  875. list(REMOVE_DUPLICATES swig_all_support_files)
  876. endif()
  877. set_property (TARGET ${target_name} PROPERTY SWIG_SUPPORT_FILES ${swig_all_support_files})
  878. endif()
  879. # to ensure legacy behavior, export some variables
  880. set (SWIG_MODULE_${name}_LANGUAGE "${SWIG_MODULE_${name}_LANGUAGE}" PARENT_SCOPE)
  881. set (SWIG_MODULE_${name}_SWIG_LANGUAGE_FLAG "${SWIG_MODULE_${name}_SWIG_LANGUAGE_FLAG}" PARENT_SCOPE)
  882. set (SWIG_MODULE_${name}_REAL_NAME "${target_name}" PARENT_SCOPE)
  883. set (SWIG_MODULE_${name}_NOPROXY "${SWIG_MODULE_${name}_NOPROXY}" PARENT_SCOPE)
  884. set (SWIG_MODULE_${name}_EXTRA_FLAGS "${SWIG_MODULE_${name}_EXTRA_FLAGS}" PARENT_SCOPE)
  885. # the last one is a bit crazy but it is documented, so...
  886. # NOTA: works as expected if only ONE input file is specified
  887. set (swig_generated_file_fullname "${swig_generated_file_fullname}" PARENT_SCOPE)
  888. endfunction()
  889. #
  890. # Like TARGET_LINK_LIBRARIES but for swig modules
  891. #
  892. function(SWIG_LINK_LIBRARIES name)
  893. if (UseSWIG_TARGET_NAME_PREFERENCE STREQUAL "STANDARD")
  894. message(DEPRECATION "SWIG_LINK_LIBRARIES is deprecated. Use TARGET_LINK_LIBRARIES instead.")
  895. target_link_libraries(${name} ${ARGN})
  896. else()
  897. if(SWIG_MODULE_${name}_REAL_NAME)
  898. target_link_libraries(${SWIG_MODULE_${name}_REAL_NAME} ${ARGN})
  899. else()
  900. message(SEND_ERROR "Cannot find Swig library \"${name}\".")
  901. endif()
  902. endif()
  903. endfunction()
  904. cmake_policy(POP)