CMakeLists.txt 3.4 KB

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