41baf64840bd1db00d9d62c7057443b223fc3d21
[platform/upstream/cmake.git] / Help / guide / tutorial / Complete / 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 set(CMAKE_DEBUG_POSTFIX d)
7
8 add_library(tutorial_compiler_flags INTERFACE)
9 target_compile_features(tutorial_compiler_flags INTERFACE cxx_std_11)
10
11 # add compiler warning flags just when building this project via
12 # the BUILD_INTERFACE genex
13 set(gcc_like_cxx "$<COMPILE_LANG_AND_ID:CXX,ARMClang,AppleClang,Clang,GNU,LCC>")
14 set(msvc_cxx "$<COMPILE_LANG_AND_ID:CXX,MSVC>")
15 target_compile_options(tutorial_compiler_flags INTERFACE
16   "$<${gcc_like_cxx}:$<BUILD_INTERFACE:-Wall;-Wextra;-Wshadow;-Wformat=2;-Wunused>>"
17   "$<${msvc_cxx}:$<BUILD_INTERFACE:-W3>>"
18 )
19
20 # control where the static and shared libraries are built so that on windows
21 # we don't need to tinker with the path to run the executable
22 set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}")
23 set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}")
24 set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}")
25
26 option(BUILD_SHARED_LIBS "Build using shared libraries" ON)
27
28 if(APPLE)
29   set(CMAKE_INSTALL_RPATH "@executable_path/../lib")
30 elseif(UNIX)
31   set(CMAKE_INSTALL_RPATH "$ORIGIN/../lib")
32 endif()
33
34 # configure a header file to pass the version number only
35 configure_file(TutorialConfig.h.in TutorialConfig.h)
36
37 # add the MathFunctions library
38 add_subdirectory(MathFunctions)
39
40 # add the executable
41 add_executable(Tutorial tutorial.cxx)
42 set_target_properties(Tutorial PROPERTIES DEBUG_POSTFIX ${CMAKE_DEBUG_POSTFIX})
43
44 target_link_libraries(Tutorial PUBLIC MathFunctions)
45
46 # add the binary tree to the search path for include files
47 # so that we will find TutorialConfig.h
48 target_include_directories(Tutorial PUBLIC
49                            "${PROJECT_BINARY_DIR}"
50                            )
51
52 # add the install targets
53 install(TARGETS Tutorial DESTINATION bin)
54 install(FILES "${PROJECT_BINARY_DIR}/TutorialConfig.h"
55   DESTINATION include
56   )
57
58 # enable testing
59 enable_testing()
60
61 # does the application run
62 add_test(NAME Runs COMMAND Tutorial 25)
63
64 # does the usage message work?
65 add_test(NAME Usage COMMAND Tutorial)
66 set_tests_properties(Usage
67   PROPERTIES PASS_REGULAR_EXPRESSION "Usage:.*number"
68   )
69
70 # define a function to simplify adding tests
71 function(do_test target arg result)
72   add_test(NAME Comp${arg} COMMAND ${target} ${arg})
73   set_tests_properties(Comp${arg}
74     PROPERTIES PASS_REGULAR_EXPRESSION ${result}
75     )
76 endfunction()
77
78 # do a bunch of result based tests
79 do_test(Tutorial 4 "4 is 2")
80 do_test(Tutorial 9 "9 is 3")
81 do_test(Tutorial 5 "5 is 2.236")
82 do_test(Tutorial 7 "7 is 2.645")
83 do_test(Tutorial 25 "25 is 5")
84 do_test(Tutorial -25 "-25 is (-nan|nan|0)")
85 do_test(Tutorial 0.0001 "0.0001 is 0.01")
86
87 include(InstallRequiredSystemLibraries)
88 set(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_SOURCE_DIR}/License.txt")
89 set(CPACK_PACKAGE_VERSION_MAJOR "${Tutorial_VERSION_MAJOR}")
90 set(CPACK_PACKAGE_VERSION_MINOR "${Tutorial_VERSION_MINOR}")
91 set(CPACK_SOURCE_GENERATOR "TGZ")
92 include(CPack)
93
94 # install the configuration targets
95 install(EXPORT MathFunctionsTargets
96   FILE MathFunctionsTargets.cmake
97   DESTINATION lib/cmake/MathFunctions
98 )
99
100 include(CMakePackageConfigHelpers)
101 # generate the config file that is includes the exports
102 configure_package_config_file(${CMAKE_CURRENT_SOURCE_DIR}/Config.cmake.in
103   "${CMAKE_CURRENT_BINARY_DIR}/MathFunctionsConfig.cmake"
104   INSTALL_DESTINATION "lib/cmake/example"
105   NO_SET_AND_CHECK_MACRO
106   NO_CHECK_REQUIRED_COMPONENTS_MACRO
107   )
108 # generate the version file for the config file
109 write_basic_package_version_file(
110   "${CMAKE_CURRENT_BINARY_DIR}/MathFunctionsConfigVersion.cmake"
111   VERSION "${Tutorial_VERSION_MAJOR}.${Tutorial_VERSION_MINOR}"
112   COMPATIBILITY AnyNewerVersion
113 )
114
115 # install the configuration file
116 install(FILES
117   ${CMAKE_CURRENT_BINARY_DIR}/MathFunctionsConfig.cmake
118   DESTINATION lib/cmake/MathFunctions
119   )
120
121 # generate the export targets for the build tree
122 # needs to be after the install(TARGETS ) command
123 export(EXPORT MathFunctionsTargets
124   FILE "${CMAKE_CURRENT_BINARY_DIR}/MathFunctionsTargets.cmake"
125 )