From 2d4f3d6a6dde394b00cc9073a923b26f447c420f Mon Sep 17 00:00:00 2001 From: Marc Chevrier Date: Sat, 20 Sep 2025 16:06:24 +0200 Subject: [PATCH] UseSWIG: Add POSTFIX management Fixes: #27185 --- Help/release/dev/UseSWIG-POSTFIX.rst | 6 +++++ Modules/UseSWIG.cmake | 31 ++++++++++++++++++++++- Tests/RunCMake/UseSWIG/RunCMakeTest.cmake | 7 +++++ Tests/RunCMake/UseSWIG/SetPOSTFIX.cmake | 28 ++++++++++++++++++++ Tests/RunCMake/UseSWIG/runme.py | 7 +++++ 5 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 Help/release/dev/UseSWIG-POSTFIX.rst create mode 100644 Tests/RunCMake/UseSWIG/SetPOSTFIX.cmake create mode 100644 Tests/RunCMake/UseSWIG/runme.py diff --git a/Help/release/dev/UseSWIG-POSTFIX.rst b/Help/release/dev/UseSWIG-POSTFIX.rst new file mode 100644 index 0000000000..3d2f932cc8 --- /dev/null +++ b/Help/release/dev/UseSWIG-POSTFIX.rst @@ -0,0 +1,6 @@ +UseSWIG-POSTFIX +--------------- + +* The ``swig_add_library()`` command, from the :module:`UseSWIG` module, gained + the option ``DEBUG_POSTFIX`` for the management of the + :prop_tgt:`DEBUG_POSTFIX` target property. diff --git a/Modules/UseSWIG.cmake b/Modules/UseSWIG.cmake index 83ec53b7bc..20b04870bf 100644 --- a/Modules/UseSWIG.cmake +++ b/Modules/UseSWIG.cmake @@ -35,6 +35,7 @@ The following command is defined for use with ``SWIG``: [TYPE ] LANGUAGE [NO_PROXY] + [DEBUG_POSTFIX ] [OUTPUT_DIR ] [OUTFILE_DIR ] SOURCES ... @@ -100,6 +101,16 @@ The following command is defined for use with ``SWIG``: Prevent the generation of the wrapper layer (swig ``-noproxy`` option). + ``DEBUG_POSTFIX`` + .. versionadded:: 4.2 + + Manage the :prop_tgt:`DEBUG_POSTFIX` target property. Currently, only + meaningful for the ``python`` language. + + If the :prop_gbl:`DEBUG_CONFIGURATIONS` global property is defined, define + the :prop_tgt:`_POSTFIX` target property for each debug + configuration. + ``OUTPUT_DIR`` .. versionadded:: 3.12 @@ -675,7 +686,7 @@ function(SWIG_ADD_SOURCE_TO_MODULE name outfiles infile) NOT ("-interface" IN_LIST swig_source_file_flags OR "-interface" IN_LIST SWIG_MODULE_${name}_EXTRA_FLAGS)) # 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" "$$") + list (APPEND SWIG_MODULE_${name}_EXTRA_FLAGS "-interface" "$$,$>,EXCLUDE,INCLUDE>>") endif() endif() list (APPEND swig_extra_flags ${SWIG_MODULE_${name}_EXTRA_FLAGS}) @@ -787,6 +798,7 @@ function(SWIG_ADD_LIBRARY name) set(options NO_PROXY) set(oneValueArgs LANGUAGE TYPE + DEBUG_POSTFIX OUTPUT_DIR OUTFILE_DIR) set(multiValueArgs SOURCES) @@ -810,6 +822,18 @@ function(SWIG_ADD_LIBRARY name) unset(_SAM_TYPE) endif() + if(_SAM_DEBUG_POSTFIX) + get_property(SWIG_DEBUG_CONFIGS GLOBAL PROPERTY DEBUG_CONFIGURATIONS) + if(NOT SWIG_DEBUG_CONFIGS) + set(SWIG_DEBUG_CONFIGS DEBUG) + endif() + string(TOUPPER "${SWIG_DEBUG_CONFIGS}" SWIG_DEBUG_CONFIGS) + set(SWIG_DEBUG_POSTFIX "${_SAM_DEBUG_POSTFIX}") + else() + unset(SWIG_DEBUG_CONFIGS) + unset(SWIG_DEBUG_POSTFIX) + endif() + cmake_policy(GET CMP0078 target_name_policy) if (target_name_policy STREQUAL "NEW") set (UseSWIG_TARGET_NAME_PREFERENCE STANDARD) @@ -1001,6 +1025,11 @@ function(SWIG_ADD_LIBRARY name) if(WIN32 AND NOT CYGWIN) set_target_properties(${target_name} PROPERTIES SUFFIX ".pyd") endif() + if(SWIG_DEBUG_POSTFIX) + foreach(config IN LISTS SWIG_DEBUG_CONFIGS) + set_target_properties(${target_name} PROPERTIES ${config}_POSTFIX "${SWIG_DEBUG_POSTFIX}") + endforeach() + endif() elseif (swig_lowercase_language STREQUAL "r") set_target_properties(${target_name} PROPERTIES PREFIX "") elseif (swig_lowercase_language STREQUAL "ruby") diff --git a/Tests/RunCMake/UseSWIG/RunCMakeTest.cmake b/Tests/RunCMake/UseSWIG/RunCMakeTest.cmake index 537f67d923..6af8c7384f 100644 --- a/Tests/RunCMake/UseSWIG/RunCMakeTest.cmake +++ b/Tests/RunCMake/UseSWIG/RunCMakeTest.cmake @@ -22,6 +22,13 @@ if (CMake_TEST_FindPython2 OR CMake_TEST_FindPython3) run_cmake(CMP0086-NEW) run_cmake_target(CMP0086-NEW build example) + block() + run_cmake(SetPOSTFIX) + set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/SetPOSTFIX-build) + set(RunCMake_TEST_NO_CLEAN 1) + run_cmake_command(SetPOSTFIX-build ${CMAKE_COMMAND} --build . --config $) + run_cmake_command(SetPOSTFIX-run ${CMAKE_CTEST_COMMAND} -C $ -V) + endblock() endif() run_cmake(CMP0122-WARN) diff --git a/Tests/RunCMake/UseSWIG/SetPOSTFIX.cmake b/Tests/RunCMake/UseSWIG/SetPOSTFIX.cmake new file mode 100644 index 0000000000..16955e1fb9 --- /dev/null +++ b/Tests/RunCMake/UseSWIG/SetPOSTFIX.cmake @@ -0,0 +1,28 @@ + +enable_language(C) + +include(CTest) + +cmake_policy(SET CMP0078 NEW) +cmake_policy(SET CMP0086 NEW) + +find_package(Python REQUIRED COMPONENTS Interpreter Development) + +find_package(SWIG) +include(UseSWIG) + +swig_add_library(example LANGUAGE python TYPE MODULE DEBUG_POSTFIX "${Python_DEBUG_POSTFIX}" + SOURCES example.i) +target_link_libraries(example PRIVATE Python::Module) + + +# Path separator +if (WIN32) + set (PS "$") +else() + set (PS ":") +endif() + +add_test (NAME SetPOSTFIX + COMMAND "${CMAKE_COMMAND}" -E env "PYTHONPATH=${CMAKE_CURRENT_BINARY_DIR}${PS}$" + "${Python_EXECUTABLE}" "${CMAKE_CURRENT_SOURCE_DIR}/runme.py") diff --git a/Tests/RunCMake/UseSWIG/runme.py b/Tests/RunCMake/UseSWIG/runme.py new file mode 100644 index 0000000000..fc8293f548 --- /dev/null +++ b/Tests/RunCMake/UseSWIG/runme.py @@ -0,0 +1,7 @@ +# file: runme.py + +from __future__ import print_function + +import example + +print ("Goodbye")