add CMake build system

This commit is contained in:
Ihor Dutchak 2020-02-09 16:02:10 +02:00
parent 6fcb0bb228
commit f2bb972e7f
12 changed files with 465 additions and 1 deletions

61
CMakeLists.txt Normal file
View File

@ -0,0 +1,61 @@
cmake_minimum_required(VERSION 3.1.3 FATAL_ERROR)
if(NOT CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
add_subdirectory(src)
# compatinility with find_package() vs add_subdirectory
set(hidapi_VERSION "${hidapi_VERSION}" PARENT_SCOPE)
return()
endif()
# All of the below in this file is meant for a standalone build.
# When building as a subdirectory of a larger project, most of the options may not make sense for it,
# so it is up to developer to configure those, e.g.:
#
# # a subfolder of a master project, e.g.: 3rdparty/hidapi/CMakeLists.txt
#
# set(HIDAPI_WITH_HIDRAW OFF)
# set(CMAKE_FRAMEWORK ON)
# # and keep everything else to their defaults
# add_subdirectory(hidapi)
#
set(DEFAULT_CMAKE_BUILD_TYPES "Debug" "Release" "MinSizeRel" "RelWithDebInfo")
if(NOT DEFINED CMAKE_BUILD_TYPE OR NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE "Release" CACHE STRING "${DEFAULT_CMAKE_BUILD_TYPES}" FORCE)
endif()
# This part is for convenience, when used one of the standard build types with cmake-gui
list(FIND DEFAULT_CMAKE_BUILD_TYPES "${CMAKE_BUILD_TYPE}" _build_type_index)
if(${_build_type_index} GREATER -1)
# set it optionally, so a custom CMAKE_BUILD_TYPE can be used as well, if needed
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS ${DEFAULT_CMAKE_BUILD_TYPES})
endif()
unset(_build_type_index)
#
project(hidapi LANGUAGES C)
if(APPLE)
if(NOT CMAKE_VERSION VERSION_LESS "3.15")
option(CMAKE_FRAMEWORK "Build macOS/iOS Framework version of the library" OFF)
endif()
elseif(NOT WIN32)
if(CMAKE_SYSTEM_NAME MATCHES "Linux")
option(HIDAPI_WITH_HIDRAW "Build HIDRAW-based implementation of HIDAPI" ON)
option(HIDAPI_WITH_LIBUSB "Build LIBUSB-based implementation of HIDAPI" ON)
endif()
endif()
option(BUILD_SHARED_LIBS "Build shared version of the libraries, otherwise build statically" ON)
set(HIDAPI_INSTALL_TARGETS ON)
set(HIDAPI_PRINT_VERSION ON)
add_subdirectory(src)
set(BUILD_HIDTEST_DEFAULT OFF)
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
set(BUILD_HIDTEST_DEFAULT ON)
endif()
option(HIDAPI_BUILD_HIDTEST "Build small console test application hidtest" ${BUILD_HIDTEST_DEFAULT})
if(HIDAPI_BUILD_HIDTEST)
add_subdirectory(hidtest)
endif()

View File

@ -189,7 +189,7 @@ mkdir testgui/TestGUI.app/Contents/MacOS/
if test "x$testgui_enabled" != "xno"; then
if test "x$os" = xdarwin; then
# On Mac OS, don't use pkg-config.
# On Mac OS, do not use pkg-config.
AC_CHECK_PROG([foxconfig], [fox-config], [fox-config], false)
if test "x$foxconfig" = "xfalse"; then
hidapi_prog_error fox-config "FOX Toolkit"

23
hidtest/CMakeLists.txt Normal file
View File

