UseSWIG.cmake 40.2 KB
Newer Older
1
2
3
# Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
# file Copyright.txt or https://cmake.org/licensing for details.

4
5
6
7
#[=======================================================================[.rst:
UseSWIG
-------

8
9
10
11
This file provides support for ``SWIG``. It is assumed that :module:`FindSWIG`
module has already been loaded.

Defines the following command for use with ``SWIG``:
12

13
.. command:: swig_add_library
14

15
16
  .. versionadded:: 3.8

17
  Define swig module with given name and specified language::
18

19
20
21
    swig_add_library(<name>
                     [TYPE <SHARED|MODULE|STATIC|USE_BUILD_SHARED_LIBS>]
                     LANGUAGE <language>
Marc Chevrier's avatar
Marc Chevrier committed
22
23
24
                     [NO_PROXY]
                     [OUTPUT_DIR <directory>]
                     [OUTFILE_DIR <directory>]
25
                     SOURCES <file>...
Marc Chevrier's avatar
Marc Chevrier committed
26
                    )
27

28
29
30
31
  Targets created with the ``swig_add_library`` command have the same
  capabilities as targets created with the :command:`add_library` command, so
  those targets can be used with any command expecting a target (e.g.
  :command:`target_link_libraries`).
32

33
  .. versionchanged:: 3.13
34
35
36
37
    This command creates a target with the specified ``<name>`` when
    policy :policy:`CMP0078` is set to ``NEW``.  Otherwise, the legacy
    behavior will choose a different target name and store it in the
    ``SWIG_MODULE_<name>_REAL_NAME`` variable.
38

39
40
  .. versionchanged:: 3.15
    Alternate library name (set with the :prop_tgt:`OUTPUT_NAME` property,
41
42
43
44
45
46
47
    for example) will be passed on to ``Python`` and ``CSharp`` wrapper
    libraries.

  .. versionchanged:: 3.21
    Generated library use standard naming conventions for ``CSharp`` language
    when policy :policy:`CMP0122` is set to ``NEW``. Otherwise, the legacy
    behavior is applied.
48

49
50
51
52
53
  .. note::

    For multi-config generators, this module does not support
    configuration-specific files generated by ``SWIG``. All build
    configurations must result in the same generated source file.
54

55
56
  .. note::

57
58
59
60
61
62
    For :ref:`Makefile Generators`, if, for some sources, the
    ``USE_SWIG_DEPENDENCIES`` property is ``FALSE``, ``swig_add_library`` does
    not track file dependencies, so depending on the ``<name>_swig_compilation``
    custom target is required for targets which require the ``swig``-generated
    files to exist. Other generators may depend on the source files that would
    be generated by SWIG.
63

Marc Chevrier's avatar
Marc Chevrier committed
64
  ``TYPE``
65
66
67
68
69
    ``SHARED``, ``MODULE`` and ``STATIC`` have the same semantic as for the
    :command:`add_library` command. If ``USE_BUILD_SHARED_LIBS`` is specified,
    the library type will be ``STATIC`` or ``SHARED`` based on whether the
    current value of the :variable:`BUILD_SHARED_LIBS` variable is ``ON``. If
    no type is specified, ``MODULE`` will be used.
70

Marc Chevrier's avatar
Marc Chevrier committed
71
72
  ``LANGUAGE``
    Specify the target language.
73

74
75
76
77
78
79
80
81
82
    .. versionadded:: 3.1
      Go and Lua language support.

    .. versionadded:: 3.2
      R language support.

    .. versionadded:: 3.18
      Fortran language support.

Marc Chevrier's avatar
Marc Chevrier committed
83
  ``NO_PROXY``
84
85
    .. versionadded:: 3.12

Marc Chevrier's avatar
Marc Chevrier committed
86
87
88
    Prevent the generation of the wrapper layer (swig ``-noproxy`` option).

  ``OUTPUT_DIR``
89
90
    .. versionadded:: 3.12

91
92
93
94
95
96
97
98
99
100
    Specify where to write the language specific files (swig ``-outdir``
    option). If not given, the ``CMAKE_SWIG_OUTDIR`` variable will be used.
    If neither is specified, the default depends on the value of the
    ``UseSWIG_MODULE_VERSION`` variable as follows:

    * If ``UseSWIG_MODULE_VERSION`` is 1 or is undefined, output is written to
      the :variable:`CMAKE_CURRENT_BINARY_DIR` directory.
    * If ``UseSWIG_MODULE_VERSION`` is 2, a dedicated directory will be used.
      The path of this directory can be retrieved from the
      ``SWIG_SUPPORT_FILES_DIRECTORY`` target property.
Marc Chevrier's avatar
Marc Chevrier committed
101
102

  ``OUTFILE_DIR``
103
104
    .. versionadded:: 3.12

105
    Specify an output directory name where the generated source file will be
106
107
    placed (swig ``-o`` option). If not specified, the ``SWIG_OUTFILE_DIR``
    variable will be used. If neither is specified, ``OUTPUT_DIR`` or
108
    ``CMAKE_SWIG_OUTDIR`` is used instead.
Marc Chevrier's avatar
Marc Chevrier committed
109
110

  ``SOURCES``
111
112
    List of sources for the library. Files with extension ``.i`` will be
    identified as sources for the ``SWIG`` tool. Other files will be handled in
113
114
115
116
117
    the standard way.

    .. versionadded:: 3.14
      This behavior can be overridden by specifying the variable
      ``SWIG_SOURCE_FILE_EXTENSIONS``.
118

119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
  .. note::

    If ``UseSWIG_MODULE_VERSION`` is set to 2, it is **strongly** recommended
    to use a dedicated directory unique to the target when either the
    ``OUTPUT_DIR`` option or the ``CMAKE_SWIG_OUTDIR`` variable are specified.
    The output directory contents are erased as part of the target build, so
    to prevent interference between targets or losing other important files,
    each target should have its own dedicated output directory.

.. command:: swig_link_libraries

  Link libraries to swig module::

    swig_link_libraries(<name> <item>...)

  This command has same capabilities as :command:`target_link_libraries`
  command.

  .. note::
Marc Chevrier's avatar
Marc Chevrier committed
138

139
140
141
    If variable ``UseSWIG_TARGET_NAME_PREFERENCE`` is set to ``STANDARD``, this
    command is deprecated and :command:`target_link_libraries` command must be
    used instead.
142
143
144
145

Source file properties on module files **must** be set before the invocation
of the ``swig_add_library`` command to specify special behavior of SWIG and
ensure generated files will receive the required settings.
146

147
148
``CPLUSPLUS``
  Call SWIG in c++ mode.  For example:
149

150
  .. code-block:: cmake
151

152
153
    set_property(SOURCE mymod.i PROPERTY CPLUSPLUS ON)
    swig_add_library(mymod LANGUAGE python SOURCES mymod.i)
154

155
156
157
158
159
160
``SWIG_FLAGS``
  .. deprecated:: 3.12
    Replaced with the fine-grained properties that follow.

  Pass custom flags to the SWIG executable.

Marc Chevrier's avatar
Marc Chevrier committed
161
``INCLUDE_DIRECTORIES``, ``COMPILE_DEFINITIONS`` and ``COMPILE_OPTIONS``
162
163
  .. versionadded:: 3.12

Marc Chevrier's avatar
Marc Chevrier committed
164
  Add custom flags to SWIG compiler and have same semantic as properties
165
166
  :prop_sf:`INCLUDE_DIRECTORIES`, :prop_sf:`COMPILE_DEFINITIONS` and
  :prop_sf:`COMPILE_OPTIONS`.
