Commit 7f965277 authored by Brad King's avatar Brad King Committed by Kitware Robot

Merge topic 'UseSWIG-legacy-targets'

8f2b483c Merge branch 'backport-UseSWIG-legacy-targets' into UseSWIG-legacy-targets
38ffcedd UseSWIG: reintroduce legacy target name management
Acked-by: Kitware Robot's avatarKitware Robot <kwrobot@kitware.com>
Merge-request: !2188
parents 5c60e80c 8f2b483c
......@@ -28,6 +28,12 @@ Defines the following command for use with ``SWIG``:
those targets can be used with any command expecting a target (e.g.
:command:`target_link_libraries`).
.. note::
The variable ``SWIG_MODULE_<name>_REAL_NAME`` will be set to the name
of the swig module target library. This variable is useless if variable
``UseSWIG_TARGET_NAME_PREFERENCE`` is set to ``STANDARD``.
.. note::
For multi-config generators, this module does not support
......@@ -70,14 +76,29 @@ Defines the following command for use with ``SWIG``:
identified as sources for the ``SWIG`` tool. Other files will be handled in
the standard way.
.. note::
.. 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.
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::
If variable ``UseSWIG_TARGET_NAME_PREFERENCE`` is set to ``STANDARD``, this
command is deprecated and :command:`target_link_libraries` command must be
used instead.
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
......@@ -130,6 +151,7 @@ input files.
.. code-block:: cmake
set (UseSWIG_TARGET_NAME_PREFERENCE STANDARD)
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)
......@@ -157,6 +179,7 @@ information about support files generated by ``SWIG`` interface compilation.
.. code-block:: cmake
set (UseSWIG_TARGET_NAME_PREFERENCE STANDARD)
swig_add_library(mymod LANGUAGE python SOURCES mymod.i)
get_property(support_files TARGET mymod PROPERTY SWIG_SUPPORT_FILES)
......@@ -174,6 +197,13 @@ information about support files generated by ``SWIG`` interface compilation.
Some variables can be set to customize the behavior of ``swig_add_library``
as well as ``SWIG``:
``UseSWIG_TARGET_NAME_PREFERENCE``
Specify target name strategy.
* Set to ``LEGACY`` or undefined: legacy strategy is applied. Variable
``SWIG_MODULE_<name>_REAL_NAME`` must be used to get real target name.
* Set to ``STANDARD``: target name matches specified name.
``UseSWIG_MODULE_VERSION``
Specify different behaviors for ``UseSWIG`` module.
......@@ -316,31 +346,37 @@ function(SWIG_ADD_SOURCE_TO_MODULE name outfiles infile)
set(workingdir "${outdir}")
endif()
if(SWIG_TARGET_NAME)
set(target_name ${SWIG_TARGET_NAME})
else()
set(target_name ${name})
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)
list (APPEND swig_source_file_flags "$<$<BOOL:${include_directories}>:-I$<JOIN:${include_directories},$<SEMICOLON>-I>>")
endif()
set (property "$<TARGET_PROPERTY:${name},SWIG_INCLUDE_DIRECTORIES>")
list (APPEND swig_source_file_flags "$<$<BOOL:${property}>:-I$<JOIN:$<TARGET_GENEX_EVAL:${name},${property}>,$<SEMICOLON>-I>>")
set (property "$<TARGET_PROPERTY:${name},INCLUDE_DIRECTORIES>")
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>>")
set (property "$<TARGET_PROPERTY:${target_name},INCLUDE_DIRECTORIES>")
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
list (APPEND swig_source_file_flags "$<$<AND:$<BOOL:$<TARGET_PROPERTY:${name},SWIG_USE_TARGET_INCLUDE_DIRECTORIES>>,$<BOOL:${property}>>:-I$<JOIN:${property},$<SEMICOLON>-I>>")
list (APPEND swig_source_file_flags "$<$<AND:$<BOOL:$<TARGET_PROPERTY:${target_name},SWIG_USE_TARGET_INCLUDE_DIRECTORIES>>,$<BOOL:${property}>>:-I$<JOIN:${property},$<SEMICOLON>-I>>")
endif()
set (property "$<TARGET_PROPERTY:${name},SWIG_COMPILE_DEFINITIONS>")
list (APPEND swig_source_file_flags "$<$<BOOL:${property}>:-D$<JOIN:$<TARGET_GENEX_EVAL:${name},${property}>,$<SEMICOLON>-D>>")
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>>")
get_source_file_property (compile_definitions "${infile}" COMPILE_DEFINITIONS)
if (compile_definitions)
list (APPEND swig_source_file_flags "$<$<BOOL:${compile_definitions}>:-D$<JOIN:${compile_definitions},$<SEMICOLON>-D>>")
endif()
list (APPEND swig_source_file_flags "$<TARGET_GENEX_EVAL:${name},$<TARGET_PROPERTY:${name},SWIG_COMPILE_OPTIONS>>")
list (APPEND swig_source_file_flags "$<TARGET_GENEX_EVAL:${target_name},$<TARGET_PROPERTY:${target_name},SWIG_COMPILE_OPTIONS>>")
get_source_file_property (compile_options "${infile}" COMPILE_OPTIONS)
if (compile_options)
list (APPEND swig_source_file_flags ${compile_options})
......@@ -399,7 +435,7 @@ function(SWIG_ADD_SOURCE_TO_MODULE name outfiles infile)
list (APPEND swig_extra_flags ${SWIG_MODULE_${name}_EXTRA_FLAGS})
# dependencies
set (swig_dependencies ${SWIG_MODULE_${name}_EXTRA_DEPS} $<TARGET_PROPERTY:${name},SWIG_DEPENDS>)
set (swig_dependencies ${SWIG_MODULE_${name}_EXTRA_DEPS} $<TARGET_PROPERTY:${target_name},SWIG_DEPENDS>)
get_source_file_property(file_depends "${infile}" DEPENDS)
if (file_depends)
list (APPEND swig_dependencies ${file_depends})
......@@ -456,13 +492,13 @@ function(SWIG_ADD_SOURCE_TO_MODULE name outfiles infile)
## 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_GENEX_EVAL:${name},$<TARGET_PROPERTY:${name},SWIG_GENERATED_INCLUDE_DIRECTORIES>>)
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>>)
get_property (compile_definitions SOURCE "${infile}" PROPERTY GENERATED_COMPILE_DEFINITIONS)
set_property (SOURCE "${swig_generated_file_fullname}" PROPERTY COMPILE_DEFINITIONS $<TARGET_GENEX_EVAL:${name},$<TARGET_PROPERTY:${name},SWIG_GENERATED_COMPILE_DEFINITIONS>> ${compile_definitions})
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})
get_property (compile_options SOURCE "${infile}" PROPERTY GENERATED_COMPILE_OPTIONS)
set_property (SOURCE "${swig_generated_file_fullname}" PROPERTY COMPILE_OPTIONS $<TARGET_GENEX_EVAL:${name},$<TARGET_PROPERTY:${name},SWIG_GENERATED_COMPILE_OPTIONS>> ${compile_options})
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})
set(${outfiles} "${swig_generated_file_fullname}" ${swig_extra_generated_files} PARENT_SCOPE)
......@@ -491,13 +527,6 @@ function(SWIG_ADD_LIBRARY name)
set(multiValueArgs SOURCES)
cmake_parse_arguments(_SAM "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
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()
......@@ -516,16 +545,44 @@ function(SWIG_ADD_LIBRARY name)
unset(_SAM_TYPE)
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()
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()
set (workingdir "${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/${name}.dir")
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")
# 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}")
set (SWIG_TARGET_NAME "${target_name}")
set (outputdir "${_SAM_OUTPUT_DIR}")
if (NOT _SAM_OUTPUT_DIR)
......@@ -557,9 +614,6 @@ function(SWIG_ADD_LIBRARY name)
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$")
if (NOT swig_dot_i_sources)
......@@ -585,24 +639,24 @@ function(SWIG_ADD_LIBRARY name)
set_property (DIRECTORY APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES "${outputdir}")
endif()
add_library(${name}
add_library(${target_name}
${_SAM_TYPE}
${swig_generated_sources}
${swig_other_sources})
if(CMAKE_GENERATOR MATCHES "Make")
# see IMPLICIT_DEPENDS above
add_custom_target(${name}_swig_compilation DEPENDS ${swig_generated_timestamps})
add_dependencies(${name} ${name}_swig_compilation)
add_dependencies(${target_name} ${name}_swig_compilation)
endif()
if(_SAM_TYPE STREQUAL "MODULE")
set_target_properties(${name} PROPERTIES NO_SONAME ON)
set_target_properties(${target_name} PROPERTIES NO_SONAME ON)
endif()
string(TOLOWER "${_SAM_LANGUAGE}" swig_lowercase_language)
if (swig_lowercase_language STREQUAL "octave")
set_target_properties(${name} PROPERTIES PREFIX "")
set_target_properties(${name} PROPERTIES SUFFIX ".oct")
set_target_properties(${target_name} PROPERTIES PREFIX "")
set_target_properties(${target_name} PROPERTIES SUFFIX ".oct")
elseif (swig_lowercase_language STREQUAL "go")
set_target_properties(${name} PROPERTIES PREFIX "")
set_target_properties(${target_name} PROPERTIES PREFIX "")
elseif (swig_lowercase_language STREQUAL "java")
# In java you want:
# System.loadLibrary("LIBRARY");
......@@ -611,23 +665,23 @@ function(SWIG_ADD_LIBRARY name)
# Windows: LIBRARY.dll
# Linux : libLIBRARY.so
if (APPLE)
set_target_properties (${name} PROPERTIES SUFFIX ".jnilib")
set_target_properties (${target_name} PROPERTIES SUFFIX ".jnilib")
endif()
if ((WIN32 AND MINGW) OR CYGWIN OR CMAKE_SYSTEM_NAME STREQUAL MSYS)
set_target_properties(${name} PROPERTIES PREFIX "")
set_target_properties(${target_name} PROPERTIES PREFIX "")
endif()
elseif (swig_lowercase_language STREQUAL "lua")
if(_SAM_TYPE STREQUAL "MODULE")
set_target_properties(${name} PROPERTIES PREFIX "")
set_target_properties(${target_name} PROPERTIES PREFIX "")
endif()
elseif (swig_lowercase_language STREQUAL "python")
if (SWIG_MODULE_${name}_NOPROXY)
set_target_properties(${name} PROPERTIES PREFIX "")
else()
if (UseSWIG_TARGET_NAME_PREFERENCE STREQUAL "STANDARD" 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_properties(${name} PROPERTIES PREFIX "_")
set_target_properties(${target_name} PROPERTIES PREFIX "_")
else()
set_target_properties(${target_name} PROPERTIES PREFIX "")
endif()
# Python extension modules on Windows must have the extension ".pyd"
# instead of ".dll" as of Python 2.5. Older python versions do support
......@@ -638,10 +692,10 @@ function(SWIG_ADD_LIBRARY name)
# .pyd is now the only filename extension that will be searched for.
# </quote>
if(WIN32 AND NOT CYGWIN)
set_target_properties(${name} PROPERTIES SUFFIX ".pyd")
set_target_properties(${target_name} PROPERTIES SUFFIX ".pyd")
endif()
elseif (swig_lowercase_language STREQUAL "r")
set_target_properties(${name} PROPERTIES PREFIX "")
set_target_properties(${target_name} PROPERTIES PREFIX "")
elseif (swig_lowercase_language STREQUAL "ruby")
# In ruby you want:
# require 'LIBRARY'
......@@ -649,23 +703,23 @@ function(SWIG_ADD_LIBRARY name)
# MacOS : LIBRARY.bundle
# Windows: LIBRARY.dll
# Linux : LIBRARY.so
set_target_properties (${name} PROPERTIES PREFIX "")
set_target_properties (${target_name} PROPERTIES PREFIX "")
if (APPLE)
set_target_properties (${name} PROPERTIES SUFFIX ".bundle")
set_target_properties (${target_name} PROPERTIES SUFFIX ".bundle")
endif ()
elseif (swig_lowercase_language STREQUAL "perl")
# assume empty prefix because we expect the module to be dynamically loaded
set_target_properties (${name} PROPERTIES PREFIX "")
set_target_properties (${target_name} PROPERTIES PREFIX "")
if (APPLE)
set_target_properties (${name} PROPERTIES SUFFIX ".dylib")
set_target_properties (${target_name} PROPERTIES SUFFIX ".dylib")
endif ()
else()
# assume empty prefix because we expect the module to be dynamically loaded
set_target_properties (${name} PROPERTIES PREFIX "")
set_target_properties (${target_name} PROPERTIES PREFIX "")
endif ()
# target property SWIG_SUPPORT_FILES_DIRECTORY specify output directory of support files
set_property (TARGET ${name} PROPERTY SWIG_SUPPORT_FILES_DIRECTORY "${outputdir}")
set_property (TARGET ${target_name} PROPERTY SWIG_SUPPORT_FILES_DIRECTORY "${outputdir}")
# target property SWIG_SUPPORT_FILES lists principal proxy support files
if (NOT SWIG_MODULE_${name}_NOPROXY)
string(TOUPPER "${_SAM_LANGUAGE}" swig_uppercase_language)
......@@ -678,13 +732,13 @@ function(SWIG_ADD_LIBRARY name)
if (swig_all_support_files)
list(REMOVE_DUPLICATES swig_all_support_files)
endif()
set_property (TARGET ${name} PROPERTY SWIG_SUPPORT_FILES ${swig_all_support_files})
set_property (TARGET ${target_name} PROPERTY SWIG_SUPPORT_FILES ${swig_all_support_files})
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}_REAL_NAME "${target_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...
......@@ -696,10 +750,14 @@ endfunction()
# Like TARGET_LINK_LIBRARIES but for swig modules
#
function(SWIG_LINK_LIBRARIES name)
if (UseSWIG_TARGET_NAME_PREFERENCE STREQUAL "STANDARD")
message(DEPRECATION "SWIG_LINK_LIBRARIES is deprecated. Use TARGET_LINK_LIBRARIES instead.")
if(SWIG_MODULE_${name}_REAL_NAME)
target_link_libraries(${name} ${ARGN})
else()
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()
endif()
endfunction()
......@@ -58,6 +58,7 @@ if(${language} MATCHES lua)
set(SWIG_LANG_LIBRARIES ${LUA_LIBRARIES})
endif()
set(UseSWIG_TARGET_NAME_PREFERENCE STANDARD)
unset(CMAKE_SWIG_FLAGS)
set (CMAKE_INCLUDE_CURRENT_DIR ON)
......
......@@ -16,6 +16,7 @@ else()
set (PS ":")
endif()
set(UseSWIG_TARGET_NAME_PREFERENCE STANDARD)
set (UseSWIG_MODULE_VERSION 2)
unset(CMAKE_SWIG_FLAGS)
......
......@@ -19,6 +19,7 @@ else()
set (PS ":")
endif()
set(UseSWIG_TARGET_NAME_PREFERENCE STANDARD)
unset(CMAKE_SWIG_FLAGS)
set_property(SOURCE "../example.i" PROPERTY CPLUSPLUS ON)
......
......@@ -17,6 +17,7 @@ else()
set (PS ":")
endif()
set(UseSWIG_TARGET_NAME_PREFERENCE STANDARD)
unset(CMAKE_SWIG_FLAGS)
set_property(SOURCE "../example.i" PROPERTY CPLUSPLUS ON)
......
......@@ -9,6 +9,7 @@ include(${SWIG_USE_FILE})
find_package(Python3 REQUIRED COMPONENTS Interpreter Development)
set(UseSWIG_TARGET_NAME_PREFERENCE STANDARD)
unset(CMAKE_SWIG_FLAGS)
set_property(SOURCE "example.i" PROPERTY CPLUSPLUS ON)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment