You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

783 lines
32 KiB

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