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