C++: Adds ctx to cc_link_params creation.

This is done so that we can check whether the current target can use the C++
Skylark API.

Rolling forward: BlazeInvocationPolicy is not used in host configuration. We simply ignore host configuration and not give an error when we are building there.

RELNOTES:none
PiperOrigin-RevId: 202652552
diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcCommon.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcCommon.java
index 856ae09..4c026ec 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcCommon.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcCommon.java
@@ -208,6 +208,14 @@
       throws EvalException {
     RuleContext context = skylarkRuleContext.getRuleContext();
     Rule rule = context.getRule();
+    if (!context.getConfiguration().isHostConfiguration()
+        && !context.getFragment(CppConfiguration.class).getEnableCcSkylarkApi()) {
+      throw new EvalException(
+          rule.getLocation(),
+          "Pass --experimental_enable_cc_skylark_api in "
+              + "order to use the C++ API. Beware that we will be making breaking "
+              + "changes to this API without prior warning.");
+    }
     RuleClass ruleClass = rule.getRuleClassObject();
     Label label = ruleClass.getRuleDefinitionEnvironmentLabel();
     if (label != null
diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CppConfiguration.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CppConfiguration.java
index db39874..965317c 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CppConfiguration.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CppConfiguration.java
@@ -923,6 +923,10 @@
     return cppOptions.expandLinkoptsLabels;
   }
 
+  public boolean getEnableCcSkylarkApi() {
+    return cppOptions.enableCcSkylarkApi;
+  }
+
   /**
    * Returns the path to the GNU binutils 'objcopy' binary to use for this build. (Corresponds to
    * $(OBJCOPY) in make-dbg.) Relative paths are relative to the execution root.
diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CppOptions.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CppOptions.java
index 4c54850..c9c05c1 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CppOptions.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CppOptions.java
@@ -789,6 +789,17 @@
       help = "If true, entries in linkopts that are not preceded by - or $ will be expanded.")
   public boolean expandLinkoptsLabels;
 
+  @Option(
+      name = "experimental_enable_cc_skylark_api",
+      defaultValue = "false",
+      documentationCategory = OptionDocumentationCategory.UNDOCUMENTED,
+      effectTags = {OptionEffectTag.LOADING_AND_ANALYSIS},
+      metadataTags = {OptionMetadataTag.EXPERIMENTAL},
+      help =
+          "If true, the C++ Skylark API can be used. Don't enable this flag yet, we will be making "
+              + "breaking changes.")
+  public boolean enableCcSkylarkApi;
+
   @Override
   public FragmentOptions getHost() {
     CppOptions host = (CppOptions) getDefault();