// 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.common.collect.ImmutableList;
import com.google.devtools.build.lib.actions.Artifact;
import com.google.devtools.build.lib.collect.nestedset.NestedSet;
import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder;
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();
  }

  @SkylarkCallable(
      name = "libs",
      structField = true,
      doc =
          "Returns the immutable set of libraries for either "
              + "FULLY STATIC mode (linkopts=[\"-static\"]) or MOSTLY STATIC mode (linkstatic=1) "
              + "(possibly empty but never None)")
  public NestedSet<Artifact> getLibraries() {
    NestedSetBuilder<Artifact> libs = NestedSetBuilder.linkOrder();
    CcLinkParamsProvider ccLinkParams = getInfo().getProvider(CcLinkParamsProvider.class);
    if (ccLinkParams == null) {
      return libs.build();
    }
    for (LinkerInput lib : ccLinkParams.getCcLinkParams(true, false).getLibraries()) {
      libs.add(lib.getArtifact());
    }
    return libs.build();
  }

  @SkylarkCallable(
      name = "link_flags",
      structField = true,
      doc =
          "Returns the immutable list of flags given to the C++ linker command for either "
              + "FULLY STATIC mode (linkopts=[\"-static\"]) or MOSTLY STATIC mode (linkstatic=1) "
              + "(possibly empty but never None)")
  public ImmutableList<String> getLinkopts() {
    CcLinkParamsProvider ccLinkParams = getInfo().getProvider(CcLinkParamsProvider.class);
    if (ccLinkParams == null) {
      return ImmutableList.of();
    }
    return ccLinkParams.getCcLinkParams(true, false).flattenedLinkopts();
  }
}

@@ -1076,16 +1076,71 @@
 kModule;
+%0Aimport com.google.devtools.build.lib.vfs.PathFragment;
 %0A%0A/**%0A *
@@ -3360,11 +3360,934 @@
 s();%0A  %7D
+%0A%0A  @SkylarkCallable(%0A      name = %22compile_flags%22,%0A      structField = true,%0A      doc =%0A          %22Returns the immutable set of flags used to compile this target %22%0A              + %22(possibly empty but never None).%22)%0A  public ImmutableList%3CString%3E getCcFlags() %7B%0A    CppCompilationContext ccContext = getInfo().getProvider(CppCompilationContext.class);%0A%0A    ImmutableList.Builder%3CString%3E options = ImmutableList.builder();%0A    for (String define : ccContext.getDefines()) %7B%0A      options.add(%22-D%22 + define);%0A    %7D%0A    for (PathFragment path : ccContext.getSystemIncludeDirs()) %7B%0A      options.add(%22-isystem %22 + path.getSafePathString());%0A    %7D%0A    for (PathFragment path : ccContext.getIncludeDirs()) %7B%0A      options.add(%22-I %22 + path.getSafePathString());%0A    %7D%0A    for (PathFragment path : ccContext.getQuoteIncludeDirs()) %7B%0A      options.add(%22-iquote %22 + path.getSafePathString());%0A    %7D%0A%0A    return options.build();%0A  %7D
 %0A%7D%0A

Add skylark methods to return the flags and includes needed to compile a target.