From 51b642679bc5dc675ab0e4e3fe7dc4c053ba60f4 Mon Sep 17 00:00:00 2001
From: Marc Chevrier <marc.chevrier@sap.com>
Date: Tue, 20 Mar 2018 10:56:28 +0100
Subject: UseSWIG: fix prefix library for Java on Windows

Fixes: #17836
---
 Help/release/dev/UseSWIG-fix-library-prefix.rst |  6 ++++++
 Modules/UseSWIG.cmake                           | 26 ++++++++++++++++---------
 2 files changed, 23 insertions(+), 9 deletions(-)
 create mode 100644 Help/release/dev/UseSWIG-fix-library-prefix.rst

diff --git a/Help/release/dev/UseSWIG-fix-library-prefix.rst b/Help/release/dev/UseSWIG-fix-library-prefix.rst
new file mode 100644
index 0000000..7ff0f49
--- /dev/null
+++ b/Help/release/dev/UseSWIG-fix-library-prefix.rst
@@ -0,0 +1,6 @@
+UseSWIG-fix-library-prefix
+--------------------------
+
+* The :module:`UseSWIG` module :command:`swig_add_library` command
+  (and legacy ``swig_add_module`` command) now set the prefix of
+  Java modules to ``""`` for MINGW, MSYS, and CYGWIN environments.
diff --git a/Modules/UseSWIG.cmake b/Modules/UseSWIG.cmake
index 6d35d1b..8e7ca41 100644
--- a/Modules/UseSWIG.cmake
+++ b/Modules/UseSWIG.cmake
@@ -507,15 +507,18 @@ function(SWIG_ADD_LIBRARY name)
   elseif (swig_lowercase_language STREQUAL "go")
     set_target_properties(${name} PROPERTIES PREFIX "")
   elseif (swig_lowercase_language STREQUAL "java")
+    # 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
     if (APPLE)
-        # 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
-        set_target_properties (${name} PROPERTIES SUFFIX ".jnilib")
-      endif ()
+      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()
   elseif (swig_lowercase_language STREQUAL "lua")
     if(_SAM_TYPE STREQUAL "MODULE")
       set_target_properties(${name} PROPERTIES PREFIX "")
@@ -560,11 +563,16 @@ function(SWIG_ADD_LIBRARY name)
   # target property SWIG_SUPPORT_FILES lists proxy support files
   if (NOT SWIG_MODULE_${name}_NOPROXY)
     string(TOUPPER "${_SAM_LANGUAGE}" swig_uppercase_language)
+    set(swig_all_support_files)
     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}$")
-      set_property (TARGET ${name} APPEND PROPERTY SWIG_SUPPORT_FILES ${swig_support_files})
+      list(APPEND swig_all_support_files ${swig_support_files})
     endforeach()
+    if (swig_all_support_files)
+      list(REMOVE_DUPLICATES swig_all_support_files)
+    endif()
+    set_property (TARGET ${name} APPEND PROPERTY SWIG_SUPPORT_FILES ${swig_all_support_files})
   endif()
 
   # to ensure legacy behavior, export some variables
-- 
cgit v0.12