buildflag.h 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. // Copyright 2015 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef BUILD_BUILDFLAG_H_
  5. #define BUILD_BUILDFLAG_H_
  6. // These macros un-mangle the names of the build flags in a way that looks
  7. // natural, and gives errors if the flag is not defined. Normally in the
  8. // preprocessor it's easy to make mistakes that interpret "you haven't done
  9. // the setup to know what the flag is" as "flag is off". Normally you would
  10. // include the generated header rather than include this file directly.
  11. //
  12. // This is for use with generated headers. See build/buildflag_header.gni.
  13. // This dance of two macros does a concatenation of two preprocessor args using
  14. // ## doubly indirectly because using ## directly prevents macros in that
  15. // parameter from being expanded.
  16. #define BUILDFLAG_CAT_INDIRECT(a, b) a ## b
  17. #define BUILDFLAG_CAT(a, b) BUILDFLAG_CAT_INDIRECT(a, b)
  18. // Accessor for build flags.
  19. //
  20. // To test for a value, if the build file specifies:
  21. //
  22. // ENABLE_FOO=true
  23. //
  24. // Then you would check at build-time in source code with:
  25. //
  26. // #include "foo_flags.h" // The header the build file specified.
  27. //
  28. // #if BUILDFLAG(ENABLE_FOO)
  29. // ...
  30. // #endif
  31. //
  32. // There will no #define called ENABLE_FOO so if you accidentally test for
  33. // whether that is defined, it will always be negative. You can also use
  34. // the value in expressions:
  35. //
  36. // const char kSpamServerName[] = BUILDFLAG(SPAM_SERVER_NAME);
  37. //
  38. // Because the flag is accessed as a preprocessor macro with (), an error
  39. // will be thrown if the proper header defining the internal flag value has
  40. // not been included.
  41. #define BUILDFLAG(flag) (BUILDFLAG_CAT(BUILDFLAG_INTERNAL_, flag)())
  42. #endif // BUILD_BUILDFLAG_H_