cmake_minimum_required(VERSION 3.6.3...3.25 FATAL_ERROR)

add_library(hidapi_hidraw
    ${HIDAPI_PUBLIC_HEADERS}
    hid.c
)
hidapi_configure_sanitizers(hidapi_hidraw)
if(HIDAPI_BUILD_AS_CXX)
    set_source_files_properties(hid.c PROPERTIES LANGUAGE CXX)
endif()
target_link_libraries(hidapi_hidraw PUBLIC hidapi_include)

find_package(Threads REQUIRED)

include(FindPkgConfig)
pkg_check_modules(libusb REQUIRED IMPORTED_TARGET libusb-1.0>=1.0.9)

target_link_libraries(hidapi_hidraw PRIVATE PkgConfig::libusb Threads::Threads)

set_target_properties(hidapi_hidraw
    PROPERTIES
        EXPORT_NAME "hidraw"
        OUTPUT_NAME "hidapi-hidraw"
        VERSION ${PROJECT_VERSION}
        SOVERSION ${PROJECT_VERSION_MAJOR}
        PUBLIC_HEADER "${HIDAPI_PUBLIC_HEADERS}"
)

# compatibility with find_package()
add_library(hidapi::hidraw ALIAS hidapi_hidraw)
# compatibility with raw library link
add_library(hidapi-hidraw ALIAS hidapi_hidraw)

if(HIDAPI_INSTALL_TARGETS)
    install(TARGETS hidapi_hidraw EXPORT hidapi
        LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"
        ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
        PUBLIC_HEADER DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/hidapi"
    )
endif()

hidapi_configure_pc("${PROJECT_ROOT}/pc/hidapi-backend-specific.pc.in"
    OUTPUT_NAME "hidapi-hidraw"
    LIBRARY_NAME "hidapi-hidraw"
    DESCRIPTION "C Library for USB/Bluetooth HID device access from Linux, Mac OS X, FreeBSD, and Windows. This is the hidraw implementation."
    REQUIRES_PRIVATE "libusb-1.0 >= 1.0.9"
)
