CMakeLists.txt 2.7 KB

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