CMakeLists.txt 2.2 KB

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