Imported Upstream version 3.25.0
[platform/upstream/cmake.git] / Help / guide / tutorial / Step12 / 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 if(APPLE)
27   set(CMAKE_INSTALL_RPATH "@executable_path/../lib")
28 elseif(UNIX)
29   set(CMAKE_INSTALL_RPATH "$ORIGIN/../lib")
30 endif()
31
32 # configure a header file to pass the version number only
33 configure_file(TutorialConfig.h.in TutorialConfig.h)
34
35 # add the MathFunctions library
36 add_subdirectory(MathFunctions)
37
38 # add the executable
39 add_executable(Tutorial tutorial.cxx)
40 target_link_libraries(Tutorial PUBLIC MathFunctions tutorial_compiler_flags)
41
42 # add the binary tree to the search path for include files
43 # so that we will find TutorialConfig.h
44 target_include_directories(Tutorial PUBLIC
45                            "${PROJECT_BINARY_DIR}"
46                            )
47
48 # add the install targets
49 install(TARGETS Tutorial DESTINATION bin)
50 install(FILES "${PROJECT_BINARY_DIR}/TutorialConfig.h"
51   DESTINATION include
52   )
53
54 # enable testing
55 enable_testing()
56
57 # does the application run
58 add_test(NAME Runs COMMAND Tutorial 25)
59
60 # does the usage message work?
61 add_test(NAME Usage COMMAND Tutorial)
62 set_tests_properties(Usage
63   PROPERTIES PASS_REGULAR_EXPRESSION "Usage:.*number"
64   )
65
66 # define a function to simplify adding tests
67 function(do_test target arg result)
68   add_test(NAME Comp${arg} COMMAND ${target} ${arg})
69   set_tests_properties(Comp${arg}
70     PROPERTIES PASS_REGULAR_EXPRESSION ${result}
71     )
72 endfunction()
73
74 # do a bunch of result based tests
75 do_test(Tutorial 4 "4 is 2")
76 do_test(Tutorial 9 "9 is 3")
77 do_test(Tutorial 5 "5 is 2.236")
78 do_test(Tutorial 7 "7 is 2.645")
79 do_test(Tutorial 25 "25 is 5")
80 do_test(Tutorial -25 "-25 is (-nan|nan|0)")
81 do_test(Tutorial 0.0001 "0.0001 is 0.01")
82
83 # setup installer
84 include(InstallRequiredSystemLibraries)
85 set(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_SOURCE_DIR}/License.txt")
86 set(CPACK_PACKAGE_VERSION_MAJOR "${Tutorial_VERSION_MAJOR}")
87 set(CPACK_PACKAGE_VERSION_MINOR "${Tutorial_VERSION_MINOR}")
88 include(CPack)
89
90 # install the configuration targets
91 install(EXPORT MathFunctionsTargets
92   FILE MathFunctionsTargets.cmake
93   DESTINATION lib/cmake/MathFunctions
94 )
95
96 include(CMakePackageConfigHelpers)
97 # generate the config file that is includes the exports
98 configure_package_config_file(${CMAKE_CURRENT_SOURCE_DIR}/Config.cmake.in
99   "${CMAKE_CURRENT_BINARY_DIR}/MathFunctionsConfig.cmake"
100   INSTALL_DESTINATION "lib/cmake/example"
101   NO_SET_AND_CHECK_MACRO
102   NO_CHECK_REQUIRED_COMPONENTS_MACRO
103   )
104 # generate the version file for the config file
105 write_basic_package_version_file(
106   "${CMAKE_CURRENT_BINARY_DIR}/MathFunctionsConfigVersion.cmake"
107   VERSION "${Tutorial_VERSION_MAJOR}.${Tutorial_VERSION_MINOR}"
108   COMPATIBILITY AnyNewerVersion
109 )
110
111 # install the generated configuration files
112 install(FILES
113   ${CMAKE_CURRENT_BINARY_DIR}/MathFunctionsConfig.cmake
114   ${CMAKE_CURRENT_BINARY_DIR}/MathFunctionsConfigVersion.cmake
115   DESTINATION lib/cmake/MathFunctions
116   )
117
118 # generate the export targets for the build tree
119 # needs to be after the install(TARGETS ) command
120 export(EXPORT MathFunctionsTargets
121   FILE "${CMAKE_CURRENT_BINARY_DIR}/MathFunctionsTargets.cmake"
122 )