0
Files
src/build/write_buildflag_header.py
Lei Zhang 5a2cf8b7d8 Revert "build: Be stricter about buildflag values"
This reverts commit 8f7e0bcec0.

Reason for revert: Broke the tree. https://ci.chromium.org/ui/p/chromium/builders/ci/linux-cast-x64-rel/11722/overview

Original change's description:
> build: Be stricter about buildflag values
>
> We don't want buildflag headers to contain arbitrary expressions
>
> A recent change had used
>
>     "ENABLE_FOO=$enable_foo_1 || $enable_foo_2",
>
> which `gn` turned into `true || false`, which write_buildflag_header.py
> wrote to a generated header verbatim. Checking
>
>     #if BUILDFLAG(ENABLE_FOO)
>
> would then expand to
>
>     #if true || false
>
> but `true` and `false` are only valid in C++, not C.
>
> (...and buildflag headers get included in resource scripts, and
> rc.exe preprocesses using C semantics, not C++ semantics, and hence
> silently does the wrong thing.)
>
> So only allow `true` and `false` (which buildflag_headers.py maps
> to 1 and 0 in its output), and string literals, for things like
> ALTERNATE_CDM_STORAGE_ID_KEY. We could also consider (re)allowing
> int literals, but those silently evaluate to truthy or falsy values
> in preprocessor expressions -- several places were checking
> `#if BUILDFLAG(CHROME_PGO)` when they morally mean
> `#if BUILDFLAG(CHROME_PGO) == 1`. Since we currently have no use
> cases for arbitrary int values in buildflags, disallow them.
> String literals don't have this problem.
>
> The main motivation is to block things like `true && false` and
> `false || true`.
>
> (Convert all `=1` / `=0` to `=true` / `=false` to make this work.
> We must support `true` and `false` for the very common
> `FOO=$foo` use case.)
>
> The CHROME_PGO issue above is fine since we also checked
> BUILDFLAG(CLANG_PROFILING_INSIDE_SANDBOX) at the same time, and
> that's only set in the profiling phase, but it's still confusing.
> So use two boolean build flags for CHROME_PGO instead. (No behavior
> change.)
>
> Bug: 403123830
> Change-Id: I7a6cedf063824b280f1fe03a07586cf77f649b29
> Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6371822
> Commit-Queue: Nico Weber <thakis@chromium.org>
> Owners-Override: Nico Weber <thakis@chromium.org>
> Reviewed-by: Hans Wennborg <hans@chromium.org>
> Cr-Commit-Position: refs/heads/main@{#1435044}

Bug: 403123830
No-Presubmit: true
No-Tree-Checks: true
No-Try: true
Change-Id: I5ab67b3e6ae54e9642f2402449823146c4e45aab
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6373873
Commit-Queue: Lei Zhang <thestig@chromium.org>
Bot-Commit: Rubber Stamper <rubber-stamper@appspot.gserviceaccount.com>
Reviewed-by: Lei Zhang <thestig@chromium.org>
Owners-Override: Lei Zhang <thestig@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1435052}
2025-03-19 13:43:19 -07:00

101 lines
3.4 KiB
Python
Executable File

#!/usr/bin/env python3
# Copyright 2015 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
# This writes headers for build flags. See buildflag_header.gni for usage of
# this system as a whole.
#
# The parameters are passed in a response file so we don't have to worry
# about command line lengths. The name of the response file is passed on the
# command line.
#
# The format of the response file is:
# [--flags <list of one or more flag values>]
import optparse
import os
import re
import shlex
class Options:
def __init__(self, output, rulename, header_guard, flags):
self.output = output
self.rulename = rulename
self.header_guard = header_guard
self.flags = flags
def GetOptions():
parser = optparse.OptionParser()
parser.add_option('--output', help="Output header name inside --gen-dir.")
parser.add_option('--rulename',
help="Helpful name of build rule for including in the " +
"comment at the top of the file.")
parser.add_option('--gen-dir',
help="Path to root of generated file directory tree.")
parser.add_option('--definitions',
help="Name of the response file containing the flags.")
cmdline_options, cmdline_flags = parser.parse_args()
# Compute a valid C++ header guard by replacing non valid chars with '_',
# upper-casing everything and prepending '_' if first symbol is digit.
header_guard = cmdline_options.output.upper()
if header_guard[0].isdigit():
header_guard = '_' + header_guard
header_guard = re.sub(r'[^\w]', '_', header_guard)
header_guard += '_'
# The actual output file is inside the gen dir.
output = os.path.join(cmdline_options.gen_dir, cmdline_options.output)
# Definition file in GYP is newline separated, in GN they are shell formatted.
# shlex can parse both of these.
with open(cmdline_options.definitions, 'r') as def_file:
defs = shlex.split(def_file.read())
flags_index = defs.index('--flags')
# Everything after --flags are flags. true/false are remapped to 1/0,
# everything else is passed through.
flags = []
for flag in defs[flags_index + 1 :]:
equals_index = flag.index('=')
key = flag[:equals_index]
value = flag[equals_index + 1:]
# Canonicalize and validate the value.
if value == 'true':
value = '1'
elif value == 'false':
value = '0'
flags.append((key, str(value)))
return Options(output=output,
rulename=cmdline_options.rulename,
header_guard=header_guard,
flags=flags)
def WriteHeader(options):
with open(options.output, 'w') as output_file:
output_file.write("// Generated by build/write_buildflag_header.py\n")
if options.rulename:
output_file.write('// From "' + options.rulename + '"\n')
output_file.write('\n#ifndef %s\n' % options.header_guard)
output_file.write('#define %s\n\n' % options.header_guard)
output_file.write('#include "build/buildflag.h" // IWYU pragma: export\n\n')
# Clangd does not detect BUILDFLAG_INTERNAL_* indirect usage, so mark the
# header as "always_keep" to avoid "unused include" warning.
output_file.write('// IWYU pragma: always_keep\n\n')
for pair in options.flags:
output_file.write('#define BUILDFLAG_INTERNAL_%s() (%s)\n' % pair)
output_file.write('\n#endif // %s\n' % options.header_guard)
options = GetOptions()
WriteHeader(options)