Marc Chevrier's avatar
Marc Chevrier committed
167

168
``USE_TARGET_INCLUDE_DIRECTORIES``
169
170
  .. versionadded:: 3.13

171
172
173
  If set to ``TRUE``, contents of target property
  :prop_tgt:`INCLUDE_DIRECTORIES` will be forwarded to ``SWIG`` compiler.
  If set to ``FALSE`` target property :prop_tgt:`INCLUDE_DIRECTORIES` will be
174
  ignored. If not set, target property ``SWIG_USE_TARGET_INCLUDE_DIRECTORIES``
175
176
  will be considered.

Marc Chevrier's avatar
Marc Chevrier committed
177
``GENERATED_INCLUDE_DIRECTORIES``, ``GENERATED_COMPILE_DEFINITIONS`` and ``GENERATED_COMPILE_OPTIONS``
178
179
  .. versionadded:: 3.12

Marc Chevrier's avatar
Marc Chevrier committed
180
181
182
  Add custom flags to the C/C++ generated source. They will fill, respectively,
  properties :prop_sf:`INCLUDE_DIRECTORIES`, :prop_sf:`COMPILE_DEFINITIONS` and
  :prop_sf:`COMPILE_OPTIONS` of generated C/C++ file.
183

Marc Chevrier's avatar
Marc Chevrier committed
184
``DEPENDS``
185
186
  .. versionadded:: 3.12

Marc Chevrier's avatar
Marc Chevrier committed
187
  Specify additional dependencies to the source file.
188

189
190
191
192
193
``USE_SWIG_DEPENDENCIES``
  .. versionadded:: 3.20

  If set to ``TRUE``, implicit dependencies are generated by the ``swig`` tool
  itself. This property is only meaningful for
194
195
196
197
198
199
  :ref:`Makefile <Makefile Generators>`,
  :ref:`Ninja <Ninja Generators>` and :generator:`Xcode` generators. Default
  value is ``FALSE``.

  .. versionadded:: 3.21
    Added the support of :generator:`Xcode` generator.
200

201
202
203
204
``SWIG_MODULE_NAME``
  Specify the actual import name of the module in the target language.
  This is required if it cannot be scanned automatically from source
  or different from the module file basename.  For example:
205

206
  .. code-block:: cmake
207

208
209
    set_property(SOURCE mymod.i PROPERTY SWIG_MODULE_NAME mymod_realname)

210
  .. versionchanged:: 3.14
211
212
213
    If policy :policy:`CMP0086` is set to ``NEW``, ``-module <module_name>``
    is passed to ``SWIG`` compiler.

214
``OUTPUT_DIR``
215
216
  .. versionadded:: 3.19

217
218
219
220
221
222
  Specify where to write the language specific files (swig ``-outdir`` option)
  for the considered source file. If not specified, the other ways to define
  the output directory applies (see ``OUTPUT_DIR`` option of
  ``swig_add_library()`` command).

``OUTFILE_DIR``
223
224
  .. versionadded:: 3.19

225
226
227
228
229
230
  Specify an output directory where the generated source file will be placed
  (swig ``-o`` option) for the considered source file. If not specified,
  ``OUTPUT_DIR`` source property will be used. If neither are specified, the
  other ways to define output file directory applies (see ``OUTFILE_DIR``
  option of ``swig_add_library()`` command).

231
232
Target library properties can be set to apply same configuration to all SWIG
input files.
Marc Chevrier's avatar
Marc Chevrier committed
233
234

``SWIG_INCLUDE_DIRECTORIES``, ``SWIG_COMPILE_DEFINITIONS`` and ``SWIG_COMPILE_OPTIONS``
235
236
  .. versionadded:: 3.12

237
238
239
  These properties will be applied to all SWIG input files and have same
  semantic as target properties :prop_tgt:`INCLUDE_DIRECTORIES`,
  :prop_tgt:`COMPILE_DEFINITIONS` and :prop_tgt:`COMPILE_OPTIONS`.
Marc Chevrier's avatar
Marc Chevrier committed
240
241
242

  .. code-block:: cmake

243
    set (UseSWIG_TARGET_NAME_PREFERENCE STANDARD)
Marc Chevrier's avatar
Marc Chevrier committed
244
245
246
247
    swig_add_library(mymod LANGUAGE python SOURCES mymod.i)
    set_property(TARGET mymod PROPERTY SWIG_COMPILE_DEFINITIONS MY_DEF1 MY_DEF2)
    set_property(TARGET mymod PROPERTY SWIG_COMPILE_OPTIONS -bla -blb)

248
``SWIG_USE_TARGET_INCLUDE_DIRECTORIES``
249
250
  .. versionadded:: 3.13

251
252
253
254
255
256
  If set to ``TRUE``, contents of target property
  :prop_tgt:`INCLUDE_DIRECTORIES` will be forwarded to ``SWIG`` compiler.
  If set to ``FALSE`` or not defined, target property
  :prop_tgt:`INCLUDE_DIRECTORIES` will be ignored. This behavior can be
  overridden by specifying source property ``USE_TARGET_INCLUDE_DIRECTORIES``.

Marc Chevrier's avatar
Marc Chevrier committed
257
``SWIG_GENERATED_INCLUDE_DIRECTORIES``, ``SWIG_GENERATED_COMPILE_DEFINITIONS`` and ``SWIG_GENERATED_COMPILE_OPTIONS``
258
259
  .. versionadded:: 3.12

260
261
262
  These properties will populate, respectively, properties
  :prop_sf:`INCLUDE_DIRECTORIES`, :prop_sf:`COMPILE_DEFINITIONS` and
  :prop_sf:`COMPILE_FLAGS` of all generated C/C++ files.
Marc Chevrier's avatar
Marc Chevrier committed
263
264

``SWIG_DEPENDS``
265
266
  .. versionadded:: 3.12

Marc Chevrier's avatar
Marc Chevrier committed
267
268
  Add dependencies to all SWIG input files.

269
270
271
The following target properties are output properties and can be used to get
information about support files generated by ``SWIG`` interface compilation.

Marc Chevrier's avatar
Marc Chevrier committed
272
``SWIG_SUPPORT_FILES``
273
274
  .. versionadded:: 3.12

Marc Chevrier's avatar
Marc Chevrier committed
275
276
277
278
  This output property list of wrapper files generated during SWIG compilation.

  .. code-block:: cmake

279
    set (UseSWIG_TARGET_NAME_PREFERENCE STANDARD)
Marc Chevrier's avatar
Marc Chevrier committed
280
281
282
    swig_add_library(mymod LANGUAGE python SOURCES mymod.i)
    get_property(support_files TARGET mymod PROPERTY SWIG_SUPPORT_FILES)

283
284
285
286
287
288
289
290
  .. note::

    Only most principal support files are listed. In case some advanced
    features of ``SWIG`` are used (for example ``%template``), associated
    support files may not be listed. Prefer to use the
    ``SWIG_SUPPORT_FILES_DIRECTORY`` property to handle support files.

``SWIG_SUPPORT_FILES_DIRECTORY``
291
292
  .. versionadded:: 3.12

293
294
295
  This output property specifies the directory where support files will be
  generated.

296
297
298
299
300
  .. note::

    When source property ``OUTPUT_DIR`` is defined, multiple directories can be
    specified as part of ``SWIG_SUPPORT_FILES_DIRECTORY``.

301
302
303
304
Some variables can be set to customize the behavior of ``swig_add_library``
as well as ``SWIG``:

``UseSWIG_MODULE_VERSION``
305
306
  .. versionadded:: 3.12

