2015-10-29 07:03:49 +08:00
# Building ANTLR
Most programmers do not need the information on this page because they will simply download the appropriate jar(s) or use ANTLR through maven (via ANTLR's antlr4-maven-plugin). If you would like to fork the project and fix bugs or tweak the runtime code generation, then you will almost certainly need to build ANTLR itself. There are two components:
1. the tool that compiles grammars down into parsers and lexers in one of the target languages
1. the runtime used by those generated parsers and lexers.
I will assume that the root directory is `/tmp` for the purposes of explaining how to build ANTLR in this document.
2016-11-22 02:51:07 +08:00
*As of 4.6, ANTLR tool and Java-target runtime requires Java 7.*
2015-10-29 07:03:49 +08:00
# Get the source
The first step is to get the Java source code from the ANTLR 4 repository at github. You can download the repository from github, but the easiest thing to do is simply clone the repository on your local disk:
```bash
$ cd /tmp
2016-09-24 04:19:44 +08:00
/tmp $ git clone https://github.com/antlr/antlr4.git
2015-10-29 07:03:49 +08:00
Cloning into 'antlr4'...
2016-09-24 04:19:44 +08:00
remote: Counting objects: 61480, done.
remote: Total 61480 (delta 0), reused 0 (delta 0), pack-reused 61480
Receiving objects: 100% (61480/61480), 31.24 MiB | 7.18 MiB/s, done.
Resolving deltas: 100% (32970/32970), done.
2015-10-29 07:03:49 +08:00
Checking connectivity... done.
2016-09-24 04:19:44 +08:00
Checking out files: 100% (1427/1427), done.
2015-10-29 07:03:49 +08:00
```
# Compile
```bash
2018-11-09 05:35:56 +08:00
$ cd /tmp/antlr4
2016-12-15 02:28:14 +08:00
$ export MAVEN_OPTS="-Xmx1G" # don't forget this on linux
2017-03-10 01:54:06 +08:00
$ mvn clean # must be separate, not part of install/compile
2016-11-22 03:44:16 +08:00
$ mvn -DskipTests install
...
2016-11-17 07:42:45 +08:00
[INFO] ------------------------------------------------------------------------
2015-10-29 07:03:49 +08:00
[INFO] Reactor Summary:
[INFO]
2016-11-22 03:44:16 +08:00
[INFO] ANTLR 4 ............................................ SUCCESS [ 0.287 s]
[INFO] ANTLR 4 Runtime .................................... SUCCESS [ 4.915 s]
[INFO] ANTLR 4 Tool ....................................... SUCCESS [ 1.315 s]
[INFO] ANTLR 4 Maven plugin ............................... SUCCESS [ 2.393 s]
[INFO] ANTLR 4 Runtime Test Annotations ................... SUCCESS [ 0.078 s]
[INFO] ANTLR 4 Runtime Test Processors .................... SUCCESS [ 0.019 s]
[INFO] ANTLR 4 Runtime Tests (2nd generation) ............. SUCCESS [ 1.986 s]
[INFO] ANTLR 4 Tool Tests ................................. SUCCESS [ 0.513 s]
2015-10-29 07:03:49 +08:00
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
2016-11-17 07:42:45 +08:00
[INFO] ------------------------------------------------------------------------
2016-11-22 03:44:16 +08:00
[INFO] Total time: 12.005 s
[INFO] Finished at: 2016-11-21T11:42:42-08:00
[INFO] Final Memory: 52M/434M
2016-11-17 07:42:45 +08:00
[INFO] ------------------------------------------------------------------------
2015-10-29 07:03:49 +08:00
```
2017-03-10 01:54:06 +08:00
**NOTE:** We do `install` not `compile` as tool tests and such refer to modules that must be pulled from the maven install local cache.
2016-11-22 03:44:16 +08:00
2016-11-22 02:51:07 +08:00
# Installing libs to mvn cache locally
2015-10-29 07:03:49 +08:00
2016-11-22 02:51:07 +08:00
To skip the tests (which require all the target languages be installed) and **install into local repository** `~/.m2/repository/org/antlr` , do this:
2015-10-29 07:03:49 +08:00
```bash
2016-12-15 02:28:14 +08:00
$ export MAVEN_OPTS="-Xmx1G" # don't forget this on linux
2016-11-22 02:51:07 +08:00
$ mvn install -DskipTests=true # make sure all artifacts are visible on this machine
2015-10-29 07:03:49 +08:00
```
2016-11-17 07:42:45 +08:00
You should see these jars (when building 4.6-SNAPSHOT):
2015-10-29 07:03:49 +08:00
```bash
/Users/parrt/.m2/repository/org/antlr $ find antlr4* -name '*.jar'
2016-11-17 07:42:45 +08:00
antlr4-maven-plugin/4.6-SNAPSHOT/antlr4-maven-plugin-4.6-SNAPSHOT.jar
antlr4-runtime-test-annotation-processors/4.6-SNAPSHOT/antlr4-runtime-test-annotation-processors-4.6-SNAPSHOT.jar
antlr4-runtime-test-annotations/4.6-SNAPSHOT/antlr4-runtime-test-annotations-4.6-SNAPSHOT.jar
antlr4-runtime-testsuite/4.6-SNAPSHOT/antlr4-runtime-testsuite-4.6-SNAPSHOT-tests.jar
antlr4-runtime-testsuite/4.6-SNAPSHOT/antlr4-runtime-testsuite-4.6-SNAPSHOT.jar
antlr4-runtime/4.6-SNAPSHOT/antlr4-runtime-4.6-SNAPSHOT.jar
antlr4-tool-testsuite/4.6-SNAPSHOT/antlr4-tool-testsuite-4.6-SNAPSHOT.jar
antlr4/4.6-SNAPSHOT/antlr4-4.6-SNAPSHOT-tests.jar
antlr4/4.6-SNAPSHOT/antlr4-4.6-SNAPSHOT.jar
```
Note that ANTLR is written in itself, which is why maven downloads antlr4-4.5.jar for boostrapping 4.6-SNAPSHOT purposes.
2016-12-15 03:51:28 +08:00
# Testing tool and targets
2016-11-17 07:42:45 +08:00
2016-12-15 03:51:28 +08:00
See [ANTLR project unit tests ](antlr-project-testing.md ).
2016-11-17 07:42:45 +08:00
2015-10-29 07:03:49 +08:00
2016-12-15 03:51:28 +08:00
# Building without testing
2016-11-17 07:42:45 +08:00
2016-10-02 22:51:57 +08:00
To build without running the tests (saves a lot of time), do this:
2015-10-29 07:03:49 +08:00
```bash
2016-12-15 02:28:14 +08:00
$ mvn -DskipTests install
2015-10-29 07:03:49 +08:00
```
## Building ANTLR in Intellij IDE
After download ANTLR source, just "import project from existing sources" and click on the "Maven Projects" tab in right gutter of IDE. It should build stuff in the background automatically and look like:
2016-06-13 19:12:02 +08:00
< img src = images/intellij-maven.png width = 200 >