CMakeLists.txt 3.8 KB

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