UseSWIG.cmake 41 KB

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