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/Step4 | |
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/Step4')
-rw-r--r-- | Help/guide/tutorial/Step4/CMakeLists.txt | 35 | ||||
-rw-r--r-- | Help/guide/tutorial/Step4/MathFunctions/CMakeLists.txt | 7 | ||||
-rw-r--r-- | Help/guide/tutorial/Step4/MathFunctions/MathFunctions.h | 1 | ||||
-rw-r--r-- | Help/guide/tutorial/Step4/MathFunctions/mysqrt.cxx | 23 | ||||
-rw-r--r-- | Help/guide/tutorial/Step4/TutorialConfig.h.in | 4 | ||||
-rw-r--r-- | Help/guide/tutorial/Step4/directions.txt | 72 | ||||
-rw-r--r-- | Help/guide/tutorial/Step4/tutorial.cxx | 32 |
7 files changed, 174 insertions, 0 deletions
diff --git a/Help/guide/tutorial/Step4/CMakeLists.txt b/Help/guide/tutorial/Step4/CMakeLists.txt new file mode 100644 index 0000000..9ce60b9 --- /dev/null +++ b/Help/guide/tutorial/Step4/CMakeLists.txt @@ -0,0 +1,35 @@ +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(USE_MYMATH) + +# 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}" + ) diff --git a/Help/guide/tutorial/Step4/MathFunctions/CMakeLists.txt b/Help/guide/tutorial/Step4/MathFunctions/CMakeLists.txt new file mode 100644 index 0000000..0515852 --- /dev/null +++ b/Help/guide/tutorial/Step4/MathFunctions/CMakeLists.txt @@ -0,0 +1,7 @@ +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} + ) diff --git a/Help/guide/tutorial/Step4/MathFunctions/MathFunctions.h b/Help/guide/tutorial/Step4/MathFunctions/MathFunctions.h new file mode 100644 index 0000000..cd36bcc --- /dev/null +++ b/Help/guide/tutorial/Step4/MathFunctions/MathFunctions.h @@ -0,0 +1 @@ +double mysqrt(double x); diff --git a/Help/guide/tutorial/Step4/MathFunctions/mysqrt.cxx b/Help/guide/tutorial/Step4/MathFunctions/mysqrt.cxx new file mode 100644 index 0000000..7d9379e --- /dev/null +++ b/Help/guide/tutorial/Step4/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/Step4/TutorialConfig.h.in b/Help/guide/tutorial/Step4/TutorialConfig.h.in new file mode 100644 index 0000000..e23f521 --- /dev/null +++ b/Help/guide/tutorial/Step4/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/Step4/directions.txt b/Help/guide/tutorial/Step4/directions.txt new file mode 100644 index 0000000..91e4043 --- /dev/null +++ b/Help/guide/tutorial/Step4/directions.txt @@ -0,0 +1,72 @@ +# Installing and Testing # + +Now we can start adding testing support and install rules to our project. + +The install rules are fairly simple; for MathFunctions we install the library +and header file, for the application we install the executable and configured +header. + +So to MathFunctions/CMakeLists.txt we add: + + install (TARGETS MathFunctions DESTINATION bin) + install (FILES MathFunctions.h DESTINATION include) + +And the to top-level CMakeLists.txt we add: + + install(TARGETS Tutorial DESTINATION bin) + install(FILES "${PROJECT_BINARY_DIR}/TutorialConfig.h" + DESTINATION include + ) + +That is all that is needed to create a basic local install of the tutorial. + +Run cmake or cmake-gui to configure the project and then build it with your +chosen build tool. Then build the “install” target by typing 'make install' +from the command line or build the INSTALL target from an IDE. This will +install the appropriate header files, libraries, and executables. + +Verify that the installed Tutorial runs. Note: The CMake variable +CMAKE_INSTALL_PREFIX is used to determine the root of where the files will +be installed. + +Next let's test our application. Adding testing is an easy process. At the +end of the top-level CMakeLists file we can add a number of basic tests to +verify that the application is working correctly. + + # 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 25 "25 is 5") + do_test(Tutorial -25 "-25 is [-nan|nan|0]") + do_test(Tutorial 0.0001 "0.0001 is 0.01") + +The first test simply verifies that the application runs, does not segfault or +otherwise crash, and has a zero return value. This is the basic form of a CTest +test. + +The Usage test uses a regular expression to verify that the usage message +is printed when an incorrect number of arguments are provided. + +Lastly, we have a function called do_test that simplifies running the +application and verifying that the computed square root is correct for given +input. + +To run tests, cd to the binary directory and run “ctest -N” and “ctest -VV”. diff --git a/Help/guide/tutorial/Step4/tutorial.cxx b/Help/guide/tutorial/Step4/tutorial.cxx new file mode 100644 index 0000000..c2b89df --- /dev/null +++ b/Help/guide/tutorial/Step4/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; +} |