Skip to content

Commit

Permalink
Ensure Iconv is found when provided via CFLAGS/LDFLAGS (signal11#430)
Browse files Browse the repository at this point in the history
- 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`;
  • Loading branch information
Youw authored Jun 21, 2022
1 parent 59e84ca commit 81dd62d
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 11 deletions.
56 changes: 48 additions & 8 deletions libusb/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,53 @@ 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)
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);
Expand All @@ -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
Expand Down
3 changes: 0 additions & 3 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 81dd62d

Please sign in to comment.