CMakeLists.txt 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. cmake_minimum_required(VERSION 3.10)
  2. project(Cbox)
  3. # define options
  4. option(ASAN "Enable AddressSanitizer" OFF)
  5. option(COVERAGE "Enable code coverage" OFF)
  6. option(STRICT_ERROR "Treats all compiler warnings as errors." OFF)
  7. option(CI_BUILD "Configure GitHub CI compilation." OFF)
  8. # basic configuration
  9. set(CMAKE_CXX_STANDARD 11)
  10. set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
  11. set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
  12. set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
  13. set_property(GLOBAL PROPERTY USE_FOLDERS ON)
  14. if (NOT CI_BUILD)
  15. configure_file(CMakeLists.txt.in googletest-download/CMakeLists.txt)
  16. execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
  17. RESULT_VARIABLE result
  18. WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download )
  19. if(result)
  20. message(FATAL_ERROR "CMake step for googletest failed: ${result}")
  21. endif()
  22. execute_process(COMMAND ${CMAKE_COMMAND} --build .
  23. RESULT_VARIABLE result
  24. WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download )
  25. if(result)
  26. message(FATAL_ERROR "Build step for googletest failed: ${result}")
  27. endif()
  28. # Prevent overriding the parent project's compiler/linker
  29. # settings on Windows
  30. set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
  31. # Add googletest directly to our build. This defines
  32. # the gtest and gtest_main targets.
  33. add_subdirectory(${CMAKE_CURRENT_BINARY_DIR}/googletest-src
  34. ${CMAKE_CURRENT_BINARY_DIR}/googletest-build
  35. EXCLUDE_FROM_ALL)
  36. # The gtest/gtest_main targets carry header search path
  37. # dependencies automatically when using CMake 2.8.11 or
  38. # later. Otherwise we have to add them here ourselves.
  39. if (CMAKE_VERSION VERSION_LESS 2.8.11)
  40. include_directories("${gtest_SOURCE_DIR}/include")
  41. endif()
  42. endif()
  43. if (STRICT_ERROR)
  44. if (MSVC)
  45. add_compile_options("/WX")
  46. else ()
  47. add_compile_options("-Werror")
  48. endif ()
  49. endif ()
  50. if (ASAN)
  51. message(STATUS "Enable AddressSanitizer.")
  52. if (MSVC)
  53. add_compile_options("/fsanitize=address")
  54. else ()
  55. add_compile_options("-fno-omit-frame-pointer" "-fsanitize=address" "-fsanitize=undefined")
  56. add_link_options("-fno-omit-frame-pointer" "-fsanitize=address" "-fsanitize=undefined")
  57. endif ()
  58. endif ()
  59. if (COVERAGE)
  60. message(STATUS "Enable code coverage.")
  61. if (${CMAKE_CXX_COMPILER_ID} STREQUAL "Clang")
  62. add_compile_options("-fprofile-instr-generate" "-fcoverage-mapping")
  63. add_link_options("-fprofile-instr-generate" "-fcoverage-mapping")
  64. elseif (${CMAKE_CXX_COMPILER_ID} STREQUAL "GNU")
  65. add_compile_options("--coverage")
  66. add_link_options("--coverage")
  67. else ()
  68. message(FATAL_ERROR "${CMAKE_CXX_COMPILER_ID} does not support coverage analysis.")
  69. endif ()
  70. endif ()
  71. # compiler options
  72. if (MSVC)
  73. add_compile_options("/MP")
  74. endif ()
  75. if (${CMAKE_CXX_COMPILER_ID} STREQUAL "Clang" OR ${CMAKE_CXX_COMPILER_ID} STREQUAL "GNU")
  76. add_compile_options("-Wall" "-Wextra")
  77. endif ()
  78. # build target
  79. add_executable (Cbox-test)
  80. # add include path
  81. include_directories(include)
  82. # add source file
  83. add_subdirectory(src)
  84. add_subdirectory(test)
  85. if (CI_BUILD)
  86. # Now simply link against gtest_main or gtest_main as needed.
  87. TARGET_LINK_LIBRARIES(Cbox-test gtest pthread)
  88. else ()
  89. TARGET_LINK_LIBRARIES(Cbox-test gtest_main)
  90. endif ()
  91. enable_testing()