Use Official FindIconv (3.11+) to get check the existence of iconv, and add it in dependencies if used

Gary Gai <[email protected]> Mon, 27 May 2024 16:38:18 +0000
Newsgroups gmane.text.xml.xerces-c.devel
Message-ID <OSZP286MB1703FC1E3E1647A761D233E7A6F02@OSZP286MB1703.JPNP286.PROD.OUTLOOK.COM>
Since CMake 3.11,`FindIconv` is available for searching libcionv.
https://cmake.org/cmake/help/latest/module/FindIconv.html
(Note that the cmake version of xerces-c is 3.12 currently)

There are 2 benefits to replace `HAVE_ICONV_*` by `find_package(Iconv)` + `Iconv_IS_BUILT_IN`/`Iconv_FOUND`.


  1.  It’s simpler.
  2.  Users are more convenient to link their custom libiconv provided by custom module/config script.
  3.  `target_link_libraries(xerces-c PRIVATE Iconv::Iconv)` is help for cmake to analyse dependencies.

My patch:
https://github.com/zjyhjqs/xerces-c/commit/fc8e21bab3b98704d639d0ae2e7df8e347c6e4d5


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
FindIconv.diff (application/octet-stream, 3.8 KB)
diff --git a/CMakeLists.txt b/CMakeLists.txt
index bee6cd581..019d8c2c6 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -100,6 +100,8 @@ set(EXTRA_DIST
   tools
 )

+find_package(Iconv)
+
 include(GNUInstallDirs)
 include(XercesWarnings)
 include(XercesIncludes)
diff --git a/cmake/XercesMsgLoaderSelection.cmake b/cmake/XercesMsgLoaderSelection.cmake
index f0d9b0d0d..10efd77d8 100644
--- a/cmake/XercesMsgLoaderSelection.cmake
+++ b/cmake/XercesMsgLoaderSelection.cmake
@@ -32,7 +32,8 @@ check_include_file_cxx(nl_types.h HAVE_NLTYPES_H)
 check_function_exists(catopen HAVE_CATOPEN)
 check_function_exists(catclose HAVE_CATCLOSE)
 check_function_exists(catgets HAVE_CATGETS)
-if(HAVE_NLTYPES_H AND HAVE_CATOPEN AND HAVE_CATCLOSE AND HAVE_CATGETS)
+if(HAVE_NLTYPES_H AND HAVE_CATOPEN AND HAVE_CATCLOSE AND HAVE_CATGETS AND
+   Iconv_FOUND)
   set(iconv_available 1)
 endif()
 if(iconv_available)
diff --git a/cmake/XercesTranscoderSelection.cmake b/cmake/XercesTranscoderSelection.cmake
index 5dd7c6257..16a32ff60 100644
--- a/cmake/XercesTranscoderSelection.cmake
+++ b/cmake/XercesTranscoderSelection.cmake
@@ -41,7 +41,6 @@ endif()

 # GNU iconv

-check_include_file_cxx(iconv.h HAVE_ICONV_H)
 check_include_file_cxx(wchar.h HAVE_WCHAR_H)
 check_include_file_cxx(ctype.h HAVE_CTYPE_H)
 check_include_file_cxx(locale.h HAVE_LOCALE_H)
@@ -49,15 +48,11 @@ check_include_file_cxx(errno.h HAVE_ERRNO_H)
 check_include_file_cxx(endian.h HAVE_ENDIAN_H)
 check_include_file_cxx(machine/endian.h HAVE_MACHINE_ENDIAN_H)
 check_include_file_cxx(arpa/nameser_compat.h HAVE_ARPA_NAMESER_COMPAT_H)
-check_function_exists(iconv_open HAVE_ICONV_OPEN)
-check_function_exists(iconv_close HAVE_ICONV_CLOSE)
-check_function_exists(iconv HAVE_ICONV)

 set(gnuiconv_available 0)
-if(HAVE_ICONV_H AND HAVE_WCHAR_H AND
-   HAVE_CTYPE_H AND HAVE_LOCALE_H AND HAVE_ERRNO_H)
+if(HAVE_WCHAR_H AND HAVE_CTYPE_H AND HAVE_LOCALE_H AND HAVE_ERRNO_H)
   if (HAVE_ENDIAN_H OR HAVE_MACHINE_ENDIAN_H OR HAVE_ARPA_NAMESER_COMPAT_H)
-    if(HAVE_ICONV_OPEN AND HAVE_ICONV_CLOSE AND HAVE_ICONV)
+    if(Iconv_IS_BUILT_IN)
       set(gnuiconv_available 1)
       list(APPEND transcoders gnuiconv)
     endif()
@@ -79,7 +74,8 @@ check_function_exists(wcstombs HAVE_WCSTOMBS)
 check_function_exists(mbstowcs HAVE_MBSTOWCS)

 set(iconv_available 0)
-if(HAVE_WCHAR_H AND HAVE_MBLEN AND HAVE_WCSTOMBS AND HAVE_MBSTOWCS)
+if(HAVE_WCHAR_H AND HAVE_MBLEN AND HAVE_WCSTOMBS AND HAVE_MBSTOWCS AND
+   Iconv_FOUND)
   set(iconv_available 1)
   list(APPEND transcoders iconv)
 endif()
diff --git a/config.h.cmake.in b/config.h.cmake.in
index b86c12f1f..cf5e09ff4 100644
--- a/config.h.cmake.in
+++ b/config.h.cmake.in
@@ -67,18 +67,6 @@
 /* Define to 1 if you have the `gmtime_r' function. */
 #cmakedefine HAVE_GMTIME_R 1

-/* Define to 1 if you have the `iconv' function. */
-#cmakedefine HAVE_ICONV 1
-
-/* Define to 1 if you have the `iconv_close' function. */
-#cmakedefine HAVE_ICONV_CLOSE 1
-
-/* Define to 1 if you have the <iconv.h> header file. */
-#cmakedefine HAVE_ICONV_H 1
-
-/* Define to 1 if you have the `iconv_open' function. */
-#cmakedefine HAVE_ICONV_OPEN 1
-
 /* Define to 1 if you have the <langinfo.h> header file. */
 #cmakedefine HAVE_LANGINFO_H 1

diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index aef3a76f9..35410409d 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -1116,11 +1116,13 @@ endif()
 if(XERCES_USE_TRANSCODER_GNUICONV)
   list(APPEND libxerces_c_SOURCES ${gnuiconv_sources})
   list(APPEND libxerces_c_HEADERS ${gnuiconv_headers})
+  list(APPEND libxerces_c_DEPS Iconv::Iconv) # convinient for CMake to analyse dependencies
 endif()

 if(XERCES_USE_TRANSCODER_ICONV)
   list(APPEND libxerces_c_SOURCES ${iconv_sources})
   list(APPEND libxerces_c_HEADERS ${iconv_headers})
+  list(APPEND libxerces_c_DEPS Iconv::Iconv)
 endif()

 if(XERCES_USE_TRANSCODER_MACOSUNICODECONVERTER)