CMakeLists.txt 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. cmake_minimum_required(VERSION 3.15)
  2. project(Tutorial)
  3. add_library(tutorial_compiler_flags INTERFACE)
  4. target_compile_features(tutorial_compiler_flags INTERFACE cxx_std_11)
  5. # add compiler warning flags just when building this project via
  6. # the BUILD_INTERFACE genex
  7. set(gcc_like_cxx "$<COMPILE_LANG_AND_ID:CXX,ARMClang,AppleClang,Clang,GNU>")
  8. set(msvc_cxx "$<COMPILE_LANG_AND_ID:CXX,MSVC>")
  9. target_compile_options(tutorial_compiler_flags INTERFACE
  10. "$<${gcc_like_cxx}:$<BUILD_INTERFACE:-Wall;-Wextra;-Wshadow;-Wformat=2;-Wunused>>"
  11. "$<${msvc_cxx}:$<BUILD_INTERFACE:-W3>>"
  12. )
  13. # set the version number
  14. set(Tutorial_VERSION_MAJOR 1)
  15. set(Tutorial_VERSION_MINOR 0)
  16. # control where the static and shared libraries are built so that on windows
  17. # we don't need to tinker with the path to run the executable
  18. set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}")
  19. set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}")
  20. set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}")
  21. option(BUILD_SHARED_LIBS "Build using shared libraries" ON)
  22. if(APPLE)
  23. set(CMAKE_INSTALL_RPATH "@executable_path/../lib")
  24. elseif(UNIX)
  25. set(CMAKE_INSTALL_RPATH "$ORIGIN/../lib")
  26. endif()
  27. # configure a header file to pass the version number only
  28. configure_file(
  29. "${PROJECT_SOURCE_DIR}/TutorialConfig.h.in"
  30. "${PROJECT_BINARY_DIR}/TutorialConfig.h"
  31. )
  32. # add the MathFunctions library
  33. add_subdirectory(MathFunctions)
  34. # add the executable
  35. add_executable(Tutorial tutorial.cxx)
  36. target_link_libraries(Tutorial MathFunctions)
  37. # add the binary tree to the search path for include files
  38. # so that we will find TutorialConfig.h
  39. target_include_directories(Tutorial PUBLIC
  40. "${PROJECT_BINARY_DIR}"
  41. )
  42. # add the install targets
  43. install(TARGETS Tutorial DESTINATION bin)
  44. install(FILES "${PROJECT_BINARY_DIR}/TutorialConfig.h"
  45. DESTINATION include
  46. )
  47. # enable testing
  48. enable_testing()
  49. # does the application run
  50. add_test(NAME Runs COMMAND Tutorial 25)
  51. # does the usage message work?
  52. add_test(NAME Usage COMMAND Tutorial)
  53. set_tests_properties(Usage
  54. PROPERTIES PASS_REGULAR_EXPRESSION "Usage:.*number"
  55. )
  56. # define a function to simplify adding tests
  57. function(do_test target arg result)
  58. add_test(NAME Comp${arg} COMMAND ${target} ${arg})
  59. set_tests_properties(Comp${arg}
  60. PROPERTIES PASS_REGULAR_EXPRESSION ${result}
  61. )
  62. endfunction(do_test)
  63. # do a bunch of result based tests
  64. do_test(Tutorial 4 "4 is 2")
  65. do_test(Tutorial 9 "9 is 3")
  66. do_test(Tutorial 5 "5 is 2.236")
  67. do_test(Tutorial 7 "7 is 2.645")
  68. do_test(Tutorial 25 "25 is 5")
  69. do_test(Tutorial -25 "-25 is [-nan|nan|0]")
  70. do_test(Tutorial 0.0001 "0.0001 is 0.01")
  71. include(InstallRequiredSystemLibraries)
  72. set(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_SOURCE_DIR}/License.txt")
  73. set(CPACK_PACKAGE_VERSION_MAJOR "${Tutorial_VERSION_MAJOR}")
  74. set(CPACK_PACKAGE_VERSION_MINOR "${Tutorial_VERSION_MINOR}")
  75. include(CPack)
  76. # install the configuration targets
  77. install(EXPORT MathFunctionsTargets
  78. FILE MathFunctionsTargets.cmake
  79. DESTINATION lib/cmake/MathFunctions
  80. )
  81. include(CMakePackageConfigHelpers)
  82. # generate the config file that is includes the exports
  83. configure_package_config_file(${CMAKE_CURRENT_SOURCE_DIR}/Config.cmake.in
  84. "${CMAKE_CURRENT_BINARY_DIR}/MathFunctionsConfig.cmake"
  85. INSTALL_DESTINATION "lib/cmake/example"
  86. NO_SET_AND_CHECK_MACRO
  87. NO_CHECK_REQUIRED_COMPONENTS_MACRO
  88. )
  89. # generate the version file for the config file
  90. write_basic_package_version_file(
  91. "${CMAKE_CURRENT_BINARY_DIR}/MathFunctionsConfigVersion.cmake"
  92. VERSION "${Tutorial_VERSION_MAJOR}.${Tutorial_VERSION_MINOR}"
  93. COMPATIBILITY AnyNewerVersion
  94. )
  95. # install the configuration file
  96. install(FILES
  97. ${CMAKE_CURRENT_BINARY_DIR}/MathFunctionsConfig.cmake
  98. DESTINATION lib/cmake/MathFunctions
  99. )
  100. # generate the export targets for the build tree
  101. # needs to be after the install(TARGETS ) command
  102. export(EXPORT MathFunctionsTargets
  103. FILE "${CMAKE_CURRENT_BINARY_DIR}/MathFunctionsTargets.cmake"
  104. )