CMakeLists.txt 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. cmake_minimum_required(VERSION 3.1.3 FATAL_ERROR)
  2. if(NOT CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
  3. add_subdirectory(src)
  4. # compatibility with find_package() vs add_subdirectory
  5. set(hidapi_VERSION "${hidapi_VERSION}" PARENT_SCOPE)
  6. return()
  7. endif()
  8. # All of the below in this file is meant for a standalone build.
  9. # When building as a subdirectory of a larger project, most of the options may not make sense for it,
  10. # so it is up to developer to configure those, e.g.:
  11. #
  12. # # a subfolder of a master project, e.g.: 3rdparty/hidapi/CMakeLists.txt
  13. #
  14. # set(HIDAPI_WITH_HIDRAW OFF)
  15. # set(CMAKE_FRAMEWORK ON)
  16. # # and keep everything else to their defaults
  17. # add_subdirectory(hidapi)
  18. #
  19. set(DEFAULT_CMAKE_BUILD_TYPES "Debug" "Release" "MinSizeRel" "RelWithDebInfo")
  20. if(NOT DEFINED CMAKE_BUILD_TYPE OR NOT CMAKE_BUILD_TYPE)
  21. set(CMAKE_BUILD_TYPE "Release" CACHE STRING "${DEFAULT_CMAKE_BUILD_TYPES}" FORCE)
  22. endif()
  23. # This part is for convenience, when used one of the standard build types with cmake-gui
  24. list(FIND DEFAULT_CMAKE_BUILD_TYPES "${CMAKE_BUILD_TYPE}" _build_type_index)
  25. if(${_build_type_index} GREATER -1)
  26. # set it optionally, so a custom CMAKE_BUILD_TYPE can be used as well, if needed
  27. set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS ${DEFAULT_CMAKE_BUILD_TYPES})
  28. endif()
  29. unset(_build_type_index)
  30. #
  31. project(hidapi LANGUAGES C)
  32. if(APPLE)
  33. if(NOT CMAKE_VERSION VERSION_LESS "3.15")
  34. option(CMAKE_FRAMEWORK "Build macOS/iOS Framework version of the library" OFF)
  35. endif()
  36. elseif(NOT WIN32)
  37. if(CMAKE_SYSTEM_NAME MATCHES "Linux")
  38. option(HIDAPI_WITH_HIDRAW "Build HIDRAW-based implementation of HIDAPI" ON)
  39. option(HIDAPI_WITH_LIBUSB "Build LIBUSB-based implementation of HIDAPI" ON)
  40. endif()
  41. endif()
  42. option(BUILD_SHARED_LIBS "Build shared version of the libraries, otherwise build statically" ON)
  43. set(HIDAPI_INSTALL_TARGETS ON)
  44. set(HIDAPI_PRINT_VERSION ON)
  45. set(IS_DEBUG_BUILD OFF)
  46. if(CMAKE_BUILD_TYPE STREQUAL "Debug")
  47. set(IS_DEBUG_BUILD ON)
  48. endif()
  49. option(HIDAPI_ENABLE_ASAN "Build HIDAPI with ASAN address sanitizer instrumentation" OFF)
  50. if(HIDAPI_ENABLE_ASAN)
  51. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address")
  52. if(MSVC)
  53. # the default is to have "/INCREMENTAL" which causes a warning when "-fsanitize=address" is present
  54. set(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} /INCREMENTAL:NO")
  55. set(CMAKE_SHARED_LINKER_FLAGS_DEBUG "${CMAKE_SHARED_LINKER_FLAGS_DEBUG} /INCREMENTAL:NO")
  56. set(CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO} /INCREMENTAL:NO")
  57. set(CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO} /INCREMENTAL:NO")
  58. endif()
  59. endif()
  60. if(WIN32)
  61. # so far only Windows has tests
  62. option(HIDAPI_WITH_TESTS "Build HIDAPI (unit-)tests" ${IS_DEBUG_BUILD})
  63. else()
  64. set(HIDAPI_WITH_TESTS OFF)
  65. endif()
  66. if(HIDAPI_WITH_TESTS)
  67. enable_testing()
  68. endif()
  69. if(WIN32)
  70. option(HIDAPI_BUILD_PP_DATA_DUMP "Build small Windows console application pp_data_dump.exe" ${IS_DEBUG_BUILD})
  71. endif()
  72. add_subdirectory(src)
  73. option(HIDAPI_BUILD_HIDTEST "Build small console test application hidtest" ${IS_DEBUG_BUILD})
  74. if(HIDAPI_BUILD_HIDTEST)
  75. add_subdirectory(hidtest)
  76. endif()
  77. if(HIDAPI_ENABLE_ASAN)
  78. if(NOT MSVC)
  79. # MSVC doesn't recognize those options, other compilers - requiring it
  80. foreach(HIDAPI_TARGET hidapi_winapi hidapi_darwin hidapi_hidraw hidapi_libusb hidtest_hidraw hidtest_libusb hidtest)
  81. if(TARGET ${HIDAPI_TARGET})
  82. if(BUILD_SHARED_LIBS)
  83. target_link_options(${HIDAPI_TARGET} PRIVATE -fsanitize=address)
  84. else()
  85. target_link_options(${HIDAPI_TARGET} PUBLIC -fsanitize=address)
  86. endif()
  87. endif()
  88. endforeach()
  89. endif()
  90. endif()