UseSWIG.cmake 26.4 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
  Define swig module with given name and specified language::
16

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

26
27
28
29
  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`).
30

31
32
33
34
35
  .. 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.
36

Marc Chevrier's avatar
Marc Chevrier committed
37
  ``TYPE``
38
39
40
41
42
    ``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.
43

Marc Chevrier's avatar
Marc Chevrier committed
44
45
  ``LANGUAGE``
    Specify the target language.
46

Marc Chevrier's avatar
Marc Chevrier committed
47
48
49
50
  ``NO_PROXY``
    Prevent the generation of the wrapper layer (swig ``-noproxy`` option).

  ``OUTPUT_DIR``
51
52
53
54
55
56
57
58
59
60
    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
61
62

  ``OUTFILE_DIR``
63
64
65
66
    Specify an output directory name where the generated source file will be
    placed (swig -o option). If not specified, the ``SWIG_OUTFILE_DIR`` variable
    will be used. If neither is specified, ``OUTPUT_DIR`` or
    ``CMAKE_SWIG_OUTDIR`` is used instead.
Marc Chevrier's avatar
Marc Chevrier committed
67
68

  ``SOURCES``
69
70
71
72
73
    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
    the standard way.

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

75
76
77
78
79
80
81
82
83
84
  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.

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.
85

86
87
``CPLUSPLUS``
  Call SWIG in c++ mode.  For example:
88

89
  .. code-block:: cmake
90

91
92
    set_property(SOURCE mymod.i PROPERTY CPLUSPLUS ON)
    swig_add_library(mymod LANGUAGE python SOURCES mymod.i)
93

Marc Chevrier's avatar
Marc Chevrier committed
94
95
``INCLUDE_DIRECTORIES``, ``COMPILE_DEFINITIONS`` and ``COMPILE_OPTIONS``
  Add custom flags to SWIG compiler and have same semantic as properties
96
97
  :prop_sf:`INCLUDE_DIRECTORIES`, :prop_sf:`COMPILE_DEFINITIONS` and
  :prop_sf:`COMPILE_OPTIONS`.
Marc Chevrier's avatar
Marc Chevrier committed
98
99
100
101
102

``GENERATED_INCLUDE_DIRECTORIES``, ``GENERATED_COMPILE_DEFINITIONS`` and ``GENERATED_COMPILE_OPTIONS``
  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.
103

Marc Chevrier's avatar
Marc Chevrier committed
104
105
``DEPENDS``
  Specify additional dependencies to the source file.
106

107
108
109
110
``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:
111

112
  .. code-block:: cmake
113

114
115
    set_property(SOURCE mymod.i PROPERTY SWIG_MODULE_NAME mymod_realname)

116
117
Target library properties can be set to apply same configuration to all SWIG
input files.
Marc Chevrier's avatar
Marc Chevrier committed
118
119

``SWIG_INCLUDE_DIRECTORIES``, ``SWIG_COMPILE_DEFINITIONS`` and ``SWIG_COMPILE_OPTIONS``
120
121
122
  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
123
124
125
126
127
128
129
130

  .. code-block:: cmake

    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)

``SWIG_GENERATED_INCLUDE_DIRECTORIES``, ``SWIG_GENERATED_COMPILE_DEFINITIONS`` and ``SWIG_GENERATED_COMPILE_OPTIONS``
131
132
133
  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
134
135
136
137

``SWIG_DEPENDS``
  Add dependencies to all SWIG input files.

138
139
140
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
141
142
143
144
145
146
147
148
``SWIG_SUPPORT_FILES``
  This output property list of wrapper files generated during SWIG compilation.

  .. code-block:: cmake

    swig_add_library(mymod LANGUAGE python SOURCES mymod.i)
    get_property(support_files TARGET mymod PROPERTY SWIG_SUPPORT_FILES)

149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
  .. 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``
  This output property specifies the directory where support files will be
  generated.

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

``UseSWIG_MODULE_VERSION``
  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.
169
170
171
172
173
174
175
176

``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``
177
178
  Specify an output directory name where the generated source file will be
  placed.  If not specified, ``CMAKE_SWIG_OUTDIR`` is used.
179
180
181

``SWIG_MODULE_<name>_EXTRA_DEPS``
  Specify extra dependencies for the generated module for ``<name>``.
182
#]=======================================================================]
183

Marc Chevrier's avatar
Marc Chevrier committed
184
185
186

cmake_policy (VERSION 3.11)

187
188
set(SWIG_CXX_EXTENSION "cxx")
set(SWIG_EXTRA_LIBRARIES "")
189

190
191
set(SWIG_PYTHON_EXTRA_FILE_EXTENSIONS ".py")
set(SWIG_JAVA_EXTRA_FILE_EXTENSIONS ".java" "JNI.java")
192

Marc Chevrier's avatar
Marc Chevrier committed
193
194
195
196
197
198
199
200
201
202
203
204
205
##
## 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()

206
207
208
#
# For given swig module initialize variables associated with it
#
209
macro(SWIG_MODULE_INITIALIZE name language)
Marc Chevrier's avatar
Marc Chevrier committed
210
211
  string(TOUPPER "${language}" SWIG_MODULE_${name}_LANGUAGE)
  string(TOLOWER "${language}" SWIG_MODULE_${name}_SWIG_LANGUAGE_FLAG)
212

Marc Chevrier's avatar
Marc Chevrier committed
213
214
215
216
217
  set(SWIG_MODULE_${name}_EXTRA_FLAGS)
  if (NOT DEFINED SWIG_MODULE_${name}_NOPROXY)
    set (SWIG_MODULE_${name}_NOPROXY FALSE)
  endif()
  if ("-noproxy" IN_LIST CMAKE_SWIG_FLAGS)
218
219
    set (SWIG_MODULE_${name}_NOPROXY TRUE)
  endif ()
Marc Chevrier's avatar
Marc Chevrier committed
220
221
222
223
224

  if (SWIG_MODULE_${name}_NOPROXY AND NOT "-noproxy" IN_LIST CMAKE_SWIG_FLAGS)
    list (APPEND SWIG_MODULE_${name}_EXTRA_FLAGS "-noproxy")
  endif()
  if(SWIG_MODULE_${name}_LANGUAGE STREQUAL "UNKNOWN")
225
    message(FATAL_ERROR "SWIG Error: Language \"${language}\" not found")
Marc Chevrier's avatar
Marc Chevrier committed
226
227
  elseif(SWIG_MODULE_${name}_LANGUAGE STREQUAL "PERL")
    list(APPEND SWIG_MODULE_${name}_EXTRA_FLAGS "-shadow")
228
  endif()
229
endmacro()
230
231
232

#
# For a given language, input file, and output file, determine extra files that
233
# will be generated. This is internal swig macro.
234
235
#

Marc Chevrier's avatar
Marc Chevrier committed
236
237
238
239
240
function(SWIG_GET_EXTRA_OUTPUT_FILES language outfiles generatedpath infile)
  set(files)
  get_source_file_property(module_basename
    "${infile}" SWIG_MODULE_NAME)
  if(NOT swig_module_basename)
241
242

    # try to get module name from "%module foo" syntax
Marc Chevrier's avatar
Marc Chevrier committed
243
244
    if ( EXISTS "${infile}" )
      file ( STRINGS "${infile}" module_basename REGEX "[ ]*%module[ ]*[a-zA-Z0-9_]+.*" )
245
    endif ()
Marc Chevrier's avatar
Marc Chevrier committed
246
247
    if ( module_basename )
      string ( REGEX REPLACE "[ ]*%module[ ]*([a-zA-Z0-9_]+).*" "\\1" module_basename "${module_basename}" )
248
249
250

    else ()
      # try to get module name from "%module (options=...) foo" syntax
Marc Chevrier's avatar
Marc Chevrier committed
251
252
      if ( EXISTS "${infile}" )
        file ( STRINGS "${infile}" module_basename REGEX "[ ]*%module[ ]*\\(.*\\)[ ]*[a-zA-Z0-9_]+.*" )
253
      endif ()
Marc Chevrier's avatar
Marc Chevrier committed
254
255
      if ( module_basename )
        string ( REGEX REPLACE "[ ]*%module[ ]*\\(.*\\)[ ]*([a-zA-Z0-9_]+).*" "\\1" module_basename "${module_basename}" )
256
257
258

      else ()
        # fallback to file basename
Marc Chevrier's avatar
Marc Chevrier committed
259
        get_filename_component(module_basename "${infile}" NAME_WE)
260
261
262
      endif ()
    endif ()

263
  endif()
264
  foreach(it ${SWIG_${language}_EXTRA_FILE_EXTENSIONS})
Marc Chevrier's avatar
Marc Chevrier committed
265
266
    set(extra_file "${generatedpath}/${module_basename}${it}")
    list(APPEND files "${extra_file}")
267
  endforeach()
Marc Chevrier's avatar
Marc Chevrier committed
268
269
270
271
272
    # Treat extra outputs as plain files regardless of language.
  set_source_files_properties(${files} PROPERTIES LANGUAGE "")

  set (${outfiles} ${files} PARENT_SCOPE)
endfunction()
273
274
275
276

#
# Take swig (*.i) file and add proper custom commands for it
#
Marc Chevrier's avatar
Marc Chevrier committed
277
function(SWIG_ADD_SOURCE_TO_MODULE name outfiles infile)
278
  get_filename_component(swig_source_file_name_we "${infile}" NAME_WE)
Marc Chevrier's avatar
Marc Chevrier committed
279
  get_source_file_property(swig_source_file_cplusplus "${infile}" CPLUSPLUS)
280

Ken Martin's avatar
Ken Martin committed
281
  # If CMAKE_SWIG_OUTDIR was specified then pass it to -outdir
282
  if(CMAKE_SWIG_OUTDIR)
Marc Chevrier's avatar
Marc Chevrier committed
283
    set(outdir ${CMAKE_SWIG_OUTDIR})
284
  else()
Marc Chevrier's avatar
Marc Chevrier committed
285
    set(outdir ${CMAKE_CURRENT_BINARY_DIR})
286
  endif()
287
288

  if(SWIG_OUTFILE_DIR)
Marc Chevrier's avatar
Marc Chevrier committed
289
    set(outfiledir ${SWIG_OUTFILE_DIR})
290
  else()
Marc Chevrier's avatar
Marc Chevrier committed
291
    set(outfiledir ${outdir})
292
293
  endif()

Marc Chevrier's avatar
Marc Chevrier committed
294
295
296
297
298
299
300
301
302
303
  if(SWIG_WORKING_DIR)
    set (workingdir "${SWIG_WORKING_DIR}")
  else()
    set(workingdir "${outdir}")
  endif()

  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)
304
    list (APPEND swig_source_file_flags "$<$<BOOL:${include_directories}>:-I$<JOIN:${include_directories},$<SEMICOLON>-I>>")
Marc Chevrier's avatar
Marc Chevrier committed
305
306
307
308
309
310
311
312
  endif()
  set (property "$<TARGET_PROPERTY:${name},SWIG_INCLUDE_DIRECTORIES>")
  list (APPEND swig_source_file_flags "$<$<BOOL:${property}>:-I$<JOIN:${property},$<SEMICOLON>-I>>")

  set (property "$<TARGET_PROPERTY:${name},SWIG_COMPILE_DEFINITIONS>")
  list (APPEND swig_source_file_flags "$<$<BOOL:${property}>:-D$<JOIN:${property},$<SEMICOLON>-D>>")
  get_source_file_property (compile_definitions "${infile}" COMPILE_DEFINITIONS)
  if (compile_definitions)
313
    list (APPEND swig_source_file_flags "$<$<BOOL:${compile_definitions}>:-D$<JOIN:${compile_definitions},$<SEMICOLON>-D>>")
Marc Chevrier's avatar
Marc Chevrier committed
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
  endif()

  list (APPEND swig_source_file_flags "$<TARGET_PROPERTY:${name},SWIG_COMPILE_OPTIONS>")
  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)

330
331
332
  if (NOT SWIG_MODULE_${name}_NOPROXY)
    SWIG_GET_EXTRA_OUTPUT_FILES(${SWIG_MODULE_${name}_LANGUAGE}
      swig_extra_generated_files
Marc Chevrier's avatar
Marc Chevrier committed
333
      "${outdir}"
334
335
      "${swig_source_file_fullname}")
  endif()
336
  set(swig_generated_file_fullname
Marc Chevrier's avatar
Marc Chevrier committed
337
    "${outfiledir}/${swig_source_file_name_we}")
Bill Hoffman's avatar
Bill Hoffman committed
338
339
  # 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
340
341
  string(APPEND swig_generated_file_fullname
    "${SWIG_MODULE_${name}_LANGUAGE}_wrap")
342

343
  if(swig_source_file_cplusplus)
Daniel Pfeifer's avatar
Daniel Pfeifer committed
344
345
    string(APPEND swig_generated_file_fullname
      ".${SWIG_CXX_EXTENSION}")
346
  else()
Daniel Pfeifer's avatar
Daniel Pfeifer committed
347
348
    string(APPEND swig_generated_file_fullname
      ".c")
349
  endif()
350

Marc Chevrier's avatar
Marc Chevrier committed
351
352
353
354
  get_directory_property (cmake_include_directories INCLUDE_DIRECTORIES)
  list (REMOVE_DUPLICATES cmake_include_directories)
  set (swig_include_dirs)
  if (cmake_include_directories)
355
    set (swig_include_dirs "$<$<BOOL:${cmake_include_directories}>:-I$<JOIN:${cmake_include_directories},$<SEMICOLON>-I>>")
Marc Chevrier's avatar
Marc Chevrier committed
356
  endif()
357

358
  set(swig_special_flags)
Bill Hoffman's avatar
Bill Hoffman committed
359
  # default is c, so add c++ flag if it is c++
360
  if(swig_source_file_cplusplus)
Marc Chevrier's avatar
Marc Chevrier committed
361
    list (APPEND swig_special_flags "-c++")
362
  endif()
Marc Chevrier's avatar
Marc Chevrier committed
363
364
365
366

  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))
367
368
      # This makes sure that the name used in the generated DllImport
      # matches the library name created by CMake
Marc Chevrier's avatar
Marc Chevrier committed
369
      list (APPEND SWIG_MODULE_${name}_EXTRA_FLAGS "-dllimport" "${name}")
370
371
    endif()
  endif()
Marc Chevrier's avatar
Marc Chevrier committed
372
373
374
375
376
377
378
  list (APPEND swig_extra_flags ${SWIG_MODULE_${name}_EXTRA_FLAGS})

  # dependencies
  set (swig_dependencies ${SWIG_MODULE_${name}_EXTRA_DEPS} $<TARGET_PROPERTY:${name},SWIG_DEPENDS>)
  get_source_file_property(file_depends "${infile}" DEPENDS)
  if (file_depends)
    list (APPEND swig_dependencies ${file_depends})
379
  endif()
Marc Chevrier's avatar
Marc Chevrier committed
380

381
  if (UseSWIG_MODULE_VERSION VERSION_GREATER 1)
382
383
384
385
386
387
388
    # as part of custom command, start by removing old generated files
    # to ensure obsolete files do not stay
    set (swig_cleanup_command COMMAND "${CMAKE_COMMAND}" -E remove_directory "${outdir}")
  else()
    unset (swig_cleanup_command)
  endif()

389
390
391
392
  # IMPLICIT_DEPENDS below 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
  if(CMAKE_GENERATOR MATCHES "Make")
Marc Chevrier's avatar
Marc Chevrier committed
393
394
395
    __swig_compute_timestamp(${name} ${SWIG_MODULE_${name}_LANGUAGE}
      "${infile}" "${workingdir}" swig_generated_timestamp)
    set(swig_custom_output "${swig_generated_timestamp}")
396
397
398
    set(swig_custom_products
      BYPRODUCTS "${swig_generated_file_fullname}" ${swig_extra_generated_files})
    set(swig_timestamp_command
Marc Chevrier's avatar
Marc Chevrier committed
399
      COMMAND ${CMAKE_COMMAND} -E touch "${swig_generated_timestamp}")
400
401
402
403
404
405
  else()
    set(swig_custom_output
      "${swig_generated_file_fullname}" ${swig_extra_generated_files})
    set(swig_custom_products)
    set(swig_timestamp_command)
  endif()
406
  add_custom_command(
407
408
    OUTPUT ${swig_custom_output}
    ${swig_custom_products}
409
    ${swig_cleanup_command}
Marc Chevrier's avatar
Marc Chevrier committed
410
411
    # Let's create the ${outdir} at execution time, in case dir contains $(OutDir)
    COMMAND "${CMAKE_COMMAND}" -E make_directory ${outdir} ${outfiledir}
412
    ${swig_timestamp_command}
413
    COMMAND "${CMAKE_COMMAND}" -E env "SWIG_LIB=${SWIG_DIR}" "${SWIG_EXECUTABLE}"
Marc Chevrier's avatar
Marc Chevrier committed
414
415
416
    "-${SWIG_MODULE_${name}_SWIG_LANGUAGE_FLAG}"
    "${swig_source_file_flags}"
    -outdir "${outdir}"
Ken Martin's avatar
Ken Martin committed
417
418
    ${swig_special_flags}
    ${swig_extra_flags}
Marc Chevrier's avatar
Marc Chevrier committed
419
    "${swig_include_dirs}"
Ken Martin's avatar
Ken Martin committed
420
421
    -o "${swig_generated_file_fullname}"
    "${swig_source_file_fullname}"
422
    MAIN_DEPENDENCY "${swig_source_file_fullname}"
Marc Chevrier's avatar
Marc Chevrier committed
423
    DEPENDS ${swig_dependencies}
424
    IMPLICIT_DEPENDS CXX "${swig_source_file_fullname}"
Marc Chevrier's avatar
Marc Chevrier committed
425
426
    COMMENT "Swig source"
    COMMAND_EXPAND_LISTS)
427
  set_source_files_properties("${swig_generated_file_fullname}" ${swig_extra_generated_files}
428
    PROPERTIES GENERATED 1)
Marc Chevrier's avatar
Marc Chevrier committed
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444

  ## add all properties for generated file to various properties
  get_property (include_directories SOURCE "${infile}" PROPERTY GENERATED_INCLUDE_DIRECTORIES)
  set_property (SOURCE "${swig_generated_file_fullname}" PROPERTY INCLUDE_DIRECTORIES ${include_directories} $<TARGET_PROPERTY:${name},SWIG_GENERATED_INCLUDE_DIRECTORIES>)

  get_property (compile_definitions SOURCE "${infile}" PROPERTY GENERATED_COMPILE_DEFINITIONS)
  set_property (SOURCE "${swig_generated_file_fullname}" PROPERTY COMPILE_DEFINITIONS $<TARGET_PROPERTY:${name},SWIG_GENERATED_COMPILE_DEFINITIONS> ${compile_definitions})

  get_property (compile_options SOURCE "${infile}" PROPERTY GENERATED_COMPILE_OPTIONS)
  set_property (SOURCE "${swig_generated_file_fullname}" PROPERTY COMPILE_OPTIONS $<TARGET_PROPERTY:${name},SWIG_GENERATED_COMPILE_OPTIONS> ${compile_options})

  set(${outfiles} "${swig_generated_file_fullname}" ${swig_extra_generated_files} PARENT_SCOPE)

  # legacy support
  set (swig_generated_file_fullname "${swig_generated_file_fullname}" PARENT_SCOPE)
endfunction()
445
446
447
448

#
# Create Swig module
#
449
macro(SWIG_ADD_MODULE name language)
450
451
452
453
454
455
456
457
  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
458
459
function(SWIG_ADD_LIBRARY name)
  set(options NO_PROXY)
460
  set(oneValueArgs LANGUAGE
Marc Chevrier's avatar
Marc Chevrier committed
461
462
463
                   TYPE
                   OUTPUT_DIR
                   OUTFILE_DIR)
464
465
466
  set(multiValueArgs SOURCES)
  cmake_parse_arguments(_SAM "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})

Marc Chevrier's avatar
Marc Chevrier committed
467
468
469
470
471
472
473
474
475
476
477
  if (TARGET ${name})
    # a target with same name is already defined.
    # call NOW add_library command to raise the most useful error message
    add_library(${name})
    return()
  endif()

  if (_SAM_UNPARSED_ARGUMENTS)
    message(FATAL_ERROR "SWIG_ADD_LIBRARY: ${_SAM_UNPARSED_ARGUMENTS}: unexpected arguments")
  endif()

478
479
480
481
482
483
484
485
486
487
  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
488
  elseif(_SAM_TYPE STREQUAL "USE_BUILD_SHARED_LIBS")
489
    unset(_SAM_TYPE)
490
491
  endif()

492
493
494
495
496
497
  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()

Marc Chevrier's avatar
Marc Chevrier committed
498
499
500
501
  set (workingdir "${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/${name}.dir")
  # 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}")
502

Marc Chevrier's avatar
Marc Chevrier committed
503
504
505
506
  set (outputdir "${_SAM_OUTPUT_DIR}")
  if (NOT _SAM_OUTPUT_DIR)
    if (CMAKE_SWIG_OUTDIR)
      set (outputdir "${CMAKE_SWIG_OUTDIR}")
507
    else()
508
      if (UseSWIG_MODULE_VERSION VERSION_GREATER 1)
509
510
511
512
        set (outputdir "${workingdir}/${_SAM_LANGUAGE}.files")
      else()
        set (outputdir "${CMAKE_CURRENT_BINARY_DIR}")
      endif()
513
    endif()
Marc Chevrier's avatar
Marc Chevrier committed
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
  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}")

  set (SWIG_MODULE_${name}_NOPROXY ${_SAM_NO_PROXY})
  swig_module_initialize(${name} ${_SAM_LANGUAGE})

  set(swig_dot_i_sources ${_SAM_SOURCES})
  list(FILTER swig_dot_i_sources INCLUDE REGEX "\\.i$")
538
539
540
  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
541
542
  set(swig_other_sources ${_SAM_SOURCES})
  list(REMOVE_ITEM swig_other_sources ${swig_dot_i_sources})
543
544

  set(swig_generated_sources)
Marc Chevrier's avatar
Marc Chevrier committed
545
546
547
548
549
550
551
552
553
  set(swig_generated_timestamps)
  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}")
    if(CMAKE_GENERATOR MATCHES "Make")
      __swig_compute_timestamp(${name} ${SWIG_MODULE_${name}_LANGUAGE} "${swig_it}"
        "${workingdir}" swig_timestamp)
      list (APPEND swig_generated_timestamps "${swig_timestamp}")
    endif()
554
  endforeach()
Marc Chevrier's avatar
Marc Chevrier committed
555
556
  set_property (DIRECTORY APPEND PROPERTY
    ADDITIONAL_MAKE_CLEAN_FILES ${swig_generated_sources} ${swig_generated_timestamps})
557
  if (UseSWIG_MODULE_VERSION VERSION_GREATER 1)
558
559
    set_property (DIRECTORY APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES "${outputdir}")
  endif()
Marc Chevrier's avatar
Marc Chevrier committed
560
561

  add_library(${name}
562
    ${_SAM_TYPE}
563
564
    ${swig_generated_sources}
    ${swig_other_sources})
565
566
  if(CMAKE_GENERATOR MATCHES "Make")
    # see IMPLICIT_DEPENDS above
Marc Chevrier's avatar
Marc Chevrier committed
567
568
    add_custom_target(${name}_swig_compilation DEPENDS ${swig_generated_timestamps})
    add_dependencies(${name} ${name}_swig_compilation)
569
  endif()
Marc Chevrier's avatar
Marc Chevrier committed
570
571
  if(_SAM_TYPE STREQUAL "MODULE")
    set_target_properties(${name} PROPERTIES NO_SONAME ON)
572
  endif()
573
  string(TOLOWER "${_SAM_LANGUAGE}" swig_lowercase_language)
Marc Chevrier's avatar
Marc Chevrier committed
574
575
576
577
578
579
  if (swig_lowercase_language STREQUAL "octave")
    set_target_properties(${name} PROPERTIES PREFIX "")
    set_target_properties(${name} PROPERTIES SUFFIX ".oct")
  elseif (swig_lowercase_language STREQUAL "go")
    set_target_properties(${name} PROPERTIES PREFIX "")
  elseif (swig_lowercase_language STREQUAL "java")
580
581
582
583
584
585
    # 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
586
    if (APPLE)
587
588
589
590
591
      set_target_properties (${name} PROPERTIES SUFFIX ".jnilib")
    endif()
    if ((WIN32 AND MINGW) OR CYGWIN OR CMAKE_SYSTEM_NAME STREQUAL MSYS)
      set_target_properties(${name} PROPERTIES PREFIX "")
    endif()
Marc Chevrier's avatar
Marc Chevrier committed
592
593
594
  elseif (swig_lowercase_language STREQUAL "lua")
    if(_SAM_TYPE STREQUAL "MODULE")
      set_target_properties(${name} PROPERTIES PREFIX "")
595
    endif()
Marc Chevrier's avatar
Marc Chevrier committed
596
  elseif (swig_lowercase_language STREQUAL "python")
597
598
599
600
601
602
603
604
    if (SWIG_MODULE_${name}_NOPROXY)
      set_target_properties(${name} PROPERTIES PREFIX "")
    else()
      # 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_properties(${name} PROPERTIES PREFIX "_")
    endif()
605
606
607
608
609
610
611
612
    # 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>
613
    if(WIN32 AND NOT CYGWIN)
Marc Chevrier's avatar
Marc Chevrier committed
614
      set_target_properties(${name} PROPERTIES SUFFIX ".pyd")
615
    endif()
Marc Chevrier's avatar
Marc Chevrier committed
616
617
618
  elseif (swig_lowercase_language STREQUAL "r")
    set_target_properties(${name} PROPERTIES PREFIX "")
  elseif (swig_lowercase_language STREQUAL "ruby")
619
620
621
622
623
624
    # 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
Marc Chevrier's avatar
Marc Chevrier committed
625
    set_target_properties (${name} PROPERTIES PREFIX "")
626
    if (APPLE)
Marc Chevrier's avatar
Marc Chevrier committed
627
      set_target_properties (${name} PROPERTIES SUFFIX ".bundle")
628
    endif ()
Marc Chevrier's avatar
Marc Chevrier committed
629
  elseif (swig_lowercase_language STREQUAL "perl")
630
    # assume empty prefix because we expect the module to be dynamically loaded
Marc Chevrier's avatar
Marc Chevrier committed
631
    set_target_properties (${name} PROPERTIES PREFIX "")
632
    if (APPLE)
Marc Chevrier's avatar
Marc Chevrier committed
633
      set_target_properties (${name} PROPERTIES SUFFIX ".dylib")
634
    endif ()
635
636
  else()
    # assume empty prefix because we expect the module to be dynamically loaded
Marc Chevrier's avatar
Marc Chevrier committed
637
    set_target_properties (${name} PROPERTIES PREFIX "")
638
  endif ()
639
640
641
642

  # target property SWIG_SUPPORT_FILES_DIRECTORY specify output directory of support files
  set_property (TARGET ${name} PROPERTY SWIG_SUPPORT_FILES_DIRECTORY "${outputdir}")
  # target property SWIG_SUPPORT_FILES lists principal proxy support files
Marc Chevrier's avatar
Marc Chevrier committed
643
644
  if (NOT SWIG_MODULE_${name}_NOPROXY)
    string(TOUPPER "${_SAM_LANGUAGE}" swig_uppercase_language)
645
    set(swig_all_support_files)
Marc Chevrier's avatar
Marc Chevrier committed
646
647
648
    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}$")
649
      list(APPEND swig_all_support_files ${swig_support_files})
Marc Chevrier's avatar
Marc Chevrier committed
650
    endforeach()
651
652
653
    if (swig_all_support_files)
      list(REMOVE_DUPLICATES swig_all_support_files)
    endif()
654
    set_property (TARGET ${name} PROPERTY SWIG_SUPPORT_FILES ${swig_all_support_files})
Marc Chevrier's avatar
Marc Chevrier committed
655
656
657
658
659
660
661
662
663
664
665
666
  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)
  set (SWIG_MODULE_${name}_REAL_NAME "${name}" PARENT_SCOPE)
  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()
667
668
669
670

#
# Like TARGET_LINK_LIBRARIES but for swig modules
#
Marc Chevrier's avatar
Marc Chevrier committed
671
672
function(SWIG_LINK_LIBRARIES name)
  message(DEPRECATION "SWIG_LINK_LIBRARIES is deprecated. Use TARGET_LINK_LIBRARIES instead.")
673
  if(SWIG_MODULE_${name}_REAL_NAME)
Marc Chevrier's avatar
Marc Chevrier committed
674
    target_link_libraries(${name} ${ARGN})
675
  else()
676
    message(SEND_ERROR "Cannot find Swig library \"${name}\".")
677
  endif()
Marc Chevrier's avatar
Marc Chevrier committed
678
endfunction()