From eb16fe05a1660dab637c14aeef7366b6e31ac08a Mon Sep 17 00:00:00 2001 From: Han-Wen Nienhuys Date: Wed, 21 Sep 2016 11:14:04 +0200 Subject: [PATCH] bazel: generate license list for documentation. Example: $ bazel build //Documentation:pgm-licenses.txt .. $ cat bazel-genfiles/Documentation/pgm-licenses.txt //lib:jsch //lib:LICENSE-jsch //lib:servlet-api-3_1 //lib:LICENSE-Apache2.0 Still missing: * check that there are dependencies on forbidden licenses * check that there are no external dependencies except through the //lib directory. Change-Id: I8f0ed94de20f68e0dbc1fe87dfd56ce11e02fab7 --- Documentation/BUILD | 7 +++++++ lib/BUILD | 2 ++ tools/bzl/BUILD | 2 ++ tools/bzl/license-map.py | 25 +++++++++++++++++++++++++ tools/bzl/license.bzl | 22 ++++++++++++++++++++++ 5 files changed, 58 insertions(+) create mode 100644 Documentation/BUILD create mode 100644 tools/bzl/license-map.py create mode 100644 tools/bzl/license.bzl diff --git a/Documentation/BUILD b/Documentation/BUILD new file mode 100644 index 0000000000..98b3ce45ac --- /dev/null +++ b/Documentation/BUILD @@ -0,0 +1,7 @@ + +load("//tools/bzl:license.bzl", "license_map") + +license_map( + name = "pgm-licenses", + target = "//gerrit-pgm:pgm", +) diff --git a/lib/BUILD b/lib/BUILD index 44c293d5d7..19abf4f001 100644 --- a/lib/BUILD +++ b/lib/BUILD @@ -1,6 +1,7 @@ java_library( name = 'servlet-api-3_1', neverlink = 1, + data = [ ":LICENSE-Apache2.0" ], exports = ['@servlet_api_3_1//jar'], visibility = ['//visibility:public'], ) @@ -75,6 +76,7 @@ java_library( name = 'jsch', exports = ['@jsch//jar'], visibility = ['//visibility:public'], + data = [ ":LICENSE-jsch" ], ) java_library( diff --git a/tools/bzl/BUILD b/tools/bzl/BUILD index e69de29bb2..90c91c3933 100644 --- a/tools/bzl/BUILD +++ b/tools/bzl/BUILD @@ -0,0 +1,2 @@ + +exports_files(["license-map.py"]) diff --git a/tools/bzl/license-map.py b/tools/bzl/license-map.py new file mode 100644 index 0000000000..72c7ae8277 --- /dev/null +++ b/tools/bzl/license-map.py @@ -0,0 +1,25 @@ +#!/usr/bin/env python + +# reads a bazel query XML file, to join target names with their licenses. + +import sys +import xml.etree.ElementTree as ET + +tree = ET.parse(sys.argv[1]) +root = tree.getroot() + +entries = {} + +for child in root: + rule_name = child.attrib["name"] + for c in child.getchildren(): + if c.tag != "rule-input": + continue + + license_name = c.attrib["name"] + if "//lib:LICENSE" in license_name: + assert rule_name not in entries, (license_name, entries[rule_name]) + entries[rule_name] = license_name + +for k, v in sorted(entries.items()): + print k, v diff --git a/tools/bzl/license.bzl b/tools/bzl/license.bzl new file mode 100644 index 0000000000..a9a144410b --- /dev/null +++ b/tools/bzl/license.bzl @@ -0,0 +1,22 @@ + +def license_map(name, target): + """Generate XML for all targets that depend directly on a LICENSE file""" + native.genquery( + name = name + ".xml", + scope = [ target, ], + + # Find everything that depends on a license file, but remove + # the license files themselves from this list. + expression = 'rdeps(%s, filter("//lib:LICENSE.*", deps(%s)),1) - filter("//lib:LICENSE.*", deps(%s))' % (target, target, target), + + # We are interested in the edges of the graph ({java_library, + # license-file} tuples). 'query' provides this in the XML output. + opts = [ "--output=xml"], + ) + + # post process the XML into our favorite format. + native.genrule( + name = "gen_license_txt_" + name, + cmd = "python $(location //tools/bzl:license-map.py) $(location :%s.xml) > $@" % name, + outs = [ name + ".txt",], + tools = [ "//tools/bzl:license-map.py", name + ".xml"])