Skip to content

Commit

Permalink
Fixed dependency loading
Browse files Browse the repository at this point in the history
  • Loading branch information
Marco Hunsicker committed Apr 3, 2018
1 parent 91db939 commit 330daaf
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 75 deletions.
28 changes: 14 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@ Different rules are available that align with the ANTLR release streams.
## Quick Setup

Add the following to your [`WORKSPACE`](https://docs.bazel.build/versions/master/build-ref.html#workspace)
file to include the external repository, replacing the version number in the
[`tag`](https://docs.bazel.build/versions/master/be/workspace.html#git_repository.tag) attribute
with the version of the rules you wish to depend on and load the external dependencies necessary for
file to include the external repository and load the external dependencies necessary for
the [`antlr4`](#antlr4) rule:

```python
git_repository(
http_archive(
name = "rules_antlr",
remote = "https://github.com/marcohu/rules_antlr.git",
tag = "0.1.0",
sha256 = "66e1fcf1f8b5f2daa7c09268e5a10ab136834d73f0d0a94724100958ae560763",
strip_prefix = "rules_antlr-0.1.0",
urls = ["https://github.com/marcohu/rules_antlr/archive/0.1.0.tar.gz"],
)
load("@rules_antlr//antlr:deps.bzl", "antlr4_dependencies")
antlr4_dependencies()

load("@rules_antlr//antlr:deps.bzl", "antlr_dependencies")
antlr_dependencies()
```

More detailed instructions can be found in the
Expand Down Expand Up @@ -79,20 +79,20 @@ java_library(
Compiling the project generates the lexer/parser files:

```
$ bazel build //antlr4/HelloWorld
INFO: Analysed target //antlr4/HelloWorld:HelloWorld (0 packages loaded).
$ bazel build //HelloWorld
INFO: Analysed target //HelloWorld:HelloWorld (0 packages loaded).
INFO: Found 1 target...
Target //antlr4/HelloWorld:HelloWorld up-to-date:
bazel-bin/antlr4/HelloWorld/libHelloWorld.jar
Target //HelloWorld:HelloWorld up-to-date:
bazel-bin/HelloWorld/libHelloWorld.jar
INFO: Elapsed time: 0.940s, Critical Path: 0.76s
INFO: Build completed successfully, 4 total actions
```

The generated source files can be found in the `generated.srcjar` archive below your workspace `bazel-bin/antlr4/HelloWorld` directory.
The generated source files can be found in the `generated.srcjar` archive below your workspace `bazel-bin/HelloWorld` directory.

To just generate the source files you would use:

$ bazel build //antlr4/HelloWorld:generated
$ bazel build //HelloWorld:generated

Refer to the [examples](https://github.com/marcohu/rules_antlr/tree/master/examples)
directory for further samples.
Expand Down
101 changes: 59 additions & 42 deletions antlr/deps.bzl
Original file line number Diff line number Diff line change
@@ -1,21 +1,36 @@
"""Loads ANTLR dependencies."""

def antlr_dependencies(*versions):
skipAntlr3 = False
versions = sorted(versions)
for version in versions:
if (version == 4):
antlr4_dependencies(skipAntlr3)
elif (version == 3):
antlr3_dependencies()
skipAntlr3 = True
elif (version == 2):
antlr2_dependencies()
else:
fail("Invalid ANTLR version provided: {0}. Currently supported are 2, 3, 4".format(version))
""" Loads the dependencies for the specified ANTLR releases. """
if versions:
skipAntlr3 = False
versions = sorted(versions)
for version in versions:
if (version == 4):
_antlr4_dependencies(skipAntlr3)
elif (version == 3):
_antlr3_dependencies()
skipAntlr3 = True
elif (version == 2):
_antlr2_dependencies()
else:
fail("Invalid ANTLR version provided: {0}. Currently supported are 2, 3, 4".format(version))
else:
_antlr4_dependencies(False)

def antlr4_dependencies(skipAntlr3):
""" Loads the dependencies for the official ANTLR 4 release. """
def antlr_optimized_dependencies(*versions):
""" Loads the dependencies for the "optimized" fork of ANTLR 4 maintained by Sam Harwell. """
if versions:
versions = sorted(versions)
for version in versions:
if (version == 4):
_antlr4_optimized_dependencies()
else:
fail("Invalid ANTLR version provided: {0}. Currently supported is 4".format(version))
else:
_antlr4_optimized_dependencies()

def _antlr4_dependencies(skipAntlr3=False):
native.http_jar(
name = "antlr4_runtime",
url = "http://central.maven.org/maven2/org/antlr/antlr4-runtime/4.7.1/antlr4-runtime-4.7.1.jar",
Expand All @@ -26,24 +41,40 @@ def antlr4_dependencies(skipAntlr3):
url = "http://central.maven.org/maven2/org/antlr/antlr4/4.7.1/antlr4-4.7.1.jar",
sha256 = "a2cdc2f2f8eb893728832568dc54d080eb5a1495edb3b66e51b97122a60a0d87",
)
_antlr4_dependencies(skipAntlr3)
_antlr4_transitive_dependencies(skipAntlr3)

def antlr4_optimized_dependencies():
""" Loads the dependencies for the "optimized" fork of ANTLR 4 maintained by Sam Harwell. """
def _antlr4_optimized_dependencies():
native.http_jar(
name = "antlr4_runtime",
url = "http://central.maven.org/maven2/com/tunnelvisionlabs/antlr4-runtime/4.7/antlr4-runtime-4.7.jar",
sha256 = "",
sha256 = "729e327795535e62633d15b364a168f07f3239f9a692a98b154623cdec1807c8",
)
native.http_jar(
name = "antlr4_tool",
url = "http://central.maven.org/maven2/com/tunnelvisionlabs/antlr4/4.7/antlr4-4.7.jar",
sha256 = "",
sha256 = "e0133570df02e063b29733ccc1587965c89b0298b58909f6696de9d271671f2f",
)
_antlr4_dependencies()
_antlr4_transitive_dependencies(False)

def antlr3_dependencies():
""" Loads the dependencies for the official ANTLR 3 release. """
def _antlr4_transitive_dependencies(skipAntlr3):
if not skipAntlr3:
native.http_jar(
name = "antlr3_runtime",
url = "http://central.maven.org/maven2/org/antlr/antlr-runtime/3.5.2/antlr-runtime-3.5.2.jar",
sha256 = "ce3fc8ecb10f39e9a3cddcbb2ce350d272d9cd3d0b1e18e6fe73c3b9389c8734",
)
native.http_jar(
name = "stringtemplate4",
url = "http://central.maven.org/maven2/org/antlr/ST4/4.0.8/ST4-4.0.8.jar",
sha256 = "58caabc40c9f74b0b5993fd868e0f64a50c0759094e6a251aaafad98edfc7a3b",
)
native.http_jar(
name = "javax_json",
url = "http://central.maven.org/maven2/org/glassfish/javax.json/1.0.4/javax.json-1.0.4.jar",
sha256 = "0e1dec40a1ede965941251eda968aeee052cc4f50378bc316cc48e8159bdbeb4",
)

def _antlr3_dependencies():
native.http_jar(
name = "antlr3_runtime",
url = "http://central.maven.org/maven2/org/antlr/antlr-runtime/3.5.2/antlr-runtime-3.5.2.jar",
Expand All @@ -52,31 +83,17 @@ def antlr3_dependencies():
native.http_jar(
name = "antlr3_tool",
url = "http://central.maven.org/maven2/org/antlr/antlr/3.5.2/antlr-3.5.2.jar",
sha256 = "",
sha256 = "5ac36c2acfb0a0f3d37dafe20b5b570f2643e2d000c648d44503c2738be643df",
)

def antlr2_dependencies():
""" Loads the dependencies for the official ANTLR 2 release. """
native.http_jar(
name = "antlr2",
url = "http://central.maven.org/maven2/antlr/antlr/2.7.7/antlr-2.7.7.jar",
sha256 = "88fbda4b912596b9f56e8e12e580cc954bacfb51776ecfddd3e18fc1cf56dc4c",
)

def _antlr4_dependencies(skipAntlr3):
if not skipAntlr3:
native.http_jar(
name = "antlr3_runtime",
url = "http://central.maven.org/maven2/org/antlr/antlr-runtime/3.5.2/antlr-runtime-3.5.2.jar",
sha256 = "ce3fc8ecb10f39e9a3cddcbb2ce350d272d9cd3d0b1e18e6fe73c3b9389c8734",
)
native.http_jar(
name = "stringtemplate4",
url = "http://central.maven.org/maven2/org/antlr/ST4/4.0.8/ST4-4.0.8.jar",
sha256 = "58caabc40c9f74b0b5993fd868e0f64a50c0759094e6a251aaafad98edfc7a3b",
)

def _antlr2_dependencies():
native.http_jar(
name = "javax_json",
url = "http://central.maven.org/maven2/org/glassfish/javax.json/1.0.4/javax.json-1.0.4.jar",
sha256 = "0e1dec40a1ede965941251eda968aeee052cc4f50378bc316cc48e8159bdbeb4",
name = "antlr2",
url = "http://central.maven.org/maven2/antlr/antlr/2.7.7/antlr-2.7.7.jar",
sha256 = "88fbda4b912596b9f56e8e12e580cc954bacfb51776ecfddd3e18fc1cf56dc4c",
)
36 changes: 17 additions & 19 deletions docs/setup.md
Original file line number Diff line number Diff line change
@@ -1,44 +1,42 @@
# Setup

To use the ANTLR rules, add the following to your `WORKSPACE` file to include
the external repository, replacing the version number in the `tag` attribute
with the version of the rules you wish to depend on:
To use the ANTLR rules, add the following to your [`WORKSPACE`](https://docs.bazel.build/versions/master/build-ref.html#workspace) file to include
the external repository:

```python
git_repository(
http_archive(
name = "rules_antlr",
remote = "https://github.com/marcohu/rules_antlr.git",
tag = "0.1.0",
sha256 = "66e1fcf1f8b5f2daa7c09268e5a10ab136834d73f0d0a94724100958ae560763",
strip_prefix = "rules_antlr-0.1.0",
urls = ["https://github.com/marcohu/rules_antlr/archive/0.1.0.tar.gz"],
)
```

Then you can load the necessary external dependencies with the provided shortcuts in your `WORKSPACE` file:
Then you can load the necessary external dependencies in your [`WORKSPACE`](https://docs.bazel.build/versions/master/build-ref.html#workspace) file. For the current ANTLR release:

```python
load("@rules_antlr//antlr:deps.bzl", "antlr4_dependencies")
load("@rules_antlr//antlr:deps.bzl", "antlr_dependencies")

antlr4_dependencies()
antlr_dependencies()
```

If you need different versions at once, you can either use:
If you need a different version or want to make the version explicit, you can specify the version number:

```python
load("@rules_antlr//antlr:deps.bzl", "antlr2_dependencies", "antlr3_dependencies", "antlr4_dependencies")
load("@rules_antlr//antlr:deps.bzl", "antlr_dependencies")

antlr2_dependencies()
antlr3_dependencies()
antlr4_dependencies()
antlr_dependencies(4)
```

Or the shorter:
If you require several releases, you can specify several versions at once:

```python
load("@rules_antlr//antlr:deps.bzl", "antlr_dependencies")

antlr_dependencies(2, 3, 4)
```
But be careful when updating to a new ANTLR rules version as these dependencies
might change. Alternatively you can pull the necessary dependencies yourself to
But be careful when updating to a new ANTLR rules version as the bundled dependencies
might change with each release. Alternatively you can pull the necessary dependencies yourself to
avoid coupling. For ANTLR 4.7.1:

```python
Expand Down Expand Up @@ -78,7 +76,7 @@ invocation.
As a convenience there is also a shortcut for the ["optimized" fork](https://github.com/tunnelvisionlabs/antlr4) maintained by Sam Harwell:

```python
load("@rules_antlr//antlr:deps.bzl", "antlr4_optimized_dependencies")
load("@rules_antlr//antlr:deps.bzl", "antlr_optimized_dependencies")

antlr4_optimized_dependencies()
antlr_optimized_dependencies()
```

0 comments on commit 330daaf

Please sign in to comment.