From 332c34a594c4b266531d2fe9a56e43f813096b76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20D=C3=A9camps?= Date: Sat, 13 Oct 2018 03:04:59 +0200 Subject: [PATCH] Add a genrule for cup (#442) * Add a genrule for cup - Update readme for cup - Fix sample --- cup/BUILD | 9 +++++++++ cup/README.md | 11 +++++++++++ cup/cup.bzl | 24 ++++++++++++++++++++++++ cup/sample-project/BUILD | 6 ++++++ 4 files changed, 50 insertions(+) create mode 100644 cup/cup.bzl create mode 100644 cup/sample-project/BUILD diff --git a/cup/BUILD b/cup/BUILD index 98028b6bb..1708c5b21 100644 --- a/cup/BUILD +++ b/cup/BUILD @@ -2,6 +2,15 @@ package(default_visibility = ["//visibility:public"]) licenses(["notice"]) # GPL-compatible +java_binary( + name = "cup_bin", + main_class = "java_cup.Main", + runtime_deps = [ + ":cup", + ":cup_runtime", + ], +) + java_import( name = "cup", jars = ["cup/target/cup-11b.jar"], diff --git a/cup/README.md b/cup/README.md index 91fcb04d0..384a03a5a 100644 --- a/cup/README.md +++ b/cup/README.md @@ -11,4 +11,15 @@ Home directory and parent POM of 2 artefacts: Note that this code is excluded from many rules of our codebase, for instance google-java-format is not applied. + +## Bazel rule + + load("//cup:cup.bzl", "cup") + + cup( + name = "rule_name", + src = "spec.cup", + ) + + [cup]: http://www2.cs.tum.edu/projects/cup/ diff --git a/cup/cup.bzl b/cup/cup.bzl new file mode 100644 index 000000000..804f9c915 --- /dev/null +++ b/cup/cup.bzl @@ -0,0 +1,24 @@ +"""Bazel rules for cup. """ + +# CUP can only read from stdin, which Skylark rules don't support. Use a genrule for now. +def cup(name, src, parser = "Parser", symbols = "Symbols", interface = False): + """Generate a parser with CUP. + + Args: + name: name of the rule + srcs: list of cup specifications + parser: name of the generated parser class + symbols: name of the generated symbols class + interface: whether to generate an interface + """ + opts = "-parser {parser} -symbols {symbols}".format(parser = parser, symbols = symbols) + if interface: + opts = opts + " -interface" + cmd = ("$(location //cup:cup_bin) -destdir $(@D) " + opts + " < $<") + native.genrule( + name = name, + srcs = [src], + tools = ["//cup:cup_bin"], + outs = [parser + ".java", symbols + ".java"], + cmd = cmd, + ) diff --git a/cup/sample-project/BUILD b/cup/sample-project/BUILD new file mode 100644 index 000000000..adac8145b --- /dev/null +++ b/cup/sample-project/BUILD @@ -0,0 +1,6 @@ +load("//cup:cup.bzl", "cup") + +cup( + name = "gen_parser", + src = "src/main/cup/calculator.cup", +)