Adding a Custom Command and Generated File.rst 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. Step 6: Adding a Custom Command and Generated File
  2. ==================================================
  3. Suppose, for the purpose of this tutorial, we decide that we never want to use
  4. the platform ``log`` and ``exp`` functions and instead would like to
  5. generate a table of precomputed values to use in the ``mysqrt`` function.
  6. In this section, we will create the table as part of the build process,
  7. and then compile that table into our application.
  8. First, let's remove the check for the ``log`` and ``exp`` functions in
  9. ``MathFunctions/CMakeLists.txt``. Then remove the check for ``HAVE_LOG`` and
  10. ``HAVE_EXP`` from ``mysqrt.cxx``. At the same time, we can remove
  11. :code:`#include <cmath>`.
  12. In the ``MathFunctions`` subdirectory, a new source file named
  13. ``MakeTable.cxx`` has been provided to generate the table.
  14. After reviewing the file, we can see that the table is produced as valid C++
  15. code and that the output filename is passed in as an argument.
  16. The next step is to add the appropriate commands to the
  17. ``MathFunctions/CMakeLists.txt`` file to build the MakeTable executable and
  18. then run it as part of the build process. A few commands are needed to
  19. accomplish this.
  20. First, at the top of ``MathFunctions/CMakeLists.txt``, the executable for
  21. ``MakeTable`` is added as any other executable would be added.
  22. .. literalinclude:: Step7/MathFunctions/CMakeLists.txt
  23. :caption: MathFunctions/CMakeLists.txt
  24. :language: cmake
  25. :start-after: # first we add the executable that generates the table
  26. :end-before: # add the command to generate the source code
  27. Then we add a custom command that specifies how to produce ``Table.h``
  28. by running MakeTable.
  29. .. literalinclude:: Step7/MathFunctions/CMakeLists.txt
  30. :caption: MathFunctions/CMakeLists.txt
  31. :language: cmake
  32. :start-after: # add the command to generate the source code
  33. :end-before: # add the main library
  34. Next we have to let CMake know that ``mysqrt.cxx`` depends on the generated
  35. file ``Table.h``. This is done by adding the generated ``Table.h`` to the list
  36. of sources for the library MathFunctions.
  37. .. literalinclude:: Step7/MathFunctions/CMakeLists.txt
  38. :caption: MathFunctions/CMakeLists.txt
  39. :language: cmake
  40. :start-after: # add the main library
  41. :end-before: # state that anybody linking
  42. We also have to add the current binary directory to the list of include
  43. directories so that ``Table.h`` can be found and included by ``mysqrt.cxx``.
  44. .. literalinclude:: Step7/MathFunctions/CMakeLists.txt
  45. :caption: MathFunctions/CMakeLists.txt
  46. :language: cmake
  47. :start-after: # state that we depend on our bin
  48. :end-before: # install rules
  49. Now let's use the generated table. First, modify ``mysqrt.cxx`` to include
  50. ``Table.h``. Next, we can rewrite the ``mysqrt`` function to use the table:
  51. .. literalinclude:: Step7/MathFunctions/mysqrt.cxx
  52. :caption: MathFunctions/mysqrt.cxx
  53. :language: c++
  54. :start-after: // a hack square root calculation using simple operations
  55. Run the :manual:`cmake <cmake(1)>` executable or the
  56. :manual:`cmake-gui <cmake-gui(1)>` to configure the project and then build it
  57. with your chosen build tool.
  58. When this project is built it will first build the ``MakeTable`` executable.
  59. It will then run ``MakeTable`` to produce ``Table.h``. Finally, it will
  60. compile ``mysqrt.cxx`` which includes ``Table.h`` to produce the
  61. ``MathFunctions`` library.
  62. Run the Tutorial executable and verify that it is using the table.