xref: /aosp_15_r20/external/skia/infra/bots/task_drivers/common/bazel_flags.go (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1// Copyright 2022 Google LLC
2//
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5
6package common
7
8import (
9	"context"
10	"flag"
11	"fmt"
12
13	"go.skia.org/infra/go/common"
14	"go.skia.org/infra/task_driver/go/td"
15	"go.skia.org/skia/bazel/device_specific_configs"
16)
17
18// BazelFlags is a struct that holds common flags for task drivers that shell out to Bazel.
19type BazelFlags struct {
20	Label                *string
21	Config               *string
22	DeviceSpecificConfig *string
23	AdditionalArgs       *[]string
24	CacheDir             *string
25
26	labelRequired                bool
27	configRequired               bool
28	deviceSpecificConfigRequired bool
29}
30
31// Validate performs common flag validation steps.
32func (f *BazelFlags) Validate(ctx context.Context) {
33	if f.labelRequired && *f.Label == "" {
34		td.Fatal(ctx, fmt.Errorf("--bazel_label is required"))
35	}
36	if f.configRequired && *f.Config == "" {
37		td.Fatal(ctx, fmt.Errorf("--bazel_config is required"))
38	}
39	if f.deviceSpecificConfigRequired {
40		if *f.DeviceSpecificConfig == "" {
41			td.Fatal(ctx, fmt.Errorf("--device_specific_bazel_config is required"))
42		}
43		if _, ok := device_specific_configs.Configs[*f.DeviceSpecificConfig]; !ok {
44			td.Fatal(ctx, fmt.Errorf("unknown flag --device_specific_bazel_config value: %q", *f.DeviceSpecificConfig))
45		}
46	}
47	if *f.CacheDir == "" {
48		td.Fatal(ctx, fmt.Errorf("--bazel_cache_dir is required"))
49	}
50}
51
52// MakeBazelFlagsOpts controls which flags are defiend by MakeBazelFlags.
53type MakeBazelFlagsOpts struct {
54	Label                bool
55	Config               bool
56	DeviceSpecificConfig bool
57	AdditionalArgs       bool
58}
59
60// MakeBazelFlags declares common Bazel flags.
61func MakeBazelFlags(opts MakeBazelFlagsOpts) *BazelFlags {
62	bazelFlags := &BazelFlags{
63		CacheDir: flag.String("bazel_cache_dir", "", "Path to the Bazel cache directory. This should be located on a large partition, as the cache can take tens of GB."),
64	}
65	if opts.Label {
66		bazelFlags.Label = flag.String("bazel_label", "", "An fully qualified Bazel label to the target that should be built/tested/executed.")
67		bazelFlags.labelRequired = true
68	}
69	if opts.Config {
70		bazelFlags.Config = flag.String("bazel_config", "", "A custom Bazel configuration defined in //bazel/buildrc. This configuration potentially encapsulates many features and options.")
71		bazelFlags.configRequired = true
72	}
73	if opts.DeviceSpecificConfig {
74		bazelFlags.DeviceSpecificConfig = flag.String("device_specific_bazel_config", "", "A device-specific Bazel configuration defined in //bazel/devicesrc. This configuration determines various keys in any produced Gold/Perf traces, and has no effect in the built artifact.")
75	}
76	if opts.AdditionalArgs {
77		bazelFlags.AdditionalArgs = common.NewMultiStringFlag("bazel_arg", nil, "Additional arguments that should be forwarded directly to the Bazel invocation.")
78	}
79	return bazelFlags
80}
81