Building and Linking the ANTLR4 C++ Runtime with CMake

  antlr4, c++, cmake, windows

I’m trying to get started with making a compiler in C++, using ANTLR4 as a parser generator, but I’m inexperienced with CMake and don’t understand the process of building and linking the ANTLR C++ runtime to my own application. I’ve been trying to follow this tutorial which is linked in the ANTLR Git repo. I’m running Windows 10. My directory structure (minus some mess that I don’t think is relevant) looks like this:

CMakeLists.txt
├───cmake
│       antlr4-generator.cmake.in
│       antlr4-runtime.cmake.in
│       ExternalAntlr4Cpp.cmake
│       FindANTLR.cmake
│
├───compiler-src
│       TestMain.cpp
│
├───Language1
│   │   Language1.g4
│   └───cpp
│           Language1.interp
│           Language1.tokens
│           Language1BaseListener.cpp
│           Language1BaseListener.h
│           Language1BaseListener.java
│           Language1Lexer.cpp
│           Language1Lexer.h
│           Language1Lexer.interp
│           Language1Lexer.java
│           Language1Lexer.tokens
│           Language1Listener.cpp
│           Language1Listener.h
│           Language1Listener.java
│           Language1Parser.cpp
│           Language1Parser.h
│           Language1Parser.java
└───lib
        antlr-4.9.2-complete.jar

(I manually generated the files in Language1/cpp)

My CMakeLists.txt looks like this:

cmake_minimum_required(VERSION 3.5)

project(project)

list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)

set(CMAKE_CXX_STANDARD 14)

set(ANTLR4CPP_JAR_LOCATION ${PROJECT_SOURCE_DIR}/lib/antlr-4.9.2-complete.jar)
include(ExternalAntlr4Cpp)

include_directories(${ANTLR4CPP_INCLUDE_DIRS})
link_directories(${ANTLR4CPP_LIBS})

message(STATUS "antlr4cpp libs at: ${ANTLR}")
message(STATUS "antlr4cpp includes are at: ${ANTLR4CPP_INCLUDE_DIRS}")

antlr4cpp_process_grammar(demo test_main ${CMAKE_CURRENT_SOURCE_DIR}/Language1/Language1.g4)

include_directories(${antlr4cpp_include_dirs_test_main})

add_executable(TestMain compiler-src/TestMain.cpp ${antlr4cpp_src_files_test_main})

add_dependencies(TestMain antlr4cpp antlr4cpp_generation_test_main)

target_link_libraries(TestMain antlr4-runtime)

ExternalAntlr4Cpp.cmake is from the ANTLR Git repo

From what the tutorial says, my understanding is that my CMakeLists.txt should invoke the ExternalAtlr4Cpp.cmake file which will pull the latest version of the ANTLR source and build it.

I attempt to build by executing the following from the project root directory

mkdir build
cd build
cmake ..

I get the following output from CMake:

-- Building for: Visual Studio 16 2019
-- The C compiler identification is MSVC 19.29.30136.0
-- The CXX compiler identification is MSVC 19.29.30136.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.29.30133/bin/Hostx64/x64/cl.exe - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.29.30133/bin/Hostx64/x64/cl.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
CMake Warning (dev) at C:/Program Files/CMake/share/cmake-3.21/Modules/ExternalProject.cmake:2235 (message):
  Policy CMP0114 is not set: ExternalProject step targets fully adopt their
  steps.  Run "cmake --help-policy CMP0114" for policy details.  Use the
  cmake_policy command to set the policy and suppress this warning.

  ExternalProject target 'antlr4_runtime' would depend on the targets for
  step(s) 'build_static' under policy CMP0114, but this is being left out for
  compatibility since the policy is not set.
Call Stack (most recent call first):
  cmake/ExternalAntlr4Cpp.cmake:132 (ExternalProject_Add_StepTargets)
  CMakeLists.txt:10 (include)
This warning is for project developers.  Use -Wno-dev to suppress it.

CMake Warning (dev) at C:/Program Files/CMake/share/cmake-3.21/Modules/ExternalProject.cmake:2235 (message):
  Policy CMP0114 is not set: ExternalProject step targets fully adopt their
  steps.  Run "cmake --help-policy CMP0114" for policy details.  Use the
  cmake_policy command to set the policy and suppress this warning.

  ExternalProject target 'antlr4_runtime' would depend on the targets for
  step(s) 'build_shared' under policy CMP0114, but this is being left out for
  compatibility since the policy is not set.
Call Stack (most recent call first):
  cmake/ExternalAntlr4Cpp.cmake:149 (ExternalProject_Add_StepTargets)
  CMakeLists.txt:10 (include)
This warning is for project developers.  Use -Wno-dev to suppress it.

-- antlr4cpp libs at:
-- antlr4cpp includes are at:
CMake Error at CMakeLists.txt:18 (antlr4cpp_process_grammar):
  Unknown CMake command "antlr4cpp_process_grammar".


-- Configuring incomplete, errors occurred!

When I comment out the offending line antlr4cpp_process_grammar(demo test_main ${CMAKE_CURRENT_SOURCE_DIR}/Language1/Language1.g4) I get these errors instead

CMake Error at CMakeLists.txt:27 (add_dependencies):
  The dependency target "antlr4cpp" of target "TestMain" does not exist.


CMake Error at CMakeLists.txt:27 (add_dependencies):
  The dependency target "antlr4cpp_generation_test_main" of target "TestMain"
  does not exist.

Any pointers as to what I’m missing here would be much appreciated. Thanks!

Source: Windows Questions

LEAVE A COMMENT