Tutorial: Refactor MakeTable commands into MakeTable.cmake

This commit is contained in:
Markus Ferrell 2023-03-03 10:48:17 -05:00
parent 35ca2d524b
commit e1f2b35723
21 changed files with 272 additions and 114 deletions

View File

@ -105,7 +105,7 @@ The specific lines to remove are as follows:
:name: CMakeLists.txt-CXX_STANDARD-variable-remove :name: CMakeLists.txt-CXX_STANDARD-variable-remove
:language: cmake :language: cmake
:start-after: # specify the C++ standard :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 Next, we need to create an interface library, ``tutorial_compiler_flags``. And
then use :command:`target_compile_features` to add the compiler feature 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> </details>
Finally, with our interface library set up, we need to link our 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 ``tutorial_compiler_flags`` library. Respectively, the code will look like
this: this:
@ -147,7 +148,7 @@ this:
</details> </details>
and this: this:
.. raw:: html .. raw:: html
@ -158,12 +159,30 @@ and this:
:name: MathFunctions-CMakeLists.txt-target_link_libraries-step4 :name: MathFunctions-CMakeLists.txt-target_link_libraries-step4
:language: cmake :language: cmake
:start-after: # link our compiler flags interface library :start-after: # link our compiler flags interface library
:end-before: # TODO 1 :end-before: target_link_libraries(MathFunctions
.. raw:: html .. raw:: html
</details> </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 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 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 which targets get specific requirements. In addition, we create a single
@ -199,8 +218,8 @@ Files to Edit
Getting Started Getting Started
--------------- ---------------
Start with the resulting files from Exercise 1. Complete ``TODO 4`` through Start with the resulting files from Exercise 1. Complete ``TODO 5`` through
``TODO 7``. ``TODO 8``.
First, in the top level ``CMakeLists.txt`` file, we need to set the 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 :command:`cmake_minimum_required` to ``3.15``. In this exercise we are going
@ -230,10 +249,10 @@ version ``3.15``:
.. raw:: html .. 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 .. literalinclude:: Step5/CMakeLists.txt
:caption: TODO 4: CMakeLists.txt :caption: TODO 5: CMakeLists.txt
:name: MathFunctions-CMakeLists.txt-minimum-required-step4 :name: MathFunctions-CMakeLists.txt-minimum-required-step4
:language: cmake :language: cmake
:end-before: # set the project name and version :end-before: # set the project name and version
@ -249,10 +268,10 @@ variables ``gcc_like_cxx`` and ``msvc_cxx`` as follows:
.. raw:: html .. 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 .. literalinclude:: Step5/CMakeLists.txt
:caption: TODO 5: CMakeLists.txt :caption: TODO 6: CMakeLists.txt
:name: CMakeLists.txt-compile_lang_and_id :name: CMakeLists.txt-compile_lang_and_id
:language: cmake :language: cmake
:start-after: # the BUILD_INTERFACE genex :start-after: # the BUILD_INTERFACE genex
@ -270,10 +289,10 @@ interface library.
.. raw:: html .. 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 .. code-block:: cmake
:caption: TODO 6: CMakeLists.txt :caption: TODO 7: CMakeLists.txt
:name: CMakeLists.txt-compile_flags :name: CMakeLists.txt-compile_flags
target_compile_options(tutorial_compiler_flags INTERFACE target_compile_options(tutorial_compiler_flags INTERFACE
@ -292,10 +311,10 @@ condition. The resulting full code looks like the following:
.. raw:: html .. 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 .. literalinclude:: Step5/CMakeLists.txt
:caption: TODO 7: CMakeLists.txt :caption: TODO 8: CMakeLists.txt
:name: CMakeLists.txt-target_compile_options-genex :name: CMakeLists.txt-target_compile_options-genex
:language: cmake :language: cmake
:start-after: set(msvc_cxx "$<COMPILE_LANG_AND_ID:CXX,MSVC>") :start-after: set(msvc_cxx "$<COMPILE_LANG_AND_ID:CXX,MSVC>")

View File

@ -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++ 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. code and that the output filename is passed in as an argument.
The next step is to add the appropriate commands to the The next step is to create ``MathFunctions/MakeTable.cmake``. Then, add the
``MathFunctions/CMakeLists.txt`` file to build the MakeTable executable and 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 then run it as part of the build process. A few commands are needed to
accomplish this. accomplish this.
First, in the ``USE_MYMATH`` section of ``MathFunctions/CMakeLists.txt``, First, we add an executable for ``MakeTable``.
we add an executable for ``MakeTable``.
.. literalinclude:: Step9/MathFunctions/CMakeLists.txt .. literalinclude:: Step9/MathFunctions/MakeTable.cmake
:caption: MathFunctions/CMakeLists.txt :caption: MathFunctions/MakeTable.cmake
:name: MathFunctions/CMakeLists.txt-add_executable-MakeTable :name: MathFunctions/MakeTable.cmake-add_executable-MakeTable
:language: cmake :language: cmake
:start-after: # first we add the executable that generates the table :start-after: # first we add the executable that generates the table
:end-before: target_link_libraries :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 After creating the executable, we add the ``tutorial_compiler_flags`` to our
executable using :command:`target_link_libraries`. executable using :command:`target_link_libraries`.
.. literalinclude:: Step9/MathFunctions/CMakeLists.txt .. literalinclude:: Step9/MathFunctions/MakeTable.cmake
:caption: MathFunctions/CMakeLists.txt :caption: MathFunctions/MakeTable.cmake
:name: MathFunctions/CMakeLists.txt-link-tutorial-compiler-flags :name: MathFunctions/MakeTable.cmake-link-tutorial-compiler-flags
:language: cmake :language: cmake
:start-after: add_executable :start-after: add_executable
:end-before: # add the command to generate :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`` Then we add a custom command that specifies how to produce ``Table.h``
by running MakeTable. by running MakeTable.
.. literalinclude:: Step9/MathFunctions/CMakeLists.txt .. literalinclude:: Step9/MathFunctions/MakeTable.cmake
:caption: MathFunctions/CMakeLists.txt :caption: MathFunctions/MakeTable.cmake
:name: MathFunctions/CMakeLists.txt-add_custom_command-Table.h :name: MathFunctions/MakeTable.cmake-add_custom_command-Table.h
:language: cmake :language: cmake
:start-after: # add the command to generate the source code :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 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 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 .. literalinclude:: Step9/MathFunctions/CMakeLists.txt
:caption: 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 :start-after: # state that we depend on our bin
:end-before: target_link_libraries :end-before: target_link_libraries
As the last thing in our ``USE_MYMATH`` section, we need to link the our As the last step, we need to include
flags onto ``SqrtLibrary`` and then link ``SqrtLibrary`` onto ``MakeTable.cmake`` at the top of the ``MathFunctions/CMakeLists.txt``.
``MathFunctions``. This makes the resulting ``USE_MYMATH`` section look like
the following:
.. literalinclude:: Step9/MathFunctions/CMakeLists.txt .. literalinclude:: Step9/MathFunctions/CMakeLists.txt
:caption: MathFunctions/CMakeLists.txt :caption: MathFunctions/CMakeLists.txt
:name: MathFunctions/CMakeLists.txt-full_USE_MYMATH-section :name: MathFunctions/CMakeLists.txt-include-MakeTable.cmake
:language: cmake :language: cmake
:start-after: if (USE_MYMATH) :start-after: # generate Table.h
:end-before: endif() :end-before: # library that just does sqrt
Now let's use the generated table. First, modify ``mysqrt.cxx`` to include 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: ``Table.h``. Next, we can rewrite the ``mysqrt`` function to use the table:

View File

@ -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> <details><summary>TODO 1: Click to show/hide answer</summary>
.. literalinclude:: Step3/MathFunctions/CMakeLists.txt .. code-block:: cmake
:caption: TODO 1: MathFunctions/CMakeLists.txt :caption: TODO 1: MathFunctions/CMakeLists.txt
:name: MathFunctions/CMakeLists.txt-add_library :name: MathFunctions/CMakeLists.txt-add_library
:language: cmake
:end-before: # TODO 1 add_library(MathFunctions MathFunctions.cxx mysqrt.cxx)
.. raw:: html .. 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> <details><summary>TODO 6: Click to show/hide answer</summary>
.. literalinclude:: Step3/tutorial.cxx .. literalinclude:: Step3/tutorial.cxx
:caption: TODO 7: tutorial.cxx :caption: TODO 6: tutorial.cxx
:name: CMakeLists.txt-option :name: CMakeLists.txt-option
:language: cmake :language: cmake
:start-after: const double inputValue = std::stod(argv[1]); :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 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 First create a variable ``USE_MYMATH`` using the :command:`option` command
in ``MathFunctions/CMakeLists.txt``. In that same file, use that option 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 Then, update ``MathFunctions.cxx`` to redirect compilation based on
``USE_MYMATH``. ``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 Build and Run
------------- -------------
@ -315,16 +319,22 @@ definition ``USE_MYMATH``.
<details><summary>TODO 8: Click to show/hide answer</summary> <details><summary>TODO 8: Click to show/hide answer</summary>
.. literalinclude:: Step3/MathFunctions/CMakeLists.txt .. code-block:: cmake
:caption: TODO 8: MathFunctions/CMakeLists.txt :caption: TODO 8: MathFunctions/CMakeLists.txt
:name: CMakeLists.txt-USE_MYMATH :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 .. raw:: html
</details> </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. The corresponding changes to the source code are fairly straightforward.
In ``MathFunctions.cxx``, we make ``USE_MYMATH`` control which square root In ``MathFunctions.cxx``, we make ``USE_MYMATH`` control which square root
function is used: function is used:
@ -377,10 +387,68 @@ Finally, we need to include ``cmath`` now that we are using ``std::sqrt``.
</details> </details>
When ``USE_MYMATH`` is ``ON``, the compile definition ``USE_MYMATH`` will At this point, if ``USE_MYMATH`` is ``OFF``, ``mysqrt.cxx`` would not be used
be set. We can then use this compile definition to enable or disable but it will still be compiled because the ``MathFunctions`` target has
sections of our source code. With this strategy, we allow users to ``mysqrt.cxx`` listed under sources.
toggle ``USE_MYMATH`` to manipulate what library is used in the build.
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 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.

View File

@ -15,16 +15,7 @@ if(USE_MYMATH)
target_compile_definitions(MathFunctions PRIVATE "USE_MYMATH") target_compile_definitions(MathFunctions PRIVATE "USE_MYMATH")
# first we add the executable that generates the table include(MakeTable.cmake) # generates Table.h
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
)
# library that just does sqrt # library that just does sqrt
add_library(SqrtLibrary STATIC add_library(SqrtLibrary STATIC

View File

@ -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
)

View File

@ -13,16 +13,7 @@ if(USE_MYMATH)
target_compile_definitions(MathFunctions PRIVATE "USE_MYMATH") target_compile_definitions(MathFunctions PRIVATE "USE_MYMATH")
# first we add the executable that generates the table include(MakeTable.cmake) # generates Table.h
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
)
# library that just does sqrt # library that just does sqrt
add_library(SqrtLibrary STATIC add_library(SqrtLibrary STATIC

View File

@ -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
)

View File

@ -13,16 +13,7 @@ if(USE_MYMATH)
target_compile_definitions(MathFunctions PRIVATE "USE_MYMATH") target_compile_definitions(MathFunctions PRIVATE "USE_MYMATH")
# first we add the executable that generates the table include(MakeTable.cmake) # generates Table.h
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
)
# library that just does sqrt # library that just does sqrt
add_library(SqrtLibrary STATIC add_library(SqrtLibrary STATIC

View File

@ -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
)

View File

@ -15,16 +15,7 @@ if(USE_MYMATH)
target_compile_definitions(MathFunctions PRIVATE "USE_MYMATH") target_compile_definitions(MathFunctions PRIVATE "USE_MYMATH")
# first we add the executable that generates the table include(MakeTable.cmake) # generates Table.h
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
)
# library that just does sqrt # library that just does sqrt
add_library(SqrtLibrary STATIC add_library(SqrtLibrary STATIC

View File

@ -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
)

View File

@ -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 # Hint: You will need the add_library command
# TODO 7: Create a variable USE_MYMATH using option and set default to ON # 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 # TODO 8: If USE_MYMATH is ON, use target_compile_definitions to pass
# USE_MYMATH as a precompiled definition to our source files # 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

View File

@ -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 # TODO 1: State that anybody linking to MathFunctions needs to include the
# current source directory, while MathFunctions itself doesn't. # 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) option(USE_MYMATH "Use tutorial provided math implementation" ON)
if (USE_MYMATH) if (USE_MYMATH)
target_compile_definitions(MathFunctions PRIVATE "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() endif()

View File

@ -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) cmake_minimum_required(VERSION 3.10)
@ -15,19 +15,19 @@ project(Tutorial VERSION 1.0)
set(CMAKE_CXX_STANDARD 11) set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED True) 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 # * 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 # 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 # * Create a new variable msvc_cxx that is true if we are using CXX and MSVC
# Hint: Use set() and COMPILE_LANG_AND_ID # 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. # tutorial_compiler_flags.
# * For gcc_like_cxx, add flags -Wall;-Wextra;-Wshadow;-Wformat=2;-Wunused # * For gcc_like_cxx, add flags -Wall;-Wextra;-Wshadow;-Wformat=2;-Wunused
# * For msvc_cxx, add flags -W3 # * For msvc_cxx, add flags -W3
# Hint: Use target_compile_options() # 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 # build-tree
# Hint: Use BUILD_INTERFACE # Hint: Use BUILD_INTERFACE

View File

@ -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 # state that anybody linking to us needs to include the current source dir
# to find MathFunctions.h, while we don't. # 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) option(USE_MYMATH "Use tutorial provided math implementation" ON)
if (USE_MYMATH) if (USE_MYMATH)
target_compile_definitions(MathFunctions PRIVATE "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() endif()
# TODO 3: Link to tutorial_compiler_flags # TODO 3: Link to tutorial_compiler_flags

View File

@ -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 # state that anybody linking to us needs to include the current source dir
# to find MathFunctions.h, while we don't. # 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) option(USE_MYMATH "Use tutorial provided math implementation" ON)
if (USE_MYMATH) if (USE_MYMATH)
target_compile_definitions(MathFunctions PRIVATE "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() endif()
# link our compiler flags interface library # link our compiler flags interface library

View File

@ -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 # state that anybody linking to us needs to include the current source dir
# to find MathFunctions.h, while we don't. # 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) option(USE_MYMATH "Use tutorial provided math implementation" ON)
if (USE_MYMATH) if (USE_MYMATH)
target_compile_definitions(MathFunctions PRIVATE "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() endif()
# link our compiler flags interface library # link our compiler flags interface library
@ -17,6 +25,9 @@ target_link_libraries(MathFunctions PUBLIC tutorial_compiler_flags)
# install libs # install libs
set(installable_libs MathFunctions tutorial_compiler_flags) set(installable_libs MathFunctions tutorial_compiler_flags)
if(TARGET SqrtLibrary)
list(APPEND installable_libs SqrtLibrary)
endif()
install(TARGETS ${installable_libs} DESTINATION lib) install(TARGETS ${installable_libs} DESTINATION lib)
# install include headers # install include headers
install(FILES MathFunctions.h DESTINATION include) install(FILES MathFunctions.h DESTINATION include)

View File

@ -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 # state that anybody linking to us needs to include the current source dir
# to find MathFunctions.h, while we don't. # to find MathFunctions.h, while we don't.
@ -11,6 +11,13 @@ option(USE_MYMATH "Use tutorial provided math implementation" ON)
if (USE_MYMATH) if (USE_MYMATH)
target_compile_definitions(MathFunctions PRIVATE "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 1: Include CheckCXXSourceCompiles
# TODO 2: Use check_cxx_source_compiles with simple C++ code to verify # 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 # 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() # Hint: Use target_compile_definitions()
target_link_libraries(MathFunctions PRIVATE SqrtLibrary)
endif() endif()
# link our compiler flags interface library # link our compiler flags interface library
@ -37,6 +46,9 @@ target_link_libraries(MathFunctions PUBLIC tutorial_compiler_flags)
# install libs # install libs
set(installable_libs MathFunctions tutorial_compiler_flags) set(installable_libs MathFunctions tutorial_compiler_flags)
if(TARGET SqrtLibrary)
list(APPEND installable_libs SqrtLibrary)
endif()
install(TARGETS ${installable_libs} DESTINATION lib) install(TARGETS ${installable_libs} DESTINATION lib)
# install include headers # install include headers
install(FILES MathFunctions.h DESTINATION include) install(FILES MathFunctions.h DESTINATION include)

View File

@ -1,10 +1,17 @@
add_library(MathFunctions MathFunctions.cxx mysqrt.cxx) add_library(MathFunctions MathFunctions.cxx)
# should we use our own math functions # should we use our own math functions
option(USE_MYMATH "Use tutorial provided math implementation" ON) option(USE_MYMATH "Use tutorial provided math implementation" ON)
if (USE_MYMATH) if (USE_MYMATH)
target_compile_definitions(MathFunctions PRIVATE "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? # does this system provide the log and exp functions?
include(CheckCXXSourceCompiles) include(CheckCXXSourceCompiles)
check_cxx_source_compiles(" check_cxx_source_compiles("
@ -24,10 +31,12 @@ if (USE_MYMATH)
# add compile definitions # add compile definitions
if(HAVE_LOG AND HAVE_EXP) if(HAVE_LOG AND HAVE_EXP)
target_compile_definitions(MathFunctions target_compile_definitions(SqrtLibrary
PRIVATE "HAVE_LOG" "HAVE_EXP" PRIVATE "HAVE_LOG" "HAVE_EXP"
) )
endif() endif()
target_link_libraries(MathFunctions PRIVATE SqrtLibrary)
endif() endif()
# state that anybody linking to us needs to include the current source dir # 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 # install libs
set(installable_libs MathFunctions tutorial_compiler_flags) set(installable_libs MathFunctions tutorial_compiler_flags)
if(TARGET SqrtLibrary)
list(APPEND installable_libs SqrtLibrary)
endif()
install(TARGETS ${installable_libs} DESTINATION lib) install(TARGETS ${installable_libs} DESTINATION lib)
# install include headers # install include headers
install(FILES MathFunctions.h DESTINATION include) install(FILES MathFunctions.h DESTINATION include)

View File

@ -11,16 +11,8 @@ option(USE_MYMATH "Use tutorial provided math implementation" ON)
if (USE_MYMATH) if (USE_MYMATH)
target_compile_definitions(MathFunctions PRIVATE "USE_MYMATH") target_compile_definitions(MathFunctions PRIVATE "USE_MYMATH")
# first we add the executable that generates the table # generate Table.h
add_executable(MakeTable MakeTable.cxx) include(MakeTable.cmake)
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
)
# library that just does sqrt # library that just does sqrt
add_library(SqrtLibrary STATIC add_library(SqrtLibrary STATIC
@ -30,8 +22,8 @@ if (USE_MYMATH)
# state that we depend on our binary dir to find Table.h # state that we depend on our binary dir to find Table.h
target_include_directories(SqrtLibrary PRIVATE target_include_directories(SqrtLibrary PRIVATE
${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_BINARY_DIR}
) )
target_link_libraries(SqrtLibrary PUBLIC tutorial_compiler_flags) target_link_libraries(SqrtLibrary PUBLIC tutorial_compiler_flags)
target_link_libraries(MathFunctions PRIVATE SqrtLibrary) target_link_libraries(MathFunctions PRIVATE SqrtLibrary)

View File

@ -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
)