UseSWIG.cmake 40 KB

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