build_time.qbk 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. [/
  2. Boost.Config
  3. Copyright (c) 2001 Beman Dawes
  4. Copyright (c) 2001 Vesa Karvonen
  5. Copyright (c) 2001 John Maddock
  6. Distributed under the Boost Software License, Version 1.0.
  7. (See accompanying file LICENSE_1_0.txt or copy at
  8. http://www.boost.org/LICENSE_1_0.txt)
  9. ]
  10. [section:build_config Build Time Configuration]
  11. There are times when you want to control whether a build target gets built or not, based
  12. on what features the compiler supports. For example, suppose you have a test file
  13. "test_constexpr_128.cpp" which requires three key features in order to build:
  14. * The `constexpr` keyword as detected by BOOST_NO_CXX11_CONSTEXPR.
  15. * User defined literals, as detected by BOOST_NO_CXX11_USER_DEFINED_LITERALS.
  16. * The `__int128` data type, as detected by BOOST_HAS_INT128.
  17. Clearly we know that if these features are not supported by the compiler, then
  18. there's simply no point in even trying to build the test program. The main advantages being:
  19. * Faster compile times - build configuration uses lightweight tests the results of which are also cached.
  20. * Less noise in build output - there's no reason to be faced with pages of template
  21. instantiation backtrace if we know the file can never compile anyway.
  22. * Less noise in the online test results - the test will show up as blank, rather than as a fail
  23. in the online test matrix.
  24. * A better experience for end users building all of Boost, if those libraries which can not be built
  25. for the current target compiler are simply skipped, rather than generating pages of error output.
  26. Returning to our example, the test case is probably executed in it's Jamfile via the "run" rule:
  27. run test_constexpr_128.cpp ;
  28. We now need to make this target conditional on the necessary features.
  29. We can do that by first importing the necessary rule at the start of the Jamfile:
  30. import path-to-config-lib/checks/config : requires ;
  31. Assuming that the test case is in the usual directory:
  32. libs/yourlib/test
  33. then the import rule will actually be:
  34. import ../../config/checks/config : requires ;
  35. Then add a "requires" rule invocation to the requirements section of the target:
  36. run test_constexpr_128.cpp
  37. : : : #requirements:
  38. [ requires cxx11_constexpr cxx11_user_defined_literals int128 ] ;
  39. Notice that multiple arguments can be added to the requires rule, and that these are
  40. always the same as the Boost.Config macro name, but in lower case and with the ['boost_no_]
  41. or ['boost_has_] prefix removed. You can also use any C++ standard feature-macro name
  42. with the leading underscores removed (see more below).
  43. When building the above example, you will see at the start of the build process the results
  44. of the configuration, for example GCC in C++11 mode gives:
  45. - Boost.Config Feature Check: int128 : yes
  46. - Boost.Config Feature Check: cxx11_constexpr : yes
  47. - Boost.Config Feature Check: cxx11_user_defined_literals : yes
  48. If you wish to make a build conditional on a C++ standard feature macro then you can specify
  49. these too, just remove the leading underscores from the name. For example:
  50. [ requires cpp_constexpr ]
  51. To require C++11 style const-expressions. If you want to specify a macro from a particular
  52. standard, then you append an underscore followed by the (2 digit) year of the standard, for example:
  53. [ requires cpp_constexpr_17 ]
  54. For C++17 constepxr. If you don't specify a standard then you get the first version that
  55. introduced the macro. In addition there are only standard-specific rules for each version
  56. bump of the macro, so:
  57. [ requires cpp_if_constexpr_17 ]
  58. Is fine since the macro was introduced in C++17 and is the same as the un-versioned name, but:
  59. [ requires cpp_if_constexpr_20 ]
  60. Will result in a build error since there is no C++20 version bump for `__cpp_if_constexpr`.
  61. That's all there is to this handy feature, should at any time you be unsure of the feature-test
  62. names you can pass to the "requires" rule, then search for the Boost.Config macro of interest in
  63. libs/config/checks/Jamfiles.v2, and the name of the feature check will follow it.
  64. And finally, this feature is built around the Boost.Build built in rule ['check-target-builds]
  65. which can be used to perform more generalized build-time feature testing. The checks in this
  66. library are provided as a convenient shorthand without the need for you to write the test cases yourself.
  67. [endsect]