Skip to content

Building ANTLR 4

aborkar-ibm edited this page Oct 31, 2022 · 42 revisions

Building ANTLR v4.11.1

Below versions of ANTLR (ANother Tool for Language Recognition) version 4 are available in respective distributions at the time of creation of these build instructions:

  • Ubuntu 18.04 has ANTLR 4.7.2-1
  • Ubuntu 20.04 has ANTLR 4.7.2-2
  • Ubuntu 22.04 has ANTLR 4.7.2-5

If only the Java runtime is required, then the pre-built jars for ANTLR v4.11.1 can be downloaded from here.

The instructions provided below specify the steps to build ANTLR v4.11.1 on Linux on IBM Z for the following distributions:

  • RHEL (7.8, 7.9, 8.4, 8.6, 9.0)
  • Ubuntu (18.04, 20.04, 22.04)
  • SLES (12 SP5, 15 SP3, 15 SP4)

The instructions below describe:

  1. How to download and install the required Java SDK.
  2. If only the ANTLR Java runtime is required - how to download and install ANTLR pre-built binaries
  3. If other ANTLR runtimes are required - how to download and install the required packages, build ANTLR and the supported runtimes from source.

General Notes:

  • When following the steps below please use a standard permission user unless otherwise specified.
  • A directory /<source_root>/ will be referred to in these instructions, this is a temporary writable directory anywhere you'd like to place it.

Step 1: Install Java SDK

1.1) Download and install Java

  • RHEL (7.8, 7.9, 8.4, 8.6, 9.0)

    • With IBM Semeru Runtime (previously known as AdoptOpenJDK openj9)

      • Download and install IBM Semeru Runtime (Java 11) from here.
    • With Eclipse Adoptium Temurin Runtime (previously known as AdoptOpenJDK hotspot)

      • Download and install Eclipse Adoptium Temurin Runtime (Java 11) from here.
    • With OpenJDK:

      sudo yum install -y wget tar java-11-openjdk-devel which curl
      
  • SLES (12 SP5, 15 SP3, 15 SP4)

    • With IBM Semeru Runtime (previously known as AdoptOpenJDK openj9)

      • Download and install IBM Semeru Runtime (Java 11) from here.
    • With Eclipse Adoptium Temurin Runtime (previously known as AdoptOpenJDK hotspot)

      • Download and install Eclipse Adoptium Temurin Runtime (Java 11) from here.
    • With OpenJDK:

      sudo zypper install -y wget java-11-openjdk-devel gzip tar curl xz
      
  • Ubuntu (18.04, 20.04, 22.04)

    • With IBM Semeru Runtime (previously known as AdoptOpenJDK openj9)

      • Download and install IBM Semeru Runtime (Java 11) from here.
    • With Eclipse Adoptium Temurin Runtime (previously known as AdoptOpenJDK hotspot)

      • Download and install Eclipse Adoptium Temurin Runtime (Java 11) from here.
    • With OpenJDK:

      sudo apt-get update
      sudo apt-get install -y wget openjdk-11-jdk tar curl
      

Note: At the time of creation of these build instructions, ANTLR was verified with Eclipse Adoptium Temurin Runtim (build 11.0.16.1+1) and IBM Semeru Runtime Open Edition (build 11.0.16.1+1).

1.2) Set Environment variables

export SOURCE_ROOT=/<source_root>/
export JAVA_HOME=<path to installed java>
export PATH=$JAVA_HOME/bin:$PATH

Step 2: If only the ANTLR Java runtime is required

2.1) Download and install ANTLR pre-built binaries

cd $SOURCE_ROOT
curl -s -S -L -O https://www.antlr.org/download/antlr-4.11.1-complete.jar
JAR=$SOURCE_ROOT/antlr-4.11.1-complete.jar
export CLASSPATH=".:$JAR:$CLASSPATH"

2.2) Test the ANTLR tool and Java runtime

Test the tool and runtime using the example provided here.

alias antlr4='java -Xmx500M -cp $CLASSPATH org.antlr.v4.Tool'
alias grun='java -Xmx500M -cp $CLASSPATH org.antlr.v4.gui.TestRig'

