CMakeLists.txt 3.4 KB

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