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: License lookup - a better version #692

Draft
wants to merge 5 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ http_file(
urls = [COURSIER_CLI_GITHUB_ASSET_URL],
)

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",
)

# Begin Skylib dependencies

http_archive(
Expand Down
7 changes: 7 additions & 0 deletions license_classifier.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# lookup_license: Returns the license information for a maven it.
# The dependency parser calls this while generating the @maven/BUILD file.
# This is a no-op fallback, that will be overwritten by the patch command
# use_license_classifier.sh.

def lookup_license(url=None, sha256=None, maven_id=None):
return None
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; }
}
60 changes: 60 additions & 0 deletions license_demo/WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# 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.
http_archive(
name = "rules_jvm_external",
# path = "..",
urls = ["file:///tmp/rje.tar"],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How did you generate this .tar? I ended up just using git_repository() workspace rule pointing at this branch as a work-around

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tar from the command line. This is just to get something to demo, not what we would really do.

patch_cmds = [
"/bin/pwd >/tmp/toast.pwd",
"./use_license_classifier.sh my_compliance",
# "/usr/local/google/home/aiuto/ws/rules_jvm_external/license_demo/splice_lc.sh",
],
)

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("//: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
20 changes: 20 additions & 0 deletions use_license_classifier.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/bash -e
#
# Replace the no-op fallback license classifier with one implemented in
# a repository of our choosing


if [[ $# != 1 ]] ; then
echo 'usage: use_license_classifier.sh REPOSITORY_NAME'
exit 1
fi

cat >license_classifier.bzl <<INP
# This file is generated by the patch command "use_license_classifier.sh".
# It overwrites the default license_classifier.bzl

load("@${1}//:license_classifier.bzl", _ll = "lookup_license")

def lookup_license(url=None, sha256=None, maven_id=None):
return _ll(url=url, sha256=sha256, maven_id=maven_id)
INP