307
308
309
310
311
  Specify different behaviors for ``UseSWIG`` module.

  * Set to 1 or undefined: Legacy behavior is applied.
  * Set to 2: A new strategy is applied regarding support files: the output
    directory of support files is erased before ``SWIG`` interface compilation.
312
313
314
315
316
317
318
319

``CMAKE_SWIG_FLAGS``
  Add flags to all swig calls.

``CMAKE_SWIG_OUTDIR``
  Specify where to write the language specific files (swig ``-outdir`` option).

``SWIG_OUTFILE_DIR``
320
321
  .. versionadded:: 3.8

322
323
  Specify an output directory name where the generated source file will be
  placed.  If not specified, ``CMAKE_SWIG_OUTDIR`` is used.
324
325
326

``SWIG_MODULE_<name>_EXTRA_DEPS``
  Specify extra dependencies for the generated module for ``<name>``.
327
328

``SWIG_SOURCE_FILE_EXTENSIONS``
329
330
  .. versionadded:: 3.14

331
332
333
334
335
336
337
  Specify a list of source file extensions to override the default
  behavior of considering only ``.i`` files as sources for the ``SWIG``
  tool. For example:

  .. code-block:: cmake

    set(SWIG_SOURCE_FILE_EXTENSIONS ".i" ".swg")
338
339
340
341
342

``SWIG_USE_SWIG_DEPENDENCIES``
  .. versionadded:: 3.20

  If set to ``TRUE``, implicit dependencies are generated by the ``swig`` tool
343
344
345
346
  itself. This variable is only meaningful for
  :ref:`Makefile <Makefile Generators>`,
  :ref:`Ninja <Ninja Generators>` and :generator:`Xcode` generators. Default
  value is ``FALSE``.
347
348
349

  Source file property ``USE_SWIG_DEPENDENCIES``, if not defined, will be
  initialized with the value of this variable.
350
351
352
353

  .. versionadded:: 3.21
    Added the support of :generator:`Xcode` generator.

354
#]=======================================================================]
355

356
357
358
359
360
cmake_policy(PUSH)
# numbers and boolean constants
cmake_policy (SET CMP0012 NEW)
# IN_LIST operator
cmake_policy (SET CMP0057 NEW)
361
362
# Ninja generator normalizes custom command depfile paths
cmake_policy (SET CMP0116 NEW)
Marc Chevrier's avatar
Marc Chevrier committed
363

364
365
set(SWIG_CXX_EXTENSION "cxx")
set(SWIG_EXTRA_LIBRARIES "")
366

367
368
set(SWIG_PYTHON_EXTRA_FILE_EXTENSIONS ".py")
set(SWIG_JAVA_EXTRA_FILE_EXTENSIONS ".java" "JNI.java")
369
set(SWIG_CSHARP_EXTRA_FILE_EXTENSIONS ".cs" "PINVOKE.cs")
370

371
372
set(SWIG_MANAGE_SUPPORT_FILES_SCRIPT "${CMAKE_CURRENT_LIST_DIR}/UseSWIG/ManageSupportFiles.cmake")

Marc Chevrier's avatar
Marc Chevrier committed
373
374
375
376
377
378
379
380
381
382
383
384
385
##
## PRIVATE functions
##
function (__SWIG_COMPUTE_TIMESTAMP name language infile workingdir __timestamp)
  get_filename_component(filename "${infile}" NAME_WE)
  set(${__timestamp}
    "${workingdir}/${filename}${language}.stamp" PARENT_SCOPE)
  # get_filename_component(filename "${infile}" ABSOLUTE)
  # string(UUID uuid NAMESPACE 9735D882-D2F8-4E1D-88C9-A0A4F1F6ECA4
  #   NAME ${name}-${language}-${filename} TYPE SHA1)
  # set(${__timestamp} "${workingdir}/${uuid}.stamp" PARENT_SCOPE)
endfunction()

386
387
388
#
# For given swig module initialize variables associated with it
#
389
macro(SWIG_MODULE_INITIALIZE name language)
Marc Chevrier's avatar
Marc Chevrier committed
390
391
  string(TOUPPER "${language}" SWIG_MODULE_${name}_LANGUAGE)
  string(TOLOWER "${language}" SWIG_MODULE_${name}_SWIG_LANGUAGE_FLAG)
392

Marc Chevrier's avatar
Marc Chevrier committed
393
394
395
396
  if (NOT DEFINED SWIG_MODULE_${name}_NOPROXY)
    set (SWIG_MODULE_${name}_NOPROXY FALSE)
  endif()
  if ("-noproxy" IN_LIST CMAKE_SWIG_FLAGS)
397
398
    set (SWIG_MODULE_${name}_NOPROXY TRUE)
  endif ()
Marc Chevrier's avatar
Marc Chevrier committed
399

400
401
  if (SWIG_MODULE_${name}_NOPROXY AND
      NOT ("-noproxy" IN_LIST CMAKE_SWIG_FLAGS OR "-noproxy" IN_LIST SWIG_MODULE_${name}_EXTRA_FLAGS))
Marc Chevrier's avatar
Marc Chevrier committed
402
403
404
    list (APPEND SWIG_MODULE_${name}_EXTRA_FLAGS "-noproxy")
  endif()
  if(SWIG_MODULE_${name}_LANGUAGE STREQUAL "UNKNOWN")
405
    message(FATAL_ERROR "SWIG Error: Language \"${language}\" not found")
406
407
  elseif(SWIG_MODULE_${name}_LANGUAGE STREQUAL "PERL" AND
         NOT "-shadow" IN_LIST SWIG_MODULE_${name}_EXTRA_FLAGS)
Marc Chevrier's avatar
Marc Chevrier committed
408
    list(APPEND SWIG_MODULE_${name}_EXTRA_FLAGS "-shadow")
409
  endif()
410
endmacro()
411
412
413

#
# For a given language, input file, and output file, determine extra files that
414
# will be generated. This is internal swig macro.
415
416
#

Marc Chevrier's avatar
Marc Chevrier committed
417
418
419
420
function(SWIG_GET_EXTRA_OUTPUT_FILES language outfiles generatedpath infile)
  set(files)
  get_source_file_property(module_basename
    "${infile}" SWIG_MODULE_NAME)
421
  if(NOT module_basename)
422
423

    # try to get module name from "%module foo" syntax
Marc Chevrier's avatar
Marc Chevrier committed
424
425
    if ( EXISTS "${infile}" )
      file ( STRINGS "${infile}" module_basename REGEX "[ ]*%module[ ]*[a-zA-Z0-9_]+.*" )
426
    endif ()
Marc Chevrier's avatar
Marc Chevrier committed
427
428
    if ( module_basename )
      string ( REGEX REPLACE "[ ]*%module[ ]*([a-zA-Z0-9_]+).*" "\\1" module_basename "${module_basename}" )
429
430
431

    else ()
      # try to get module name from "%module (options=...) foo" syntax
Marc Chevrier's avatar
Marc Chevrier committed
432
433
      if ( EXISTS "${infile}" )
        file ( STRINGS "${infile}" module_basename REGEX "[ ]*%module[ ]*\\(.*\\)[ ]*[a-zA-Z0-9_]+.*" )
434
      endif ()
Marc Chevrier's avatar
Marc Chevrier committed
435
436
      if ( module_basename )
        string ( REGEX REPLACE "[ ]*%module[ ]*\\(.*\\)[ ]*([a-zA-Z0-9_]+).*" "\\1" module_basename "${module_basename}" )
437
438
439

      else ()
        # fallback to file basename
Marc Chevrier's avatar
Marc Chevrier committed
440
        get_filename_component(module_basename "${infile}" NAME_WE)
