CMakeLists.txt 2.3 KB

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