Skip to content

Commit 5c5c840

Browse files
committed
cmake: add HIDAPI_BUILD_AS_CXX to compile sources as C++
Add an opt-in CMake variable HIDAPI_BUILD_AS_CXX that, when set to TRUE, attaches LANGUAGE CXX to each backend's C source file so it is compiled with the C++ compiler instead of the C compiler. Useful as a portability check, to confirm hidapi sources remain compatible with consumers that embed them into a C++ translation unit. The variable is intentionally not declared as a CMake option(), so it is not surfaced in cmake-gui or `cmake -L` output. It is documented under "Testing-only variables" in BUILD.cmake.md. Wire it through every backend (linux, libusb, mac, netbsd, windows). For the windows backend, both hid.c and hidapi_descriptor_reconstruct.c are re-tagged. Three small mac/hid.c adjustments are required for Apple Clang to accept the source as C++: - cast CFTypeRef to CFNumberRef at CFNumberGetValue() (C allows implicit void*-to-pointer conversion; C++ does not); - mark BUF_LEN const so the stack buffer is a non-VLA array; - cast calloc()'s return value when assigning to cur_dev->path. CI: enable HIDAPI_BUILD_AS_CXX=ON on the ubuntu-cmake, macos-cmake and windows-cmake jobs to guard against regressions. The MSVC variant also sets CMAKE_CXX_STANDARD=20 so the designated initialisers in the Windows backend compile as ISO C++ (g++ and Clang accept them as a GNU extension in their default mode). Assisted-by: Claude:claude-opus-4.7
1 parent e8243e1 commit 5c5c840

9 files changed

Lines changed: 57 additions & 3 deletions

File tree

.github/workflows/builds.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,15 @@ jobs:
9090
cd build/static_test
9191
make install
9292
93+
- name: Check C++ portability build
94+
run: |
95+
rm -rf build/cxx
96+
cmake -B build/cxx -S hidapisrc \
97+
-DCMAKE_BUILD_TYPE=Release \
98+
-DHIDAPI_BUILD_AS_CXX=ON \
99+
"-DCMAKE_CXX_FLAGS=-Wall -Wextra -Werror"
100+
cmake --build build/cxx
101+
93102
- name: Check Meson build
94103
run: |
95104
meson setup build_meson hidapisrc
@@ -154,6 +163,15 @@ jobs:
154163
cd build/static_test
155164
make install
156165
166+
- name: Check C++ portability build
167+
run: |
168+
rm -rf build/cxx
169+
cmake -B build/cxx -S hidapisrc \
170+
-DCMAKE_BUILD_TYPE=Release \
171+
-DHIDAPI_BUILD_AS_CXX=ON \
172+
"-DCMAKE_CXX_FLAGS=-Wall -Wextra -Werror -Wformat-signedness"
173+
cmake --build build/cxx
174+
157175
- name: Check Meson build
158176
run: |
159177
meson setup build_meson hidapisrc
@@ -206,6 +224,14 @@ jobs:
206224
working-directory: build/msvc
207225
run: ctest -C RelWithDebInfo --no-compress-output --output-on-failure
208226

227+
- name: Configure CMake MSVC C++
228+
shell: cmd
229+
run: |
230+
cmake -B build\msvc_cxx -S hidapisrc -DCMAKE_BUILD_TYPE=Release -DHIDAPI_BUILD_AS_CXX=ON -DCMAKE_CXX_STANDARD=20 "-DCMAKE_CXX_FLAGS=%MSVC_COMPILE_FLAGS%"
231+
- name: Build CMake MSVC C++
232+
working-directory: build/msvc_cxx
233+
run: cmake --build . --config Release
234+
209235
- name: Configure CMake NMake MSVC
210236
shell: cmd
211237
run: |

BUILD.cmake.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,13 @@ currently this option is only available on Windows, since only Windows backend h
101101

102102
</details><br>
103103

104+
<details>
105+
<summary>Testing-only variables</summary>
106+
107+
- `HIDAPI_BUILD_AS_CXX` - when set to TRUE, the HIDAPI C source files are compiled with a C++ compiler instead of a C compiler. Useful to confirm that HIDAPI's sources stay compatible with consumers that include them into a C++ translation unit. Not intended for production builds; defaults to FALSE.
108+
109+
</details><br>
110+
104111
To 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/)).
105112