441
442
443
      endif ()
    endif ()

444
  endif()
445
  foreach(it ${SWIG_${language}_EXTRA_FILE_EXTENSIONS})
Marc Chevrier's avatar
Marc Chevrier committed
446
    set(extra_file "${generatedpath}/${module_basename}${it}")
447
    if (extra_file MATCHES "\\.cs$" AND CMAKE_CSharp_COMPILER_LOADED)
448
449
450
451
452
      set_source_files_properties(${extra_file} PROPERTIES LANGUAGE "CSharp")
    else()
      # Treat extra outputs as plain files regardless of language.
      set_source_files_properties(${extra_file} PROPERTIES LANGUAGE "")
    endif()
Marc Chevrier's avatar
Marc Chevrier committed
453
    list(APPEND files "${extra_file}")
454
  endforeach()
Marc Chevrier's avatar
Marc Chevrier committed
455

456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
  if (language STREQUAL "FORTRAN" AND CMAKE_Fortran_COMPILER_LOADED)
    # Process possible user-supplied extension in flags (obtained via parent
    # scope variable) to determine the source file name.
    list(FIND SWIG_COMPILATION_FLAGS "-fext" fext_idx)
    if (fext_idx EQUAL -1)
      # Default Fortran generated extension
      set(fext "f90")
    else()
      # Get extension from user-provided flag
      math(EXPR fext_idx "${fext_idx} + 1")
      list(GET SWIG_COMPILATION_FLAGS "${fext_idx}" fext)
    endif()
    set(extra_file "${generatedpath}/${module_basename}.${fext}")
    set_source_files_properties("${extra_file}" PROPERTIES LANGUAGE "Fortran")
    list(APPEND files "${extra_file}")
  endif()

Marc Chevrier's avatar
Marc Chevrier committed
473
474
  set (${outfiles} ${files} PARENT_SCOPE)
endfunction()
475
476
477
478

#
# Take swig (*.i) file and add proper custom commands for it
#
Marc Chevrier's avatar
Marc Chevrier committed
479
function(SWIG_ADD_SOURCE_TO_MODULE name outfiles infile)
480
  get_filename_component(swig_source_file_name_we "${infile}" NAME_WE)
Marc Chevrier's avatar
Marc Chevrier committed
481
  get_source_file_property(swig_source_file_cplusplus "${infile}" CPLUSPLUS)
482
483
484
485
486
487
488
489
490
491
  get_source_file_property(swig_source_file_outdir "${infile}" OUTPUT_DIR)
  get_source_file_property(swig_source_file_outfiledir "${infile}" OUTFILE_DIR)

  if (swig_source_file_outdir)
    # use source file property
    set(outdir "${swig_source_file_outdir}")
    if (NOT swig_source_file_outfiledir)
      set (swig_source_file_outfiledir "${outdir}")
    endif()
  elseif(CMAKE_SWIG_OUTDIR)
Marc Chevrier's avatar
Marc Chevrier committed
492
    set(outdir ${CMAKE_SWIG_OUTDIR})
493
  else()
Marc Chevrier's avatar
Marc Chevrier committed
494
    set(outdir ${CMAKE_CURRENT_BINARY_DIR})
495
  endif()
496

497
498
499
  if (swig_source_file_outfiledir)
    set (outfiledir "${swig_source_file_outfiledir}")
  elseif(SWIG_OUTFILE_DIR)
Marc Chevrier's avatar
Marc Chevrier committed
500
    set(outfiledir ${SWIG_OUTFILE_DIR})
501
  else()
Marc Chevrier's avatar
Marc Chevrier committed
502
    set(outfiledir ${outdir})
503
504
  endif()

Marc Chevrier's avatar
Marc Chevrier committed
505
506
507
508
509
510
  if(SWIG_WORKING_DIR)
    set (workingdir "${SWIG_WORKING_DIR}")
  else()
    set(workingdir "${outdir}")
  endif()

511
512
513
514
515
516
  if(SWIG_TARGET_NAME)
    set(target_name ${SWIG_TARGET_NAME})
  else()
    set(target_name ${name})
  endif()

517
  set (use_swig_dependencies ${SWIG_USE_SWIG_DEPENDENCIES})
518
  if (CMAKE_GENERATOR MATCHES "Make|Ninja|Xcode")
519
520
521
522
523
524
    get_property(use_swig_dependencies_set SOURCE "${infile}" PROPERTY USE_SWIG_DEPENDENCIES SET)
    if (use_swig_dependencies_set)
      get_property(use_swig_dependencies SOURCE "${infile}" PROPERTY USE_SWIG_DEPENDENCIES)
    endif()
  endif()

Marc Chevrier's avatar
Marc Chevrier committed
525
526
527
528
  set (swig_source_file_flags ${CMAKE_SWIG_FLAGS})
  # handle various swig compile flags properties
  get_source_file_property (include_directories "${infile}" INCLUDE_DIRECTORIES)
  if (include_directories)
529
    list (APPEND swig_source_file_flags "$<$<BOOL:${include_directories}>:-I$<JOIN:${include_directories},$<SEMICOLON>-I>>")
Marc Chevrier's avatar
Marc Chevrier committed
530
  endif()
531
532
  set (property "$<TARGET_PROPERTY:${target_name},SWIG_INCLUDE_DIRECTORIES>")
  list (APPEND swig_source_file_flags "$<$<BOOL:${property}>:-I$<JOIN:$<TARGET_GENEX_EVAL:${target_name},${property}>,$<SEMICOLON>-I>>")
533
  set (property "$<REMOVE_DUPLICATES:$<TARGET_PROPERTY:${target_name},INCLUDE_DIRECTORIES>>")
534
535
536
537
538
  get_source_file_property(use_target_include_dirs "${infile}" USE_TARGET_INCLUDE_DIRECTORIES)
  if (use_target_include_dirs)
    list (APPEND swig_source_file_flags "$<$<BOOL:${property}>:-I$<JOIN:${property},$<SEMICOLON>-I>>")
  elseif(use_target_include_dirs STREQUAL "NOTFOUND")
    # not defined at source level, rely on target level
539
    list (APPEND swig_source_file_flags "$<$<AND:$<BOOL:$<TARGET_PROPERTY:${target_name},SWIG_USE_TARGET_INCLUDE_DIRECTORIES>>,$<BOOL:${property}>>:-I$<JOIN:${property},$<SEMICOLON>-I>>")
540
  endif()
Marc Chevrier's avatar
Marc Chevrier committed
541

542
543
  set (property "$<TARGET_PROPERTY:${target_name},SWIG_COMPILE_DEFINITIONS>")
  list (APPEND swig_source_file_flags "$<$<BOOL:${property}>:-D$<JOIN:$<TARGET_GENEX_EVAL:${target_name},${property}>,$<SEMICOLON>-D>>")
Marc Chevrier's avatar
Marc Chevrier committed
544
545
  get_source_file_property (compile_definitions "${infile}" COMPILE_DEFINITIONS)
  if (compile_definitions)
546
    list (APPEND swig_source_file_flags "$<$<BOOL:${compile_definitions}>:-D$<JOIN:${compile_definitions},$<SEMICOLON>-D>>")
Marc Chevrier's avatar
Marc Chevrier committed
547
548
  endif()

549
  list (APPEND swig_source_file_flags "$<TARGET_GENEX_EVAL:${target_name},$<TARGET_PROPERTY:${target_name},SWIG_COMPILE_OPTIONS>>")
