1044748d9977c8fedb01e21cadfa61e9696be889
[platform/upstream/cmake.git] / Help / guide / tutorial / Step11 / 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 add_library(tutorial_compiler_flags INTERFACE)
7 target_compile_features(tutorial_compiler_flags INTERFACE cxx_std_11)
8
9 # add compiler warning flags just when building this project via
10 # the BUILD_INTERFACE genex
11 set(gcc_like_cxx "$<COMPILE_LANG_AND_ID:CXX,ARMClang,AppleClang,Clang,GNU,LCC>")
12 set(msvc_cxx "$<COMPILE_LANG_AND_ID:CXX,MSVC>")
13 target_compile_options(tutorial_compiler_flags INTERFACE
14   "$<${gcc_like_cxx}:$<BUILD_INTERFACE:-Wall;-Wextra;-Wshadow;-Wformat=2;-Wunused>>"
15   "$<${msvc_cxx}:$<BUILD_INTERFACE:-W3>>"
16 )
17
18 # control where the static and shared libraries are built so that on windows
19 # we don't need to tinker with the path to run the executable
20 set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}")
21 set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}")
22 set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}")
23
24 option(BUILD_SHARED_LIBS "Build using shared libraries" ON)
25
26 # configure a header file to pass the version number only
27 configure_file(TutorialConfig.h.in TutorialConfig.h)
28
29 # add the MathFunctions library
30 add_subdirectory(MathFunctions)
31
32 # add the executable
33 add_executable(Tutorial tutorial.cxx)
34 target_link_libraries(Tutorial PUBLIC MathFunctions)
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 enable_testing()
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")
76
77 include(InstallRequiredSystemLibraries)
78 set(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_SOURCE_DIR}/License.txt")
79 set(CPACK_PACKAGE_VERSION_MAJOR "${Tutorial_VERSION_MAJOR}")
80 set(CPACK_PACKAGE_VERSION_MINOR "${Tutorial_VERSION_MINOR}")
81 set(CPACK_SOURCE_GENERATOR "TGZ")
82 include(CPack)