Googler | 472b96d | 2024-03-22 14:06:23 -0700 | [diff] [blame] | 1 | // 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. |
| 14 | package com.google.devtools.build.lib.analysis; |
| 15 | |
Googler | 239682b | 2024-05-30 08:15:14 -0700 | [diff] [blame] | 16 | import static com.google.common.collect.ImmutableMap.toImmutableMap; |
Googler | 472b96d | 2024-03-22 14:06:23 -0700 | [diff] [blame] | 17 | |
| 18 | import com.google.common.collect.ImmutableList; |
| 19 | import com.google.common.collect.ImmutableMap; |
| 20 | import com.google.common.collect.ImmutableMultimap; |
Googler | 253fedc | 2024-10-15 15:05:02 -0700 | [diff] [blame] | 21 | import com.google.common.collect.ImmutableSet; |
Googler | 89e602a | 2024-04-04 11:06:53 -0700 | [diff] [blame] | 22 | import com.google.devtools.build.lib.analysis.config.BuildOptions; |
| 23 | import com.google.devtools.build.lib.analysis.config.CoreOptions; |
| 24 | import com.google.devtools.build.lib.analysis.config.InvalidConfigurationException; |
Googler | 472b96d | 2024-03-22 14:06:23 -0700 | [diff] [blame] | 25 | import com.google.devtools.build.lib.cmdline.Label; |
Googler | 472b96d | 2024-03-22 14:06:23 -0700 | [diff] [blame] | 26 | import com.google.devtools.build.lib.events.ExtendedEventHandler; |
Googler | 89e602a | 2024-04-04 11:06:53 -0700 | [diff] [blame] | 27 | import com.google.devtools.build.lib.server.FailureDetails.BuildConfiguration.Code; |
Googler | 239682b | 2024-05-30 08:15:14 -0700 | [diff] [blame] | 28 | import com.google.devtools.build.lib.skyframe.ProjectFilesLookupValue; |
Googler | 472b96d | 2024-03-22 14:06:23 -0700 | [diff] [blame] | 29 | import com.google.devtools.build.lib.skyframe.SkyframeExecutor; |
Googler | 89e602a | 2024-04-04 11:06:53 -0700 | [diff] [blame] | 30 | import com.google.devtools.build.lib.skyframe.config.FlagSetValue; |
Googler | 89e602a | 2024-04-04 11:06:53 -0700 | [diff] [blame] | 31 | import com.google.devtools.build.skyframe.EvaluationResult; |
Googler | 472b96d | 2024-03-22 14:06:23 -0700 | [diff] [blame] | 32 | import com.google.devtools.build.skyframe.SkyKey; |
Googler | 89e602a | 2024-04-04 11:06:53 -0700 | [diff] [blame] | 33 | import com.google.devtools.build.skyframe.SkyValue; |
Googler | 472b96d | 2024-03-22 14:06:23 -0700 | [diff] [blame] | 34 | import java.util.Collection; |
Googler | 472b96d | 2024-03-22 14:06:23 -0700 | [diff] [blame] | 35 | |
| 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. |
| 59 | public final class Project { |
| 60 | private Project() {} |
| 61 | |
| 62 | /** Thrown when project data can't be read. */ |
Googler | 53857ef | 2024-03-26 13:00:13 -0700 | [diff] [blame] | 63 | public static class ProjectParseException extends Exception { |
Googler | 472b96d | 2024-03-22 14:06:23 -0700 | [diff] [blame] | 64 | 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 |
Googler | 89c4866 | 2024-06-03 23:57:20 -0700 | [diff] [blame] | 73 | * 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). |
Googler | 472b96d | 2024-03-22 14:06:23 -0700 | [diff] [blame] | 77 | * |
Googler | 239682b | 2024-05-30 08:15:14 -0700 | [diff] [blame] | 78 | * @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}. |
Googler | 472b96d | 2024-03-22 14:06:23 -0700 | [diff] [blame] | 80 | */ |
| 81 | // TODO: b/324127050 - Document resolution semantics when this is less experimental. |
Googler | 239682b | 2024-05-30 08:15:14 -0700 | [diff] [blame] | 82 | public static ImmutableMultimap<Label, Label> findProjectFiles( |
Googler | 472b96d | 2024-03-22 14:06:23 -0700 | [diff] [blame] | 83 | Collection<Label> targets, |
| 84 | SkyframeExecutor skyframeExecutor, |
| 85 | ExtendedEventHandler eventHandler) |
| 86 | throws ProjectParseException { |
Googler | 239682b | 2024-05-30 08:15:14 -0700 | [diff] [blame] | 87 | // TODO: b/324127050 - Support other repos. |
| 88 | ImmutableMap<Label, ProjectFilesLookupValue.Key> targetsToSkyKeys = |
Googler | 472b96d | 2024-03-22 14:06:23 -0700 | [diff] [blame] | 89 | targets.stream() |
Googler | 239682b | 2024-05-30 08:15:14 -0700 | [diff] [blame] | 90 | .collect( |
| 91 | toImmutableMap( |
| 92 | target -> target, |
| 93 | target -> ProjectFilesLookupValue.key(target.getPackageIdentifier()))); |
| 94 | var evalResult = |
| 95 | skyframeExecutor.evaluateSkyKeys( |
| 96 | eventHandler, targetsToSkyKeys.values(), /* keepGoing= */ false); |
Googler | 472b96d | 2024-03-22 14:06:23 -0700 | [diff] [blame] | 97 | if (evalResult.hasError()) { |
| 98 | throw new ProjectParseException( |
| 99 | "Error finding project files", evalResult.getError().getException()); |
| 100 | } |
Googler | 472b96d | 2024-03-22 14:06:23 -0700 | [diff] [blame] | 101 | |
Googler | 239682b | 2024-05-30 08:15:14 -0700 | [diff] [blame] | 102 | 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()); |
Googler | 472b96d | 2024-03-22 14:06:23 -0700 | [diff] [blame] | 107 | } |
| 108 | return ans.build(); |
| 109 | } |
Googler | 89e602a | 2024-04-04 11:06:53 -0700 | [diff] [blame] | 110 | |
| 111 | /** |
Googler | b5c273a | 2024-06-18 15:41:55 -0700 | [diff] [blame] | 112 | * Applies {@link CoreOptions.sclConfig} to the top-level {@link BuildOptions}. |
Googler | 89e602a | 2024-04-04 11:06:53 -0700 | [diff] [blame] | 113 | * |
Googler | b5c273a | 2024-06-18 15:41:55 -0700 | [diff] [blame] | 114 | * <p>Given an existing PROJECT.scl file and an {@link CoreOptions.sclConfig}, the method creates |
Googler | 89e602a | 2024-04-04 11:06:53 -0700 | [diff] [blame] | 115 | * a {@link SkyKey} containing the {@link PathFragment} of the scl file and the config name which |
Googler | b5c273a | 2024-06-18 15:41:55 -0700 | [diff] [blame] | 116 | * is evaluated by the {@link FlagSetFunction}. |
Googler | 89e602a | 2024-04-04 11:06:53 -0700 | [diff] [blame] | 117 | * |
| 118 | * @return {@link FlagSetValue} which has the effective top-level {@link BuildOptions} after |
| 119 | * project file resolution. |
| 120 | */ |
| 121 | public static FlagSetValue modifyBuildOptionsWithFlagSets( |
Googler | 239682b | 2024-05-30 08:15:14 -0700 | [diff] [blame] | 122 | Label projectFile, |
Googler | 89e602a | 2024-04-04 11:06:53 -0700 | [diff] [blame] | 123 | BuildOptions targetOptions, |
Googler | 253fedc | 2024-10-15 15:05:02 -0700 | [diff] [blame] | 124 | ImmutableSet<String> userOptions, |
Googler | b5c273a | 2024-06-18 15:41:55 -0700 | [diff] [blame] | 125 | boolean enforceCanonicalConfigs, |
Googler | 89e602a | 2024-04-04 11:06:53 -0700 | [diff] [blame] | 126 | ExtendedEventHandler eventHandler, |
| 127 | SkyframeExecutor skyframeExecutor) |
| 128 | throws InvalidConfigurationException { |
| 129 | |
| 130 | FlagSetValue.Key flagSetKey = |
| 131 | FlagSetValue.Key.create( |
Googler | b5c273a | 2024-06-18 15:41:55 -0700 | [diff] [blame] | 132 | projectFile, |
| 133 | targetOptions.get(CoreOptions.class).sclConfig, |
| 134 | targetOptions, |
Googler | 253fedc | 2024-10-15 15:05:02 -0700 | [diff] [blame] | 135 | userOptions, |
Googler | b5c273a | 2024-06-18 15:41:55 -0700 | [diff] [blame] | 136 | enforceCanonicalConfigs); |
Googler | 89e602a | 2024-04-04 11:06:53 -0700 | [diff] [blame] | 137 | |
| 138 | EvaluationResult<SkyValue> result = |
| 139 | skyframeExecutor.evaluateSkyKeys( |
| 140 | eventHandler, ImmutableList.of(flagSetKey), /* keepGoing= */ false); |
| 141 | if (result.hasError()) { |
Googler | b5c273a | 2024-06-18 15:41:55 -0700 | [diff] [blame] | 142 | throw new InvalidConfigurationException( |
| 143 | "Cannot parse options: " + result.getError().getException().getMessage(), |
| 144 | Code.INVALID_BUILD_OPTIONS); |
Googler | 89e602a | 2024-04-04 11:06:53 -0700 | [diff] [blame] | 145 | } |
| 146 | return (FlagSetValue) result.get(flagSetKey); |
| 147 | } |
Googler | 472b96d | 2024-03-22 14:06:23 -0700 | [diff] [blame] | 148 | } |