From cfcddf90ea6add9d4aaa99ee2decc5a9140bdf37 Mon Sep 17 00:00:00 2001 From: Ihor Dutchak Date: Sat, 18 Jun 2022 15:58:31 +0300 Subject: [PATCH 1/4] 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(-) diff --git a/libusb/CMakeLists.txt b/libusb/CMakeLists.txt index e5d7d5180..afa32029b 100644 --- a/libusb/CMakeLists.txt +++ b/libusb/CMakeLists.txt @@ -22,11 +22,38 @@ target_link_libraries(hidapi_libusb PRIVATE Threads::Threads) 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) + if(NOT CMAKE_VERSION VERSION_LESS 3.11) + find_package(Iconv) + if(NOT Iconv_FOUND) + # 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 succeedes + # due to other compiler/link flags. + # In such case CMake will find Iconv and will detect it as Iconv_IS_BUILT_IN. + set(CMAKE_REQUIRED_LIBRARIES "iconv") + find_package(Iconv) + if(Iconv_FOUND) + target_link_libraries(hidapi_libusb PRIVATE iconv) + endif() + endif() + if(NOT Iconv_FOUND) + message(FATAL_ERROR "Iconv is not found, make sure your build environment is right") + endif() + 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() + # 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() + include(CheckCSourceCompiles) - target_link_libraries(hidapi_libusb PRIVATE Iconv::Iconv) - set(CMAKE_REQUIRED_LIBRARIES "Iconv::Iconv") # check for error: "conflicting types for 'iconv'" check_c_source_compiles("#include extern size_t iconv (iconv_t cd, const char **inbuf, size_t *inbytesleft, char **outbuf, size_t *outbytesleft); @@ -35,11 +62,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 diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 504b20548..f096d498f 100644 --- 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") From ca44a07e859fead3d399254bcbcdc2c521c8e949 Mon Sep 17 00:00:00 2001 From: Ihor Dutchak Date: Sun, 19 Jun 2022 03:51:29 +0300 Subject: [PATCH 2/4] trigger CI --- libusb/CMakeLists.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libusb/CMakeLists.txt b/libusb/CMakeLists.txt index afa32029b..4d81f13eb 100644 --- a/libusb/CMakeLists.txt +++ b/libusb/CMakeLists.txt @@ -49,8 +49,8 @@ else() 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. + # 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() include(CheckCSourceCompiles) From 72530a3ed1701f6e0c4afe836697c448c9e61486 Mon Sep 17 00:00:00 2001 From: Ihor Dutchak Date: Mon, 20 Jun 2022 15:28:46 +0300 Subject: [PATCH 3/4] alternative check --- libusb/CMakeLists.txt | 47 ++++++++++++++++++++++++++++--------------- 1 file changed, 31 insertions(+), 16 deletions(-) diff --git a/libusb/CMakeLists.txt b/libusb/CMakeLists.txt index 4d81f13eb..9a178d584 100644 --- a/libusb/CMakeLists.txt +++ b/libusb/CMakeLists.txt @@ -23,28 +23,44 @@ if(HIDAPI_NO_ICONV) target_compile_definitions(hidapi_libusb PRIVATE NO_ICONV) else() if(NOT ANDROID) + include(CheckCSourceCompiles) + if(NOT CMAKE_VERSION VERSION_LESS 3.11) + message(STATUS "Check for Iconv") find_package(Iconv) - if(NOT Iconv_FOUND) + 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 succeedes + # But if we simply link against iconv (-liconv), the build may succeed # due to other compiler/link flags. - # In such case CMake will find Iconv and will detect it as Iconv_IS_BUILT_IN. set(CMAKE_REQUIRED_LIBRARIES "iconv") - find_package(Iconv) - if(Iconv_FOUND) + check_c_source_compiles(" + #include + #include + 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) - endif() - endif() - if(NOT Iconv_FOUND) - message(FATAL_ERROR "Iconv is not found, make sure your build environment is right") - endif() - 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) + else() + message(FATAL_ERROR "Iconv is not found, make sure to provide it in the build environment") endif() endif() else() @@ -53,7 +69,6 @@ else() # 2) The _user_ has to provide additiona compilation options for this project/target endif() - include(CheckCSourceCompiles) # check for error: "conflicting types for 'iconv'" check_c_source_compiles("#include extern size_t iconv (iconv_t cd, const char **inbuf, size_t *inbytesleft, char **outbuf, size_t *outbytesleft); From 7e1cef4a8a630b4ee5212313a79c9bde83e1b3fe Mon Sep 17 00:00:00 2001 From: Ihor Dutchak Date: Mon, 20 Jun 2022 23:02:25 +0300 Subject: [PATCH 4/4] Update libusb/CMakeLists.txt --- libusb/CMakeLists.txt | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/libusb/CMakeLists.txt b/libusb/CMakeLists.txt index 9a178d584..6cd48c4f7 100644 --- a/libusb/CMakeLists.txt +++ b/libusb/CMakeLists.txt @@ -47,12 +47,12 @@ else() #include #include 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); + 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)