CMakeLists.txt 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. # configure a header file to pass the version number only
  16. configure_file(TutorialConfig.h.in TutorialConfig.h)
  17. # add the MathFunctions library
  18. add_subdirectory(MathFunctions)
  19. # add the executable
  20. add_executable(Tutorial tutorial.cxx)
  21. target_link_libraries(Tutorial PUBLIC MathFunctions tutorial_compiler_flags)
  22. # add the binary tree to the search path for include files
  23. # so that we will find TutorialConfig.h
  24. target_include_directories(Tutorial PUBLIC
  25. "${PROJECT_BINARY_DIR}"
  26. )
  27. # add the install targets
  28. install(TARGETS Tutorial DESTINATION bin)
  29. install(FILES "${PROJECT_BINARY_DIR}/TutorialConfig.h"
  30. DESTINATION include
  31. )
  32. # enable testing
  33. include(CTest)
  34. # does the application run
  35. add_test(NAME Runs COMMAND Tutorial 25)
  36. # does the usage message work?
  37. add_test(NAME Usage COMMAND Tutorial)
  38. set_tests_properties(Usage
  39. PROPERTIES PASS_REGULAR_EXPRESSION "Usage:.*number"
  40. )
  41. # define a function to simplify adding tests
  42. function(do_test target arg result)
  43. add_test(NAME Comp${arg} COMMAND ${target} ${arg})
  44. set_tests_properties(Comp${arg}
  45. PROPERTIES PASS_REGULAR_EXPRESSION ${result}
  46. )
  47. endfunction()
  48. # do a bunch of result based tests
  49. do_test(Tutorial 4 "4 is 2")
  50. do_test(Tutorial 9 "9 is 3")
  51. do_test(Tutorial 5 "5 is 2.236")
  52. do_test(Tutorial 7 "7 is 2.645")
  53. do_test(Tutorial 25 "25 is 5")
  54. do_test(Tutorial -25 "-25 is (-nan|nan|0)")
  55. do_test(Tutorial 0.0001 "0.0001 is 0.01")
  56. # setup installer
  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. set(CPACK_GENERATOR "TGZ")
  62. set(CPACK_SOURCE_GENERATOR "TGZ")
  63. include(CPack)