1
0

CMakeLists.txt 3.3 KB

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