// Copyright 2016 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.rules.objc;

import static com.google.devtools.build.lib.packages.Attribute.attr;
import static com.google.devtools.build.lib.packages.BuildType.LABEL;
import static com.google.devtools.build.lib.packages.ImplicitOutputsFunction.fromTemplates;
import static com.google.devtools.build.lib.syntax.Type.BOOLEAN;

import com.google.devtools.build.lib.analysis.BaseRuleClasses;
import com.google.devtools.build.lib.analysis.RuleDefinition;
import com.google.devtools.build.lib.analysis.RuleDefinitionEnvironment;
import com.google.devtools.build.lib.packages.ImplicitOutputsFunction;
import com.google.devtools.build.lib.packages.ImplicitOutputsFunction.SafeImplicitOutputsFunction;
import com.google.devtools.build.lib.packages.RuleClass;
import com.google.devtools.build.lib.packages.RuleClass.Builder;
import com.google.devtools.build.lib.rules.apple.AppleConfiguration;

/**
 * Rule definition for apple_binary.
 */
public class AppleBinaryRule implements RuleDefinition {

  /**
   * Template for the fat binary output (using Apple's "lipo" tool to combine binaries of
   * multiple architectures).
   */
  private static final SafeImplicitOutputsFunction LIPOBIN = fromTemplates("%{name}_lipobin");
  
  /**
   * Template for the fat archive output (using Apple's "lipo" tool to combine .a archive files of
   * multiple architectures).
   */
  static final SafeImplicitOutputsFunction LIPO_ARCHIVE = fromTemplates("%{name}_lipo.a");

  @Override
  public RuleClass build(Builder builder, RuleDefinitionEnvironment env) {
    return builder
        .requiresConfigurationFragments(
            ObjcConfiguration.class, J2ObjcConfiguration.class, AppleConfiguration.class)
        .add(attr("$is_executable", BOOLEAN).value(true)
            .nonconfigurable("Called from RunCommand.isExecutable, which takes a Target"))
        .override(builder.copy("deps").cfg(AppleBinary.SPLIT_TRANSITION_PROVIDER))
        .override(builder.copy("non_propagated_deps").cfg(AppleBinary.SPLIT_TRANSITION_PROVIDER))
        // This is currently a hack to obtain all child configurations regardless of the attribute
        // values of this rule -- this rule does not currently use the actual info provided by
        // this attribute.
        .add(attr(":cc_toolchain", LABEL)
            .cfg(AppleBinary.SPLIT_TRANSITION_PROVIDER)
            .value(ObjcRuleClasses.APPLE_TOOLCHAIN))
        .setImplicitOutputsFunction(
            ImplicitOutputsFunction.fromFunctions(LIPOBIN, LIPO_ARCHIVE))
        .build();
  }

  @Override
  public Metadata getMetadata() {
    return RuleDefinition.Metadata.builder()
        .name("apple_binary")
        .factoryClass(AppleBinary.class)
        .ancestors(BaseRuleClasses.BaseRule.class, ObjcRuleClasses.LinkingRule.class,
            ObjcRuleClasses.XcodegenRule.class, ObjcRuleClasses.SimulatorRule.class)
        .build();
  }
}

@@ -3021,16 +3021,493 @@
 CHAIN))%0A
+        /*%3C!-- #BLAZE_RULE(apple_binary).IMPLICIT_OUTPUTS --%3E%0A        %3Cul%3E%0A         %3Cli%3E%3Ccode%3E%3Cvar%3Ename%3C/var%3E_lipobin%3C/code%3E: the 'lipo'ed potentially multi-architecture%0A             binary. All transitive dependencies and %3Ccode%3Esrcs%3C/code%3E are linked.%3C/li%3E%0A         %3Cli%3E%3Ccode%3E%3Cvar%3Ename%3C/var%3E_.lipo.a%3C/code%3E: a 'lipo'ed archive file linking together only%0A             the %3Ccode%3Esrcs%3C/code%3E of this target.%3C/li%3E%0A        %3C/ul%3E%0A        %3C!-- #END_BLAZE_RULE.IMPLICIT_OUTPUTS --%3E*/%0A
         
@@ -3912,16 +3912,17 @@
 ses.
-Xcodegen
+Simulator
 Rule
@@ -3931,67 +3931,669 @@
 lass
-, ObjcRuleClasses.SimulatorRule.class)%0A        .build();%0A  %7D%0A%7D
+)%0A        .build();%0A  %7D%0A%7D%0A%0A/*%3C!-- #BLAZE_RULE (NAME = apple_binary, TYPE = BINARY, FAMILY = Objective-C) --%3E%0A%0A%3Cp%3EThis rule produces single- or multi-architecture (%22fat%22) Objective-C libraries and/or binaries,%0Atypically used in creating apple bundles, such as frameworks, extensions, or applications.%3C/p%3E%0A%0A%3Cp%3EThe %3Ccode%3Elipo%3C/code%3E tool is used to combine files of multiple architectures. The%0A%3Ccode%3E--ios_multi_cpus%3C/code%3E flag controls which architectures are included in the output.%3C/p%3E%0A%0A%3Cp%3EThis rule currently only supports building for ios architectures, though more platforms%0Awill be supported in the future.%3C/p%3E%0A%0A$%7BIMPLICIT_OUTPUTS%7D%0A%0A%3C!-- #END_BLAZE_RULE --%3E*/
 %0A

Add documentation for apple_binary rule, making it visible for public consumption.