From cfcddf90ea6add9d4aaa99ee2decc5a9140bdf37 Mon Sep 17 00:00:00 2001
From: Ihor Dutchak <ihor.youw@gmail.com>
Date: Sat, 18 Jun 2022 15:58:31 +0300
Subject: [PATCH 1/3] Ensure Iconv is found when provided via CFLAGS/LDFLAGS

- by default find_file/find_library doesn't respect CFLAGS/LDFLAGS,
and FindIconv fails to find Iconv;
- by explicitly trying to link against `-liconv` - we're checking if library is available in such way;
- additionally: if Iconv is detected as BUILT_IN, no need to explicitly depend on `Iconv::Iconv`;
---
 libusb/CMakeLists.txt | 41 +++++++++++++++++++++++++++++++++--------
 src/CMakeLists.txt    |  3 ---
 2 files changed, 33 insertions(+), 11 deletions(-)

--- a/libusb/CMakeLists.txt
+++ b/libusb/CMakeLists.txt
@@ -22,11 +22,53 @@ target_link_libraries(hidapi_libusb PRIV
 if(HIDAPI_NO_ICONV)
     target_compile_definitions(hidapi_libusb PRIVATE NO_ICONV)
 else()
-    if(NOT ANDROID AND NOT CMAKE_VERSION VERSION_LESS 3.11)
-        find_package(Iconv REQUIRED)
+    if(NOT ANDROID)
         include(CheckCSourceCompiles)
-        target_link_libraries(hidapi_libusb PRIVATE Iconv::Iconv)
-        set(CMAKE_REQUIRED_LIBRARIES "Iconv::Iconv")
+
+        if(NOT CMAKE_VERSION VERSION_LESS 3.11)
+            message(STATUS "Check for Iconv")
+            find_package(Iconv)
+            if(Iconv_FOUND)
+                if(NOT Iconv_IS_BUILT_IN)
+                    target_link_libraries(hidapi_libusb PRIVATE Iconv::Iconv)
+                    set(CMAKE_REQUIRED_LIBRARIES "Iconv::Iconv")
+                    if(NOT BUILD_SHARED_LIBS)
+                        set(HIDAPI_NEED_EXPORT_ICONV TRUE PARENT_SCOPE)
+                    endif()
+                endif()
+            else()
+                message(STATUS "Iconv Explicitly check '-liconv'")
+                # Sometime the build environment is not setup
+                # in a way CMake can find Iconv on its own by default.
+                # But if we simply link against iconv (-liconv), the build may succeed
+                # due to other compiler/link flags.
+                set(CMAKE_REQUIRED_LIBRARIES "iconv")
+                check_c_source_compiles("
+                    #include <stddef.h>
+                    #include <iconv.h>
+                    int main() {
+                    char *a, *b;
+                    size_t i, j;
+                    iconv_t ic;
+                    ic = iconv_open(\"to\", \"from\");
+                    iconv(ic, &a, &i, &b, &j);
+                    iconv_close(ic);
+                    }
+                    "
+                    Iconv_EXPLICITLY_AT_ENV)
+                if(Iconv_EXPLICITLY_AT_ENV)
+                    message(STATUS "Iconv Explicitly check '-liconv' - Available")
+                    target_link_libraries(hidapi_libusb PRIVATE iconv)
+                else()
+                    message(FATAL_ERROR "Iconv is not found, make sure to provide it in the build environment")
+                endif()
+            endif()
+        else()
+            # otherwise there is 2 options:
+            # 1) iconv is provided by Standard C library and the build will be just fine
+            # 2) The _user_ has to provide additiona compilation options for this project/target
+        endif()
+
         # check for error: "conflicting types for 'iconv'"
         check_c_source_compiles("#include<iconv.h>
             extern size_t iconv (iconv_t cd, const char **inbuf, size_t *inbytesleft, char **outbuf, size_t *outbytesleft);
@@ -35,11 +77,9 @@ else()
         if(HIDAPI_ICONV_CONST)
             target_compile_definitions(hidapi_libusb PRIVATE "ICONV_CONST=const")
         endif()
+    else()
+        # On Android Iconv is disabled on the code level anyway, so no issue;
     endif()
-    # otherwise there is 3 options:
-    # 1) On Android Iconv is disabled on the code level anyway, so no issue;
-    # 2) iconv is provided by Standard C library and the build will be just fine;
-    # 4) The _user_ has to provide additiona compilation options for this project/target.
 endif()
 
 set_target_properties(hidapi_libusb
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -148,9 +148,6 @@ else()
             if(NOT TARGET usb-1.0)
                 set(HIDAPI_NEED_EXPORT_LIBUSB TRUE)
             endif()
-            if(NOT HIDAPI_NO_ICONV AND NOT ANDROID AND NOT CMAKE_VERSION VERSION_LESS 3.11)
-                set(HIDAPI_NEED_EXPORT_ICONV TRUE)
-            endif()
         endif()
     elseif(NOT TARGET hidapi_hidraw)
         message(FATAL_ERROR "Select at least one option to build: HIDAPI_WITH_LIBUSB or HIDAPI_WITH_HIDRAW")
