diff options
author | Markus Ferrell <markus.ferrell@kitware.com> | 2023-03-03 15:48:17 (GMT) |
---|---|---|
committer | Markus Ferrell <markus.ferrell@kitware.com> | 2023-03-07 16:43:26 (GMT) |
commit | e1f2b35723f28b55b786bbe4cbee70a3e54e7da9 (patch) | |
tree | 6f967531211adff82d72cabd9c049a4c3c109655 /Help/guide | |
parent | 35ca2d524befc71b840808cce4e0a773ef722b71 (diff) | |
download | CMake-e1f2b35723f28b55b786bbe4cbee70a3e54e7da9.zip CMake-e1f2b35723f28b55b786bbe4cbee70a3e54e7da9.tar.gz CMake-e1f2b35723f28b55b786bbe4cbee70a3e54e7da9.tar.bz2 |
Tutorial: Refactor MakeTable commands into MakeTable.cmake
Diffstat (limited to 'Help/guide')
21 files changed, 272 insertions, 114 deletions
diff --git a/Help/guide/tutorial/Adding Generator Expressions.rst b/Help/guide/tutorial/Adding Generator Expressions.rst index 08f3eea..aba9f7a 100644 --- a/Help/guide/tutorial/Adding Generator Expressions.rst +++ b/Help/guide/tutorial/Adding Generator Expressions.rst @@ -105,7 +105,7 @@ The specific lines to remove are as follows: :name: CMakeLists.txt-CXX_STANDARD-variable-remove :language: cmake :start-after: # specify the C++ standard - :end-before: # TODO 5: Create helper variables + :end-before: # TODO 6: Create helper variables Next, we need to create an interface library, ``tutorial_compiler_flags``. And then use :command:`target_compile_features` to add the compiler feature @@ -128,7 +128,8 @@ then use :command:`target_compile_features` to add the compiler feature </details> Finally, with our interface library set up, we need to link our -executable ``Target`` and our ``MathFunctions`` library to our new +executable ``Target``, our ``MathFunctions`` library, and our ``SqrtLibrary`` +library to our new ``tutorial_compiler_flags`` library. Respectively, the code will look like this: @@ -147,7 +148,7 @@ this: </details> -and this: +this: .. raw:: html @@ -158,12 +159,30 @@ and this: :name: MathFunctions-CMakeLists.txt-target_link_libraries-step4 :language: cmake :start-after: # link our compiler flags interface library - :end-before: # TODO 1 + :end-before: target_link_libraries(MathFunctions + +.. raw:: html + + </details> + +and this: + +.. raw:: html + + <details><summary>TODO 4: Click to show/hide answer</summary> + +.. literalinclude:: Step5/MathFunctions/CMakeLists.txt + :caption: TODO 4: MathFunctions/CMakeLists.txt + :name: MathFunctions-SqrtLibrary-target_link_libraries-step4 + :language: cmake + :start-after: target_link_libraries(SqrtLibrary + :end-before: endif() .. raw:: html </details> + With this, all of our code still requires C++ 11 to build. Notice though that with this method, it gives us the ability to be specific about which targets get specific requirements. In addition, we create a single @@ -199,8 +218,8 @@ Files to Edit Getting Started --------------- -Start with the resulting files from Exercise 1. Complete ``TODO 4`` through -``TODO 7``. +Start with the resulting files from Exercise 1. Complete ``TODO 5`` through +``TODO 8``. First, in the top level ``CMakeLists.txt`` file, we need to set the :command:`cmake_minimum_required` to ``3.15``. In this exercise we are going @@ -230,10 +249,10 @@ version ``3.15``: .. raw:: html - <details><summary>TODO 4: Click to show/hide answer</summary> + <details><summary>TODO 5: Click to show/hide answer</summary> .. literalinclude:: Step5/CMakeLists.txt - :caption: TODO 4: CMakeLists.txt + :caption: TODO 5: CMakeLists.txt :name: MathFunctions-CMakeLists.txt-minimum-required-step4 :language: cmake :end-before: # set the project name and version @@ -249,10 +268,10 @@ variables ``gcc_like_cxx`` and ``msvc_cxx`` as follows: .. raw:: html - <details><summary>TODO 5: Click to show/hide answer</summary> + <details><summary>TODO 6: Click to show/hide answer</summary> .. literalinclude:: Step5/CMakeLists.txt - :caption: TODO 5: CMakeLists.txt + :caption: TODO 6: CMakeLists.txt :name: CMakeLists.txt-compile_lang_and_id :language: cmake :start-after: # the BUILD_INTERFACE genex @@ -270,10 +289,10 @@ interface library. .. raw:: html - <details><summary>TODO 6: Click to show/hide answer</summary> + <details><summary>TODO 7: Click to show/hide answer</summary> .. code-block:: cmake - :caption: TODO 6: CMakeLists.txt + :caption: TODO 7: CMakeLists.txt :name: CMakeLists.txt-compile_flags target_compile_options(tutorial_compiler_flags INTERFACE @@ -292,10 +311,10 @@ condition. The resulting full code looks like the following: .. raw:: html - <details><summary>TODO 7: Click to show/hide answer</summary> + <details><summary>TODO 8: Click to show/hide answer</summary> .. literalinclude:: Step5/CMakeLists.txt - :caption: TODO 7: CMakeLists.txt + :caption: TODO 8: CMakeLists.txt :name: CMakeLists.txt-target_compile_options-genex :language: cmake :start-after: set(msvc_cxx "$<COMPILE_LANG_AND_ID:CXX,MSVC>") diff --git a/Help/guide/tutorial/Adding a Custom Command and Generated File.rst b/Help/guide/tutorial/Adding a Custom Command and Generated File.rst index 9e6311e..c71a889 100644 --- a/Help/guide/tutorial/Adding a Custom Command and Generated File.rst +++ b/Help/guide/tutorial/Adding a Custom Command and Generated File.rst @@ -18,17 +18,16 @@ In the ``MathFunctions`` subdirectory, a new source file named After reviewing the file, we can see 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 the -``MathFunctions/CMakeLists.txt`` file to build the MakeTable executable and +The next step is to create ``MathFunctions/MakeTable.cmake``. Then, add the +appropriate commands to the file to build the ``MakeTable`` executable and then run it as part of the build process. A few commands are needed to accomplish this. -First, in the ``USE_MYMATH`` section of ``MathFunctions/CMakeLists.txt``, -we add an executable for ``MakeTable``. +First, we add an executable for ``MakeTable``. -.. literalinclude:: Step9/MathFunctions/CMakeLists.txt - :caption: MathFunctions/CMakeLists.txt - :name: MathFunctions/CMakeLists.txt-add_executable-MakeTable +.. literalinclude:: Step9/MathFunctions/MakeTable.cmake + :caption: MathFunctions/MakeTable.cmake + :name: MathFunctions/MakeTable.cmake-add_executable-MakeTable :language: cmake :start-after: # first we add the executable that generates the table :end-before: target_link_libraries @@ -36,9 +35,9 @@ we add an executable for ``MakeTable``. After creating the executable, we add the ``tutorial_compiler_flags`` to our executable using :command:`target_link_libraries`. -.. literalinclude:: Step9/MathFunctions/CMakeLists.txt - :caption: MathFunctions/CMakeLists.txt - :name: MathFunctions/CMakeLists.txt-link-tutorial-compiler-flags +.. literalinclude:: Step9/MathFunctions/MakeTable.cmake + :caption: MathFunctions/MakeTable.cmake + :name: MathFunctions/MakeTable.cmake-link-tutorial-compiler-flags :language: cmake :start-after: add_executable :end-before: # add the command to generate @@ -46,16 +45,15 @@ executable using :command:`target_link_libraries`. Then we add a custom command that specifies how to produce ``Table.h`` by running MakeTable. -.. literalinclude:: Step9/MathFunctions/CMakeLists.txt - :caption: MathFunctions/CMakeLists.txt - :name: MathFunctions/CMakeLists.txt-add_custom_command-Table.h +.. literalinclude:: Step9/MathFunctions/MakeTable.cmake + :caption: MathFunctions/MakeTable.cmake + :name: MathFunctions/MakeTable.cmake-add_custom_command-Table.h :language: cmake :start-after: # add the command to generate the source code - :end-before: # library that just does sqrt 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. +of sources for the library ``SqrtLibrary``. .. literalinclude:: Step9/MathFunctions/CMakeLists.txt :caption: MathFunctions/CMakeLists.txt @@ -74,17 +72,15 @@ directories so that ``Table.h`` can be found and included by ``mysqrt.cxx``. :start-after: # state that we depend on our bin :end-before: target_link_libraries -As the last thing in our ``USE_MYMATH`` section, we need to link the our -flags onto ``SqrtLibrary`` and then link ``SqrtLibrary`` onto -``MathFunctions``. This makes the resulting ``USE_MYMATH`` section look like -the following: +As the last step, we need to include +``MakeTable.cmake`` at the top of the ``MathFunctions/CMakeLists.txt``. .. literalinclude:: Step9/MathFunctions/CMakeLists.txt :caption: MathFunctions/CMakeLists.txt - :name: MathFunctions/CMakeLists.txt-full_USE_MYMATH-section + :name: MathFunctions/CMakeLists.txt-include-MakeTable.cmake :language: cmake - :start-after: if (USE_MYMATH) - :end-before: endif() + :start-after: # generate Table.h + :end-before: # library that just does sqrt 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: diff --git a/Help/guide/tutorial/Adding a Library.rst b/Help/guide/tutorial/Adding a Library.rst index 2dd731f..d606f30 100644 --- a/Help/guide/tutorial/Adding a Library.rst +++ b/Help/guide/tutorial/Adding a Library.rst @@ -100,11 +100,11 @@ source files for the library are passed as an argument to <details><summary>TODO 1: Click to show/hide answer</summary> -.. literalinclude:: Step3/MathFunctions/CMakeLists.txt +.. code-block:: cmake :caption: TODO 1: MathFunctions/CMakeLists.txt :name: MathFunctions/CMakeLists.txt-add_library - :language: cmake - :end-before: # TODO 1 + + add_library(MathFunctions MathFunctions.cxx mysqrt.cxx) .. raw:: html @@ -191,7 +191,7 @@ Lastly, replace ``sqrt`` with our library function ``mathfunctions::mysqrt``. <details><summary>TODO 6: Click to show/hide answer</summary> .. literalinclude:: Step3/tutorial.cxx - :caption: TODO 7: tutorial.cxx + :caption: TODO 6: tutorial.cxx :name: CMakeLists.txt-option :language: cmake :start-after: const double inputValue = std::stod(argv[1]); @@ -238,7 +238,7 @@ Getting Started --------------- Start with the resulting files from Exercise 1. Complete ``TODO 7`` through -``TODO 9``. +``TODO 14``. First create a variable ``USE_MYMATH`` using the :command:`option` command in ``MathFunctions/CMakeLists.txt``. In that same file, use that option @@ -247,6 +247,10 @@ to pass a compile definition to the ``MathFunctions`` library. Then, update ``MathFunctions.cxx`` to redirect compilation based on ``USE_MYMATH``. +Lastly, prevent ``mysqrt.cxx`` from being compiled when ``USE_MYMATH`` is on +by making it its own library inside of the ``USE_MYMATH`` block of +``MathFunctions/CMakeLists.txt``. + Build and Run ------------- @@ -315,16 +319,22 @@ definition ``USE_MYMATH``. <details><summary>TODO 8: Click to show/hide answer</summary> -.. literalinclude:: Step3/MathFunctions/CMakeLists.txt +.. code-block:: cmake :caption: TODO 8: MathFunctions/CMakeLists.txt :name: CMakeLists.txt-USE_MYMATH - :language: cmake - :start-after: USE_MYMATH "Use tutorial provided math implementation" ON) + + if (USE_MYMATH) + target_compile_definitions(MathFunctions PRIVATE "USE_MYMATH") + endif() .. raw:: html </details> +When ``USE_MYMATH`` is ``ON``, the compile definition ``USE_MYMATH`` will +be set. We can then use this compile definition to enable or disable +sections of our source code. + The corresponding changes to the source code are fairly straightforward. In ``MathFunctions.cxx``, we make ``USE_MYMATH`` control which square root function is used: @@ -377,10 +387,68 @@ Finally, we need to include ``cmath`` now that we are using ``std::sqrt``. </details> -When ``USE_MYMATH`` is ``ON``, the compile definition ``USE_MYMATH`` will -be set. We can then use this compile definition to enable or disable -sections of our source code. With this strategy, we allow users to -toggle ``USE_MYMATH`` to manipulate what library is used in the build. +At this point, if ``USE_MYMATH`` is ``OFF``, ``mysqrt.cxx`` would not be used +but it will still be compiled because the ``MathFunctions`` target has +``mysqrt.cxx`` listed under sources. + +There are a few ways to fix this. The first option is to use +:command:`target_sources` to add ``mysqrt.cxx`` from within the ``USE_MYMATH`` +block. Another option is to create an additional library within the +``USE_MYMATH`` block which is responsible for compiling ``mysqrt.cxx``. For +the sake of this tutorial, we are going to create an additional library. + +First, from within ``USE_MYMATH`` create a library called ``SqrtLibrary`` +that has sources ``mysqrt.cxx``. + +.. raw:: html + + <details><summary>TODO 12: Click to show/hide answer</summary> + +.. literalinclude:: Step3/MathFunctions/CMakeLists.txt + :caption: TODO 12 : MathFunctions/CMakeLists.txt + :name: MathFunctions/CMakeLists.txt-add_library-SqrtLibrary + :language: cmake + :start-after: # library that just does sqrt + :end-before: target_link_libraries(MathFunctions + +.. raw:: html + + </details> + +Next, we link ``SqrtLibrary`` onto ``MathFunctions`` when ``USE_MYMATH`` is +enabled. + +.. raw:: html + + <details><summary>TODO 13: Click to show/hide answer</summary> + +.. literalinclude:: Step3/MathFunctions/CMakeLists.txt + :caption: TODO 13 : MathFunctions/CMakeLists.txt + :name: MathFunctions/CMakeLists.txt-target_link_libraries-SqrtLibrary + :language: cmake + :lines: 16-18 + +.. raw:: html + + </details> + +Finally, we can remove ``mysqrt.cxx`` from our ``MathFunctions`` library +source list because it will be pulled in when ``SqrtLibrary`` is included. + +.. raw:: html + + <details><summary>TODO 14: Click to show/hide answer</summary> + +.. literalinclude:: Step3/MathFunctions/CMakeLists.txt + :caption: TODO 14 : MathFunctions/CMakeLists.txt + :name: MathFunctions/CMakeLists.txt-remove-mysqrt.cxx-MathFunctions + :language: cmake + :end-before: # TODO 1: + +.. raw:: html + + </details> With these changes, the ``mysqrt`` function is now completely optional to -whoever is building and using the ``MathFunctions`` library. +whoever is building and using the ``MathFunctions`` library. Users can toggle +``USE_MYMATH`` to manipulate what library is used in the build. diff --git a/Help/guide/tutorial/Complete/MathFunctions/CMakeLists.txt b/Help/guide/tutorial/Complete/MathFunctions/CMakeLists.txt index d256db2..b221506 100644 --- a/Help/guide/tutorial/Complete/MathFunctions/CMakeLists.txt +++ b/Help/guide/tutorial/Complete/MathFunctions/CMakeLists.txt @@ -15,16 +15,7 @@ if(USE_MYMATH) target_compile_definitions(MathFunctions PRIVATE "USE_MYMATH") - # 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 - ) + include(MakeTable.cmake) # generates Table.h # library that just does sqrt add_library(SqrtLibrary STATIC diff --git a/Help/guide/tutorial/Complete/MathFunctions/MakeTable.cmake b/Help/guide/tutorial/Complete/MathFunctions/MakeTable.cmake new file mode 100644 index 0000000..12865a9 --- /dev/null +++ b/Help/guide/tutorial/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/Help/guide/tutorial/Step10/MathFunctions/CMakeLists.txt b/Help/guide/tutorial/Step10/MathFunctions/CMakeLists.txt index 6f6c430..36b3fe1 100644 --- a/Help/guide/tutorial/Step10/MathFunctions/CMakeLists.txt +++ b/Help/guide/tutorial/Step10/MathFunctions/CMakeLists.txt @@ -13,16 +13,7 @@ if(USE_MYMATH) target_compile_definitions(MathFunctions PRIVATE "USE_MYMATH") - # 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 - ) + include(MakeTable.cmake) # generates Table.h # library that just does sqrt add_library(SqrtLibrary STATIC diff --git a/Help/guide/tutorial/Step10/MathFunctions/MakeTable.cmake b/Help/guide/tutorial/Step10/MathFunctions/MakeTable.cmake new file mode 100644 index 0000000..12865a9 --- /dev/null +++ b/Help/guide/tutorial/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/Help/guide/tutorial/Step11/MathFunctions/CMakeLists.txt b/Help/guide/tutorial/Step11/MathFunctions/CMakeLists.txt index a60fb63..813bf90 100644 --- a/Help/guide/tutorial/Step11/MathFunctions/CMakeLists.txt +++ b/Help/guide/tutorial/Step11/MathFunctions/CMakeLists.txt @@ -13,16 +13,7 @@ if(USE_MYMATH) target_compile_definitions(MathFunctions PRIVATE "USE_MYMATH") - # 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 - ) + include(MakeTable.cmake) # generates Table.h # library that just does sqrt add_library(SqrtLibrary STATIC diff --git a/Help/guide/tutorial/Step11/MathFunctions/MakeTable.cmake b/Help/guide/tutorial/Step11/MathFunctions/MakeTable.cmake new file mode 100644 index 0000000..12865a9 --- /dev/null +++ b/Help/guide/tutorial/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/Help/guide/tutorial/Step12/MathFunctions/CMakeLists.txt b/Help/guide/tutorial/Step12/MathFunctions/CMakeLists.txt index a85f3cb..38694dd 100644 --- a/Help/guide/tutorial/Step12/MathFunctions/CMakeLists.txt +++ b/Help/guide/tutorial/Step12/MathFunctions/CMakeLists.txt @@ -15,16 +15,7 @@ if(USE_MYMATH) target_compile_definitions(MathFunctions PRIVATE "USE_MYMATH") - # 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 - ) + include(MakeTable.cmake) # generates Table.h # library that just does sqrt add_library(SqrtLibrary STATIC diff --git a/Help/guide/tutorial/Step12/MathFunctions/MakeTable.cmake b/Help/guide/tutorial/Step12/MathFunctions/MakeTable.cmake new file mode 100644 index 0000000..12865a9 --- /dev/null +++ b/Help/guide/tutorial/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/Help/guide/tutorial/Step2/MathFunctions/CMakeLists.txt b/Help/guide/tutorial/Step2/MathFunctions/CMakeLists.txt index c468b0e..c3cd806 100644 --- a/Help/guide/tutorial/Step2/MathFunctions/CMakeLists.txt +++ b/Help/guide/tutorial/Step2/MathFunctions/CMakeLists.txt @@ -1,7 +1,15 @@ -# TODO 1: Add a library called MathFunctions +# 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/Help/guide/tutorial/Step3/MathFunctions/CMakeLists.txt b/Help/guide/tutorial/Step3/MathFunctions/CMakeLists.txt index 90d6c24..6f86ffe 100644 --- a/Help/guide/tutorial/Step3/MathFunctions/CMakeLists.txt +++ b/Help/guide/tutorial/Step3/MathFunctions/CMakeLists.txt @@ -1,4 +1,4 @@ -add_library(MathFunctions MathFunctions.cxx mysqrt.cxx) +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. @@ -8,4 +8,11 @@ add_library(MathFunctions MathFunctions.cxx mysqrt.cxx) 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 + ) + + target_link_libraries(MathFunctions PUBLIC SqrtLibrary) endif() diff --git a/Help/guide/tutorial/Step4/CMakeLists.txt b/Help/guide/tutorial/Step4/CMakeLists.txt index dcda135..7531fb4 100644 --- a/Help/guide/tutorial/Step4/CMakeLists.txt +++ b/Help/guide/tutorial/Step4/CMakeLists.txt @@ -1,4 +1,4 @@ -# TODO 4: Update the minimum required version to 3.15 +# TODO 5: Update the minimum required version to 3.15 cmake_minimum_required(VERSION 3.10) @@ -15,19 +15,19 @@ project(Tutorial VERSION 1.0) set(CMAKE_CXX_STANDARD 11) set(CMAKE_CXX_STANDARD_REQUIRED True) -# TODO 5: Create helper variables to determine which compiler we are using: +# TODO 6: 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 6: Add warning flag compile options to the interface library +# TODO 7: 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 7: With nested generator expressions, only use the flags for the +# TODO 8: With nested generator expressions, only use the flags for the # build-tree # Hint: Use BUILD_INTERFACE diff --git a/Help/guide/tutorial/Step4/MathFunctions/CMakeLists.txt b/Help/guide/tutorial/Step4/MathFunctions/CMakeLists.txt index cc71d86..ffab4f0 100644 --- a/Help/guide/tutorial/Step4/MathFunctions/CMakeLists.txt +++ b/Help/guide/tutorial/Step4/MathFunctions/CMakeLists.txt @@ -1,4 +1,5 @@ -add_library(MathFunctions MathFunctions.cxx mysqrt.cxx) +# 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. @@ -10,6 +11,15 @@ target_include_directories(MathFunctions 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 4: Link to tutorial_compiler_flags + + target_link_libraries(MathFunctions PUBLIC SqrtLibrary) endif() # TODO 3: Link to tutorial_compiler_flags diff --git a/Help/guide/tutorial/Step5/MathFunctions/CMakeLists.txt b/Help/guide/tutorial/Step5/MathFunctions/CMakeLists.txt index 000a786..0c688f2 100644 --- a/Help/guide/tutorial/Step5/MathFunctions/CMakeLists.txt +++ b/Help/guide/tutorial/Step5/MathFunctions/CMakeLists.txt @@ -1,4 +1,4 @@ -add_library(MathFunctions MathFunctions.cxx mysqrt.cxx) +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. @@ -10,6 +10,15 @@ target_include_directories(MathFunctions 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 our compiler flags interface library + target_link_libraries(SqrtLibrary PUBLIC tutorial_compiler_flags) + target_link_libraries(MathFunctions PRIVATE SqrtLibrary) endif() # link our compiler flags interface library diff --git a/Help/guide/tutorial/Step6/MathFunctions/CMakeLists.txt b/Help/guide/tutorial/Step6/MathFunctions/CMakeLists.txt index 623cb74..b1b925e 100644 --- a/Help/guide/tutorial/Step6/MathFunctions/CMakeLists.txt +++ b/Help/guide/tutorial/Step6/MathFunctions/CMakeLists.txt @@ -1,4 +1,4 @@ -add_library(MathFunctions MathFunctions.cxx mysqrt.cxx) +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. @@ -10,6 +10,14 @@ target_include_directories(MathFunctions 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 + ) + + target_link_libraries(SqrtLibrary PUBLIC tutorial_compiler_flags) + target_link_libraries(MathFunctions PRIVATE SqrtLibrary) endif() # link our compiler flags interface library @@ -17,6 +25,9 @@ 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/Help/guide/tutorial/Step7/MathFunctions/CMakeLists.txt b/Help/guide/tutorial/Step7/MathFunctions/CMakeLists.txt index c0d1e72..897ec0e 100644 --- a/Help/guide/tutorial/Step7/MathFunctions/CMakeLists.txt +++ b/Help/guide/tutorial/Step7/MathFunctions/CMakeLists.txt @@ -1,4 +1,4 @@ -add_library(MathFunctions MathFunctions.cxx mysqrt.cxx) +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. @@ -11,6 +11,13 @@ 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 + ) + + 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 @@ -27,9 +34,11 @@ if (USE_MYMATH) # } # TODO 3: Conditionally on HAVE_LOG and HAVE_EXP, add private compile - # definitions "HAVE_LOG" and "HAVE_EXP" to the MathFunctions target. + # definitions "HAVE_LOG" and "HAVE_EXP" to the SqrtLibrary target. # Hint: Use target_compile_definitions() + + target_link_libraries(MathFunctions PRIVATE SqrtLibrary) endif() # link our compiler flags interface library @@ -37,6 +46,9 @@ 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/Help/guide/tutorial/Step8/MathFunctions/CMakeLists.txt b/Help/guide/tutorial/Step8/MathFunctions/CMakeLists.txt index 861014d..872a24a 100644 --- a/Help/guide/tutorial/Step8/MathFunctions/CMakeLists.txt +++ b/Help/guide/tutorial/Step8/MathFunctions/CMakeLists.txt @@ -1,10 +1,17 @@ -add_library(MathFunctions MathFunctions.cxx mysqrt.cxx) +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 + ) + + target_link_libraries(SqrtLibrary PUBLIC tutorial_compiler_flags) + # does this system provide the log and exp functions? include(CheckCXXSourceCompiles) check_cxx_source_compiles(" @@ -24,10 +31,12 @@ if (USE_MYMATH) # add compile definitions if(HAVE_LOG AND HAVE_EXP) - target_compile_definitions(MathFunctions + 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 @@ -41,6 +50,9 @@ 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/Help/guide/tutorial/Step9/MathFunctions/CMakeLists.txt b/Help/guide/tutorial/Step9/MathFunctions/CMakeLists.txt index 05c8616..54cecf8 100644 --- a/Help/guide/tutorial/Step9/MathFunctions/CMakeLists.txt +++ b/Help/guide/tutorial/Step9/MathFunctions/CMakeLists.txt @@ -11,16 +11,8 @@ option(USE_MYMATH "Use tutorial provided math implementation" ON) if (USE_MYMATH) target_compile_definitions(MathFunctions PRIVATE "USE_MYMATH") - # 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 - ) + # generate Table.h + include(MakeTable.cmake) # library that just does sqrt add_library(SqrtLibrary STATIC @@ -30,8 +22,8 @@ if (USE_MYMATH) # state that we depend on our binary dir to find Table.h target_include_directories(SqrtLibrary PRIVATE - ${CMAKE_CURRENT_BINARY_DIR} - ) + ${CMAKE_CURRENT_BINARY_DIR} + ) target_link_libraries(SqrtLibrary PUBLIC tutorial_compiler_flags) target_link_libraries(MathFunctions PRIVATE SqrtLibrary) diff --git a/Help/guide/tutorial/Step9/MathFunctions/MakeTable.cmake b/Help/guide/tutorial/Step9/MathFunctions/MakeTable.cmake new file mode 100644 index 0000000..12865a9 --- /dev/null +++ b/Help/guide/tutorial/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 + ) |