Skip to content

Commit

Permalink
libusb: cmake: Proper dependency on Iconv (signal11#405)
Browse files Browse the repository at this point in the history
- explicitly add Iconv as a dependent library;
- check if iconv requires pointer-to-const as input (introduce ICONV_CONST check);
- NetBSD CI (has external Iconv library implementation that uses all of the above);
  • Loading branch information
Youw authored May 2, 2022
1 parent 874b29c commit cd95af8
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 2 deletions.
18 changes: 18 additions & 0 deletions .builds/netbsd.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
image: netbsd/latest
packages:
- cmake
- pkgconf
- libusb1
- libiconv
sources:
- https://github.com/libusb/hidapi
tasks:
- setup: |
cd hidapi
mkdir -p build install
cmake -B build -S . -DCMAKE_INSTALL_PREFIX=install
- build: |
cd hidapi/build
make
make install
make clean
23 changes: 23 additions & 0 deletions libusb/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,29 @@ endif()
find_package(Threads REQUIRED)
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)
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<iconv.h>
extern size_t iconv (iconv_t cd, const char **inbuf, size_t *inbytesleft, char **outbuf, size_t *outbytesleft);
int main() {}"
HIDAPI_ICONV_CONST)
if(HIDAPI_ICONV_CONST)
target_compile_definitions(hidapi_libusb PRIVATE "ICONV_CONST=const")
endif()
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
PROPERTIES
EXPORT_NAME "libusb"
Expand Down
7 changes: 5 additions & 2 deletions libusb/hid.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@
#include <libusb.h>
#if !defined(__ANDROID__) && !defined(NO_ICONV)
#include <iconv.h>
#ifndef ICONV_CONST
#define ICONV_CONST
#endif
#endif

#include "hidapi_libusb.h"
Expand Down Expand Up @@ -405,7 +408,7 @@ static wchar_t *get_usb_string(libusb_device_handle *dev, uint8_t idx)
size_t inbytes;
size_t outbytes;
size_t res;
char *inptr;
ICONV_CONST char *inptr;
char *outptr;
#endif

Expand All @@ -421,7 +424,7 @@ static wchar_t *get_usb_string(libusb_device_handle *dev, uint8_t idx)
lang,
(unsigned char*)buf,
sizeof(buf));
if (len < 0)
if (len < 2) /* we always skip first 2 bytes */
return NULL;

#if defined(__ANDROID__) || defined(NO_ICONV)
Expand Down
7 changes: 7 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ set(EXPORT_COMPONENTS)
set(HIDAPI_NEED_EXPORT_THREADS FALSE)
set(HIDAPI_NEED_EXPORT_LIBUSB FALSE)
set(HIDAPI_NEED_EXPORT_LIBUDEV FALSE)
set(HIDAPI_NEED_EXPORT_ICONV FALSE)

if(WIN32)
add_subdirectory("${PROJECT_ROOT}/windows" windows)
Expand Down Expand Up @@ -128,6 +129,9 @@ else()
target_include_directories(hidapi_include INTERFACE
"$<BUILD_INTERFACE:${PROJECT_ROOT}/libusb>"
)
if(NOT DEFINED HIDAPI_NO_ICONV)
set(HIDAPI_NO_ICONV OFF)
endif()
add_subdirectory("${PROJECT_ROOT}/libusb" libusb)
list(APPEND EXPORT_COMPONENTS libusb)
if(NOT EXPORT_ALIAS)
Expand All @@ -138,6 +142,9 @@ 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
9 changes: 9 additions & 0 deletions src/cmake/hidapi-config.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ set(hidapi_FOUND FALSE)
set(HIDAPI_NEED_EXPORT_THREADS @HIDAPI_NEED_EXPORT_THREADS@)
set(HIDAPI_NEED_EXPORT_LIBUSB @HIDAPI_NEED_EXPORT_LIBUSB@)
set(HIDAPI_NEED_EXPORT_LIBUDEV @HIDAPI_NEED_EXPORT_LIBUDEV@)
set(HIDAPI_NEED_EXPORT_ICONV @HIDAPI_NEED_EXPORT_ICONV@)

if(HIDAPI_NEED_EXPORT_THREADS)
if(CMAKE_VERSION VERSION_LESS 3.4.3)
Expand All @@ -32,6 +33,14 @@ if(HIDAPI_NEED_EXPORT_LIBUSB OR HIDAPI_NEED_EXPORT_LIBUDEV)
endif()
endif()

if(HIDAPI_NEED_EXPORT_ICONV)
if(CMAKE_VERSION VERSION_LESS 3.11)
message(WARNING "HIDAPI requires CMake target Iconv::Iconv, make sure to provide it")
else()
find_package(Iconv REQUIRED)
endif()
endif()

include("${CMAKE_CURRENT_LIST_DIR}/libhidapi.cmake")

set(hidapi_FOUND TRUE)
Expand Down

0 comments on commit cd95af8

Please sign in to comment.