Skip to content

Commit

Permalink
Merge pull request #12294 from fwcd/linux-static-hidapi-udev
Browse files Browse the repository at this point in the history
FindLibudev: Link hidapi and libusb with libudev in static builds on Linux
  • Loading branch information
daschuer authored Nov 13, 2023
2 parents 18e17c2 + acac65e commit 3252675
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 0 deletions.
12 changes: 12 additions & 0 deletions cmake/modules/FindLibUSB.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ The following cache variables may also be set:
#]=======================================================================]

include(IsStaticLibrary)

find_package(PkgConfig QUIET)
if(PkgConfig_FOUND)
pkg_check_modules(PC_LibUSB QUIET libusb-1.0)
Expand Down Expand Up @@ -87,5 +89,15 @@ if(LibUSB_FOUND)
INTERFACE_COMPILE_OPTIONS "${PC_LibUSB_CFLAGS_OTHER}"
INTERFACE_INCLUDE_DIRECTORIES "${LibUSB_INCLUDE_DIR}"
)

is_static_library(LibUSB_IS_STATIC LibUSB::LibUSB)
if(LibUSB_IS_STATIC)
find_package(Libudev)
if(Libudev_FOUND)
set_property(TARGET LibUSB::LibUSB APPEND PROPERTY INTERFACE_LINK_LIBRARIES
Libudev::Libudev
)
endif()
endif()
endif()
endif()
61 changes: 61 additions & 0 deletions cmake/modules/FindLibudev.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# This file is part of Mixxx, Digital DJ'ing software.
# Copyright (C) 2001-2023 Mixxx Development Team
# Distributed under the GNU General Public Licence (GPL) version 2 or any later
# later version. See the LICENSE file for details.

#[=======================================================================[.rst:
FindLibudev
--------
Finds the libudev (userspace device manager) library.
Imported Targets
^^^^^^^^^^^^^^^^
This module provides the following imported targets, if found:
``Libudev::Libudev``
The libudev library
#]=======================================================================]

find_package(PkgConfig QUIET)
if(PkgConfig_FOUND)
pkg_check_modules(PC_Libudev QUIET libudev)
endif()

find_path(Libudev_INCLUDE_DIR
NAMES libudev.h
PATHS ${PC_Libudev_INCLUDE_DIRS}
DOC "The libudev include directory")
mark_as_advanced(Libudev_INCLUDE_DIR)

find_library(Libudev_LIBRARY
NAMES udev
PATH ${PC_Libudev_LIBRARY_DIRS}
DOC "The libudev library"
)
mark_as_advanced(Libudev_LIBRARY)

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(
Libudev
REQUIRED_VARS Libudev_LIBRARY Libudev_INCLUDE_DIR
VERSION_VAR Libudev_VERSION
)

if(Libudev_FOUND)
set(Libudev_LIBRARIES "${Libudev_LIBRARY}")
set(Libudev_INCLUDE_DIRS "${Libudev_INCLUDE_DIR}")
set(Libudev_DEFINITIONS ${PC_Libudev_CFLAGS_OTHER})

if(NOT TARGET Libudev::Libudev)
add_library(Libudev::Libudev UNKNOWN IMPORTED)
set_target_properties(Libudev::Libudev
PROPERTIES
IMPORTED_LOCATION "${Libudev_LIBRARY}"
INTERFACE_COMPILE_OPTIONS "${PC_Libudev_CFLAGS_OTHER}"
INTERFACE_INCLUDE_DIRECTORIES "${Libudev_INCLUDE_DIR}"
)
endif()
endif()
19 changes: 19 additions & 0 deletions cmake/modules/Findhidapi.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ The following cache variables may also be set:
#]=======================================================================]

include(IsStaticLibrary)

find_package(PkgConfig QUIET)
if(PkgConfig_FOUND)
pkg_search_module(PC_hidapi QUIET hidapi-libusb hidapi)
Expand Down Expand Up @@ -121,6 +123,23 @@ if(hidapi_FOUND)
INTERFACE_COMPILE_OPTIONS "${PC_hidapi_CFLAGS_OTHER}"
INTERFACE_INCLUDE_DIRECTORIES "${hidapi_INCLUDE_DIR}"
)

find_package(Libudev)
if(Libudev_FOUND)
is_static_library(hidapi_IS_STATIC hidapi::hidapi)
if(hidapi_IS_STATIC)
set_property(TARGET hidapi::hidapi APPEND PROPERTY INTERFACE_LINK_LIBRARIES
Libudev::Libudev
)
endif()

is_static_library(hidapi-hidraw_IS_STATIC hidapi::hidraw)
if(hidapi-hidraw_IS_STATIC)
set_property(TARGET hidapi::hidraw APPEND PROPERTY INTERFACE_LINK_LIBRARIES
Libudev::Libudev
)
endif()
endif()
endif()
endif()
endif()

0 comments on commit 3252675

Please sign in to comment.