Marc Chevrier's avatar
Marc Chevrier committed
550
551
552
553
554
555
556
557
558
559
560
561
562
  get_source_file_property (compile_options "${infile}" COMPILE_OPTIONS)
  if (compile_options)
    list (APPEND swig_source_file_flags ${compile_options})
  endif()

  # legacy support
  get_source_file_property (swig_flags "${infile}" SWIG_FLAGS)
  if (swig_flags)
    list (APPEND swig_source_file_flags ${swig_flags})
  endif()

  get_filename_component(swig_source_file_fullname "${infile}" ABSOLUTE)

563
  if (NOT SWIG_MODULE_${name}_NOPROXY)
564
    set(SWIG_COMPILATION_FLAGS ${swig_source_file_flags})
565
566
    SWIG_GET_EXTRA_OUTPUT_FILES(${SWIG_MODULE_${name}_LANGUAGE}
      swig_extra_generated_files
Marc Chevrier's avatar
Marc Chevrier committed
567
      "${outdir}"
568
569
      "${swig_source_file_fullname}")
  endif()
570
  set(swig_generated_file_fullname
Marc Chevrier's avatar
Marc Chevrier committed
571
    "${outfiledir}/${swig_source_file_name_we}")
Bill Hoffman's avatar
Bill Hoffman committed
572
573
  # add the language into the name of the file (i.e. TCL_wrap)
  # this allows for the same .i file to be wrapped into different languages
Daniel Pfeifer's avatar
Daniel Pfeifer committed
574
575
  string(APPEND swig_generated_file_fullname
    "${SWIG_MODULE_${name}_LANGUAGE}_wrap")
576

577
  if(swig_source_file_cplusplus)
Daniel Pfeifer's avatar
Daniel Pfeifer committed
578
579
    string(APPEND swig_generated_file_fullname
      ".${SWIG_CXX_EXTENSION}")
580
  else()
Daniel Pfeifer's avatar
Daniel Pfeifer committed
581
582
    string(APPEND swig_generated_file_fullname
      ".c")
583
  endif()
584

Marc Chevrier's avatar
Marc Chevrier committed
585
586
587
588
  get_directory_property (cmake_include_directories INCLUDE_DIRECTORIES)
  list (REMOVE_DUPLICATES cmake_include_directories)
  set (swig_include_dirs)
  if (cmake_include_directories)
589
    set (swig_include_dirs "$<$<BOOL:${cmake_include_directories}>:-I$<JOIN:${cmake_include_directories},$<SEMICOLON>-I>>")
Marc Chevrier's avatar
Marc Chevrier committed
590
  endif()
591

592
  set(swig_special_flags)
Bill Hoffman's avatar
Bill Hoffman committed
593
  # default is c, so add c++ flag if it is c++
594
  if(swig_source_file_cplusplus)
Marc Chevrier's avatar
Marc Chevrier committed
595
    list (APPEND swig_special_flags "-c++")
596
  endif()
Marc Chevrier's avatar
Marc Chevrier committed
597

598
599
600
601
602
603
604
605
606
607
608
609
610
  cmake_policy(GET CMP0086 module_name_policy)
  if (module_name_policy STREQUAL "NEW")
    get_source_file_property(module_name "${infile}" SWIG_MODULE_NAME)
    if (module_name)
      list (APPEND swig_special_flags "-module" "${module_name}")
    endif()
  else()
    if (NOT module_name_policy)
      cmake_policy(GET_WARNING CMP0086 _cmp0086_warning)
      message(AUTHOR_WARNING "${_cmp0086_warning}\n")
    endif()
  endif()

Marc Chevrier's avatar
Marc Chevrier committed
611
612
613
  set (swig_extra_flags)
  if(SWIG_MODULE_${name}_LANGUAGE STREQUAL "CSHARP")
    if(NOT ("-dllimport" IN_LIST swig_source_file_flags OR "-dllimport" IN_LIST SWIG_MODULE_${name}_EXTRA_FLAGS))
614
615
      # This makes sure that the name used in the generated DllImport
      # matches the library name created by CMake
616
617
618
619
      list (APPEND SWIG_MODULE_${name}_EXTRA_FLAGS "-dllimport" "$<TARGET_FILE_PREFIX:${target_name}>$<TARGET_FILE_BASE_NAME:${target_name}>")
    endif()
  endif()
  if (SWIG_MODULE_${name}_LANGUAGE STREQUAL "PYTHON" AND NOT SWIG_MODULE_${name}_NOPROXY)
620
621
    if(SWIG_USE_INTERFACE AND
        NOT ("-interface" IN_LIST swig_source_file_flags OR "-interface" IN_LIST SWIG_MODULE_${name}_EXTRA_FLAGS))
622
623
624
      # This makes sure that the name used in the proxy code
      # matches the library name created by CMake
      list (APPEND SWIG_MODULE_${name}_EXTRA_FLAGS "-interface" "$<TARGET_FILE_PREFIX:${target_name}>$<TARGET_FILE_BASE_NAME:${target_name}>")
625
626
    endif()
  endif()
Marc Chevrier's avatar
Marc Chevrier committed
627
628
629
  list (APPEND swig_extra_flags ${SWIG_MODULE_${name}_EXTRA_FLAGS})

  # dependencies
630
  set (swig_dependencies DEPENDS ${SWIG_MODULE_${name}_EXTRA_DEPS} $<TARGET_PROPERTY:${target_name},SWIG_DEPENDS>)
Marc Chevrier's avatar
Marc Chevrier committed
631
632
633
  get_source_file_property(file_depends "${infile}" DEPENDS)
  if (file_depends)
    list (APPEND swig_dependencies ${file_depends})
634
  endif()
Marc Chevrier's avatar
Marc Chevrier committed
635

636
  if (UseSWIG_MODULE_VERSION VERSION_GREATER 1)
637
638
    # as part of custom command, start by removing old generated files
    # to ensure obsolete files do not stay
639
640
641
    set (swig_file_outdir "${workingdir}/${swig_source_file_name_we}.files")
    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}")
    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}")
642
  else()
643
    set (swig_file_outdir "${outdir}")
644
    unset (swig_cleanup_command)
645
    unset (swig_copy_command)
646
647
  endif()

648
649
650
651
652
  set(swig_depends_flags)
  if(NOT use_swig_dependencies AND CMAKE_GENERATOR MATCHES "Make")
    # IMPLICIT_DEPENDS can not handle situations where a dependent file is
    # removed. We need an extra step with timestamp and custom target, see #16830
    # As this is needed only for Makefile generator do it conditionally
Marc Chevrier's avatar
Marc Chevrier committed
653
654
655
    __swig_compute_timestamp(${name} ${SWIG_MODULE_${name}_LANGUAGE}
      "${infile}" "${workingdir}" swig_generated_timestamp)
    set(swig_custom_output "${swig_generated_timestamp}")
656
657
658
    set(swig_custom_products
      BYPRODUCTS "${swig_generated_file_fullname}" ${swig_extra_generated_files})
    set(swig_timestamp_command
Marc Chevrier's avatar
Marc Chevrier committed
659
      COMMAND ${CMAKE_COMMAND} -E touch "${swig_generated_timestamp}")
660
    list(APPEND swig_dependencies IMPLICIT_DEPENDS CXX "${swig_source_file_fullname}")
661
  else()
662
    set(swig_generated_timestamp)
663
664
665
666
    set(swig_custom_output
      "${swig_generated_file_fullname}" ${swig_extra_generated_files})
    set(swig_custom_products)
    set(swig_timestamp_command)
667
668
669
670
671
672
    if (use_swig_dependencies)
      cmake_path(GET infile FILENAME swig_depends_filename)
      set(swig_depends_filename "${workingdir}/${swig_depends_filename}.d")
      list(APPEND swig_dependencies DEPFILE "${swig_depends_filename}")
      set(swig_depends_flags -MF "${swig_depends_filename}" -MD)
    endif()
