// Copyright 2014 Google Inc. 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.rules.python;

import com.google.devtools.build.lib.actions.Artifact;
import com.google.devtools.build.lib.analysis.ConfiguredTarget;
import com.google.devtools.build.lib.analysis.RuleConfiguredTarget.Mode;
import com.google.devtools.build.lib.analysis.RuleConfiguredTargetBuilder;
import com.google.devtools.build.lib.analysis.RuleContext;
import com.google.devtools.build.lib.analysis.Runfiles;
import com.google.devtools.build.lib.analysis.RunfilesProvider;
import com.google.devtools.build.lib.analysis.RunfilesSupport;
import com.google.devtools.build.lib.analysis.TransitiveInfoCollection;
import com.google.devtools.build.lib.rules.RuleConfiguredTargetFactory;
import com.google.devtools.build.lib.rules.cpp.CcLinkParams;
import com.google.devtools.build.lib.rules.cpp.CcLinkParamsProvider;
import com.google.devtools.build.lib.rules.cpp.CcLinkParamsStore;

import java.util.List;

/**
 * An implementation for the {@code py_binary} rule.
 */
public abstract class PyBinary implements RuleConfiguredTargetFactory {
  /**
   * Create a {@link PythonSemantics} object that governs
   * the behavior of this rule.
   */
  protected abstract PythonSemantics createSemantics();

  @Override
  public ConfiguredTarget create(RuleContext ruleContext) {
    PyCommon common = new PyCommon(ruleContext);
    common.initCommon(common.getDefaultPythonVersion());

    RuleConfiguredTargetBuilder builder = init(ruleContext, createSemantics(), common);
    if (builder == null) {
      return null;
    }
    return builder.build();
  }

  static RuleConfiguredTargetBuilder init(
      RuleContext ruleContext, PythonSemantics semantics, PyCommon common) {
    List<Artifact> srcs = common.validateSrcs();
    CcLinkParamsStore ccLinkParamsStore = initializeCcLinkParamStore(ruleContext);

    common.initBinary(srcs);
    semantics.validate(ruleContext, common);
    if (ruleContext.hasErrors()) {
      return null;
    }

    semantics.createExecutable(ruleContext, common, ccLinkParamsStore);
    Runfiles.Builder runfilesBuilder = collectCommonRunfiles(ruleContext, common);
    semantics.collectRunfilesForBinary(ruleContext, runfilesBuilder, common);
    Runfiles dataRunfiles = runfilesBuilder.build();
    semantics.collectDefaultRunfilesForBinary(ruleContext, runfilesBuilder);
    Runfiles defaultRunfiles = runfilesBuilder.build();

    RunfilesSupport runfilesSupport = RunfilesSupport.withExecutable(ruleContext, defaultRunfiles,
        common.getExecutable(), ruleContext.shouldCreateRunfilesSymlinks());

    if (ruleContext.hasErrors()) {
      return null;
    }

    RunfilesProvider runfilesProvider = RunfilesProvider.withData(defaultRunfiles, dataRunfiles);

    RuleConfiguredTargetBuilder builder =
        new RuleConfiguredTargetBuilder(ruleContext);
    common.addCommonTransitiveInfoProviders(builder, semantics, common.getFilesToBuild());

    semantics.postInitBinary(ruleContext, runfilesSupport, common);
    return builder
        .setFilesToBuild(common.getFilesToBuild())
        .add(RunfilesProvider.class, runfilesProvider)
        .setRunfilesSupport(runfilesSupport, common.getExecutable())
        .add(CcLinkParamsProvider.class, new CcLinkParamsProvider(ccLinkParamsStore));
  }

  private static Runfiles.Builder collectCommonRunfiles(RuleContext ruleContext, PyCommon common) {
    Runfiles.Builder builder = new Runfiles.Builder();
    builder.addArtifact(common.getExecutable());
    if (common.getConvertedFiles() != null) {
      builder.addSymlinks(common.getConvertedFiles());
    } else {
      builder.addTransitiveArtifacts(common.getFilesToBuild());
    }
    builder.addRunfiles(ruleContext, RunfilesProvider.DEFAULT_RUNFILES);
    builder.add(ruleContext, PythonRunfilesProvider.TO_RUNFILES);
    builder.setEmptyFilesSupplier(PythonUtils.GET_INIT_PY_FILES);
    return builder;
  }

  private static CcLinkParamsStore initializeCcLinkParamStore(final RuleContext ruleContext) {
    return new CcLinkParamsStore() {
      @Override
      protected void collect(CcLinkParams.Builder builder, boolean linkingStatically,
                             boolean linkShared) {
        Iterable<? extends TransitiveInfoCollection> deps =
            ruleContext.getPrerequisites("deps", Mode.TARGET);
        builder.addTransitiveTargets(deps);
        builder.addTransitiveLangTargets(deps, PyCcLinkParamsProvider.TO_LINK_PARAMS);
      }
    };
  }
}


@@ -2648,32 +2648,23 @@
 iles
-.Builder runfilesBuilder
+ commonRunfiles
  = c
@@ -2703,22 +2703,17 @@
 , common
-);%0A   
+,
  semanti
@@ -2718,123 +2718,101 @@
 tics
-.collectRunfilesForBinary(ruleContext, runfilesBuilder, common);%0A    Runfiles dataRunfiles = runfilesBuilder.build(
+);%0A%0A    Runfiles.Builder defaultRunfilesBuilder = new Runfiles.Builder().merge(commonRunfiles
 );%0A 
@@ -2865,25 +2865,32 @@
 uleContext, 
-r
+defaultR
 unfilesBuild
@@ -2925,17 +2925,24 @@
 files = 
-r
+defaultR
 unfilesB
@@ -3192,24 +3192,400 @@
 ull;%0A    %7D%0A%0A
+    // Only include common runfiles and middleman. Default runfiles added by semantics are%0A    // excluded. The middleman is necessary to ensure the runfiles trees are generated for all%0A    // dependency binaries.%0A    Runfiles dataRunfiles = new Runfiles.Builder()%0A        .merge(commonRunfiles)%0A        .addArtifact(runfilesSupport.getRunfilesMiddleman())%0A        .build();%0A%0A
     Runfiles
@@ -4238,24 +4238,16 @@
 Runfiles
-.Builder
  collect
@@ -4293,32 +4293,65 @@
  PyCommon common
+,%0A      PythonSemantics semantics
 ) %7B%0A    Runfiles
@@ -4827,24 +4827,94 @@
 _PY_FILES);%0A
+    semantics.collectRunfilesForBinary(ruleContext, builder, common);%0A
     return b
@@ -4919,16 +4919,24 @@
  builder
+.build()
 ;%0A  %7D%0A%0A 

Attach runfiles middlemen to py-binary data runfiles