antlr / antlr4

ANTLR (ANother Tool for Language Recognition) is a powerful parser generator for reading, processing, executing, or translating structured text or binary files.
http://antlr.org
BSD 3-Clause "New" or "Revised" License
16.73k stars 3.24k forks source link

[antlr-cpp-runtime]failed to compile the runtime when use ccache and ExternalAntlr4Cpp. #3930

Open hagen666 opened 1 year ago

hagen666 commented 1 year ago

target information: C++ antlr version: antlr 4.10 smallest possible grammar: None

smallest possible code to reproduce:

when following environment was set, I cannot use ExternalAntlr4Cpp to compile the antlr_runtime: It's a CentOS 7. The system's gcc is version 4.8.5, ccache version is v4.0 , and I installed gcc 7.3.0 using scl toolset.

 export CCACHE_NOHASHDIR=1 && export GCC_HOME=/opt/rh/devtoolset-7/root/usr/ && export PATH=/opt/rh/devtoolset-7/root/usr/:$PATH && export ASM="${CCACHE_HOME}/bin/ccache ${GCC_HOME}/bin/gcc" && export CC="${CCACHE_HOME}/bin/ccache ${GCC_HOME}/bin/gcc" && export CXX="${CCACHE_HOME} ${GCC_HOME}/bin/g++"

It has an error:

CMake Error at CMakeLists.txt:114
  LIBANTLR4 requires g++ 5.0 or greater.

I found the following code cannot get the version 7.3.0 in the latest version: https://github.com/antlr/antlr4/blob/aa1f1f12a846846fcb78544bdb2ad135471376db/runtime/Cpp/CMakeLists.txt#L124-L130 as the same in v4.10.1: https://github.com/antlr/antlr4/blob/44d87bc1d130c88aa452894aa5f7e2f710f68253/runtime/Cpp/CMakeLists.txt#L110-L116

Here the variable value of CMAKE_CXX_COMPILER is the path to ccache(ccache's version is 4.0), and the gcc is in the variable CMAKE_CXX_COMPILER_ARG1. The variable GCC_VERSION here may be empty.

expected behavior:

ExternalAntlr4Cpp can work when using ccache.

a possible solution:

use CMAKE_CXX_COMPILER_VERSION other than ${CMAKE_CXX_COMPILER} -dumpversion When the file changed, the behavior is normal:

if(NOT (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 5.0 OR CMAKE_CXX_COMPILER_VERSION VERSION_EQUAL 5.0)) 
         message(FATAL_ERROR "${PROJECT_NAME} requires g++ 5.0 or greater.") 
     endif () 

reference:

reference 1 Check GCC minor: https://stackoverflow.com/questions/4058565/check-gcc-minor-in-cmake reference 2 How to use ccache with cmake:https://stackoverflow.com/questions/1815688/how-to-use-ccache-with-cmake

some code segment

Addition cmake file segment:

set(ANTLR4_ZIP_REPOSITORY ${CMAKE_SOURCE_DIR}/3rdparty/antlr4-4.10.1.zip)
set(ANTLR4_CMAKE_ROOT ${PROJECT_SOURCE_DIR}/deps/antlr4-4.10.1/runtime/Cpp/cmake)
set(ANTLR_EXECUTABLE ${CMAKE_SOURCE_DIR}/3rdparty/antlr4-4.10.1-complete.jar)
if ((NOT EXISTS ${ANTLR4_CMAKE_ROOT}))
    if ((NOT EXISTS ${ANTLR4_ZIP_REPOSITORY}))
        message(fatal_error "cannot find dependency antlr4 in the path ${ANTLR4_ZIP_REPOSITORY}.")
    endif()
    execute_process(
            COMMAND unzip -o ${ANTLR4_ZIP_REPOSITORY} "*.cmake" -d ${PROJECT_SOURCE_DIR}/deps/
    )
endif()
list(APPEND CMAKE_MODULE_PATH ${ANTLR4_CMAKE_ROOT})
add_definitions(-DANTLR4CPP_STATIC)
set(ANTLR_BUILD_CPP_TESTS OFF)
set(ANTLR4_WITH_STATIC_CRT OFF)
include(ExternalAntlr4Cpp)
include_directories(${ANTLR4_INCLUDE_DIRS})

and then link antlr4_static to the project:

target_link_libraries(${Program_NAME} antlr4_static)
hagen666 commented 1 year ago

No one response?

sheth-de commented 1 year ago

Facing the same issue while using Antlr-4.x The build fails with the error:

CMake Error at CMakeLists.txt:112 (message):
LIBANTLR4 requires g++ 5.0 or greater.

Is there a fix for this issue?

hagen666 commented 1 year ago

in runtime/Cpp/CMakeLists.txt, replace the 'GCC_VERSION' with 'CMAKE_CXX_COMPILER_VERSION'.

Facing the same issue while using Antlr-4.x The build fails with the error:

CMake Error at CMakeLists.txt:112 (message):
LIBANTLR4 requires g++ 5.0 or greater.

Is there a fix for this issue?