C++: Add legacy C++ Starlark provider that can be created from Starlark

The cc needs to be cleaned up from the depot. Right now it cannot be created
from a Starlark rule but the Starlark version of cc_library will need this.

This CL refactors the code to make it possible while keeping backwards
compatibility before the cleanup happens.

RELNOTES:none
PiperOrigin-RevId: 397079901
diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcStarlarkApiInfo.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcStarlarkApiInfo.java
new file mode 100644
index 0000000..7f586bc
--- /dev/null
+++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcStarlarkApiInfo.java
@@ -0,0 +1,196 @@
+// Copyright 2015 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.cpp;
+
+import com.google.common.base.Preconditions;
+import com.google.common.collect.ImmutableList;
+import com.google.devtools.build.lib.actions.Artifact;
+import com.google.devtools.build.lib.collect.nestedset.Depset;
+import com.google.devtools.build.lib.collect.nestedset.NestedSet;
+import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder;
+import com.google.devtools.build.lib.packages.BuiltinProvider;
+import com.google.devtools.build.lib.packages.NativeInfo;
+import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec;
+import com.google.devtools.build.lib.starlarkbuildapi.cpp.CcStarlarkApiProviderApi;
+import com.google.devtools.build.lib.vfs.PathFragment;
+
+/**
+ * A class that exposes the C++ providers to Starlark. It is intended to provide a simple and stable
+ * interface for Starlark users.
+ */
+@AutoCodec
+public final class CcStarlarkApiInfo extends NativeInfo
+    implements CcStarlarkApiProviderApi<Artifact> {
+  public static final BuiltinProvider<CcStarlarkApiInfo> PROVIDER =
+      new BuiltinProvider<CcStarlarkApiInfo>(
+          CcStarlarkApiProvider.NAME, CcStarlarkApiInfo.class) {};
+
+  private final CcInfo ccInfo;
+
+  public CcStarlarkApiInfo(CcInfo ccInfo) {
+    Preconditions.checkNotNull(ccInfo);
+    this.ccInfo = ccInfo;
+  }
+
+  @Override
+  public BuiltinProvider<CcStarlarkApiInfo> getProvider() {
+    return PROVIDER;
+  }
+
+  static Depset /*<Artifact>*/ getTransitiveHeadersForStarlark(CcInfo ccInfo) {
+    return Depset.of(Artifact.TYPE, getTransitiveHeaders(ccInfo));
+  }
+
+  @Override
+  public Depset /*<Artifact>*/ getTransitiveHeadersForStarlark() {
+    return CcStarlarkApiInfo.getTransitiveHeadersForStarlark(ccInfo);
+  }
+
+  NestedSet<Artifact> getTransitiveHeaders() {
+    return CcStarlarkApiInfo.getTransitiveHeaders(ccInfo);
+  }
+
+  static NestedSet<Artifact> getTransitiveHeaders(CcInfo ccInfo) {
+    CcCompilationContext ccCompilationContext = ccInfo.getCcCompilationContext();
+    return ccCompilationContext.getDeclaredIncludeSrcs();
+  }
+
+  @Override
+  public Depset /*<Artifact>*/ getLibrariesForStarlark() {
+    return CcStarlarkApiInfo.getLibrariesForStarlark(ccInfo);
+  }
+
+  static Depset /*<Artifact>*/ getLibrariesForStarlark(CcInfo ccInfo) {
+    return Depset.of(Artifact.TYPE, getLibraries(ccInfo));
+  }
+
+  NestedSet<Artifact> getLibraries() {
+    return CcStarlarkApiInfo.getLibraries(ccInfo);
+  }
+
+  static NestedSet<Artifact> getLibraries(CcInfo ccInfo) {
+    NestedSetBuilder<Artifact> libs = NestedSetBuilder.linkOrder();
+    if (ccInfo == null) {
+      return libs.build();
+    }
+    for (Artifact lib : ccInfo.getCcLinkingContext().getStaticModeParamsForExecutableLibraries()) {
+      libs.add(lib);
+    }
+    return libs.build();
+  }
+
+  @Override
+  public ImmutableList<String> getLinkopts() {
+    return CcStarlarkApiInfo.getLinkopts(ccInfo);
+  }
+
+  static ImmutableList<String> getLinkopts(CcInfo ccInfo) {
+    if (ccInfo == null) {
+      return ImmutableList.of();
+    }
+    return ccInfo.getCcLinkingContext().getFlattenedUserLinkFlags();
+  }
+
+  @Override
+  public ImmutableList<String> getDefines() {
+    CcCompilationContext ccCompilationContext = ccInfo.getCcCompilationContext();
+    return ccCompilationContext == null
+        ? ImmutableList.<String>of()
+        : ccCompilationContext.getDefines();
+  }
+
+  static ImmutableList<String> getDefines(CcInfo ccInfo) {
+    CcCompilationContext ccCompilationContext = ccInfo.getCcCompilationContext();
+    return ccCompilationContext == null
+        ? ImmutableList.<String>of()
+        : ccCompilationContext.getDefines();
+  }
+
+  @Override
+  public ImmutableList<String> getSystemIncludeDirs() {
+    return CcStarlarkApiInfo.getSystemIncludeDirs(ccInfo);
+  }
+
+  static ImmutableList<String> getSystemIncludeDirs(CcInfo ccInfo) {
+    CcCompilationContext ccCompilationContext = ccInfo.getCcCompilationContext();
+    if (ccCompilationContext == null) {
+      return ImmutableList.of();
+    }
+    ImmutableList.Builder<String> builder = ImmutableList.builder();
+    for (PathFragment path : ccCompilationContext.getSystemIncludeDirs()) {
+      builder.add(path.getSafePathString());
+    }
+    return builder.build();
+  }
+
+  @Override
+  public ImmutableList<String> getIncludeDirs() {
+    return CcStarlarkApiInfo.getIncludeDirs(ccInfo);
+  }
+
+  static ImmutableList<String> getIncludeDirs(CcInfo ccInfo) {
+    CcCompilationContext ccCompilationContext = ccInfo.getCcCompilationContext();
+    if (ccCompilationContext == null) {
+      return ImmutableList.of();
+    }
+    ImmutableList.Builder<String> builder = ImmutableList.builder();
+    for (PathFragment path : ccCompilationContext.getIncludeDirs()) {
+      builder.add(path.getSafePathString());
+    }
+    return builder.build();
+  }
+
+  @Override
+  public ImmutableList<String> getQuoteIncludeDirs() {
+    return CcStarlarkApiInfo.getQuoteIncludeDirs(ccInfo);
+  }
+
+  static ImmutableList<String> getQuoteIncludeDirs(CcInfo ccInfo) {
+    CcCompilationContext ccCompilationContext = ccInfo.getCcCompilationContext();
+    if (ccCompilationContext == null) {
+      return ImmutableList.of();
+    }
+    ImmutableList.Builder<String> builder = ImmutableList.builder();
+    for (PathFragment path : ccCompilationContext.getQuoteIncludeDirs()) {
+      builder.add(path.getSafePathString());
+    }
+    return builder.build();
+  }
+
+  @Override
+  public ImmutableList<String> getCcFlags() {
+    return CcStarlarkApiInfo.getCcFlags(ccInfo);
+  }
+
+  static ImmutableList<String> getCcFlags(CcInfo ccInfo) {
+    CcCompilationContext ccCompilationContext = ccInfo.getCcCompilationContext();
+
+    ImmutableList.Builder<String> options = ImmutableList.builder();
+    for (String define : ccCompilationContext.getDefines()) {
+      options.add("-D" + define);
+    }
+    for (PathFragment path : ccCompilationContext.getSystemIncludeDirs()) {
+      options.add("-isystem " + path.getSafePathString());
+    }
+    for (PathFragment path : ccCompilationContext.getIncludeDirs()) {
+      options.add("-I " + path.getSafePathString());
+    }
+    for (PathFragment path : ccCompilationContext.getQuoteIncludeDirs()) {
+      options.add("-iquote " + path.getSafePathString());
+    }
+
+    return options.build();
+  }
+}
diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcStarlarkApiProvider.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcStarlarkApiProvider.java
index 9758f5a..d7c9a17 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcStarlarkApiProvider.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcStarlarkApiProvider.java
@@ -15,24 +15,32 @@
 package com.google.devtools.build.lib.rules.cpp;
 
 import com.google.common.collect.ImmutableList;
