55dc4096523f88449b8a3c03469629d4216cb0a7
[platform/upstream/cmake.git] / Help / guide / tutorial / Step10 / 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 # control where the static and shared libraries are built so that on windows
11 # we don't need to tinker with the path to run the executable
12 set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}")
13 set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}")
14 set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}")
15
16 option(BUILD_SHARED_LIBS "Build using shared libraries" ON)
17
18 # configure a header file to pass the version number only
19 configure_file(TutorialConfig.h.in TutorialConfig.h)
20
21 # add the MathFunctions library
22 add_subdirectory(MathFunctions)
23
24 # add the executable
25 add_executable(Tutorial tutorial.cxx)
26 target_link_libraries(Tutorial PUBLIC MathFunctions)
27
28 # add the binary tree to the search path for include files
29 # so that we will find TutorialConfig.h
30 target_include_directories(Tutorial PUBLIC
31                            "${PROJECT_BINARY_DIR}"
32                            )
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
40 # enable testing
41 include(CTest)
42
43 # does the application run
44 add_test(NAME Runs COMMAND Tutorial 25)
45
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
52 # define a function to simplify adding tests
53 function(do_test target arg result)
54   add_test(NAME Comp${arg} COMMAND ${target} ${arg})
55   set_tests_properties(Comp${arg}
56     PROPERTIES PASS_REGULAR_EXPRESSION ${result}
57     )
58 endfunction()
59
60 # do a bunch of result based tests
61 do_test(Tutorial 4 "4 is 2")
62 do_test(Tutorial 9 "9 is 3")
63 do_test(Tutorial 5 "5 is 2.236")
64 do_test(Tutorial 7 "7 is 2.645")
65 do_test(Tutorial 25 "25 is 5")
66 do_test(Tutorial -25 "-25 is (-nan|nan|0)")
67 do_test(Tutorial 0.0001 "0.0001 is 0.01")
68
69 include(InstallRequiredSystemLibraries)
70 set(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_SOURCE_DIR}/License.txt")
71 set(CPACK_PACKAGE_VERSION_MAJOR "${Tutorial_VERSION_MAJOR}")
72 set(CPACK_PACKAGE_VERSION_MINOR "${Tutorial_VERSION_MINOR}")
73 set(CPACK_SOURCE_GENERATOR "TGZ")
74 include(CPack)