Imported Upstream version 3.25.0
[platform/upstream/cmake.git] / Help / guide / tutorial / Step9 / 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 # should we use our own math functions
20 option(USE_MYMATH "Use tutorial provided math implementation" ON)
21
22 # configure a header file to pass some of the CMake settings
23 # to the source code
24 configure_file(TutorialConfig.h.in TutorialConfig.h)
25
26 # add the MathFunctions library
27 if(USE_MYMATH)
28   add_subdirectory(MathFunctions)
29   list(APPEND EXTRA_LIBS MathFunctions)
30 endif()
31
32 # add the executable
33 add_executable(Tutorial tutorial.cxx)
34 target_link_libraries(Tutorial PUBLIC ${EXTRA_LIBS} tutorial_compiler_flags)
35
36 # add the binary tree to the search path for include files
37 # so that we will find TutorialConfig.h
38 target_include_directories(Tutorial PUBLIC
39                            "${PROJECT_BINARY_DIR}"
40                            )
41
42 # add the install targets
43 install(TARGETS Tutorial DESTINATION bin)
44 install(FILES "${PROJECT_BINARY_DIR}/TutorialConfig.h"
45   DESTINATION include
46   )
47
48 # enable testing
49 include(CTest)
50
51 # does the application run
52 add_test(NAME Runs COMMAND Tutorial 25)
53
54 # does the usage message work?
55 add_test(NAME Usage COMMAND Tutorial)
56 set_tests_properties(Usage
57   PROPERTIES PASS_REGULAR_EXPRESSION "Usage:.*number"
58   )
59
60 # define a function to simplify adding tests
61 function(do_test target arg result)
62   add_test(NAME Comp${arg} COMMAND ${target} ${arg})
63   set_tests_properties(Comp${arg}
64     PROPERTIES PASS_REGULAR_EXPRESSION ${result}
65     )
66 endfunction()
67
68 # do a bunch of result based tests
69 do_test(Tutorial 4 "4 is 2")
70 do_test(Tutorial 9 "9 is 3")
71 do_test(Tutorial 5 "5 is 2.236")
72 do_test(Tutorial 7 "7 is 2.645")
73 do_test(Tutorial 25 "25 is 5")
74 do_test(Tutorial -25 "-25 is (-nan|nan|0)")
75 do_test(Tutorial 0.0001 "0.0001 is 0.01")