Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: proof of concept for license splicing #685

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ kt_register_toolchains()
# dependencies. So, we omit them to keep the WORKSPACE file simpler.
# https://skydoc.bazel.build/docs/getting_started_stardoc.html

load("//:set_license_classifier.bzl", "use_default_license_classifier")

use_default_license_classifier()

load("//:repositories.bzl", "rules_jvm_external_deps")

rules_jvm_external_deps()
Expand Down
4 changes: 4 additions & 0 deletions examples/android_instrumentation_test/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ local_repository(
path = "../../",
)

load("//:set_license_classifier.bzl", "use_default_license_classifier")

use_default_license_classifier()

load("@rules_jvm_external//:defs.bzl", "maven_install")

maven_install(
Expand Down
4 changes: 4 additions & 0 deletions examples/android_kotlin_app/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ local_repository(
path = "../../",
)

load("//:set_license_classifier.bzl", "use_default_license_classifier")

use_default_license_classifier()

load("@rules_jvm_external//:defs.bzl", "maven_install")

maven_install(
Expand Down
4 changes: 4 additions & 0 deletions examples/android_local_test/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ local_repository(
path = "../../",
)

load("//:set_license_classifier.bzl", "use_default_license_classifier")

use_default_license_classifier()

load("@rules_jvm_external//:defs.bzl", "maven_install")

maven_install(
Expand Down
4 changes: 4 additions & 0 deletions examples/java-export/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ local_repository(
path = "../../",
)

load("//:set_license_classifier.bzl", "use_default_license_classifier")

use_default_license_classifier()

load("@rules_jvm_external//:repositories.bzl", "rules_jvm_external_deps")

rules_jvm_external_deps()
Expand Down
4 changes: 4 additions & 0 deletions examples/kt_jvm_export/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ local_repository(
path = "../../",
)

load("//:set_license_classifier.bzl", "use_default_license_classifier")

use_default_license_classifier()

load("@rules_jvm_external//:repositories.bzl", "rules_jvm_external_deps")

rules_jvm_external_deps()
Expand Down
4 changes: 4 additions & 0 deletions examples/pom_file_generation/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ local_repository(
path = "../../",
)

load("//:set_license_classifier.bzl", "use_default_license_classifier")

use_default_license_classifier()

load("@rules_jvm_external//:defs.bzl", "maven_install")

maven_install(
Expand Down
4 changes: 4 additions & 0 deletions examples/protobuf-java/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ local_repository(
path = "../..",
)

load("//:set_license_classifier.bzl", "use_default_license_classifier")

use_default_license_classifier()

load("@rules_jvm_external//:defs.bzl", "maven_install")

maven_install(
Expand Down
4 changes: 4 additions & 0 deletions examples/scala_akka/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ local_repository(
path = "../../",
)

load("//:set_license_classifier.bzl", "use_default_license_classifier")

use_default_license_classifier()

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
Expand Down
4 changes: 4 additions & 0 deletions examples/simple/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ local_repository(
path = "../../",
)

load("//:set_license_classifier.bzl", "use_default_license_classifier")

use_default_license_classifier()

load("@rules_jvm_external//:defs.bzl", "maven_install")

maven_install(
Expand Down
4 changes: 4 additions & 0 deletions examples/spring_boot/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ local_repository(
path = "../../",
)

load("//:set_license_classifier.bzl", "use_default_license_classifier")

use_default_license_classifier()

load("@rules_jvm_external//:defs.bzl", "maven_install")

maven_install(
Expand Down
9 changes: 9 additions & 0 deletions license_demo/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@


java_library(
name = "my_lib",
srcs = ["MyLib.java"],
deps = [
"@maven//:junit_junit",
],
)
10 changes: 10 additions & 0 deletions license_demo/MyLib.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
*
*/
final class MyLib {

public MyLib() {
}

int foo() { return 1; }
}
62 changes: 62 additions & 0 deletions license_demo/WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Copyright 2022 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.

workspace(name = "demo")

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
name = "rules_license",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/rules_license/releases/download/0.0.1/rules_license-0.0.1.tar.gz",
],
sha256 = "4865059254da674e3d18ab242e21c17f7e3e8c6b1f1421fffa4c5070f82e98b5",
)

# You must expose your own license declarations as a repository so the rules
# generated in @maven can point to them. Otherwise you would have to generate
# the declarations in @maven.
local_repository(
name = "my_compliance",
path = "compliance",
)


# Point to parent for testing. You would use http_archive in real life.
local_repository(
name = "rules_jvm_external",
path = "..",
)


load("@rules_jvm_external//:set_license_classifier.bzl", "set_license_classifier")
set_license_classifier(path="compliance")

# You should never have to call this. rules_jvm_external_setup does it for you.
#load("@rules_jvm_external//:set_license_classifier.bzl", "use_default_license_classifier")
#use_default_license_classifier()

load("@rules_jvm_external//:defs.bzl", "maven_install")

maven_install(
artifacts = [
"junit:junit:4.12",
"com.google.guava:guava:28.0-jre",
],
fetch_sources = True,
repositories = [
"http://uk.maven.org/maven2",
"https://jcenter.bintray.com/",
],
)
16 changes: 16 additions & 0 deletions license_demo/compliance/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Organization local compliance rules go here

load("@rules_license//rules:license.bzl", "license")

package(
default_applicable_licenses = [":license"],
default_visibility = ["//visibility:public"],
)

license(
name = "apache",
license_kinds = [
"@rules_license//licenses/spdx:Apache-2.0"
],
license_text = "LICENSE_apache.txt",
)
1 change: 1 addition & 0 deletions license_demo/compliance/LICENSE_apache.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
apache license
Empty file.
9 changes: 9 additions & 0 deletions license_demo/compliance/license_classifier.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"""Users will provider their own.

This is a respository rule.
"""


def lookup_license(url=None, sha256=None, maven_id=None):
print("==================================== yes")
return "@my_compliance//:apache"
7 changes: 7 additions & 0 deletions private/dependency_tree_parser.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ This file contains parsing functions to turn a JSON-like dependency tree
into target declarations (jvm_import) for the final @maven//:BUILD file.
"""

load("@rules_jvm_license_classifier//:license_classifier.bzl", "lookup_license")
load("//private:coursier_utilities.bzl", "escape", "get_classifier", "get_packaging", "strip_packaging_and_classifier", "strip_packaging_and_classifier_and_version")

JETIFY_INCLUDE_LIST_JETIFY_ALL = ["*"]
Expand Down Expand Up @@ -293,6 +294,12 @@ def _generate_imports(repository_ctx, dep_tree, explicit_artifacts, neverlink_ar
target_import_string.append("\tvisibility = [%s]," % (",".join(["\"%s\"" % v for v in default_visibilities])))
alias_visibility = "\tvisibility = [%s],\n" % (",".join(["\"%s\"" % v for v in default_visibilities]))


# Splice in the license
lic = lookup_license(maven_id = simple_coord)
if lic:
target_import_string.append("\tapplicable_licenses = [\"%s\"]," % lic)

# 9. Finish the java_import rule.
#
# java_import(
Expand Down
1 change: 0 additions & 1 deletion repositories.bzl
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
load("//:defs.bzl", "maven_install")
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

_DEFAULT_REPOSITORIES = [
"https://repo1.maven.org/maven2",
Expand Down
52 changes: 52 additions & 0 deletions set_license_classifier.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Copyright 2022 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.

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

def _ensure_rules_license():
if not native.existing_rule("rules_license"):
http_archive(
name = "rules_license",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/rules_license/releases/download/0.0.1/rules_license-0.0.1.tar.gz",
],
sha256 = "4865059254da674e3d18ab242e21c17f7e3e8c6b1f1421fffa4c5070f82e98b5",
)


def set_default_license_classifier_impl(rctx):
rctx.file("license_classifier.bzl", content = """
def lookup_license(url=None, sha256=None, maven_id=None):
return None
""")
rctx.file("BUILD", content = "")

_set_default_license_classifier = repository_rule(
implementation=set_default_license_classifier_impl,
)

def use_default_license_classifier():
_ensure_rules_license()
if not native.existing_rule("rules_jvm_license_classifier"):
_set_default_license_classifier(
name = "rules_jvm_license_classifier",
)

def set_license_classifier(path):
_ensure_rules_license()
if native.existing_rule("rules_jvm_license_classifier"):
fail("You are trying to set the rules_jvm_external license classifier a second time.")
native.local_repository(
name = "rules_jvm_license_classifier",
path = path)