1 # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
2 # file Copyright.txt or https://cmake.org/licensing for details.
4 #[=======================================================================[.rst:
8 This file provides support for ``SWIG``. It is assumed that :module:`FindSWIG`
9 module has already been loaded.
11 Defines the following command for use with ``SWIG``:
13 .. command:: swig_add_library
17 Define swig module with given name and specified language::
19 swig_add_library(<name>
20 [TYPE <SHARED|MODULE|STATIC|USE_BUILD_SHARED_LIBS>]
23 [OUTPUT_DIR <directory>]
24 [OUTFILE_DIR <directory>]
28 Targets created with the ``swig_add_library`` command have the same
29 capabilities as targets created with the :command:`add_library` command, so
30 those targets can be used with any command expecting a target (e.g.
31 :command:`target_link_libraries`).
33 .. versionchanged:: 3.13
34 This command creates a target with the specified ``<name>`` when
35 policy :policy:`CMP0078` is set to ``NEW``. Otherwise, the legacy
36 behavior will choose a different target name and store it in the
37 ``SWIG_MODULE_<name>_REAL_NAME`` variable.
39 .. versionchanged:: 3.15
40 Alternate library name (set with the :prop_tgt:`OUTPUT_NAME` property,
41 for example) will be passed on to ``Python`` and ``CSharp`` wrapper
44 .. versionchanged:: 3.21
45 Generated library use standard naming conventions for ``CSharp`` language
46 when policy :policy:`CMP0122` is set to ``NEW``. Otherwise, the legacy
51 For multi-config generators, this module does not support
52 configuration-specific files generated by ``SWIG``. All build
53 configurations must result in the same generated source file.
57 For :ref:`Makefile Generators`, if, for some sources, the
58 ``USE_SWIG_DEPENDENCIES`` property is ``FALSE``, ``swig_add_library`` does
59 not track file dependencies, so depending on the ``<name>_swig_compilation``
60 custom target is required for targets which require the ``swig``-generated
61 files to exist. Other generators may depend on the source files that would
65 ``SHARED``, ``MODULE`` and ``STATIC`` have the same semantic as for the
66 :command:`add_library` command. If ``USE_BUILD_SHARED_LIBS`` is specified,
67 the library type will be ``STATIC`` or ``SHARED`` based on whether the
68 current value of the :variable:`BUILD_SHARED_LIBS` variable is ``ON``. If
69 no type is specified, ``MODULE`` will be used.
72 Specify the target language.
75 Go and Lua language support.
80 .. versionadded:: 3.18
81 Fortran language support.
84 .. versionadded:: 3.12
86 Prevent the generation of the wrapper layer (swig ``-noproxy`` option).
89 .. versionadded:: 3.12
91 Specify where to write the language specific files (swig ``-outdir``
92 option). If not given, the ``CMAKE_SWIG_OUTDIR`` variable will be used.
93 If neither is specified, the default depends on the value of the
94 ``UseSWIG_MODULE_VERSION`` variable as follows:
96 * If ``UseSWIG_MODULE_VERSION`` is 1 or is undefined, output is written to
97 the :variable:`CMAKE_CURRENT_BINARY_DIR` directory.
98 * If ``UseSWIG_MODULE_VERSION`` is 2, a dedicated directory will be used.
99 The path of this directory can be retrieved from the
100 ``SWIG_SUPPORT_FILES_DIRECTORY`` target property.
103 .. versionadded:: 3.12
105 Specify an output directory name where the generated source file will be
106 placed (swig ``-o`` option). If not specified, the ``SWIG_OUTFILE_DIR``
107 variable will be used. If neither is specified, ``OUTPUT_DIR`` or
108 ``CMAKE_SWIG_OUTDIR`` is used instead.
111 List of sources for the library. Files with extension ``.i`` will be
112 identified as sources for the ``SWIG`` tool. Other files will be handled in
115 .. versionadded:: 3.14
116 This behavior can be overridden by specifying the variable
117 ``SWIG_SOURCE_FILE_EXTENSIONS``.
121 If ``UseSWIG_MODULE_VERSION`` is set to 2, it is **strongly** recommended
122 to use a dedicated directory unique to the target when either the
123 ``OUTPUT_DIR`` option or the ``CMAKE_SWIG_OUTDIR`` variable are specified.
124 The output directory contents are erased as part of the target build, so
125 to prevent interference between targets or losing other important files,
126 each target should have its own dedicated output directory.
128 .. command:: swig_link_libraries
130 Link libraries to swig module::
132 swig_link_libraries(<name> <item>...)
134 This command has same capabilities as :command:`target_link_libraries`
139 If variable ``UseSWIG_TARGET_NAME_PREFERENCE`` is set to ``STANDARD``, this
140 command is deprecated and :command:`target_link_libraries` command must be
143 Source file properties on module files **must** be set before the invocation
144 of the ``swig_add_library`` command to specify special behavior of SWIG and
145 ensure generated files will receive the required settings.
148 Call SWIG in c++ mode. For example:
150 .. code-block:: cmake
152 set_property(SOURCE mymod.i PROPERTY CPLUSPLUS ON)
153 swig_add_library(mymod LANGUAGE python SOURCES mymod.i)
157 Replaced with the fine-grained properties that follow.
159 Pass custom flags to the SWIG executable.
161 ``INCLUDE_DIRECTORIES``, ``COMPILE_DEFINITIONS`` and ``COMPILE_OPTIONS``
162 .. versionadded:: 3.12
164 Add custom flags to SWIG compiler and have same semantic as properties
165 :prop_sf:`INCLUDE_DIRECTORIES`, :prop_sf:`COMPILE_DEFINITIONS` and
166 :prop_sf:`COMPILE_OPTIONS`.
168 ``USE_TARGET_INCLUDE_DIRECTORIES``
169 .. versionadded:: 3.13
171 If set to ``TRUE``, contents of target property
172 :prop_tgt:`INCLUDE_DIRECTORIES` will be forwarded to ``SWIG`` compiler.
173 If set to ``FALSE`` target property :prop_tgt:`INCLUDE_DIRECTORIES` will be
174 ignored. If not set, target property ``SWIG_USE_TARGET_INCLUDE_DIRECTORIES``
177 ``GENERATED_INCLUDE_DIRECTORIES``, ``GENERATED_COMPILE_DEFINITIONS`` and ``GENERATED_COMPILE_OPTIONS``
178 .. versionadded:: 3.12
180 Add custom flags to the C/C++ generated source. They will fill, respectively,
181 properties :prop_sf:`INCLUDE_DIRECTORIES`, :prop_sf:`COMPILE_DEFINITIONS` and
182 :prop_sf:`COMPILE_OPTIONS` of generated C/C++ file.
185 .. versionadded:: 3.12
187 Specify additional dependencies to the source file.
189 ``USE_SWIG_DEPENDENCIES``
190 .. versionadded:: 3.20
192 If set to ``TRUE``, implicit dependencies are generated by the ``swig`` tool
193 itself. This property is only meaningful for
194 :ref:`Makefile <Makefile Generators>`,
195 :ref:`Ninja <Ninja Generators>`, :generator:`Xcode`, and
196 :ref:`Visual Studio <Visual Studio Generators>`
197 (:generator:`Visual Studio 11 2012` and above) generators. Default value is
200 .. versionadded:: 3.21
201 Added the support of :generator:`Xcode` generator.
203 .. versionadded:: 3.22
204 Added the support of :ref:`Visual Studio Generators`.
207 Specify the actual import name of the module in the target language.
208 This is required if it cannot be scanned automatically from source
209 or different from the module file basename. For example:
211 .. code-block:: cmake
213 set_property(SOURCE mymod.i PROPERTY SWIG_MODULE_NAME mymod_realname)
215 .. versionchanged:: 3.14
216 If policy :policy:`CMP0086` is set to ``NEW``, ``-module <module_name>``
217 is passed to ``SWIG`` compiler.
220 .. versionadded:: 3.19
222 Specify where to write the language specific files (swig ``-outdir`` option)
223 for the considered source file. If not specified, the other ways to define
224 the output directory applies (see ``OUTPUT_DIR`` option of
225 ``swig_add_library()`` command).
228 .. versionadded:: 3.19
230 Specify an output directory where the generated source file will be placed
231 (swig ``-o`` option) for the considered source file. If not specified,
232 ``OUTPUT_DIR`` source property will be used. If neither are specified, the
233 other ways to define output file directory applies (see ``OUTFILE_DIR``
234 option of ``swig_add_library()`` command).
236 Target library properties can be set to apply same configuration to all SWIG
239 ``SWIG_INCLUDE_DIRECTORIES``, ``SWIG_COMPILE_DEFINITIONS`` and ``SWIG_COMPILE_OPTIONS``
240 .. versionadded:: 3.12
242 These properties will be applied to all SWIG input files and have same
243 semantic as target properties :prop_tgt:`INCLUDE_DIRECTORIES`,
244 :prop_tgt:`COMPILE_DEFINITIONS` and :prop_tgt:`COMPILE_OPTIONS`.
246 .. code-block:: cmake
248 set (UseSWIG_TARGET_NAME_PREFERENCE STANDARD)
249 swig_add_library(mymod LANGUAGE python SOURCES mymod.i)
250 set_property(TARGET mymod PROPERTY SWIG_COMPILE_DEFINITIONS MY_DEF1 MY_DEF2)
251 set_property(TARGET mymod PROPERTY SWIG_COMPILE_OPTIONS -bla -blb)
253 ``SWIG_USE_TARGET_INCLUDE_DIRECTORIES``
254 .. versionadded:: 3.13
256 If set to ``TRUE``, contents of target property
257 :prop_tgt:`INCLUDE_DIRECTORIES` will be forwarded to ``SWIG`` compiler.
258 If set to ``FALSE`` or not defined, target property
259 :prop_tgt:`INCLUDE_DIRECTORIES` will be ignored. This behavior can be
260 overridden by specifying source property ``USE_TARGET_INCLUDE_DIRECTORIES``.
262 ``SWIG_GENERATED_INCLUDE_DIRECTORIES``, ``SWIG_GENERATED_COMPILE_DEFINITIONS`` and ``SWIG_GENERATED_COMPILE_OPTIONS``
263 .. versionadded:: 3.12
265 These properties will populate, respectively, properties
266 :prop_sf:`INCLUDE_DIRECTORIES`, :prop_sf:`COMPILE_DEFINITIONS` and
267 :prop_sf:`COMPILE_FLAGS` of all generated C/C++ files.
270 .. versionadded:: 3.12
272 Add dependencies to all SWIG input files.
274 The following target properties are output properties and can be used to get
275 information about support files generated by ``SWIG`` interface compilation.
277 ``SWIG_SUPPORT_FILES``
278 .. versionadded:: 3.12
280 This output property list of wrapper files generated during SWIG compilation.
282 .. code-block:: cmake
284 set (UseSWIG_TARGET_NAME_PREFERENCE STANDARD)
285 swig_add_library(mymod LANGUAGE python SOURCES mymod.i)
286 get_property(support_files TARGET mymod PROPERTY SWIG_SUPPORT_FILES)
290 Only most principal support files are listed. In case some advanced
291 features of ``SWIG`` are used (for example ``%template``), associated
292 support files may not be listed. Prefer to use the
293 ``SWIG_SUPPORT_FILES_DIRECTORY`` property to handle support files.
295 ``SWIG_SUPPORT_FILES_DIRECTORY``
296 .. versionadded:: 3.12
298 This output property specifies the directory where support files will be
303 When source property ``OUTPUT_DIR`` is defined, multiple directories can be
304 specified as part of ``SWIG_SUPPORT_FILES_DIRECTORY``.
306 Some variables can be set to customize the behavior of ``swig_add_library``
309 ``UseSWIG_MODULE_VERSION``
310 .. versionadded:: 3.12
312 Specify different behaviors for ``UseSWIG`` module.
314 * Set to 1 or undefined: Legacy behavior is applied.
315 * Set to 2: A new strategy is applied regarding support files: the output
316 directory of support files is erased before ``SWIG`` interface compilation.
319 Add flags to all swig calls.
321 ``CMAKE_SWIG_OUTDIR``
322 Specify where to write the language specific files (swig ``-outdir`` option).
325 .. versionadded:: 3.8
327 Specify an output directory name where the generated source file will be
328 placed. If not specified, ``CMAKE_SWIG_OUTDIR`` is used.
330 ``SWIG_MODULE_<name>_EXTRA_DEPS``
331 Specify extra dependencies for the generated module for ``<name>``.
333 ``SWIG_SOURCE_FILE_EXTENSIONS``
334 .. versionadded:: 3.14
336 Specify a list of source file extensions to override the default
337 behavior of considering only ``.i`` files as sources for the ``SWIG``
340 .. code-block:: cmake
342 set(SWIG_SOURCE_FILE_EXTENSIONS ".i" ".swg")
344 ``SWIG_USE_SWIG_DEPENDENCIES``
345 .. versionadded:: 3.20
347 If set to ``TRUE``, implicit dependencies are generated by the ``swig`` tool
348 itself. This variable is only meaningful for
349 :ref:`Makefile <Makefile Generators>`,
350 :ref:`Ninja <Ninja Generators>`, :generator:`Xcode`, and
351 :ref:`Visual Studio <Visual Studio Generators>`
352 (:generator:`Visual Studio 11 2012` and above) generators. Default value is
355 Source file property ``USE_SWIG_DEPENDENCIES``, if not defined, will be
356 initialized with the value of this variable.
358 .. versionadded:: 3.21
359 Added the support of :generator:`Xcode` generator.
361 .. versionadded:: 3.22
362 Added the support of :ref:`Visual Studio Generators`.
364 #]=======================================================================]
367 # numbers and boolean constants
368 cmake_policy (SET CMP0012 NEW)
370 cmake_policy (SET CMP0057 NEW)
371 # Ninja generator normalizes custom command depfile paths
372 cmake_policy (SET CMP0116 NEW)
374 set(SWIG_CXX_EXTENSION "cxx")
375 set(SWIG_EXTRA_LIBRARIES "")
377 set(SWIG_PYTHON_EXTRA_FILE_EXTENSIONS ".py")
378 set(SWIG_JAVA_EXTRA_FILE_EXTENSIONS ".java" "JNI.java")
379 set(SWIG_CSHARP_EXTRA_FILE_EXTENSIONS ".cs" "PINVOKE.cs")
381 set(SWIG_MANAGE_SUPPORT_FILES_SCRIPT "${CMAKE_CURRENT_LIST_DIR}/UseSWIG/ManageSupportFiles.cmake")
386 function (__SWIG_COMPUTE_TIMESTAMP name language infile workingdir __timestamp)
387 get_filename_component(filename "${infile}" NAME_WE)
389 "${workingdir}/${filename}${language}.stamp" PARENT_SCOPE)
390 # get_filename_component(filename "${infile}" ABSOLUTE)
391 # string(UUID uuid NAMESPACE 9735D882-D2F8-4E1D-88C9-A0A4F1F6ECA4
392 # NAME ${name}-${language}-${filename} TYPE SHA1)
393 # set(${__timestamp} "${workingdir}/${uuid}.stamp" PARENT_SCOPE)
397 # For given swig module initialize variables associated with it
399 macro(SWIG_MODULE_INITIALIZE name language)
400 string(TOUPPER "${language}" SWIG_MODULE_${name}_LANGUAGE)
401 string(TOLOWER "${language}" SWIG_MODULE_${name}_SWIG_LANGUAGE_FLAG)
403 if (NOT DEFINED SWIG_MODULE_${name}_NOPROXY)
404 set (SWIG_MODULE_${name}_NOPROXY FALSE)
406 if ("-noproxy" IN_LIST CMAKE_SWIG_FLAGS)
407 set (SWIG_MODULE_${name}_NOPROXY TRUE)
410 if (SWIG_MODULE_${name}_NOPROXY AND
411 NOT ("-noproxy" IN_LIST CMAKE_SWIG_FLAGS OR "-noproxy" IN_LIST SWIG_MODULE_${name}_EXTRA_FLAGS))
412 list (APPEND SWIG_MODULE_${name}_EXTRA_FLAGS "-noproxy")
414 if(SWIG_MODULE_${name}_LANGUAGE STREQUAL "UNKNOWN")
415 message(FATAL_ERROR "SWIG Error: Language \"${language}\" not found")
416 elseif(SWIG_MODULE_${name}_LANGUAGE STREQUAL "PERL" AND
417 NOT "-shadow" IN_LIST SWIG_MODULE_${name}_EXTRA_FLAGS)
418 list(APPEND SWIG_MODULE_${name}_EXTRA_FLAGS "-shadow")
423 # For a given language, input file, and output file, determine extra files that
424 # will be generated. This is internal swig macro.
427 function(SWIG_GET_EXTRA_OUTPUT_FILES language outfiles generatedpath infile)
429 get_source_file_property(module_basename
430 "${infile}" SWIG_MODULE_NAME)
431 if(NOT module_basename)
433 # try to get module name from "%module foo" syntax
434 if ( EXISTS "${infile}" )
435 file ( STRINGS "${infile}" module_basename REGEX "[ ]*%module[ ]*[a-zA-Z0-9_]+.*" )
437 if ( module_basename )
438 string ( REGEX REPLACE "[ ]*%module[ ]*([a-zA-Z0-9_]+).*" "\\1" module_basename "${module_basename}" )
441 # try to get module name from "%module (options=...) foo" syntax
442 if ( EXISTS "${infile}" )
443 file ( STRINGS "${infile}" module_basename REGEX "[ ]*%module[ ]*\\(.*\\)[ ]*[a-zA-Z0-9_]+.*" )
445 if ( module_basename )
446 string ( REGEX REPLACE "[ ]*%module[ ]*\\(.*\\)[ ]*([a-zA-Z0-9_]+).*" "\\1" module_basename "${module_basename}" )
449 # fallback to file basename
450 get_filename_component(module_basename "${infile}" NAME_WE)
455 foreach(it ${SWIG_${language}_EXTRA_FILE_EXTENSIONS})
456 set(extra_file "${generatedpath}/${module_basename}${it}")
457 if (extra_file MATCHES "\\.cs$" AND CMAKE_CSharp_COMPILER_LOADED)
458 set_source_files_properties(${extra_file} PROPERTIES LANGUAGE "CSharp")
460 # Treat extra outputs as plain files regardless of language.
461 set_source_files_properties(${extra_file} PROPERTIES LANGUAGE "")
463 list(APPEND files "${extra_file}")
466 if (language STREQUAL "FORTRAN" AND CMAKE_Fortran_COMPILER_LOADED)
467 # Process possible user-supplied extension in flags (obtained via parent
468 # scope variable) to determine the source file name.
469 list(FIND SWIG_COMPILATION_FLAGS "-fext" fext_idx)
470 if (fext_idx EQUAL -1)
471 # Default Fortran generated extension
474 # Get extension from user-provided flag
475 math(EXPR fext_idx "${fext_idx} + 1")
476 list(GET SWIG_COMPILATION_FLAGS "${fext_idx}" fext)
478 set(extra_file "${generatedpath}/${module_basename}.${fext}")
479 set_source_files_properties("${extra_file}" PROPERTIES LANGUAGE "Fortran")
480 list(APPEND files "${extra_file}")
483 set (${outfiles} ${files} PARENT_SCOPE)
487 # Take swig (*.i) file and add proper custom commands for it
489 function(SWIG_ADD_SOURCE_TO_MODULE name outfiles infile)
490 get_filename_component(swig_source_file_name_we "${infile}" NAME_WE)
491 get_source_file_property(swig_source_file_cplusplus "${infile}" CPLUSPLUS)
492 get_source_file_property(swig_source_file_outdir "${infile}" OUTPUT_DIR)
493 get_source_file_property(swig_source_file_outfiledir "${infile}" OUTFILE_DIR)
495 if (swig_source_file_outdir)
496 # use source file property
497 set(outdir "${swig_source_file_outdir}")
498 if (NOT swig_source_file_outfiledir)
499 set (swig_source_file_outfiledir "${outdir}")
501 elseif(CMAKE_SWIG_OUTDIR)
502 set(outdir ${CMAKE_SWIG_OUTDIR})
504 set(outdir ${CMAKE_CURRENT_BINARY_DIR})
507 if (swig_source_file_outfiledir)
508 set (outfiledir "${swig_source_file_outfiledir}")
509 elseif(SWIG_OUTFILE_DIR)
510 set(outfiledir ${SWIG_OUTFILE_DIR})
512 set(outfiledir ${outdir})
516 set (workingdir "${SWIG_WORKING_DIR}")
518 set(workingdir "${outdir}")
522 set(target_name ${SWIG_TARGET_NAME})
524 set(target_name ${name})
527 set (use_swig_dependencies ${SWIG_USE_SWIG_DEPENDENCIES})
528 if (CMAKE_GENERATOR MATCHES "Make|Ninja|Xcode|Visual Studio (1[1-9]|[2-9][0-9])")
529 get_property(use_swig_dependencies_set SOURCE "${infile}" PROPERTY USE_SWIG_DEPENDENCIES SET)
530 if (use_swig_dependencies_set)
531 get_property(use_swig_dependencies SOURCE "${infile}" PROPERTY USE_SWIG_DEPENDENCIES)
535 set (swig_source_file_flags ${CMAKE_SWIG_FLAGS})
536 # handle various swig compile flags properties
537 get_source_file_property (include_directories "${infile}" INCLUDE_DIRECTORIES)
538 if (include_directories)
539 list (APPEND swig_source_file_flags "$<$<BOOL:${include_directories}>:-I$<JOIN:${include_directories},$<SEMICOLON>-I>>")
541 set (property "$<TARGET_PROPERTY:${target_name},SWIG_INCLUDE_DIRECTORIES>")
542 list (APPEND swig_source_file_flags "$<$<BOOL:${property}>:-I$<JOIN:$<TARGET_GENEX_EVAL:${target_name},${property}>,$<SEMICOLON>-I>>")
543 set (property "$<REMOVE_DUPLICATES:$<TARGET_PROPERTY:${target_name},INCLUDE_DIRECTORIES>>")
544 get_source_file_property(use_target_include_dirs "${infile}" USE_TARGET_INCLUDE_DIRECTORIES)
545 if (use_target_include_dirs)
546 list (APPEND swig_source_file_flags "$<$<BOOL:${property}>:-I$<JOIN:${property},$<SEMICOLON>-I>>")
547 elseif(use_target_include_dirs STREQUAL "NOTFOUND")
548 # not defined at source level, rely on target level
549 list (APPEND swig_source_file_flags "$<$<AND:$<BOOL:$<TARGET_PROPERTY:${target_name},SWIG_USE_TARGET_INCLUDE_DIRECTORIES>>,$<BOOL:${property}>>:-I$<JOIN:${property},$<SEMICOLON>-I>>")
552 set (property "$<TARGET_PROPERTY:${target_name},SWIG_COMPILE_DEFINITIONS>")
553 list (APPEND swig_source_file_flags "$<$<BOOL:${property}>:-D$<JOIN:$<TARGET_GENEX_EVAL:${target_name},${property}>,$<SEMICOLON>-D>>")
554 get_source_file_property (compile_definitions "${infile}" COMPILE_DEFINITIONS)
555 if (compile_definitions)
556 list (APPEND swig_source_file_flags "$<$<BOOL:${compile_definitions}>:-D$<JOIN:${compile_definitions},$<SEMICOLON>-D>>")
559 list (APPEND swig_source_file_flags "$<TARGET_GENEX_EVAL:${target_name},$<TARGET_PROPERTY:${target_name},SWIG_COMPILE_OPTIONS>>")
560 get_source_file_property (compile_options "${infile}" COMPILE_OPTIONS)
562 list (APPEND swig_source_file_flags ${compile_options})
566 get_source_file_property (swig_flags "${infile}" SWIG_FLAGS)
568 list (APPEND swig_source_file_flags ${swig_flags})
571 get_filename_component(swig_source_file_fullname "${infile}" ABSOLUTE)
573 if (NOT SWIG_MODULE_${name}_NOPROXY)
574 set(SWIG_COMPILATION_FLAGS ${swig_source_file_flags})
575 SWIG_GET_EXTRA_OUTPUT_FILES(${SWIG_MODULE_${name}_LANGUAGE}
576 swig_extra_generated_files
578 "${swig_source_file_fullname}")
580 set(swig_generated_file_fullname
581 "${outfiledir}/${swig_source_file_name_we}")
582 # add the language into the name of the file (i.e. TCL_wrap)
583 # this allows for the same .i file to be wrapped into different languages
584 string(APPEND swig_generated_file_fullname
585 "${SWIG_MODULE_${name}_LANGUAGE}_wrap")
587 if(swig_source_file_cplusplus)
588 string(APPEND swig_generated_file_fullname
589 ".${SWIG_CXX_EXTENSION}")
591 string(APPEND swig_generated_file_fullname
595 get_directory_property (cmake_include_directories INCLUDE_DIRECTORIES)
596 list (REMOVE_DUPLICATES cmake_include_directories)
597 set (swig_include_dirs)
598 if (cmake_include_directories)
599 set (swig_include_dirs "$<$<BOOL:${cmake_include_directories}>:-I$<JOIN:${cmake_include_directories},$<SEMICOLON>-I>>")
602 set(swig_special_flags)
603 # default is c, so add c++ flag if it is c++
604 if(swig_source_file_cplusplus)
605 list (APPEND swig_special_flags "-c++")
608 cmake_policy(GET CMP0086 module_name_policy)
609 if (module_name_policy STREQUAL "NEW")
610 get_source_file_property(module_name "${infile}" SWIG_MODULE_NAME)
612 list (APPEND swig_special_flags "-module" "${module_name}")
615 if (NOT module_name_policy)
616 cmake_policy(GET_WARNING CMP0086 _cmp0086_warning)
617 message(AUTHOR_WARNING "${_cmp0086_warning}\n")
621 set (swig_extra_flags)
622 if(SWIG_MODULE_${name}_LANGUAGE STREQUAL "CSHARP")
623 if(NOT ("-dllimport" IN_LIST swig_source_file_flags OR "-dllimport" IN_LIST SWIG_MODULE_${name}_EXTRA_FLAGS))
624 # This makes sure that the name used in the generated DllImport
625 # matches the library name created by CMake
626 list (APPEND SWIG_MODULE_${name}_EXTRA_FLAGS "-dllimport" "$<TARGET_FILE_PREFIX:${target_name}>$<TARGET_FILE_BASE_NAME:${target_name}>")
629 if (SWIG_MODULE_${name}_LANGUAGE STREQUAL "PYTHON" AND NOT SWIG_MODULE_${name}_NOPROXY)
630 if(SWIG_USE_INTERFACE AND
631 NOT ("-interface" IN_LIST swig_source_file_flags OR "-interface" IN_LIST SWIG_MODULE_${name}_EXTRA_FLAGS))
632 # This makes sure that the name used in the proxy code
633 # matches the library name created by CMake
634 list (APPEND SWIG_MODULE_${name}_EXTRA_FLAGS "-interface" "$<TARGET_FILE_PREFIX:${target_name}>$<TARGET_FILE_BASE_NAME:${target_name}>")
637 list (APPEND swig_extra_flags ${SWIG_MODULE_${name}_EXTRA_FLAGS})
640 set (swig_dependencies DEPENDS ${SWIG_MODULE_${name}_EXTRA_DEPS} $<TARGET_PROPERTY:${target_name},SWIG_DEPENDS>)
641 get_source_file_property(file_depends "${infile}" DEPENDS)
643 list (APPEND swig_dependencies ${file_depends})
646 if (UseSWIG_MODULE_VERSION VERSION_GREATER 1)
647 # as part of custom command, start by removing old generated files
648 # to ensure obsolete files do not stay
649 set (swig_file_outdir "${workingdir}/${swig_source_file_name_we}.files")
650 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}")
651 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}")
653 set (swig_file_outdir "${outdir}")
654 unset (swig_cleanup_command)
655 unset (swig_copy_command)
658 set(swig_depends_flags)
659 if(NOT use_swig_dependencies AND CMAKE_GENERATOR MATCHES "Make")
660 # IMPLICIT_DEPENDS can not handle situations where a dependent file is
661 # removed. We need an extra step with timestamp and custom target, see #16830
662 # As this is needed only for Makefile generator do it conditionally
663 __swig_compute_timestamp(${name} ${SWIG_MODULE_${name}_LANGUAGE}
664 "${infile}" "${workingdir}" swig_generated_timestamp)
665 set(swig_custom_output "${swig_generated_timestamp}")
666 set(swig_custom_products
667 BYPRODUCTS "${swig_generated_file_fullname}" ${swig_extra_generated_files})
668 set(swig_timestamp_command
669 COMMAND ${CMAKE_COMMAND} -E touch "${swig_generated_timestamp}")
670 list(APPEND swig_dependencies IMPLICIT_DEPENDS CXX "${swig_source_file_fullname}")
672 set(swig_generated_timestamp)
673 set(swig_custom_output
674 "${swig_generated_file_fullname}" ${swig_extra_generated_files})
675 set(swig_custom_products)
676 set(swig_timestamp_command)
677 if (use_swig_dependencies)
678 cmake_path(GET infile FILENAME swig_depends_filename)
679 set(swig_depends_filename "${workingdir}/${swig_depends_filename}.d")
680 list(APPEND swig_dependencies DEPFILE "${swig_depends_filename}")
681 set(swig_depends_flags -MF "${swig_depends_filename}" -MD)
685 OUTPUT ${swig_custom_output}
686 ${swig_custom_products}
687 ${swig_cleanup_command}
688 # Let's create the ${outdir} at execution time, in case dir contains $(OutDir)
689 COMMAND "${CMAKE_COMMAND}" -E make_directory "${workingdir}" "${outdir}" "${outfiledir}"
690 ${swig_timestamp_command}
691 COMMAND "${CMAKE_COMMAND}" -E env "SWIG_LIB=${SWIG_DIR}" "${SWIG_EXECUTABLE}"
692 "-${SWIG_MODULE_${name}_SWIG_LANGUAGE_FLAG}"
693 "${swig_source_file_flags}"
694 -outdir "${swig_file_outdir}"
695 ${swig_special_flags}
697 ${swig_depends_flags}
698 "${swig_include_dirs}"
699 -o "${swig_generated_file_fullname}"
700 "${swig_source_file_fullname}"
702 MAIN_DEPENDENCY "${swig_source_file_fullname}"
704 COMMENT "Swig compile ${infile} for ${SWIG_MODULE_${name}_SWIG_LANGUAGE_FLAG}"
705 COMMAND_EXPAND_LISTS)
706 set_source_files_properties("${swig_generated_file_fullname}" ${swig_extra_generated_files}
707 PROPERTIES GENERATED 1)
709 ## add all properties for generated file to various properties
710 get_property (include_directories SOURCE "${infile}" PROPERTY GENERATED_INCLUDE_DIRECTORIES)
711 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>>)
713 get_property (compile_definitions SOURCE "${infile}" PROPERTY GENERATED_COMPILE_DEFINITIONS)
714 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})
716 get_property (compile_options SOURCE "${infile}" PROPERTY GENERATED_COMPILE_OPTIONS)
717 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})
719 if (SWIG_MODULE_${name}_SWIG_LANGUAGE_FLAG MATCHES "php")
720 set_property (SOURCE "${swig_generated_file_fullname}" APPEND PROPERTY INCLUDE_DIRECTORIES "${outdir}")
723 set(${outfiles} "${swig_generated_file_fullname}" ${swig_extra_generated_files} PARENT_SCOPE)
724 set(swig_timestamp "${swig_generated_timestamp}" PARENT_SCOPE)
727 set (swig_generated_file_fullname "${swig_generated_file_fullname}" PARENT_SCOPE)
733 macro(SWIG_ADD_MODULE name language)
734 message(DEPRECATION "SWIG_ADD_MODULE is deprecated. Use SWIG_ADD_LIBRARY instead.")
735 swig_add_library(${name}
742 function(SWIG_ADD_LIBRARY name)
743 set(options NO_PROXY)
744 set(oneValueArgs LANGUAGE
748 set(multiValueArgs SOURCES)
749 cmake_parse_arguments(_SAM "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
751 if (_SAM_UNPARSED_ARGUMENTS)
752 message(FATAL_ERROR "SWIG_ADD_LIBRARY: ${_SAM_UNPARSED_ARGUMENTS}: unexpected arguments")
755 if(NOT DEFINED _SAM_LANGUAGE)
756 message(FATAL_ERROR "SWIG_ADD_LIBRARY: Missing LANGUAGE argument")
759 if(NOT DEFINED _SAM_SOURCES)
760 message(FATAL_ERROR "SWIG_ADD_LIBRARY: Missing SOURCES argument")
763 if(NOT DEFINED _SAM_TYPE)
764 set(_SAM_TYPE MODULE)
765 elseif(_SAM_TYPE STREQUAL "USE_BUILD_SHARED_LIBS")
769 cmake_policy(GET CMP0078 target_name_policy)
770 if (target_name_policy STREQUAL "NEW")
771 set (UseSWIG_TARGET_NAME_PREFERENCE STANDARD)
773 if (NOT target_name_policy)
774 cmake_policy(GET_WARNING CMP0078 _cmp0078_warning)
775 message(AUTHOR_WARNING "${_cmp0078_warning}\n")
777 if (NOT DEFINED UseSWIG_TARGET_NAME_PREFERENCE)
778 set (UseSWIG_TARGET_NAME_PREFERENCE LEGACY)
779 elseif (NOT UseSWIG_TARGET_NAME_PREFERENCE MATCHES "^(LEGACY|STANDARD)$")
780 message (FATAL_ERROR "UseSWIG_TARGET_NAME_PREFERENCE: ${UseSWIG_TARGET_NAME_PREFERENCE}: invalid value. 'LEGACY' or 'STANDARD' is expected.")
784 if (NOT DEFINED UseSWIG_MODULE_VERSION)
785 set (UseSWIG_MODULE_VERSION 1)
786 elseif (NOT UseSWIG_MODULE_VERSION MATCHES "^(1|2)$")
787 message (FATAL_ERROR "UseSWIG_MODULE_VERSION: ${UseSWIG_MODULE_VERSION}: invalid value. 1 or 2 is expected.")
790 set (SWIG_MODULE_${name}_NOPROXY ${_SAM_NO_PROXY})
791 swig_module_initialize(${name} ${_SAM_LANGUAGE})
793 # compute real target name.
794 if (UseSWIG_TARGET_NAME_PREFERENCE STREQUAL "LEGACY" AND
795 SWIG_MODULE_${name}_LANGUAGE STREQUAL "PYTHON" AND NOT SWIG_MODULE_${name}_NOPROXY)
796 # swig will produce a module.py containing an 'import _modulename' statement,
797 # which implies having a corresponding _modulename.so (*NIX), _modulename.pyd (Win32),
798 # unless the -noproxy flag is used
799 set(target_name "_${name}")
801 set(target_name "${name}")
804 if (TARGET ${target_name})
805 # a target with same name is already defined.
806 # call NOW add_library command to raise the most useful error message
807 add_library(${target_name})
811 set (workingdir "${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/${target_name}.dir")
812 # set special variable to pass extra information to command SWIG_ADD_SOURCE_TO_MODULE
813 # which cannot be changed due to legacy compatibility
814 set (SWIG_WORKING_DIR "${workingdir}")
815 set (SWIG_TARGET_NAME "${target_name}")
817 set (outputdir "${_SAM_OUTPUT_DIR}")
818 if (NOT _SAM_OUTPUT_DIR)
819 if (CMAKE_SWIG_OUTDIR)
820 set (outputdir "${CMAKE_SWIG_OUTDIR}")
822 if (UseSWIG_MODULE_VERSION VERSION_GREATER 1)
823 set (outputdir "${workingdir}/${_SAM_LANGUAGE}.files")
825 set (outputdir "${CMAKE_CURRENT_BINARY_DIR}")
830 set (outfiledir "${_SAM_OUTFILE_DIR}")
831 if(NOT _SAM_OUTFILE_DIR)
832 if (SWIG_OUTFILE_DIR)
833 set (outfiledir "${SWIG_OUTFILE_DIR}")
835 if (_SAM_OUTPUT_DIR OR CMAKE_SWIG_OUTDIR)
836 set (outfiledir "${outputdir}")
838 set (outfiledir "${workingdir}")
842 # set again, locally, predefined variables to ensure compatibility
843 # with command SWIG_ADD_SOURCE_TO_MODULE
844 set(CMAKE_SWIG_OUTDIR "${outputdir}")
845 set(SWIG_OUTFILE_DIR "${outfiledir}")
847 # See if the user has specified source extensions for swig files?
848 if (NOT DEFINED SWIG_SOURCE_FILE_EXTENSIONS)
849 # Assume the default (*.i) file extension for Swig source files
850 set(SWIG_SOURCE_FILE_EXTENSIONS ".i")
853 if (CMAKE_GENERATOR MATCHES "Make|Ninja|Xcode|Visual Studio (1[1-9]|[2-9][0-9])")
854 # For Makefiles, Ninja, Xcode and Visual Studio generators,
855 # use SWIG generated dependencies if requested
856 if (NOT DEFINED SWIG_USE_SWIG_DEPENDENCIES)
857 set (SWIG_USE_SWIG_DEPENDENCIES OFF)
860 set (SWIG_USE_SWIG_DEPENDENCIES OFF)
863 # Generate a regex out of file extensions.
864 string(REGEX REPLACE "([$^.*+?|()-])" "\\\\\\1" swig_source_ext_regex "${SWIG_SOURCE_FILE_EXTENSIONS}")
865 list (JOIN swig_source_ext_regex "|" swig_source_ext_regex)
866 string (PREPEND swig_source_ext_regex "(")
867 string (APPEND swig_source_ext_regex ")$")
869 set(swig_dot_i_sources ${_SAM_SOURCES})
870 list(FILTER swig_dot_i_sources INCLUDE REGEX ${swig_source_ext_regex})
871 if (NOT swig_dot_i_sources)
872 message(FATAL_ERROR "SWIG_ADD_LIBRARY: no SWIG interface files specified")
874 set(swig_other_sources ${_SAM_SOURCES})
875 list(REMOVE_ITEM swig_other_sources ${swig_dot_i_sources})
877 set(swig_generated_sources)
878 set(swig_generated_timestamps)
879 set(swig_generated_outdirs "${outputdir}")
880 list(LENGTH swig_dot_i_sources swig_sources_count)
881 if (swig_sources_count GREATER "1")
882 # option -interface cannot be used
883 set(SWIG_USE_INTERFACE FALSE)
885 set(SWIG_USE_INTERFACE TRUE)
887 foreach(swig_it IN LISTS swig_dot_i_sources)
888 SWIG_ADD_SOURCE_TO_MODULE(${name} swig_generated_source "${swig_it}")
889 list (APPEND swig_generated_sources "${swig_generated_source}")
891 list (APPEND swig_generated_timestamps "${swig_timestamp}")
893 get_source_file_property(swig_source_file_outdir "${swig_it}" OUTPUT_DIR)
894 if (swig_source_file_outdir)
895 list (APPEND swig_generated_outdirs "${swig_source_file_outdir}")
898 list(REMOVE_DUPLICATES swig_generated_outdirs)
899 set_property (DIRECTORY APPEND PROPERTY
900 ADDITIONAL_CLEAN_FILES ${swig_generated_sources} ${swig_generated_timestamps})
901 if (UseSWIG_MODULE_VERSION VERSION_GREATER 1)
902 set_property (DIRECTORY APPEND PROPERTY ADDITIONAL_CLEAN_FILES ${swig_generated_outdirs})
905 add_library(${target_name}
907 ${swig_generated_sources}
908 ${swig_other_sources})
909 if(swig_generated_timestamps)
910 # see IMPLICIT_DEPENDS above
911 add_custom_target(${name}_swig_compilation DEPENDS ${swig_generated_timestamps})
912 add_dependencies(${target_name} ${name}_swig_compilation)
914 if(_SAM_TYPE STREQUAL "MODULE")
915 set_target_properties(${target_name} PROPERTIES NO_SONAME ON)
917 string(TOLOWER "${_SAM_LANGUAGE}" swig_lowercase_language)
918 if (swig_lowercase_language STREQUAL "octave")
919 set_target_properties(${target_name} PROPERTIES PREFIX "")
920 set_target_properties(${target_name} PROPERTIES SUFFIX ".oct")
921 elseif (swig_lowercase_language STREQUAL "go")
922 set_target_properties(${target_name} PROPERTIES PREFIX "")
923 elseif (swig_lowercase_language STREQUAL "java")
925 # System.loadLibrary("LIBRARY");
926 # then JNI will look for a library whose name is platform dependent, namely
927 # MacOS : libLIBRARY.jnilib
928 # Windows: LIBRARY.dll
929 # Linux : libLIBRARY.so
931 set_target_properties (${target_name} PROPERTIES SUFFIX ".jnilib")
933 if ((WIN32 AND MINGW) OR CYGWIN OR CMAKE_SYSTEM_NAME STREQUAL "MSYS")
934 set_target_properties(${target_name} PROPERTIES PREFIX "")
936 elseif (swig_lowercase_language STREQUAL "lua")
937 if(_SAM_TYPE STREQUAL "MODULE")
938 set_target_properties(${target_name} PROPERTIES PREFIX "")
940 elseif (swig_lowercase_language STREQUAL "python")
941 if (UseSWIG_TARGET_NAME_PREFERENCE STREQUAL "STANDARD" AND NOT SWIG_MODULE_${name}_NOPROXY)
942 # swig will produce a module.py containing an 'import _modulename' statement,
943 # which implies having a corresponding _modulename.so (*NIX), _modulename.pyd (Win32),
944 # unless the -noproxy flag is used
945 set_target_properties(${target_name} PROPERTIES PREFIX "_")
947 set_target_properties(${target_name} PROPERTIES PREFIX "")
949 # Python extension modules on Windows must have the extension ".pyd"
950 # instead of ".dll" as of Python 2.5. Older python versions do support
952 # http://docs.python.org/whatsnew/ports.html#SECTION0001510000000000000000
954 # Windows: .dll is no longer supported as a filename extension for extension modules.
955 # .pyd is now the only filename extension that will be searched for.
957 if(WIN32 AND NOT CYGWIN)
958 set_target_properties(${target_name} PROPERTIES SUFFIX ".pyd")
960 elseif (swig_lowercase_language STREQUAL "r")
961 set_target_properties(${target_name} PROPERTIES PREFIX "")
962 elseif (swig_lowercase_language STREQUAL "ruby")
965 # then ruby will look for a library whose name is platform dependent, namely
966 # MacOS : LIBRARY.bundle
967 # Windows: LIBRARY.dll
969 set_target_properties (${target_name} PROPERTIES PREFIX "")
971 set_target_properties (${target_name} PROPERTIES SUFFIX ".bundle")
973 elseif (swig_lowercase_language STREQUAL "perl")
974 # assume empty prefix because we expect the module to be dynamically loaded
975 set_target_properties (${target_name} PROPERTIES PREFIX "")
977 set_target_properties (${target_name} PROPERTIES SUFFIX ".dylib")
979 elseif (swig_lowercase_language STREQUAL "fortran")
980 # Do *not* override the target's library prefix
981 elseif (swig_lowercase_language STREQUAL "csharp")
982 cmake_policy(GET CMP0122 csharp_naming_policy)
983 if (csharp_naming_policy STREQUAL "NEW")
984 # Do *not* override the target's library prefix
986 if (NOT csharp_naming_policy)
987 cmake_policy(GET_WARNING CMP0122 _cmp0122_warning)
988 message(AUTHOR_WARNING "${_cmp0122_warning}\n")
990 set_target_properties (${target_name} PROPERTIES PREFIX "")
993 # assume empty prefix because we expect the module to be dynamically loaded
994 set_target_properties (${target_name} PROPERTIES PREFIX "")
997 # target property SWIG_SUPPORT_FILES_DIRECTORY specify output directories of support files
998 set_property (TARGET ${target_name} PROPERTY SWIG_SUPPORT_FILES_DIRECTORY ${swig_generated_outdirs})
999 # target property SWIG_SUPPORT_FILES lists principal proxy support files
1000 if (NOT SWIG_MODULE_${name}_NOPROXY)
1001 string(TOUPPER "${_SAM_LANGUAGE}" swig_uppercase_language)
1002 set(swig_all_support_files)
1003 foreach (swig_it IN LISTS SWIG_${swig_uppercase_language}_EXTRA_FILE_EXTENSIONS)
1004 set (swig_support_files ${swig_generated_sources})
1005 list (FILTER swig_support_files INCLUDE REGEX ".*${swig_it}$")
1006 list(APPEND swig_all_support_files ${swig_support_files})
1008 if (swig_all_support_files)
1009 list(REMOVE_DUPLICATES swig_all_support_files)
1011 set_property (TARGET ${target_name} PROPERTY SWIG_SUPPORT_FILES ${swig_all_support_files})
1014 # to ensure legacy behavior, export some variables
1015 set (SWIG_MODULE_${name}_LANGUAGE "${SWIG_MODULE_${name}_LANGUAGE}" PARENT_SCOPE)
1016 set (SWIG_MODULE_${name}_SWIG_LANGUAGE_FLAG "${SWIG_MODULE_${name}_SWIG_LANGUAGE_FLAG}" PARENT_SCOPE)
1017 set (SWIG_MODULE_${name}_REAL_NAME "${target_name}" PARENT_SCOPE)
1018 set (SWIG_MODULE_${name}_NOPROXY "${SWIG_MODULE_${name}_NOPROXY}" PARENT_SCOPE)
1019 set (SWIG_MODULE_${name}_EXTRA_FLAGS "${SWIG_MODULE_${name}_EXTRA_FLAGS}" PARENT_SCOPE)
1020 # the last one is a bit crazy but it is documented, so...
1021 # NOTA: works as expected if only ONE input file is specified
1022 set (swig_generated_file_fullname "${swig_generated_file_fullname}" PARENT_SCOPE)
1026 # Like TARGET_LINK_LIBRARIES but for swig modules
1028 function(SWIG_LINK_LIBRARIES name)
1029 if (UseSWIG_TARGET_NAME_PREFERENCE STREQUAL "STANDARD")
1030 message(DEPRECATION "SWIG_LINK_LIBRARIES is deprecated. Use TARGET_LINK_LIBRARIES instead.")
1031 target_link_libraries(${name} ${ARGN})
1033 if(SWIG_MODULE_${name}_REAL_NAME)
1034 target_link_libraries(${SWIG_MODULE_${name}_REAL_NAME} ${ARGN})
1036 message(SEND_ERROR "Cannot find Swig library \"${name}\".")