Debian Codegraph @

infra/bots/task_drivers/toolchain_layering_check/toolchain_layering_check.go

raw
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 //
6 // This executable tests the "layering_check" feature of our custom Bazel toolchain. It builds
7 // a target multiple times with different defines, which turn on a different "invalid" include
8 // and verifies that they fail.
9 package main
10
11 import (
12 "context"
13 "flag"
14 "fmt"
15 "path/filepath"
16
17 sk_exec "go.skia.org/infra/go/exec"
18 "go.skia.org/infra/task_driver/go/lib/bazel"
19 "go.skia.org/infra/task_driver/go/lib/os_steps"
20 "go.skia.org/infra/task_driver/go/td"
21 "go.skia.org/skia/infra/bots/task_drivers/common"
22 )
23
24 var (
25 // Required properties for this task.
26 projectId = flag.String("project_id", "", "ID of the Google Cloud project.")
27 taskId = flag.String("task_id", "", "ID of this task.")
28 taskName = flag.String("task_name", "", "Name of the task.")
29 workdir = flag.String("workdir", ".", "Working directory, the root directory of a full Skia checkout")
30 // Optional flags.
31 local = flag.Bool("local", false, "True if running locally (as opposed to on the CI/CQ)")
32 output = flag.String("o", "", "If provided, dump a JSON blob of step data to the given file. Prints to stdout if '-' is given.")
33 )
34
35 func main() {
36 bazelFlags := common.MakeBazelFlags(common.MakeBazelFlagsOpts{
37 Label: true,
38 Config: true,
39 })
40
41 // StartRun calls flag.Parse()
42 ctx := td.StartRun(projectId, taskId, taskName, output, local)
43 defer td.EndRun(ctx)
44
45 bazelFlags.Validate(ctx)
46
47 wd, err := os_steps.Abs(ctx, *workdir)
48 if err != nil {
49 td.Fatal(ctx, err)
50 }
51 skiaDir := filepath.Join(wd, "skia")
52
53 opts := bazel.BazelOptions{
54 // We want the cache to be on a bigger disk than default. The root disk, where the home
55 // directory (and default Bazel cache) lives, is only 15 GB on our GCE VMs.
56 CachePath: *bazelFlags.CacheDir,
57 }
58 if err := bazel.EnsureBazelRCFile(ctx, opts); err != nil {
59 td.Fatal(ctx, err)
60 }
61
62 // This build should succeed
63 if err := bazelBuild(ctx, skiaDir, *bazelFlags.Label, *bazelFlags.Config); err != nil {
64 td.Fatal(ctx, err)
65 }
66
67 // All of these should fail
68 definesToIncludeExtraHeaders := []string{
69 "HEADER_INCLUDES_TRANSITIVE_HEADER",
70 "HEADER_INCLUDES_PRIVATE_HEADER",
71 "SOURCE_INCLUDES_TRANSITIVE_HEADER",
72 "SOURCE_INCLUDES_PRIVATE_HEADER",
73 }
74 for _, define := range definesToIncludeExtraHeaders {
75 if err := expectFailure(ctx, skiaDir, *bazelFlags.Label, *bazelFlags.Config, define); err != nil {
76 td.Fatal(ctx, err)
77 }
78 }
79
80 if !*local {
81 if err := common.BazelCleanIfLowDiskSpace(ctx, *bazelFlags.CacheDir, skiaDir, "bazelisk"); err != nil {
82 td.Fatal(ctx, err)
83 }
84 }
85 }
86
87 // bazelBuild builds the target referenced by the given absolute label passing the provided
88 // config to Bazel command. Instead of calling Bazel directly, we use
89 func bazelBuild(ctx context.Context, checkoutDir, label, config string) error {
90 step := fmt.Sprintf("Build %s", label)
91 return td.Do(ctx, td.Props(step), func(ctx context.Context) error {
92 runCmd := &sk_exec.Command{
93 Name: "bazelisk",
94 Args: append([]string{"build",
95 label,
96 "--config=" + config, // Should be defined in //bazel/buildrc
97 }),
98 InheritEnv: true, // Makes sure bazelisk is on PATH
99 Dir: checkoutDir,
100 LogStdout: true,
101 LogStderr: true,
102 }
103 _, err := sk_exec.RunCommand(ctx, runCmd)
104 if err != nil {
105 return err
106 }
107 return nil
108 })
109 }
110
111 // expectFailure builds the target with the given config and additional define. Note that this
112 // define affects *all* build commands, whereas setting a copt on the BUILD.bazel impacts that
113 // cc_library/cc_binary but not dependencies nor dependents (except headers from dependencies).
114 // It returns an error if the underlying build does *not* fail. We expect setting the define to
115 // include a file that is not allowed and our Bazel build should enforce that.
116 func expectFailure(ctx context.Context, checkoutDir, label, config, define string) error {
117 step := fmt.Sprintf("Expect building %s with %s to fail", label, define)
118 return td.Do(ctx, td.Props(step), func(ctx context.Context) error {
119 runCmd := &sk_exec.Command{
120 Name: "bazelisk",
121 Args: append([]string{"build",
122 label,
123 "--config=" + config, // Should be defined in //bazel/buildrc
124 "--copt=-D" + define + "=1",
125 }),
126 InheritEnv: true, // Makes sure bazelisk is on PATH
127 Dir: checkoutDir,
128 LogStdout: true,
129 LogStderr: true,
130 }
131 _, err := sk_exec.RunCommand(ctx, runCmd)
132 if err == nil {
133 return fmt.Errorf("Expected a failure but didn't get one.")
134 }
135 return nil
136 })
137 }