CMakeLists.txt 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. cmake_minimum_required(VERSION 3.15)
  2. # set the project name and version
  3. project(Tutorial VERSION 1.0)
  4. # specify the C++ standard
  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. # configure a header file to pass the version number only
  22. configure_file(TutorialConfig.h.in TutorialConfig.h)
  23. # add the MathFunctions library
  24. add_subdirectory(MathFunctions)
  25. # add the executable
  26. add_executable(Tutorial tutorial.cxx)
  27. target_link_libraries(Tutorial PUBLIC MathFunctions tutorial_compiler_flags)
  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. include(CTest)
  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()
  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. # setup installer
  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. set(CPACK_GENERATOR "TGZ")
  68. set(CPACK_SOURCE_GENERATOR "TGZ")
  69. include(CPack)