xref: /aosp_15_r20/external/bazelbuild-rules_go/go/tools/gopackagesdriver/bazel_json_builder.go (revision 9bb1b549b6a84214c53be0924760be030e66b93a)
1// Copyright 2021 The Bazel Authors. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//    http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package main
16
17import (
18	"bufio"
19	"context"
20	"errors"
21	"fmt"
22	"io/ioutil"
23	"os"
24	"path"
25	"path/filepath"
26	"regexp"
27	"strings"
28)
29
30type BazelJSONBuilder struct {
31	bazel        *Bazel
32	includeTests bool
33}
34
35var RulesGoStdlibLabel = rulesGoRepositoryName + "//:stdlib"
36
37var _defaultKinds = []string{"go_library", "go_test", "go_binary"}
38
39var externalRe = regexp.MustCompile(".*\\/external\\/([^\\/]+)(\\/(.*))?\\/([^\\/]+.go)")
40
41func (b *BazelJSONBuilder) fileQuery(filename string) string {
42	label := filename
43
44	if filepath.IsAbs(filename) {
45		label, _ = filepath.Rel(b.bazel.WorkspaceRoot(), filename)
46	} else if strings.HasPrefix(filename, "./") {
47		label = strings.TrimPrefix(filename, "./")
48	}
49
50	if matches := externalRe.FindStringSubmatch(filename); len(matches) == 5 {
51		// if filepath is for a third party lib, we need to know, what external
52		// library this file is part of.
53		matches = append(matches[:2], matches[3:]...)
54		label = fmt.Sprintf("@%s//%s", matches[1], strings.Join(matches[2:], ":"))
55	}
56
57	relToBin, err := filepath.Rel(b.bazel.info["output_path"], filename)
58	if err == nil && !strings.HasPrefix(relToBin, "../") {
59		parts := strings.SplitN(relToBin, string(filepath.Separator), 3)
60		relToBin = parts[2]
61		// We've effectively converted filename from bazel-bin/some/path.go to some/path.go;
62		// Check if a BUILD.bazel files exists under this dir, if not walk up and repeat.
63		relToBin = filepath.Dir(relToBin)
64		_, err = os.Stat(filepath.Join(b.bazel.WorkspaceRoot(), relToBin, "BUILD.bazel"))
65		for errors.Is(err, os.ErrNotExist) && relToBin != "." {
66			relToBin = filepath.Dir(relToBin)
67			_, err = os.Stat(filepath.Join(b.bazel.WorkspaceRoot(), relToBin, "BUILD.bazel"))
68		}
69
70		if err == nil {
71			// return package path found and build all targets (codegen doesn't fall under go_library)
72			// Otherwise fallback to default
73			if relToBin == "." {
74				relToBin = ""
75			}
76			label = fmt.Sprintf("//%s:all", relToBin)
77			additionalKinds = append(additionalKinds, "go_.*")
78		}
79	}
80
81	kinds := append(_defaultKinds, additionalKinds...)
82	return fmt.Sprintf(`kind("%s", same_pkg_direct_rdeps("%s"))`, strings.Join(kinds, "|"), label)
83}
84
85func (b *BazelJSONBuilder) getKind() string {
86	kinds := []string{"go_library"}
87	if b.includeTests {
88		kinds = append(kinds, "go_test")
89	}
90
91	return strings.Join(kinds, "|")
92}
93
94func (b *BazelJSONBuilder) localQuery(request string) string {
95	request = path.Clean(request)
96	if filepath.IsAbs(request) {
97		if relPath, err := filepath.Rel(workspaceRoot, request); err == nil {
98			request = relPath
99		}
100	}
101
102	if !strings.HasSuffix(request, "...") {
103		request = fmt.Sprintf("%s:*", request)
104	}
105
106	return fmt.Sprintf(`kind("%s", %s)`, b.getKind(), request)
107}
108
109func (b *BazelJSONBuilder) packageQuery(importPath string) string {
110	if strings.HasSuffix(importPath, "/...") {
111		importPath = fmt.Sprintf(`^%s(/.+)?$`, strings.TrimSuffix(importPath, "/..."))
112	}
113
114	return fmt.Sprintf(
115		`kind("%s", attr(importpath, "%s", deps(%s)))`,
116		b.getKind(),
117		importPath,
118		bazelQueryScope)
119}
120
121func (b *BazelJSONBuilder) queryFromRequests(requests ...string) string {
122	ret := make([]string, 0, len(requests))
123	for _, request := range requests {
124		result := ""
125		if strings.HasSuffix(request, ".go") {
126			f := strings.TrimPrefix(request, "file=")
127			result = b.fileQuery(f)
128		} else if isLocalPattern(request) {
129			result = b.localQuery(request)
130		} else if request == "builtin" || request == "std" {
131			result = fmt.Sprintf(RulesGoStdlibLabel)
132		} else if bazelQueryScope != "" {
133			result = b.packageQuery(request)
134		}
135
136		if result != "" {
137			ret = append(ret, result)
138		}
139	}
140	if len(ret) == 0 {
141		return RulesGoStdlibLabel
142	}
143	return strings.Join(ret, " union ")
144}
145
146func NewBazelJSONBuilder(bazel *Bazel, includeTests bool) (*BazelJSONBuilder, error) {
147	return &BazelJSONBuilder{
148		bazel:        bazel,
149		includeTests: includeTests,
150	}, nil
151}
152
153func (b *BazelJSONBuilder) outputGroupsForMode(mode LoadMode) string {
154	og := "go_pkg_driver_json_file,go_pkg_driver_stdlib_json_file,go_pkg_driver_srcs"
155	if mode&NeedExportsFile != 0 {
156		og += ",go_pkg_driver_export_file"
157	}
158	return og
159}
160
161func (b *BazelJSONBuilder) query(ctx context.Context, query string) ([]string, error) {
162	queryArgs := concatStringsArrays(bazelQueryFlags, []string{
163		"--ui_event_filters=-info,-stderr",
164		"--noshow_progress",
165		"--order_output=no",
166		"--output=label",
167		"--nodep_deps",
168		"--noimplicit_deps",
169		"--notool_deps",
170		query,
171	})
172	labels, err := b.bazel.Query(ctx, queryArgs...)
173	if err != nil {
174		return nil, fmt.Errorf("unable to query: %w", err)
175	}
176
177	return labels, nil
178}
179
180func (b *BazelJSONBuilder) Labels(ctx context.Context, requests []string) ([]string, error) {
181	labels, err := b.query(ctx, b.queryFromRequests(requests...))
182	if err != nil {
183		return nil, fmt.Errorf("query failed: %w", err)
184	}
185
186	if len(labels) == 0 {
187		return nil, fmt.Errorf("found no labels matching the requests")
188	}
189
190	return labels, nil
191}
192
193func (b *BazelJSONBuilder) Build(ctx context.Context, labels []string, mode LoadMode) ([]string, error) {
194	aspects := append(additionalAspects, goDefaultAspect)
195
196	buildArgs := concatStringsArrays([]string{
197		"--experimental_convenience_symlinks=ignore",
198		"--ui_event_filters=-info,-stderr",
199		"--noshow_progress",
200		"--aspects=" + strings.Join(aspects, ","),
201		"--output_groups=" + b.outputGroupsForMode(mode),
202		"--keep_going", // Build all possible packages
203	}, bazelBuildFlags)
204
205	if len(labels) < 100 {
206		buildArgs = append(buildArgs, labels...)
207	} else {
208		// To avoid hitting MAX_ARGS length, write labels to a file and use `--target_pattern_file`
209		targetsFile, err := ioutil.TempFile("", "gopackagesdriver_targets_")
210		if err != nil {
211			return nil, fmt.Errorf("unable to create target pattern file: %w", err)
212		}
213		writer := bufio.NewWriter(targetsFile)
214		defer writer.Flush()
215		for _, l := range labels {
216			writer.WriteString(l + "\n")
217		}
218		if err := writer.Flush(); err != nil {
219			return nil, fmt.Errorf("unable to flush data to target pattern file: %w", err)
220		}
221		defer func() {
222			targetsFile.Close()
223			os.Remove(targetsFile.Name())
224		}()
225
226		buildArgs = append(buildArgs, "--target_pattern_file="+targetsFile.Name())
227	}
228	files, err := b.bazel.Build(ctx, buildArgs...)
229	if err != nil {
230		return nil, fmt.Errorf("unable to bazel build %v: %w", buildArgs, err)
231	}
232
233	ret := []string{}
234	for _, f := range files {
235		if strings.HasSuffix(f, ".pkg.json") {
236			ret = append(ret, f)
237		}
238	}
239
240	return ret, nil
241}
242
243func (b *BazelJSONBuilder) PathResolver() PathResolverFunc {
244	return func(p string) string {
245		p = strings.Replace(p, "__BAZEL_EXECROOT__", b.bazel.ExecutionRoot(), 1)
246		p = strings.Replace(p, "__BAZEL_WORKSPACE__", b.bazel.WorkspaceRoot(), 1)
247		p = strings.Replace(p, "__BAZEL_OUTPUT_BASE__", b.bazel.OutputBase(), 1)
248		return p
249	}
250}
251