CMakeLists.txt 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. cmake_minimum_required(VERSION 3.10)
  2. # set the project name and version
  3. project(Tutorial VERSION 1.0)
  4. # specify the C++ standard
  5. set(CMAKE_CXX_STANDARD 11)
  6. set(CMAKE_CXX_STANDARD_REQUIRED True)
  7. # does this system provide the log and exp functions?
  8. include(CheckSymbolExists)
  9. set(CMAKE_REQUIRED_LIBRARIES "m")
  10. check_symbol_exists(log "math.h" HAVE_LOG)
  11. check_symbol_exists(exp "math.h" HAVE_EXP)
  12. # should we use our own math functions
  13. option(USE_MYMATH "Use tutorial provided math implementation" ON)
  14. # configure a header file to pass some of the CMake settings
  15. # to the source code
  16. configure_file(TutorialConfig.h.in TutorialConfig.h)
  17. # add the MathFunctions library
  18. if(USE_MYMATH)
  19. add_subdirectory(MathFunctions)
  20. list(APPEND EXTRA_LIBS MathFunctions)
  21. endif()
  22. # add the executable
  23. add_executable(Tutorial tutorial.cxx)
  24. target_link_libraries(Tutorial PUBLIC ${EXTRA_LIBS})
  25. # add the binary tree to the search path for include files
  26. # so that we will find TutorialConfig.h
  27. target_include_directories(Tutorial PUBLIC
  28. "${PROJECT_BINARY_DIR}"
  29. )
  30. # add the install targets
  31. install(TARGETS Tutorial DESTINATION bin)
  32. install(FILES "${PROJECT_BINARY_DIR}/TutorialConfig.h"
  33. DESTINATION include
  34. )
  35. # enable testing
  36. enable_testing()
  37. # does the application run
  38. add_test(NAME Runs COMMAND Tutorial 25)
  39. # does the usage message work?
  40. add_test(NAME Usage COMMAND Tutorial)
  41. set_tests_properties(Usage
  42. PROPERTIES PASS_REGULAR_EXPRESSION "Usage:.*number"
  43. )
  44. # define a function to simplify adding tests
  45. function(do_test target arg result)
  46. add_test(NAME Comp${arg} COMMAND ${target} ${arg})
  47. set_tests_properties(Comp${arg}
  48. PROPERTIES PASS_REGULAR_EXPRESSION ${result}
  49. )
  50. endfunction(do_test)
  51. # do a bunch of result based tests
  52. do_test(Tutorial 4 "4 is 2")
  53. do_test(Tutorial 9 "9 is 3")
  54. do_test(Tutorial 5 "5 is 2.236")
  55. do_test(Tutorial 7 "7 is 2.645")
  56. do_test(Tutorial 25 "25 is 5")
  57. do_test(Tutorial -25 "-25 is [-nan|nan|0]")
  58. do_test(Tutorial 0.0001 "0.0001 is 0.01")
  59. # setup installer
  60. include(InstallRequiredSystemLibraries)
  61. set(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_SOURCE_DIR}/License.txt")
  62. set(CPACK_PACKAGE_VERSION_MAJOR "${Tutorial_VERSION_MAJOR}")
  63. set(CPACK_PACKAGE_VERSION_MINOR "${Tutorial_VERSION_MINOR}")
  64. include(CPack)