11cmake_minimum_required (VERSION 2.8.12...3.15 )
22project (logfile_parser_cpp LANGUAGES CXX )
33
4+ # Use the static runtime on MSVC so the produced binaries do not depend on the
5+ # redistributable DLLs. This also matches the statically built ANTLR runtime.
6+ if (MSVC )
7+ set (CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG :Debug >:Debug >" )
8+ endif ()
9+
10+ set (_antlr_runtime_is_static OFF )
11+
412# Prefer a known Homebrew install or project-local jar if present
513if (NOT ANTLR_EXECUTABLE)
614 if (EXISTS "/opt/homebrew/Cellar/antlr/4.13.2/antlr-4.13.2-complete.jar" )
@@ -24,19 +32,30 @@ if(NOT TARGET ANTLR4::Runtime AND NOT TARGET ANTLR::Runtime)
2432
2533 include (FetchContent )
2634
35+ # Ensure the fetched ANTLR runtime is built statically. This keeps the
36+ # Windows binary self-contained and avoids shipping libantlr4 DLLs.
37+ set (ANTLR4_BUILD_SHARED OFF CACHE BOOL "Build ANTLR4 shared runtime" FORCE )
38+ set (ANTLR4_BUILD_STATIC ON CACHE BOOL "Build ANTLR4 static runtime" FORCE )
39+ set (ANTLR4_INSTALL OFF CACHE BOOL "Skip installing ANTLR4 runtime" FORCE )
40+ if (MSVC )
41+ set (ANTLR4_WITH_STATIC_CRT ON CACHE BOOL "Link ANTLR4 runtime against the static Microsoft CRT" FORCE )
42+ endif ()
43+
2744 # Only fetch/build if we don't already have runtime variables pointing to a prebuilt lib.
2845 if (NOT DEFINED ANTLR_RUNTIME_LIBRARY AND NOT DEFINED ANTLR_LIBRARY)
2946 message (STATUS "Attempting to FetchContent antlr/antlr4 to build the C++ runtime" )
3047
48+ # Define the ANTLR 4 branch / commit hash
49+ set (ANTLR4_TAG bd8f9930d053ec6a83e7d751e8c6f69138957665)
50+
3151 FetchContent_Declare (
3252 antlr4
3353 GIT_REPOSITORY https://github.com/antlr/antlr4.git
34- # Pin to a compatible version (change tag if you want a different release)
35- GIT_TAG 4.13.2
54+ GIT_TAG ${ANTLR4_TAG} # dev as of 2025-10-16
3655 )
3756
38- # This will clone into the build directory and make antlr4_SOURCE_DIR available.
39- FetchContent_MakeAvailable (antlr4)
57+ # This will clone into the build directory and make antlr4_SOURCE_DIR available.
58+ FetchContent_MakeAvailable (antlr4)
4059
4160 # Make ANTLR CMake helper macros discoverable if provided by the fetched source.
4261 if (DEFINED antlr4_SOURCE_DIR )
@@ -70,19 +89,20 @@ if(NOT TARGET ANTLR4::Runtime AND NOT TARGET ANTLR::Runtime)
7089 endif ()
7190
7291 # Create alias targets matching common Find module conventions so existing code can link unchanged.
73- if (TARGET antlr4_shared )
92+ if (TARGET antlr4_static )
7493 if (NOT TARGET ANTLR4::Runtime)
75- add_library (ANTLR4::Runtime ALIAS antlr4_shared )
94+ add_library (ANTLR4::Runtime ALIAS antlr4_static )
7695 endif ()
7796 if (NOT TARGET ANTLR::Runtime)
78- add_library (ANTLR::Runtime ALIAS antlr4_shared )
97+ add_library (ANTLR::Runtime ALIAS antlr4_static )
7998 endif ()
80- elseif (TARGET antlr4_static)
99+ set (_antlr_runtime_is_static ON )
100+ elseif (TARGET antlr4_shared)
81101 if (NOT TARGET ANTLR4::Runtime)
82- add_library (ANTLR4::Runtime ALIAS antlr4_static )
102+ add_library (ANTLR4::Runtime ALIAS antlr4_shared )
83103 endif ()
84104 if (NOT TARGET ANTLR::Runtime)
85- add_library (ANTLR::Runtime ALIAS antlr4_static )
105+ add_library (ANTLR::Runtime ALIAS antlr4_shared )
86106 endif ()
87107 else ()
88108 message (STATUS "FetchContent provided antlr4 but runtime targets not found; will try bundled runtime or Find module variables" )
@@ -109,6 +129,11 @@ set_target_properties(logparser PROPERTIES
109129 CXX_EXTENSIONS NO
110130)
111131
132+ # For Windows, we have to define `NOMINMAX` to prevent Windows headers from defining `min` and `max` macros.
133+ if (MSVC )
134+ target_compile_definitions (logparser PRIVATE NOMINMAX )
135+ endif ()
136+
112137# Link to the ANTLR runtime. Support several possible Find module conventions.
113138if (TARGET ANTLR4::Runtime)
114139 target_link_libraries (logparser PRIVATE ANTLR4::Runtime )
@@ -122,6 +147,10 @@ else()
122147 message (FATAL_ERROR "ANTLR runtime target or variables not found. Verify FindANTLR.cmake" )
123148endif ()
124149
150+ if (_antlr_runtime_is_static)
151+ target_compile_definitions (logparser PRIVATE ANTLR4CPP_STATIC )
152+ endif ()
153+
125154# Add include directories if the find module provided them
126155if (DEFINED ANTLR_RUNTIME_INCLUDE_DIR)
127156 target_include_directories (logparser PRIVATE ${ANTLR_RUNTIME_INCLUDE_DIR} )
0 commit comments