673
  endif()
674
  add_custom_command(
675
676
    OUTPUT ${swig_custom_output}
    ${swig_custom_products}
677
    ${swig_cleanup_command}
Marc Chevrier's avatar
Marc Chevrier committed
678
679
    # Let's create the ${outdir} at execution time, in case dir contains $(OutDir)
    COMMAND "${CMAKE_COMMAND}" -E make_directory ${outdir} ${outfiledir}
680
    ${swig_timestamp_command}
681
    COMMAND "${CMAKE_COMMAND}" -E env "SWIG_LIB=${SWIG_DIR}" "${SWIG_EXECUTABLE}"
Marc Chevrier's avatar
Marc Chevrier committed
682
683
    "-${SWIG_MODULE_${name}_SWIG_LANGUAGE_FLAG}"
    "${swig_source_file_flags}"
684
    -outdir "${swig_file_outdir}"
Ken Martin's avatar
Ken Martin committed
685
686
    ${swig_special_flags}
    ${swig_extra_flags}
687
    ${swig_depends_flags}
Marc Chevrier's avatar
Marc Chevrier committed
688
    "${swig_include_dirs}"
Ken Martin's avatar
Ken Martin committed
689
690
    -o "${swig_generated_file_fullname}"
    "${swig_source_file_fullname}"
691
    ${swig_copy_command}
692
    MAIN_DEPENDENCY "${swig_source_file_fullname}"
693
    ${swig_dependencies}
694
    COMMENT "Swig compile ${infile} for ${SWIG_MODULE_${name}_SWIG_LANGUAGE_FLAG}"
Marc Chevrier's avatar
Marc Chevrier committed
695
    COMMAND_EXPAND_LISTS)
696
  set_source_files_properties("${swig_generated_file_fullname}" ${swig_extra_generated_files}
697
    PROPERTIES GENERATED 1)
Marc Chevrier's avatar
Marc Chevrier committed
698
699
700

  ## add all properties for generated file to various properties
  get_property (include_directories SOURCE "${infile}" PROPERTY GENERATED_INCLUDE_DIRECTORIES)
701
  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>>)
Marc Chevrier's avatar
Marc Chevrier committed
702
703

  get_property (compile_definitions SOURCE "${infile}" PROPERTY GENERATED_COMPILE_DEFINITIONS)
704
  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})
Marc Chevrier's avatar
Marc Chevrier committed
705
706

  get_property (compile_options SOURCE "${infile}" PROPERTY GENERATED_COMPILE_OPTIONS)
707
  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})
Marc Chevrier's avatar
Marc Chevrier committed
708

709
710
711
712
  if (SWIG_MODULE_${name}_SWIG_LANGUAGE_FLAG MATCHES "php")
    set_property (SOURCE "${swig_generated_file_fullname}" APPEND PROPERTY INCLUDE_DIRECTORIES "${outdir}")
  endif()

Marc Chevrier's avatar
Marc Chevrier committed
713
  set(${outfiles} "${swig_generated_file_fullname}" ${swig_extra_generated_files} PARENT_SCOPE)
714
  set(swig_timestamp "${swig_generated_timestamp}" PARENT_SCOPE)
Marc Chevrier's avatar
Marc Chevrier committed
715
716
717
718

  # legacy support
  set (swig_generated_file_fullname "${swig_generated_file_fullname}" PARENT_SCOPE)
endfunction()
719
720
721
722

#
# Create Swig module
#
723
macro(SWIG_ADD_MODULE name language)
724
725
726
727
728
729
730
731
  message(DEPRECATION "SWIG_ADD_MODULE is deprecated. Use SWIG_ADD_LIBRARY instead.")
  swig_add_library(${name}
                   LANGUAGE ${language}
                   TYPE MODULE
                   SOURCES ${ARGN})
endmacro()


Marc Chevrier's avatar
Marc Chevrier committed
732
733
function(SWIG_ADD_LIBRARY name)
  set(options NO_PROXY)
734
  set(oneValueArgs LANGUAGE
Marc Chevrier's avatar
Marc Chevrier committed
735
736
737
                   TYPE
                   OUTPUT_DIR
                   OUTFILE_DIR)
