From 38ffcedd26af077cbfb0e0c2c96c5018df23849b Mon Sep 17 00:00:00 2001 From: Marc Chevrier Date: Tue, 3 Jul 2018 11:36:36 +0200 Subject: UseSWIG: reintroduce legacy target name management Fixes: #18140 --- Modules/UseSWIG.cmake | 164 ++++++++++++++++++--------- Tests/UseSWIG/BasicConfiguration.cmake | 1 + Tests/UseSWIG/ModuleVersion2/CMakeLists.txt | 1 + Tests/UseSWIG/MultipleModules/CMakeLists.txt | 1 + Tests/UseSWIG/MultiplePython/CMakeLists.txt | 1 + 5 files changed, 115 insertions(+), 53 deletions(-) diff --git a/Modules/UseSWIG.cmake b/Modules/UseSWIG.cmake index 7127b8f..b306181 100644 --- a/Modules/UseSWIG.cmake +++ b/Modules/UseSWIG.cmake @@ -30,6 +30,12 @@ Defines the following command for use with ``SWIG``: .. note:: + The variable ``SWIG_MODULE__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 configuration-specific files generated by ``SWIG``. All build configurations must result in the same generated source file. @@ -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. + +.. command:: swig_link_libraries + + Link libraries to swig module:: + + swig_link_libraries( ...) + + This command has same capabilities as :command:`target_link_libraries` + command. + + .. 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. + 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 @@ -123,6 +144,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) @@ -143,6 +165,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) @@ -160,6 +183,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__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. @@ -302,23 +332,29 @@ 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 "$<$:-I$-I>>") endif() - set (property "$") - list (APPEND swig_source_file_flags "$<$:-I$,$-I>>") + set (property "$") + list (APPEND swig_source_file_flags "$<$:-I$,$-I>>") - set (property "$") - list (APPEND swig_source_file_flags "$<$:-D$,$-D>>") + set (property "$") + list (APPEND swig_source_file_flags "$<$:-D$,$-D>>") get_source_file_property (compile_definitions "${infile}" COMPILE_DEFINITIONS) if (compile_definitions) list (APPEND swig_source_file_flags "$<$:-D$-D>>") endif() - list (APPEND swig_source_file_flags "$>") + list (APPEND swig_source_file_flags "$>") get_source_file_property (compile_options "${infile}" COMPILE_OPTIONS) if (compile_options) list (APPEND swig_source_file_flags ${compile_options}) @@ -377,7 +413,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} $) + set (swig_dependencies ${SWIG_MODULE_${name}_EXTRA_DEPS} $) get_source_file_property(file_depends "${infile}" DEPENDS) if (file_depends) list (APPEND swig_dependencies ${file_depends}) @@ -434,13 +470,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} $>) + set_property (SOURCE "${swig_generated_file_fullname}" PROPERTY INCLUDE_DIRECTORIES ${include_directories} $>) get_property (compile_definitions SOURCE "${infile}" PROPERTY GENERATED_COMPILE_DEFINITIONS) - set_property (SOURCE "${swig_generated_file_fullname}" PROPERTY COMPILE_DEFINITIONS $> ${compile_definitions}) + set_property (SOURCE "${swig_generated_file_fullname}" PROPERTY COMPILE_DEFINITIONS $> ${compile_definitions}) get_property (compile_options SOURCE "${infile}" PROPERTY GENERATED_COMPILE_OPTIONS) - set_property (SOURCE "${swig_generated_file_fullname}" PROPERTY COMPILE_OPTIONS $> ${compile_options}) + set_property (SOURCE "${swig_generated_file_fullname}" PROPERTY COMPILE_OPTIONS $> ${compile_options}) set(${outfiles} "${swig_generated_file_fullname}" ${swig_extra_generated_files} PARENT_SCOPE) @@ -469,13 +505,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() @@ -494,16 +523,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) @@ -535,9 +592,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) @@ -563,24 +617,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"); @@ -589,23 +643,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 @@ -616,10 +670,10 @@ function(SWIG_ADD_LIBRARY name) # .pyd is now the only filename extension that will be searched for. # 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' @@ -627,23 +681,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) @@ -656,13 +710,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... @@ -674,10 +728,14 @@ endfunction() # Like TARGET_LINK_LIBRARIES but for swig modules # function(SWIG_LINK_LIBRARIES name) - message(DEPRECATION "SWIG_LINK_LIBRARIES is deprecated. Use TARGET_LINK_LIBRARIES instead.") - if(SWIG_MODULE_${name}_REAL_NAME) + if (UseSWIG_TARGET_NAME_PREFERENCE STREQUAL "STANDARD") + message(DEPRECATION "SWIG_LINK_LIBRARIES is deprecated. Use TARGET_LINK_LIBRARIES instead.") target_link_libraries(${name} ${ARGN}) else() - message(SEND_ERROR "Cannot find Swig library \"${name}\".") + 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() diff --git a/Tests/UseSWIG/BasicConfiguration.cmake b/Tests/UseSWIG/BasicConfiguration.cmake index fd3ac40..ad34d39 100644 --- a/Tests/UseSWIG/BasicConfiguration.cmake +++ b/Tests/UseSWIG/BasicConfiguration.cmake @@ -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) diff --git a/Tests/UseSWIG/ModuleVersion2/CMakeLists.txt b/Tests/UseSWIG/ModuleVersion2/CMakeLists.txt index 3f9d363..452f9e2 100644 --- a/Tests/UseSWIG/ModuleVersion2/CMakeLists.txt +++ b/Tests/UseSWIG/ModuleVersion2/CMakeLists.txt @@ -16,6 +16,7 @@ else() set (PS ":") endif() +set(UseSWIG_TARGET_NAME_PREFERENCE STANDARD) set (UseSWIG_MODULE_VERSION 2) unset(CMAKE_SWIG_FLAGS) diff --git a/Tests/UseSWIG/MultipleModules/CMakeLists.txt b/Tests/UseSWIG/MultipleModules/CMakeLists.txt index 578825f..25dd6b3 100644 --- a/Tests/UseSWIG/MultipleModules/CMakeLists.txt +++ b/Tests/UseSWIG/MultipleModules/CMakeLists.txt @@ -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) diff --git a/Tests/UseSWIG/MultiplePython/CMakeLists.txt b/Tests/UseSWIG/MultiplePython/CMakeLists.txt index a5e3eed..a1651fb 100644 --- a/Tests/UseSWIG/MultiplePython/CMakeLists.txt +++ b/Tests/UseSWIG/MultiplePython/CMakeLists.txt @@ -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) -- cgit v0.12