aboutsummaryrefslogtreecommitdiff
path: root/examples/vendor/constant_gen
diff options
context:
space:
mode:
Diffstat (limited to 'examples/vendor/constant_gen')
-rw-r--r--examples/vendor/constant_gen/BUILD69
-rw-r--r--examples/vendor/constant_gen/LICENSE1
-rw-r--r--examples/vendor/constant_gen/LICENSE.on_output1
-rw-r--r--examples/vendor/constant_gen/constant_generator.py33
-rw-r--r--examples/vendor/constant_gen/defs.bzl59
-rw-r--r--examples/vendor/constant_gen/generated_code_licenses.golden15
-rw-r--r--examples/vendor/constant_gen/generator_licenses.golden15
7 files changed, 193 insertions, 0 deletions
diff --git a/examples/vendor/constant_gen/BUILD b/examples/vendor/constant_gen/BUILD
new file mode 100644
index 0000000..e837fd1
--- /dev/null
+++ b/examples/vendor/constant_gen/BUILD
@@ -0,0 +1,69 @@
+# An example of a code generator with a distinct license for the generated code.
+
+load(":defs.bzl", "constant_gen")
+load("@rules_license//rules:compliance.bzl", "licenses_used")
+load("@rules_license//rules:license.bzl", "license")
+load("@rules_license//tools:test_helpers.bzl", "golden_test")
+
+package(
+ default_applicable_licenses = [":license"],
+ default_visibility = ["//visibility:public"],
+)
+
+# The default license for an entire package is typically named "license".
+license(
+ name = "license",
+ package_name = "Trivial Code Generator",
+ license_kinds = [
+ "@rules_license//examples/my_org/licenses:generic_restricted",
+ ],
+ license_text = "LICENSE",
+)
+
+license(
+ name = "license_for_emitted_code",
+ package_name = "Trivial Code Generator Output",
+ license = "LICENSE.on_output",
+ license_kinds = [
+ "@rules_license//examples/my_org/licenses:unencumbered",
+ ],
+)
+
+# The generator itself will be licensed under :license
+py_binary(
+ name = "constant_generator",
+ srcs = ["constant_generator.py"],
+ python_version = "PY3",
+)
+
+# Sample: This target will be licensed under :license_for_emitted_code
+constant_gen(
+ name = "libhello",
+ text = "Hello, world.",
+ var = "hello_world",
+)
+
+# Verify the licenses are what we expect
+licenses_used(
+ name = "generator_licenses",
+ out = "generator_licenses.json",
+ deps = [":constant_generator"],
+)
+
+golden_test(
+ name = "verify_generator_licenses",
+ golden = "generator_licenses.golden",
+ subject = ":generator_licenses.json",
+)
+
+licenses_used(
+ name = "generated_code_licenses",
+ # Note: using default output file name
+ deps = [":libhello"],
+)
+
+golden_test(
+ name = "verify_generated_code_licenses",
+ golden = "generated_code_licenses.golden",
+ subject = ":generated_code_licenses.json",
+)
diff --git a/examples/vendor/constant_gen/LICENSE b/examples/vendor/constant_gen/LICENSE
new file mode 100644
index 0000000..861da0d
--- /dev/null
+++ b/examples/vendor/constant_gen/LICENSE
@@ -0,0 +1 @@
+This code is provided under a license which contains some restrictions.
diff --git a/examples/vendor/constant_gen/LICENSE.on_output b/examples/vendor/constant_gen/LICENSE.on_output
new file mode 100644
index 0000000..b699638
--- /dev/null
+++ b/examples/vendor/constant_gen/LICENSE.on_output
@@ -0,0 +1 @@
+The generated output from constant_gen has no license encumberances.
diff --git a/examples/vendor/constant_gen/constant_generator.py b/examples/vendor/constant_gen/constant_generator.py
new file mode 100644
index 0000000..6bd92b2
--- /dev/null
+++ b/examples/vendor/constant_gen/constant_generator.py
@@ -0,0 +1,33 @@
+# Copyright 2020 Google LLC
+#
+# 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
+#
+# https://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.
+
+# Lint as: python3
+"""A trivial tool to turn a string into a C++ constant.
+
+This is not meant to be useful. It is only to provide an example of a tool that
+generates code.
+"""
+
+import sys
+
+
+def main(argv):
+ if len(argv) != 4:
+ raise Exception('usage: constant_generator out_file var_name text')
+ with open(argv[1], 'w') as out:
+ out.write('const char* %s = "%s";\n' % (argv[2], argv[3]))
+
+
+if __name__ == '__main__':
+ main(sys.argv)
diff --git a/examples/vendor/constant_gen/defs.bzl b/examples/vendor/constant_gen/defs.bzl
new file mode 100644
index 0000000..f979664
--- /dev/null
+++ b/examples/vendor/constant_gen/defs.bzl
@@ -0,0 +1,59 @@
+"""A trivial rule to turn a string into a C++ constant."""
+
+# Copyright 2020 Google LLC
+#
+# 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
+#
+# https://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.
+
+def _constant_gen_impl(ctx):
+ # Turn text into a C++ constant.
+ outputs = [ctx.outputs.src_out]
+ ctx.actions.run(
+ mnemonic = "GenerateConstant",
+ progress_message = "Generating %s" % ctx.attr.var,
+ outputs = outputs,
+ executable = ctx.executable._generator,
+ arguments = [ctx.outputs.src_out.path, ctx.attr.var, ctx.attr.text],
+ )
+ return [DefaultInfo(files = depset(outputs))]
+
+_constant_gen = rule(
+ implementation = _constant_gen_impl,
+ attrs = {
+ "src_out": attr.output(mandatory = True),
+ "text": attr.string(mandatory = True),
+ "var": attr.string(mandatory = False),
+ "_generator": attr.label(
+ default = Label("@rules_license//examples/vendor/constant_gen:constant_generator"),
+ executable = True,
+ allow_files = True,
+ cfg = "host",
+ ),
+ },
+)
+
+def constant_gen(name, text, var):
+ # Generate the code
+ _constant_gen(
+ name = name + "_src_",
+ src_out = name + "_src_.cc",
+ text = text,
+ var = var,
+ applicable_licenses = ["@rules_license//examples/vendor/constant_gen:license_for_emitted_code"],
+ )
+
+ # And turn it into a library we can link against
+ native.cc_library(
+ name = name,
+ srcs = [name + "_src_"],
+ applicable_licenses = ["@rules_license//examples/vendor/constant_gen:license_for_emitted_code"],
+ )
diff --git a/examples/vendor/constant_gen/generated_code_licenses.golden b/examples/vendor/constant_gen/generated_code_licenses.golden
new file mode 100644
index 0000000..a37723a
--- /dev/null
+++ b/examples/vendor/constant_gen/generated_code_licenses.golden
@@ -0,0 +1,15 @@
+[
+ {
+ "rule": "//examples/vendor/constant_gen:license_for_emitted_code",
+ "license_kinds": [
+ {
+ "target": "@//examples/my_org/licenses:unencumbered",
+ "name": "unencumbered",
+ "conditions": []
+ }
+ ],
+ "copyright_notice": "",
+ "package_name": "Trivial Code Generator Output",
+ "license_text": "examples/vendor/constant_gen/LICENSE"
+ }
+]
diff --git a/examples/vendor/constant_gen/generator_licenses.golden b/examples/vendor/constant_gen/generator_licenses.golden
new file mode 100644
index 0000000..164b00b
--- /dev/null
+++ b/examples/vendor/constant_gen/generator_licenses.golden
@@ -0,0 +1,15 @@
+[
+ {
+ "rule": "//examples/vendor/constant_gen:license",
+ "license_kinds": [
+ {
+ "target": "@//examples/my_org/licenses:generic_restricted",
+ "name": "generic_restricted",
+ "conditions": ["restricted"]
+ }
+ ],
+ "copyright_notice": "",
+ "package_name": "Trivial Code Generator",
+ "license_text": "examples/vendor/constant_gen/LICENSE"
+ }
+]