1515#
1616#######################
1717
18- # Find ANTLR4 runtime
1918find_package (antlr4-runtime QUIET )
2019
2120if (NOT antlr4-runtime_FOUND)
@@ -27,7 +26,6 @@ if(NOT antlr4-runtime_FOUND)
2726 return ()
2827endif ()
2928
30- # Find ANTLR4 generator tool
3129find_program (ANTLR4_EXECUTABLE antlr4 NAMES antlr4 antlr )
3230
3331if (NOT ANTLR4_EXECUTABLE)
@@ -39,22 +37,55 @@ if(NOT ANTLR4_EXECUTABLE)
3937 return ()
4038endif ()
4139
42- # ANTLR4 available - hrw4u will be built
40+ # Verify tool version matches runtime (ANTLR4 C++ API breaks between minor versions).
41+ if (ANTLR_VERSION)
42+ execute_process (
43+ COMMAND ${ANTLR4_EXECUTABLE}
44+ OUTPUT_VARIABLE _antlr4_tool_stdout
45+ ERROR_VARIABLE _antlr4_tool_stderr
46+ OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_STRIP_TRAILING_WHITESPACE
47+ TIMEOUT 10
48+ )
49+ set (_antlr4_tool_output "${_antlr4_tool_stdout} ${_antlr4_tool_stderr} " )
50+ string (REGEX MATCH "[Vv]ersion[ \t ]+([0-9]+\\ .[0-9]+\\ .?[0-9]*)" _antlr4_version_match "${_antlr4_tool_output} " )
51+ set (_antlr4_tool_version "${CMAKE_MATCH_1} " )
52+ unset (_antlr4_tool_stdout)
53+ unset (_antlr4_tool_stderr)
54+ unset (_antlr4_tool_output)
55+ unset (_antlr4_version_match)
56+
57+ if (_antlr4_tool_version)
58+ string (REGEX MATCH "^([0-9]+\\ .[0-9]+)" _tool_major_minor "${_antlr4_tool_version} " )
59+ string (REGEX MATCH "^([0-9]+\\ .[0-9]+)" _runtime_major_minor "${ANTLR_VERSION} " )
60+ if (NOT _tool_major_minor STREQUAL _runtime_major_minor)
61+ message (WARNING "ANTLR4 version mismatch: tool is ${_antlr4_tool_version} , runtime is ${ANTLR_VERSION} . "
62+ "hrw4u will not be built. Install matching versions to enable hrw4u."
63+ )
64+ set (TS_HAS_CPP_HRW4U
65+ FALSE
66+ PARENT_SCOPE
67+ )
68+ return ()
69+ endif ()
70+ message (STATUS "ANTLR4 tool version ${_antlr4_tool_version} matches runtime ${ANTLR_VERSION} " )
71+ unset (_tool_major_minor)
72+ unset (_runtime_major_minor)
73+ else ()
74+ message (STATUS "Could not determine ANTLR4 tool version, proceeding with runtime ${ANTLR_VERSION} " )
75+ endif ()
76+ unset (_antlr4_tool_version)
77+ endif ()
78+
4379set (TS_HAS_CPP_HRW4U
4480 TRUE
4581 PARENT_SCOPE
4682)
47-
4883message (STATUS "Building hrw4u library with ANTLR4 support" )
4984
50- # Grammar file location
5185set (HRW4U_GRAMMAR_FILE "${PROJECT_SOURCE_DIR} /tools/hrw4u/grammar/hrw4u.g4" )
52-
53- # Generated parser output directory
5486set (HRW4U_GENERATED_DIR "${CMAKE_CURRENT_BINARY_DIR} /generated" )
5587file (MAKE_DIRECTORY ${HRW4U_GENERATED_DIR} )
5688
57- # Generate C++ parser from grammar
5889set (HRW4U_GENERATED_SOURCES ${HRW4U_GENERATED_DIR} /hrw4uLexer.cpp ${HRW4U_GENERATED_DIR} /hrw4uParser.cpp
5990 ${HRW4U_GENERATED_DIR} /hrw4uBaseVisitor.cpp ${HRW4U_GENERATED_DIR} /hrw4uVisitor.cpp
6091)
@@ -73,19 +104,14 @@ add_custom_command(
73104
74105add_custom_target (hrw4u_generated DEPENDS ${HRW4U_GENERATED_SOURCES} )
75106
76- # Suppress warnings for generated ANTLR4 code
77107set_source_files_properties (${HRW4U_GENERATED_SOURCES} PROPERTIES COMPILE_FLAGS "-Wno-unused-parameter" )
78108
79- # Library sources
80109set (HRW4U_SOURCES Error.cc Types.cc Tables.cc Visitor.cc HRW4UVisitorImpl.cc)
81110
82- # Create static library
83111add_library (hrw4u STATIC ${HRW4U_SOURCES} ${HRW4U_GENERATED_SOURCES} )
84112add_library (ts::hrw4u ALIAS hrw4u )
85113
86- # GCC treats out-of-order and missing designated initializers as errors under
87- # -Werror. The hrw4u tables use Clang-style designated initializers that rely
88- # on default member values, which is safe but upsets GCC.
114+ # GCC errors on Clang-style designated initializers under -Werror.
89115target_compile_options (hrw4u PRIVATE -Wno-missing-field-initializers )
90116set_target_properties (hrw4u PROPERTIES POSITION_INDEPENDENT_CODE ON )
91117
@@ -97,12 +123,16 @@ target_include_directories(
97123 PRIVATE ${HRW4U_GENERATED_DIR} ${CMAKE_CURRENT_SOURCE_DIR}
98124)
99125
100- # Prefer static ANTLR4, fall back to shared if unavailable.
101- if (TARGET antlr4_static)
102- target_link_libraries (hrw4u PUBLIC antlr4_static )
103- else ()
126+ # Prefer shared ANTLR4 runtime (distro static libs may lack -fPIC).
127+ if (TARGET antlr4_shared)
104128 target_link_libraries (hrw4u PUBLIC antlr4_shared )
105- target_include_directories (hrw4u PRIVATE ${ANTLR4_INCLUDE_DIR} )
129+ elseif (TARGET antlr4_static)
130+ target_link_libraries (hrw4u PUBLIC antlr4_static )
131+ endif ()
132+
133+ # Fallback for distro packages that don't set INTERFACE_INCLUDE_DIRECTORIES.
134+ if (ANTLR4_INCLUDE_DIR)
135+ target_include_directories (hrw4u PUBLIC ${ANTLR4_INCLUDE_DIR} )
106136endif ()
107137
108138set (HRW4U_PUBLIC_HEADERS
0 commit comments