Skip to content
This repository was archived by the owner on Aug 22, 2024. It is now read-only.

Commit 67e7ba1

Browse files
authored
Build and statically link in LibUSB (#269)
* Add libusb as submodule * Builds cmake from source * Fix zero-length-array errors from libusb
1 parent 619f203 commit 67e7ba1

File tree

9 files changed

+212
-136
lines changed

9 files changed

+212
-136
lines changed

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,6 @@
3232
[submodule "extern/libjpeg-turbo/src"]
3333
path = extern/libjpeg-turbo/src
3434
url = https://github.com/libjpeg-turbo/libjpeg-turbo.git
35+
[submodule "extern/libusb/src"]
36+
path = extern/libusb/src
37+
url = https://github.com/libusb/libusb

cmake/FindLibUSB.cmake

Lines changed: 0 additions & 119 deletions
This file was deleted.

extern/libusb/CMakeLists.txt

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,45 @@
1-
find_package(LibUSB REQUIRED)
1+
add_library(LibUSB
2+
STATIC
3+
"${CMAKE_CURRENT_SOURCE_DIR}/src/libusb/core.c"
4+
"${CMAKE_CURRENT_SOURCE_DIR}/src/libusb/descriptor.c"
5+
"${CMAKE_CURRENT_SOURCE_DIR}/src/libusb/hotplug.c"
6+
"${CMAKE_CURRENT_SOURCE_DIR}/src/libusb/io.c"
7+
"${CMAKE_CURRENT_SOURCE_DIR}/src/libusb/strerror.c"
8+
"${CMAKE_CURRENT_SOURCE_DIR}/src/libusb/sync.c")
9+
10+
target_include_directories(LibUSB PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/src/libusb)
11+
target_include_directories(LibUSB PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src/libusb/os)
12+
13+
add_library(LibUSB::LibUSB ALIAS LibUSB)
14+
15+
if ("${CMAKE_C_COMPILER_ID}" STREQUAL "Clang")
16+
target_compile_options(LibUSB PUBLIC "-Wno-zero-length-array")
17+
endif()
18+
219

320
if ("${CMAKE_SYSTEM_NAME}" STREQUAL "Windows")
4-
# Setup install
5-
include(GNUInstallDirs)
6-
install(
7-
FILES
8-
$<TARGET_FILE:LibUSB::LibUSB>
9-
DESTINATION
10-
${CMAKE_INSTALL_BINDIR}
11-
)
12-
endif()
21+
target_sources(LibUSB PRIVATE
22+
"${CMAKE_CURRENT_SOURCE_DIR}/src/libusb/os/poll_windows.c"
23+
"${CMAKE_CURRENT_SOURCE_DIR}/src/libusb/os/threads_windows.c"
24+
"${CMAKE_CURRENT_SOURCE_DIR}/src/libusb/os/windows_nt_common.c"
25+
"${CMAKE_CURRENT_SOURCE_DIR}/src/libusb/os/windows_usbdk.c"
26+
"${CMAKE_CURRENT_SOURCE_DIR}/src/libusb/os/windows_winusb.c")
27+
28+
target_include_directories(LibUSB PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src/msvc)
29+
30+
target_compile_definitions(LibUSB PRIVATE "_LIB" "_CRT_SECURE_NO_WARNINGS" "WINVER=0x0501" "_WIN32_WINNT=0x0501")
31+
32+
elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux")
33+
target_sources(LibUSB PRIVATE
34+
"${CMAKE_CURRENT_SOURCE_DIR}/src/libusb/os/poll_posix.c"
35+
"${CMAKE_CURRENT_SOURCE_DIR}/src/libusb/os/threads_posix.c"
36+
"${CMAKE_CURRENT_SOURCE_DIR}/src/libusb/os/linux_usbfs.c"
37+
"${CMAKE_CURRENT_SOURCE_DIR}/src/libusb/os/linux_udev.c")
38+
39+
target_include_directories(LibUSB PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/)
40+
41+
target_link_libraries(LibUSB PUBLIC "udev")
42+
43+
else()
44+
message(FATAL_ERROR "Unknown CMAKE_SYSTEM_NAME: ${CMAKE_SYSTEM_NAME}")
45+
endif()

extern/libusb/config.h

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
/* config.h. THIS WAS GENERATED FROM configure and autoheader and modified to be used in Linux builds */
2+
3+
/* config.h. Generated from config.h.in by configure. */
4+
/* config.h.in. Generated from configure.ac by autoheader. */
5+
6+
/* Default visibility */
7+
#define DEFAULT_VISIBILITY __attribute__((visibility("default")))
8+
9+
/* Start with debug message logging enabled */
10+
/* #undef ENABLE_DEBUG_LOGGING */
11+
12+
/* Message logging */
13+
#define ENABLE_LOGGING 1
14+
15+
/* Define to 1 if you have the <asm/types.h> header file. */
16+
/* #undef HAVE_ASM_TYPES_H */
17+
18+
/* Define to 1 if you have the declaration of `TFD_CLOEXEC', and to 0 if you
19+
don't. */
20+
#define HAVE_DECL_TFD_CLOEXEC 1
21+
22+
/* Define to 1 if you have the declaration of `TFD_NONBLOCK', and to 0 if you
23+
don't. */
24+
#define HAVE_DECL_TFD_NONBLOCK 1
25+
26+
/* Define to 1 if you have the <dlfcn.h> header file. */
27+
#define HAVE_DLFCN_H 1
28+
29+
/* Define to 1 if you have the <inttypes.h> header file. */
30+
#define HAVE_INTTYPES_H 1
31+
32+
/* Define to 1 if you have the `udev' library (-ludev). */
33+
#define HAVE_LIBUDEV 1
34+
35+
/* Define to 1 if you have the <libudev.h> header file. */
36+
#define HAVE_LIBUDEV_H 1
37+
38+
/* Define to 1 if you have the <linux/netlink.h> header file. */
39+
/* #undef HAVE_LINUX_NETLINK_H */
40+
41+
/* Define to 1 if you have the <memory.h> header file. */
42+
#define HAVE_MEMORY_H 1
43+
44+
/* Define to 1 if you have the `pipe2' function. */
45+
#define HAVE_PIPE2 1
46+
47+
/* Define to 1 if you have the <poll.h> header file. */
48+
#define HAVE_POLL_H 1
49+
50+
/* Define to 1 if you have the <stdint.h> header file. */
51+
#define HAVE_STDINT_H 1
52+
53+
/* Define to 1 if you have the <stdlib.h> header file. */
54+
#define HAVE_STDLIB_H 1
55+
56+
/* Define to 1 if you have the <strings.h> header file. */
57+
#define HAVE_STRINGS_H 1
58+
59+
/* Define to 1 if you have the <string.h> header file. */
60+
#define HAVE_STRING_H 1
61+
62+
/* Define to 1 if the system has the type `struct timespec'. */
63+
#define HAVE_STRUCT_TIMESPEC 1
64+
65+
/* syslog() function available */
66+
/* #undef HAVE_SYSLOG_FUNC */
67+
68+
/* Define to 1 if you have the <syslog.h> header file. */
69+
/* #undef HAVE_SYSLOG_H */
70+
71+
/* Define to 1 if you have the <sys/socket.h> header file. */
72+
/* #undef HAVE_SYS_SOCKET_H */
73+
74+
/* Define to 1 if you have the <sys/stat.h> header file. */
75+
#define HAVE_SYS_STAT_H 1
76+
77+
/* Define to 1 if you have the <sys/time.h> header file. */
78+
#define HAVE_SYS_TIME_H 1
79+
80+
/* Define to 1 if you have the <sys/types.h> header file. */
81+
#define HAVE_SYS_TYPES_H 1
82+
83+
/* Define to 1 if you have the <unistd.h> header file. */
84+
#define HAVE_UNISTD_H 1
85+
86+
/* Define to the sub-directory where libtool stores uninstalled libraries. */
87+
#define LT_OBJDIR ".libs/"
88+
89+
/* Darwin backend */
90+
/* #undef OS_DARWIN */
91+
92+
/* Haiku backend */
93+
/* #undef OS_HAIKU */
94+
95+
/* Linux backend */
96+
#define OS_LINUX 1
97+
98+
/* NetBSD backend */
99+
/* #undef OS_NETBSD */
100+
101+
/* OpenBSD backend */
102+
/* #undef OS_OPENBSD */
103+
104+
/* SunOS backend */
105+
/* #undef OS_SUNOS */
106+
107+
/* Windows backend */
108+
/* #undef OS_WINDOWS */
109+
110+
/* Name of package */
111+
#define PACKAGE "libusb"
112+
113+
/* Define to the address where bug reports for this package should be sent. */
114+
#define PACKAGE_BUGREPORT "[email protected]"
115+
116+
/* Define to the full name of this package. */
117+
#define PACKAGE_NAME "libusb"
118+
119+
/* Define to the full name and version of this package. */
120+
#define PACKAGE_STRING "libusb 1.0.22"
121+
122+
/* Define to the one symbol short name of this package. */
123+
#define PACKAGE_TARNAME "libusb"
124+
125+
/* Define to the home page for this package. */
126+
#define PACKAGE_URL "http://libusb.info"
127+
128+
/* Define to the version of this package. */
129+
#define PACKAGE_VERSION "1.0.22"
130+
131+
/* type of second poll() argument */
132+
#define POLL_NFDS_TYPE nfds_t
133+
134+
/* Define to 1 if you have the ANSI C header files. */
135+
#define STDC_HEADERS 1
136+
137+
/* Use POSIX Threads */
138+
#define THREADS_POSIX 1
139+
140+
/* timerfd headers available */
141+
#define USBI_TIMERFD_AVAILABLE 1
142+
143+
/* Enable output to system log */
144+
/* #undef USE_SYSTEM_LOGGING_FACILITY */
145+
146+
/* Use udev for device enumeration/hotplug */
147+
#define USE_UDEV 1
148+
149+
/* Version number of package */
150+
#define VERSION "1.0.22"
151+
152+
/* Oldest Windows version supported */
153+
/* #undef WINVER */
154+
155+
/* Oldest Windows version supported */
156+
/* #undef _WIN32_WINNT */
157+
158+
/* Define to `__inline__' or `__inline' if that's what the C compiler
159+
calls it, or to nothing if 'inline' is not supported under any name. */
160+
#ifndef __cplusplus
161+
/* #undef inline */
162+
#endif

extern/libusb/src

Submodule src added at 0034b2a

extern/libuvc/CMakeLists.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
if ("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux")
22
if (NOT TARGET project_libuvc)
3-
find_package(LibUSB REQUIRED)
4-
53
set(CMAKE_BUILD_TARGET Static)
64

75
include(ExternalProject)

scripts/bootstrap-ubuntu.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,6 @@ sudo apt install -y \
3131
libxrandr-dev \
3232
libusb-1.0-0-dev \
3333
libssl-dev \
34+
libudev-dev \
3435
mesa-common-dev \
3536
uuid-dev

src/usbcommand/CMakeLists.txt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,11 @@ add_library(k4a_usb_cmd STATIC
77
)
88

99
# Consumers should #include <k4ainternal/usbcommand.h>
10-
target_include_directories(k4a_usb_cmd PUBLIC
10+
target_include_directories(k4a_usb_cmd PUBLIC
1111
${K4A_PRIV_INCLUDE_DIR})
1212

13-
find_package(LibUSB REQUIRED)
14-
1513
# Dependencies of this library
16-
target_link_libraries(k4a_usb_cmd PUBLIC
14+
target_link_libraries(k4a_usb_cmd PUBLIC
1715
azure::aziotsharedutil
1816
LibUSB::LibUSB
1917
k4ainternal::allocator

tools/k4aviewer/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ configure_file(
4949
)
5050

5151
find_package(OpenGL REQUIRED)
52-
find_package(LibUSB REQUIRED)
5352
include_directories(
5453
${OPENGL_INCLUDE_DIRS}
5554
${CMAKE_CURRENT_LIST_DIR}

0 commit comments

Comments
 (0)