Skip to content

Commit 81dd62d

Browse files
authored
Ensure Iconv is found when provided via CFLAGS/LDFLAGS (signal11#430)
- 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`;
1 parent 59e84ca commit 81dd62d

File tree

2 files changed

+48
-11
lines changed

2 files changed

+48
-11
lines changed

libusb/CMakeLists.txt

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,53 @@ target_link_libraries(hidapi_libusb PRIVATE Threads::Threads)
2222
if(HIDAPI_NO_ICONV)
2323
target_compile_definitions(hidapi_libusb PRIVATE NO_ICONV)
2424
else()
25-
if(NOT ANDROID AND NOT CMAKE_VERSION VERSION_LESS 3.11)
26-
find_package(Iconv REQUIRED)
25+
if(NOT ANDROID)
2726
include(CheckCSourceCompiles)
28-
target_link_libraries(hidapi_libusb PRIVATE Iconv::Iconv)
29-
set(CMAKE_REQUIRED_LIBRARIES "Iconv::Iconv")
27+
28+
if(NOT CMAKE_VERSION VERSION_LESS 3.11)
29+
message(STATUS "Check for Iconv")
30+
find_package(Iconv)
31+
if(Iconv_FOUND)
32+
if(NOT Iconv_IS_BUILT_IN)
33+
target_link_libraries(hidapi_libusb PRIVATE Iconv::Iconv)
34+
set(CMAKE_REQUIRED_LIBRARIES "Iconv::Iconv")
35+
if(NOT BUILD_SHARED_LIBS)
36+
set(HIDAPI_NEED_EXPORT_ICONV TRUE PARENT_SCOPE)
37+
endif()
38+
endif()
39+
else()
40+
message(STATUS "Iconv Explicitly check '-liconv'")
41+
# Sometime the build environment is not setup
42+
# in a way CMake can find Iconv on its own by default.
43+
# But if we simply link against iconv (-liconv), the build may succeed
44+
# due to other compiler/link flags.
45+
set(CMAKE_REQUIRED_LIBRARIES "iconv")
46+
check_c_source_compiles("
47+
#include <stddef.h>
48+
#include <iconv.h>
49+
int main() {
50+
char *a, *b;
51+
size_t i, j;
52+
iconv_t ic;
53+
ic = iconv_open(\"to\", \"from\");
54+
iconv(ic, &a, &i, &b, &j);
55+
iconv_close(ic);
56+
}
57+
"
58+
Iconv_EXPLICITLY_AT_ENV)
59+
if(Iconv_EXPLICITLY_AT_ENV)
60+
message(STATUS "Iconv Explicitly check '-liconv' - Available")
61+
target_link_libraries(hidapi_libusb PRIVATE iconv)
62+
else()
63+
message(FATAL_ERROR "Iconv is not found, make sure to provide it in the build environment")
64+
endif()
65+
endif()
66+
else()
67+
# otherwise there is 2 options:
68+
# 1) iconv is provided by Standard C library and the build will be just fine
69+
# 2) The _user_ has to provide additiona compilation options for this project/target
70+
endif()
71+
3072
# check for error: "conflicting types for 'iconv'"
3173
check_c_source_compiles("#include<iconv.h>
3274
extern size_t iconv (iconv_t cd, const char **inbuf, size_t *inbytesleft, char **outbuf, size_t *outbytesleft);
@@ -35,11 +77,9 @@ else()
3577
if(HIDAPI_ICONV_CONST)
3678
target_compile_definitions(hidapi_libusb PRIVATE "ICONV_CONST=const")
3779
endif()
80+
else()
81+
# On Android Iconv is disabled on the code level anyway, so no issue;
3882
endif()
39-
# otherwise there is 3 options:
40-
# 1) On Android Iconv is disabled on the code level anyway, so no issue;
41-
# 2) iconv is provided by Standard C library and the build will be just fine;
42-
# 4) The _user_ has to provide additiona compilation options for this project/target.
4383
endif()
4484

4585
set_target_properties(hidapi_libusb

src/CMakeLists.txt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,6 @@ else()
148148
if(NOT TARGET usb-1.0)
149149
set(HIDAPI_NEED_EXPORT_LIBUSB TRUE)
150150
endif()
151-
if(NOT HIDAPI_NO_ICONV AND NOT ANDROID AND NOT CMAKE_VERSION VERSION_LESS 3.11)
152-
set(HIDAPI_NEED_EXPORT_ICONV TRUE)
153-
endif()
154151
endif()
155152
elseif(NOT TARGET hidapi_hidraw)
156153
message(FATAL_ERROR "Select at least one option to build: HIDAPI_WITH_LIBUSB or HIDAPI_WITH_HIDRAW")

0 commit comments

Comments
 (0)