Imported Upstream version 3.25.0
[platform/upstream/cmake.git] / Help / guide / tutorial / Step8 / CMakeLists.txt
1 cmake_minimum_required(VERSION 3.15)
2
3 # set the project name and version
4 project(Tutorial VERSION 1.0)
5
6 # specify the C++ standard
7 add_library(tutorial_compiler_flags INTERFACE)
8 target_compile_features(tutorial_compiler_flags INTERFACE cxx_std_11)
9
10 # add compiler warning flags just when building this project via
11 # the BUILD_INTERFACE genex
12 set(gcc_like_cxx "$<COMPILE_LANG_AND_ID:CXX,ARMClang,AppleClang,Clang,GNU,LCC>")
13 set(msvc_cxx "$<COMPILE_LANG_AND_ID:CXX,MSVC>")
14 target_compile_options(tutorial_compiler_flags INTERFACE
15   "$<${gcc_like_cxx}:$<BUILD_INTERFACE:-Wall;-Wextra;-Wshadow;-Wformat=2;-Wunused>>"
16   "$<${msvc_cxx}:$<BUILD_INTERFACE:-W3>>"
17 )
18
19
20 # should we use our own math functions
21 option(USE_MYMATH "Use tutorial provided math implementation" ON)
22
23 # configure a header file to pass some of the CMake settings
24 # to the source code
25 configure_file(TutorialConfig.h.in TutorialConfig.h)
26
27 # add the MathFunctions library
28 if(USE_MYMATH)
29   add_subdirectory(MathFunctions)
30   list(APPEND EXTRA_LIBS MathFunctions)
31 endif()
32
33 # add the executable
34 add_executable(Tutorial tutorial.cxx)
35 target_link_libraries(Tutorial PUBLIC ${EXTRA_LIBS} tutorial_compiler_flags)
36
37 # add the binary tree to the search path for include files
38 # so that we will find TutorialConfig.h
39 target_include_directories(Tutorial PUBLIC
40                            "${PROJECT_BINARY_DIR}"
41                            )
42
43 # add the install targets
44 install(TARGETS Tutorial DESTINATION bin)
45 install(FILES "${PROJECT_BINARY_DIR}/TutorialConfig.h"
46   DESTINATION include
47   )
48
49 # enable testing
50 include(CTest)
51
52 # does the application run
53 add_test(NAME Runs COMMAND Tutorial 25)
54
55 # does the usage message work?
56 add_test(NAME Usage COMMAND Tutorial)
57 set_tests_properties(Usage
58   PROPERTIES PASS_REGULAR_EXPRESSION "Usage:.*number"
59   )
60
61 # define a function to simplify adding tests
62 function(do_test target arg result)
63   add_test(NAME Comp${arg} COMMAND ${target} ${arg})
64   set_tests_properties(Comp${arg}
65     PROPERTIES PASS_REGULAR_EXPRESSION ${result}
66     )
67 endfunction()
68
69 # do a bunch of result based tests
70 do_test(Tutorial 4 "4 is 2")
71 do_test(Tutorial 9 "9 is 3")
72 do_test(Tutorial 5 "5 is 2.236")
73 do_test(Tutorial 7 "7 is 2.645")
74 do_test(Tutorial 25 "25 is 5")
75 do_test(Tutorial -25 "-25 is (-nan|nan|0)")
76 do_test(Tutorial 0.0001 "0.0001 is 0.01")