Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 0 additions & 26 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,6 @@ endif()

option(HIDAPI_ENABLE_ASAN "Build HIDAPI with ASAN address sanitizer instrumentation" OFF)

if(HIDAPI_ENABLE_ASAN)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address")
if(MSVC)
# the default is to have "/INCREMENTAL" which causes a warning when "-fsanitize=address" is present
set(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} /INCREMENTAL:NO")
set(CMAKE_SHARED_LINKER_FLAGS_DEBUG "${CMAKE_SHARED_LINKER_FLAGS_DEBUG} /INCREMENTAL:NO")
set(CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO} /INCREMENTAL:NO")
set(CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO} /INCREMENTAL:NO")
endif()
endif()

if(WIN32)
option(HIDAPI_WITH_TESTS "Build HIDAPI (unit-)tests" ${IS_DEBUG_BUILD})
elseif(CMAKE_SYSTEM_NAME MATCHES "Linux" OR APPLE)
Expand Down Expand Up @@ -105,18 +94,3 @@ endif()
if(HIDAPI_WITH_TESTS AND (WIN32 OR CMAKE_SYSTEM_NAME MATCHES "Linux" OR APPLE))
add_subdirectory(src/tests)
endif()

if(HIDAPI_ENABLE_ASAN)
if(NOT MSVC)
# MSVC doesn't recognize those options, other compilers - requiring it
foreach(HIDAPI_TARGET hidapi_winapi hidapi_darwin hidapi_hidraw hidapi_libusb hidtest_hidraw hidtest_libusb hidtest)
if(TARGET ${HIDAPI_TARGET})
if(BUILD_SHARED_LIBS)
target_link_options(${HIDAPI_TARGET} PRIVATE -fsanitize=address)
else()
target_link_options(${HIDAPI_TARGET} PUBLIC -fsanitize=address)
endif()
endif()
endforeach()
endif()
endif()
1 change: 1 addition & 0 deletions freebsd/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ add_library(hidapi_hidraw
${HIDAPI_PUBLIC_HEADERS}
hid.c
)
hidapi_configure_sanitizers(hidapi_hidraw)
if(HIDAPI_BUILD_AS_CXX)
set_source_files_properties(hid.c PROPERTIES LANGUAGE CXX)
endif()
Expand Down
4 changes: 4 additions & 0 deletions hidtest/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
cmake_minimum_required(VERSION 3.1.3...4.3 FATAL_ERROR)
project(hidtest C)
include("${CMAKE_CURRENT_LIST_DIR}/../src/cmake/hidapi-sanitizers.cmake")