+import com.google.devtools.build.docgen.annot.DocCategory;
 import com.google.devtools.build.lib.actions.Artifact;
 import com.google.devtools.build.lib.analysis.RuleConfiguredTargetBuilder;
 import com.google.devtools.build.lib.analysis.RuleContext;
 import com.google.devtools.build.lib.analysis.starlark.StarlarkApiProvider;
 import com.google.devtools.build.lib.collect.nestedset.Depset;
 import com.google.devtools.build.lib.collect.nestedset.NestedSet;
-import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder;
 import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec;
 import com.google.devtools.build.lib.starlarkbuildapi.cpp.CcStarlarkApiProviderApi;
-import com.google.devtools.build.lib.vfs.PathFragment;
+import net.starlark.java.annot.StarlarkBuiltin;
+import net.starlark.java.eval.StarlarkValue;
 
 /**
  * A class that exposes the C++ providers to Starlark. It is intended to provide a simple and stable
  * interface for Starlark users.
  */
 @AutoCodec
+@StarlarkBuiltin(
+    name = "CcStarlarkApiProvider",
+    category = DocCategory.PROVIDER,
+    doc =
+        "Provides access to information about C++ rules.  Every C++-related target provides this"
+            + " struct, accessible as a <code>cc</code> field on <a"
+            + " href=\"Target.html\">target</a>.")
 public final class CcStarlarkApiProvider extends StarlarkApiProvider
