1 cmake_minimum_required(VERSION 3.15)
3 # set the project name and version
4 project(Tutorial VERSION 1.0)
6 set(CMAKE_DEBUG_POSTFIX d)
8 add_library(tutorial_compiler_flags INTERFACE)
9 target_compile_features(tutorial_compiler_flags INTERFACE cxx_std_11)
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>>"
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}")
26 option(BUILD_SHARED_LIBS "Build using shared libraries" ON)
29 set(CMAKE_INSTALL_RPATH "@executable_path/../lib")
31 set(CMAKE_INSTALL_RPATH "$ORIGIN/../lib")
34 # configure a header file to pass the version number only
35 configure_file(TutorialConfig.h.in TutorialConfig.h)
37 # add the MathFunctions library
38 add_subdirectory(MathFunctions)
41 add_executable(Tutorial tutorial.cxx)
42 set_target_properties(Tutorial PROPERTIES DEBUG_POSTFIX ${CMAKE_DEBUG_POSTFIX})
44 target_link_libraries(Tutorial PUBLIC MathFunctions tutorial_compiler_flags)
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}"
52 # add the install targets
53 install(TARGETS Tutorial DESTINATION bin)
54 install(FILES "${PROJECT_BINARY_DIR}/TutorialConfig.h"
61 # does the application run
62 add_test(NAME Runs COMMAND Tutorial 25)
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"
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}
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")
88 include(InstallRequiredSystemLibraries)
89 set(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_SOURCE_DIR}/License.txt")
90 set(CPACK_PACKAGE_VERSION_MAJOR "${Tutorial_VERSION_MAJOR}")
91 set(CPACK_PACKAGE_VERSION_MINOR "${Tutorial_VERSION_MINOR}")
92 set(CPACK_SOURCE_GENERATOR "TGZ")
95 # install the configuration targets
96 install(EXPORT MathFunctionsTargets
97 FILE MathFunctionsTargets.cmake
98 DESTINATION lib/cmake/MathFunctions
101 include(CMakePackageConfigHelpers)
102 # generate the config file that is includes the exports
103 configure_package_config_file(${CMAKE_CURRENT_SOURCE_DIR}/Config.cmake.in
104 "${CMAKE_CURRENT_BINARY_DIR}/MathFunctionsConfig.cmake"
105 INSTALL_DESTINATION "lib/cmake/example"
106 NO_SET_AND_CHECK_MACRO
107 NO_CHECK_REQUIRED_COMPONENTS_MACRO
109 # generate the version file for the config file
110 write_basic_package_version_file(
111 "${CMAKE_CURRENT_BINARY_DIR}/MathFunctionsConfigVersion.cmake"
112 VERSION "${Tutorial_VERSION_MAJOR}.${Tutorial_VERSION_MINOR}"
113 COMPATIBILITY AnyNewerVersion
116 # install the configuration file
118 ${CMAKE_CURRENT_BINARY_DIR}/MathFunctionsConfig.cmake
119 DESTINATION lib/cmake/MathFunctions
122 # generate the export targets for the build tree
123 # needs to be after the install(TARGETS ) command
124 export(EXPORT MathFunctionsTargets
125 FILE "${CMAKE_CURRENT_BINARY_DIR}/MathFunctionsTargets.cmake"