Skip to content

Commit

Permalink
add CMake build system
Browse files Browse the repository at this point in the history
  • Loading branch information
Youw committed Jul 3, 2021
1 parent 6fcb0bb commit f2bb972
Show file tree
Hide file tree
Showing 12 changed files with 465 additions and 1 deletion.
61 changes: 61 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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()
2 changes: 1 addition & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
23 changes: 23 additions & 0 deletions hidtest/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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 changes: 42 additions & 0 deletions libusb/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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 changes: 38 additions & 0 deletions linux/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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 changes: 46 additions & 0 deletions mac/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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")
1 change: 1 addition & 0 deletions pc/hidapi-hidraw.pc.in
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions pc/hidapi-libusb.pc.in
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions pc/hidapi.pc.in
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading

0 comments on commit f2bb972

Please sign in to comment.