cd $SOURCE_ROOT
mkdir antlrtest
cd antlrtest
cat > Hello.g4 <<'EOF'
  grammar Hello;
  r  : 'hello' ID ;
  ID : [a-z]+ ;
  WS : [ \t\r\n]+ -> skip ;
EOF

antlr4 Hello.g4
javac Hello*.java
diff -w <(grun Hello r -tree <<<"hello world") <(echo "(r hello world)")

Step 3: If other ANTLR runtimes are required

3.1) Install the required packages

  • RHEL (7.8, 7.9)
sudo yum install -y unzip xz xz-devel libuuid-devel curl wget git make devtoolset-11-gcc devtoolset-11-gcc-c++ llvm-toolset-11.0 python2 bzip2-devel gdbm-devel libdb-devel libffi-devel ncurses-devel readline-devel sqlite-devel tar tk-devel zlib-devel openssl-devel libcurl-devel

#switch to GCC 11
source /opt/rh/llvm-toolset-11.0/enable
source /opt/rh/devtoolset-11/enable
  • RHEL (8.4, 8.6)
sudo yum install -y unzip xz libuuid-devel curl wget git make gcc gcc-c++ python2 python38 cmake libarchive clang
  • RHEL 9.0
sudo yum install -y unzip xz libuuid-devel curl wget git make gcc gcc-c++ python3 cmake libarchive clang
  • Ubuntu 18.04
sudo apt-get update
sudo apt-get install -y unzip xz-utils uuid-dev curl wget git make python python3.8 gcc-8 g++-8 cmake clang pkg-config libssl-dev libcurl4-openssl-dev zlib1g-dev

#switch to GCC 8
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-8 100 --slave /usr/bin/g++ g++ /usr/bin/g++-8
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.8 100
  • Ubuntu 20.04
sudo apt-get update
sudo apt-get install -y unzip xz-utils uuid-dev curl wget git make python python3.8 gcc g++ cmake clang pkg-config libssl-dev libcurl4-openssl-dev zlib1g-dev
  • Ubuntu 22.04
sudo apt-get update
sudo apt-get install -y unzip xz-utils uuid-dev curl wget git make python2 bzip2 tk-dev libghc-bzlib-dev gcc g++ cmake clang pkg-config  
  • SLES 12 SP5
sudo zypper install -y unzip tar xz xz-devel  libuuid-devel curl wget git make gcc11 gcc11-c++ python python-typing cmake gawk gdbm-devel libbz2-devel libdb-4_8-devel libffi48-devel ncurses-devel readline-devel sqlite3-devel tk-devel zlib-devel openssl-devel libcurl-devel

#switch to GCC 11
sudo update-alternatives --install /usr/bin/cc cc /usr/bin/gcc-11 50
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-11 50
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-11 50
sudo update-alternatives --install /usr/bin/c++ c++ /usr/bin/g++-11 50
  • SLES (15 SP3, 15 SP4)
sudo zypper install -y unzip xz xz-devel libuuid-devel curl wget git make gcc11 gcc11-c++ python cmake clang7 gawk gdbm-devel libbz2-devel libdb-4_8-devel libffi-devel libnsl-devel libopenssl-devel libuuid-devel make ncurses-devel readline-devel sqlite3-devel tar tk-devel zlib-devel gzip

sudo zypper install -y python3 # SLES 15 SP3 only

#switch to GCC 11
sudo update-alternatives --install /usr/bin/cc cc /usr/bin/gcc-11 40
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-11 40
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-11 40
sudo update-alternatives --install /usr/bin/c++ c++ /usr/bin/g++-11 40

3.2) Download(or Install) required binaries(packages)

  • Install Go 1.18.4 from the instructions here, then use the following command to set the GO111MODULE environment variable to auto:
go env -w GO111MODULE=auto
  • Build and install OpenSSL from source (Only on RHEL 7.x and SLES 12 SP5)
cd $SOURCE_ROOT
wget https://www.openssl.org/source/openssl-1.1.1h.tar.gz
tar -xzf openssl-1.1.1h.tar.gz
cd openssl-1.1.1h
./config --prefix=/usr/local --openssldir=/usr/local
make
sudo make install

