Add tool for generating field trial registry header

The tool will generate a C++ header with all field trials in
REGISTERED_FIELD_TRIALS. This registry will later be used while looking
up field trials from native code to ensure they have been properly
registered in accordance with the policy.

Bug: webrtc:14154
Change-Id: I29bf880735121034585c541c46ef19f617d0afb9
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/276268
Reviewed-by: Harald Alvestrand <hta@webrtc.org>
Reviewed-by: Mirko Bonadei <mbonadei@webrtc.org>
Commit-Queue: Emil Lundmark <lndmrk@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#38426}
This commit is contained in:
Emil Lundmark 2022-09-21 15:20:22 +02:00 committed by WebRTC LUCI CQ
parent 14f23b7ceb
commit 64a33f2453
3 changed files with 141 additions and 0 deletions

View File

@ -11,6 +11,7 @@ include_rules = [
"-common_video",
"-data",
"-examples",
"-experiments",
"-g3doc",
"-ios",
"-infra",

26
experiments/BUILD.gn Normal file
View File

@ -0,0 +1,26 @@
# Copyright (c) 2022 The WebRTC project authors. All Rights Reserved.
#
# Use of this source code is governed by a BSD-style license
# that can be found in the LICENSE file in the root of the source
# tree. An additional intellectual property rights grant can be found
# in the file PATENTS. All contributing project authors may
# be found in the AUTHORS file in the root of the source tree.
import("../webrtc.gni")
action("registered_field_trials_header") {
visibility = [ ":*" ]
script = "field_trials.py"
args = [
"header",
"--output",
rebase_path(target_gen_dir, root_build_dir) + "/registered_field_trials.h",
]
outputs = [ "$target_gen_dir/registered_field_trials.h" ]
}
rtc_library("registered_field_trials") {
visibility = [ "*" ]
sources = get_target_outputs(":registered_field_trials_header")
deps = [ ":registered_field_trials_header" ]
}

114
experiments/field_trials.py Executable file
View File

@ -0,0 +1,114 @@
#!/usr/bin/env vpython3
# Copyright (c) 2022 The WebRTC Project Authors. All Rights Reserved.
#
# Use of this source code is governed by a BSD-style license
# that can be found in the LICENSE file in the root of the source
# tree. An additional intellectual property rights grant can be found
# in the file PATENTS. All contributing project authors may
# be found in the AUTHORS file in the root of the source tree.
import sys
from typing import Set
import argparse
import dataclasses
# TODO(bugs.webrtc.org/14154): End date and bug should also be stored.
@dataclasses.dataclass(frozen=True)
class FieldTrial:
"""Representation of all attributes associated with a field trial.
Attributes:
key: Field trial key.
"""
key: str
# As per the policy in `g3doc/field-trials.md`, all field trials should be
# registered in the container below. Please keep the keys sorted.
REGISTERED_FIELD_TRIALS: Set[FieldTrial] = {
FieldTrial(''), # TODO(bugs.webrtc.org/14154): Populate
}
def RegistryHeader(field_trials: Set[FieldTrial] = None) -> str:
"""Generates a C++ header with all field trial keys.
Args:
field_trials: Field trials to include in the header.
Returns:
String representation of a C++ header file containing all field trial keys.
>>> trials = {FieldTrial('B'), FieldTrial('A'), FieldTrial('B')}
>>> print(RegistryHeader(trials))
// This file was automatically generated. Do not edit.
<BLANKLINE>
#ifndef GEN_REGISTERED_FIELD_TRIALS_H_
#define GEN_REGISTERED_FIELD_TRIALS_H_
<BLANKLINE>
#include "absl/strings/string_view.h"
<BLANKLINE>
namespace webrtc {
<BLANKLINE>
inline constexpr absl::string_view kRegisteredFieldTrials[] = {
"A",
"B",
};
<BLANKLINE>
} // namespace webrtc
<BLANKLINE>
#endif // GEN_REGISTERED_FIELD_TRIALS_H_
<BLANKLINE>
"""
if not field_trials:
field_trials = REGISTERED_FIELD_TRIALS
registered_keys = [f.key for f in field_trials]
keys = '\n'.join(f' "{k}",' for k in sorted(registered_keys))
return ('// This file was automatically generated. Do not edit.\n'
'\n'
'#ifndef GEN_REGISTERED_FIELD_TRIALS_H_\n'
'#define GEN_REGISTERED_FIELD_TRIALS_H_\n'
'\n'
'#include "absl/strings/string_view.h"\n'
'\n'
'namespace webrtc {\n'
'\n'
'inline constexpr absl::string_view kRegisteredFieldTrials[] = {\n'
f'{keys}\n'
'};\n'
'\n'
'} // namespace webrtc\n'
'\n'
'#endif // GEN_REGISTERED_FIELD_TRIALS_H_\n')
def CmdHeader(args: argparse.Namespace) -> None:
args.output.write(RegistryHeader())
def main() -> None:
parser = argparse.ArgumentParser()
subcommand = parser.add_subparsers(dest='cmd')
parser_header = subcommand.add_parser(
'header',
help='generate C++ header file containing registered field trial keys')
parser_header.add_argument('--output',
default=sys.stdout,
type=argparse.FileType('w'),
required=False,
help='output file')
parser_header.set_defaults(cmd=CmdHeader)
args = parser.parse_args()
if not args.cmd:
parser.print_help(sys.stderr)
sys.exit(1)
args.cmd(args)
if __name__ == '__main__':
main()