diff options
Diffstat (limited to 'Help/guide/tutorial')
-rw-r--r-- | Help/guide/tutorial/Adding Export Configuration.rst | 6 | ||||
-rw-r--r-- | Help/guide/tutorial/Adding Support for a Testing Dashboard.rst | 95 | ||||
-rw-r--r-- | Help/guide/tutorial/Adding System Introspection.rst | 153 | ||||
-rw-r--r-- | Help/guide/tutorial/Installing and Testing.rst | 8 | ||||
-rw-r--r-- | Help/guide/tutorial/Selecting Static or Shared Libraries.rst | 2 | ||||
-rw-r--r-- | Help/guide/tutorial/Step12/CMakeLists.txt | 4 | ||||
-rw-r--r-- | Help/guide/tutorial/Step5/CMakeLists.txt | 4 | ||||
-rw-r--r-- | Help/guide/tutorial/Step6/CMakeLists.txt | 1 | ||||
-rw-r--r-- | Help/guide/tutorial/Step7/MathFunctions/CMakeLists.txt | 20 | ||||
-rw-r--r-- | Help/guide/tutorial/Step7/MathFunctions/mysqrt.cxx | 10 |
10 files changed, 236 insertions, 67 deletions
diff --git a/Help/guide/tutorial/Adding Export Configuration.rst b/Help/guide/tutorial/Adding Export Configuration.rst index eb14f42..6c83276 100644 --- a/Help/guide/tutorial/Adding Export Configuration.rst +++ b/Help/guide/tutorial/Adding Export Configuration.rst @@ -102,7 +102,7 @@ but prepended with a ``PACKAGE_`` prefix. :end-before: # generate the version file The :command:`write_basic_package_version_file` is next. This command writes -a file which is used by the "find_package" document the version and +a file which is used by :command:`find_package`, documenting the version and compatibility of the desired package. Here, we use the ``Tutorial_VERSION_*`` variables and say that it is compatible with ``AnyNewerVersion``, which denotes that this version or any higher one are compatible with the requested @@ -133,8 +133,8 @@ the following to the bottom of the top level ``CMakeLists.txt``: :caption: CMakeLists.txt :name: CMakeLists.txt-export :language: cmake - :start-after: # needs to be after the install(TARGETS ) command + :start-after: # needs to be after the install(TARGETS) command -With this export call we now generate a ``Targets.cmake``, allowing the +With this export call we now generate a ``MathFunctionsTargets.cmake``, allowing the configured ``MathFunctionsConfig.cmake`` in the build directory to be used by other projects, without needing it to be installed. diff --git a/Help/guide/tutorial/Adding Support for a Testing Dashboard.rst b/Help/guide/tutorial/Adding Support for a Testing Dashboard.rst index 45d5976..787e777 100644 --- a/Help/guide/tutorial/Adding Support for a Testing Dashboard.rst +++ b/Help/guide/tutorial/Adding Support for a Testing Dashboard.rst @@ -4,33 +4,40 @@ Step 6: Adding Support for a Testing Dashboard Adding support for submitting our test results to a dashboard is simple. We already defined a number of tests for our project in :ref:`Testing Support <Tutorial Testing Support>`. Now we just have to run -those tests and submit them to a dashboard. To include support for dashboards -we include the :module:`CTest` module in our top-level ``CMakeLists.txt``. +those tests and submit them to CDash. -Replace: -.. literalinclude:: Step6/CMakeLists.txt - :caption: CMakeLists.txt - :name: CMakeLists.txt-enable_testing-remove - :language: cmake - :start-after: # enable testing - :end-before: # does the application run +Exercise 1 - Send Results to a Testing Dashboard +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -With: +Goal +---- -.. literalinclude:: Step7/CMakeLists.txt - :caption: CMakeLists.txt - :name: CMakeLists.txt-include-CTest - :language: cmake - :start-after: # enable testing - :end-before: # does the application run +Display our CTest results with CDash. + +Helpful Resources +----------------- + +* :manual:`ctest(1)` +* :command:`include` +* :module:`CTest` + +Files to Edit +------------- -The :module:`CTest` module will automatically call ``enable_testing()``, so we -can remove it from our CMake files. +* ``CMakeLists.txt`` + +Getting Started +--------------- + +For this exercise, complete ``TODO 1`` in the top-level ``CMakeLists.txt`` by +including the :module:`CTest` module. This will enable testing with CTest as +well as dashboard submissions to CDash, so we can safely remove the call to +:command:`enable_testing`. We will also need to acquire a ``CTestConfig.cmake`` file to be placed in the -top-level directory where we can specify information to CTest about the -project. It contains: +top-level directory. When run, the :manual:`ctest <ctest(1)>` executable will +read this file to gather information about the testing dashboard. It contains: * The project name @@ -41,9 +48,10 @@ project. It contains: * The URL of the CDash instance where the submission's generated documents will be sent -One has been provided for you in this directory. It would normally be -downloaded from the ``Settings`` page of the project on the CDash -instance that will host and display the test results. Once downloaded from +For this tutorial, a public dashboard server is used and its corresponding +``CTestConfig.cmake`` file is provided for you in this step's root directory. +In practice, this file would be downloaded from a project's ``Settings`` page +on the CDash instance intended to host the test results. Once downloaded from CDash, the file should not be modified locally. .. literalinclude:: Step7/CTestConfig.cmake @@ -51,11 +59,16 @@ CDash, the file should not be modified locally. :name: CTestConfig.cmake :language: cmake -The :manual:`ctest <ctest(1)>` executable will read in this file when it runs. -To create a simple dashboard you can run the :manual:`cmake <cmake(1)>` -executable or the :manual:`cmake-gui <cmake-gui(1)>` to configure the project, -but do not build it yet. Instead, change directory to the binary tree, and then -run: + +Build and Run +------------- + +Note that as part of the CDash submission some information about your +development system (e.g. site name or full pathnames) may displayed publicly. + +To create a simple test dashboard, run the :manual:`cmake <cmake(1)>` +executable or the :manual:`cmake-gui <cmake-gui(1)>` to configure the project +but do not build it yet. Instead, navigate to the build directory and run: .. code-block:: console @@ -70,6 +83,28 @@ type must be specified: Or, from an IDE, build the ``Experimental`` target. -The :manual:`ctest <ctest(1)>` executable will build and test the project and -submit the results to Kitware's public dashboard: +The :manual:`ctest <ctest(1)>` executable will build the project, run any +tests, and submit the results to Kitware's public dashboard: https://my.cdash.org/index.php?project=CMakeTutorial. + +Solution +-------- + +The only CMake code changed needed in this step was to enable dashboard +submissions to CDash by including the :module:`CTest` module in our top-level +``CMakeLists.txt``: + +.. raw:: html + + <details><summary>TODO 1: Click to show/hide answer</summary> + +.. literalinclude:: Step7/CMakeLists.txt + :caption: TODO 1: CMakeLists.txt + :name: CMakeLists.txt-include-CTest + :language: cmake + :start-after: # enable testing + :end-before: # does the application run + +.. raw:: html + + </details> diff --git a/Help/guide/tutorial/Adding System Introspection.rst b/Help/guide/tutorial/Adding System Introspection.rst index ba91df4..b69abd2 100644 --- a/Help/guide/tutorial/Adding System Introspection.rst +++ b/Help/guide/tutorial/Adding System Introspection.rst @@ -7,53 +7,156 @@ 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 :module:`CheckCXXSourceCompiles` module in +Exercise 1 - Assessing Dependency Availability +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +Goal +---- + +Change implementation based on available system dependencies. + +Helpful Resources +----------------- + +* :module:`CheckCXXSourceCompiles` +* :command:`target_compile_definitions` + +Files to Edit +------------- + +* ``MathFunctions/CMakeLists.txt`` +* ``MathFunctions/mysqrt.cxx`` + +Getting Started +--------------- + +The starting source code is provided in the ``Step7`` directory. In this +exercise, complete ``TODO 1`` through ``TODO 5``. + +Start by editing ``MathFunctions/CMakeLists.txt``. Include the +:module:`CheckCXXSourceCompiles` module. Then, use +``check_cxx_source_compiles`` to determine whether ``log`` and ``exp`` are +available from ``cmath``. If they are available, use +:command:`target_compile_definitions` to specify ``HAVE_LOG`` and ``HAVE_EXP`` +as compile definitions. + +In the ``MathFunctions/mysqrt.cxx``, include ``cmath``. Then, if the system has +``log`` and ``exp``, use them to compute the square root. + +Build and Run +------------- + +Make a new directory called ``Step7_build``. Run the +:manual:`cmake <cmake(1)>` executable or the +:manual:`cmake-gui <cmake-gui(1)>` to configure the project and then build it +with your chosen build tool and run the ``Tutorial`` executable. + +This can look like the following: + +.. code-block:: console + + mkdir Step7_build + cd Step7_build + cmake ../Step7 + cmake --build . + +Which function gives better results now, ``sqrt`` or ``mysqrt``? + +Solution +-------- + +In this exercise we will use functions from the +:module:`CheckCXXSourceCompiles` module so first we must include it in ``MathFunctions/CMakeLists.txt``. -Add the checks for ``log`` and ``exp`` to ``MathFunctions/CMakeLists.txt``, -after the call to :command:`target_include_directories`: +.. raw:: html + + <details><summary>TODO 1: Click to show/hide answer</summary> .. literalinclude:: Step8/MathFunctions/CMakeLists.txt - :caption: MathFunctions/CMakeLists.txt + :caption: TODO 1: MathFunctions/CMakeLists.txt + :name: MathFunctions/CMakeLists.txt-include-check_cxx_source_compiles + :language: cmake + :start-after: # does this system provide the log and exp functions? + :end-before: check_cxx_source_compiles + +.. raw:: html + + </details> + +Then test for the availability of +``log`` and ``exp`` using ``check_cxx_compiles_source``. This function +lets us try compiling simple code with the required dependency prior to +the true source code compilation. The resulting variables ``HAVE_LOG`` +and ``HAVE_EXP`` represent whether those dependencies are available. + +.. raw:: html + + <details><summary>TODO 2: Click to show/hide answer</summary> + +.. literalinclude:: Step8/MathFunctions/CMakeLists.txt + :caption: TODO 2: MathFunctions/CMakeLists.txt :name: MathFunctions/CMakeLists.txt-check_cxx_source_compiles :language: cmake - :start-after: # to find MathFunctions.h, while we don't. + :start-after: include(CheckCXXSourceCompiles) :end-before: # add compile definitions -If available, use :command:`target_compile_definitions` to specify +.. raw:: html + + </details> + +Next, we need to pass these CMake variables to our source code. This way, +our source code can tell what resources are available. If both ``log`` and +``exp`` are available, use :command:`target_compile_definitions` to specify ``HAVE_LOG`` and ``HAVE_EXP`` as ``PRIVATE`` compile definitions. +.. raw:: html + + <details><summary>TODO 3: Click to show/hide answer</summary> + .. literalinclude:: Step8/MathFunctions/CMakeLists.txt - :caption: MathFunctions/CMakeLists.txt + :caption: TODO 3: MathFunctions/CMakeLists.txt :name: MathFunctions/CMakeLists.txt-target_compile_definitions :language: cmake :start-after: # add compile definitions :end-before: # install libs -If ``log`` and ``exp`` are available on the system, then we will use them to -compute the square root in the ``mysqrt`` function. Add the following code to -the ``mysqrt`` function in ``MathFunctions/mysqrt.cxx`` (don't forget the -``#endif`` before returning the result!): +.. raw:: html -.. literalinclude:: Step8/MathFunctions/mysqrt.cxx - :caption: MathFunctions/mysqrt.cxx - :name: MathFunctions/mysqrt.cxx-ifdef - :language: c++ - :start-after: // if we have both log and exp then use them - :end-before: // do ten iterations + </details> + +Since we may be using ``log`` and ``exp``, we need to modify +``mysqrt.cxx`` to include ``cmath``. + +.. raw:: html -We will also need to modify ``mysqrt.cxx`` to include ``cmath``. + <details><summary>TODO 4: Click to show/hide answer</summary> .. literalinclude:: Step8/MathFunctions/mysqrt.cxx - :caption: MathFunctions/mysqrt.cxx + :caption: TODO 4: MathFunctions/mysqrt.cxx :name: MathFunctions/mysqrt.cxx-include-cmath :language: c++ :end-before: #include <iostream> -Run the :manual:`cmake <cmake(1)>` executable or the -:manual:`cmake-gui <cmake-gui(1)>` to configure the project and then build it -with your chosen build tool and run the Tutorial executable. +.. raw:: html -Which function gives better results now, ``sqrt`` or ``mysqrt``? + </details> + +If ``log`` and ``exp`` are available on the system, then use them to +compute the square root in the ``mysqrt`` function. The ``mysqrt`` function in +``MathFunctions/mysqrt.cxx`` will look as follows: + +.. raw:: html + + <details><summary>TODO 5: Click to show/hide answer</summary> + +.. literalinclude:: Step8/MathFunctions/mysqrt.cxx + :caption: TODO 5: MathFunctions/mysqrt.cxx + :name: MathFunctions/mysqrt.cxx-ifdef + :language: c++ + :start-after: // if we have both log and exp then use them + :end-before: // do ten iterations + +.. raw:: html + + </details> diff --git a/Help/guide/tutorial/Installing and Testing.rst b/Help/guide/tutorial/Installing and Testing.rst index fa13040..7a59fcb 100644 --- a/Help/guide/tutorial/Installing and Testing.rst +++ b/Help/guide/tutorial/Installing and Testing.rst @@ -1,8 +1,6 @@ Step 5: Installing and Testing ============================== -.. _`Tutorial Testing Support`: - Exercise 1 - Install Rules ^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -145,7 +143,7 @@ are similar. To the end of the top-level ``CMakeLists.txt`` we add: :name: TODO 3,4: CMakeLists.txt-install-TARGETS :language: cmake :start-after: # add the install targets - :end-before: # enable testing + :end-before: # TODO 1: Replace enable_testing() with include(CTest) .. raw:: html @@ -154,6 +152,8 @@ are similar. To the end of the top-level ``CMakeLists.txt`` we add: That is all that is needed to create a basic local install of the tutorial. +.. _`Tutorial Testing Support`: + Exercise 2 - Testing Support ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -195,7 +195,7 @@ Build and Run ------------- Navigate to the build directory and rebuild the application. Then, run the -``ctest`` executable: :option:`ctest -N` and :option:`ctest -VV`. For +:program:`ctest` executable: :option:`ctest -N` and :option:`ctest -VV`. For multi-config generators (e.g. Visual Studio), the configuration type must be specified with the :option:`-C \<mode\> <ctest -C>` flag. For example, to run tests in Debug mode use ``ctest -C Debug -VV`` from the build directory diff --git a/Help/guide/tutorial/Selecting Static or Shared Libraries.rst b/Help/guide/tutorial/Selecting Static or Shared Libraries.rst index 1c49c23..7befe1d 100644 --- a/Help/guide/tutorial/Selecting Static or Shared Libraries.rst +++ b/Help/guide/tutorial/Selecting Static or Shared Libraries.rst @@ -65,7 +65,7 @@ At this point, if you build everything, you may notice that linking fails as we are combining a static library without position independent code with a library that has position independent code. The solution to this is to explicitly set the :prop_tgt:`POSITION_INDEPENDENT_CODE` target property of -SqrtLibrary to be ``True`` no matter the build type. +SqrtLibrary to be ``True`` when building shared libraries. .. literalinclude:: Step11/MathFunctions/CMakeLists.txt :caption: MathFunctions/CMakeLists.txt diff --git a/Help/guide/tutorial/Step12/CMakeLists.txt b/Help/guide/tutorial/Step12/CMakeLists.txt index 220ed4b..1d8b5a6 100644 --- a/Help/guide/tutorial/Step12/CMakeLists.txt +++ b/Help/guide/tutorial/Step12/CMakeLists.txt @@ -94,7 +94,7 @@ install(EXPORT MathFunctionsTargets ) include(CMakePackageConfigHelpers) -# generate the config file that is includes the exports +# 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" @@ -116,7 +116,7 @@ install(FILES ) # generate the export targets for the build tree -# needs to be after the install(TARGETS ) command +# needs to be after the install(TARGETS) command export(EXPORT MathFunctionsTargets FILE "${CMAKE_CURRENT_BINARY_DIR}/MathFunctionsTargets.cmake" ) diff --git a/Help/guide/tutorial/Step5/CMakeLists.txt b/Help/guide/tutorial/Step5/CMakeLists.txt index a8f241a..279ddf9 100644 --- a/Help/guide/tutorial/Step5/CMakeLists.txt +++ b/Help/guide/tutorial/Step5/CMakeLists.txt @@ -42,7 +42,7 @@ target_include_directories(Tutorial PUBLIC # TODO 3: Install Tutorial in the bin directory # Hint: Use the TARGETS and DESTINATION parameters -# TODO 4: Install Tutorial.h to the include directory +# TODO 4: Install TutorialConfig.h to the include directory # Hint: Use the FILES and DESTINATION parameters # TODO 5: Enable testing @@ -61,4 +61,4 @@ target_include_directories(Tutorial PUBLIC # 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.00001. +# paste. Test the following values: 4, 9, 5, 7, 25, -25 and 0.0001. diff --git a/Help/guide/tutorial/Step6/CMakeLists.txt b/Help/guide/tutorial/Step6/CMakeLists.txt index da9e852..c11e307 100644 --- a/Help/guide/tutorial/Step6/CMakeLists.txt +++ b/Help/guide/tutorial/Step6/CMakeLists.txt @@ -45,6 +45,7 @@ install(FILES "${PROJECT_BINARY_DIR}/TutorialConfig.h" DESTINATION include ) +# TODO 1: Replace enable_testing() with include(CTest) # enable testing enable_testing() diff --git a/Help/guide/tutorial/Step7/MathFunctions/CMakeLists.txt b/Help/guide/tutorial/Step7/MathFunctions/CMakeLists.txt index b4724c4..e5bdc4d 100644 --- a/Help/guide/tutorial/Step7/MathFunctions/CMakeLists.txt +++ b/Help/guide/tutorial/Step7/MathFunctions/CMakeLists.txt @@ -9,6 +9,26 @@ target_include_directories(MathFunctions # link our compiler flags interface library target_link_libraries(MathFunctions 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 <cmath> +# 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 MathFunctions target. + +#Hint: Use target_compile_definitions() + # install libs set(installable_libs MathFunctions tutorial_compiler_flags) install(TARGETS ${installable_libs} DESTINATION lib) diff --git a/Help/guide/tutorial/Step7/MathFunctions/mysqrt.cxx b/Help/guide/tutorial/Step7/MathFunctions/mysqrt.cxx index abe767d..3d2492a 100644 --- a/Help/guide/tutorial/Step7/MathFunctions/mysqrt.cxx +++ b/Help/guide/tutorial/Step7/MathFunctions/mysqrt.cxx @@ -1,5 +1,6 @@ #include <iostream> +// TODO 4: include cmath #include "MathFunctions.h" // a hack square root calculation using simple operations @@ -9,6 +10,14 @@ double mysqrt(double x) 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 @@ -20,5 +29,6 @@ double mysqrt(double x) result = result + 0.5 * delta / result; std::cout << "Computing sqrt of " << x << " to be " << result << std::endl; } + return result; } |