// Copyright 2014 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.devtools.build.lib.skyframe;

import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableSet;
import com.google.devtools.build.lib.actions.ActionKeyContext;
import com.google.devtools.build.lib.actions.Actions;
import com.google.devtools.build.lib.actions.Actions.GeneratingActions;
import com.google.devtools.build.lib.actions.ArtifactFactory;
import com.google.devtools.build.lib.actions.MutableActionGraph.ActionConflictException;
import com.google.devtools.build.lib.analysis.buildinfo.BuildInfoCollection;
import com.google.devtools.build.lib.analysis.buildinfo.BuildInfoFactory;
import com.google.devtools.build.lib.analysis.buildinfo.BuildInfoFactory.BuildInfoContext;
import com.google.devtools.build.lib.analysis.buildinfo.BuildInfoFactory.BuildInfoType;
import com.google.devtools.build.lib.analysis.buildinfo.BuildInfoKey;
import com.google.devtools.build.lib.analysis.config.BuildConfigurationValue;
import com.google.devtools.build.lib.skyframe.BuildInfoCollectionValue.BuildInfoKeyAndConfig;
import com.google.devtools.build.lib.skyframe.PrecomputedValue.Precomputed;
import com.google.devtools.build.skyframe.SkyFunction;
import com.google.devtools.build.skyframe.SkyKey;
import com.google.devtools.build.skyframe.SkyValue;
import java.util.Map;

/**
 * Creates a {@link BuildInfoCollectionValue}. Only depends on the unique {@link
 * WorkspaceStatusValue} and the constant {@link #BUILD_INFO_FACTORIES} injected value.
 *
 * <p>For more information, see {@link
 * com.google.devtools.build.lib.analysis.buildinfo.BuildInfoFactory}.
 */
public class BuildInfoCollectionFunction implements SkyFunction {

  public static final Precomputed<Map<BuildInfoKey, BuildInfoFactory>> BUILD_INFO_FACTORIES =
      new Precomputed<>("build_info_factories");

  private final ActionKeyContext actionKeyContext;
  private final ArtifactFactory artifactFactory;

  BuildInfoCollectionFunction(ActionKeyContext actionKeyContext, ArtifactFactory artifactFactory) {
    this.actionKeyContext = actionKeyContext;
    this.artifactFactory = artifactFactory;
  }

  @Override
  public SkyValue compute(SkyKey skyKey, Environment env) throws InterruptedException {
    final BuildInfoKeyAndConfig keyAndConfig = (BuildInfoKeyAndConfig) skyKey.argument();
    ImmutableSet<SkyKey> keysToRequest =
        ImmutableSet.of(
            WorkspaceStatusValue.BUILD_INFO_KEY,
            WorkspaceNameValue.key(),
            keyAndConfig.getConfigurationKey());
    Map<SkyKey, SkyValue> result = env.getValues(keysToRequest);
    if (env.valuesMissing()) {
      return null;
    }
    WorkspaceStatusValue infoArtifactValue =
        (WorkspaceStatusValue) result.get(WorkspaceStatusValue.BUILD_INFO_KEY);

    BuildConfigurationValue config =
        (BuildConfigurationValue) result.get(keyAndConfig.getConfigurationKey());
    Map<BuildInfoKey, BuildInfoFactory> buildInfoFactories = BUILD_INFO_FACTORIES.get(env);
    BuildInfoFactory buildInfoFactory = buildInfoFactories.get(keyAndConfig.getInfoKey());
    Preconditions.checkState(buildInfoFactory.isEnabled(config));

    BuildInfoContext context =
        (rootRelativePath, root, type) ->
            type == BuildInfoType.NO_REBUILD
                ? artifactFactory.getConstantMetadataArtifact(rootRelativePath, root, keyAndConfig)
                : artifactFactory.getDerivedArtifact(rootRelativePath, root, keyAndConfig);
    BuildInfoCollection collection =
        buildInfoFactory.create(
            context,
            config,
            infoArtifactValue.getStableArtifact(),
            infoArtifactValue.getVolatileArtifact());
    GeneratingActions generatingActions;
    try {
      generatingActions =
          Actions.assignOwnersAndFilterSharedActionsAndThrowActionConflict(
              actionKeyContext,
              collection.getActions(),
              keyAndConfig,
              /*outputFiles=*/ null);
    } catch (ActionConflictException | Actions.ArtifactGeneratedByOtherRuleException e) {
      throw new IllegalStateException("Errors not expected in build info: " + skyKey, e);
    }
    return new BuildInfoCollectionValue(collection, generatingActions);
  }
}

@@ -1563,24 +1563,82 @@
 ationValue;%0A
+import com.google.devtools.build.lib.bugreport.BugReport;%0A
 import com.g
@@ -1948,24 +1948,90 @@
 e.SkyValue;%0A
+import com.google.devtools.build.skyframe.SkyframeIterableResult;%0A
 import java.
@@ -3147,46 +3147,8 @@
 EY,%0A
-            WorkspaceNameValue.key(),%0A
     
@@ -3200,51 +3200,72 @@
     
-Map%3CSkyKey, SkyValue%3E result = env.getValue
+SkyframeIterableResult result = env.getOrderedValuesAndException
 s(ke
@@ -3378,24 +3378,16 @@
 tValue =
-%0A       
  (Worksp
@@ -3413,49 +3413,173 @@
 ult.
-get(WorkspaceStatusValue.BUILD_INFO_KEY);
+next();%0A    if (infoArtifactValue == null) %7B%0A      BugReport.logUnexpected(%22Value for: BuildInfoKey was missing, this should never happen%22);%0A      return null;%0A    %7D
 %0A%0A  
@@ -3612,24 +3612,16 @@
 config =
-%0A       
  (BuildC
@@ -3646,20 +3646,158 @@
  result.
-get(
+next();%0A    if (config == null) %7B%0A      BugReport.logUnexpected(%0A          %22Value for: '%25s' was missing, this should never happen%22,%0A          
 keyAndCo
@@ -3821,24 +3821,49 @@
 tionKey());%0A
+      return null;%0A    %7D%0A
     Map%3CBuil

Refactor `getValues` in `BuildInfoCollectionFunction` to create less garbage.