CMakeLists.txt 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. cmake_minimum_required(VERSION 3.15)
  2. # set the project name and version
  3. project(Tutorial VERSION 1.0)
  4. add_library(tutorial_compiler_flags INTERFACE)
  5. target_compile_features(tutorial_compiler_flags INTERFACE cxx_std_11)
  6. # add compiler warning flags just when building this project via
  7. # the BUILD_INTERFACE genex
  8. set(gcc_like_cxx "$<COMPILE_LANG_AND_ID:CXX,ARMClang,AppleClang,Clang,GNU>")
  9. set(msvc_cxx "$<COMPILE_LANG_AND_ID:CXX,MSVC>")
  10. target_compile_options(tutorial_compiler_flags INTERFACE
  11. "$<${gcc_like_cxx}:$<BUILD_INTERFACE:-Wall;-Wextra;-Wshadow;-Wformat=2;-Wunused>>"
  12. "$<${msvc_cxx}:$<BUILD_INTERFACE:-W3>>"
  13. )
  14. # control where the static and shared libraries are built so that on windows
  15. # we don't need to tinker with the path to run the executable
  16. set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}")
  17. set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}")
  18. set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}")
  19. option(BUILD_SHARED_LIBS "Build using shared libraries" ON)
  20. # configure a header file to pass the version number only
  21. configure_file(TutorialConfig.h.in TutorialConfig.h)
  22. # add the MathFunctions library
  23. add_subdirectory(MathFunctions)
  24. # add the executable
  25. add_executable(Tutorial tutorial.cxx)
  26. target_link_libraries(Tutorial MathFunctions)
  27. # add the binary tree to the search path for include files
  28. # so that we will find TutorialConfig.h
  29. target_include_directories(Tutorial PUBLIC
  30. "${PROJECT_BINARY_DIR}"
  31. )
  32. # add the install targets
  33. install(TARGETS Tutorial DESTINATION bin)
  34. install(FILES "${PROJECT_BINARY_DIR}/TutorialConfig.h"
  35. DESTINATION include
  36. )
  37. # enable testing
  38. enable_testing()
  39. # does the application run
  40. add_test(NAME Runs COMMAND Tutorial 25)
  41. # does the usage message work?
  42. add_test(NAME Usage COMMAND Tutorial)
  43. set_tests_properties(Usage
  44. PROPERTIES PASS_REGULAR_EXPRESSION "Usage:.*number"
  45. )
  46. # define a function to simplify adding tests
  47. function(do_test target arg result)
  48. add_test(NAME Comp${arg} COMMAND ${target} ${arg})
  49. set_tests_properties(Comp${arg}
  50. PROPERTIES PASS_REGULAR_EXPRESSION ${result}
  51. )
  52. endfunction(do_test)
  53. # do a bunch of result based tests
  54. do_test(Tutorial 4 "4 is 2")
  55. do_test(Tutorial 9 "9 is 3")
  56. do_test(Tutorial 5 "5 is 2.236")
  57. do_test(Tutorial 7 "7 is 2.645")
  58. do_test(Tutorial 25 "25 is 5")
  59. do_test(Tutorial -25 "-25 is [-nan|nan|0]")
  60. do_test(Tutorial 0.0001 "0.0001 is 0.01")
  61. include(InstallRequiredSystemLibraries)
  62. set(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_SOURCE_DIR}/License.txt")
  63. set(CPACK_PACKAGE_VERSION_MAJOR "${Tutorial_VERSION_MAJOR}")
  64. set(CPACK_PACKAGE_VERSION_MINOR "${Tutorial_VERSION_MINOR}")
  65. include(CPack)