Expose License to build API as a deprecated opaque type

Prior to this change, attr.license() is already deprecated and subject to removal via --incompatible_no_attr_license. However, until this flag is fully flipped, the value of license attributes needs to be well defined. This change makes it so while providing nothing more than an opaque type.

Progress toward #7281.

RELNOTES: None.
PiperOrigin-RevId: 232535266
diff --git a/src/main/java/com/google/devtools/build/lib/packages/License.java b/src/main/java/com/google/devtools/build/lib/packages/License.java
index 579c29e..8c68447 100644
--- a/src/main/java/com/google/devtools/build/lib/packages/License.java
+++ b/src/main/java/com/google/devtools/build/lib/packages/License.java
@@ -30,6 +30,8 @@
 import com.google.devtools.build.lib.events.Location;
 import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec;
 import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec.VisibleForSerialization;
+import com.google.devtools.build.lib.skylarkbuildapi.LicenseApi;
+import com.google.devtools.build.lib.skylarkinterface.SkylarkPrinter;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.EnumSet;
@@ -41,7 +43,7 @@
 @Immutable
 @ThreadSafe
 @AutoCodec
-public final class License {
+public final class License implements LicenseApi {
   private final ImmutableSet<LicenseType> licenseTypes;
   private final ImmutableSet<Label> exceptions;
 
@@ -333,4 +335,9 @@
   public int hashCode() {
     return licenseTypes.hashCode() * 43 + exceptions.hashCode();
   }
+
+  @Override
+  public void repr(SkylarkPrinter printer) {
+    printer.append("<license object>");
+  }
 }
diff --git a/src/main/java/com/google/devtools/build/lib/packages/PackageFactory.java b/src/main/java/com/google/devtools/build/lib/packages/PackageFactory.java
index 43ed23a..945f42b 100644
--- a/src/main/java/com/google/devtools/build/lib/packages/PackageFactory.java
+++ b/src/main/java/com/google/devtools/build/lib/packages/PackageFactory.java
@@ -1002,13 +1002,14 @@
       return null;
     }
 
-    if (val instanceof SkylarkValue) {
-      return val;
+    if (val instanceof License) {
+      // License is deprecated as a Starlark type, so omit this type from Starlark values
+      // to avoid exposing these objects, even though they are technically SkylarkValue.
+      return null;
     }
 
-    if (val instanceof License) {
-      // TODO(bazel-team): convert License.getLicenseTypes() to a list of strings.
-      return null;
+    if (val instanceof SkylarkValue) {
+      return val;
     }
 
     if (val instanceof BuildType.SelectorList) {
diff --git a/src/main/java/com/google/devtools/build/lib/skylarkbuildapi/LicenseApi.java b/src/main/java/com/google/devtools/build/lib/skylarkbuildapi/LicenseApi.java
new file mode 100644
index 0000000..8c79a7b
--- /dev/null
+++ b/src/main/java/com/google/devtools/build/lib/skylarkbuildapi/LicenseApi.java
@@ -0,0 +1,29 @@
+// Copyright 2019 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.skylarkbuildapi;
+
+import com.google.devtools.build.lib.skylarkinterface.SkylarkModule;
+import com.google.devtools.build.lib.skylarkinterface.SkylarkModuleCategory;
+import com.google.devtools.build.lib.skylarkinterface.SkylarkValue;
+
+/** The interface for a license in Skylark. */
+@SkylarkModule(
+    name = "License",
+    category = SkylarkModuleCategory.BUILTIN,
+    documented = true,
+    doc =
+        "This API is deprecated and will be removed. Please do not depend on it. "
+            + "This object represents the value of a license attribute.")
+public interface LicenseApi extends SkylarkValue {}
diff --git a/src/test/java/com/google/devtools/build/lib/skylark/SkylarkIntegrationTest.java b/src/test/java/com/google/devtools/build/lib/skylark/SkylarkIntegrationTest.java
index 29decc0..6078e34 100644
--- a/src/test/java/com/google/devtools/build/lib/skylark/SkylarkIntegrationTest.java
+++ b/src/test/java/com/google/devtools/build/lib/skylark/SkylarkIntegrationTest.java
@@ -2727,6 +2727,28 @@
     assertContainsEvent("Unused function-based split transition whitelist");
   }
 
+  @Test
+  public void testLicenseType() throws Exception {
+    // Note that attr.license is deprecated, and thus this test is subject to imminent removal.
+    // (See --incompatible_no_attr_license). However, this verifies that until the attribute
+    // is removed, values of the attribute are a valid Starlark type.
+    scratch.file(
+        "test/rule.bzl",
+        "def _my_rule_impl(ctx): ",
+        "  print(ctx.attr.my_license)",
+        "  return []",
+        "my_rule = rule(",
+        "  implementation = _my_rule_impl,",
+        "  attrs = {",
+        "    'my_license':  attr.license(),",
+        "  })");
+    scratch.file("test/BUILD", "load(':rule.bzl', 'my_rule')", "my_rule(name = 'test')");
+
+    getConfiguredTarget("//test:test");
+
+    assertContainsEvent("<license object>");
+  }
+
   /**
    * Skylark integration test that forces inlining.
    */