CMakeLists.txt 3.4 KB

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