diff options
author | Kitware Robot <kwrobot@kitware.com> | 2019-05-30 16:41:21 (GMT) |
---|---|---|
committer | Betsy McPhail <betsy.mcphail@kitware.com> | 2019-06-18 14:36:16 (GMT) |
commit | 862cfc0e6c3f275db73281f3b9b989704251ab6a (patch) | |
tree | 91c65801e02f337fa95e9776a6bf59057d55ec17 /Help/guide/tutorial/Step5 | |
parent | d2fde9480955cf2246519357e01ab5142a067efc (diff) | |
download | CMake-862cfc0e6c3f275db73281f3b9b989704251ab6a.zip CMake-862cfc0e6c3f275db73281f3b9b989704251ab6a.tar.gz CMake-862cfc0e6c3f275db73281f3b9b989704251ab6a.tar.bz2 |
Help/guide/tutorial: Adopt tutorial code
Diffstat (limited to 'Help/guide/tutorial/Step5')
-rw-r--r-- | Help/guide/tutorial/Step5/CMakeLists.txt | 69 | ||||
-rw-r--r-- | Help/guide/tutorial/Step5/MathFunctions/CMakeLists.txt | 10 | ||||
-rw-r--r-- | Help/guide/tutorial/Step5/MathFunctions/MakeTable.cxx | 25 | ||||
-rw-r--r-- | Help/guide/tutorial/Step5/MathFunctions/MathFunctions.h | 1 | ||||
-rw-r--r-- | Help/guide/tutorial/Step5/MathFunctions/mysqrt.cxx | 23 | ||||
-rw-r--r-- | Help/guide/tutorial/Step5/TutorialConfig.h.in | 4 | ||||
-rw-r--r-- | Help/guide/tutorial/Step5/directions.txt | 69 | ||||
-rw-r--r-- | Help/guide/tutorial/Step5/tutorial.cxx | 32 |
8 files changed, 233 insertions, 0 deletions
diff --git a/Help/guide/tutorial/Step5/CMakeLists.txt b/Help/guide/tutorial/Step5/CMakeLists.txt new file mode 100644 index 0000000..828b9fc --- /dev/null +++ b/Help/guide/tutorial/Step5/CMakeLists.txt @@ -0,0 +1,69 @@ +cmake_minimum_required(VERSION 3.3) +project(Tutorial) + +set(CMAKE_CXX_STANDARD 14) + +# should we use our own math functions +option(USE_MYMATH "Use tutorial provided math implementation" ON) + +# the version number. +set(Tutorial_VERSION_MAJOR 1) +set(Tutorial_VERSION_MINOR 0) + +# configure a header file to pass some of the CMake settings +# to the source code +configure_file( + "${PROJECT_SOURCE_DIR}/TutorialConfig.h.in" + "${PROJECT_BINARY_DIR}/TutorialConfig.h" + ) + +# add the MathFunctions library? +if(USE_MYMATH) + add_subdirectory(MathFunctions) + list(APPEND EXTRA_LIBS MathFunctions) +endif() + +# add the executable +add_executable(Tutorial tutorial.cxx) +target_link_libraries(Tutorial PUBLIC ${EXTRA_LIBS}) + +# 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_test) + +# 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/Help/guide/tutorial/Step5/MathFunctions/CMakeLists.txt b/Help/guide/tutorial/Step5/MathFunctions/CMakeLists.txt new file mode 100644 index 0000000..11cf412 --- /dev/null +++ b/Help/guide/tutorial/Step5/MathFunctions/CMakeLists.txt @@ -0,0 +1,10 @@ +add_library(MathFunctions mysqrt.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} + ) + +install(TARGETS MathFunctions DESTINATION lib) +install(FILES MathFunctions.h DESTINATION include) diff --git a/Help/guide/tutorial/Step5/MathFunctions/MakeTable.cxx b/Help/guide/tutorial/Step5/MathFunctions/MakeTable.cxx new file mode 100644 index 0000000..ee58556 --- /dev/null +++ b/Help/guide/tutorial/Step5/MathFunctions/MakeTable.cxx @@ -0,0 +1,25 @@ +// A simple program that builds a sqrt table +#include <cmath> +#include <fstream> +#include <iostream> + +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<double>(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/Help/guide/tutorial/Step5/MathFunctions/MathFunctions.h b/Help/guide/tutorial/Step5/MathFunctions/MathFunctions.h new file mode 100644 index 0000000..cd36bcc --- /dev/null +++ b/Help/guide/tutorial/Step5/MathFunctions/MathFunctions.h @@ -0,0 +1 @@ +double mysqrt(double x); diff --git a/Help/guide/tutorial/Step5/MathFunctions/mysqrt.cxx b/Help/guide/tutorial/Step5/MathFunctions/mysqrt.cxx new file mode 100644 index 0000000..7d9379e --- /dev/null +++ b/Help/guide/tutorial/Step5/MathFunctions/mysqrt.cxx @@ -0,0 +1,23 @@ +#include "MathFunctions.h" +#include <iostream> + +// 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/Help/guide/tutorial/Step5/TutorialConfig.h.in b/Help/guide/tutorial/Step5/TutorialConfig.h.in new file mode 100644 index 0000000..e23f521 --- /dev/null +++ b/Help/guide/tutorial/Step5/TutorialConfig.h.in @@ -0,0 +1,4 @@ +// the configured options and settings for Tutorial +#define Tutorial_VERSION_MAJOR @Tutorial_VERSION_MAJOR@ +#define Tutorial_VERSION_MINOR @Tutorial_VERSION_MINOR@ +#cmakedefine USE_MYMATH diff --git a/Help/guide/tutorial/Step5/directions.txt b/Help/guide/tutorial/Step5/directions.txt new file mode 100644 index 0000000..e6f5197 --- /dev/null +++ b/Help/guide/tutorial/Step5/directions.txt @@ -0,0 +1,69 @@ +# Adding System Introspection # + +Let us consider adding some code to our project that depends on features the +target platform may not have. For this example, we will add some code that +depends on whether or not the target platform has the log and exp functions. Of +course almost every platform has these functions but for this tutorial assume +that they are not common. + +If the platform has log and exp then we will use them to compute the square +root in the mysqrt function. We first test for the availability of these +functions using the CheckSymbolExists.cmake macro in the top-level CMakeLists +file as follows: + + # does this system provide the log and exp functions? + include(CheckSymbolExists) + set(CMAKE_REQUIRED_LIBRARIES "m") + check_symbol_exists(log "math.h" HAVE_LOG) + check_symbol_exists(exp "math.h" HAVE_EXP) + +Now let's add these defines to TutorialConfig.h.in so that we can use them +from mysqrt.cxx: + + // does the platform provide exp and log functions? + #cmakedefine HAVE_LOG + #cmakedefine HAVE_EXP + +Modify mysqrt.cxx to include math.h. Next, in the mysqrt function we can +provide an alternate implementation based on log and exp if they are available +on the system using the following code: + + // if we have both log and exp then use them + #if defined(HAVE_LOG) && defined (HAVE_EXP) + double result = exp(log(x)*0.5); + std::cout << "Computing sqrt of " << x << " to be " << result << " using log" << std::endl; + #else + ... + +Run cmake or cmake-gui to configure the project and then build it with your +chosen build tool. + +You will notice that even though HAVE_LOG and HAVE_EXP are both defined mysqrt +isn't using them. We should realize quickly that we have forgotten to include +TutorialConfig.h in mysqrt.cxx. We will also need to update +MathFunctions/CMakeLists.txt with where it is located. + +So let's go ahead and update MathFunctions/CMakeLists.txt to look like: + + add_library(MathFunctions mysqrt.cxx) + + target_include_directories(MathFunctions + INTERFACE ${CMAKE_CURRENT_SOURCE_DIR} + PRIVATE ${Tutorial_BINARY_DIR} + ) + + install(TARGETS MathFunctions DESTINATION lib) + install(FILES MathFunctions.h DESTINATION include) + +Now all we need to do is include TutorialConfig.h in mysqrt.cxx + +At this point you should go ahead and build the project again. + +Run the built Tutorial executable. Which function gives better results now, +Step1’s sqrt or Step5’s mysqrt? + +Exercise: Why is it important that we configure TutorialConfig.h.in after the +checks for HAVE_LOG and HAVE_EXP? What would happen if we inverted the two? + +Exercise: Is there a better place for us to save the HAVE_LOG and HAVE_EXP +values other than in TutorialConfig.h? diff --git a/Help/guide/tutorial/Step5/tutorial.cxx b/Help/guide/tutorial/Step5/tutorial.cxx new file mode 100644 index 0000000..c2b89df --- /dev/null +++ b/Help/guide/tutorial/Step5/tutorial.cxx @@ -0,0 +1,32 @@ +// A simple program that computes the square root of a number +#include <cmath> +#include <iostream> +#include <string> + +#include "TutorialConfig.h" + +#ifdef USE_MYMATH +# include "MathFunctions.h" +#endif + +int main(int argc, char* argv[]) +{ + if (argc < 2) { + std::cout << argv[0] << " Version " << Tutorial_VERSION_MAJOR << "." + << Tutorial_VERSION_MINOR << std::endl; + std::cout << "Usage: " << argv[0] << " number" << std::endl; + return 1; + } + + double inputValue = std::stod(argv[1]); + +#ifdef USE_MYMATH + double outputValue = mysqrt(inputValue); +#else + double outputValue = sqrt(inputValue); +#endif + + std::cout << "The square root of " << inputValue << " is " << outputValue + << std::endl; + return 0; +} |