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
This commit is contained in:
Han-Wen Nienhuys
2016-09-21 11:14:04 +02:00
parent 924d93f9f9
commit eb16fe05a1
5 changed files with 58 additions and 0 deletions

7
Documentation/BUILD Normal file
View File

@@ -0,0 +1,7 @@
load("//tools/bzl:license.bzl", "license_map")
license_map(
name = "pgm-licenses",
target = "//gerrit-pgm:pgm",
)

View File

@@ -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(

View File

@@ -0,0 +1,2 @@
exports_files(["license-map.py"])

25
tools/bzl/license-map.py Normal file
View File

@@ -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

22
tools/bzl/license.bzl Normal file
View File

@@ -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"])