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