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

feat: modules (broken, checkpoint) #35

Draft
wants to merge 2 commits into
base: main
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
1 change: 1 addition & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
build --javacopt="--release 8"
common --noenable_workspace

# CI
common:ci --announce_rc
Expand Down
2 changes: 1 addition & 1 deletion .bazelversion
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5.4.1
8.0.0
50 changes: 50 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
module(
name = "rules_antlr",
version = "0.0.0",
compatibility_level = 1,
)

bazel_dep(name = "rules_java", version = "5.4.1")
bazel_dep(name = "stardoc", version = "0.5.0", repo_name = "io_bazel_stardoc")

http_jar = use_repo_rule("@bazel_tools//tools/build_defs/repo:http.bzl", "http_jar")
http_jar(
name = "junit",
sha256 = "59721f0805e223d84b90677887d9ff567dc534d7c502ca903c0c2b17f05c116a",
url = "https://repo1.maven.org/maven2/junit/junit/4.12/junit-4.12.jar",
)

http_jar(
name = "jimfs",
sha256 = "c4828e28d7c0a930af9387510b3bada7daa5c04d7c25a75c7b8b081f1c257ddd",
url = "https://repo1.maven.org/maven2/com/google/jimfs/jimfs/1.1/jimfs-1.1.jar",
)

http_jar(
name = "guava",
sha256 = "4a5aa70cc968a4d137e599ad37553e5cfeed2265e8c193476d7119036c536fe7",
url = "https://repo1.maven.org/maven2/com/google/guava/guava/27.1-jre/guava-27.1-jre.jar",
)

http_jar(
name = "javax_json",
sha256 = "0e1dec40a1ede965941251eda968aeee052cc4f50378bc316cc48e8159bdbeb4",
url = "https://repo1.maven.org/maven2/org/glassfish/javax.json/1.0.4/javax.json-1.0.4.jar",
)

http_jar(
name = "stringtemplate4",
sha256 = "28547dba48cfceb77b6efbfe069aebe9ed3324ae60dbd52093d13a1d636ed069",
url = "https://repo1.maven.org/maven2/org/antlr/ST4/4.3/ST4-4.3.jar",
)

