Debian Codegraph @

bazel/deps_parser/deps_parser.go

raw
1 // Copyright 2022 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 package main
6
7 import (
8 "encoding/json"
9 "flag"
10 "fmt"
11 "os"
12 "regexp"
13 "strings"
14 )
15
16 type depConfig struct {
17 bazelNameOverride string // Bazel style uses underscores not dashes, so we fix those if needed.
18 needsBazelFile bool
19 patches []string
20 patchCmds []string
21 patchCmdsWin []string
22 isIndirect bool // if True, it's used by another dependency, not by Skia directly
23 }
24
25 // These are all C++ deps or Rust deps (with a compatible C++ FFI) used by the Bazel build.
26 // They are a subset of those listed in DEPS.
27 // The key is the name of the repo as specified in DEPS.
28 var depsOverrides = map[string]depConfig{
29 "abseil-cpp": {bazelNameOverride: "abseil_cpp", isIndirect: true},
30 "brotli": {isIndirect: true},
31 "highway": {isIndirect: true},
32 "spirv-tools": {bazelNameOverride: "spirv_tools"},
33 // This name is important because spirv_tools expects @spirv_headers to exist by that name.
34 "spirv-headers": {bazelNameOverride: "spirv_headers"},
35
36 "dawn": {needsBazelFile: true},
37 "delaunator-cpp": {bazelNameOverride: "delaunator", needsBazelFile: true},
38 "dng_sdk": {needsBazelFile: true},
39 "expat": {
40 needsBazelFile: true,
41 patches: []string{"//bazel/external/expat:config_files.patch"},
42 },
43 "freetype": {
44 needsBazelFile: true,
45 patches: []string{"//bazel/external/freetype:config_files.patch"},
46 },
47 "harfbuzz": {
48 needsBazelFile: true,
49 patches: []string{"//bazel/external/harfbuzz:config_files.patch"},
50 },
51 "icu": {
52 needsBazelFile: true,
53 patches: []string{"//bazel/external/icu:icu_utils.patch"},
54 patchCmds: []string{
55 "rm source/i18n/BUILD.bazel",
56 "rm source/common/BUILD.bazel",
57 "rm source/stubdata/BUILD.bazel",
58 },
59 patchCmdsWin: []string{
60 "del source/i18n/BUILD.bazel",
61 "del source/common/BUILD.bazel",
62 "del source/stubdata/BUILD.bazel",
63 },
64 },
65 "icu4x": {needsBazelFile: true},
66 "imgui": {needsBazelFile: true},
67 "libavif": {needsBazelFile: true},
68 "libgav1": {needsBazelFile: true},
69 "libjpeg-turbo": {bazelNameOverride: "libjpeg_turbo", needsBazelFile: true},
70 "libjxl": {needsBazelFile: true},
71 "libpng": {needsBazelFile: true},
72 "libwebp": {needsBazelFile: true},
73 "libyuv": {needsBazelFile: true},
74 "spirv-cross": {bazelNameOverride: "spirv_cross", needsBazelFile: true},
75 "perfetto": {needsBazelFile: true},
76 "piex": {needsBazelFile: true},
77 "vello": {needsBazelFile: true},
78 "vulkan-headers": {bazelNameOverride: "vulkan_headers", needsBazelFile: true},
79 "vulkan-tools": {bazelNameOverride: "vulkan_tools", needsBazelFile: true},
80 "vulkan-utility-libraries": {bazelNameOverride: "vulkan_utility_libraries", needsBazelFile: true},
81 "vulkanmemoryallocator": {needsBazelFile: true},
82 "wuffs": {needsBazelFile: true},
83 "zlib": {needsBazelFile: true},
84 }
85
86 func main() {
87 var (
88 depsFile = flag.String("deps_file", "DEPS", "The location of the DEPS file. Usually at the root of the repository")
89 genJSONFile = flag.String("gen_json_file", "bazel/deps.json", "The location of the .json file that has the generated data used to make Bazel git_repository rules.")
90 // https://bazel.build/docs/user-manual#running-executables
91 repoDir = flag.String("repo_dir", os.Getenv("BUILD_WORKSPACE_DIRECTORY"), "The root directory of the repo. Default set by BUILD_WORKSPACE_DIRECTORY env variable.")
92 )
93 flag.Parse()
94
95 if *repoDir == "" {
96 fmt.Println(`Must set --repo_dir
97 This is done automatically via:
98 bazel run //bazel/deps_parser`)
99 os.Exit(1)
100 }
101
102 fmt.Println(os.Environ())
103
104 if *depsFile == "" || *genJSONFile == "" {
105 fmt.Println("Must set --deps_file and --gen_json_file")
106 flag.PrintDefaults()
107 }
108
109 if err := os.Chdir(*repoDir); err != nil {
110 fmt.Printf("Could not cd to %s\n", *repoDir)
111 os.Exit(1)
112 }
113
114 b, err := os.ReadFile(*depsFile)
115 if err != nil {
116 fmt.Printf("Could not open %s: %s\n", *depsFile, err)
117 os.Exit(1)
118 }
119 contents := strings.Split(string(b), "\n")
120
121 outputFile, count, err := parseDEPSFile(contents)
122 if err != nil {
123 fmt.Printf("Parsing error %s\n", err)
124 os.Exit(1)
125 }
126 if err := moveWithCopyBackup(outputFile, *genJSONFile); err != nil {
127 fmt.Printf("Could not write to generated .bzl file: %s\n", err)
128 os.Exit(1)
129 }
130 fmt.Printf("Wrote %d deps\n", count)
131 }
132
133 type repoConfig struct {
134 // https://bazel.build/rules/lib/repo/git
135 BuildFile string `json:"build_file,omitempty"`
136 Commit string `json:"commit"`
137 Name string `json:"name"`
138 PatchCmds []string `json:"patch_cmds,omitempty"`
139 PatchCmdsWin []string `json:"patch_cmds_win,omitempty"`
140 Patches []string `json:"patches,omitempty"`
141 Remote string `json:"remote"`
142 }
143
144 type deps struct {
145 Warning string `json:"!note"`
146 Direct []repoConfig `json:"direct"`
147 Indirect []repoConfig `json:"indirect"`
148 }
149
150 func parseDEPSFile(contents []string) (string, int, error) {
151 depsLine := regexp.MustCompile(`externals/(\S+)".+"(https.+)@([a-f0-9]+)"`)
152 outputFile, err := os.CreateTemp("", "genbzl")
153 if err != nil {
154 return "", 0, fmt.Errorf("Could not create output file: %s\n", err)
155 }
156 defer outputFile.Close()
157
158 var d deps
159 d.Warning = "DO NOT MODIFY BY HAND. Instead, bazelisk run //bazel/deps_parser"
160 for _, line := range contents {
161 if match := depsLine.FindStringSubmatch(line); len(match) > 0 {
162 rc := repoConfig{Name: match[1], Remote: match[2], Commit: match[3]}
163
164 cfg, ok := depsOverrides[rc.Name]
165 if !ok {
166 continue
167 }
168 if cfg.bazelNameOverride != "" {
169 rc.Name = cfg.bazelNameOverride
170 }
171 rc.Patches = cfg.patches
172 rc.PatchCmds = cfg.patchCmds
173 rc.PatchCmdsWin = cfg.patchCmdsWin
174 if cfg.needsBazelFile {
175 rc.BuildFile = fmt.Sprintf("//bazel/external/%s:BUILD.bazel", rc.Name)
176 }
177 if cfg.isIndirect {
178 d.Indirect = append(d.Indirect, rc)
179 } else {
180 d.Direct = append(d.Direct, rc)
181 }
182
183 }
184 }
185
186 je := json.NewEncoder(outputFile)
187 je.SetIndent("", " ")
188 if err := je.Encode(d); err != nil {
189 return "", 0, fmt.Errorf("Could not encode json file: %s\n", err)
190 }
191
192 return outputFile.Name(), len(d.Direct) + len(d.Indirect), nil
193 }
194
195 func moveWithCopyBackup(src, dst string) error {
196 // Atomically rename temp file to workspace. This should minimize the chance of corruption
197 // or writing a partial file if there is an error or the program is interrupted.
198 if err := os.Rename(src, dst); err != nil {
199 // Errors can happen if the temporary file is on a different partition than the Skia
200 // codebase. In that case, do a manual read/write to copy the data. See
201 // https://github.com/jenkins-x/jx/issues/449 for a similar issue
202 if strings.Contains(err.Error(), "invalid cross-device link") {
203 bytes, err := os.ReadFile(src)
204 if err != nil {
205 return fmt.Errorf("Could not do backup read from %s: %s\n", src, err)
206 }
207 if err := os.WriteFile(dst, bytes, 0644); err != nil {
208 return fmt.Errorf("Could not do backup write of %d bytes to %s: %s\n", len(bytes), dst, err)
209 }
210 // Backup "move" successful
211 return nil
212 }
213 return fmt.Errorf("Could not write %s -> %s: %s\n", src, dst, err)
214 }
215 return nil
216 }