android_webview
apps
ash
base
build
3pp_common
android
apple
args
chromeos
cipd
config
docs
fuchsia
gn_ast
internal
ios
linux
mac
private_code_test
rust
sanitizers
skia_gold_common
toolchain
util
win
.clang-tidy
.clangd
.git-blame-ignore-revs
.gitignore
.style.yapf
BUILD.gn
DEPS
DIR_METADATA
OWNERS
OWNERS.setnoparent
OWNERS.status
PRESUBMIT.py
PRESUBMIT_test.py
README.md
action_helpers.py
action_helpers_unittest.py
add_rts_filters.py
build-ctags.sh
build_config.h
buildflag.h
buildflag_header.gni
check_gn_headers.py
check_gn_headers_allowlist.txt
check_gn_headers_unittest.py
check_return_value.py
ciopfs.sha1
clobber.py
clobber_unittest.py
compiled_action.gni
compute_build_timestamp.py
copy_test_data_ios.py
cp.py
detect_host_arch.py
dir_exists.py
dotfile_settings.gni
download_nacl_toolchains.py
env_dump.py
extract_from_cab.py
extract_partition.py
find_depot_tools.py
fix_gn_headers.py
gdb-add-index
get_landmines.py
get_symlink_targets.py
gn_editor
gn_helpers.py
gn_helpers_unittest.py
gn_logs.gni
gn_run_binary.py
install-build-deps.py
install-build-deps.sh
install-chroot.sh
landmine_utils.py
landmines.py
locale_tool.py
mac_toolchain.py
metadata.json.in
nocompile.gni
noop.py
partitioned_shared_library.gni
precompile.cc
precompile.h
print_python_deps.py
protoc_java.py
protoc_java.pydeps
redirect_stdout.py
rm.py
sample_arg_file.gn
sanitize-mac-build-log.sed
sanitize-mac-build-log.sh
sanitize-win-build-log.sed
sanitize-win-build-log.sh
shim_headers.gni
symlink.gni
symlink.py
timestamp.gni
tree_truth.sh
update-linux-sandbox.sh
vs_toolchain.py
whitespace_file.txt
write_buildflag_header.py
xcode_binaries.yaml
zip_helpers.py
zip_helpers_unittest.py
build_overrides
buildtools
cc
chrome
chromecast
chromeos
clank
codelabs
components
content
crypto
dbus
device
docs
extensions
fuchsia_web
gin
google_apis
gpu
headless
infra
internal
ios
ios_internal
ipc
media
mojo
native_client
native_client_sdk
net
pdf
ppapi
printing
remoting
rlz
sandbox
services
signing_keys
skia
sql
storage
styleguide
testing
third_party
tools
ui
url
v8
webkit
.clang-format
.clang-tidy
.clangd
.git-blame-ignore-revs
.gitallowed
.gitattributes
.gitignore
.gitmodules
.gn
.mailmap
.rustfmt.toml
.vpython3
.yapfignore
ATL_OWNERS
AUTHORS
BUILD.gn
CODE_OF_CONDUCT.md
CPPLINT.cfg
CRYPTO_OWNERS
DEPS
DIR_METADATA
LICENSE
LICENSE.chromium_os
OWNERS
PRESUBMIT.py
PRESUBMIT_test.py
PRESUBMIT_test_mocks.py
README.md
WATCHLISTS
codereview.settings

Clangd does not detect BUILDFLAG_INTERNAL_* indirect usage and marks all generated buildflag headers as unused. Although Chromium has UnusedIncludes disabled, developers and downstream projects may enable it explicitly in their setup. Change-Id: I6231a670ba996f189c8535026300a76d338f8f7a Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5560319 Commit-Queue: Dirk Pranke <dpranke@google.com> Auto-Submit: Aleksey Khoroshilov <akhoroshilov@brave.com> Reviewed-by: Dirk Pranke <dpranke@google.com> Reviewed-by: Sylvain Defresne <sdefresne@chromium.org> Cr-Commit-Position: refs/heads/main@{#1305300}
101 lines
3.4 KiB
Python
Executable File
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)
|