antlr_extension = use_extension("@rules_antlr//antlr:extensions.bzl", "antlr_extension", dev_dependency = True)
antlr_extension.toolchain(
languages = [
],
versions = [
"2.7.7",
"3.5.2",
"4.8",
],
)
2 changes: 1 addition & 1 deletion antlr/antlr2.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ downloaded and unzipped the debugger over the top of the standard ANTLR
distribution, the code emanating from ANTLR with this option will not
compile.
"""),
"deps": attr.label_list(default = [Label("@antlr2//jar")], doc = "The dependencies to use. Defaults to the final ANTLR 2 release, but if you need to use a different version, you can specify the dependencies here."),
"deps": attr.label_list(default = [Label("@@antlr2//jar")], doc = "The dependencies to use. Defaults to the final ANTLR 2 release, but if you need to use a different version, you can specify the dependencies here."),
"diagnostic": attr.bool(default = False, doc = "Generate a text file from your grammar with a lot of debugging info."),
"docbook": attr.bool(default = False, doc = "Generate a docbook SGML file from your grammar without actions and so on. It only works for parsers, not lexers or tree parsers."),
"html": attr.bool(default = False, doc = "Generate a HTML file from your grammar without actions and so on. It only works for parsers, not lexers or tree parsers."),
Expand Down
4 changes: 2 additions & 2 deletions antlr/antlr3.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ antlr = rule(
"debug": attr.bool(default = False, doc = "Generate a parser that emits debugging events."),
"depend": attr.bool(default = False, doc = "Generate file dependencies; don't actually run antlr."),
"deps": attr.label_list(default = [
Label("@antlr3_runtime//jar"),
Label("@antlr3_tool//jar"),
Label("@@antlr3_runtime//jar"),
Label("@@antlr3_tool//jar"),
Label("@stringtemplate4//jar"),
], doc = """
The dependencies to use. Defaults to the most recent ANTLR 3 release,
Expand Down
6 changes: 3 additions & 3 deletions antlr/antlr4.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ Runs [ANTLR 4](https://www.antlr.org//) on a set of grammars.
"atn": attr.bool(default = False, doc = "Generate rule augmented transition network diagrams."),
"depend": attr.bool(default = False, doc = "Generate a list of file dependencies instead of parser and/or lexer."),
"deps": attr.label_list(default = [
Label("@antlr4_tool//jar"),
Label("@antlr4_runtime//jar"),
Label("@antlr3_runtime//jar"),
Label("@@antlr4_tool//jar"),
Label("@@antlr4_runtime//jar"),
Label("@@antlr3_runtime//jar"),
Label("@stringtemplate4//jar"),
Label("@javax_json//jar"),
], doc = """
Expand Down
24 changes: 24 additions & 0 deletions antlr/extensions.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
load("repositories.bzl", "rules_antlr_dependencies")

def _antlr_repositories_impl(module_ctx):
for mod in module_ctx.modules:
for toolchain in mod.tags.toolchain:
all_args = toolchain.versions + toolchain.languages
rules_antlr_dependencies(
*all_args,
)
return module_ctx.extension_metadata(
root_module_direct_deps="all",
root_module_direct_dev_deps=[],
reproducible=False)

antlr_extension = module_extension(
implementation = _antlr_repositories_impl,
tag_classes = {
"toolchain": tag_class(attrs = {
"languages": attr.string_list(doc = "A list of languages to support."),
"name": attr.string(doc = "The name of the repository used for the toolchains.", default = "antlr_repositories"),
"versions": attr.string_list(doc = "The antlr versions to support."),
}),
},
)
110 changes: 58 additions & 52 deletions antlr/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -178,17 +178,28 @@ def rules_antlr_dependencies(*versionsAndLanguages):
if not languages:
languages = [JAVA]

# Collect up deps, then download them once. This avoids an
# error where bazel reports that the dependency has already
# been downloaded.
all_deps = {}
for version in sorted(versions, key = _toString):
if version == 4 or version == "4.8":
_antlr48_dependencies(languages)
deps = _antlr48_dependencies(languages)
all_deps.update(deps)
elif version == "4.7.2":
_antlr472_dependencies(languages)
deps = _antlr472_dependencies(languages)
all_deps.update(deps)
elif version == "4.7.1":
_antlr471_dependencies(languages)
deps = _antlr471_dependencies(languages)
all_deps.update(deps)
elif version == 3 or version == "3.5.2":
_antlr352_dependencies(languages)
deps = _antlr352_dependencies(languages)
all_deps.update(deps)
elif version == 2 or version == "2.7.7":
_antlr277_dependencies(languages)
deps = _antlr277_dependencies(languages)
all_deps.update(deps)
print(all_deps)
_dependencies(all_deps)
else:
fail("Missing ANTLR version", attr = "versionsAndLanguages")

Expand Down Expand Up @@ -218,88 +229,85 @@ def rules_antlr_optimized_dependencies(version):
fail('Unsupported ANTLR version provided: "{0}". Currently supported are: {1}'.format(version, v4_opt), attr = "version")

def _antlr48_dependencies(languages):
_antlr4_dependencies(
_antlr4_runtime(
"4.8",
languages,
{
languages)

return {
"antlr4_runtime": "4.8",
"antlr4_tool": "4.8",
"antlr3_runtime": "3.5.2",
"stringtemplate4": "4.3",
"javax_json": "1.0.4",
},
)
}

def _antlr472_dependencies(languages):
_antlr4_dependencies(
_antlr4_runtime(
"4.7.2",
languages,
{
languages)
return {
"antlr4_runtime": "4.7.2",
"antlr4_tool": "4.7.2",
"antlr3_runtime": "3.5.2",
"stringtemplate4": "4.0.8",
"javax_json": "1.0.4",
},
)
}

def _antlr471_dependencies(languages):
_antlr4_dependencies(
_antlr4_runtime(
"4.7.1",
languages,
{
languages)
return {
"antlr4_runtime": "4.7.1",
"antlr4_tool": "4.7.1",
"antlr3_runtime": "3.5.2",
"stringtemplate4": "4.0.8",
"javax_json": "1.0.4",
},
)
}

def _antlr474_optimized_dependencies():
_dependencies({
return {
"antlr4_runtime": "4.7.4-opt",
"antlr4_tool": "4.7.4-opt",
"antlr3_runtime": "3.5.2",
"stringtemplate4": "4.0.8",
"javax_json": "1.0.4",
})
}

def _antlr473_optimized_dependencies():
_dependencies({
return {
"antlr4_runtime": "4.7.3-opt",
"antlr4_tool": "4.7.3-opt",
"antlr3_runtime": "3.5.2",
"stringtemplate4": "4.0.8",
"javax_json": "1.0.4",
})
}

def _antlr472_optimized_dependencies():
_dependencies({
return {
"antlr4_runtime": "4.7.2-opt",
"antlr4_tool": "4.7.2-opt",
"antlr3_runtime": "3.5.2",
"stringtemplate4": "4.0.8",
"javax_json": "1.0.4",
})
}

def _antlr471_optimized_dependencies():
_dependencies({
return {
"antlr4_runtime": "4.7.1-opt",
"antlr4_tool": "4.7.1-opt",
"antlr3_runtime": "3.5.2",
"stringtemplate4": "4.0.8",
"javax_json": "1.0.4",
})
}

def _antlr4_dependencies(version, languages, dependencies):
_dependencies(dependencies)
def _antlr4_runtime(version, languages):
archive = PACKAGES["antlr"][version]
build_script, workspace = _antlr4_build_script(languages)

if build_script:
http_archive(
name = "antlr4_runtimes",
name = "antlr4_runtime",
sha256 = archive["sha256"],
strip_prefix = archive["prefix"],
url = archive["url"],
Expand Down Expand Up @@ -392,24 +400,22 @@ def _load_rules_python_defs(script):
return "" if script.find('load("@rules_python//python:defs.bzl"') > -1 else 'load("@rules_python//python:defs.bzl", "py_library")'

def _antlr352_dependencies(languages):
_antlr3_dependencies(
_antlr3_runtime(
"3.5.2",
languages,
{
languages)
return {
"antlr3_runtime": "3.5.2",
"antlr3_tool": "3.5.2",
"stringtemplate4": "4.0.8",
},
)
}

def _antlr3_dependencies(version, languages, dependencies):
_dependencies(dependencies)
def _antlr3_runtime(version, languages):
archive = PACKAGES["antlr"][version]
build_script = _antlr3_build_script(languages)

if build_script:
http_archive(
name = "antlr3_runtimes",
name = "antlr3_runtime",
sha256 = archive["sha256"],
strip_prefix = archive["prefix"],
url = archive["url"],
Expand Down Expand Up @@ -452,22 +458,20 @@ py_library(
return script

def _antlr277_dependencies(languages):
_antlr2_dependencies(
_antlr2_runtime(
"2.7.7",
languages,
{
languages)
return {
"antlr2": "2.7.7",
},
)
}

def _antlr2_dependencies(version, languages, dependencies):
_dependencies(dependencies)
def _antlr2_runtime(version, languages):
archive = PACKAGES["antlr"][version]
build_script = _antlr2_build_script(languages)

if build_script:
http_archive(
name = "antlr2_runtimes",
name = "antlr2_runtime",
sha256 = archive["sha256"],
strip_prefix = "antlr-2.7.7",
url = archive["url"],
Expand Down Expand Up @@ -507,13 +511,15 @@ py_library(
def _dependencies(dependencies):
for key in dependencies:
version = dependencies[key]
_download(
name = key,
path = PACKAGES[key][version]["path"],
sha256 = PACKAGES[key][version]["sha256"],
)
if not key.endswith("_runtime"):
_download(
name = key,
path = PACKAGES[key][version]["path"],
sha256 = PACKAGES[key][version]["sha256"],
)

def _download(name, path, sha256):
print(name, path, sha256)
http_jar(
name = name,
url = path if path.startswith("https") else "https://repo1.maven.org/maven2/" + path,
Expand Down
3 changes: 2 additions & 1 deletion examples/.bazelrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
build --incompatible_use_platforms_repo_for_constraints
common --noenable_workspace
common --noincompatible_disallow_empty_glob

# CI
common:ci --announce_rc
Expand Down
2 changes: 1 addition & 1 deletion examples/.bazelversion
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5.4.1
8.0.1
Loading
Loading