@ -0,0 +1,23 @@
project(hidtest C)
set(HIDAPI_HIDTEST_TARGETS)
if(NOT WIN32 AND NOT APPLE AND CMAKE_SYSTEM_NAME MATCHES "Linux")
if(TARGET hidapi::hidraw)
add_executable(hidtest_hidraw test.c)
target_link_libraries(hidtest_hidraw hidapi::hidraw)
list(APPEND HIDAPI_HIDTEST_TARGETS hidtest_hidraw)
endif()
if(TARGET hidapi::libusb)
add_executable(hidtest_libusb test.c)
target_link_libraries(hidtest_libusb hidapi::libusb)
list(APPEND HIDAPI_HIDTEST_TARGETS hidtest_libusb)
endif()
else()
add_executable(hidtest test.c)
target_link_libraries(hidtest hidapi::hidapi)
list(APPEND HIDAPI_HIDTEST_TARGETS hidtest)
endif()
install(TARGETS ${HIDAPI_HIDTEST_TARGETS}
RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
)

42
libusb/CMakeLists.txt Normal file
View File

@ -0,0 +1,42 @@
cmake_minimum_required(VERSION 3.6.3 FATAL_ERROR)
add_library(hidapi_libusb
${HIDAPI_PUBLIC_HEADERS}
hid.c
)
target_link_libraries(hidapi_libusb PUBLIC hidapi_include)
if(TARGET usb-1.0)
target_link_libraries(hidapi_libusb PRIVATE usb-1.0)
else()
include(FindPkgConfig)
pkg_check_modules(libusb REQUIRED IMPORTED_TARGET libusb-1.0>=1.0.9)
target_link_libraries(hidapi_libusb PRIVATE PkgConfig::libusb)
endif()
find_package(Threads REQUIRED)
target_link_libraries(hidapi_libusb PRIVATE Threads::Threads)
set_target_properties(hidapi_libusb
PROPERTIES
EXPORT_NAME "libusb"
OUTPUT_NAME "hidapi-libusb"
VERSION ${PROJECT_VERSION}
SOVERSION ${PROJECT_VERSION_MAJOR}
PUBLIC_HEADER "${HIDAPI_PUBLIC_HEADERS}"
)
# compatibility with find_package()
add_library(hidapi::libusb ALIAS hidapi_libusb)
# compatibility with raw library link
add_library(hidapi-libusb ALIAS hidapi_libusb)
if(HIDAPI_INSTALL_TARGETS)
install(TARGETS hidapi_libusb 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-libusb.pc.in")

38
linux/CMakeLists.txt Normal file
View File

@ -0,0 +1,38 @@
cmake_minimum_required(VERSION 3.6.3 FATAL_ERROR)
add_library(hidapi_hidraw
${HIDAPI_PUBLIC_HEADERS}
hid.c
)
target_link_libraries(hidapi_hidraw PUBLIC hidapi_include)
find_package(Threads REQUIRED)
include(FindPkgConfig)
pkg_check_modules(libudev REQUIRED IMPORTED_TARGET libudev)
target_link_libraries(hidapi_hidraw PRIVATE PkgConfig::libudev 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-hidraw.pc.in")

46
mac/CMakeLists.txt Normal file
View File

@ -0,0 +1,46 @@
cmake_minimum_required(VERSION 3.4.3 FATAL_ERROR)
add_library(hidapi_darwin
${HIDAPI_PUBLIC_HEADERS}
hid.c
)
find_package(Threads REQUIRED)
target_link_libraries(hidapi_darwin
PUBLIC hidapi_include
PRIVATE Threads::Threads
PRIVATE "-framework IOKit" "-framework CoreFoundation" "-framework AppKit"
)
set_target_properties(hidapi_darwin
PROPERTIES
EXPORT_NAME "darwin"
OUTPUT_NAME "hidapi"
VERSION ${PROJECT_VERSION}
SOVERSION ${PROJECT_VERSION_MAJOR}
MACHO_COMPATIBILITY_VERSION ${PROJECT_VERSION_MAJOR}
FRAMEWORK_VERSION ${PROJECT_VERSION_MAJOR}
PUBLIC_HEADER "${HIDAPI_PUBLIC_HEADERS}"
)
# compatibility with find_package()
add_library(hidapi::darwin ALIAS hidapi_darwin)
# compatibility with raw library link
add_library(hidapi ALIAS hidapi_darwin)
set(PUBLIC_HEADER_DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}")
if(NOT CMAKE_FRAMEWORK)
set(PUBLIC_HEADER_DESTINATION "${PUBLIC_HEADER_DESTINATION}/hidapi")
endif()
if(HIDAPI_INSTALL_TARGETS)
install(TARGETS hidapi_darwin EXPORT hidapi
LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"
ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
FRAMEWORK DESTINATION "${CMAKE_INSTALL_LIBDIR}"
PUBLIC_HEADER DESTINATION "${PUBLIC_HEADER_DESTINATION}"
)
endif()
hidapi_configure_pc("${PROJECT_ROOT}/pc/hidapi.pc.in")

View File

@ -5,6 +5,7 @@ includedir=@includedir@
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.
URL: https://github.com/libusb/hidapi
Version: @VERSION@
Libs: -L${libdir} -lhidapi-hidraw
Cflags: -I${includedir}/hidapi

View File

@ -5,6 +5,7 @@ includedir=@includedir@
Name: hidapi-libusb
Description: C Library for USB HID device access from Linux, Mac OS X, FreeBSD, and Windows. This is the libusb implementation.
URL: https://github.com/libusb/hidapi
Version: @VERSION@
Libs: -L${libdir} -lhidapi-libusb
Cflags: -I${includedir}/hidapi

View File

@ -5,6 +5,7 @@ includedir=@includedir@
Name: hidapi
Description: C Library for USB/Bluetooth HID device access from Linux, Mac OS X, FreeBSD, and Windows.
URL: https://github.com/libusb/hidapi
Version: @VERSION@
Libs: -L${libdir} -lhidapi
Cflags: -I${includedir}/hidapi

167
src/CMakeLists.txt Normal file
View File

@ -0,0 +1,167 @@
get_filename_component(PROJECT_ROOT "${CMAKE_CURRENT_LIST_DIR}/.." ABSOLUTE)
# Read version from file
file(READ "${PROJECT_ROOT}/VERSION" RAW_VERSION_STR)
string(REGEX MATCH "^([0-9]+\\.[0-9]+\\.[0-9]+)(.*)" VERSION_STR "${RAW_VERSION_STR}")
if(NOT VERSION_STR)
message(FATAL_ERROR "Broken VERSION file, couldn't parse '${PROJECT_ROOT}/VERSION' with content: '${RAW_VERSION_STR}'")
endif()
set(VERSION "${CMAKE_MATCH_1}")
string(STRIP "${CMAKE_MATCH_2}" VERSION_SUFFIX)
# compatinility with find_package() vs add_subdirectory
set(hidapi_VERSION "${VERSION}" PARENT_SCOPE)
#
if(DEFINED HIDAPI_PRINT_VERSION AND HIDAPI_PRINT_VERSION)
set(HIDAPI_PRINT_VERSION "hidapi: v${VERSION}")
if(VERSION_SUFFIX)
set(HIDAPI_PRINT_VERSION "${HIDAPI_PRINT_VERSION} (${VERSION_SUFFIX})")
endif()
message(STATUS "${HIDAPI_PRINT_VERSION}")
endif()
project(hidapi VERSION "${VERSION}" LANGUAGES C)
# Defaults and required options
if(NOT DEFINED BUILD_SHARED_LIBS)
set(BUILD_SHARED_LIBS ON)
endif()
if(NOT DEFINED HIDAPI_INSTALL_TARGETS)
set(HIDAPI_INSTALL_TARGETS OFF)
endif()
if(NOT DEFINED CMAKE_POSITION_INDEPENDENT_CODE)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
endif()
get_directory_property(IS_EXCLUDE_FROM_ALL EXCLUDE_FROM_ALL)
if(IS_EXCLUDE_FROM_ALL)
if(HIDAPI_INSTALL_TARGETS)
message(WARNING "Installing EXCLUDE_FROM_ALL targets in an undefined behavior in CMake.\nDon't add 'hidapi' sundirectory with 'EXCLUDE_FROM_ALL' property, or don't set 'HIDAPI_INSTALL_TARGETS' to TRUE.")
endif()
endif()
# Helper(s)
function(hidapi_configure_pc PC_IN_FILE)
file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/pc")
set(VERSION "${VERSION}${VERSION_SUFFIX}")
set(prefix "${CMAKE_INSTALL_PREFIX}")
set(exec_prefix "\${prefix}")
if(IS_ABSOLUTE "${CMAKE_INSTALL_LIBDIR}")
set(libdir "${CMAKE_INSTALL_LIBDIR}")
else()
set(libdir "\${exec_prefix}/${CMAKE_INSTALL_LIBDIR}")
endif()
if(IS_ABSOLUTE "${CMAKE_INSTALL_INCLUDEDIR}")
set(includedir "${CMAKE_INSTALL_INCLUDEDIR}")
else()
set(includedir "\${prefix}/${CMAKE_INSTALL_INCLUDEDIR}")
endif()
get_filename_component(PC_IN_FILENAME "${PC_IN_FILE}" NAME_WE)
set(PC_FILE "${CMAKE_CURRENT_BINARY_DIR}/pc/${PC_IN_FILENAME}.pc")
configure_file("${PC_IN_FILE}" "${PC_FILE}" @ONLY)
install(FILES "${PC_FILE}" DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig/")
endfunction()
# The library
add_library(hidapi_include INTERFACE)
target_include_directories(hidapi_include INTERFACE
"$<BUILD_INTERFACE:${PROJECT_ROOT}/hidapi>"
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/hidapi>"
)
set_target_properties(hidapi_include PROPERTIES EXPORT_NAME "include")
set(HIDAPI_PUBLIC_HEADERS "${PROJECT_ROOT}/hidapi/hidapi.h")
add_library(hidapi::include ALIAS hidapi_include)
if(HIDAPI_INSTALL_TARGETS)
include(GNUInstallDirs)
install(TARGETS hidapi_include EXPORT hidapi)
endif()
set(EXPORT_ALIAS)
set(EXPORT_COMPONENTS)
set(HIDAPI_NEED_EXPORT_THREADS FALSE)
set(HIDAPI_NEED_EXPORT_LIBUSB FALSE)
set(HIDAPI_NEED_EXPORT_LIBUDEV FALSE)
if(WIN32)
add_subdirectory("${PROJECT_ROOT}/windows" windows)
set(EXPORT_ALIAS winapi)
list(APPEND EXPORT_COMPONENTS winapi)
elseif(APPLE)
add_subdirectory("${PROJECT_ROOT}/mac" mac)
set(EXPORT_ALIAS darwin)
list(APPEND EXPORT_COMPONENTS darwin)
if(NOT BUILD_SHARED_LIBS)
set(HIDAPI_NEED_EXPORT_THREADS TRUE)
endif()
else()
if(NOT DEFINED HIDAPI_WITH_LIBUSB)
set(HIDAPI_WITH_LIBUSB ON)
endif()
if(CMAKE_SYSTEM_NAME MATCHES "Linux")
if(NOT DEFINED HIDAPI_WITH_HIDRAW)
set(HIDAPI_WITH_HIDRAW ON)
endif()
if(HIDAPI_WITH_HIDRAW)
add_subdirectory("${PROJECT_ROOT}/linux" linux)
list(APPEND EXPORT_COMPONENTS hidraw)
set(EXPORT_ALIAS hidraw)
if(NOT BUILD_SHARED_LIBS)
set(HIDAPI_NEED_EXPORT_THREADS TRUE)
set(HIDAPI_NEED_EXPORT_LIBUDEV TRUE)
endif()
endif()
else()
set(HIDAPI_WITH_LIBUSB ON)
endif()
if(HIDAPI_WITH_LIBUSB)
add_subdirectory("${PROJECT_ROOT}/libusb" libusb)
list(APPEND EXPORT_COMPONENTS libusb)
if(NOT EXPORT_ALIAS)
set(EXPORT_ALIAS libusb)
endif()
if(NOT BUILD_SHARED_LIBS)
set(HIDAPI_NEED_EXPORT_THREADS TRUE)
if(NOT TARGET usb-1.0)
set(HIDAPI_NEED_EXPORT_LIBUSB 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")
endif()
endif()
add_library(hidapi::hidapi ALIAS hidapi_${EXPORT_ALIAS})
if(HIDAPI_INSTALL_TARGETS)
include(CMakePackageConfigHelpers)
set(EXPORT_DENERATED_LOCATION "${CMAKE_BINARY_DIR}/export_generated")
set(EXPORT_DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/hidapi")
write_basic_package_version_file("${EXPORT_DENERATED_LOCATION}/hidapi-config-version.cmake"
COMPATIBILITY SameMajorVersion
)
configure_package_config_file("cmake/hidapi-config.cmake.in" "${EXPORT_DENERATED_LOCATION}/hidapi-config.cmake"
INSTALL_DESTINATION "${EXPORT_DESTINATION}"
NO_SET_AND_CHECK_MACRO
)
install(EXPORT hidapi
DESTINATION "${EXPORT_DESTINATION}"
NAMESPACE hidapi::
)
install(FILES
"${EXPORT_DENERATED_LOCATION}/hidapi-config-version.cmake"
"${EXPORT_DENERATED_LOCATION}/hidapi-config.cmake"
DESTINATION "${EXPORT_DESTINATION}"
)
endif()

View File

@ -0,0 +1,52 @@
@PACKAGE_INIT@
set(hidapi_VERSION_MAJOR "@hidapi_VERSION_MAJOR@")
set(hidapi_VERSION_MINOR "@hidapi_VERSION_MINOR@")
set(hidapi_VERSION_PATCH "@hidapi_VERSION_PATCH@")
set(hidapi_VERSION "@hidapi_VERSION@")
set(hidapi_VERSION_STR "@hidapi_VERSION@@VERSION_SUFFIX@")
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@)
if(HIDAPI_NEED_EXPORT_THREADS)
if(CMAKE_VERSION VERSION_LESS 3.4.3)
message(FATAL_ERROR "This file relies on consumers using CMake 3.4.3 or greater.")
endif()
find_package(Threads REQUIRED)
endif()
if(HIDAPI_NEED_EXPORT_LIBUSB OR HIDAPI_NEED_EXPORT_LIBUDEV)
if(CMAKE_VERSION VERSION_LESS 3.6.3)
message(FATAL_ERROR "This file relies on consumers using CMake 3.6.3 or greater.")
endif()
include(FindPkgConfig)
if(HIDAPI_NEED_EXPORT_LIBUSB)
pkg_check_modules(libusb REQUIRED IMPORTED_TARGET libusb-1.0>=1.0.9)
endif()
if(HIDAPI_NEED_EXPORT_LIBUDEV)
pkg_check_modules(libudev REQUIRED IMPORTED_TARGET libudev)
endif()
endif()
include("${CMAKE_CURRENT_LIST_DIR}/hidapi.cmake")
set(hidapi_FOUND TRUE)
foreach(_component @EXPORT_COMPONENTS@)
if(TARGET hidapi::${_component})
set(hidapi_${_component}_FOUND TRUE)
endif()
endforeach()
check_required_components(hidapi)
if(NOT TARGET hidapi::hidapi)
add_library(hidapi::hidapi INTERFACE IMPORTED)
set_target_properties(hidapi::hidapi PROPERTIES
INTERFACE_LINK_LIBRARIES hidapi::@EXPORT_ALIAS@
)
endif()

32
windows/CMakeLists.txt Normal file
View File

@ -0,0 +1,32 @@
add_library(hidapi_winapi
${HIDAPI_PUBLIC_HEADERS}
hid.c
)
target_link_libraries(hidapi_winapi
PUBLIC hidapi_include
PRIVATE setupapi
)
set_target_properties(hidapi_winapi
PROPERTIES
EXPORT_NAME "winapi"
OUTPUT_NAME "hidapi"
VERSION ${PROJECT_VERSION}
PUBLIC_HEADER "${HIDAPI_PUBLIC_HEADERS}"
)
# compatibility with find_package()
add_library(hidapi::winapi ALIAS hidapi_winapi)
# compatibility with raw library link
add_library(hidapi ALIAS hidapi_winapi)
if(HIDAPI_INSTALL_TARGETS)
install(TARGETS hidapi_winapi EXPORT hidapi
RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
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.pc.in")