if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
# hidtest is build as a standalone project
Expand All @@ -20,17 +21,20 @@ set(HIDAPI_HIDTEST_TARGETS)
if(NOT WIN32 AND NOT APPLE AND (CMAKE_SYSTEM_NAME MATCHES "Linux" OR CMAKE_SYSTEM_NAME MATCHES "FreeBSD"))
if(TARGET hidapi::hidraw)
add_executable(hidtest_hidraw test.c)
hidapi_configure_sanitizers(hidtest_hidraw)
target_link_libraries(hidtest_hidraw hidapi::hidraw)
list(APPEND HIDAPI_HIDTEST_TARGETS hidtest_hidraw)
endif()
if(TARGET hidapi::libusb)
add_executable(hidtest_libusb test.c)
hidapi_configure_sanitizers(hidtest_libusb)
target_compile_definitions(hidtest_libusb PRIVATE USING_HIDAPI_LIBUSB)
target_link_libraries(hidtest_libusb hidapi::libusb)
list(APPEND HIDAPI_HIDTEST_TARGETS hidtest_libusb)
endif()
else()
add_executable(hidtest test.c)
hidapi_configure_sanitizers(hidtest)
target_link_libraries(hidtest hidapi::hidapi)
list(APPEND HIDAPI_HIDTEST_TARGETS hidtest)
endif()
Expand Down
1 change: 1 addition & 0 deletions libusb/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ add_library(hidapi_libusb
hidapi_libusb_report_descriptor.h
hid.c
)
hidapi_configure_sanitizers(hidapi_libusb)
if(HIDAPI_BUILD_AS_CXX)
set_source_files_properties(hid.c PROPERTIES LANGUAGE CXX)
endif()
Expand Down
1 change: 1 addition & 0 deletions libusb/test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
add_executable(max_input_report_size_test max_input_report_size_test.c)
hidapi_configure_sanitizers(max_input_report_size_test)
set_target_properties(max_input_report_size_test
PROPERTIES
C_STANDARD 11
Expand Down
1 change: 1 addition & 0 deletions linux/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ add_library(hidapi_hidraw
${HIDAPI_PUBLIC_HEADERS}
hid.c
)
hidapi_configure_sanitizers(hidapi_hidraw)
if(HIDAPI_BUILD_AS_CXX)
set_source_files_properties(hid.c PROPERTIES LANGUAGE CXX)
endif()
Expand Down
1 change: 1 addition & 0 deletions mac/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ add_library(hidapi_darwin
${HIDAPI_PUBLIC_HEADERS}
hid.c
)
hidapi_configure_sanitizers(hidapi_darwin)
if(HIDAPI_BUILD_AS_CXX)
set_source_files_properties(hid.c PROPERTIES LANGUAGE CXX)
endif()
Expand Down
1 change: 1 addition & 0 deletions netbsd/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ add_library(hidapi_netbsd
${HIDAPI_PUBLIC_HEADERS}
hid.c
)
hidapi_configure_sanitizers(hidapi_netbsd)
if(HIDAPI_BUILD_AS_CXX)
set_source_files_properties(hid.c PROPERTIES LANGUAGE CXX)
endif()
Expand Down
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
get_filename_component(PROJECT_ROOT "${CMAKE_CURRENT_LIST_DIR}/.." ABSOLUTE)
include("${CMAKE_CURRENT_LIST_DIR}/cmake/hidapi-sanitizers.cmake")

# Read version from file
file(READ "${PROJECT_ROOT}/VERSION" RAW_VERSION_STR)
Expand Down
30 changes: 30 additions & 0 deletions src/cmake/hidapi-sanitizers.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Apply HIDAPI_ENABLE_ASAN to a library or executable target.
# Call it where the target is defined. It instruments C and C++ sources and,
# for an instrumented static library, propagates the sanitizer runtime to
# consumers (including installed packages).
function(hidapi_configure_sanitizers target)
if(NOT HIDAPI_ENABLE_ASAN)
return()
endif()

target_compile_options(${target} PRIVATE -fsanitize=address)
if(MSVC)
# The default "/INCREMENTAL" causes a warning when "-fsanitize=address" is present.
set_property(TARGET ${target} APPEND_STRING PROPERTY LINK_FLAGS_DEBUG " /INCREMENTAL:NO")
set_property(TARGET ${target} APPEND_STRING PROPERTY LINK_FLAGS_RELWITHDEBINFO " /INCREMENTAL:NO")
else()
get_target_property(target_type ${target} TYPE)
if(target_type STREQUAL "STATIC_LIBRARY")
# Consumers of an instrumented static library need the runtime.
if(CMAKE_VERSION VERSION_LESS "3.13")
set_property(TARGET ${target} APPEND PROPERTY INTERFACE_LINK_LIBRARIES -fsanitize=address)
else()
target_link_options(${target} INTERFACE -fsanitize=address)
endif()
elseif(CMAKE_VERSION VERSION_LESS "3.13")
set_property(TARGET ${target} APPEND_STRING PROPERTY LINK_FLAGS " -fsanitize=address")
else()
target_link_options(${target} PRIVATE -fsanitize=address)
endif()
endif()
endfunction()
4 changes: 1 addition & 3 deletions src/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,12 @@ find_package(Threads REQUIRED)
# via return code 77 when no virtual device is present).
function(hidapi_add_vdev_test name provider backend)
add_executable(${name} test_device_io.c ${provider})
hidapi_configure_sanitizers(${name})
set_target_properties(${name} PROPERTIES
C_STANDARD 11
C_STANDARD_REQUIRED TRUE
)
target_link_libraries(${name} PRIVATE ${backend} Threads::Threads)
if(HIDAPI_ENABLE_ASAN AND NOT MSVC)
target_link_options(${name} PRIVATE -fsanitize=address)
endif()
add_test(NAME ${name} COMMAND ${name})
set_tests_properties(${name} PROPERTIES
SKIP_RETURN_CODE 77
Expand Down
1 change: 1 addition & 0 deletions windows/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ add_library(hidapi_winapi
${HIDAPI_PUBLIC_HEADERS}
${SOURCES}
)
hidapi_configure_sanitizers(hidapi_winapi)
if(HIDAPI_BUILD_AS_CXX)
set_source_files_properties(hid.c hidapi_descriptor_reconstruct.c PROPERTIES LANGUAGE CXX)
endif()
Expand Down
1 change: 1 addition & 0 deletions windows/pp_data_dump/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
project(pp_data_dump C)

add_executable(pp_data_dump pp_data_dump.c)
hidapi_configure_sanitizers(pp_data_dump)
set_target_properties(pp_data_dump
PROPERTIES
C_STANDARD 11
Expand Down
1 change: 1 addition & 0 deletions windows/test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
add_executable(hid_report_reconstructor_test hid_report_reconstructor_test.c)
hidapi_configure_sanitizers(hid_report_reconstructor_test)
set_target_properties(hid_report_reconstructor_test
PROPERTIES
C_STANDARD 11
Expand Down
Loading