summaryrefslogtreecommitdiff
path: root/contrib/SDL-3.2.8/src/hidapi/BUILD.cmake.md
diff options
context:
space:
mode:
author3gg <3gg@shellblade.net>2025-12-27 12:03:39 -0800
committer3gg <3gg@shellblade.net>2025-12-27 12:03:39 -0800
commit5a079a2d114f96d4847d1ee305d5b7c16eeec50e (patch)
tree8926ab44f168acf787d8e19608857b3af0f82758 /contrib/SDL-3.2.8/src/hidapi/BUILD.cmake.md
Initial commit
Diffstat (limited to 'contrib/SDL-3.2.8/src/hidapi/BUILD.cmake.md')
-rw-r--r--contrib/SDL-3.2.8/src/hidapi/BUILD.cmake.md280
1 files changed, 280 insertions, 0 deletions
diff --git a/contrib/SDL-3.2.8/src/hidapi/BUILD.cmake.md b/contrib/SDL-3.2.8/src/hidapi/BUILD.cmake.md
new file mode 100644
index 0000000..573f910
--- /dev/null
+++ b/contrib/SDL-3.2.8/src/hidapi/BUILD.cmake.md
@@ -0,0 +1,280 @@
1# Building HIDAPI using CMake
2
3To build HIDAPI with CMake, it has to be [installed](#installing-cmake)/available in the system.
4
5Make sure you've checked [prerequisites](BUILD.md#prerequisites) and installed all required dependencies.
6
7HIDAPI CMake build system allows you to build HIDAPI in two generally different ways:
81) As a [standalone package/library](#standalone-package-build);
92) As [part of a larger CMake project](#hidapi-as-a-subdirectory).
10
11**TL;DR**: if you're experienced developer and have been working with CMake projects or have been written some of your own -
12most of this document may not be of interest for you; just check variables names, its default values and the target names.
13
14## Installing CMake
15
16CMake can be installed either using your system's package manager,
17or by downloading an installer/prebuilt version from the [official website](https://cmake.org/download/).
18
19On most \*nix systems, the preferred way to install CMake is via package manager,
20e.g. `sudo apt install cmake`.
21
22On Windows CMake could be provided by your development environment (e.g. by Visual Studio Installer or MinGW installer),
23or you may install it system-wise using the installer from the official website.
24
25On macOS CMake may be installed by Homebrew/MacPorts or using the installer from the official website.
26
27## Standalone package build
28
29To build HIDAPI as a standalone package, you follow [general steps](https://cmake.org/runningcmake/) of building any CMake project.
30
31An example of building HIDAPI with CMake:
32```sh
33# precondition: create a <build dir> somewhere on the filesystem (preferably outside of the HIDAPI source)
34# this is the place where all intermediate/build files are going to be located
35cd <build dir>
36# configure the build
37cmake <HIDAPI source dir>
38# build it!
39cmake --build .
40# install library; by default installs into /usr/local/
41cmake --build . --target install
42# NOTE: you need to run install command as root, to be able to install into /usr/local/
43```
44Such invocation will use the default (as per CMake magic) compiler/build environment available in your system.
45
46You may pass some additional CMake variables to control the build configuration as `-D<CMake Variable>=value`.
47E.g.:
48```sh
49# install command now would install things into /usr
50cmake <HIDAPI source dir> -DCMAKE_INSTALL_PREFIX=/usr
51```
52
53<details>
54 <summary>Using a specific CMake generator</summary>
55
56An example of using `Ninja` as a CMake generator:
57
58```sh
59cd <build dir>
60# configure the build
61cmake -GNinja <HIDAPI source dir>
62# we know, that CMake has generated build files for Ninja,
63# so we can use `ninja` directly, instead of `cmake --build .`
64ninja
65# install library
66ninja install
67```
68
69`-G` here specifies a native build system CMake would generate build files for.
70Check [CMake Documentation](https://cmake.org/cmake/help/latest/manual/cmake-generators.7.html) for a list of available generators (system-specific).
71
72</details><br>
73
74Some of the [standard](https://cmake.org/cmake/help/latest/manual/cmake-variables.7.html) CMake variables you may want to use to configure a build:
75
76- [`CMAKE_INSTALL_PREFIX`](https://cmake.org/cmake/help/latest/variable/CMAKE_INSTALL_PREFIX.html) - prefix where `install` target would install the library(ies);
77- [`CMAKE_BUILD_TYPE`](https://cmake.org/cmake/help/latest/variable/CMAKE_BUILD_TYPE.html) - standard possible values: `Debug`, `Release`, `RelWithDebInfo`, `MinSizeRel`; Defaults to `Release` for HIDAPI, if not specified;
78- [`BUILD_SHARED_LIBS`](https://cmake.org/cmake/help/latest/variable/BUILD_SHARED_LIBS.html) - when set to TRUE, HIDAPI is built as a shared library, otherwise build statically; Defaults to `TRUE` for HIDAPI, if not specified;
79
80<details>
81 <summary>macOS-specific variables</summary>
82
83 - [`CMAKE_FRAMEWORK`](https://cmake.org/cmake/help/latest/variable/CMAKE_FRAMEWORK.html) - (since CMake 3.15) when set to TRUE, HIDAPI is built as a framework library, otherwise build as a regular static/shared library; Defaults to `FALSE` for HIDAPI, if not specified;
84 - [`CMAKE_OSX_DEPLOYMENT_TARGET`](https://cmake.org/cmake/help/latest/variable/CMAKE_OSX_DEPLOYMENT_TARGET.html) - minimum version of the target platform (e.g. macOS or iOS) on which the target binaries are to be deployed; defaults to a maximum supported target platform by currently used XCode/Toolchain;
85
86</details><br>
87
88HIDAPI-specific CMake variables:
89
90- `HIDAPI_BUILD_HIDTEST` - when set to TRUE, build a small test application `hidtest`;
91- `HIDAPI_WITH_TESTS` - when set to TRUE, build all (unit-)tests;
92currently this option is only available on Windows, since only Windows backend has tests;
93
94<details>
95 <summary>Linux-specific variables</summary>
96
97 - `HIDAPI_WITH_HIDRAW` - when set to TRUE, build HIDRAW-based implementation of HIDAPI (`hidapi-hidraw`), otherwise don't build it; defaults to TRUE;
98 - `HIDAPI_WITH_LIBUSB` - when set to TRUE, build LIBUSB-based implementation of HIDAPI (`hidapi-libusb`), otherwise don't build it; defaults to TRUE;
99
100 **NOTE**: at least one of `HIDAPI_WITH_HIDRAW` or `HIDAPI_WITH_LIBUSB` has to be set to TRUE.
101
102</details><br>
103
104To see all most-useful CMake variables available for HIDAPI, one of the most convenient ways is too use [`cmake-gui`](https://cmake.org/cmake/help/latest/manual/cmake-gui.1.html) tool ([example](https://cmake.org/runningcmake/)).
105
106_NOTE_: HIDAPI packages built by CMake can be used with `pkg-config`, as if built with [Autotools](BUILD.autotools.md).
107
108### MSVC and Ninja
109It is possible to build a CMake project (including HIDAPI) using MSVC compiler and Ninja (for medium and larger projects it is so much faster than msbuild).
110
111For that:
1121) Open cmd.exe;
1132) Setup MSVC build environment variables, e.g.: `vcvarsall.bat x64`, where:
114 - `vcvarsall.bat` is an environment setup script of your MSVC toolchain installation;<br>For MSVC 2019 Community edition it is located at: `C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\`;
115 - `x64` -a target architecture to build;
1163) Follow general build steps, and use `Ninja` as a generator.
117
118### Using HIDAPI in a CMake project
119
120When HIDAPI is used as a standalone package (either installed into the system or built manually and installed elsewhere), the simplest way to use it is as showed in the example:
121
122```cmake
123project(my_application)
124
125add_executable(my_application main.c)
126
127find_package(hidapi REQUIRED)
128target_link_libraries(my_application PRIVATE hidapi::hidapi)
129```
130
131If HIDAPI isn't installed in your system, or `find_package` cannot find HIDAPI by default for any other reasons,
132the recommended way manually specify which HIDAPI package to use is via `hidapi_ROOT` CMake variable, e.g.:
133`-Dhidapi_ROOT=<path to HIDAPI installation prefix>`.
134
135_NOTE_: usage of `hidapi_ROOT` is only possible (and recommended) with CMake 3.12 and higher. For older versions of CMake you'd need to specify [`CMAKE_PREFIX_PATH`](https://cmake.org/cmake/help/latest/variable/CMAKE_PREFIX_PATH.html#variable:CMAKE_PREFIX_PATH) instead.
136
137Check with [`find_package`](https://cmake.org/cmake/help/latest/command/find_package.html) documentation if you need more details.
138
139Available CMake targets after successful `find_package(hidapi)`:
140- `hidapi::hidapi` - indented to be used in most cases;
141- `hidapi::include` - if you need only to include `<hidapi.h>` but not link against the library;
142- `hidapi::winapi` - same as `hidapi::hidapi` on Windows; available only on Windows;
143- `hidapi::darwin` - same as `hidapi::hidapi` on macOS; available only on macOS;
144- `hidapi::libusb` - available when libusb backend is used/available;
145- `hidapi::hidraw` - available when hidraw backend is used/available on Linux;
146
147**NOTE**: on Linux often both `hidapi::libusb` and `hidapi::hidraw` backends are available; in that case `hidapi::hidapi` is an alias for **`hidapi::hidraw`**. The motivation is that `hidraw` backend is a native Linux kernel implementation of HID protocol, and supports various HID devices (USB, Bluetooth, I2C, etc.). If `hidraw` backend isn't built at all (`hidapi::libusb` is the only target) - `hidapi::hidapi` is an alias for `hidapi::libusb`.
148If you're developing a cross-platform application and you are sure you need to use `libusb` backend on Linux, the simple way to achieve this is:
149```cmake
150if(TARGET hidapi::libusb)
151 target_link_libraries(my_project PRIVATE hidapi::libusb)
152else()
153 target_link_libraries(my_project PRIVATE hidapi::hidapi)
154endif()
155```
156
157## HIDAPI as a subdirectory
158
159HIDAPI can be easily used as a subdirectory of a larger CMake project:
160```cmake
161# root CMakeLists.txt
162cmake_minimum_required(VERSION 3.4.3...3.25 FATAL_ERROR)
163
164add_subdirectory(hidapi)
165add_subdirectory(my_application)
166
167# my_application/CMakeLists.txt
168project(my_application)
169
170add_executable(my_application main.c)
171
172# NOTE: no `find_package` is required, since HIDAPI targets are already a part of the project tree
173target_link_libraries(my_application PRIVATE hidapi::hidapi)
174```
175Lets call this "larger project" a "host project".
176
177All of the variables described in [standalone build](#standalone-package-build) section can be used to control HIDAPI build in case of a subdirectory, e.g.:
178```cmake
179set(HIDAPI_WITH_LIBUSB FALSE) # surely will be used only on Linux
180set(BUILD_SHARED_LIBS FALSE) # HIDAPI as static library on all platforms
181add_subdirectory(hidapi)
182```
183
184<details>
185 <summary>NOTE</summary>
186
187 If you project happen to use `BUILD_SHARED_LIBS` as a `CACHE` variable globally for you project, setting it as simple variable, as showed above _will have not affect_ up until _CMake 3.13_. See [CMP0077](https://cmake.org/cmake/help/latest/policy/CMP0077.html) for details.
188</details><br>
189
190There are several important differences in the behavior of HIDAPI CMake build system when CMake is built as standalone package vs subdirectory build:
191
1921) In _standalone build_ a number of standard and HIDAPI-specific variables are marked as _cache variables_ or _options_.
193This is done for convenience: when you're building HIDAPI as a standalone package and using tools like `cmake-gui` - those are highlighted as variables that can be changed and has some short description/documentation. E.g.:
194![an example of highlighted variables in cmake-gui](documentation/cmake-gui-highlights.png "cmake-gui highlighted variables")<br>
195E.g.2:<br>
196![an example of drop-down menu in cmake-gui](documentation/cmake-gui-drop-down.png "cmake-gui drop-down menu")<br>
197When HIDAPI is built as a _subdirectory_ - **_none of the variables are marked for cache or as options_** by HIDAPI.
198This is done to let the host project's developer decide what is important (what needs to be highlighted) and what's not.
199
2002) The default behavior/default value for some of the variables is a bit different:
201 - by default, none of HIDAPI targets are [installed](https://cmake.org/cmake/help/latest/command/install.html); if required, HIDAPI targets can be installed by host project _after_ including HIDAPI subdirectory (requires CMake 3.13 or later); **or**, the default installation can be enabled by setting `HIDAPI_INSTALL_TARGETS` variable _before_ including HIDAPI subdirectory.
202 HIDAPI uses [GNUInstallDirs](https://cmake.org/cmake/help/latest/module/GNUInstallDirs.html) to specify install locations. Variables like `CMAKE_INSTALL_LIBDIR` can be used to control HIDAPI's installation locations. E.g.:
203 ```cmake
204 # enable the installation if you need it
205 set(HIDAPI_INSTALL_TARGETS ON)
206 # (optionally) change default installation locations if it makes sense for your target platform, etc.
207 set(CMAKE_INSTALL_LIBDIR "lib64")
208 add_subdirectory(hidapi)
209 ```
210 - HIDAPI prints its version during the configuration when built as a standalone package; to enable this for subdirectory builds - set `HIDAPI_PRINT_VERSION` to TRUE before including HIDAPI;
211
2123) In a subdirectory build, HIDAPI _doesn't modify or set any of the CMake variables_ that may change the build behavior.
213 For instance, in a _standalone build_, if CMAKE_BUILD_TYPE or BUILD_SHARED_LIBS variables are not set, those are defaulted to "Release" and "TRUE" explicitly.
214 In a _subdirectory build_, even if not set, those variables remain unchanged, so a host project's developer has a full control over the HIDAPI build configuration.
215
216Available CMake targets after `add_subdirectory(hidapi)` _are the same as in case of [standalone build](#standalone-package-build)_, and a few additional ones:
217- `hidapi_include` - the interface library; `hidapi::include` is an alias of it;
218- `hidapi_winapi` - library target on Windows; `hidapi::winapi` is an alias of it;
219- `hidapi_darwin` - library target on macOS; `hidapi::darwin` is an alias of it;
220- `hidapi_libusb` - library target for libusb backend; `hidapi::libusb` is an alias of it;
221- `hidapi_hidraw` - library target for hidraw backend; `hidapi::hidraw` is an alias of it;
222- `hidapi-libusb` - an alias of `hidapi_libusb` for compatibility with raw library name;
223- `hidapi-hidraw` - an alias of `hidapi_hidraw` for compatibility with raw library name;
224- `hidapi` - an alias of `hidapi_winapi` or `hidapi_darwin` on Windows or macOS respectfully.
225
226Advanced:
227- Why would I need additional targets described in this section above, if I already have alias targets compatible with `find_package`?
228 - an example:
229 ```cmake
230 add_subdirectory(hidapi)
231 if(TARGET hidapi_libusb)
232 # see libusb/hid.c for usage of `NO_ICONV`
233 target_compile_definitions(hidapi_libusb PRIVATE NO_ICONV)
234 endif()
235 ```
236
237## Both Shared and Static build
238
239If you're a former (or present) user of Autotools build scripts for HIDAPI, or you're a package manager maintainer and you're often working with those - you're likely asking how to build HIDAPI with CMake and get both Shared and Static libraries (as would be done by Autotools: `./configure --enable-static --enable-shared ...`).
240
241CMake doesn't have such option of-the-box and it is decided not to introduce any manual CMake-level workarounds for HIDAPI on this matter.
242
243If you want to mimic the Autotools behavior, it is possible by building/installing first the static version of the library and then shared version of the library. The installation folder (`CMAKE_INSTALL_PREFIX`) should point to the same directory for both variants, that way:
244- both static and shared library binaries will be available and usable;
245- a single header file(s) for both of them;
246- Autotools/pkg-config (`.pc`) files will be generated and usable _as if_ generated by Autotools natively and build configured with both `-enable-static --enable-shared` options;
247- CMake package scripts will be generated and fully usable, but _only the last build installed_, i.e. if the last was installed Shared version of the binary - CMake targets found by `find_package(hidapi)` would point to a Shared binaries.
248
249There is a historical discussion, why such solution is simplest/preferable: https://github.com/libusb/hidapi/issues/424
250
251#### TL;DR/Sample
252
253```sh
254# First - configure/build
255
256# Static libraries
257cmake -S <HIDAPI source dir> -B "<build dir>/static" -DCMAKE_INSTALL_PREFIX=<your installation prefix> -DBUILD_SHARED_LIBS=FALSE
258cmake --build "<build dir>/static"
259# Shared libraries
260cmake -S <HIDAPI source dir> -B "<build dir>/shared" -DCMAKE_INSTALL_PREFIX=<your installation prefix> -DBUILD_SHARED_LIBS=TRUE
261cmake --build "<build dir>/shared"
262
263# (Optionally) change the installation destination.
264# NOTE1: this is supported by CMake only on UNIX platforms
265# See https://cmake.org/cmake/help/latest/envvar/DESTDIR.html
266# NOTE2: this is not the same as `CMAKE_INSTALL_PREFIX` set above
267# NOTE3: this is only required if you have a staging dir other than the final runtime dir,
268# e.g. during cross-compilation
269export DESTDIR="$STAGING_DIR"
270
271#
272# Install the libraries
273# NOTE: order of installation matters - install Shared variant *the last*
274
275# Static libraries
276cmake --install "<build dir>/static"
277# Shared libraries
278cmake --install "<build dir>/shared"
279
280```