UseSWIG.cmake 34 KB

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