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/Step6 | |
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/Step6')
-rw-r--r-- | Help/guide/tutorial/Step6/CMakeLists.txt | 75 | ||||
-rw-r--r-- | Help/guide/tutorial/Step6/MathFunctions/CMakeLists.txt | 14 | ||||
-rw-r--r-- | Help/guide/tutorial/Step6/MathFunctions/MakeTable.cxx | 25 | ||||
-rw-r--r-- | Help/guide/tutorial/Step6/MathFunctions/MathFunctions.h | 1 | ||||
-rw-r--r-- | Help/guide/tutorial/Step6/MathFunctions/mysqrt.cxx | 33 | ||||
-rw-r--r-- | Help/guide/tutorial/Step6/TutorialConfig.h.in | 8 | ||||
-rw-r--r-- | Help/guide/tutorial/Step6/directions.txt | 104 | ||||
-rw-r--r-- | Help/guide/tutorial/Step6/tutorial.cxx | 32 |
8 files changed, 292 insertions, 0 deletions
diff --git a/Help/guide/tutorial/Step6/CMakeLists.txt b/Help/guide/tutorial/Step6/CMakeLists.txt new file mode 100644 index 0000000..a78b0ff --- /dev/null +++ b/Help/guide/tutorial/Step6/CMakeLists.txt @@ -0,0 +1,75 @@ +cmake_minimum_required(VERSION 3.3) +project(Tutorial) + +set(CMAKE_CXX_STANDARD 14) + +# the version number. +set(Tutorial_VERSION_MAJOR 1) +set(Tutorial_VERSION_MINOR 0) + +# 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) + +# should we use our own math functions +option(USE_MYMATH "Use tutorial provided math implementation" ON) + +# 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/Step6/MathFunctions/CMakeLists.txt b/Help/guide/tutorial/Step6/MathFunctions/CMakeLists.txt new file mode 100644 index 0000000..2946075 --- /dev/null +++ b/Help/guide/tutorial/Step6/MathFunctions/CMakeLists.txt @@ -0,0 +1,14 @@ +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. +# state that we depend on Tutorial_BINARY_DIR but consumers don't, as the +# TutorialConfig.h include is an implementation detail + +target_include_directories(MathFunctions + INTERFACE ${CMAKE_CURRENT_SOURCE_DIR} + PRIVATE ${Tutorial_BINARY_DIR} + ) + +install(TARGETS MathFunctions DESTINATION lib) +install(FILES MathFunctions.h DESTINATION include) diff --git a/Help/guide/tutorial/Step6/MathFunctions/MakeTable.cxx b/Help/guide/tutorial/Step6/MathFunctions/MakeTable.cxx new file mode 100644 index 0000000..ee58556 --- /dev/null +++ b/Help/guide/tutorial/Step6/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/Step6/MathFunctions/MathFunctions.h b/Help/guide/tutorial/Step6/MathFunctions/MathFunctions.h new file mode 100644 index 0000000..cd36bcc --- /dev/null +++ b/Help/guide/tutorial/Step6/MathFunctions/MathFunctions.h @@ -0,0 +1 @@ +double mysqrt(double x); diff --git a/Help/guide/tutorial/Step6/MathFunctions/mysqrt.cxx b/Help/guide/tutorial/Step6/MathFunctions/mysqrt.cxx new file mode 100644 index 0000000..b9ad20a --- /dev/null +++ b/Help/guide/tutorial/Step6/MathFunctions/mysqrt.cxx @@ -0,0 +1,33 @@ +#include "MathFunctions.h" +#include "TutorialConfig.h" +#include <iostream> + +#include <cmath> + +// 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 = exp(log(x) * 0.5); + std::cout << "Computing sqrt of " << x << " to be " << result << " using log" + << 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/Help/guide/tutorial/Step6/TutorialConfig.h.in b/Help/guide/tutorial/Step6/TutorialConfig.h.in new file mode 100644 index 0000000..e97ce24 --- /dev/null +++ b/Help/guide/tutorial/Step6/TutorialConfig.h.in @@ -0,0 +1,8 @@ +// the configured options and settings for Tutorial +#define Tutorial_VERSION_MAJOR @Tutorial_VERSION_MAJOR@ +#define Tutorial_VERSION_MINOR @Tutorial_VERSION_MINOR@ +#cmakedefine USE_MYMATH + +// does the platform provide exp and log functions? +#cmakedefine HAVE_LOG +#cmakedefine HAVE_EXP diff --git a/Help/guide/tutorial/Step6/directions.txt b/Help/guide/tutorial/Step6/directions.txt new file mode 100644 index 0000000..42b9f06 --- /dev/null +++ b/Help/guide/tutorial/Step6/directions.txt @@ -0,0 +1,104 @@ +# Adding a Custom Command and Generated File # + +In this section we will show how you can add a generated source file into the +build process of an application. For this example, we will create a table of +precomputed square roots as part of the build process, and then compile that +table into our application. + +To accomplish this, we first need a program that will generate the table. In the +MathFunctions subdirectory a new source file named MakeTable.cxx will do just that. + + // A simple program that builds a sqrt table + #include <iostream> + #include <fstream> + #include <cmath> + + 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 + } + +Note that the table is produced as valid C++ code and that the output filename +is passed in as an argument. + +The next step is to add the appropriate commands to MathFunctions’ CMakeLists +file to build the MakeTable executable and then run it as part of the build +process. A few commands are needed to accomplish this, as shown below: + + # first we add the executable that generates the table + add_executable(MakeTable MakeTable.cxx) + + # 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 + ) + + # add the main library + add_library(MathFunctions + mysqrt.cxx + ${CMAKE_CURRENT_BINARY_DIR}/Table.h + ) + + target_include_directories(MathFunctions + INTERFACE ${CMAKE_CURRENT_SOURCE_DIR} + PUBLIC ${Tutorial_BINARY_DIR} + # add the binary tree directory to the search path for include files + ${CMAKE_CURRENT_BINARY_DIR} + ) + + install(TARGETS MathFunctions DESTINATION lib) + install(FILES MathFunctions.h DESTINATION include) + +First, the executable for MakeTable is added as any other executable would be +added. Then we add a custom command that specifies how to produce Table.h by +running MakeTable. Next we have to let CMake know that mysqrt.cxx depends on +the generated file Table.h. This is done by adding the generated Table.h to the +list of sources for the library MathFunctions. We also have to add the current +binary directory to the list of include directories so that Table.h can be +found and included by mysqrt.cxx. + +Now let's use the generated table. First, modify mysqrt.cxx to include Table.h. +Next, we can rewrite the mysqrt function to use the table: + + if (x <= 0) { + return 0; + } + + // use the table to help find an initial value + double result = x; + if (x >= 1 && x < 10) { + result = sqrtTable[static_cast<int>(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; + } + +Run cmake or cmake-gui to configure the project and then build it with your +chosen build tool. When this project is built it will first build the MakeTable +executable. It will then run MakeTable to produce Table.h. Finally, it will +compile mysqrt.cxx which includes Table.h to produce the MathFunctions library. diff --git a/Help/guide/tutorial/Step6/tutorial.cxx b/Help/guide/tutorial/Step6/tutorial.cxx new file mode 100644 index 0000000..c2b89df --- /dev/null +++ b/Help/guide/tutorial/Step6/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; +} |