1 cmake_minimum_required(VERSION 3.15)
3 # set the project name and version
4 project(Tutorial VERSION 1.0)
6 add_library(tutorial_compiler_flags INTERFACE)
7 target_compile_features(tutorial_compiler_flags INTERFACE cxx_std_11)
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>>"
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}")
24 option(BUILD_SHARED_LIBS "Build using shared libraries" ON)
26 # configure a header file to pass the version number only
27 configure_file(TutorialConfig.h.in TutorialConfig.h)
29 # add the MathFunctions library
30 add_subdirectory(MathFunctions)
33 add_executable(Tutorial tutorial.cxx)
34 target_link_libraries(Tutorial PUBLIC MathFunctions)
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}"
42 # add the install targets
43 install(TARGETS Tutorial DESTINATION bin)
44 install(FILES "${PROJECT_BINARY_DIR}/TutorialConfig.h"
51 # does the application run
52 add_test(NAME Runs COMMAND Tutorial 25)
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"
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}
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")
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")