CMakeLists.txt 4.0 KB

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