738
739
740
  set(multiValueArgs SOURCES)
  cmake_parse_arguments(_SAM "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})

Marc Chevrier's avatar
Marc Chevrier committed
741
742
743
744
  if (_SAM_UNPARSED_ARGUMENTS)
    message(FATAL_ERROR "SWIG_ADD_LIBRARY: ${_SAM_UNPARSED_ARGUMENTS}: unexpected arguments")
  endif()

745
746
747
748
749
750
751
752
753
754
  if(NOT DEFINED _SAM_LANGUAGE)
    message(FATAL_ERROR "SWIG_ADD_LIBRARY: Missing LANGUAGE argument")
  endif()

  if(NOT DEFINED _SAM_SOURCES)
    message(FATAL_ERROR "SWIG_ADD_LIBRARY: Missing SOURCES argument")
  endif()

  if(NOT DEFINED _SAM_TYPE)
    set(_SAM_TYPE MODULE)
Marc Chevrier's avatar
Marc Chevrier committed
755
  elseif(_SAM_TYPE STREQUAL "USE_BUILD_SHARED_LIBS")
756
    unset(_SAM_TYPE)
757
758
  endif()

759
760
761
762
763
  cmake_policy(GET CMP0078 target_name_policy)
  if (target_name_policy STREQUAL "NEW")
    set (UseSWIG_TARGET_NAME_PREFERENCE STANDARD)
  else()
    if (NOT target_name_policy)
764
765
      cmake_policy(GET_WARNING CMP0078 _cmp0078_warning)
      message(AUTHOR_WARNING "${_cmp0078_warning}\n")
766
767
768
769
770
771
    endif()
    if (NOT DEFINED UseSWIG_TARGET_NAME_PREFERENCE)
      set (UseSWIG_TARGET_NAME_PREFERENCE LEGACY)
    elseif (NOT UseSWIG_TARGET_NAME_PREFERENCE MATCHES "^(LEGACY|STANDARD)$")
      message (FATAL_ERROR "UseSWIG_TARGET_NAME_PREFERENCE: ${UseSWIG_TARGET_NAME_PREFERENCE}: invalid value. 'LEGACY' or 'STANDARD' is expected.")
    endif()
772
773
  endif()

774
775
776
777
778
779
  if (NOT DEFINED UseSWIG_MODULE_VERSION)
    set (UseSWIG_MODULE_VERSION 1)
  elseif (NOT UseSWIG_MODULE_VERSION MATCHES "^(1|2)$")
    message (FATAL_ERROR "UseSWIG_MODULE_VERSION: ${UseSWIG_MODULE_VERSION}: invalid value. 1 or 2 is expected.")
  endif()

780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
  set (SWIG_MODULE_${name}_NOPROXY ${_SAM_NO_PROXY})
  swig_module_initialize(${name} ${_SAM_LANGUAGE})

  # compute real target name.
  if (UseSWIG_TARGET_NAME_PREFERENCE STREQUAL "LEGACY" AND
      SWIG_MODULE_${name}_LANGUAGE STREQUAL "PYTHON" AND NOT SWIG_MODULE_${name}_NOPROXY)
    # swig will produce a module.py containing an 'import _modulename' statement,
    # which implies having a corresponding _modulename.so (*NIX), _modulename.pyd (Win32),
    # unless the -noproxy flag is used
    set(target_name "_${name}")
  else()
    set(target_name "${name}")
  endif()

  if (TARGET ${target_name})
    # a target with same name is already defined.
    # call NOW add_library command to raise the most useful error message
    add_library(${target_name})
    return()
  endif()

  set (workingdir "${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/${target_name}.dir")
Marc Chevrier's avatar
Marc Chevrier committed
802
803
804
  # set special variable to pass extra information to command SWIG_ADD_SOURCE_TO_MODULE
  # which cannot be changed due to legacy compatibility
  set (SWIG_WORKING_DIR "${workingdir}")
805
  set (SWIG_TARGET_NAME "${target_name}")
806

Marc Chevrier's avatar
Marc Chevrier committed
807
808
809
810
  set (outputdir "${_SAM_OUTPUT_DIR}")
  if (NOT _SAM_OUTPUT_DIR)
    if (CMAKE_SWIG_OUTDIR)
      set (outputdir "${CMAKE_SWIG_OUTDIR}")
811
    else()
812
      if (UseSWIG_MODULE_VERSION VERSION_GREATER 1)
813
814
815
816
        set (outputdir "${workingdir}/${_SAM_LANGUAGE}.files")
      else()
        set (outputdir "${CMAKE_CURRENT_BINARY_DIR}")
      endif()
817
    endif()
Marc Chevrier's avatar
Marc Chevrier committed
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
  endif()

  set (outfiledir "${_SAM_OUTFILE_DIR}")
  if(NOT _SAM_OUTFILE_DIR)
    if (SWIG_OUTFILE_DIR)
      set (outfiledir "${SWIG_OUTFILE_DIR}")
    else()
      if (_SAM_OUTPUT_DIR OR CMAKE_SWIG_OUTDIR)
        set (outfiledir "${outputdir}")
    else()
        set (outfiledir "${workingdir}")
      endif()
    endif()
  endif()
  # set again, locally, predefined variables to ensure compatibility
  # with command SWIG_ADD_SOURCE_TO_MODULE
  set(CMAKE_SWIG_OUTDIR "${outputdir}")
  set(SWIG_OUTFILE_DIR "${outfiledir}")

837
838
839
840
841
842
  # See if the user has specified source extensions for swig files?
  if (NOT DEFINED SWIG_SOURCE_FILE_EXTENSIONS)
    # Assume the default (*.i) file extension for Swig source files
    set(SWIG_SOURCE_FILE_EXTENSIONS ".i")
  endif()

843
  if (CMAKE_GENERATOR MATCHES "Make|Ninja|Xcode")
844
845
846
847
848
849
850
851
    # For Makefiles and Ninja generators, use SWIG generated dependencies
    if (NOT DEFINED SWIG_USE_SWIG_DEPENDENCIES)
        set (SWIG_USE_SWIG_DEPENDENCIES OFF)
    endif()
  else()
    set (SWIG_USE_SWIG_DEPENDENCIES OFF)
  endif()

852
853
854
855
856
857
  # Generate a regex out of file extensions.
  string(REGEX REPLACE "([$^.*+?|()-])" "\\\\\\1" swig_source_ext_regex "${SWIG_SOURCE_FILE_EXTENSIONS}")
  list (JOIN swig_source_ext_regex "|" swig_source_ext_regex)
  string (PREPEND swig_source_ext_regex "(")
  string (APPEND swig_source_ext_regex ")$")

Marc Chevrier's avatar
Marc Chevrier committed
858
  set(swig_dot_i_sources ${_SAM_SOURCES})
859
  list(FILTER swig_dot_i_sources INCLUDE REGEX ${swig_source_ext_regex})
860
861
862
  if (NOT swig_dot_i_sources)
    message(FATAL_ERROR "SWIG_ADD_LIBRARY: no SWIG interface files specified")
  endif()
Marc Chevrier's avatar
Marc Chevrier committed
863
864
  set(swig_other_sources ${_SAM_SOURCES})
  list(REMOVE_ITEM swig_other_sources ${swig_dot_i_sources})
865
866

  set(swig_generated_sources)
Marc Chevrier's avatar
Marc Chevrier committed
867
  set(swig_generated_timestamps)
868
  set(swig_generated_outdirs "${outputdir}")
869
870
871
872
873
874
875
  list(LENGTH swig_dot_i_sources swig_sources_count)
  if (swig_sources_count GREATER "1")
    # option -interface cannot be used
    set(SWIG_USE_INTERFACE FALSE)
  else()
    set(SWIG_USE_INTERFACE TRUE)
  endif()
Marc Chevrier's avatar
Marc Chevrier committed
876
877
878
  foreach(swig_it IN LISTS swig_dot_i_sources)
    SWIG_ADD_SOURCE_TO_MODULE(${name} swig_generated_source "${swig_it}")
    list (APPEND swig_generated_sources "${swig_generated_source}")
879
    if(swig_timestamp)
Marc Chevrier's avatar
Marc Chevrier committed
880
881
      list (APPEND swig_generated_timestamps "${swig_timestamp}")
    endif()
882
883
884
885
    get_source_file_property(swig_source_file_outdir "${swig_it}" OUTPUT_DIR)
    if (swig_source_file_outdir)
      list (APPEND swig_generated_outdirs "${swig_source_file_outdir}")
    endif()
886
  endforeach()
887
  list(REMOVE_DUPLICATES swig_generated_outdirs)
Marc Chevrier's avatar
Marc Chevrier committed
888
  set_property (DIRECTORY APPEND PROPERTY
889
    ADDITIONAL_CLEAN_FILES ${swig_generated_sources} ${swig_generated_timestamps})
890
  if (UseSWIG_MODULE_VERSION VERSION_GREATER 1)
891
    set_property (DIRECTORY APPEND PROPERTY ADDITIONAL_CLEAN_FILES ${swig_generated_outdirs})
892
  endif()
Marc Chevrier's avatar
Marc Chevrier committed
893

894
  add_library(${target_name}
895
    ${_SAM_TYPE}
896
897
    ${swig_generated_sources}
    ${swig_other_sources})
898
  if(swig_generated_timestamps)
899
    # see IMPLICIT_DEPENDS above
Marc Chevrier's avatar
Marc Chevrier committed
900
    add_custom_target(${name}_swig_compilation DEPENDS ${swig_generated_timestamps})
901
    add_dependencies(${target_name} ${name}_swig_compilation)
902
  endif()
Marc Chevrier's avatar
Marc Chevrier committed
903
  if(_SAM_TYPE STREQUAL "MODULE")
904
    set_target_properties(${target_name} PROPERTIES NO_SONAME ON)
905
  endif()
906
  string(TOLOWER "${_SAM_LANGUAGE}" swig_lowercase_language)
Marc Chevrier's avatar
Marc Chevrier committed
907
  if (swig_lowercase_language STREQUAL "octave")
908
909
    set_target_properties(${target_name} PROPERTIES PREFIX "")
    set_target_properties(${target_name} PROPERTIES SUFFIX ".oct")
Marc Chevrier's avatar
Marc Chevrier committed
910
  elseif (swig_lowercase_language STREQUAL "go")
911
    set_target_properties(${target_name} PROPERTIES PREFIX "")
Marc Chevrier's avatar
Marc Chevrier committed
912
  elseif (swig_lowercase_language STREQUAL "java")
913
914
915
916
917
918
    # In java you want:
    #      System.loadLibrary("LIBRARY");
    # then JNI will look for a library whose name is platform dependent, namely
    #   MacOS  : libLIBRARY.jnilib
    #   Windows: LIBRARY.dll
    #   Linux  : libLIBRARY.so
919
    if (APPLE)
920
      set_target_properties (${target_name} PROPERTIES SUFFIX ".jnilib")
921
    endif()
922
    if ((WIN32 AND MINGW) OR CYGWIN OR CMAKE_SYSTEM_NAME STREQUAL "MSYS")
923
      set_target_properties(${target_name} PROPERTIES PREFIX "")
924
    endif()
Marc Chevrier's avatar
Marc Chevrier committed
925
926
  elseif (swig_lowercase_language STREQUAL "lua")
    if(_SAM_TYPE STREQUAL "MODULE")
927
      set_target_properties(${target_name} PROPERTIES PREFIX "")
928
    endif()
Marc Chevrier's avatar
Marc Chevrier committed
929
  elseif (swig_lowercase_language STREQUAL "python")
930
    if (UseSWIG_TARGET_NAME_PREFERENCE STREQUAL "STANDARD" AND NOT SWIG_MODULE_${name}_NOPROXY)
931
932
933
      # swig will produce a module.py containing an 'import _modulename' statement,
      # which implies having a corresponding _modulename.so (*NIX), _modulename.pyd (Win32),
      # unless the -noproxy flag is used
934
935
936
      set_target_properties(${target_name} PROPERTIES PREFIX "_")
    else()
      set_target_properties(${target_name} PROPERTIES PREFIX "")
937
    endif()
938
939
940
941
942
943
944
945
    # Python extension modules on Windows must have the extension ".pyd"
    # instead of ".dll" as of Python 2.5.  Older python versions do support
    # this suffix.
    # http://docs.python.org/whatsnew/ports.html#SECTION0001510000000000000000
    # <quote>
    # Windows: .dll is no longer supported as a filename extension for extension modules.
    # .pyd is now the only filename extension that will be searched for.
    # </quote>
946
    if(WIN32 AND NOT CYGWIN)
947
      set_target_properties(${target_name} PROPERTIES SUFFIX ".pyd")
948
    endif()
Marc Chevrier's avatar
Marc Chevrier committed
949
  elseif (swig_lowercase_language STREQUAL "r")
950
    set_target_properties(${target_name} PROPERTIES PREFIX "")
Marc Chevrier's avatar
Marc Chevrier committed
951
  elseif (swig_lowercase_language STREQUAL "ruby")
952
953
954
955
956
957
    # In ruby you want:
    #      require 'LIBRARY'
    # then ruby will look for a library whose name is platform dependent, namely
    #   MacOS  : LIBRARY.bundle
    #   Windows: LIBRARY.dll
    #   Linux  : LIBRARY.so
958
    set_target_properties (${target_name} PROPERTIES PREFIX "")
959
    if (APPLE)
960
      set_target_properties (${target_name} PROPERTIES SUFFIX ".bundle")
961
    endif ()
Marc Chevrier's avatar
Marc Chevrier committed
962
  elseif (swig_lowercase_language STREQUAL "perl")
963
    # assume empty prefix because we expect the module to be dynamically loaded
964
    set_target_properties (${target_name} PROPERTIES PREFIX "")
965
    if (APPLE)
966
      set_target_properties (${target_name} PROPERTIES SUFFIX ".dylib")
967
    endif ()
968
969
  elseif (swig_lowercase_language STREQUAL "fortran")
    # Do *not* override the target's library prefix
970
971
972
973
974
975
976
977
978
979
980
  elseif (swig_lowercase_language STREQUAL "csharp")
    cmake_policy(GET CMP0122 csharp_naming_policy)
    if (csharp_naming_policy STREQUAL "NEW")
      # Do *not* override the target's library prefix
    else()
      if (NOT csharp_naming_policy)
        cmake_policy(GET_WARNING CMP0122 _cmp0122_warning)
        message(AUTHOR_WARNING "${_cmp0122_warning}\n")
      endif()
      set_target_properties (${target_name} PROPERTIES PREFIX "")
    endif()
981
982
  else()
    # assume empty prefix because we expect the module to be dynamically loaded
983
    set_target_properties (${target_name} PROPERTIES PREFIX "")
984
  endif ()
985

986
987
  # target property SWIG_SUPPORT_FILES_DIRECTORY specify output directories of support files
  set_property (TARGET ${target_name} PROPERTY SWIG_SUPPORT_FILES_DIRECTORY ${swig_generated_outdirs})
988
  # target property SWIG_SUPPORT_FILES lists principal proxy support files
Marc Chevrier's avatar
Marc Chevrier committed
989
990
  if (NOT SWIG_MODULE_${name}_NOPROXY)
    string(TOUPPER "${_SAM_LANGUAGE}" swig_uppercase_language)
991
    set(swig_all_support_files)
Marc Chevrier's avatar
Marc Chevrier committed
992
993
994
    foreach (swig_it IN LISTS SWIG_${swig_uppercase_language}_EXTRA_FILE_EXTENSIONS)
      set (swig_support_files ${swig_generated_sources})
      list (FILTER swig_support_files INCLUDE REGEX ".*${swig_it}$")
995
      list(APPEND swig_all_support_files ${swig_support_files})
Marc Chevrier's avatar
Marc Chevrier committed
996
    endforeach()
997
998
999
    if (swig_all_support_files)
      list(REMOVE_DUPLICATES swig_all_support_files)
    endif()
1000
    set_property (TARGET ${target_name} PROPERTY SWIG_SUPPORT_FILES ${swig_all_support_files})
Marc Chevrier's avatar
Marc Chevrier committed
1001
1002
1003
1004
1005
  endif()

  # to ensure legacy behavior, export some variables
  set (SWIG_MODULE_${name}_LANGUAGE "${SWIG_MODULE_${name}_LANGUAGE}" PARENT_SCOPE)
  set (SWIG_MODULE_${name}_SWIG_LANGUAGE_FLAG "${SWIG_MODULE_${name}_SWIG_LANGUAGE_FLAG}" PARENT_SCOPE)
1006
  set (SWIG_MODULE_${name}_REAL_NAME "${target_name}" PARENT_SCOPE)
Marc Chevrier's avatar
Marc Chevrier committed
1007
1008
1009
1010
1011
1012
  set (SWIG_MODULE_${name}_NOPROXY "${SWIG_MODULE_${name}_NOPROXY}" PARENT_SCOPE)
  set (SWIG_MODULE_${name}_EXTRA_FLAGS "${SWIG_MODULE_${name}_EXTRA_FLAGS}" PARENT_SCOPE)
  # the last one is a bit crazy but it is documented, so...
  # NOTA: works as expected if only ONE input file is specified
  set (swig_generated_file_fullname "${swig_generated_file_fullname}" PARENT_SCOPE)
endfunction()
1013
1014
1015
1016

#
# Like TARGET_LINK_LIBRARIES but for swig modules
#
Marc Chevrier's avatar
Marc Chevrier committed
1017
function(SWIG_LINK_LIBRARIES name)
1018
1019
  if (UseSWIG_TARGET_NAME_PREFERENCE STREQUAL "STANDARD")
    message(DEPRECATION "SWIG_LINK_LIBRARIES is deprecated. Use TARGET_LINK_LIBRARIES instead.")
Marc Chevrier's avatar
Marc Chevrier committed
1020
    target_link_libraries(${name} ${ARGN})
1021
  else()
1022
1023
1024
1025
1026
    if(SWIG_MODULE_${name}_REAL_NAME)
      target_link_libraries(${SWIG_MODULE_${name}_REAL_NAME} ${ARGN})
    else()
      message(SEND_ERROR "Cannot find Swig library \"${name}\".")
    endif()
1027
  endif()
Marc Chevrier's avatar
Marc Chevrier committed
1028
endfunction()
1029
1030

cmake_policy(POP)