Debian Codegraph @

contrib/depends/patches/libusb/CMakeLists.txt

raw
1 # From https://github.com/libusb/libusb-cmake/tree/main
2
3 cmake_minimum_required(VERSION 3.16)
4
5 get_filename_component(LIBUSB_ROOT "libusb" ABSOLUTE)
6
7 # Get the version information from version.h ignoring the nano version as it appears in version_nano.h and so we need it?
8 file(READ "${LIBUSB_ROOT}/version.h" VERSIONHEADERDATA)
9 string(REGEX MATCH "#define LIBUSB_MAJOR ([0-9]*)" _ ${VERSIONHEADERDATA})
10 set(LIBUSB_VERSION_MAJOR ${CMAKE_MATCH_1})
11 string(REGEX MATCH "#define LIBUSB_MINOR ([0-9]*)" _ ${VERSIONHEADERDATA})
12 set(LIBUSB_VERSION_MINOR ${CMAKE_MATCH_1})
13 string(REGEX MATCH "#define LIBUSB_MICRO ([0-9]*)" _ ${VERSIONHEADERDATA})
14 set(LIBUSB_VERSION_MICRO ${CMAKE_MATCH_1})
15 set(LIBUSB_VERSION "${LIBUSB_VERSION_MAJOR}.${LIBUSB_VERSION_MINOR}.${LIBUSB_VERSION_MICRO}")
16
17 project(usb-1.0
18 DESCRIPTION "A cross-platform library to access USB devices"
19 VERSION ${LIBUSB_VERSION}
20 LANGUAGES C
21 )
22 if(EMSCRIPTEN)
23 set(CMAKE_CXX_STANDARD 20)
24 enable_language(CXX)
25 endif()
26
27 # This function generates all the local variables what end up getting written to config.
28 # We use a function as any vars set in this context don't mess with the rest of the file.
29 # e.g. Logging LIBUSB_ENABLE_LOGGING mapps to ENABLE_LOGGING in the config, keeps it clean
30 function(generate_config_file)
31 include(CheckIncludeFiles)
32 include(CheckFunctionExists)
33 include(CheckSymbolExists)
34 include(CheckStructHasMember)
35 include(CheckCCompilerFlag)
36
37 check_function_exists(clock_gettime HAVE_CLOCK_GETTIME)
38 check_function_exists(pthread_condattr_setclock HAVE_PTHREAD_CONDATTR_SETCLOCK)
39 check_function_exists(pthread_setname_np HAVE_PTHREAD_SETNAME_NP)
40 check_function_exists(pthread_threadid_np HAVE_PTHREAD_THREADID_NP)
41 check_function_exists(eventfd HAVE_EVENTFD)
42 check_function_exists(pipe2 HAVE_PIPE2)
43 check_function_exists(syslog HAVE_SYSLOG)
44
45 check_include_files(asm/types.h HAVE_ASM_TYPES_H)
46 check_include_files(sys/eventfd.h HAVE_EVENTFD)
47 check_include_files(string.h HAVE_STRING_H)
48 check_include_files(sys/time.h HAVE_SYS_TIME_H)
49
50 check_symbol_exists(timerfd_create "sys/timerfd.h" HAVE_TIMERFD)
51 check_symbol_exists(nfds_t "poll.h" HAVE_NFDS_T)
52
53 check_struct_has_member("struct timespec" tv_sec time.h HAVE_STRUCT_TIMESPEC)
54
55 if(HAVE_VISIBILITY)
56 set(DEFAULT_VISIBILITY "__attribute__((visibility(\"default\")))")
57 else()
58 set(DEFAULT_VISIBILITY "" )
59 endif()
60
61 # Set vars that will be written into the config file.
62 if(WIN32)
63 set(PLATFORM_WINDOWS 1)
64 else()
65 set(PLATFORM_POSIX 1)
66 endif()
67
68 if(LIBUSB_ENABLE_LOGGING)
69 set(ENABLE_LOGGING ${LIBUSB_ENABLE_LOGGING})
70 endif()
71 if(LIBUSB_ENABLE_DEBUG_LOGGING)
72 set(ENABLE_DEBUG_LOGGING ${LIBUSB_ENABLE_DEBUG_LOGGING})
73 endif()
74
75 if(CMAKE_C_COMPILER_ID MATCHES "Clang" OR CMAKE_C_COMPILER_ID STREQUAL "GNU")
76 check_c_compiler_flag("-fvisibility=hidden" HAVE_VISIBILITY)
77 endif()
78
79 file(MAKE_DIRECTORY "${LIBUSB_GEN_INCLUDES}")
80 if(NOT MSVC)
81 set(_GNU_SOURCE TRUE)
82 endif()
83 configure_file(config.h.in "${LIBUSB_GEN_INCLUDES}/config.h" @ONLY)
84 endfunction()
85
86 if(BUILD_SHARED_LIBS)
87 set(LIBUSB_BUILD_SHARED_LIBS_DEFAULT ON)
88 else()
89 set(LIBUSB_BUILD_SHARED_LIBS_DEFAULT OFF)
90 endif()
91
92 option(LIBUSB_BUILD_SHARED_LIBS "Build Shared Libraries for libusb" ${LIBUSB_BUILD_SHARED_LIBS_DEFAULT})
93 option(LIBUSB_BUILD_TESTING "Build Tests" OFF)
94 if(LIBUSB_BUILD_TESTING)
95 enable_testing()
96 endif()
97
98 option(LIBUSB_BUILD_EXAMPLES "Build Example Applications" OFF)
99
100 option(LIBUSB_INSTALL_TARGETS "Install libusb targets" ON)
101 option(LIBUSB_TARGETS_INCLUDE_USING_SYSTEM "Make targets include paths System" ON)
102
103 option(LIBUSB_ENABLE_LOGGING "Enable Logging" ON)
104 option(LIBUSB_ENABLE_DEBUG_LOGGING "Enable Debug Logging" OFF)
105 if(CMAKE_SYSTEM_NAME MATCHES "Linux")
106 option(LIBUSB_ENABLE_UDEV "Enable udev backend for device enumeration" ON)
107 endif()
108
109 set(LIBUSB_GEN_INCLUDES "${CMAKE_CURRENT_BINARY_DIR}/gen_include")
110 generate_config_file()
111
112 if(LIBUSB_BUILD_SHARED_LIBS)
113 add_library(usb-1.0 SHARED)
114 else()
115 add_library(usb-1.0 STATIC)
116 endif()
117
118 set_target_properties(usb-1.0 PROPERTIES
119 PREFIX lib # to be consistent with mainline libusb build system(s)
120 )
121
122 # common sources
123 target_sources(usb-1.0 PRIVATE
124 "${LIBUSB_GEN_INCLUDES}/config.h"
125 "${LIBUSB_ROOT}/core.c"
126 "${LIBUSB_ROOT}/descriptor.c"
127 "${LIBUSB_ROOT}/hotplug.c"
128 "${LIBUSB_ROOT}/io.c"
129 "${LIBUSB_ROOT}/libusb.h"
130 "${LIBUSB_ROOT}/libusbi.h"
131 "${LIBUSB_ROOT}/strerror.c"
132 "${LIBUSB_ROOT}/sync.c"
133 "${LIBUSB_ROOT}/version.h"
134 "${LIBUSB_ROOT}/version_nano.h"
135 )
136 target_include_directories(usb-1.0
137 PRIVATE
138 "${LIBUSB_GEN_INCLUDES}"
139 "${LIBUSB_ROOT}/os"
140 )
141
142 if (LIBUSB_TARGETS_INCLUDE_USING_SYSTEM)
143 target_include_directories(usb-1.0 SYSTEM PUBLIC "${LIBUSB_ROOT}")
144 else()
145 target_include_directories(usb-1.0 PUBLIC "${LIBUSB_ROOT}")
146 endif()
147
148 if(WIN32)
149 target_sources(usb-1.0 PRIVATE
150 "${LIBUSB_ROOT}/libusb-1.0.def"
151 "${LIBUSB_ROOT}/os/events_windows.c"
152 "${LIBUSB_ROOT}/os/events_windows.h"
153 "${LIBUSB_ROOT}/os/threads_windows.c"
154 "${LIBUSB_ROOT}/os/threads_windows.h"
155 "${LIBUSB_ROOT}/os/windows_common.c"
156 "${LIBUSB_ROOT}/os/windows_common.h"
157 "${LIBUSB_ROOT}/os/windows_usbdk.c"
158 "${LIBUSB_ROOT}/os/windows_usbdk.h"
159 "${LIBUSB_ROOT}/os/windows_winusb.c"
160 "${LIBUSB_ROOT}/os/windows_winusb.h"
161 $<$<C_COMPILER_ID:MSVC>:${LIBUSB_ROOT}/libusb-1.0.rc>
162 )
163 target_compile_definitions(usb-1.0 PRIVATE $<$<C_COMPILER_ID:MSVC>:_CRT_SECURE_NO_WARNINGS=1>)
164 target_link_libraries(usb-1.0 PRIVATE windowsapp)
165 set_target_properties(usb-1.0 PROPERTIES UNITY_BUILD OFF)
166 else()
167 # common POSIX/non-Windows sources
168 target_sources(usb-1.0 PRIVATE
169 "${LIBUSB_ROOT}/os/events_posix.c"
170 "${LIBUSB_ROOT}/os/events_posix.h"
171 "${LIBUSB_ROOT}/os/threads_posix.c"
172 "${LIBUSB_ROOT}/os/threads_posix.h"
173 )
174 if(CMAKE_SYSTEM_NAME MATCHES "Linux")
175 target_sources(usb-1.0 PRIVATE
176 "${LIBUSB_ROOT}/os/linux_usbfs.c"
177 "${LIBUSB_ROOT}/os/linux_usbfs.h"
178 )
179 if(LIBUSB_ENABLE_UDEV)
180 target_sources(usb-1.0 PRIVATE
181 "${LIBUSB_ROOT}/os/linux_udev.c"
182 )
183 target_link_libraries(usb-1.0 PRIVATE udev)
184 target_compile_definitions(usb-1.0 PRIVATE HAVE_LIBUDEV=1)
185 else()
186 target_sources(usb-1.0 PRIVATE
187 "${LIBUSB_ROOT}/os/linux_netlink.c"
188 )
189 endif()
190 find_package(Threads REQUIRED)
191 target_link_libraries(usb-1.0 PRIVATE Threads::Threads)
192 elseif(ANDROID)
193 target_sources(usb-1.0 PRIVATE
194 "${LIBUSB_ROOT}/os/linux_netlink.c"
195 "${LIBUSB_ROOT}/os/linux_usbfs.c"
196 "${LIBUSB_ROOT}/os/linux_usbfs.h"
197 )
198 target_link_libraries(usb-1.0 PRIVATE android log)
199 elseif(APPLE)
200 target_sources(usb-1.0 PRIVATE
201 "${LIBUSB_ROOT}/os/darwin_usb.c"
202 "${LIBUSB_ROOT}/os/darwin_usb.h"
203 )
204 target_link_libraries(usb-1.0 PRIVATE
205 "-framework Foundation"
206 "-framework IOKit"
207 "-framework Security"
208 )
209 elseif(CMAKE_SYSTEM_NAME STREQUAL "NetBSD")
210 target_sources(usb-1.0 PRIVATE
211 "${LIBUSB_ROOT}/os/netbsd_usb.c"
212 )
213 elseif(CMAKE_SYSTEM_NAME STREQUAL "OpenBSD")
214 target_sources(usb-1.0 PRIVATE
215 "${LIBUSB_ROOT}/os/openbsd_usb.c"
216 )
217 elseif(EMSCRIPTEN)
218 target_sources(usb-1.0 PRIVATE
219 "${LIBUSB_ROOT}/os/emscripten_webusb.cpp"
220 )
221 target_compile_options(usb-1.0 PRIVATE -pthread)
222 else()
223 message(FATAL_ERROR "Unsupported target platform: ${CMAKE_SYSTEM_NAME}")
224 endif()
225 endif()
226
227 if(LIBUSB_BUILD_TESTING)
228 add_subdirectory(tests)
229 endif()
230
231 if(LIBUSB_BUILD_EXAMPLES)
232 add_subdirectory(examples)
233 endif()
234
235 if(LIBUSB_INSTALL_TARGETS)
236 install(TARGETS usb-1.0)
237 install(FILES "${LIBUSB_ROOT}/libusb.h" DESTINATION "include/libusb-1.0")
238 endif()