-    implements CcStarlarkApiProviderApi<Artifact> {
+    implements CcStarlarkApiProviderApi<Artifact>, StarlarkValue {
   /** The name of the field in Starlark used to access this class. */
   public static final String NAME = "cc";
 
@@ -44,39 +52,25 @@
 
   @Override
   public Depset /*<Artifact>*/ getTransitiveHeadersForStarlark() {
-    return Depset.of(Artifact.TYPE, getTransitiveHeaders());
+    return CcStarlarkApiInfo.getTransitiveHeadersForStarlark(getInfo().get(CcInfo.PROVIDER));
   }
 
   NestedSet<Artifact> getTransitiveHeaders() {
-    CcCompilationContext ccCompilationContext =
-        getInfo().get(CcInfo.PROVIDER).getCcCompilationContext();
-    return ccCompilationContext.getDeclaredIncludeSrcs();
+    return CcStarlarkApiInfo.getTransitiveHeaders(getInfo().get(CcInfo.PROVIDER));
   }
 
   @Override
   public Depset /*<Artifact>*/ getLibrariesForStarlark() {
-    return Depset.of(Artifact.TYPE, getLibraries());
+    return CcStarlarkApiInfo.getLibrariesForStarlark(getInfo().get(CcInfo.PROVIDER));
   }
 
   NestedSet<Artifact> getLibraries() {
-    NestedSetBuilder<Artifact> libs = NestedSetBuilder.linkOrder();
-    CcInfo ccInfo = getInfo().get(CcInfo.PROVIDER);
-    if (ccInfo == null) {
-      return libs.build();
-    }
-    for (Artifact lib : ccInfo.getCcLinkingContext().getStaticModeParamsForExecutableLibraries()) {
-      libs.add(lib);
-    }
-    return libs.build();
+    return CcStarlarkApiInfo.getLibraries(getInfo().get(CcInfo.PROVIDER));
   }
 
   @Override
   public ImmutableList<String> getLinkopts() {
-    CcInfo ccInfo = getInfo().get(CcInfo.PROVIDER);
-    if (ccInfo == null) {
-      return ImmutableList.of();
-    }
-    return ccInfo.getCcLinkingContext().getFlattenedUserLinkFlags();
+    return CcStarlarkApiInfo.getLinkopts(getInfo().get(CcInfo.PROVIDER));
   }
 
   @Override
@@ -90,65 +84,21 @@
 
   @Override
   public ImmutableList<String> getSystemIncludeDirs() {
-    CcCompilationContext ccCompilationContext =
-        getInfo().get(CcInfo.PROVIDER).getCcCompilationContext();
-    if (ccCompilationContext == null) {
-      return ImmutableList.of();
-    }
-    ImmutableList.Builder<String> builder = ImmutableList.builder();
-    for (PathFragment path : ccCompilationContext.getSystemIncludeDirs()) {
-      builder.add(path.getSafePathString());
-    }
-    return builder.build();
+    return CcStarlarkApiInfo.getSystemIncludeDirs(getInfo().get(CcInfo.PROVIDER));
   }
 
   @Override
   public ImmutableList<String> getIncludeDirs() {
-    CcCompilationContext ccCompilationContext =
-        getInfo().get(CcInfo.PROVIDER).getCcCompilationContext();
-    if (ccCompilationContext == null) {
-      return ImmutableList.of();
-    }
-    ImmutableList.Builder<String> builder = ImmutableList.builder();
-    for (PathFragment path : ccCompilationContext.getIncludeDirs()) {
-      builder.add(path.getSafePathString());
-    }
-    return builder.build();
+    return CcStarlarkApiInfo.getIncludeDirs(getInfo().get(CcInfo.PROVIDER));
   }
 
   @Override
   public ImmutableList<String> getQuoteIncludeDirs() {
-    CcCompilationContext ccCompilationContext =
-        getInfo().get(CcInfo.PROVIDER).getCcCompilationContext();
-    if (ccCompilationContext == null) {
-      return ImmutableList.of();
-    }
-    ImmutableList.Builder<String> builder = ImmutableList.builder();
-    for (PathFragment path : ccCompilationContext.getQuoteIncludeDirs()) {
-      builder.add(path.getSafePathString());
-    }
-    return builder.build();
+    return CcStarlarkApiInfo.getQuoteIncludeDirs(getInfo().get(CcInfo.PROVIDER));
   }
 
   @Override
   public ImmutableList<String> getCcFlags() {
-    CcCompilationContext ccCompilationContext =
-        getInfo().get(CcInfo.PROVIDER).getCcCompilationContext();
-
-    ImmutableList.Builder<String> options = ImmutableList.builder();
-    for (String define : ccCompilationContext.getDefines()) {
-      options.add("-D" + define);
-    }
-    for (PathFragment path : ccCompilationContext.getSystemIncludeDirs()) {
-      options.add("-isystem " + path.getSafePathString());
-    }
-    for (PathFragment path : ccCompilationContext.getIncludeDirs()) {
-      options.add("-I " + path.getSafePathString());
-    }
-    for (PathFragment path : ccCompilationContext.getQuoteIncludeDirs()) {
-      options.add("-iquote " + path.getSafePathString());
-    }
-
-    return options.build();
+    return CcStarlarkApiInfo.getCcFlags(getInfo().get(CcInfo.PROVIDER));
   }
 }
diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcStarlarkInternal.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcStarlarkInternal.java
index 8741d79..b706ce3 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcStarlarkInternal.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcStarlarkInternal.java
@@ -59,4 +59,14 @@
       throw new EvalException(e);
     }
   }
