blob: 2facd4927bdabb4a39c982a1ce9a14027e773482 [file] [log] [blame]
Googler472b96d2024-03-22 14:06:23 -07001// Copyright 2024 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.
14package com.google.devtools.build.lib.analysis;
15
Googler239682b2024-05-30 08:15:14 -070016import static com.google.common.collect.ImmutableMap.toImmutableMap;
Googler472b96d2024-03-22 14:06:23 -070017
18import com.google.common.collect.ImmutableList;
19import com.google.common.collect.ImmutableMap;
20import com.google.common.collect.ImmutableMultimap;
Googler253fedc2024-10-15 15:05:02 -070021import com.google.common.collect.ImmutableSet;
Googler89e602a2024-04-04 11:06:53 -070022import com.google.devtools.build.lib.analysis.config.BuildOptions;
23import com.google.devtools.build.lib.analysis.config.CoreOptions;
24import com.google.devtools.build.lib.analysis.config.InvalidConfigurationException;
Googler472b96d2024-03-22 14:06:23 -070025import com.google.devtools.build.lib.cmdline.Label;
Googler472b96d2024-03-22 14:06:23 -070026import com.google.devtools.build.lib.events.ExtendedEventHandler;
Googler89e602a2024-04-04 11:06:53 -070027import com.google.devtools.build.lib.server.FailureDetails.BuildConfiguration.Code;
Googler239682b2024-05-30 08:15:14 -070028import com.google.devtools.build.lib.skyframe.ProjectFilesLookupValue;
Googler472b96d2024-03-22 14:06:23 -070029import com.google.devtools.build.lib.skyframe.SkyframeExecutor;
Googler89e602a2024-04-04 11:06:53 -070030import com.google.devtools.build.lib.skyframe.config.FlagSetValue;
Googler89e602a2024-04-04 11:06:53 -070031import com.google.devtools.build.skyframe.EvaluationResult;
Googler472b96d2024-03-22 14:06:23 -070032import com.google.devtools.build.skyframe.SkyKey;
Googler89e602a2024-04-04 11:06:53 -070033import com.google.devtools.build.skyframe.SkyValue;
Googler472b96d2024-03-22 14:06:23 -070034import java.util.Collection;
Googler472b96d2024-03-22 14:06:23 -070035
36/**
37 * Container for reading project metadata.
38 *
39 * <p>A "project" is a set of related packages that support a common piece of software. For example,
40 * "bazel" is a project that includes packages {@code src/main/java/com/google/devtools/build/lib},
41 * {@code src/main/java/com/google/devtools/build/lib/analysis}, {@code src/test/cpp}, and more.
42 *
43 * <p>"Project metadata" is any useful information that might be associated with a project. Possible
44 * consumers include <a
45 * href="https://github.com/bazelbuild/bazel/commit/693215317a6732085731809266f63ff0e7fc31a5">
46 * Skyfocus</a>> and project-sanctioned build flags (i.e. "these are the correct flags to use with
47 * this project").
48 *
49 * <p>Projects are defined in .scl files that are checked into source control with BUILD files and
50 * code. scl stands for "Starlark configuration language". This is a limited subset of Starlark
51 * intended to model generic configuration without requiring Bazel to parse it (similar to JSON).
52 *
53 * <p>This is not the same as {@link com.google.devtools.build.lib.runtime.ProjectFile}. That's an
54 * older implementation of the same idea that was built before .scl and .bzl existed. The code here
55 * is a rejuvenation of these ideas with more modern APIs.
56 */
57// TODO: b/324127050 - Make the co-existence of this and ProjectFile less confusing. ProjectFile is
58// an outdated API that should be removed.
59public final class Project {
60 private Project() {}
61
62 /** Thrown when project data can't be read. */
Googler53857ef2024-03-26 13:00:13 -070063 public static class ProjectParseException extends Exception {
Googler472b96d2024-03-22 14:06:23 -070064 ProjectParseException(String msg, Throwable cause) {
65 super(msg, cause);
66 }
67 }
68
69 /**
70 * Finds and returns the project files for a set of build targets.
71 *
72 * <p>This walks up each target's package path looking for {@link
Googler89c48662024-06-03 23:57:20 -070073 * com.google.devtools.build.lib.skyframe.ProjectFilesLookupFunction#PROJECT_FILE_NAME} files. For
74 * example, for {@code //foo/bar/baz:mytarget}, this might look in {@code foo/bar/baz}, {@code
75 * foo/bar}, and {@code foo} ("might" because it skips directories that don't have BUILD files -
76 * those directories aren't packages).
Googler472b96d2024-03-22 14:06:23 -070077 *
Googler239682b2024-05-30 08:15:14 -070078 * @return a map from each target to its set of project files, ordered by reverse package depth.
79 * So a project file in {@code foo/bar} appears before a project file in {@code foo}.
Googler472b96d2024-03-22 14:06:23 -070080 */
81 // TODO: b/324127050 - Document resolution semantics when this is less experimental.
Googler239682b2024-05-30 08:15:14 -070082 public static ImmutableMultimap<Label, Label> findProjectFiles(
Googler472b96d2024-03-22 14:06:23 -070083 Collection<Label> targets,
84 SkyframeExecutor skyframeExecutor,
85 ExtendedEventHandler eventHandler)
86 throws ProjectParseException {
Googler239682b2024-05-30 08:15:14 -070087 // TODO: b/324127050 - Support other repos.
88 ImmutableMap<Label, ProjectFilesLookupValue.Key> targetsToSkyKeys =
Googler472b96d2024-03-22 14:06:23 -070089 targets.stream()
Googler239682b2024-05-30 08:15:14 -070090 .collect(
91 toImmutableMap(
92 target -> target,
93 target -> ProjectFilesLookupValue.key(target.getPackageIdentifier())));
94 var evalResult =
95 skyframeExecutor.evaluateSkyKeys(
96 eventHandler, targetsToSkyKeys.values(), /* keepGoing= */ false);
Googler472b96d2024-03-22 14:06:23 -070097 if (evalResult.hasError()) {
98 throw new ProjectParseException(
99 "Error finding project files", evalResult.getError().getException());
100 }
Googler472b96d2024-03-22 14:06:23 -0700101
Googler239682b2024-05-30 08:15:14 -0700102 ImmutableMultimap.Builder<Label, Label> ans = ImmutableMultimap.builder();
103 for (var entry : targetsToSkyKeys.entrySet()) {
104 ProjectFilesLookupValue containingProjects =
105 (ProjectFilesLookupValue) evalResult.get(entry.getValue());
106 ans.putAll(entry.getKey(), containingProjects.getProjectFiles());
Googler472b96d2024-03-22 14:06:23 -0700107 }
108 return ans.build();
109 }
Googler89e602a2024-04-04 11:06:53 -0700110
111 /**
Googlerb5c273a2024-06-18 15:41:55 -0700112 * Applies {@link CoreOptions.sclConfig} to the top-level {@link BuildOptions}.
Googler89e602a2024-04-04 11:06:53 -0700113 *
Googlerb5c273a2024-06-18 15:41:55 -0700114 * <p>Given an existing PROJECT.scl file and an {@link CoreOptions.sclConfig}, the method creates
Googler89e602a2024-04-04 11:06:53 -0700115 * a {@link SkyKey} containing the {@link PathFragment} of the scl file and the config name which
Googlerb5c273a2024-06-18 15:41:55 -0700116 * is evaluated by the {@link FlagSetFunction}.
Googler89e602a2024-04-04 11:06:53 -0700117 *
118 * @return {@link FlagSetValue} which has the effective top-level {@link BuildOptions} after
119 * project file resolution.
120 */
121 public static FlagSetValue modifyBuildOptionsWithFlagSets(
Googler239682b2024-05-30 08:15:14 -0700122 Label projectFile,
Googler89e602a2024-04-04 11:06:53 -0700123 BuildOptions targetOptions,
Googler253fedc2024-10-15 15:05:02 -0700124 ImmutableSet<String> userOptions,
Googlerb5c273a2024-06-18 15:41:55 -0700125 boolean enforceCanonicalConfigs,
Googler89e602a2024-04-04 11:06:53 -0700126 ExtendedEventHandler eventHandler,
127 SkyframeExecutor skyframeExecutor)
128 throws InvalidConfigurationException {
129
130 FlagSetValue.Key flagSetKey =
131 FlagSetValue.Key.create(
Googlerb5c273a2024-06-18 15:41:55 -0700132 projectFile,
133 targetOptions.get(CoreOptions.class).sclConfig,
134 targetOptions,
Googler253fedc2024-10-15 15:05:02 -0700135 userOptions,
Googlerb5c273a2024-06-18 15:41:55 -0700136 enforceCanonicalConfigs);
Googler89e602a2024-04-04 11:06:53 -0700137
138 EvaluationResult<SkyValue> result =
139 skyframeExecutor.evaluateSkyKeys(
140 eventHandler, ImmutableList.of(flagSetKey), /* keepGoing= */ false);
141 if (result.hasError()) {
Googlerb5c273a2024-06-18 15:41:55 -0700142 throw new InvalidConfigurationException(
143 "Cannot parse options: " + result.getError().getException().getMessage(),
144 Code.INVALID_BUILD_OPTIONS);
Googler89e602a2024-04-04 11:06:53 -0700145 }
146 return (FlagSetValue) result.get(flagSetKey);
147 }
Googler472b96d2024-03-22 14:06:23 -0700148}