106113
_NOTE_: HIDAPI packages built by CMake can be used with `pkg-config`, as if built with [Autotools](BUILD.autotools.md).

libusb/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ add_library(hidapi_libusb
44
${HIDAPI_PUBLIC_HEADERS}
55
hid.c
66
)
7+
if(HIDAPI_BUILD_AS_CXX)
8+
set_source_files_properties(hid.c PROPERTIES LANGUAGE CXX)
9+
endif()
710
target_link_libraries(hidapi_libusb PUBLIC hidapi_include)
811

912
if(TARGET usb-1.0)

linux/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ add_library(hidapi_hidraw
22
${HIDAPI_PUBLIC_HEADERS}
33
hid.c
44
)
5+
if(HIDAPI_BUILD_AS_CXX)
6+
set_source_files_properties(hid.c PROPERTIES LANGUAGE CXX)
7+
endif()
58
target_link_libraries(hidapi_hidraw PUBLIC hidapi_include)
69

710
find_package(Threads REQUIRED)

mac/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ add_library(hidapi_darwin
44
${HIDAPI_PUBLIC_HEADERS}
55
hid.c
66
)
7+
if(HIDAPI_BUILD_AS_CXX)
8+
set_source_files_properties(hid.c PROPERTIES LANGUAGE CXX)
9+
endif()
710

811
find_package(Threads REQUIRED)
912

mac/hid.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ static bool try_get_ioregistry_int_property(io_service_t service, CFStringRef pr
339339

340340
if (ref) {
341341
if (CFGetTypeID(ref) == CFNumberGetTypeID()) {
342-
result = CFNumberGetValue(ref, kCFNumberSInt32Type, out_val);
342+
result = CFNumberGetValue((CFNumberRef) ref, kCFNumberSInt32Type, out_val);
343343
}
344344

345345
CFRelease(ref);
@@ -544,7 +544,7 @@ static struct hid_device_info *create_device_info_with_usage(IOHIDDeviceRef dev,
544544
{
545545
unsigned short dev_vid;
546546
unsigned short dev_pid;
547-
int BUF_LEN = 256;
547+
const int BUF_LEN = 256;
548548
wchar_t buf[BUF_LEN];
549549
CFTypeRef transport_prop;
550550

@@ -586,7 +586,7 @@ static struct hid_device_info *create_device_info_with_usage(IOHIDDeviceRef dev,
586586
so for (max) "path" string 'DevSrvsID:18446744073709551615' we would need
587587
9+1+20+1=31 bytes buffer, but allocate 32 for simple alignment */
588588
const size_t path_len = 32;
589-
cur_dev->path = calloc(1, path_len);
589+
cur_dev->path = (char *) calloc(1, path_len);
590590
if (cur_dev->path != NULL) {
591591
snprintf(cur_dev->path, path_len, "DevSrvsID:%llu", entry_id);
592592
}

netbsd/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ add_library(hidapi_netbsd
22
${HIDAPI_PUBLIC_HEADERS}
33
hid.c
44
)
5+
if(HIDAPI_BUILD_AS_CXX)
6+
set_source_files_properties(hid.c PROPERTIES LANGUAGE CXX)
7+
endif()
58
target_link_libraries(hidapi_netbsd PUBLIC hidapi_include)
69

710
find_package(Threads REQUIRED)

src/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ endif()
3838
if(NOT DEFINED CMAKE_POSITION_INDEPENDENT_CODE)
3939
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
4040
endif()
41+
if(NOT DEFINED HIDAPI_BUILD_AS_CXX)
42+
set(HIDAPI_BUILD_AS_CXX FALSE)
43+
endif()
44+
if(HIDAPI_BUILD_AS_CXX)
45+
enable_language(CXX)
46+
endif()
4147

4248
get_directory_property(IS_EXCLUDE_FROM_ALL EXCLUDE_FROM_ALL)
4349
if(IS_EXCLUDE_FROM_ALL)

windows/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ add_library(hidapi_winapi
1818
${HIDAPI_PUBLIC_HEADERS}
1919
${SOURCES}
2020
)
21+
if(HIDAPI_BUILD_AS_CXX)
22+
set_source_files_properties(hid.c hidapi_descriptor_reconstruct.c PROPERTIES LANGUAGE CXX)
23+
endif()
2124
target_link_libraries(hidapi_winapi
2225
PUBLIC hidapi_include
2326
)

0 commit comments

Comments
 (0)