+
+  @StarlarkMethod(
+      name = "create_cc_provider",
+      documented = false,
+      parameters = {
+        @Param(name = "cc_info", positional = false, named = true),
+      })
+  public CcStarlarkApiInfo createCcProvider(CcInfo ccInfo) {
+    return new CcStarlarkApiInfo(ccInfo);
+  }
 }
diff --git a/src/main/java/com/google/devtools/build/lib/starlarkbuildapi/cpp/CcStarlarkApiProviderApi.java b/src/main/java/com/google/devtools/build/lib/starlarkbuildapi/cpp/CcStarlarkApiProviderApi.java
index ca19582..7aad98e 100644
--- a/src/main/java/com/google/devtools/build/lib/starlarkbuildapi/cpp/CcStarlarkApiProviderApi.java
+++ b/src/main/java/com/google/devtools/build/lib/starlarkbuildapi/cpp/CcStarlarkApiProviderApi.java
@@ -15,22 +15,12 @@
 package com.google.devtools.build.lib.starlarkbuildapi.cpp;
 
 import com.google.common.collect.ImmutableList;
-import com.google.devtools.build.docgen.annot.DocCategory;
 import com.google.devtools.build.lib.collect.nestedset.Depset;
 import com.google.devtools.build.lib.starlarkbuildapi.FileApi;
-import net.starlark.java.annot.StarlarkBuiltin;
 import net.starlark.java.annot.StarlarkMethod;
-import net.starlark.java.eval.StarlarkValue;
 
 /** Object with information about C++ rules. Every C++-related target should provide this. */
-@StarlarkBuiltin(
-    name = "CcStarlarkApiProvider",
-    category = DocCategory.PROVIDER,
-    doc =
-        "Provides access to information about C++ rules.  Every C++-related target provides this"
-            + " struct, accessible as a <code>cc</code> field on <a"
-            + " href=\"Target.html\">target</a>.")
-public interface CcStarlarkApiProviderApi<FileT extends FileApi> extends StarlarkValue {
+public interface CcStarlarkApiProviderApi<FileT extends FileApi> {
 
   @StarlarkMethod(
       name = "transitive_headers",