From 348c4c8a9197957dac6b72f27de63a241c42df15 Mon Sep 17 00:00:00 2001 From: Nigel Date: Sun, 29 Oct 2023 11:50:48 +0100 Subject: [PATCH] Started a cmake tutorial --- .idea/.gitignore | 8 ++ .idea/cmake-3.28.0-rc3-tutorial-source.iml | 8 ++ .idea/modules.xml | 8 ++ Complete/CMakeLists.txt | 126 +++++++++++++++++++++ Complete/CTestConfig.cmake | 7 ++ Complete/Config.cmake.in | 4 + Complete/License.txt | 2 + Complete/MathFunctions/CMakeLists.txt | 62 ++++++++++ Complete/MathFunctions/MakeTable.cmake | 10 ++ Complete/MathFunctions/MakeTable.cxx | 25 ++++ Complete/MathFunctions/MathFunctions.cxx | 20 ++++ Complete/MathFunctions/MathFunctions.h | 14 +++ Complete/MathFunctions/mysqrt.cxx | 37 ++++++ Complete/MathFunctions/mysqrt.h | 6 + Complete/MultiCPackConfig.cmake | 6 + Complete/TutorialConfig.h.in | 3 + Complete/tutorial.cxx | 26 +++++ README.txt | 4 + Step1/CMakeLists.txt | 16 +++ Step1/TutorialConfig.h.in | 2 + Step1/tutorial.cxx | 27 +++++ Step10/CMakeLists.txt | 76 +++++++++++++ Step10/CTestConfig.cmake | 7 ++ Step10/License.txt | 2 + Step10/MathFunctions/CMakeLists.txt | 45 ++++++++ Step10/MathFunctions/MakeTable.cmake | 10 ++ Step10/MathFunctions/MakeTable.cxx | 25 ++++ Step10/MathFunctions/MathFunctions.cxx | 20 ++++ Step10/MathFunctions/MathFunctions.h | 3 + Step10/MathFunctions/mysqrt.cxx | 37 ++++++ Step10/MathFunctions/mysqrt.h | 6 + Step10/TutorialConfig.h.in | 3 + Step10/tutorial.cxx | 27 +++++ Step11/CMakeLists.txt | 84 ++++++++++++++ Step11/CTestConfig.cmake | 7 ++ Step11/License.txt | 2 + Step11/MathFunctions/CMakeLists.txt | 54 +++++++++ Step11/MathFunctions/MakeTable.cmake | 10 ++ Step11/MathFunctions/MakeTable.cxx | 25 ++++ Step11/MathFunctions/MathFunctions.cxx | 20 ++++ Step11/MathFunctions/MathFunctions.h | 14 +++ Step11/MathFunctions/mysqrt.cxx | 37 ++++++ Step11/MathFunctions/mysqrt.h | 6 + Step11/TutorialConfig.h.in | 3 + Step11/tutorial.cxx | 27 +++++ Step12/CMakeLists.txt | 122 ++++++++++++++++++++ Step12/CTestConfig.cmake | 7 ++ Step12/Config.cmake.in | 4 + Step12/License.txt | 2 + Step12/MathFunctions/CMakeLists.txt | 58 ++++++++++ Step12/MathFunctions/MakeTable.cmake | 10 ++ Step12/MathFunctions/MakeTable.cxx | 25 ++++ Step12/MathFunctions/MathFunctions.cxx | 20 ++++ Step12/MathFunctions/MathFunctions.h | 14 +++ Step12/MathFunctions/mysqrt.cxx | 37 ++++++ Step12/MathFunctions/mysqrt.h | 6 + Step12/TutorialConfig.h.in | 3 + Step12/tutorial.cxx | 26 +++++ Step2/CMakeLists.txt | 28 +++++ Step2/MathFunctions/CMakeLists.txt | 15 +++ Step2/MathFunctions/MathFunctions.cxx | 15 +++ Step2/MathFunctions/MathFunctions.h | 5 + Step2/MathFunctions/mysqrt.cxx | 28 +++++ Step2/MathFunctions/mysqrt.h | 7 ++ Step2/TutorialConfig.h.in | 3 + Step2/tutorial.cxx | 29 +++++ Step3/CMakeLists.txt | 40 +++++++ Step3/MathFunctions/CMakeLists.txt | 22 ++++ Step3/MathFunctions/MathFunctions.cxx | 19 ++++ Step3/MathFunctions/MathFunctions.h | 5 + Step3/MathFunctions/mysqrt.cxx | 28 +++++ Step3/MathFunctions/mysqrt.h | 7 ++ Step3/TutorialConfig.h.in | 3 + Step3/tutorial.cxx | 27 +++++ Step4/CMakeLists.txt | 44 +++++++ Step4/MathFunctions/CMakeLists.txt | 27 +++++ Step4/MathFunctions/MathFunctions.cxx | 19 ++++ Step4/MathFunctions/MathFunctions.h | 5 + Step4/MathFunctions/mysqrt.cxx | 28 +++++ Step4/MathFunctions/mysqrt.h | 7 ++ Step4/TutorialConfig.h.in | 3 + Step4/tutorial.cxx | 27 +++++ Step5/CMakeLists.txt | 59 ++++++++++ Step5/MathFunctions/CMakeLists.txt | 34 ++++++ Step5/MathFunctions/MathFunctions.cxx | 19 ++++ Step5/MathFunctions/MathFunctions.h | 5 + Step5/MathFunctions/mysqrt.cxx | 28 +++++ Step5/MathFunctions/mysqrt.h | 7 ++ Step5/TutorialConfig.h.in | 3 + Step5/tutorial.cxx | 27 +++++ Step6/CMakeLists.txt | 71 ++++++++++++ Step6/CTestConfig.cmake | 7 ++ Step6/MathFunctions/CMakeLists.txt | 35 ++++++ Step6/MathFunctions/MathFunctions.cxx | 19 ++++ Step6/MathFunctions/MathFunctions.h | 5 + Step6/MathFunctions/mysqrt.cxx | 28 +++++ Step6/MathFunctions/mysqrt.h | 7 ++ Step6/TutorialConfig.h.in | 3 + Step6/tutorial.cxx | 27 +++++ Step7/CMakeLists.txt | 70 ++++++++++++ Step7/CTestConfig.cmake | 7 ++ Step7/MathFunctions/CMakeLists.txt | 55 +++++++++ Step7/MathFunctions/MathFunctions.cxx | 19 ++++ Step7/MathFunctions/MathFunctions.h | 5 + Step7/MathFunctions/mysqrt.cxx | 37 ++++++ Step7/MathFunctions/mysqrt.h | 7 ++ Step7/TutorialConfig.h.in | 3 + Step7/tutorial.cxx | 27 +++++ Step8/CMakeLists.txt | 70 ++++++++++++ Step8/CTestConfig.cmake | 7 ++ Step8/MathFunctions/CMakeLists.txt | 59 ++++++++++ Step8/MathFunctions/MakeTable.cxx | 25 ++++ Step8/MathFunctions/MathFunctions.cxx | 19 ++++ Step8/MathFunctions/MathFunctions.h | 5 + Step8/MathFunctions/mysqrt.cxx | 36 ++++++ Step8/MathFunctions/mysqrt.h | 7 ++ Step8/TutorialConfig.h.in | 3 + Step8/tutorial.cxx | 27 +++++ Step9/CMakeLists.txt | 70 ++++++++++++ Step9/CTestConfig.cmake | 7 ++ Step9/License.txt | 2 + Step9/MathFunctions/CMakeLists.txt | 44 +++++++ Step9/MathFunctions/MakeTable.cmake | 10 ++ Step9/MathFunctions/MakeTable.cxx | 25 ++++ Step9/MathFunctions/MathFunctions.cxx | 19 ++++ Step9/MathFunctions/MathFunctions.h | 5 + Step9/MathFunctions/mysqrt.cxx | 37 ++++++ Step9/MathFunctions/mysqrt.h | 7 ++ Step9/TutorialConfig.h.in | 3 + Step9/tutorial.cxx | 27 +++++ 130 files changed, 2874 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/cmake-3.28.0-rc3-tutorial-source.iml create mode 100644 .idea/modules.xml create mode 100644 Complete/CMakeLists.txt create mode 100644 Complete/CTestConfig.cmake create mode 100644 Complete/Config.cmake.in create mode 100644 Complete/License.txt create mode 100644 Complete/MathFunctions/CMakeLists.txt create mode 100644 Complete/MathFunctions/MakeTable.cmake create mode 100644 Complete/MathFunctions/MakeTable.cxx create mode 100644 Complete/MathFunctions/MathFunctions.cxx create mode 100644 Complete/MathFunctions/MathFunctions.h create mode 100644 Complete/MathFunctions/mysqrt.cxx create mode 100644 Complete/MathFunctions/mysqrt.h create mode 100644 Complete/MultiCPackConfig.cmake create mode 100644 Complete/TutorialConfig.h.in create mode 100644 Complete/tutorial.cxx create mode 100644 README.txt create mode 100644 Step1/CMakeLists.txt create mode 100644 Step1/TutorialConfig.h.in create mode 100644 Step1/tutorial.cxx create mode 100644 Step10/CMakeLists.txt create mode 100644 Step10/CTestConfig.cmake create mode 100644 Step10/License.txt create mode 100644 Step10/MathFunctions/CMakeLists.txt create mode 100644 Step10/MathFunctions/MakeTable.cmake create mode 100644 Step10/MathFunctions/MakeTable.cxx create mode 100644 Step10/MathFunctions/MathFunctions.cxx create mode 100644 Step10/MathFunctions/MathFunctions.h create mode 100644 Step10/MathFunctions/mysqrt.cxx create mode 100644 Step10/MathFunctions/mysqrt.h create mode 100644 Step10/TutorialConfig.h.in create mode 100644 Step10/tutorial.cxx create mode 100644 Step11/CMakeLists.txt create mode 100644 Step11/CTestConfig.cmake create mode 100644 Step11/License.txt create mode 100644 Step11/MathFunctions/CMakeLists.txt create mode 100644 Step11/MathFunctions/MakeTable.cmake create mode 100644 Step11/MathFunctions/MakeTable.cxx create mode 100644 Step11/MathFunctions/MathFunctions.cxx create mode 100644 Step11/MathFunctions/MathFunctions.h create mode 100644 Step11/MathFunctions/mysqrt.cxx create mode 100644 Step11/MathFunctions/mysqrt.h create mode 100644 Step11/TutorialConfig.h.in create mode 100644 Step11/tutorial.cxx create mode 100644 Step12/CMakeLists.txt create mode 100644 Step12/CTestConfig.cmake create mode 100644 Step12/Config.cmake.in create mode 100644 Step12/License.txt create mode 100644 Step12/MathFunctions/CMakeLists.txt create mode 100644 Step12/MathFunctions/MakeTable.cmake create mode 100644 Step12/MathFunctions/MakeTable.cxx create mode 100644 Step12/MathFunctions/MathFunctions.cxx create mode 100644 Step12/MathFunctions/MathFunctions.h create mode 100644 Step12/MathFunctions/mysqrt.cxx create mode 100644 Step12/MathFunctions/mysqrt.h create mode 100644 Step12/TutorialConfig.h.in create mode 100644 Step12/tutorial.cxx create mode 100644 Step2/CMakeLists.txt create mode 100644 Step2/MathFunctions/CMakeLists.txt create mode 100644 Step2/MathFunctions/MathFunctions.cxx create mode 100644 Step2/MathFunctions/MathFunctions.h create mode 100644 Step2/MathFunctions/mysqrt.cxx create mode 100644 Step2/MathFunctions/mysqrt.h create mode 100644 Step2/TutorialConfig.h.in create mode 100644 Step2/tutorial.cxx create mode 100644 Step3/CMakeLists.txt create mode 100644 Step3/MathFunctions/CMakeLists.txt create mode 100644 Step3/MathFunctions/MathFunctions.cxx create mode 100644 Step3/MathFunctions/MathFunctions.h create mode 100644 Step3/MathFunctions/mysqrt.cxx create mode 100644 Step3/MathFunctions/mysqrt.h create mode 100644 Step3/TutorialConfig.h.in create mode 100644 Step3/tutorial.cxx create mode 100644 Step4/CMakeLists.txt create mode 100644 Step4/MathFunctions/CMakeLists.txt create mode 100644 Step4/MathFunctions/MathFunctions.cxx create mode 100644 Step4/MathFunctions/MathFunctions.h create mode 100644 Step4/MathFunctions/mysqrt.cxx create mode 100644 Step4/MathFunctions/mysqrt.h create mode 100644 Step4/TutorialConfig.h.in create mode 100644 Step4/tutorial.cxx create mode 100644 Step5/CMakeLists.txt create mode 100644 Step5/MathFunctions/CMakeLists.txt create mode 100644 Step5/MathFunctions/MathFunctions.cxx create mode 100644 Step5/MathFunctions/MathFunctions.h create mode 100644 Step5/MathFunctions/mysqrt.cxx create mode 100644 Step5/MathFunctions/mysqrt.h create mode 100644 Step5/TutorialConfig.h.in create mode 100644 Step5/tutorial.cxx create mode 100644 Step6/CMakeLists.txt create mode 100644 Step6/CTestConfig.cmake create mode 100644 Step6/MathFunctions/CMakeLists.txt create mode 100644 Step6/MathFunctions/MathFunctions.cxx create mode 100644 Step6/MathFunctions/MathFunctions.h create mode 100644 Step6/MathFunctions/mysqrt.cxx create mode 100644 Step6/MathFunctions/mysqrt.h create mode 100644 Step6/TutorialConfig.h.in create mode 100644 Step6/tutorial.cxx create mode 100644 Step7/CMakeLists.txt create mode 100644 Step7/CTestConfig.cmake create mode 100644 Step7/MathFunctions/CMakeLists.txt create mode 100644 Step7/MathFunctions/MathFunctions.cxx create mode 100644 Step7/MathFunctions/MathFunctions.h create mode 100644 Step7/MathFunctions/mysqrt.cxx create mode 100644 Step7/MathFunctions/mysqrt.h create mode 100644 Step7/TutorialConfig.h.in create mode 100644 Step7/tutorial.cxx create mode 100644 Step8/CMakeLists.txt create mode 100644 Step8/CTestConfig.cmake create mode 100644 Step8/MathFunctions/CMakeLists.txt create mode 100644 Step8/MathFunctions/MakeTable.cxx create mode 100644 Step8/MathFunctions/MathFunctions.cxx create mode 100644 Step8/MathFunctions/MathFunctions.h create mode 100644 Step8/MathFunctions/mysqrt.cxx create mode 100644 Step8/MathFunctions/mysqrt.h create mode 100644 Step8/TutorialConfig.h.in create mode 100644 Step8/tutorial.cxx create mode 100644 Step9/CMakeLists.txt create mode 100644 Step9/CTestConfig.cmake create mode 100644 Step9/License.txt create mode 100644 Step9/MathFunctions/CMakeLists.txt create mode 100644 Step9/MathFunctions/MakeTable.cmake create mode 100644 Step9/MathFunctions/MakeTable.cxx create mode 100644 Step9/MathFunctions/MathFunctions.cxx create mode 100644 Step9/MathFunctions/MathFunctions.h create mode 100644 Step9/MathFunctions/mysqrt.cxx create mode 100644 Step9/MathFunctions/mysqrt.h create mode 100644 Step9/TutorialConfig.h.in create mode 100644 Step9/tutorial.cxx diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/cmake-3.28.0-rc3-tutorial-source.iml b/.idea/cmake-3.28.0-rc3-tutorial-source.iml new file mode 100644 index 0000000..bc2cd87 --- /dev/null +++ b/.idea/cmake-3.28.0-rc3-tutorial-source.iml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..2c5cbaa --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/Complete/CMakeLists.txt b/Complete/CMakeLists.txt new file mode 100644 index 0000000..3cdaaae --- /dev/null +++ b/Complete/CMakeLists.txt @@ -0,0 +1,126 @@ +cmake_minimum_required(VERSION 3.15) + +# set the project name and version +project(Tutorial VERSION 1.0) + +set(CMAKE_DEBUG_POSTFIX d) + +add_library(tutorial_compiler_flags INTERFACE) +target_compile_features(tutorial_compiler_flags INTERFACE cxx_std_11) + +# add compiler warning flags just when building this project via +# the BUILD_INTERFACE genex +set(gcc_like_cxx "$") +set(msvc_cxx "$") +target_compile_options(tutorial_compiler_flags INTERFACE + "$<${gcc_like_cxx}:$>" + "$<${msvc_cxx}:$>" +) + +# control where the static and shared libraries are built so that on windows +# we don't need to tinker with the path to run the executable +set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}") +set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}") +set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}") + +option(BUILD_SHARED_LIBS "Build using shared libraries" ON) + +if(APPLE) + set(CMAKE_INSTALL_RPATH "@executable_path/../lib") +elseif(UNIX) + set(CMAKE_INSTALL_RPATH "$ORIGIN/../lib") +endif() + +# configure a header file to pass the version number only +configure_file(TutorialConfig.h.in TutorialConfig.h) + +# add the MathFunctions library +add_subdirectory(MathFunctions) + +# add the executable +add_executable(Tutorial tutorial.cxx) +set_target_properties(Tutorial PROPERTIES DEBUG_POSTFIX ${CMAKE_DEBUG_POSTFIX}) + +target_link_libraries(Tutorial PUBLIC MathFunctions tutorial_compiler_flags) + +# add the binary tree to the search path for include files +# so that we will find TutorialConfig.h +target_include_directories(Tutorial PUBLIC + "${PROJECT_BINARY_DIR}" + ) + +# add the install targets +install(TARGETS Tutorial DESTINATION bin) +install(FILES "${PROJECT_BINARY_DIR}/TutorialConfig.h" + DESTINATION include + ) + +# enable testing +enable_testing() + +# does the application run +add_test(NAME Runs COMMAND Tutorial 25) + +# does the usage message work? +add_test(NAME Usage COMMAND Tutorial) +set_tests_properties(Usage + PROPERTIES PASS_REGULAR_EXPRESSION "Usage:.*number" + ) + +# define a function to simplify adding tests +function(do_test target arg result) + add_test(NAME Comp${arg} COMMAND ${target} ${arg}) + set_tests_properties(Comp${arg} + PROPERTIES PASS_REGULAR_EXPRESSION ${result} + ) +endfunction() + +# do a bunch of result based tests +do_test(Tutorial 4 "4 is 2") +do_test(Tutorial 9 "9 is 3") +do_test(Tutorial 5 "5 is 2.236") +do_test(Tutorial 7 "7 is 2.645") +do_test(Tutorial 25 "25 is 5") +do_test(Tutorial -25 "-25 is (-nan|nan|0)") +do_test(Tutorial 0.0001 "0.0001 is 0.01") + +# setup installer +include(InstallRequiredSystemLibraries) +set(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_SOURCE_DIR}/License.txt") +set(CPACK_PACKAGE_VERSION_MAJOR "${Tutorial_VERSION_MAJOR}") +set(CPACK_PACKAGE_VERSION_MINOR "${Tutorial_VERSION_MINOR}") +set(CPACK_SOURCE_GENERATOR "TGZ") +include(CPack) + +# install the configuration targets +install(EXPORT MathFunctionsTargets + FILE MathFunctionsTargets.cmake + DESTINATION lib/cmake/MathFunctions +) + +include(CMakePackageConfigHelpers) +# generate the config file that is includes the exports +configure_package_config_file(${CMAKE_CURRENT_SOURCE_DIR}/Config.cmake.in + "${CMAKE_CURRENT_BINARY_DIR}/MathFunctionsConfig.cmake" + INSTALL_DESTINATION "lib/cmake/example" + NO_SET_AND_CHECK_MACRO + NO_CHECK_REQUIRED_COMPONENTS_MACRO + ) +# generate the version file for the config file +write_basic_package_version_file( + "${CMAKE_CURRENT_BINARY_DIR}/MathFunctionsConfigVersion.cmake" + VERSION "${Tutorial_VERSION_MAJOR}.${Tutorial_VERSION_MINOR}" + COMPATIBILITY AnyNewerVersion +) + +# install the configuration file +install(FILES + ${CMAKE_CURRENT_BINARY_DIR}/MathFunctionsConfig.cmake + DESTINATION lib/cmake/MathFunctions + ) + +# generate the export targets for the build tree +# needs to be after the install(TARGETS ) command +export(EXPORT MathFunctionsTargets + FILE "${CMAKE_CURRENT_BINARY_DIR}/MathFunctionsTargets.cmake" +) diff --git a/Complete/CTestConfig.cmake b/Complete/CTestConfig.cmake new file mode 100644 index 0000000..73efdb1 --- /dev/null +++ b/Complete/CTestConfig.cmake @@ -0,0 +1,7 @@ +set(CTEST_PROJECT_NAME "CMakeTutorial") +set(CTEST_NIGHTLY_START_TIME "00:00:00 EST") + +set(CTEST_DROP_METHOD "http") +set(CTEST_DROP_SITE "my.cdash.org") +set(CTEST_DROP_LOCATION "/submit.php?project=CMakeTutorial") +set(CTEST_DROP_SITE_CDASH TRUE) diff --git a/Complete/Config.cmake.in b/Complete/Config.cmake.in new file mode 100644 index 0000000..17cbabd --- /dev/null +++ b/Complete/Config.cmake.in @@ -0,0 +1,4 @@ + +@PACKAGE_INIT@ + +include ( "${CMAKE_CURRENT_LIST_DIR}/MathFunctionsTargets.cmake" ) diff --git a/Complete/License.txt b/Complete/License.txt new file mode 100644 index 0000000..85760e5 --- /dev/null +++ b/Complete/License.txt @@ -0,0 +1,2 @@ +This is the open source License.txt file introduced in +CMake/Tutorial/Step9... diff --git a/Complete/MathFunctions/CMakeLists.txt b/Complete/MathFunctions/CMakeLists.txt new file mode 100644 index 0000000..1654564 --- /dev/null +++ b/Complete/MathFunctions/CMakeLists.txt @@ -0,0 +1,62 @@ +# add the library that runs +add_library(MathFunctions MathFunctions.cxx) + +# state that anybody linking to us needs to include the current source dir +# to find MathFunctions.h, while we don't. +target_include_directories(MathFunctions + INTERFACE + $ + $ + ) + +# should we use our own math functions +option(USE_MYMATH "Use tutorial provided math implementation" ON) +if(USE_MYMATH) + + target_compile_definitions(MathFunctions PRIVATE "USE_MYMATH") + + include(MakeTable.cmake) # generates Table.h + + # library that just does sqrt + add_library(SqrtLibrary STATIC + mysqrt.cxx + ${CMAKE_CURRENT_BINARY_DIR}/Table.h + ) + + # state that we depend on our binary dir to find Table.h + target_include_directories(SqrtLibrary PRIVATE + ${CMAKE_CURRENT_BINARY_DIR} + ) + + # state that SqrtLibrary need PIC when the default is shared libraries + set_target_properties(SqrtLibrary PROPERTIES + POSITION_INDEPENDENT_CODE ${BUILD_SHARED_LIBS} + ) + + # link SqrtLibrary to tutorial_compiler_flags + target_link_libraries(SqrtLibrary PUBLIC tutorial_compiler_flags) + + target_link_libraries(MathFunctions PRIVATE SqrtLibrary) +endif() + +# link MathFunctions to tutorial_compiler_flags +target_link_libraries(MathFunctions PUBLIC tutorial_compiler_flags) + +# define the symbol stating we are using the declspec(dllexport) when +# building on windows +target_compile_definitions(MathFunctions PRIVATE "EXPORTING_MYMATH") + +# setup the version numbering +set_property(TARGET MathFunctions PROPERTY VERSION "1.0.0") +set_property(TARGET MathFunctions PROPERTY SOVERSION "1") + +# install libs +set(installable_libs MathFunctions tutorial_compiler_flags) +if(TARGET SqrtLibrary) + list(APPEND installable_libs SqrtLibrary) +endif() +install(TARGETS ${installable_libs} + EXPORT MathFunctionsTargets + DESTINATION lib) +# install include headers +install(FILES MathFunctions.h DESTINATION include) diff --git a/Complete/MathFunctions/MakeTable.cmake b/Complete/MathFunctions/MakeTable.cmake new file mode 100644 index 0000000..12865a9 --- /dev/null +++ b/Complete/MathFunctions/MakeTable.cmake @@ -0,0 +1,10 @@ +# first we add the executable that generates the table +add_executable(MakeTable MakeTable.cxx) +target_link_libraries(MakeTable PRIVATE tutorial_compiler_flags) + +# add the command to generate the source code +add_custom_command( + OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/Table.h + COMMAND MakeTable ${CMAKE_CURRENT_BINARY_DIR}/Table.h + DEPENDS MakeTable + ) diff --git a/Complete/MathFunctions/MakeTable.cxx b/Complete/MathFunctions/MakeTable.cxx new file mode 100644 index 0000000..ee58556 --- /dev/null +++ b/Complete/MathFunctions/MakeTable.cxx @@ -0,0 +1,25 @@ +// A simple program that builds a sqrt table +#include +#include +#include + +int main(int argc, char* argv[]) +{ + // make sure we have enough arguments + if (argc < 2) { + return 1; + } + + std::ofstream fout(argv[1], std::ios_base::out); + const bool fileOpen = fout.is_open(); + if (fileOpen) { + fout << "double sqrtTable[] = {" << std::endl; + for (int i = 0; i < 10; ++i) { + fout << sqrt(static_cast(i)) << "," << std::endl; + } + // close the table with a zero + fout << "0};" << std::endl; + fout.close(); + } + return fileOpen ? 0 : 1; // return 0 if wrote the file +} diff --git a/Complete/MathFunctions/MathFunctions.cxx b/Complete/MathFunctions/MathFunctions.cxx new file mode 100644 index 0000000..c0991b9 --- /dev/null +++ b/Complete/MathFunctions/MathFunctions.cxx @@ -0,0 +1,20 @@ + +#include "MathFunctions.h" + +#include + +#ifdef USE_MYMATH +# include "mysqrt.h" +#endif + +namespace mathfunctions { +double sqrt(double x) +{ +// which square root function should we use? +#ifdef USE_MYMATH + return detail::mysqrt(x); +#else + return std::sqrt(x); +#endif +} +} diff --git a/Complete/MathFunctions/MathFunctions.h b/Complete/MathFunctions/MathFunctions.h new file mode 100644 index 0000000..3fb547b --- /dev/null +++ b/Complete/MathFunctions/MathFunctions.h @@ -0,0 +1,14 @@ + +#if defined(_WIN32) +# if defined(EXPORTING_MYMATH) +# define DECLSPEC __declspec(dllexport) +# else +# define DECLSPEC __declspec(dllimport) +# endif +#else // non windows +# define DECLSPEC +#endif + +namespace mathfunctions { +double DECLSPEC sqrt(double x); +} diff --git a/Complete/MathFunctions/mysqrt.cxx b/Complete/MathFunctions/mysqrt.cxx new file mode 100644 index 0000000..8153f18 --- /dev/null +++ b/Complete/MathFunctions/mysqrt.cxx @@ -0,0 +1,37 @@ +#include + +#include "MathFunctions.h" + +// include the generated table +#include "Table.h" + +namespace mathfunctions { +namespace detail { +// a hack square root calculation using simple operations +double mysqrt(double x) +{ + if (x <= 0) { + return 0; + } + + // use the table to help find an initial value + double result = x; + if (x >= 1 && x < 10) { + std::cout << "Use the table to help find an initial value " << std::endl; + result = sqrtTable[static_cast(x)]; + } + + // do ten iterations + for (int i = 0; i < 10; ++i) { + if (result <= 0) { + result = 0.1; + } + double delta = x - (result * result); + result = result + 0.5 * delta / result; + std::cout << "Computing sqrt of " << x << " to be " << result << std::endl; + } + + return result; +} +} +} diff --git a/Complete/MathFunctions/mysqrt.h b/Complete/MathFunctions/mysqrt.h new file mode 100644 index 0000000..e1c42ef --- /dev/null +++ b/Complete/MathFunctions/mysqrt.h @@ -0,0 +1,6 @@ + +namespace mathfunctions { +namespace detail { +double mysqrt(double x); +} +} diff --git a/Complete/MultiCPackConfig.cmake b/Complete/MultiCPackConfig.cmake new file mode 100644 index 0000000..c2583df --- /dev/null +++ b/Complete/MultiCPackConfig.cmake @@ -0,0 +1,6 @@ +include("release/CPackConfig.cmake") + +set(CPACK_INSTALL_CMAKE_PROJECTS + "debug;Tutorial;ALL;/" + "release;Tutorial;ALL;/" + ) diff --git a/Complete/TutorialConfig.h.in b/Complete/TutorialConfig.h.in new file mode 100644 index 0000000..7e4d7fa --- /dev/null +++ b/Complete/TutorialConfig.h.in @@ -0,0 +1,3 @@ +// the configured options and settings for Tutorial +#define Tutorial_VERSION_MAJOR @Tutorial_VERSION_MAJOR@ +#define Tutorial_VERSION_MINOR @Tutorial_VERSION_MINOR@ diff --git a/Complete/tutorial.cxx b/Complete/tutorial.cxx new file mode 100644 index 0000000..a4f44d5 --- /dev/null +++ b/Complete/tutorial.cxx @@ -0,0 +1,26 @@ +// A simple program that computes the square root of a number +#include +#include + +#include "MathFunctions.h" +#include "TutorialConfig.h" + +int main(int argc, char* argv[]) +{ + if (argc < 2) { + // report version + std::cout << argv[0] << " Version " << Tutorial_VERSION_MAJOR << "." + << Tutorial_VERSION_MINOR << std::endl; + std::cout << "Usage: " << argv[0] << " number" << std::endl; + return 1; + } + + // convert input to double + const double inputValue = std::stod(argv[1]); + + const double outputValue = mathfunctions::sqrt(inputValue); + + std::cout << "The square root of " << inputValue << " is " << outputValue + << std::endl; + return 0; +} diff --git a/README.txt b/README.txt new file mode 100644 index 0000000..bc30679 --- /dev/null +++ b/README.txt @@ -0,0 +1,4 @@ +This directory contains source code examples for the CMake Tutorial. +Each step has its own subdirectory containing code that may be used as a +starting point. The tutorial examples are progressive so that each step +provides the complete solution for the previous step. diff --git a/Step1/CMakeLists.txt b/Step1/CMakeLists.txt new file mode 100644 index 0000000..6fcce90 --- /dev/null +++ b/Step1/CMakeLists.txt @@ -0,0 +1,16 @@ +# TODO 1: Set the minimum required version of CMake to be 3.10 + +# TODO 2: Create a project named Tutorial + +# TODO 7: Set the project version number as 1.0 in the above project command + +# TODO 6: Set the variable CMAKE_CXX_STANDARD to 11 +# and the variable CMAKE_CXX_STANDARD_REQUIRED to True + +# TODO 8: Use configure_file to configure and copy TutorialConfig.h.in to +# TutorialConfig.h + +# TODO 3: Add an executable called Tutorial to the project +# Hint: Be sure to specify the source file as tutorial.cxx + +# TODO 9: Use target_include_directories to include ${PROJECT_BINARY_DIR} diff --git a/Step1/TutorialConfig.h.in b/Step1/TutorialConfig.h.in new file mode 100644 index 0000000..990bfbd --- /dev/null +++ b/Step1/TutorialConfig.h.in @@ -0,0 +1,2 @@ +// the configured options and settings for Tutorial +// TODO 10: Define Tutorial_VERSION_MAJOR and Tutorial_VERSION_MINOR diff --git a/Step1/tutorial.cxx b/Step1/tutorial.cxx new file mode 100644 index 0000000..64d0916 --- /dev/null +++ b/Step1/tutorial.cxx @@ -0,0 +1,27 @@ +// A simple program that computes the square root of a number +#include +#include // TODO 5: Remove this line +#include +#include + +// TODO 11: Include TutorialConfig.h + +int main(int argc, char* argv[]) +{ + if (argc < 2) { + // TODO 12: Create a print statement using Tutorial_VERSION_MAJOR + // and Tutorial_VERSION_MINOR + std::cout << "Usage: " << argv[0] << " number" << std::endl; + return 1; + } + + // convert input to double + // TODO 4: Replace atof(argv[1]) with std::stod(argv[1]) + const double inputValue = atof(argv[1]); + + // calculate square root + const double outputValue = sqrt(inputValue); + std::cout << "The square root of " << inputValue << " is " << outputValue + << std::endl; + return 0; +} diff --git a/Step10/CMakeLists.txt b/Step10/CMakeLists.txt new file mode 100644 index 0000000..2dd6db5 --- /dev/null +++ b/Step10/CMakeLists.txt @@ -0,0 +1,76 @@ +cmake_minimum_required(VERSION 3.15) + +# set the project name and version +project(Tutorial VERSION 1.0) + +# specify the C++ standard +add_library(tutorial_compiler_flags INTERFACE) +target_compile_features(tutorial_compiler_flags INTERFACE cxx_std_11) + +# add compiler warning flags just when building this project via +# the BUILD_INTERFACE genex +set(gcc_like_cxx "$") +set(msvc_cxx "$") +target_compile_options(tutorial_compiler_flags INTERFACE + "$<${gcc_like_cxx}:$>" + "$<${msvc_cxx}:$>" +) + +# configure a header file to pass the version number only +configure_file(TutorialConfig.h.in TutorialConfig.h) + +# add the MathFunctions library +add_subdirectory(MathFunctions) + +# add the executable +add_executable(Tutorial tutorial.cxx) +target_link_libraries(Tutorial PUBLIC MathFunctions tutorial_compiler_flags) + +# add the binary tree to the search path for include files +# so that we will find TutorialConfig.h +target_include_directories(Tutorial PUBLIC + "${PROJECT_BINARY_DIR}" + ) + +# add the install targets +install(TARGETS Tutorial DESTINATION bin) +install(FILES "${PROJECT_BINARY_DIR}/TutorialConfig.h" + DESTINATION include + ) + +# enable testing +include(CTest) + +# does the application run +add_test(NAME Runs COMMAND Tutorial 25) + +# does the usage message work? +add_test(NAME Usage COMMAND Tutorial) +set_tests_properties(Usage + PROPERTIES PASS_REGULAR_EXPRESSION "Usage:.*number" + ) + +# define a function to simplify adding tests +function(do_test target arg result) + add_test(NAME Comp${arg} COMMAND ${target} ${arg}) + set_tests_properties(Comp${arg} + PROPERTIES PASS_REGULAR_EXPRESSION ${result} + ) +endfunction() + +# do a bunch of result based tests +do_test(Tutorial 4 "4 is 2") +do_test(Tutorial 9 "9 is 3") +do_test(Tutorial 5 "5 is 2.236") +do_test(Tutorial 7 "7 is 2.645") +do_test(Tutorial 25 "25 is 5") +do_test(Tutorial -25 "-25 is (-nan|nan|0)") +do_test(Tutorial 0.0001 "0.0001 is 0.01") + +# setup installer +include(InstallRequiredSystemLibraries) +set(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_SOURCE_DIR}/License.txt") +set(CPACK_PACKAGE_VERSION_MAJOR "${Tutorial_VERSION_MAJOR}") +set(CPACK_PACKAGE_VERSION_MINOR "${Tutorial_VERSION_MINOR}") +set(CPACK_SOURCE_GENERATOR "TGZ") +include(CPack) diff --git a/Step10/CTestConfig.cmake b/Step10/CTestConfig.cmake new file mode 100644 index 0000000..73efdb1 --- /dev/null +++ b/Step10/CTestConfig.cmake @@ -0,0 +1,7 @@ +set(CTEST_PROJECT_NAME "CMakeTutorial") +set(CTEST_NIGHTLY_START_TIME "00:00:00 EST") + +set(CTEST_DROP_METHOD "http") +set(CTEST_DROP_SITE "my.cdash.org") +set(CTEST_DROP_LOCATION "/submit.php?project=CMakeTutorial") +set(CTEST_DROP_SITE_CDASH TRUE) diff --git a/Step10/License.txt b/Step10/License.txt new file mode 100644 index 0000000..85760e5 --- /dev/null +++ b/Step10/License.txt @@ -0,0 +1,2 @@ +This is the open source License.txt file introduced in +CMake/Tutorial/Step9... diff --git a/Step10/MathFunctions/CMakeLists.txt b/Step10/MathFunctions/CMakeLists.txt new file mode 100644 index 0000000..210563a --- /dev/null +++ b/Step10/MathFunctions/CMakeLists.txt @@ -0,0 +1,45 @@ +# add the library that runs +add_library(MathFunctions MathFunctions.cxx) + +# state that anybody linking to us needs to include the current source dir +# to find MathFunctions.h, while we don't. +target_include_directories(MathFunctions + INTERFACE ${CMAKE_CURRENT_SOURCE_DIR} + ) + +# should we use our own math functions +option(USE_MYMATH "Use tutorial provided math implementation" ON) +if(USE_MYMATH) + + target_compile_definitions(MathFunctions PRIVATE "USE_MYMATH") + + include(MakeTable.cmake) # generates Table.h + + # library that just does sqrt + add_library(SqrtLibrary STATIC + mysqrt.cxx + ${CMAKE_CURRENT_BINARY_DIR}/Table.h + ) + + # state that we depend on our binary dir to find Table.h + target_include_directories(SqrtLibrary PRIVATE + ${CMAKE_CURRENT_BINARY_DIR} + ) + + # link SqrtLibrary to tutorial_compiler_flags + target_link_libraries(SqrtLibrary PUBLIC tutorial_compiler_flags) + + target_link_libraries(MathFunctions PRIVATE SqrtLibrary) +endif() + +# link MathFunctions to tutorial_compiler_flags +target_link_libraries(MathFunctions PUBLIC tutorial_compiler_flags) + +# install libs +set(installable_libs MathFunctions tutorial_compiler_flags) +if(TARGET SqrtLibrary) + list(APPEND installable_libs SqrtLibrary) +endif() +install(TARGETS ${installable_libs} DESTINATION lib) +# install include headers +install(FILES MathFunctions.h DESTINATION include) diff --git a/Step10/MathFunctions/MakeTable.cmake b/Step10/MathFunctions/MakeTable.cmake new file mode 100644 index 0000000..12865a9 --- /dev/null +++ b/Step10/MathFunctions/MakeTable.cmake @@ -0,0 +1,10 @@ +# first we add the executable that generates the table +add_executable(MakeTable MakeTable.cxx) +target_link_libraries(MakeTable PRIVATE tutorial_compiler_flags) + +# add the command to generate the source code +add_custom_command( + OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/Table.h + COMMAND MakeTable ${CMAKE_CURRENT_BINARY_DIR}/Table.h + DEPENDS MakeTable + ) diff --git a/Step10/MathFunctions/MakeTable.cxx b/Step10/MathFunctions/MakeTable.cxx new file mode 100644 index 0000000..ee58556 --- /dev/null +++ b/Step10/MathFunctions/MakeTable.cxx @@ -0,0 +1,25 @@ +// A simple program that builds a sqrt table +#include +#include +#include + +int main(int argc, char* argv[]) +{ + // make sure we have enough arguments + if (argc < 2) { + return 1; + } + + std::ofstream fout(argv[1], std::ios_base::out); + const bool fileOpen = fout.is_open(); + if (fileOpen) { + fout << "double sqrtTable[] = {" << std::endl; + for (int i = 0; i < 10; ++i) { + fout << sqrt(static_cast(i)) << "," << std::endl; + } + // close the table with a zero + fout << "0};" << std::endl; + fout.close(); + } + return fileOpen ? 0 : 1; // return 0 if wrote the file +} diff --git a/Step10/MathFunctions/MathFunctions.cxx b/Step10/MathFunctions/MathFunctions.cxx new file mode 100644 index 0000000..c0991b9 --- /dev/null +++ b/Step10/MathFunctions/MathFunctions.cxx @@ -0,0 +1,20 @@ + +#include "MathFunctions.h" + +#include + +#ifdef USE_MYMATH +# include "mysqrt.h" +#endif + +namespace mathfunctions { +double sqrt(double x) +{ +// which square root function should we use? +#ifdef USE_MYMATH + return detail::mysqrt(x); +#else + return std::sqrt(x); +#endif +} +} diff --git a/Step10/MathFunctions/MathFunctions.h b/Step10/MathFunctions/MathFunctions.h new file mode 100644 index 0000000..1e916e1 --- /dev/null +++ b/Step10/MathFunctions/MathFunctions.h @@ -0,0 +1,3 @@ +namespace mathfunctions { +double sqrt(double x); +} diff --git a/Step10/MathFunctions/mysqrt.cxx b/Step10/MathFunctions/mysqrt.cxx new file mode 100644 index 0000000..8153f18 --- /dev/null +++ b/Step10/MathFunctions/mysqrt.cxx @@ -0,0 +1,37 @@ +#include + +#include "MathFunctions.h" + +// include the generated table +#include "Table.h" + +namespace mathfunctions { +namespace detail { +// a hack square root calculation using simple operations +double mysqrt(double x) +{ + if (x <= 0) { + return 0; + } + + // use the table to help find an initial value + double result = x; + if (x >= 1 && x < 10) { + std::cout << "Use the table to help find an initial value " << std::endl; + result = sqrtTable[static_cast(x)]; + } + + // do ten iterations + for (int i = 0; i < 10; ++i) { + if (result <= 0) { + result = 0.1; + } + double delta = x - (result * result); + result = result + 0.5 * delta / result; + std::cout << "Computing sqrt of " << x << " to be " << result << std::endl; + } + + return result; +} +} +} diff --git a/Step10/MathFunctions/mysqrt.h b/Step10/MathFunctions/mysqrt.h new file mode 100644 index 0000000..e1c42ef --- /dev/null +++ b/Step10/MathFunctions/mysqrt.h @@ -0,0 +1,6 @@ + +namespace mathfunctions { +namespace detail { +double mysqrt(double x); +} +} diff --git a/Step10/TutorialConfig.h.in b/Step10/TutorialConfig.h.in new file mode 100644 index 0000000..7e4d7fa --- /dev/null +++ b/Step10/TutorialConfig.h.in @@ -0,0 +1,3 @@ +// the configured options and settings for Tutorial +#define Tutorial_VERSION_MAJOR @Tutorial_VERSION_MAJOR@ +#define Tutorial_VERSION_MINOR @Tutorial_VERSION_MINOR@ diff --git a/Step10/tutorial.cxx b/Step10/tutorial.cxx new file mode 100644 index 0000000..37a0333 --- /dev/null +++ b/Step10/tutorial.cxx @@ -0,0 +1,27 @@ +// A simple program that computes the square root of a number +#include +#include +#include + +#include "MathFunctions.h" +#include "TutorialConfig.h" + +int main(int argc, char* argv[]) +{ + if (argc < 2) { + // report version + std::cout << argv[0] << " Version " << Tutorial_VERSION_MAJOR << "." + << Tutorial_VERSION_MINOR << std::endl; + std::cout << "Usage: " << argv[0] << " number" << std::endl; + return 1; + } + + // convert input to double + const double inputValue = std::stod(argv[1]); + + const double outputValue = mathfunctions::sqrt(inputValue); + + std::cout << "The square root of " << inputValue << " is " << outputValue + << std::endl; + return 0; +} diff --git a/Step11/CMakeLists.txt b/Step11/CMakeLists.txt new file mode 100644 index 0000000..046bfc9 --- /dev/null +++ b/Step11/CMakeLists.txt @@ -0,0 +1,84 @@ +cmake_minimum_required(VERSION 3.15) + +# set the project name and version +project(Tutorial VERSION 1.0) + +# specify the C++ standard +add_library(tutorial_compiler_flags INTERFACE) +target_compile_features(tutorial_compiler_flags INTERFACE cxx_std_11) + +# add compiler warning flags just when building this project via +# the BUILD_INTERFACE genex +set(gcc_like_cxx "$") +set(msvc_cxx "$") +target_compile_options(tutorial_compiler_flags INTERFACE + "$<${gcc_like_cxx}:$>" + "$<${msvc_cxx}:$>" +) + +# control where the static and shared libraries are built so that on windows +# we don't need to tinker with the path to run the executable +set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}") +set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}") +set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}") + +option(BUILD_SHARED_LIBS "Build using shared libraries" ON) + +# configure a header file to pass the version number only +configure_file(TutorialConfig.h.in TutorialConfig.h) + +# add the MathFunctions library +add_subdirectory(MathFunctions) + +# add the executable +add_executable(Tutorial tutorial.cxx) +target_link_libraries(Tutorial PUBLIC MathFunctions tutorial_compiler_flags) + +# add the binary tree to the search path for include files +# so that we will find TutorialConfig.h +target_include_directories(Tutorial PUBLIC + "${PROJECT_BINARY_DIR}" + ) + +# add the install targets +install(TARGETS Tutorial DESTINATION bin) +install(FILES "${PROJECT_BINARY_DIR}/TutorialConfig.h" + DESTINATION include + ) + +# enable testing +include(CTest) + +# does the application run +add_test(NAME Runs COMMAND Tutorial 25) + +# does the usage message work? +add_test(NAME Usage COMMAND Tutorial) +set_tests_properties(Usage + PROPERTIES PASS_REGULAR_EXPRESSION "Usage:.*number" + ) + +# define a function to simplify adding tests +function(do_test target arg result) + add_test(NAME Comp${arg} COMMAND ${target} ${arg}) + set_tests_properties(Comp${arg} + PROPERTIES PASS_REGULAR_EXPRESSION ${result} + ) +endfunction() + +# do a bunch of result based tests +do_test(Tutorial 4 "4 is 2") +do_test(Tutorial 9 "9 is 3") +do_test(Tutorial 5 "5 is 2.236") +do_test(Tutorial 7 "7 is 2.645") +do_test(Tutorial 25 "25 is 5") +do_test(Tutorial -25 "-25 is (-nan|nan|0)") +do_test(Tutorial 0.0001 "0.0001 is 0.01") + +# setup installer +include(InstallRequiredSystemLibraries) +set(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_SOURCE_DIR}/License.txt") +set(CPACK_PACKAGE_VERSION_MAJOR "${Tutorial_VERSION_MAJOR}") +set(CPACK_PACKAGE_VERSION_MINOR "${Tutorial_VERSION_MINOR}") +set(CPACK_SOURCE_GENERATOR "TGZ") +include(CPack) diff --git a/Step11/CTestConfig.cmake b/Step11/CTestConfig.cmake new file mode 100644 index 0000000..73efdb1 --- /dev/null +++ b/Step11/CTestConfig.cmake @@ -0,0 +1,7 @@ +set(CTEST_PROJECT_NAME "CMakeTutorial") +set(CTEST_NIGHTLY_START_TIME "00:00:00 EST") + +set(CTEST_DROP_METHOD "http") +set(CTEST_DROP_SITE "my.cdash.org") +set(CTEST_DROP_LOCATION "/submit.php?project=CMakeTutorial") +set(CTEST_DROP_SITE_CDASH TRUE) diff --git a/Step11/License.txt b/Step11/License.txt new file mode 100644 index 0000000..85760e5 --- /dev/null +++ b/Step11/License.txt @@ -0,0 +1,2 @@ +This is the open source License.txt file introduced in +CMake/Tutorial/Step9... diff --git a/Step11/MathFunctions/CMakeLists.txt b/Step11/MathFunctions/CMakeLists.txt new file mode 100644 index 0000000..eacc538 --- /dev/null +++ b/Step11/MathFunctions/CMakeLists.txt @@ -0,0 +1,54 @@ +# add the library that runs +add_library(MathFunctions MathFunctions.cxx) + +# state that anybody linking to us needs to include the current source dir +# to find MathFunctions.h, while we don't. +target_include_directories(MathFunctions + INTERFACE ${CMAKE_CURRENT_SOURCE_DIR} + ) + +# should we use our own math functions +option(USE_MYMATH "Use tutorial provided math implementation" ON) +if(USE_MYMATH) + + target_compile_definitions(MathFunctions PRIVATE "USE_MYMATH") + + include(MakeTable.cmake) # generates Table.h + + # library that just does sqrt + add_library(SqrtLibrary STATIC + mysqrt.cxx + ${CMAKE_CURRENT_BINARY_DIR}/Table.h + ) + + # state that we depend on our binary dir to find Table.h + target_include_directories(SqrtLibrary PRIVATE + ${CMAKE_CURRENT_BINARY_DIR} + ) + + # state that SqrtLibrary need PIC when the default is shared libraries + set_target_properties(SqrtLibrary PROPERTIES + POSITION_INDEPENDENT_CODE ${BUILD_SHARED_LIBS} + ) + + # link SqrtLibrary to tutorial_compiler_flags + target_link_libraries(SqrtLibrary PUBLIC tutorial_compiler_flags) + + target_link_libraries(MathFunctions PRIVATE SqrtLibrary) +endif() + +# link MathFunctions to tutorial_compiler_flags +target_link_libraries(MathFunctions PUBLIC tutorial_compiler_flags) + +# define the symbol stating we are using the declspec(dllexport) when +# building on windows +target_compile_definitions(MathFunctions PRIVATE "EXPORTING_MYMATH") + +# install libs +set(installable_libs MathFunctions tutorial_compiler_flags) +if(TARGET SqrtLibrary) + list(APPEND installable_libs SqrtLibrary) +endif() +install(TARGETS ${installable_libs} DESTINATION lib) +# install include headers +install(FILES MathFunctions.h DESTINATION include) diff --git a/Step11/MathFunctions/MakeTable.cmake b/Step11/MathFunctions/MakeTable.cmake new file mode 100644 index 0000000..12865a9 --- /dev/null +++ b/Step11/MathFunctions/MakeTable.cmake @@ -0,0 +1,10 @@ +# first we add the executable that generates the table +add_executable(MakeTable MakeTable.cxx) +target_link_libraries(MakeTable PRIVATE tutorial_compiler_flags) + +# add the command to generate the source code +add_custom_command( + OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/Table.h + COMMAND MakeTable ${CMAKE_CURRENT_BINARY_DIR}/Table.h + DEPENDS MakeTable + ) diff --git a/Step11/MathFunctions/MakeTable.cxx b/Step11/MathFunctions/MakeTable.cxx new file mode 100644 index 0000000..ee58556 --- /dev/null +++ b/Step11/MathFunctions/MakeTable.cxx @@ -0,0 +1,25 @@ +// A simple program that builds a sqrt table +#include +#include +#include + +int main(int argc, char* argv[]) +{ + // make sure we have enough arguments + if (argc < 2) { + return 1; + } + + std::ofstream fout(argv[1], std::ios_base::out); + const bool fileOpen = fout.is_open(); + if (fileOpen) { + fout << "double sqrtTable[] = {" << std::endl; + for (int i = 0; i < 10; ++i) { + fout << sqrt(static_cast(i)) << "," << std::endl; + } + // close the table with a zero + fout << "0};" << std::endl; + fout.close(); + } + return fileOpen ? 0 : 1; // return 0 if wrote the file +} diff --git a/Step11/MathFunctions/MathFunctions.cxx b/Step11/MathFunctions/MathFunctions.cxx new file mode 100644 index 0000000..c0991b9 --- /dev/null +++ b/Step11/MathFunctions/MathFunctions.cxx @@ -0,0 +1,20 @@ + +#include "MathFunctions.h" + +#include + +#ifdef USE_MYMATH +# include "mysqrt.h" +#endif + +namespace mathfunctions { +double sqrt(double x) +{ +// which square root function should we use? +#ifdef USE_MYMATH + return detail::mysqrt(x); +#else + return std::sqrt(x); +#endif +} +} diff --git a/Step11/MathFunctions/MathFunctions.h b/Step11/MathFunctions/MathFunctions.h new file mode 100644 index 0000000..3fb547b --- /dev/null +++ b/Step11/MathFunctions/MathFunctions.h @@ -0,0 +1,14 @@ + +#if defined(_WIN32) +# if defined(EXPORTING_MYMATH) +# define DECLSPEC __declspec(dllexport) +# else +# define DECLSPEC __declspec(dllimport) +# endif +#else // non windows +# define DECLSPEC +#endif + +namespace mathfunctions { +double DECLSPEC sqrt(double x); +} diff --git a/Step11/MathFunctions/mysqrt.cxx b/Step11/MathFunctions/mysqrt.cxx new file mode 100644 index 0000000..8153f18 --- /dev/null +++ b/Step11/MathFunctions/mysqrt.cxx @@ -0,0 +1,37 @@ +#include + +#include "MathFunctions.h" + +// include the generated table +#include "Table.h" + +namespace mathfunctions { +namespace detail { +// a hack square root calculation using simple operations +double mysqrt(double x) +{ + if (x <= 0) { + return 0; + } + + // use the table to help find an initial value + double result = x; + if (x >= 1 && x < 10) { + std::cout << "Use the table to help find an initial value " << std::endl; + result = sqrtTable[static_cast(x)]; + } + + // do ten iterations + for (int i = 0; i < 10; ++i) { + if (result <= 0) { + result = 0.1; + } + double delta = x - (result * result); + result = result + 0.5 * delta / result; + std::cout << "Computing sqrt of " << x << " to be " << result << std::endl; + } + + return result; +} +} +} diff --git a/Step11/MathFunctions/mysqrt.h b/Step11/MathFunctions/mysqrt.h new file mode 100644 index 0000000..e1c42ef --- /dev/null +++ b/Step11/MathFunctions/mysqrt.h @@ -0,0 +1,6 @@ + +namespace mathfunctions { +namespace detail { +double mysqrt(double x); +} +} diff --git a/Step11/TutorialConfig.h.in b/Step11/TutorialConfig.h.in new file mode 100644 index 0000000..7e4d7fa --- /dev/null +++ b/Step11/TutorialConfig.h.in @@ -0,0 +1,3 @@ +// the configured options and settings for Tutorial +#define Tutorial_VERSION_MAJOR @Tutorial_VERSION_MAJOR@ +#define Tutorial_VERSION_MINOR @Tutorial_VERSION_MINOR@ diff --git a/Step11/tutorial.cxx b/Step11/tutorial.cxx new file mode 100644 index 0000000..37a0333 --- /dev/null +++ b/Step11/tutorial.cxx @@ -0,0 +1,27 @@ +// A simple program that computes the square root of a number +#include +#include +#include + +#include "MathFunctions.h" +#include "TutorialConfig.h" + +int main(int argc, char* argv[]) +{ + if (argc < 2) { + // report version + std::cout << argv[0] << " Version " << Tutorial_VERSION_MAJOR << "." + << Tutorial_VERSION_MINOR << std::endl; + std::cout << "Usage: " << argv[0] << " number" << std::endl; + return 1; + } + + // convert input to double + const double inputValue = std::stod(argv[1]); + + const double outputValue = mathfunctions::sqrt(inputValue); + + std::cout << "The square root of " << inputValue << " is " << outputValue + << std::endl; + return 0; +} diff --git a/Step12/CMakeLists.txt b/Step12/CMakeLists.txt new file mode 100644 index 0000000..1d8b5a6 --- /dev/null +++ b/Step12/CMakeLists.txt @@ -0,0 +1,122 @@ +cmake_minimum_required(VERSION 3.15) + +# set the project name and version +project(Tutorial VERSION 1.0) + +add_library(tutorial_compiler_flags INTERFACE) +target_compile_features(tutorial_compiler_flags INTERFACE cxx_std_11) + +# add compiler warning flags just when building this project via +# the BUILD_INTERFACE genex +set(gcc_like_cxx "$") +set(msvc_cxx "$") +target_compile_options(tutorial_compiler_flags INTERFACE + "$<${gcc_like_cxx}:$>" + "$<${msvc_cxx}:$>" +) + +# control where the static and shared libraries are built so that on windows +# we don't need to tinker with the path to run the executable +set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}") +set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}") +set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}") + +option(BUILD_SHARED_LIBS "Build using shared libraries" ON) + +if(APPLE) + set(CMAKE_INSTALL_RPATH "@executable_path/../lib") +elseif(UNIX) + set(CMAKE_INSTALL_RPATH "$ORIGIN/../lib") +endif() + +# configure a header file to pass the version number only +configure_file(TutorialConfig.h.in TutorialConfig.h) + +# add the MathFunctions library +add_subdirectory(MathFunctions) + +# add the executable +add_executable(Tutorial tutorial.cxx) +target_link_libraries(Tutorial PUBLIC MathFunctions tutorial_compiler_flags) + +# add the binary tree to the search path for include files +# so that we will find TutorialConfig.h +target_include_directories(Tutorial PUBLIC + "${PROJECT_BINARY_DIR}" + ) + +# add the install targets +install(TARGETS Tutorial DESTINATION bin) +install(FILES "${PROJECT_BINARY_DIR}/TutorialConfig.h" + DESTINATION include + ) + +# enable testing +enable_testing() + +# does the application run +add_test(NAME Runs COMMAND Tutorial 25) + +# does the usage message work? +add_test(NAME Usage COMMAND Tutorial) +set_tests_properties(Usage + PROPERTIES PASS_REGULAR_EXPRESSION "Usage:.*number" + ) + +# define a function to simplify adding tests +function(do_test target arg result) + add_test(NAME Comp${arg} COMMAND ${target} ${arg}) + set_tests_properties(Comp${arg} + PROPERTIES PASS_REGULAR_EXPRESSION ${result} + ) +endfunction() + +# do a bunch of result based tests +do_test(Tutorial 4 "4 is 2") +do_test(Tutorial 9 "9 is 3") +do_test(Tutorial 5 "5 is 2.236") +do_test(Tutorial 7 "7 is 2.645") +do_test(Tutorial 25 "25 is 5") +do_test(Tutorial -25 "-25 is (-nan|nan|0)") +do_test(Tutorial 0.0001 "0.0001 is 0.01") + +# setup installer +include(InstallRequiredSystemLibraries) +set(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_SOURCE_DIR}/License.txt") +set(CPACK_PACKAGE_VERSION_MAJOR "${Tutorial_VERSION_MAJOR}") +set(CPACK_PACKAGE_VERSION_MINOR "${Tutorial_VERSION_MINOR}") +include(CPack) + +# install the configuration targets +install(EXPORT MathFunctionsTargets + FILE MathFunctionsTargets.cmake + DESTINATION lib/cmake/MathFunctions +) + +include(CMakePackageConfigHelpers) +# generate the config file that includes the exports +configure_package_config_file(${CMAKE_CURRENT_SOURCE_DIR}/Config.cmake.in + "${CMAKE_CURRENT_BINARY_DIR}/MathFunctionsConfig.cmake" + INSTALL_DESTINATION "lib/cmake/example" + NO_SET_AND_CHECK_MACRO + NO_CHECK_REQUIRED_COMPONENTS_MACRO + ) +# generate the version file for the config file +write_basic_package_version_file( + "${CMAKE_CURRENT_BINARY_DIR}/MathFunctionsConfigVersion.cmake" + VERSION "${Tutorial_VERSION_MAJOR}.${Tutorial_VERSION_MINOR}" + COMPATIBILITY AnyNewerVersion +) + +# install the generated configuration files +install(FILES + ${CMAKE_CURRENT_BINARY_DIR}/MathFunctionsConfig.cmake + ${CMAKE_CURRENT_BINARY_DIR}/MathFunctionsConfigVersion.cmake + DESTINATION lib/cmake/MathFunctions + ) + +# generate the export targets for the build tree +# needs to be after the install(TARGETS) command +export(EXPORT MathFunctionsTargets + FILE "${CMAKE_CURRENT_BINARY_DIR}/MathFunctionsTargets.cmake" +) diff --git a/Step12/CTestConfig.cmake b/Step12/CTestConfig.cmake new file mode 100644 index 0000000..73efdb1 --- /dev/null +++ b/Step12/CTestConfig.cmake @@ -0,0 +1,7 @@ +set(CTEST_PROJECT_NAME "CMakeTutorial") +set(CTEST_NIGHTLY_START_TIME "00:00:00 EST") + +set(CTEST_DROP_METHOD "http") +set(CTEST_DROP_SITE "my.cdash.org") +set(CTEST_DROP_LOCATION "/submit.php?project=CMakeTutorial") +set(CTEST_DROP_SITE_CDASH TRUE) diff --git a/Step12/Config.cmake.in b/Step12/Config.cmake.in new file mode 100644 index 0000000..17cbabd --- /dev/null +++ b/Step12/Config.cmake.in @@ -0,0 +1,4 @@ + +@PACKAGE_INIT@ + +include ( "${CMAKE_CURRENT_LIST_DIR}/MathFunctionsTargets.cmake" ) diff --git a/Step12/License.txt b/Step12/License.txt new file mode 100644 index 0000000..85760e5 --- /dev/null +++ b/Step12/License.txt @@ -0,0 +1,2 @@ +This is the open source License.txt file introduced in +CMake/Tutorial/Step9... diff --git a/Step12/MathFunctions/CMakeLists.txt b/Step12/MathFunctions/CMakeLists.txt new file mode 100644 index 0000000..8aa5904 --- /dev/null +++ b/Step12/MathFunctions/CMakeLists.txt @@ -0,0 +1,58 @@ +# add the library that runs +add_library(MathFunctions MathFunctions.cxx) + +# state that anybody linking to us needs to include the current source dir +# to find MathFunctions.h, while we don't. +target_include_directories(MathFunctions + INTERFACE + $ + $ + ) + +# should we use our own math functions +option(USE_MYMATH "Use tutorial provided math implementation" ON) +if(USE_MYMATH) + + target_compile_definitions(MathFunctions PRIVATE "USE_MYMATH") + + include(MakeTable.cmake) # generates Table.h + + # library that just does sqrt + add_library(SqrtLibrary STATIC + mysqrt.cxx + ${CMAKE_CURRENT_BINARY_DIR}/Table.h + ) + + # state that we depend on our binary dir to find Table.h + target_include_directories(SqrtLibrary PRIVATE + ${CMAKE_CURRENT_BINARY_DIR} + ) + + # state that SqrtLibrary need PIC when the default is shared libraries + set_target_properties(SqrtLibrary PROPERTIES + POSITION_INDEPENDENT_CODE ${BUILD_SHARED_LIBS} + ) + + # link SqrtLibrary to tutorial_compiler_flags + target_link_libraries(SqrtLibrary PUBLIC tutorial_compiler_flags) + + target_link_libraries(MathFunctions PRIVATE SqrtLibrary) +endif() + +# link MathFunctions to tutorial_compiler_flags +target_link_libraries(MathFunctions PUBLIC tutorial_compiler_flags) + +# define the symbol stating we are using the declspec(dllexport) when +# building on windows +target_compile_definitions(MathFunctions PRIVATE "EXPORTING_MYMATH") + +# install libs +set(installable_libs MathFunctions tutorial_compiler_flags) +if(TARGET SqrtLibrary) + list(APPEND installable_libs SqrtLibrary) +endif() +install(TARGETS ${installable_libs} + EXPORT MathFunctionsTargets + DESTINATION lib) +# install include headers +install(FILES MathFunctions.h DESTINATION include) diff --git a/Step12/MathFunctions/MakeTable.cmake b/Step12/MathFunctions/MakeTable.cmake new file mode 100644 index 0000000..12865a9 --- /dev/null +++ b/Step12/MathFunctions/MakeTable.cmake @@ -0,0 +1,10 @@ +# first we add the executable that generates the table +add_executable(MakeTable MakeTable.cxx) +target_link_libraries(MakeTable PRIVATE tutorial_compiler_flags) + +# add the command to generate the source code +add_custom_command( + OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/Table.h + COMMAND MakeTable ${CMAKE_CURRENT_BINARY_DIR}/Table.h + DEPENDS MakeTable + ) diff --git a/Step12/MathFunctions/MakeTable.cxx b/Step12/MathFunctions/MakeTable.cxx new file mode 100644 index 0000000..ee58556 --- /dev/null +++ b/Step12/MathFunctions/MakeTable.cxx @@ -0,0 +1,25 @@ +// A simple program that builds a sqrt table +#include +#include +#include + +int main(int argc, char* argv[]) +{ + // make sure we have enough arguments + if (argc < 2) { + return 1; + } + + std::ofstream fout(argv[1], std::ios_base::out); + const bool fileOpen = fout.is_open(); + if (fileOpen) { + fout << "double sqrtTable[] = {" << std::endl; + for (int i = 0; i < 10; ++i) { + fout << sqrt(static_cast(i)) << "," << std::endl; + } + // close the table with a zero + fout << "0};" << std::endl; + fout.close(); + } + return fileOpen ? 0 : 1; // return 0 if wrote the file +} diff --git a/Step12/MathFunctions/MathFunctions.cxx b/Step12/MathFunctions/MathFunctions.cxx new file mode 100644 index 0000000..c0991b9 --- /dev/null +++ b/Step12/MathFunctions/MathFunctions.cxx @@ -0,0 +1,20 @@ + +#include "MathFunctions.h" + +#include + +#ifdef USE_MYMATH +# include "mysqrt.h" +#endif + +namespace mathfunctions { +double sqrt(double x) +{ +// which square root function should we use? +#ifdef USE_MYMATH + return detail::mysqrt(x); +#else + return std::sqrt(x); +#endif +} +} diff --git a/Step12/MathFunctions/MathFunctions.h b/Step12/MathFunctions/MathFunctions.h new file mode 100644 index 0000000..3fb547b --- /dev/null +++ b/Step12/MathFunctions/MathFunctions.h @@ -0,0 +1,14 @@ + +#if defined(_WIN32) +# if defined(EXPORTING_MYMATH) +# define DECLSPEC __declspec(dllexport) +# else +# define DECLSPEC __declspec(dllimport) +# endif +#else // non windows +# define DECLSPEC +#endif + +namespace mathfunctions { +double DECLSPEC sqrt(double x); +} diff --git a/Step12/MathFunctions/mysqrt.cxx b/Step12/MathFunctions/mysqrt.cxx new file mode 100644 index 0000000..8153f18 --- /dev/null +++ b/Step12/MathFunctions/mysqrt.cxx @@ -0,0 +1,37 @@ +#include + +#include "MathFunctions.h" + +// include the generated table +#include "Table.h" + +namespace mathfunctions { +namespace detail { +// a hack square root calculation using simple operations +double mysqrt(double x) +{ + if (x <= 0) { + return 0; + } + + // use the table to help find an initial value + double result = x; + if (x >= 1 && x < 10) { + std::cout << "Use the table to help find an initial value " << std::endl; + result = sqrtTable[static_cast(x)]; + } + + // do ten iterations + for (int i = 0; i < 10; ++i) { + if (result <= 0) { + result = 0.1; + } + double delta = x - (result * result); + result = result + 0.5 * delta / result; + std::cout << "Computing sqrt of " << x << " to be " << result << std::endl; + } + + return result; +} +} +} diff --git a/Step12/MathFunctions/mysqrt.h b/Step12/MathFunctions/mysqrt.h new file mode 100644 index 0000000..e1c42ef --- /dev/null +++ b/Step12/MathFunctions/mysqrt.h @@ -0,0 +1,6 @@ + +namespace mathfunctions { +namespace detail { +double mysqrt(double x); +} +} diff --git a/Step12/TutorialConfig.h.in b/Step12/TutorialConfig.h.in new file mode 100644 index 0000000..7e4d7fa --- /dev/null +++ b/Step12/TutorialConfig.h.in @@ -0,0 +1,3 @@ +// the configured options and settings for Tutorial +#define Tutorial_VERSION_MAJOR @Tutorial_VERSION_MAJOR@ +#define Tutorial_VERSION_MINOR @Tutorial_VERSION_MINOR@ diff --git a/Step12/tutorial.cxx b/Step12/tutorial.cxx new file mode 100644 index 0000000..a4f44d5 --- /dev/null +++ b/Step12/tutorial.cxx @@ -0,0 +1,26 @@ +// A simple program that computes the square root of a number +#include +#include + +#include "MathFunctions.h" +#include "TutorialConfig.h" + +int main(int argc, char* argv[]) +{ + if (argc < 2) { + // report version + std::cout << argv[0] << " Version " << Tutorial_VERSION_MAJOR << "." + << Tutorial_VERSION_MINOR << std::endl; + std::cout << "Usage: " << argv[0] << " number" << std::endl; + return 1; + } + + // convert input to double + const double inputValue = std::stod(argv[1]); + + const double outputValue = mathfunctions::sqrt(inputValue); + + std::cout << "The square root of " << inputValue << " is " << outputValue + << std::endl; + return 0; +} diff --git a/Step2/CMakeLists.txt b/Step2/CMakeLists.txt new file mode 100644 index 0000000..0a06ed7 --- /dev/null +++ b/Step2/CMakeLists.txt @@ -0,0 +1,28 @@ +cmake_minimum_required(VERSION 3.10) + +# set the project name and version +project(Tutorial VERSION 1.0) + +# specify the C++ standard +set(CMAKE_CXX_STANDARD 11) +set(CMAKE_CXX_STANDARD_REQUIRED True) + +# configure a header file to pass some of the CMake settings +# to the source code +configure_file(TutorialConfig.h.in TutorialConfig.h) + +# TODO 2: Use add_subdirectory() to add MathFunctions to this project + +# add the executable +add_executable(Tutorial tutorial.cxx) + +# TODO 3: Use target_link_libraries to link the library to our executable + +# TODO 4: Add MathFunctions to Tutorial's target_include_directories() +# Hint: ${PROJECT_SOURCE_DIR} is a path to the project source. AKA This folder! + +# add the binary tree to the search path for include files +# so that we will find TutorialConfig.h +target_include_directories(Tutorial PUBLIC + "${PROJECT_BINARY_DIR}" + ) diff --git a/Step2/MathFunctions/CMakeLists.txt b/Step2/MathFunctions/CMakeLists.txt new file mode 100644 index 0000000..c3cd806 --- /dev/null +++ b/Step2/MathFunctions/CMakeLists.txt @@ -0,0 +1,15 @@ +# TODO 14: Remove mysqrt.cxx from the list of sources + +# TODO 1: Add a library called MathFunctions with sources MathFunctions.cxx +# and mysqrt.cxx +# Hint: You will need the add_library command + +# TODO 7: Create a variable USE_MYMATH using option and set default to ON + +# TODO 8: If USE_MYMATH is ON, use target_compile_definitions to pass +# USE_MYMATH as a precompiled definition to our source files + +# TODO 12: When USE_MYMATH is ON, add a library for SqrtLibrary with +# source mysqrt.cxx + +# TODO 13: When USE_MYMATH is ON, link SqrtLibrary to the MathFunctions Library diff --git a/Step2/MathFunctions/MathFunctions.cxx b/Step2/MathFunctions/MathFunctions.cxx new file mode 100644 index 0000000..781d0ec --- /dev/null +++ b/Step2/MathFunctions/MathFunctions.cxx @@ -0,0 +1,15 @@ +#include "MathFunctions.h" + +// TODO 11: include cmath + +// TODO 10: Wrap the mysqrt include in a precompiled ifdef based on USE_MYMATH +#include "mysqrt.h" + +namespace mathfunctions { +double sqrt(double x) +{ + // TODO 9: If USE_MYMATH is defined, use detail::mysqrt. + // Otherwise, use std::sqrt. + return detail::mysqrt(x); +} +} diff --git a/Step2/MathFunctions/MathFunctions.h b/Step2/MathFunctions/MathFunctions.h new file mode 100644 index 0000000..d5c2f22 --- /dev/null +++ b/Step2/MathFunctions/MathFunctions.h @@ -0,0 +1,5 @@ +#pragma once + +namespace mathfunctions { +double sqrt(double x); +} diff --git a/Step2/MathFunctions/mysqrt.cxx b/Step2/MathFunctions/mysqrt.cxx new file mode 100644 index 0000000..ba0ac64 --- /dev/null +++ b/Step2/MathFunctions/mysqrt.cxx @@ -0,0 +1,28 @@ +#include "mysqrt.h" + +#include + +namespace mathfunctions { +namespace detail { +// a hack square root calculation using simple operations +double mysqrt(double x) +{ + if (x <= 0) { + return 0; + } + + double result = x; + + // do ten iterations + for (int i = 0; i < 10; ++i) { + if (result <= 0) { + result = 0.1; + } + double delta = x - (result * result); + result = result + 0.5 * delta / result; + std::cout << "Computing sqrt of " << x << " to be " << result << std::endl; + } + return result; +} +} +} diff --git a/Step2/MathFunctions/mysqrt.h b/Step2/MathFunctions/mysqrt.h new file mode 100644 index 0000000..593d41e --- /dev/null +++ b/Step2/MathFunctions/mysqrt.h @@ -0,0 +1,7 @@ +#pragma once + +namespace mathfunctions { +namespace detail { +double mysqrt(double x); +} +} diff --git a/Step2/TutorialConfig.h.in b/Step2/TutorialConfig.h.in new file mode 100644 index 0000000..7e4d7fa --- /dev/null +++ b/Step2/TutorialConfig.h.in @@ -0,0 +1,3 @@ +// the configured options and settings for Tutorial +#define Tutorial_VERSION_MAJOR @Tutorial_VERSION_MAJOR@ +#define Tutorial_VERSION_MINOR @Tutorial_VERSION_MINOR@ diff --git a/Step2/tutorial.cxx b/Step2/tutorial.cxx new file mode 100644 index 0000000..7a2a595 --- /dev/null +++ b/Step2/tutorial.cxx @@ -0,0 +1,29 @@ +// A simple program that computes the square root of a number +#include +#include +#include + +// TODO 5: Include MathFunctions.h +#include "TutorialConfig.h" + +int main(int argc, char* argv[]) +{ + if (argc < 2) { + // report version + std::cout << argv[0] << " Version " << Tutorial_VERSION_MAJOR << "." + << Tutorial_VERSION_MINOR << std::endl; + std::cout << "Usage: " << argv[0] << " number" << std::endl; + return 1; + } + + // convert input to double + const double inputValue = std::stod(argv[1]); + + // TODO 6: Replace sqrt with mathfunctions::sqrt + + // calculate square root + const double outputValue = sqrt(inputValue); + std::cout << "The square root of " << inputValue << " is " << outputValue + << std::endl; + return 0; +} diff --git a/Step3/CMakeLists.txt b/Step3/CMakeLists.txt new file mode 100644 index 0000000..ac3e9f1 --- /dev/null +++ b/Step3/CMakeLists.txt @@ -0,0 +1,40 @@ +cmake_minimum_required(VERSION 3.10) + +# set the project name and version +project(Tutorial VERSION 1.0) + +# TODO 4: Replace the following code by: +# * Creating an interface library called tutorial_compiler_flags +# Hint: use add_library() with the INTERFACE signature +# * Add compiler feature cxx_std_11 to tutorial_compiler_flags +# Hint: Use target_compile_features() + +# specify the C++ standard +set(CMAKE_CXX_STANDARD 11) +set(CMAKE_CXX_STANDARD_REQUIRED True) + +# configure a header file to pass some of the CMake settings +# to the source code +configure_file(TutorialConfig.h.in TutorialConfig.h) + +# TODO 2: Remove EXTRA_INCLUDES list + +# add the MathFunctions library +add_subdirectory(MathFunctions) +list(APPEND EXTRA_INCLUDES "${PROJECT_SOURCE_DIR}/MathFunctions") + +# add the executable +add_executable(Tutorial tutorial.cxx) + +# TODO 5: Link Tutorial to tutorial_compiler_flags + +target_link_libraries(Tutorial PUBLIC MathFunctions) + +# TODO 3: Remove use of EXTRA_INCLUDES + +# add the binary tree to the search path for include files +# so that we will find TutorialConfig.h +target_include_directories(Tutorial PUBLIC + "${PROJECT_BINARY_DIR}" + ${EXTRA_INCLUDES} + ) diff --git a/Step3/MathFunctions/CMakeLists.txt b/Step3/MathFunctions/CMakeLists.txt new file mode 100644 index 0000000..ffb2f35 --- /dev/null +++ b/Step3/MathFunctions/CMakeLists.txt @@ -0,0 +1,22 @@ +add_library(MathFunctions MathFunctions.cxx) + +# TODO 1: State that anybody linking to MathFunctions needs to include the +# current source directory, while MathFunctions itself doesn't. +# Hint: Use target_include_directories with the INTERFACE keyword + +# should we use our own math functions +option(USE_MYMATH "Use tutorial provided math implementation" ON) +if (USE_MYMATH) + target_compile_definitions(MathFunctions PRIVATE "USE_MYMATH") + + # library that just does sqrt + add_library(SqrtLibrary STATIC + mysqrt.cxx + ) + + # TODO 7: Link SqrtLibrary to tutorial_compiler_flags + + target_link_libraries(MathFunctions PRIVATE SqrtLibrary) +endif() + +# TODO 6: Link MathFunctions to tutorial_compiler_flags diff --git a/Step3/MathFunctions/MathFunctions.cxx b/Step3/MathFunctions/MathFunctions.cxx new file mode 100644 index 0000000..dc28b4b --- /dev/null +++ b/Step3/MathFunctions/MathFunctions.cxx @@ -0,0 +1,19 @@ +#include "MathFunctions.h" + +#include + +#ifdef USE_MYMATH +# include "mysqrt.h" +#endif + +namespace mathfunctions { +double sqrt(double x) +{ +// which square root function should we use? +#ifdef USE_MYMATH + return detail::mysqrt(x); +#else + return std::sqrt(x); +#endif +} +} diff --git a/Step3/MathFunctions/MathFunctions.h b/Step3/MathFunctions/MathFunctions.h new file mode 100644 index 0000000..d5c2f22 --- /dev/null +++ b/Step3/MathFunctions/MathFunctions.h @@ -0,0 +1,5 @@ +#pragma once + +namespace mathfunctions { +double sqrt(double x); +} diff --git a/Step3/MathFunctions/mysqrt.cxx b/Step3/MathFunctions/mysqrt.cxx new file mode 100644 index 0000000..ba0ac64 --- /dev/null +++ b/Step3/MathFunctions/mysqrt.cxx @@ -0,0 +1,28 @@ +#include "mysqrt.h" + +#include + +namespace mathfunctions { +namespace detail { +// a hack square root calculation using simple operations +double mysqrt(double x) +{ + if (x <= 0) { + return 0; + } + + double result = x; + + // do ten iterations + for (int i = 0; i < 10; ++i) { + if (result <= 0) { + result = 0.1; + } + double delta = x - (result * result); + result = result + 0.5 * delta / result; + std::cout << "Computing sqrt of " << x << " to be " << result << std::endl; + } + return result; +} +} +} diff --git a/Step3/MathFunctions/mysqrt.h b/Step3/MathFunctions/mysqrt.h new file mode 100644 index 0000000..593d41e --- /dev/null +++ b/Step3/MathFunctions/mysqrt.h @@ -0,0 +1,7 @@ +#pragma once + +namespace mathfunctions { +namespace detail { +double mysqrt(double x); +} +} diff --git a/Step3/TutorialConfig.h.in b/Step3/TutorialConfig.h.in new file mode 100644 index 0000000..7e4d7fa --- /dev/null +++ b/Step3/TutorialConfig.h.in @@ -0,0 +1,3 @@ +// the configured options and settings for Tutorial +#define Tutorial_VERSION_MAJOR @Tutorial_VERSION_MAJOR@ +#define Tutorial_VERSION_MINOR @Tutorial_VERSION_MINOR@ diff --git a/Step3/tutorial.cxx b/Step3/tutorial.cxx new file mode 100644 index 0000000..a3a2bdc --- /dev/null +++ b/Step3/tutorial.cxx @@ -0,0 +1,27 @@ +// A simple program that computes the square root of a number +#include +#include +#include + +#include "MathFunctions.h" +#include "TutorialConfig.h" + +int main(int argc, char* argv[]) +{ + if (argc < 2) { + // report version + std::cout << argv[0] << " Version " << Tutorial_VERSION_MAJOR << "." + << Tutorial_VERSION_MINOR << std::endl; + std::cout << "Usage: " << argv[0] << " number" << std::endl; + return 1; + } + + // convert input to double + const double inputValue = std::stod(argv[1]); + + const double outputValue = mathfunctions::sqrt(inputValue); + + std::cout << "The square root of " << inputValue << " is " << outputValue + << std::endl; + return 0; +} diff --git a/Step4/CMakeLists.txt b/Step4/CMakeLists.txt new file mode 100644 index 0000000..fba9766 --- /dev/null +++ b/Step4/CMakeLists.txt @@ -0,0 +1,44 @@ +# TODO 1: Update the minimum required version to 3.15 + +cmake_minimum_required(VERSION 3.10) + +# set the project name and version +project(Tutorial VERSION 1.0) + +# specify the C++ standard +add_library(tutorial_compiler_flags INTERFACE) +target_compile_features(tutorial_compiler_flags INTERFACE cxx_std_11) + +# TODO 2: Create helper variables to determine which compiler we are using: +# * Create a new variable gcc_like_cxx that is true if we are using CXX and +# any of the following compilers: ARMClang, AppleClang, Clang, GNU, LCC +# * Create a new variable msvc_cxx that is true if we are using CXX and MSVC +# Hint: Use set() and COMPILE_LANG_AND_ID + +# TODO 3: Add warning flag compile options to the interface library +# tutorial_compiler_flags. +# * For gcc_like_cxx, add flags -Wall;-Wextra;-Wshadow;-Wformat=2;-Wunused +# * For msvc_cxx, add flags -W3 +# Hint: Use target_compile_options() + +# TODO 4: With nested generator expressions, only use the flags for the +# build-tree +# Hint: Use BUILD_INTERFACE + +# configure a header file to pass some of the CMake settings +# to the source code +configure_file(TutorialConfig.h.in TutorialConfig.h) + +# add the MathFunctions library +add_subdirectory(MathFunctions) + +# add the executable +add_executable(Tutorial tutorial.cxx) + +target_link_libraries(Tutorial PUBLIC MathFunctions tutorial_compiler_flags) + +# add the binary tree to the search path for include files +# so that we will find TutorialConfig.h +target_include_directories(Tutorial PUBLIC + "${PROJECT_BINARY_DIR}" + ) diff --git a/Step4/MathFunctions/CMakeLists.txt b/Step4/MathFunctions/CMakeLists.txt new file mode 100644 index 0000000..6931898 --- /dev/null +++ b/Step4/MathFunctions/CMakeLists.txt @@ -0,0 +1,27 @@ +# create the MathFunctions library +add_library(MathFunctions MathFunctions.cxx) + +# state that anybody linking to us needs to include the current source dir +# to find MathFunctions.h, while we don't. +target_include_directories(MathFunctions + INTERFACE ${CMAKE_CURRENT_SOURCE_DIR} + ) + +# should we use our own math functions +option(USE_MYMATH "Use tutorial provided math implementation" ON) +if (USE_MYMATH) + target_compile_definitions(MathFunctions PRIVATE "USE_MYMATH") + + # library that just does sqrt + add_library(SqrtLibrary STATIC + mysqrt.cxx + ) + + # link SqrtLibrary to tutorial_compiler_flags + target_link_libraries(SqrtLibrary PUBLIC tutorial_compiler_flags) + + target_link_libraries(MathFunctions PRIVATE SqrtLibrary) +endif() + +# link MathFunctions to tutorial_compiler_flags +target_link_libraries(MathFunctions PUBLIC tutorial_compiler_flags) diff --git a/Step4/MathFunctions/MathFunctions.cxx b/Step4/MathFunctions/MathFunctions.cxx new file mode 100644 index 0000000..dc28b4b --- /dev/null +++ b/Step4/MathFunctions/MathFunctions.cxx @@ -0,0 +1,19 @@ +#include "MathFunctions.h" + +#include + +#ifdef USE_MYMATH +# include "mysqrt.h" +#endif + +namespace mathfunctions { +double sqrt(double x) +{ +// which square root function should we use? +#ifdef USE_MYMATH + return detail::mysqrt(x); +#else + return std::sqrt(x); +#endif +} +} diff --git a/Step4/MathFunctions/MathFunctions.h b/Step4/MathFunctions/MathFunctions.h new file mode 100644 index 0000000..d5c2f22 --- /dev/null +++ b/Step4/MathFunctions/MathFunctions.h @@ -0,0 +1,5 @@ +#pragma once + +namespace mathfunctions { +double sqrt(double x); +} diff --git a/Step4/MathFunctions/mysqrt.cxx b/Step4/MathFunctions/mysqrt.cxx new file mode 100644 index 0000000..ba0ac64 --- /dev/null +++ b/Step4/MathFunctions/mysqrt.cxx @@ -0,0 +1,28 @@ +#include "mysqrt.h" + +#include + +namespace mathfunctions { +namespace detail { +// a hack square root calculation using simple operations +double mysqrt(double x) +{ + if (x <= 0) { + return 0; + } + + double result = x; + + // do ten iterations + for (int i = 0; i < 10; ++i) { + if (result <= 0) { + result = 0.1; + } + double delta = x - (result * result); + result = result + 0.5 * delta / result; + std::cout << "Computing sqrt of " << x << " to be " << result << std::endl; + } + return result; +} +} +} diff --git a/Step4/MathFunctions/mysqrt.h b/Step4/MathFunctions/mysqrt.h new file mode 100644 index 0000000..593d41e --- /dev/null +++ b/Step4/MathFunctions/mysqrt.h @@ -0,0 +1,7 @@ +#pragma once + +namespace mathfunctions { +namespace detail { +double mysqrt(double x); +} +} diff --git a/Step4/TutorialConfig.h.in b/Step4/TutorialConfig.h.in new file mode 100644 index 0000000..7e4d7fa --- /dev/null +++ b/Step4/TutorialConfig.h.in @@ -0,0 +1,3 @@ +// the configured options and settings for Tutorial +#define Tutorial_VERSION_MAJOR @Tutorial_VERSION_MAJOR@ +#define Tutorial_VERSION_MINOR @Tutorial_VERSION_MINOR@ diff --git a/Step4/tutorial.cxx b/Step4/tutorial.cxx new file mode 100644 index 0000000..a3a2bdc --- /dev/null +++ b/Step4/tutorial.cxx @@ -0,0 +1,27 @@ +// A simple program that computes the square root of a number +#include +#include +#include + +#include "MathFunctions.h" +#include "TutorialConfig.h" + +int main(int argc, char* argv[]) +{ + if (argc < 2) { + // report version + std::cout << argv[0] << " Version " << Tutorial_VERSION_MAJOR << "." + << Tutorial_VERSION_MINOR << std::endl; + std::cout << "Usage: " << argv[0] << " number" << std::endl; + return 1; + } + + // convert input to double + const double inputValue = std::stod(argv[1]); + + const double outputValue = mathfunctions::sqrt(inputValue); + + std::cout << "The square root of " << inputValue << " is " << outputValue + << std::endl; + return 0; +} diff --git a/Step5/CMakeLists.txt b/Step5/CMakeLists.txt new file mode 100644 index 0000000..ad814f6 --- /dev/null +++ b/Step5/CMakeLists.txt @@ -0,0 +1,59 @@ +cmake_minimum_required(VERSION 3.15) + +# set the project name and version +project(Tutorial VERSION 1.0) + +# specify the C++ standard +add_library(tutorial_compiler_flags INTERFACE) +target_compile_features(tutorial_compiler_flags INTERFACE cxx_std_11) + +# add compiler warning flags just when building this project via +# the BUILD_INTERFACE genex +set(gcc_like_cxx "$") +set(msvc_cxx "$") +target_compile_options(tutorial_compiler_flags INTERFACE + "$<${gcc_like_cxx}:$>" + "$<${msvc_cxx}:$>" +) + +# configure a header file to pass some of the CMake settings +# to the source code +configure_file(TutorialConfig.h.in TutorialConfig.h) + +# add the MathFunctions library +add_subdirectory(MathFunctions) + +# add the executable +add_executable(Tutorial tutorial.cxx) + +target_link_libraries(Tutorial PUBLIC MathFunctions tutorial_compiler_flags) + +# add the binary tree to the search path for include files +# so that we will find TutorialConfig.h +target_include_directories(Tutorial PUBLIC + "${PROJECT_BINARY_DIR}" + ) + +# TODO 3: Install Tutorial in the bin directory +# Hint: Use the TARGETS and DESTINATION parameters + +# TODO 4: Install TutorialConfig.h to the include directory +# Hint: Use the FILES and DESTINATION parameters + +# TODO 5: Enable testing + +# TODO 6: Add a test called Runs which runs the following command: +# $ Tutorial 25 + +# TODO 7: Add a test called Usage which runs the following command: +# $ Tutorial +# Make sure the expected output is displayed. +# Hint: Use the PASS_REGULAR_EXPRESSION property with "Usage.*number" + +# TODO 8: Add a test which runs the following command: +# $ Tutorial 4 +# Make sure the result is correct. +# Hint: Use the PASS_REGULAR_EXPRESSION property with "4 is 2" + +# TODO 9: Add more tests. Create a function called do_test to avoid copy + +# paste. Test the following values: 4, 9, 5, 7, 25, -25 and 0.0001. diff --git a/Step5/MathFunctions/CMakeLists.txt b/Step5/MathFunctions/CMakeLists.txt new file mode 100644 index 0000000..61b3899 --- /dev/null +++ b/Step5/MathFunctions/CMakeLists.txt @@ -0,0 +1,34 @@ +add_library(MathFunctions MathFunctions.cxx) + +# state that anybody linking to us needs to include the current source dir +# to find MathFunctions.h, while we don't. +target_include_directories(MathFunctions + INTERFACE ${CMAKE_CURRENT_SOURCE_DIR} + ) + +# should we use our own math functions +option(USE_MYMATH "Use tutorial provided math implementation" ON) +if (USE_MYMATH) + target_compile_definitions(MathFunctions PRIVATE "USE_MYMATH") + + # library that just does sqrt + add_library(SqrtLibrary STATIC + mysqrt.cxx + ) + + # link SqrtLibrary to tutorial_compiler_flags + target_link_libraries(SqrtLibrary PUBLIC tutorial_compiler_flags) + + target_link_libraries(MathFunctions PRIVATE SqrtLibrary) +endif() + +# link MathFunctions to tutorial_compiler_flags +target_link_libraries(MathFunctions PUBLIC tutorial_compiler_flags) + +# TODO 1: Create a variable called installable_libs that is a list of all +# libraries we want to install (e.g. MathFunctions and tutorial_compiler_flags) +# Then install the installable libraries to the lib folder. +# Hint: Use the TARGETS and DESTINATION parameters + +# TODO 2: Install the library headers to the include folder. +# Hint: Use the FILES and DESTINATION parameters diff --git a/Step5/MathFunctions/MathFunctions.cxx b/Step5/MathFunctions/MathFunctions.cxx new file mode 100644 index 0000000..dc28b4b --- /dev/null +++ b/Step5/MathFunctions/MathFunctions.cxx @@ -0,0 +1,19 @@ +#include "MathFunctions.h" + +#include + +#ifdef USE_MYMATH +# include "mysqrt.h" +#endif + +namespace mathfunctions { +double sqrt(double x) +{ +// which square root function should we use? +#ifdef USE_MYMATH + return detail::mysqrt(x); +#else + return std::sqrt(x); +#endif +} +} diff --git a/Step5/MathFunctions/MathFunctions.h b/Step5/MathFunctions/MathFunctions.h new file mode 100644 index 0000000..d5c2f22 --- /dev/null +++ b/Step5/MathFunctions/MathFunctions.h @@ -0,0 +1,5 @@ +#pragma once + +namespace mathfunctions { +double sqrt(double x); +} diff --git a/Step5/MathFunctions/mysqrt.cxx b/Step5/MathFunctions/mysqrt.cxx new file mode 100644 index 0000000..ba0ac64 --- /dev/null +++ b/Step5/MathFunctions/mysqrt.cxx @@ -0,0 +1,28 @@ +#include "mysqrt.h" + +#include + +namespace mathfunctions { +namespace detail { +// a hack square root calculation using simple operations +double mysqrt(double x) +{ + if (x <= 0) { + return 0; + } + + double result = x; + + // do ten iterations + for (int i = 0; i < 10; ++i) { + if (result <= 0) { + result = 0.1; + } + double delta = x - (result * result); + result = result + 0.5 * delta / result; + std::cout << "Computing sqrt of " << x << " to be " << result << std::endl; + } + return result; +} +} +} diff --git a/Step5/MathFunctions/mysqrt.h b/Step5/MathFunctions/mysqrt.h new file mode 100644 index 0000000..593d41e --- /dev/null +++ b/Step5/MathFunctions/mysqrt.h @@ -0,0 +1,7 @@ +#pragma once + +namespace mathfunctions { +namespace detail { +double mysqrt(double x); +} +} diff --git a/Step5/TutorialConfig.h.in b/Step5/TutorialConfig.h.in new file mode 100644 index 0000000..7e4d7fa --- /dev/null +++ b/Step5/TutorialConfig.h.in @@ -0,0 +1,3 @@ +// the configured options and settings for Tutorial +#define Tutorial_VERSION_MAJOR @Tutorial_VERSION_MAJOR@ +#define Tutorial_VERSION_MINOR @Tutorial_VERSION_MINOR@ diff --git a/Step5/tutorial.cxx b/Step5/tutorial.cxx new file mode 100644 index 0000000..a3a2bdc --- /dev/null +++ b/Step5/tutorial.cxx @@ -0,0 +1,27 @@ +// A simple program that computes the square root of a number +#include +#include +#include + +#include "MathFunctions.h" +#include "TutorialConfig.h" + +int main(int argc, char* argv[]) +{ + if (argc < 2) { + // report version + std::cout << argv[0] << " Version " << Tutorial_VERSION_MAJOR << "." + << Tutorial_VERSION_MINOR << std::endl; + std::cout << "Usage: " << argv[0] << " number" << std::endl; + return 1; + } + + // convert input to double + const double inputValue = std::stod(argv[1]); + + const double outputValue = mathfunctions::sqrt(inputValue); + + std::cout << "The square root of " << inputValue << " is " << outputValue + << std::endl; + return 0; +} diff --git a/Step6/CMakeLists.txt b/Step6/CMakeLists.txt new file mode 100644 index 0000000..a86d60a --- /dev/null +++ b/Step6/CMakeLists.txt @@ -0,0 +1,71 @@ +cmake_minimum_required(VERSION 3.15) + +# set the project name and version +project(Tutorial VERSION 1.0) + +# specify the C++ standard +add_library(tutorial_compiler_flags INTERFACE) +target_compile_features(tutorial_compiler_flags INTERFACE cxx_std_11) + +# add compiler warning flags just when building this project via +# the BUILD_INTERFACE genex +set(gcc_like_cxx "$") +set(msvc_cxx "$") +target_compile_options(tutorial_compiler_flags INTERFACE + "$<${gcc_like_cxx}:$>" + "$<${msvc_cxx}:$>" +) + +# configure a header file to pass some of the CMake settings +# to the source code +configure_file(TutorialConfig.h.in TutorialConfig.h) + +# add the MathFunctions library +add_subdirectory(MathFunctions) + +# add the executable +add_executable(Tutorial tutorial.cxx) + +target_link_libraries(Tutorial PUBLIC MathFunctions tutorial_compiler_flags) + +# add the binary tree to the search path for include files +# so that we will find TutorialConfig.h +target_include_directories(Tutorial PUBLIC + "${PROJECT_BINARY_DIR}" + ) + +# add the install targets +install(TARGETS Tutorial DESTINATION bin) +install(FILES "${PROJECT_BINARY_DIR}/TutorialConfig.h" + DESTINATION include + ) + +# TODO 1: Replace enable_testing() with include(CTest) +# enable testing +enable_testing() + +# does the application run +add_test(NAME Runs COMMAND Tutorial 25) + +# does the usage message work? +add_test(NAME Usage COMMAND Tutorial) +set_tests_properties(Usage + PROPERTIES PASS_REGULAR_EXPRESSION "Usage:.*number" + ) + +# define a function to simplify adding tests +function(do_test target arg result) + add_test(NAME Comp${arg} COMMAND ${target} ${arg}) + set_tests_properties(Comp${arg} + PROPERTIES PASS_REGULAR_EXPRESSION ${result} + ) +endfunction() + +# do a bunch of result based tests +do_test(Tutorial 4 "4 is 2") +do_test(Tutorial 9 "9 is 3") +do_test(Tutorial 5 "5 is 2.236") +do_test(Tutorial 7 "7 is 2.645") +do_test(Tutorial 25 "25 is 5") +do_test(Tutorial -25 "-25 is (-nan|nan|0)") +do_test(Tutorial 0.0001 "0.0001 is 0.01") diff --git a/Step6/CTestConfig.cmake b/Step6/CTestConfig.cmake new file mode 100644 index 0000000..73efdb1 --- /dev/null +++ b/Step6/CTestConfig.cmake @@ -0,0 +1,7 @@ +set(CTEST_PROJECT_NAME "CMakeTutorial") +set(CTEST_NIGHTLY_START_TIME "00:00:00 EST") + +set(CTEST_DROP_METHOD "http") +set(CTEST_DROP_SITE "my.cdash.org") +set(CTEST_DROP_LOCATION "/submit.php?project=CMakeTutorial") +set(CTEST_DROP_SITE_CDASH TRUE) diff --git a/Step6/MathFunctions/CMakeLists.txt b/Step6/MathFunctions/CMakeLists.txt new file mode 100644 index 0000000..8499a51 --- /dev/null +++ b/Step6/MathFunctions/CMakeLists.txt @@ -0,0 +1,35 @@ +add_library(MathFunctions MathFunctions.cxx) + +# state that anybody linking to us needs to include the current source dir +# to find MathFunctions.h, while we don't. +target_include_directories(MathFunctions + INTERFACE ${CMAKE_CURRENT_SOURCE_DIR} + ) + +# should we use our own math functions +option(USE_MYMATH "Use tutorial provided math implementation" ON) +if (USE_MYMATH) + target_compile_definitions(MathFunctions PRIVATE "USE_MYMATH") + + # library that just does sqrt + add_library(SqrtLibrary STATIC + mysqrt.cxx + ) + + # link SqrtLibrary to tutorial_compiler_flags + target_link_libraries(SqrtLibrary PUBLIC tutorial_compiler_flags) + + target_link_libraries(MathFunctions PRIVATE SqrtLibrary) +endif() + +# link MathFunctions to tutorial_compiler_flags +target_link_libraries(MathFunctions PUBLIC tutorial_compiler_flags) + +# install libs +set(installable_libs MathFunctions tutorial_compiler_flags) +if(TARGET SqrtLibrary) + list(APPEND installable_libs SqrtLibrary) +endif() +install(TARGETS ${installable_libs} DESTINATION lib) +# install include headers +install(FILES MathFunctions.h DESTINATION include) diff --git a/Step6/MathFunctions/MathFunctions.cxx b/Step6/MathFunctions/MathFunctions.cxx new file mode 100644 index 0000000..dc28b4b --- /dev/null +++ b/Step6/MathFunctions/MathFunctions.cxx @@ -0,0 +1,19 @@ +#include "MathFunctions.h" + +#include + +#ifdef USE_MYMATH +# include "mysqrt.h" +#endif + +namespace mathfunctions { +double sqrt(double x) +{ +// which square root function should we use? +#ifdef USE_MYMATH + return detail::mysqrt(x); +#else + return std::sqrt(x); +#endif +} +} diff --git a/Step6/MathFunctions/MathFunctions.h b/Step6/MathFunctions/MathFunctions.h new file mode 100644 index 0000000..d5c2f22 --- /dev/null +++ b/Step6/MathFunctions/MathFunctions.h @@ -0,0 +1,5 @@ +#pragma once + +namespace mathfunctions { +double sqrt(double x); +} diff --git a/Step6/MathFunctions/mysqrt.cxx b/Step6/MathFunctions/mysqrt.cxx new file mode 100644 index 0000000..ba0ac64 --- /dev/null +++ b/Step6/MathFunctions/mysqrt.cxx @@ -0,0 +1,28 @@ +#include "mysqrt.h" + +#include + +namespace mathfunctions { +namespace detail { +// a hack square root calculation using simple operations +double mysqrt(double x) +{ + if (x <= 0) { + return 0; + } + + double result = x; + + // do ten iterations + for (int i = 0; i < 10; ++i) { + if (result <= 0) { + result = 0.1; + } + double delta = x - (result * result); + result = result + 0.5 * delta / result; + std::cout << "Computing sqrt of " << x << " to be " << result << std::endl; + } + return result; +} +} +} diff --git a/Step6/MathFunctions/mysqrt.h b/Step6/MathFunctions/mysqrt.h new file mode 100644 index 0000000..593d41e --- /dev/null +++ b/Step6/MathFunctions/mysqrt.h @@ -0,0 +1,7 @@ +#pragma once + +namespace mathfunctions { +namespace detail { +double mysqrt(double x); +} +} diff --git a/Step6/TutorialConfig.h.in b/Step6/TutorialConfig.h.in new file mode 100644 index 0000000..7e4d7fa --- /dev/null +++ b/Step6/TutorialConfig.h.in @@ -0,0 +1,3 @@ +// the configured options and settings for Tutorial +#define Tutorial_VERSION_MAJOR @Tutorial_VERSION_MAJOR@ +#define Tutorial_VERSION_MINOR @Tutorial_VERSION_MINOR@ diff --git a/Step6/tutorial.cxx b/Step6/tutorial.cxx new file mode 100644 index 0000000..a3a2bdc --- /dev/null +++ b/Step6/tutorial.cxx @@ -0,0 +1,27 @@ +// A simple program that computes the square root of a number +#include +#include +#include + +#include "MathFunctions.h" +#include "TutorialConfig.h" + +int main(int argc, char* argv[]) +{ + if (argc < 2) { + // report version + std::cout << argv[0] << " Version " << Tutorial_VERSION_MAJOR << "." + << Tutorial_VERSION_MINOR << std::endl; + std::cout << "Usage: " << argv[0] << " number" << std::endl; + return 1; + } + + // convert input to double + const double inputValue = std::stod(argv[1]); + + const double outputValue = mathfunctions::sqrt(inputValue); + + std::cout << "The square root of " << inputValue << " is " << outputValue + << std::endl; + return 0; +} diff --git a/Step7/CMakeLists.txt b/Step7/CMakeLists.txt new file mode 100644 index 0000000..97ec6aa --- /dev/null +++ b/Step7/CMakeLists.txt @@ -0,0 +1,70 @@ +cmake_minimum_required(VERSION 3.15) + +# set the project name and version +project(Tutorial VERSION 1.0) + +# specify the C++ standard +add_library(tutorial_compiler_flags INTERFACE) +target_compile_features(tutorial_compiler_flags INTERFACE cxx_std_11) + +# add compiler warning flags just when building this project via +# the BUILD_INTERFACE genex +set(gcc_like_cxx "$") +set(msvc_cxx "$") +target_compile_options(tutorial_compiler_flags INTERFACE + "$<${gcc_like_cxx}:$>" + "$<${msvc_cxx}:$>" +) + +# configure a header file to pass some of the CMake settings +# to the source code +configure_file(TutorialConfig.h.in TutorialConfig.h) + +# add the MathFunctions library +add_subdirectory(MathFunctions) + +# add the executable +add_executable(Tutorial tutorial.cxx) + +target_link_libraries(Tutorial PUBLIC MathFunctions tutorial_compiler_flags) + +# add the binary tree to the search path for include files +# so that we will find TutorialConfig.h +target_include_directories(Tutorial PUBLIC + "${PROJECT_BINARY_DIR}" + ) + +# add the install targets +install(TARGETS Tutorial DESTINATION bin) +install(FILES "${PROJECT_BINARY_DIR}/TutorialConfig.h" + DESTINATION include + ) + +# enable testing +include(CTest) + +# does the application run +add_test(NAME Runs COMMAND Tutorial 25) + +# does the usage message work? +add_test(NAME Usage COMMAND Tutorial) +set_tests_properties(Usage + PROPERTIES PASS_REGULAR_EXPRESSION "Usage:.*number" + ) + +# define a function to simplify adding tests +function(do_test target arg result) + add_test(NAME Comp${arg} COMMAND ${target} ${arg}) + set_tests_properties(Comp${arg} + PROPERTIES PASS_REGULAR_EXPRESSION ${result} + ) +endfunction() + +# do a bunch of result based tests +do_test(Tutorial 4 "4 is 2") +do_test(Tutorial 9 "9 is 3") +do_test(Tutorial 5 "5 is 2.236") +do_test(Tutorial 7 "7 is 2.645") +do_test(Tutorial 25 "25 is 5") +do_test(Tutorial -25 "-25 is (-nan|nan|0)") +do_test(Tutorial 0.0001 "0.0001 is 0.01") diff --git a/Step7/CTestConfig.cmake b/Step7/CTestConfig.cmake new file mode 100644 index 0000000..73efdb1 --- /dev/null +++ b/Step7/CTestConfig.cmake @@ -0,0 +1,7 @@ +set(CTEST_PROJECT_NAME "CMakeTutorial") +set(CTEST_NIGHTLY_START_TIME "00:00:00 EST") + +set(CTEST_DROP_METHOD "http") +set(CTEST_DROP_SITE "my.cdash.org") +set(CTEST_DROP_LOCATION "/submit.php?project=CMakeTutorial") +set(CTEST_DROP_SITE_CDASH TRUE) diff --git a/Step7/MathFunctions/CMakeLists.txt b/Step7/MathFunctions/CMakeLists.txt new file mode 100644 index 0000000..a0b3037 --- /dev/null +++ b/Step7/MathFunctions/CMakeLists.txt @@ -0,0 +1,55 @@ +add_library(MathFunctions MathFunctions.cxx) + +# state that anybody linking to us needs to include the current source dir +# to find MathFunctions.h, while we don't. +target_include_directories(MathFunctions + INTERFACE ${CMAKE_CURRENT_SOURCE_DIR} + ) + +# should we use our own math functions +option(USE_MYMATH "Use tutorial provided math implementation" ON) +if (USE_MYMATH) + target_compile_definitions(MathFunctions PRIVATE "USE_MYMATH") + + # library that just does sqrt + add_library(SqrtLibrary STATIC + mysqrt.cxx + ) + + # link SqrtLibrary to tutorial_compiler_flags + target_link_libraries(SqrtLibrary PUBLIC tutorial_compiler_flags) + + # TODO 1: Include CheckCXXSourceCompiles + + # TODO 2: Use check_cxx_source_compiles with simple C++ code to verify + # availability of: + # * std::log + # * std::exp + # Store the results in HAVE_LOG and HAVE_EXP respectively. + + # Hint: Sample C++ code which uses log: + # #include + # int main() { + # std::log(1.0); + # return 0; + # } + + # TODO 3: Conditionally on HAVE_LOG and HAVE_EXP, add private compile + # definitions "HAVE_LOG" and "HAVE_EXP" to the SqrtLibrary target. + + # Hint: Use target_compile_definitions() + + target_link_libraries(MathFunctions PRIVATE SqrtLibrary) +endif() + +# link MathFunctions to tutorial_compiler_flags +target_link_libraries(MathFunctions PUBLIC tutorial_compiler_flags) + +# install libs +set(installable_libs MathFunctions tutorial_compiler_flags) +if(TARGET SqrtLibrary) + list(APPEND installable_libs SqrtLibrary) +endif() +install(TARGETS ${installable_libs} DESTINATION lib) +# install include headers +install(FILES MathFunctions.h DESTINATION include) diff --git a/Step7/MathFunctions/MathFunctions.cxx b/Step7/MathFunctions/MathFunctions.cxx new file mode 100644 index 0000000..dc28b4b --- /dev/null +++ b/Step7/MathFunctions/MathFunctions.cxx @@ -0,0 +1,19 @@ +#include "MathFunctions.h" + +#include + +#ifdef USE_MYMATH +# include "mysqrt.h" +#endif + +namespace mathfunctions { +double sqrt(double x) +{ +// which square root function should we use? +#ifdef USE_MYMATH + return detail::mysqrt(x); +#else + return std::sqrt(x); +#endif +} +} diff --git a/Step7/MathFunctions/MathFunctions.h b/Step7/MathFunctions/MathFunctions.h new file mode 100644 index 0000000..d5c2f22 --- /dev/null +++ b/Step7/MathFunctions/MathFunctions.h @@ -0,0 +1,5 @@ +#pragma once + +namespace mathfunctions { +double sqrt(double x); +} diff --git a/Step7/MathFunctions/mysqrt.cxx b/Step7/MathFunctions/mysqrt.cxx new file mode 100644 index 0000000..9963cff --- /dev/null +++ b/Step7/MathFunctions/mysqrt.cxx @@ -0,0 +1,37 @@ +#include "mysqrt.h" + +#include + +namespace mathfunctions { +namespace detail { +// a hack square root calculation using simple operations +double mysqrt(double x) +{ + if (x <= 0) { + return 0; + } + + // TODO 5: If both HAVE_LOG and HAVE_EXP are defined, use the following: + //// double result = std::exp(std::log(x) * 0.5); + //// std::cout << "Computing sqrt of " << x << " to be " << result + //// << " using log and exp" << std::endl; + // else, use the existing logic. + + // Hint: Don't forget the #endif before returning the result! + + double result = x; + + // do ten iterations + for (int i = 0; i < 10; ++i) { + if (result <= 0) { + result = 0.1; + } + double delta = x - (result * result); + result = result + 0.5 * delta / result; + std::cout << "Computing sqrt of " << x << " to be " << result << std::endl; + } + + return result; +} +} +} diff --git a/Step7/MathFunctions/mysqrt.h b/Step7/MathFunctions/mysqrt.h new file mode 100644 index 0000000..593d41e --- /dev/null +++ b/Step7/MathFunctions/mysqrt.h @@ -0,0 +1,7 @@ +#pragma once + +namespace mathfunctions { +namespace detail { +double mysqrt(double x); +} +} diff --git a/Step7/TutorialConfig.h.in b/Step7/TutorialConfig.h.in new file mode 100644 index 0000000..7e4d7fa --- /dev/null +++ b/Step7/TutorialConfig.h.in @@ -0,0 +1,3 @@ +// the configured options and settings for Tutorial +#define Tutorial_VERSION_MAJOR @Tutorial_VERSION_MAJOR@ +#define Tutorial_VERSION_MINOR @Tutorial_VERSION_MINOR@ diff --git a/Step7/tutorial.cxx b/Step7/tutorial.cxx new file mode 100644 index 0000000..a3a2bdc --- /dev/null +++ b/Step7/tutorial.cxx @@ -0,0 +1,27 @@ +// A simple program that computes the square root of a number +#include +#include +#include + +#include "MathFunctions.h" +#include "TutorialConfig.h" + +int main(int argc, char* argv[]) +{ + if (argc < 2) { + // report version + std::cout << argv[0] << " Version " << Tutorial_VERSION_MAJOR << "." + << Tutorial_VERSION_MINOR << std::endl; + std::cout << "Usage: " << argv[0] << " number" << std::endl; + return 1; + } + + // convert input to double + const double inputValue = std::stod(argv[1]); + + const double outputValue = mathfunctions::sqrt(inputValue); + + std::cout << "The square root of " << inputValue << " is " << outputValue + << std::endl; + return 0; +} diff --git a/Step8/CMakeLists.txt b/Step8/CMakeLists.txt new file mode 100644 index 0000000..97ec6aa --- /dev/null +++ b/Step8/CMakeLists.txt @@ -0,0 +1,70 @@ +cmake_minimum_required(VERSION 3.15) + +# set the project name and version +project(Tutorial VERSION 1.0) + +# specify the C++ standard +add_library(tutorial_compiler_flags INTERFACE) +target_compile_features(tutorial_compiler_flags INTERFACE cxx_std_11) + +# add compiler warning flags just when building this project via +# the BUILD_INTERFACE genex +set(gcc_like_cxx "$") +set(msvc_cxx "$") +target_compile_options(tutorial_compiler_flags INTERFACE + "$<${gcc_like_cxx}:$>" + "$<${msvc_cxx}:$>" +) + +# configure a header file to pass some of the CMake settings +# to the source code +configure_file(TutorialConfig.h.in TutorialConfig.h) + +# add the MathFunctions library +add_subdirectory(MathFunctions) + +# add the executable +add_executable(Tutorial tutorial.cxx) + +target_link_libraries(Tutorial PUBLIC MathFunctions tutorial_compiler_flags) + +# add the binary tree to the search path for include files +# so that we will find TutorialConfig.h +target_include_directories(Tutorial PUBLIC + "${PROJECT_BINARY_DIR}" + ) + +# add the install targets +install(TARGETS Tutorial DESTINATION bin) +install(FILES "${PROJECT_BINARY_DIR}/TutorialConfig.h" + DESTINATION include + ) + +# enable testing +include(CTest) + +# does the application run +add_test(NAME Runs COMMAND Tutorial 25) + +# does the usage message work? +add_test(NAME Usage COMMAND Tutorial) +set_tests_properties(Usage + PROPERTIES PASS_REGULAR_EXPRESSION "Usage:.*number" + ) + +# define a function to simplify adding tests +function(do_test target arg result) + add_test(NAME Comp${arg} COMMAND ${target} ${arg}) + set_tests_properties(Comp${arg} + PROPERTIES PASS_REGULAR_EXPRESSION ${result} + ) +endfunction() + +# do a bunch of result based tests +do_test(Tutorial 4 "4 is 2") +do_test(Tutorial 9 "9 is 3") +do_test(Tutorial 5 "5 is 2.236") +do_test(Tutorial 7 "7 is 2.645") +do_test(Tutorial 25 "25 is 5") +do_test(Tutorial -25 "-25 is (-nan|nan|0)") +do_test(Tutorial 0.0001 "0.0001 is 0.01") diff --git a/Step8/CTestConfig.cmake b/Step8/CTestConfig.cmake new file mode 100644 index 0000000..73efdb1 --- /dev/null +++ b/Step8/CTestConfig.cmake @@ -0,0 +1,7 @@ +set(CTEST_PROJECT_NAME "CMakeTutorial") +set(CTEST_NIGHTLY_START_TIME "00:00:00 EST") + +set(CTEST_DROP_METHOD "http") +set(CTEST_DROP_SITE "my.cdash.org") +set(CTEST_DROP_LOCATION "/submit.php?project=CMakeTutorial") +set(CTEST_DROP_SITE_CDASH TRUE) diff --git a/Step8/MathFunctions/CMakeLists.txt b/Step8/MathFunctions/CMakeLists.txt new file mode 100644 index 0000000..b14d180 --- /dev/null +++ b/Step8/MathFunctions/CMakeLists.txt @@ -0,0 +1,59 @@ +add_library(MathFunctions MathFunctions.cxx) + +# should we use our own math functions +option(USE_MYMATH "Use tutorial provided math implementation" ON) +if (USE_MYMATH) + target_compile_definitions(MathFunctions PRIVATE "USE_MYMATH") + + # library that just does sqrt + add_library(SqrtLibrary STATIC + mysqrt.cxx + ) + + # link SqrtLibrary to tutorial_compiler_flags + target_link_libraries(SqrtLibrary PUBLIC tutorial_compiler_flags) + + # does this system provide the log and exp functions? + include(CheckCXXSourceCompiles) + check_cxx_source_compiles(" + #include + int main() { + std::log(1.0); + return 0; + } + " HAVE_LOG) + check_cxx_source_compiles(" + #include + int main() { + std::exp(1.0); + return 0; + } + " HAVE_EXP) + + # add compile definitions + if(HAVE_LOG AND HAVE_EXP) + target_compile_definitions(SqrtLibrary + PRIVATE "HAVE_LOG" "HAVE_EXP" + ) + endif() + + target_link_libraries(MathFunctions PRIVATE SqrtLibrary) +endif() + +# state that anybody linking to us needs to include the current source dir +# to find MathFunctions.h, while we don't. +target_include_directories(MathFunctions + INTERFACE ${CMAKE_CURRENT_SOURCE_DIR} + ) + +# link MathFunctions to tutorial_compiler_flags +target_link_libraries(MathFunctions PUBLIC tutorial_compiler_flags) + +# install libs +set(installable_libs MathFunctions tutorial_compiler_flags) +if(TARGET SqrtLibrary) + list(APPEND installable_libs SqrtLibrary) +endif() +install(TARGETS ${installable_libs} DESTINATION lib) +# install include headers +install(FILES MathFunctions.h DESTINATION include) diff --git a/Step8/MathFunctions/MakeTable.cxx b/Step8/MathFunctions/MakeTable.cxx new file mode 100644 index 0000000..ee58556 --- /dev/null +++ b/Step8/MathFunctions/MakeTable.cxx @@ -0,0 +1,25 @@ +// A simple program that builds a sqrt table +#include +#include +#include + +int main(int argc, char* argv[]) +{ + // make sure we have enough arguments + if (argc < 2) { + return 1; + } + + std::ofstream fout(argv[1], std::ios_base::out); + const bool fileOpen = fout.is_open(); + if (fileOpen) { + fout << "double sqrtTable[] = {" << std::endl; + for (int i = 0; i < 10; ++i) { + fout << sqrt(static_cast(i)) << "," << std::endl; + } + // close the table with a zero + fout << "0};" << std::endl; + fout.close(); + } + return fileOpen ? 0 : 1; // return 0 if wrote the file +} diff --git a/Step8/MathFunctions/MathFunctions.cxx b/Step8/MathFunctions/MathFunctions.cxx new file mode 100644 index 0000000..dc28b4b --- /dev/null +++ b/Step8/MathFunctions/MathFunctions.cxx @@ -0,0 +1,19 @@ +#include "MathFunctions.h" + +#include + +#ifdef USE_MYMATH +# include "mysqrt.h" +#endif + +namespace mathfunctions { +double sqrt(double x) +{ +// which square root function should we use? +#ifdef USE_MYMATH + return detail::mysqrt(x); +#else + return std::sqrt(x); +#endif +} +} diff --git a/Step8/MathFunctions/MathFunctions.h b/Step8/MathFunctions/MathFunctions.h new file mode 100644 index 0000000..d5c2f22 --- /dev/null +++ b/Step8/MathFunctions/MathFunctions.h @@ -0,0 +1,5 @@ +#pragma once + +namespace mathfunctions { +double sqrt(double x); +} diff --git a/Step8/MathFunctions/mysqrt.cxx b/Step8/MathFunctions/mysqrt.cxx new file mode 100644 index 0000000..28ab042 --- /dev/null +++ b/Step8/MathFunctions/mysqrt.cxx @@ -0,0 +1,36 @@ +#include "mysqrt.h" + +#include +#include + +namespace mathfunctions { +namespace detail { +// a hack square root calculation using simple operations +double mysqrt(double x) +{ + if (x <= 0) { + return 0; + } + + // if we have both log and exp then use them +#if defined(HAVE_LOG) && defined(HAVE_EXP) + double result = std::exp(std::log(x) * 0.5); + std::cout << "Computing sqrt of " << x << " to be " << result + << " using log and exp" << std::endl; +#else + double result = x; + + // do ten iterations + for (int i = 0; i < 10; ++i) { + if (result <= 0) { + result = 0.1; + } + double delta = x - (result * result); + result = result + 0.5 * delta / result; + std::cout << "Computing sqrt of " << x << " to be " << result << std::endl; + } +#endif + return result; +} +} +} diff --git a/Step8/MathFunctions/mysqrt.h b/Step8/MathFunctions/mysqrt.h new file mode 100644 index 0000000..593d41e --- /dev/null +++ b/Step8/MathFunctions/mysqrt.h @@ -0,0 +1,7 @@ +#pragma once + +namespace mathfunctions { +namespace detail { +double mysqrt(double x); +} +} diff --git a/Step8/TutorialConfig.h.in b/Step8/TutorialConfig.h.in new file mode 100644 index 0000000..7e4d7fa --- /dev/null +++ b/Step8/TutorialConfig.h.in @@ -0,0 +1,3 @@ +// the configured options and settings for Tutorial +#define Tutorial_VERSION_MAJOR @Tutorial_VERSION_MAJOR@ +#define Tutorial_VERSION_MINOR @Tutorial_VERSION_MINOR@ diff --git a/Step8/tutorial.cxx b/Step8/tutorial.cxx new file mode 100644 index 0000000..a3a2bdc --- /dev/null +++ b/Step8/tutorial.cxx @@ -0,0 +1,27 @@ +// A simple program that computes the square root of a number +#include +#include +#include + +#include "MathFunctions.h" +#include "TutorialConfig.h" + +int main(int argc, char* argv[]) +{ + if (argc < 2) { + // report version + std::cout << argv[0] << " Version " << Tutorial_VERSION_MAJOR << "." + << Tutorial_VERSION_MINOR << std::endl; + std::cout << "Usage: " << argv[0] << " number" << std::endl; + return 1; + } + + // convert input to double + const double inputValue = std::stod(argv[1]); + + const double outputValue = mathfunctions::sqrt(inputValue); + + std::cout << "The square root of " << inputValue << " is " << outputValue + << std::endl; + return 0; +} diff --git a/Step9/CMakeLists.txt b/Step9/CMakeLists.txt new file mode 100644 index 0000000..97ec6aa --- /dev/null +++ b/Step9/CMakeLists.txt @@ -0,0 +1,70 @@ +cmake_minimum_required(VERSION 3.15) + +# set the project name and version +project(Tutorial VERSION 1.0) + +# specify the C++ standard +add_library(tutorial_compiler_flags INTERFACE) +target_compile_features(tutorial_compiler_flags INTERFACE cxx_std_11) + +# add compiler warning flags just when building this project via +# the BUILD_INTERFACE genex +set(gcc_like_cxx "$") +set(msvc_cxx "$") +target_compile_options(tutorial_compiler_flags INTERFACE + "$<${gcc_like_cxx}:$>" + "$<${msvc_cxx}:$>" +) + +# configure a header file to pass some of the CMake settings +# to the source code +configure_file(TutorialConfig.h.in TutorialConfig.h) + +# add the MathFunctions library +add_subdirectory(MathFunctions) + +# add the executable +add_executable(Tutorial tutorial.cxx) + +target_link_libraries(Tutorial PUBLIC MathFunctions tutorial_compiler_flags) + +# add the binary tree to the search path for include files +# so that we will find TutorialConfig.h +target_include_directories(Tutorial PUBLIC + "${PROJECT_BINARY_DIR}" + ) + +# add the install targets +install(TARGETS Tutorial DESTINATION bin) +install(FILES "${PROJECT_BINARY_DIR}/TutorialConfig.h" + DESTINATION include + ) + +# enable testing +include(CTest) + +# does the application run +add_test(NAME Runs COMMAND Tutorial 25) + +# does the usage message work? +add_test(NAME Usage COMMAND Tutorial) +set_tests_properties(Usage + PROPERTIES PASS_REGULAR_EXPRESSION "Usage:.*number" + ) + +# define a function to simplify adding tests +function(do_test target arg result) + add_test(NAME Comp${arg} COMMAND ${target} ${arg}) + set_tests_properties(Comp${arg} + PROPERTIES PASS_REGULAR_EXPRESSION ${result} + ) +endfunction() + +# do a bunch of result based tests +do_test(Tutorial 4 "4 is 2") +do_test(Tutorial 9 "9 is 3") +do_test(Tutorial 5 "5 is 2.236") +do_test(Tutorial 7 "7 is 2.645") +do_test(Tutorial 25 "25 is 5") +do_test(Tutorial -25 "-25 is (-nan|nan|0)") +do_test(Tutorial 0.0001 "0.0001 is 0.01") diff --git a/Step9/CTestConfig.cmake b/Step9/CTestConfig.cmake new file mode 100644 index 0000000..73efdb1 --- /dev/null +++ b/Step9/CTestConfig.cmake @@ -0,0 +1,7 @@ +set(CTEST_PROJECT_NAME "CMakeTutorial") +set(CTEST_NIGHTLY_START_TIME "00:00:00 EST") + +set(CTEST_DROP_METHOD "http") +set(CTEST_DROP_SITE "my.cdash.org") +set(CTEST_DROP_LOCATION "/submit.php?project=CMakeTutorial") +set(CTEST_DROP_SITE_CDASH TRUE) diff --git a/Step9/License.txt b/Step9/License.txt new file mode 100644 index 0000000..85760e5 --- /dev/null +++ b/Step9/License.txt @@ -0,0 +1,2 @@ +This is the open source License.txt file introduced in +CMake/Tutorial/Step9... diff --git a/Step9/MathFunctions/CMakeLists.txt b/Step9/MathFunctions/CMakeLists.txt new file mode 100644 index 0000000..5addc6d --- /dev/null +++ b/Step9/MathFunctions/CMakeLists.txt @@ -0,0 +1,44 @@ +add_library(MathFunctions MathFunctions.cxx) + +# state that anybody linking to us needs to include the current source dir +# to find MathFunctions.h, while we don't. +target_include_directories(MathFunctions + INTERFACE ${CMAKE_CURRENT_SOURCE_DIR} + ) + +# should we use our own math functions +option(USE_MYMATH "Use tutorial provided math implementation" ON) +if (USE_MYMATH) + target_compile_definitions(MathFunctions PRIVATE "USE_MYMATH") + + # generate Table.h + include(MakeTable.cmake) + + # library that just does sqrt + add_library(SqrtLibrary STATIC + mysqrt.cxx + ${CMAKE_CURRENT_BINARY_DIR}/Table.h + ) + + # state that we depend on our binary dir to find Table.h + target_include_directories(SqrtLibrary PRIVATE + ${CMAKE_CURRENT_BINARY_DIR} + ) + + # link SqrtLibrary to tutorial_compiler_flags + target_link_libraries(SqrtLibrary PUBLIC tutorial_compiler_flags) + + target_link_libraries(MathFunctions PRIVATE SqrtLibrary) +endif() + +# link MathFunctions to tutorial_compiler_flags +target_link_libraries(MathFunctions PUBLIC tutorial_compiler_flags) + +# install libs +set(installable_libs MathFunctions tutorial_compiler_flags) +if(TARGET SqrtLibrary) + list(APPEND installable_libs SqrtLibrary) +endif() +install(TARGETS ${installable_libs} DESTINATION lib) +# install include headers +install(FILES MathFunctions.h DESTINATION include) diff --git a/Step9/MathFunctions/MakeTable.cmake b/Step9/MathFunctions/MakeTable.cmake new file mode 100644 index 0000000..12865a9 --- /dev/null +++ b/Step9/MathFunctions/MakeTable.cmake @@ -0,0 +1,10 @@ +# first we add the executable that generates the table +add_executable(MakeTable MakeTable.cxx) +target_link_libraries(MakeTable PRIVATE tutorial_compiler_flags) + +# add the command to generate the source code +add_custom_command( + OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/Table.h + COMMAND MakeTable ${CMAKE_CURRENT_BINARY_DIR}/Table.h + DEPENDS MakeTable + ) diff --git a/Step9/MathFunctions/MakeTable.cxx b/Step9/MathFunctions/MakeTable.cxx new file mode 100644 index 0000000..ee58556 --- /dev/null +++ b/Step9/MathFunctions/MakeTable.cxx @@ -0,0 +1,25 @@ +// A simple program that builds a sqrt table +#include +#include +#include + +int main(int argc, char* argv[]) +{ + // make sure we have enough arguments + if (argc < 2) { + return 1; + } + + std::ofstream fout(argv[1], std::ios_base::out); + const bool fileOpen = fout.is_open(); + if (fileOpen) { + fout << "double sqrtTable[] = {" << std::endl; + for (int i = 0; i < 10; ++i) { + fout << sqrt(static_cast(i)) << "," << std::endl; + } + // close the table with a zero + fout << "0};" << std::endl; + fout.close(); + } + return fileOpen ? 0 : 1; // return 0 if wrote the file +} diff --git a/Step9/MathFunctions/MathFunctions.cxx b/Step9/MathFunctions/MathFunctions.cxx new file mode 100644 index 0000000..dc28b4b --- /dev/null +++ b/Step9/MathFunctions/MathFunctions.cxx @@ -0,0 +1,19 @@ +#include "MathFunctions.h" + +#include + +#ifdef USE_MYMATH +# include "mysqrt.h" +#endif + +namespace mathfunctions { +double sqrt(double x) +{ +// which square root function should we use? +#ifdef USE_MYMATH + return detail::mysqrt(x); +#else + return std::sqrt(x); +#endif +} +} diff --git a/Step9/MathFunctions/MathFunctions.h b/Step9/MathFunctions/MathFunctions.h new file mode 100644 index 0000000..d5c2f22 --- /dev/null +++ b/Step9/MathFunctions/MathFunctions.h @@ -0,0 +1,5 @@ +#pragma once + +namespace mathfunctions { +double sqrt(double x); +} diff --git a/Step9/MathFunctions/mysqrt.cxx b/Step9/MathFunctions/mysqrt.cxx new file mode 100644 index 0000000..477d715 --- /dev/null +++ b/Step9/MathFunctions/mysqrt.cxx @@ -0,0 +1,37 @@ +#include "mysqrt.h" + +#include + +// include the generated table +#include "Table.h" + +namespace mathfunctions { +namespace detail { +// a hack square root calculation using simple operations +double mysqrt(double x) +{ + if (x <= 0) { + return 0; + } + + // use the table to help find an initial value + double result = x; + if (x >= 1 && x < 10) { + std::cout << "Use the table to help find an initial value " << std::endl; + result = sqrtTable[static_cast(x)]; + } + + // do ten iterations + for (int i = 0; i < 10; ++i) { + if (result <= 0) { + result = 0.1; + } + double delta = x - (result * result); + result = result + 0.5 * delta / result; + std::cout << "Computing sqrt of " << x << " to be " << result << std::endl; + } + + return result; +} +} +} diff --git a/Step9/MathFunctions/mysqrt.h b/Step9/MathFunctions/mysqrt.h new file mode 100644 index 0000000..593d41e --- /dev/null +++ b/Step9/MathFunctions/mysqrt.h @@ -0,0 +1,7 @@ +#pragma once + +namespace mathfunctions { +namespace detail { +double mysqrt(double x); +} +} diff --git a/Step9/TutorialConfig.h.in b/Step9/TutorialConfig.h.in new file mode 100644 index 0000000..7e4d7fa --- /dev/null +++ b/Step9/TutorialConfig.h.in @@ -0,0 +1,3 @@ +// the configured options and settings for Tutorial +#define Tutorial_VERSION_MAJOR @Tutorial_VERSION_MAJOR@ +#define Tutorial_VERSION_MINOR @Tutorial_VERSION_MINOR@ diff --git a/Step9/tutorial.cxx b/Step9/tutorial.cxx new file mode 100644 index 0000000..a3a2bdc --- /dev/null +++ b/Step9/tutorial.cxx @@ -0,0 +1,27 @@ +// A simple program that computes the square root of a number +#include +#include +#include + +#include "MathFunctions.h" +#include "TutorialConfig.h" + +int main(int argc, char* argv[]) +{ + if (argc < 2) { + // report version + std::cout << argv[0] << " Version " << Tutorial_VERSION_MAJOR << "." + << Tutorial_VERSION_MINOR << std::endl; + std::cout << "Usage: " << argv[0] << " number" << std::endl; + return 1; + } + + // convert input to double + const double inputValue = std::stod(argv[1]); + + const double outputValue = mathfunctions::sqrt(inputValue); + + std::cout << "The square root of " << inputValue << " is " << outputValue + << std::endl; + return 0; +}