Brian Silverman | 9d8fa39 | 2018-08-04 17:09:24 -0700 | [diff] [blame^] | 1 | <html> |
| 2 | <head> |
| 3 | <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"> |
| 4 | <title>Build Time Configuration</title> |
| 5 | <link rel="stylesheet" href="../../../../../doc/src/boostbook.css" type="text/css"> |
| 6 | <meta name="generator" content="DocBook XSL Stylesheets V1.77.1"> |
| 7 | <link rel="home" href="../index.html" title="Boost.Config"> |
| 8 | <link rel="up" href="../index.html" title="Boost.Config"> |
| 9 | <link rel="prev" href="boost_macro_reference.html" title="Boost Macro Reference"> |
| 10 | <link rel="next" href="cstdint.html" title="Standard Integer Types"> |
| 11 | </head> |
| 12 | <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> |
| 13 | <table cellpadding="2" width="100%"><tr> |
| 14 | <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../boost.png"></td> |
| 15 | <td align="center"><a href="../../../../../index.html">Home</a></td> |
| 16 | <td align="center"><a href="../../../../../libs/libraries.htm">Libraries</a></td> |
| 17 | <td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> |
| 18 | <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> |
| 19 | <td align="center"><a href="../../../../../more/index.htm">More</a></td> |
| 20 | </tr></table> |
| 21 | <hr> |
| 22 | <div class="spirit-nav"> |
| 23 | <a accesskey="p" href="boost_macro_reference.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="cstdint.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a> |
| 24 | </div> |
| 25 | <div class="section"> |
| 26 | <div class="titlepage"><div><div><h2 class="title" style="clear: both"> |
| 27 | <a name="boost_config.build_config"></a><a class="link" href="build_config.html" title="Build Time Configuration">Build Time Configuration</a> |
| 28 | </h2></div></div></div> |
| 29 | <p> |
| 30 | There are times when you want to control whether a build target gets built |
| 31 | or not, based on what features the compiler supports. For example, suppose |
| 32 | you have a test file "test_constexpr_128.cpp" which requires three |
| 33 | key features in order to build: |
| 34 | </p> |
| 35 | <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "> |
| 36 | <li class="listitem"> |
| 37 | The <code class="computeroutput"><span class="keyword">constexpr</span></code> keyword as detected |
| 38 | by BOOST_NO_CXX11_CONSTEXPR. |
| 39 | </li> |
| 40 | <li class="listitem"> |
| 41 | User defined literals, as detected by BOOST_NO_CXX11_USER_DEFINED_LITERALS. |
| 42 | </li> |
| 43 | <li class="listitem"> |
| 44 | The <code class="computeroutput"><span class="identifier">__int128</span></code> data type, |
| 45 | as detected by BOOST_HAS_INT128. |
| 46 | </li> |
| 47 | </ul></div> |
| 48 | <p> |
| 49 | Clearly we know that if these features are not supported by the compiler, then |
| 50 | there's simply no point in even trying to build the test program. The main |
| 51 | advantages being: |
| 52 | </p> |
| 53 | <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "> |
| 54 | <li class="listitem"> |
| 55 | Faster compile times - build configuration uses lightweight tests the results |
| 56 | of which are also cached. |
| 57 | </li> |
| 58 | <li class="listitem"> |
| 59 | Less noise in build output - there's no reason to be faced with pages of |
| 60 | template instantiation backtrace if we know the file can never compile |
| 61 | anyway. |
| 62 | </li> |
| 63 | <li class="listitem"> |
| 64 | Less noise in the online test results - the test will show up as blank, |
| 65 | rather than as a fail in the online test matrix. |
| 66 | </li> |
| 67 | <li class="listitem"> |
| 68 | A better experience for end users building all of Boost, if those libraries |
| 69 | which can not be built for the current target compiler are simply skipped, |
| 70 | rather than generating pages of error output. |
| 71 | </li> |
| 72 | </ul></div> |
| 73 | <p> |
| 74 | Returning to our example, the test case is probably executed in it's Jamfile |
| 75 | via the "run" rule: |
| 76 | </p> |
| 77 | <pre class="programlisting"><span class="identifier">run</span> <span class="identifier">test_constexpr_128</span><span class="special">.</span><span class="identifier">cpp</span> <span class="special">;</span> |
| 78 | </pre> |
| 79 | <p> |
| 80 | We now need to make this target conditional on the necessary features. We can |
| 81 | do that by first importing the necessary rule at the start of the Jamfile: |
| 82 | </p> |
| 83 | <pre class="programlisting"><span class="identifier">import</span> <span class="identifier">path</span><span class="special">-</span><span class="identifier">to</span><span class="special">-</span><span class="identifier">config</span><span class="special">-</span><span class="identifier">lib</span><span class="special">/</span><span class="identifier">checks</span><span class="special">/</span><span class="identifier">config</span> <span class="special">:</span> <span class="identifier">requires</span> <span class="special">;</span> |
| 84 | </pre> |
| 85 | <p> |
| 86 | Assuming that the test case is in the usual directory: |
| 87 | </p> |
| 88 | <pre class="programlisting"><span class="identifier">libs</span><span class="special">/</span><span class="identifier">yourlib</span><span class="special">/</span><span class="identifier">test</span> |
| 89 | </pre> |
| 90 | <p> |
| 91 | then the import rule will actually be: |
| 92 | </p> |
| 93 | <pre class="programlisting"><span class="identifier">import</span> <span class="special">../../</span><span class="identifier">config</span><span class="special">/</span><span class="identifier">checks</span><span class="special">/</span><span class="identifier">config</span> <span class="special">:</span> <span class="identifier">requires</span> <span class="special">;</span> |
| 94 | </pre> |
| 95 | <p> |
| 96 | Then add a "requires" rule invocation to the requirements section |
| 97 | of the target: |
| 98 | </p> |
| 99 | <pre class="programlisting"><span class="identifier">run</span> <span class="identifier">test_constexpr_128</span><span class="special">.</span><span class="identifier">cpp</span> |
| 100 | <span class="special">:</span> <span class="special">:</span> <span class="special">:</span> <span class="special">#</span><span class="identifier">requirements</span><span class="special">:</span> |
| 101 | <span class="special">[</span> <span class="identifier">requires</span> <span class="identifier">cxx11_constexpr</span> <span class="identifier">cxx11_user_defined_literals</span> <span class="identifier">int128</span> <span class="special">]</span> <span class="special">;</span> |
| 102 | </pre> |
| 103 | <p> |
| 104 | Notice that multiple arguments can be added to the requires rule, and that |
| 105 | these are always the same as the Boost.Config macro name, but in lower case |
| 106 | and with the <span class="emphasis"><em>boost_no_</em></span> or <span class="emphasis"><em>boost_has_</em></span> |
| 107 | prefix removed. |
| 108 | </p> |
| 109 | <p> |
| 110 | When building the above example, you will see at the start of the build process |
| 111 | the results of the configuration, for example GCC in C++11 mode gives: |
| 112 | </p> |
| 113 | <pre class="programlisting"><span class="special">-</span> <span class="identifier">Boost</span><span class="special">.</span><span class="identifier">Config</span> <span class="identifier">Feature</span> <span class="identifier">Check</span><span class="special">:</span> <span class="identifier">int128</span> <span class="special">:</span> <span class="identifier">yes</span> |
| 114 | <span class="special">-</span> <span class="identifier">Boost</span><span class="special">.</span><span class="identifier">Config</span> <span class="identifier">Feature</span> <span class="identifier">Check</span><span class="special">:</span> <span class="identifier">cxx11_constexpr</span> <span class="special">:</span> <span class="identifier">yes</span> |
| 115 | <span class="special">-</span> <span class="identifier">Boost</span><span class="special">.</span><span class="identifier">Config</span> <span class="identifier">Feature</span> <span class="identifier">Check</span><span class="special">:</span> <span class="identifier">cxx11_user_defined_literals</span> <span class="special">:</span> <span class="identifier">yes</span> |
| 116 | </pre> |
| 117 | <p> |
| 118 | That's all there is to this handy feature, should at any time you be unsure |
| 119 | of the feature-test names you can pass to the "requires" rule, then |
| 120 | search for the Boost.Config macro of interest in libs/config/checks/Jamfiles.v2, |
| 121 | and the name of the feature check will follow it. |
| 122 | </p> |
| 123 | <p> |
| 124 | And finally, this feature is built around the Boost.Build built in rule <span class="emphasis"><em>check-target-builds</em></span> |
| 125 | which can be used to perform more generalized build-time feature testing. The |
| 126 | checks in this library are provided as a convenient shorthand without the need |
| 127 | for you to write the test cases yourself. |
| 128 | </p> |
| 129 | </div> |
| 130 | <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> |
| 131 | <td align="left"></td> |
| 132 | <td align="right"><div class="copyright-footer">Copyright © 2001-2007 Beman Dawes, Vesa Karvonen, John |
| 133 | Maddock<p> |
| 134 | Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 135 | file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) |
| 136 | </p> |
| 137 | </div></td> |
| 138 | </tr></table> |
| 139 | <hr> |
| 140 | <div class="spirit-nav"> |
| 141 | <a accesskey="p" href="boost_macro_reference.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="cstdint.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a> |
| 142 | </div> |
| 143 | </body> |
| 144 | </html> |