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: Rules license support #683

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -548,3 +548,13 @@ rbe_autoconfig(name = "buildkite_config")
load("//migration:maven_jar_migrator_deps.bzl", "maven_jar_migrator_repositories")

maven_jar_migrator_repositories()

RULE_LICENSE_COMMIT = "683e47b13cb92cb3c2f484e35c6573a100f8fd25"

http_archive(
name = "rules_license",
sha256 = "78ffce40439a2af5b1d289a0a8f3b9aabc3857a527080164fb2e02b06b654130",
strip_prefix = "rules_license-%s" % RULE_LICENSE_COMMIT,
url = "https://github.com/bazelbuild/rules_license/archive/%s.zip" % RULE_LICENSE_COMMIT,
)

23 changes: 23 additions & 0 deletions coursier.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ _BUILD = """

load("@rules_jvm_external//private/rules:jvm_import.bzl", "jvm_import")
load("@rules_jvm_external//private/rules:jetifier.bzl", "jetify_aar_import", "jetify_jvm_import")
{rules_license_import_statement}
{aar_import_statement}

{imports}
Expand Down Expand Up @@ -157,6 +158,12 @@ def _get_aar_import_statement_or_empty_str(repository_ctx):
else:
return ""

def _get_rules_license_import_statement_or_empty_str(repository_ctx):
if repository_ctx.attr.license_json:
return "load(\"@rules_license//rules:license.bzl\", \"license\")"
else:
return ""

def _java_path(repository_ctx):
java_home = repository_ctx.os.environ.get("JAVA_HOME")
if java_home != None:
Expand Down Expand Up @@ -538,6 +545,7 @@ def _pinned_coursier_fetch_impl(repository_ctx):
repository_name = repository_ctx.name,
imports = generated_imports,
aar_import_statement = _get_aar_import_statement_or_empty_str(repository_ctx),
rules_license_import_statement = _get_rules_license_import_statement_or_empty_str(repository_ctx),
),
executable = False,
)
Expand Down Expand Up @@ -1001,6 +1009,18 @@ def _coursier_fetch_impl(repository_ctx):
"__RESOLVED_ARTIFACTS_HASH": _compute_dependency_tree_signature(dep_tree["dependencies"]),
"__INPUT_ARTIFACTS_HASH": compute_dependency_inputs_signature(repository_ctx.attr.artifacts),
})

license_info = {}
if repository_ctx.attr.license_json:
repository_ctx.symlink(
repository_ctx.path(repository_ctx.attr.license_json),
repository_ctx.path("imported_license_info.json"),
)
license_info = json.decode(
repository_ctx.read(
repository_ctx.path("imported_license_info.json"),
),
)

repository_ctx.report_progress("Generating BUILD targets..")
(generated_imports, jar_versionless_target_labels) = parser.generate_imports(
Expand All @@ -1021,6 +1041,7 @@ def _coursier_fetch_impl(repository_ctx):
if a.get("testonly", False)
},
override_targets = repository_ctx.attr.override_targets,
license_info = license_info,
)

# This repository rule can be either in the pinned or unpinned state, depending on when
Expand All @@ -1042,6 +1063,7 @@ def _coursier_fetch_impl(repository_ctx):
repository_name = repository_name,
imports = generated_imports,
aar_import_statement = _get_aar_import_statement_or_empty_str(repository_ctx),
rules_license_import_statement = _get_rules_license_import_statement_or_empty_str(repository_ctx),
),
executable = False,
)
Expand Down Expand Up @@ -1224,6 +1246,7 @@ coursier_fetch = repository_rule(
"none",
],
),
"license_json": attr.label(doc = "JSON representing rules_license's necessary metadata for applying licenses to imported artifacts"),
},
environ = [
"JAVA_HOME",
Expand Down
50 changes: 46 additions & 4 deletions private/dependency_tree_parser.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def _deduplicate_list(items):
# tree.
#
# Made function public for testing.
def _generate_imports(repository_ctx, dep_tree, explicit_artifacts, neverlink_artifacts, testonly_artifacts, override_targets):
def _generate_imports(repository_ctx, dep_tree, explicit_artifacts, neverlink_artifacts, testonly_artifacts, override_targets, license_info):
# The list of java_import/aar_import declaration strings to be joined at the end
all_imports = []

Expand Down Expand Up @@ -293,7 +293,34 @@ 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]))

# 9. Finish the java_import rule.
# 9. If `strict_visibility` is True in the artifact spec, define public
# visibility only for non-transitive dependencies.
#
# java_import(
# name = "org_hamcrest_hamcrest_library",
# jars = ["https/repo1.maven.org/maven2/org/hamcrest/hamcrest-library/1.3/hamcrest-library-1.3.jar"],
# srcjar = "https/repo1.maven.org/maven2/org/hamcrest/hamcrest-library/1.3/hamcrest-library-1.3-sources.jar",
# deps = [
# ":org_hamcrest_hamcrest_core",
# ],
# tags = [
# "maven_coordinates=org.hamcrest:hamcrest.library:1.3"],
# "maven_url=https://repo1.maven.org/maven/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar",
# ],
# neverlink = True,
# testonly = True,
# visibility = ["//visibility:public"],
# applicable_licenses = ["@myorg_compliance//licenses:license-org.hamcrest:hamcrest.library:1.3"]
target_import_string.append("\tapplicable_licenses = [")

if artifact["coord"] in license_info:
licenses = license_info[artifact["coord"]]
# target_import_labels.append("\t\t\"%s\",\n" % dep_target_label)
for license in licenses:
target_import_string.append("\t\t\":%s\"," % (license["name"]))
target_import_string.append("\t],")

# 10. Finish the java_import rule.
#
# java_import(
# name = "org_hamcrest_hamcrest_library",
Expand All @@ -313,7 +340,7 @@ def _generate_imports(repository_ctx, dep_tree, explicit_artifacts, neverlink_ar

all_imports.append("\n".join(target_import_string))

# 10. Create a versionless alias target
# 11. Create a versionless alias target
#
# alias(
# name = "org_hamcrest_hamcrest_library_1_3",
Expand All @@ -323,7 +350,21 @@ def _generate_imports(repository_ctx, dep_tree, explicit_artifacts, neverlink_ar
all_imports.append("alias(\n\tname = \"%s\",\n\tactual = \"%s\",\n%s)" %
(versioned_target_alias_label, target_label, alias_visibility))

# 11. If using maven_install.json, use a genrule to copy the file from the http_file
# 12. If there is a corresponding for the artifact, create a license target.
#
# license(
# name = "license-org.hamcrest:hamcrest.library:1.3",
# license_text = "LICENSE-org.hamcrest:hamcrest.library:1.3",
# package_name = "org.hamcrest:hamcrest.library:1.3",
# license_kinds = [
# "@rules_license//licenses/spdx:BSD-1-Clause",
# ]
# )
if artifact["coord"] in license_info:
for license in license_info[artifact["coord"]]:
all_imports.append("license(\n\tname = \"%s\",\n\tliense_text = \"%s\",\n\tpackage_name = \"%s\",\n\tlicense_kinds = %s\n)" %
(license["name"], license["license_text"], license["package_name"], license["license_kinds"]))
# 13. If using maven_install.json, use a genrule to copy the file from the http_file
# repository into this repository.
#
# genrule(
Expand All @@ -335,6 +376,7 @@ def _generate_imports(repository_ctx, dep_tree, explicit_artifacts, neverlink_ar
if repository_ctx.attr.maven_install_json:
all_imports.append(_genrule_copy_artifact_from_http_file(artifact, default_visibilities))


else: # artifact_path == None:
# Special case for certain artifacts that only come with a POM file.
# Such artifacts "aggregate" their dependencies, so they don't have
Expand Down
4 changes: 3 additions & 1 deletion private/rules/maven_install.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ def maven_install(
fail_if_repin_required = False,
use_starlark_android_rules = False,
aar_import_bzl_label = DEFAULT_AAR_IMPORT_LABEL,
duplicate_version_warning = "warn"):
duplicate_version_warning = "warn",
license_json = None):
"""Resolves and fetches artifacts transitively from Maven repositories.

This macro runs a repository rule that invokes the Coursier CLI to resolve
Expand Down Expand Up @@ -126,6 +127,7 @@ def maven_install(
use_starlark_android_rules = use_starlark_android_rules,
aar_import_bzl_label = aar_import_bzl_label,
duplicate_version_warning = duplicate_version_warning,
license_json = license_json,
)

if maven_install_json != None:
Expand Down