Skip to content

Commit

Permalink
Statically build and link the ANTLR4 runtime.
Browse files Browse the repository at this point in the history
Signed-off-by: Dusty DeWeese <[email protected]>
  • Loading branch information
HackerFoo committed Dec 15, 2020
1 parent 700a4d2 commit 124fa7c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
9 changes: 8 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/../third_party/antlr4/

set(CMAKE_CXX_STANDARD 11)

# required if linking to static library
add_definitions(-DANTLR4CPP_STATIC)

# add external build for antlrcpp
include(ExternalAntlr4Cpp)

# add antrl4cpp artifacts to project environment
include_directories(${ANTLR4_INCLUDE_DIRS})

Expand All @@ -25,7 +31,7 @@ set(ANTLR_EXECUTABLE ${CMAKE_CURRENT_SOURCE_DIR}/../third_party/antlr4/antlr-4.9
# add macros to generate ANTLR Cpp code from grammar
find_package(ANTLR REQUIRED)

find_library(ANTLR4_RUNTIME NAMES antlr4-runtime REQUIRED)
set(ANTLR4_RUNTIME antlr4_static)

# Unit testing library
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../third_party/googletest EXCLUDE_FROM_ALL googletest)
Expand Down Expand Up @@ -69,6 +75,7 @@ add_library(parse_fasm SHARED ParseFasm.cpp
${ANTLR_FasmParser_CXX_OUTPUTS})
target_link_libraries(parse_fasm ${ANTLR4_RUNTIME})
target_compile_options(parse_fasm PRIVATE -Wno-attributes) # Disable warning from antlr4-runtime
set_property(TARGET parse_fasm PROPERTY POSITION_INDEPENDENT_CODE ON)

add_executable(parse_fasm_tests
ParseFasmTests.cpp
Expand Down
11 changes: 9 additions & 2 deletions third_party/antlr4/runtime/Cpp/cmake/ExternalAntlr4Cpp.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ endif()
if(${CMAKE_GENERATOR} MATCHES ".* Makefiles")
# This avoids
# 'warning: jobserver unavailable: using -j1. Add '+' to parent make rule.'
set(ANTLR4_BUILD_COMMAND $(MAKE))
set(ANTLR4_BUILD_COMMAND $(MAKE) CXX_FLAGS="-fPIC" C_FLAGS="-fPIC")
elseif(${CMAKE_GENERATOR} MATCHES "Visual Studio.*")
set(ANTLR4_BUILD_COMMAND
${CMAKE_COMMAND}
Expand All @@ -68,7 +68,10 @@ else()
set(ANTLR4_BUILD_COMMAND
${CMAKE_COMMAND}
--build .
--target)
--target
-DCMAKE_CXX_FLAGS="-fPIC"
-DCMAKE_C_FLAGS="-fPIC"
-DCMAKE_POSITION_INDEPENDENT_CODE=ON)
endif()

if(NOT DEFINED ANTLR4_WITH_STATIC_CRT)
Expand Down Expand Up @@ -128,6 +131,10 @@ ExternalProject_Add_Step(
ExternalProject_Add_StepTargets(antlr4_runtime build_static)

add_library(antlr4_static STATIC IMPORTED)

# https://github.com/antlr/antlr4/issues/2776#issuecomment-677438028
set_property(TARGET antlr4_static PROPERTY POSITION_INDEPENDENT_CODE ON)

add_dependencies(antlr4_static antlr4_runtime-build_static)
set_target_properties(antlr4_static PROPERTIES
IMPORTED_LOCATION ${ANTLR4_STATIC_LIBRARIES})
Expand Down

0 comments on commit 124fa7c

Please sign in to comment.