export LDFLAGS="-L/usr/local/lib/ -L/usr/local/lib64/"
export LD_LIBRARY_PATH=/usr/local/lib/:/usr/local/lib64/:/usr/lib/:/usr/lib64/${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
export CPPFLAGS="-I/usr/local/include/ -I/usr/local/include/openssl"
  • Install Python 3 (Only on RHEL 7.x, SLES 12)
cd $SOURCE_ROOT
ver=3.8.8
wget https://www.python.org/ftp/python/$ver/Python-$ver.tgz
tar -xzf Python-$ver.tgz
cd Python-$ver
./configure
make && sudo make install
python3 -V
  • Install cmake (Only on RHEL 7.x, SLES 12.5, Ubuntu 18.04)
cd $SOURCE_ROOT
wget https://github.com/Kitware/CMake/releases/download/v3.24.2/cmake-3.24.2.tar.gz
tar -xzf cmake-3.24.2.tar.gz
cd cmake-3.24.2
./bootstrap --system-curl
make
sudo make install
export PATH=/usr/local/bin:$PATH
  • Install Clang 8.0.1 (Only on SLES 12.5)

    • Build and Install LLVM Dependency
    cd $SOURCE_ROOT
    wget https://github.com/llvm/llvm-project/releases/download/llvmorg-8.0.1/llvm-8.0.1.src.tar.xz
    tar -xf llvm-8.0.1.src.tar.xz
    cd llvm-8.0.1.src/
    mkdir build && cd build
    cmake -G "Unix Makefiles" -DCMAKE_INSTALL_PREFIX=/usr/local -DCMAKE_BUILD_TYPE=release ..
    make && sudo make install
    • Build and Install CLANG
    cd $SOURCE_ROOT
    wget https://github.com/llvm/llvm-project/releases/download/llvmorg-8.0.1/cfe-8.0.1.src.tar.xz
    tar -xf cfe-8.0.1.src.tar.xz
    cd cfe-8.0.1.src/
    mkdir build && cd build
    cmake -G "Unix Makefiles" -DCMAKE_INSTALL_PREFIX=/usr/local -DCMAKE_BUILD_TYPE=release ..
    make && sudo make install
  • Download NodeJS, Maven and ANTLR source for all distros.

cd $SOURCE_ROOT

TARBALL=node-v16.17.1-linux-s390x.tar.xz
curl -s -S -L -O https://nodejs.org/dist/v16.17.1/${TARBALL}
tar xJf ${TARBALL}

TARBALL=apache-maven-3.8.6-bin.tar.gz
curl -s -S -L -O https://archive.apache.org/dist/maven/maven-3/3.8.6/binaries/${TARBALL}
tar xzf ${TARBALL}

ZIP=antlr4-4.11.1.zip
curl -s -S -L -o ${ZIP} https://github.com/antlr/antlr4/archive/4.11.1.zip
unzip -q ${ZIP}

3.3) Build ANTLR and the supported runtimes

  • Set the PATH environment variable
export PATH=$SOURCE_ROOT/node-v16.17.1-linux-s390x/bin:$SOURCE_ROOT/apache-maven-3.8.6/bin:${PATH}
  • Build the ANTLR tool, then build the supported runtimes.
export MAVEN_OPTS="-Xmx1G"

cd $SOURCE_ROOT/antlr4-4.11.1
mvn install -DskipTests=true
cd runtime/Cpp
mkdir build && mkdir run
cd build
cmake -DWITH_LIBCXX=Off -DCMAKE_BUILD_TYPE=release ..
make -j 8
DESTDIR=$SOURCE_ROOT/antlr4-4.11.1/runtime/Cpp/run make install

Note: If multiple gcc versions are installed, DCMAKE_CXX_COMPILER and DCMAKE_C_COMPILER flags can be used in cmake command to give correct version path.
Note: ANTLR 4.11.1 runtimes supported for Linux on IBM Z are Java, Python, Go, NodeJS and C++.

Step 4: Test (Optional)

cd $SOURCE_ROOT/antlr4-4.11.1/runtime-testsuite
mvn -Dtest=java.** test
mvn -Dtest=python2.** test     # except for RHEL 9.0 and SLES 15 SP4 as Python 2 has been removed from these distros
mvn -Dtest=python3.** test
mvn -Dtest=go.** test
mvn -Dtest=javascript.** test
mvn -Dtest=cpp.** test

References:

Clone this wiki locally