6bae26e42b8d0bb4a4571aa7a239139dca0f2414
[platform/upstream/cmake.git] / Help / guide / tutorial / Step9 / CMakeLists.txt
1 cmake_minimum_required(VERSION 3.10)
2
3 # set the project name and version
4 project(Tutorial VERSION 1.0)
5
6 # specify the C++ standard
7 set(CMAKE_CXX_STANDARD 11)
8 set(CMAKE_CXX_STANDARD_REQUIRED True)
9
10 # should we use our own math functions
11 option(USE_MYMATH "Use tutorial provided math implementation" ON)
12
13 # configure a header file to pass some of the CMake settings
14 # to the source code
15 configure_file(TutorialConfig.h.in TutorialConfig.h)
16
17 # add the MathFunctions library
18 if(USE_MYMATH)
19   add_subdirectory(MathFunctions)
20   list(APPEND EXTRA_LIBS MathFunctions)
21 endif()
22
23 # add the executable
24 add_executable(Tutorial tutorial.cxx)
25 target_link_libraries(Tutorial PUBLIC ${EXTRA_LIBS})
26
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
33 # add the install targets
34 install(TARGETS Tutorial DESTINATION bin)
35 install(FILES "${PROJECT_BINARY_DIR}/TutorialConfig.h"
36   DESTINATION include
37   )
38
39 # enable testing
40 include(CTest)
41
42 # does the application run
43 add_test(NAME Runs COMMAND Tutorial 25)
44
45 # does the usage message work?
46 add_test(NAME Usage COMMAND Tutorial)
47 set_tests_properties(Usage
48   PROPERTIES PASS_REGULAR_EXPRESSION "Usage:.*number"
49   )
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()
58
59 # do a bunch of result based tests
60 do_test(Tutorial 4 "4 is 2")
61 do_test(Tutorial 9 "9 is 3")
62 do_test(Tutorial 5 "5 is 2.236")
63 do_test(Tutorial 7 "7 is 2.645")
64 do_test(Tutorial 25 "25 is 5")
65 do_test(Tutorial -25 "-25 is (-nan|nan|0)")
66 do_test(Tutorial 0.0001 "0.0001 is 0.01")
67
68 include(InstallRequiredSystemLibraries)
69 set(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_SOURCE_DIR}/License.txt")
70 set(CPACK_PACKAGE_VERSION_MAJOR "${Tutorial_VERSION_MAJOR}")
71 set(CPACK_PACKAGE_VERSION_MINOR "${Tutorial_VERSION_MINOR}")
72 set(CPACK_SOURCE_GENERATOR "TGZ")
73 include(CPack)