// Copyright 2015 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.cpp;

import com.google.devtools.build.lib.actions.Artifact;
import com.google.devtools.build.lib.collect.nestedset.NestedSet;
import com.google.devtools.build.lib.rules.SkylarkApiProvider;
import com.google.devtools.build.lib.syntax.SkylarkCallable;
import com.google.devtools.build.lib.syntax.SkylarkModule;

/**
 * A class that exposes the C++ providers to Skylark. It is intended to provide a
 * simple and stable interface for Skylark users.
 */
@SkylarkModule(
    name = "CcSkylarkApiProvider", doc = "Provides access to information about C++ rules")
public final class CcSkylarkApiProvider extends SkylarkApiProvider {
  /** The name of the field in Skylark used to access this class. */
  static final String NAME = "cc";

  @SkylarkCallable(
      name = "transitive_headers",
      structField = true,
      doc =
          "Returns the immutable set of headers that have been declared in the <code>src</code> "
              + "or <code>headers</code> attribute (possibly empty but never None).")
  public NestedSet<Artifact> getTransitiveHeaders() {
    CppCompilationContext ccContext = getInfo().getProvider(CppCompilationContext.class);
    return ccContext.getDeclaredIncludeSrcs();
  }
}

@@ -652,16 +652,64 @@
 s.cpp;%0A%0A
+import com.google.common.collect.ImmutableList;%0A
 import c
@@ -821,16 +821,89 @@
 tedSet;%0A
+import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder;%0A
 import c
@@ -1970,11 +1970,1346 @@
 s();%0A  %7D
+%0A%0A  @SkylarkCallable(%0A      name = %22libs%22,%0A      structField = true,%0A      doc =%0A          %22Returns the immutable set of libraries for either %22%0A              + %22FULLY STATIC mode (linkopts=%5B%5C%22-static%5C%22%5D) or MOSTLY STATIC mode (linkstatic=1) %22%0A              + %22(possibly empty but never None)%22)%0A  public NestedSet%3CArtifact%3E getLibraries() %7B%0A    NestedSetBuilder%3CArtifact%3E libs = NestedSetBuilder.linkOrder();%0A    CcLinkParamsProvider ccLinkParams = getInfo().getProvider(CcLinkParamsProvider.class);%0A    if (ccLinkParams == null) %7B%0A      return libs.build();%0A    %7D%0A    for (LinkerInput lib : ccLinkParams.getCcLinkParams(true, false).getLibraries()) %7B%0A      libs.add(lib.getArtifact());%0A    %7D%0A    return libs.build();%0A  %7D%0A%0A  @SkylarkCallable(%0A      name = %22link_flags%22,%0A      structField = true,%0A      doc =%0A          %22Returns the immutable list of flags given to the C++ linker command for either %22%0A              + %22FULLY STATIC mode (linkopts=%5B%5C%22-static%5C%22%5D) or MOSTLY STATIC mode (linkstatic=1) %22%0A              + %22(possibly empty but never None)%22)%0A  public ImmutableList%3CString%3E getLinkopts() %7B%0A    CcLinkParamsProvider ccLinkParams = getInfo().getProvider(CcLinkParamsProvider.class);%0A    if (ccLinkParams == null) %7B%0A      return ImmutableList.of();%0A    %7D%0A    return ccLinkParams.getCcLinkParams(true, false).flattenedLinkopts();%0A  %7D
 %0A%7D%0A

Add link_flags and libs methods to the C++ Skylark API