Skip to content

Commit

Permalink
Cpp: Add cmake options to selectively disable shared/static build
Browse files Browse the repository at this point in the history
The default behavior is left unchanged (build both) but now users can
choose to optionally only build one of the two variants.
Due to macro-magic, both variants had to be compiled separately and
therefore building both variants really does compile everything twice.
Therefore, disabling the version that one is not interested in can cut
down compilation time significantly.

Fixes #3993

Signed-off-by: Robert Adam <dev@robert-adam.de>
  • Loading branch information
Krzmbrzl authored and parrt committed Feb 19, 2023
1 parent 1bd8bbe commit b6cb1cc
Showing 1 changed file with 76 additions and 41 deletions.
117 changes: 76 additions & 41 deletions runtime/Cpp/runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
option(ANTLR_BUILD_CPP_TESTS "Build C++ tests." ON)
option(TRACE_ATN "Trace ATN simulation" OFF)
option(ANTLR_BUILD_SHARED "Build the shared library of the ANTLR runtime" ON)
option(ANTLR_BUILD_STATIC "Build the static library of the ANTLR runtime" ON)

if (NOT ANTLR_BUILD_SHARED AND NOT ANTLR_BUILD_STATIC)
message(FATAL_ERROR "Options ANTLR_BUILD_SHARED and ANTLR_BUILD_STATIC can't both be OFF")
endif()

include_directories(
${PROJECT_SOURCE_DIR}/runtime/src
Expand All @@ -26,15 +32,24 @@ file(GLOB libantlrcpp_SRC
"${PROJECT_SOURCE_DIR}/runtime/src/tree/xpath/*.cpp"
)

add_library(antlr4_shared SHARED ${libantlrcpp_SRC})
add_library(antlr4_static STATIC ${libantlrcpp_SRC})
if (ANTLR_BUILD_SHARED)
add_library(antlr4_shared SHARED ${libantlrcpp_SRC})
endif()
if (ANTLR_BUILD_STATIC)
add_library(antlr4_static STATIC ${libantlrcpp_SRC})
endif()

# Make sure to link against threads (pthreads) library in order to be able to
# make use of std::call_once in the code without producing runtime errors
# (see also https://github.com/antlr/antlr4/issues/3708 and/or https://stackoverflow.com/q/51584960).
find_package(Threads REQUIRED)
target_link_libraries(antlr4_shared Threads::Threads)
target_link_libraries(antlr4_static Threads::Threads)

if (TARGET antlr4_shared)
target_link_libraries(antlr4_shared Threads::Threads)
endif()
if (TARGET antlr4_static)
target_link_libraries(antlr4_static Threads::Threads)
endif()

IF(TRACE_ATN)
ADD_DEFINITIONS(-DTRACE_ATN_SIM=1)
Expand Down Expand Up @@ -65,7 +80,7 @@ if (ANTLR_BUILD_CPP_TESTS)

target_link_libraries(
antlr4_tests
antlr4_static
$<IF:$<TARGET_EXISTS:antlr4_static>,antlr4_static,antlr4_shared>
gtest_main
)

Expand All @@ -75,8 +90,12 @@ if (ANTLR_BUILD_CPP_TESTS)
endif()

if(APPLE)
target_link_libraries(antlr4_shared ${COREFOUNDATION_LIBRARY})
target_link_libraries(antlr4_static ${COREFOUNDATION_LIBRARY})
if (TARGET antlr4_shared)
target_link_libraries(antlr4_shared ${COREFOUNDATION_LIBRARY})
endif()
if (TARGET antlr4_static)
target_link_libraries(antlr4_static ${COREFOUNDATION_LIBRARY})
endif()
endif()

if(CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
Expand All @@ -98,54 +117,70 @@ set(static_lib_suffix "")

if (WIN32)
set(static_lib_suffix "-static")
target_compile_definitions(antlr4_shared PUBLIC ANTLR4CPP_EXPORTS)
target_compile_definitions(antlr4_static PUBLIC ANTLR4CPP_STATIC)
if (TARGET antlr4_shared)
target_compile_definitions(antlr4_shared PUBLIC ANTLR4CPP_EXPORTS)
endif()
if (TARGET antlr4_static)
target_compile_definitions(antlr4_static PUBLIC ANTLR4CPP_STATIC)
endif()
if(CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
set(extra_share_compile_flags "-MP /wd4251")
set(extra_static_compile_flags "-MP")
endif()
endif()

set_target_properties(antlr4_shared
PROPERTIES VERSION ${ANTLR_VERSION}
SOVERSION ${ANTLR_VERSION}
OUTPUT_NAME antlr4-runtime
COMPILE_FLAGS "${disabled_compile_warnings} ${extra_share_compile_flags}")
if (TARGET antlr4_shared)
set_target_properties(antlr4_shared
PROPERTIES VERSION ${ANTLR_VERSION}
SOVERSION ${ANTLR_VERSION}
OUTPUT_NAME antlr4-runtime
COMPILE_FLAGS "${disabled_compile_warnings} ${extra_share_compile_flags}")
endif()

set_target_properties(antlr4_static
PROPERTIES VERSION ${ANTLR_VERSION}
SOVERSION ${ANTLR_VERSION}
OUTPUT_NAME "antlr4-runtime${static_lib_suffix}"
COMPILE_PDB_NAME "antlr4-runtime${static_lib_suffix}"
COMPILE_FLAGS "${disabled_compile_warnings} ${extra_static_compile_flags}")
if (TARGET antlr4_static)
set_target_properties(antlr4_static
PROPERTIES VERSION ${ANTLR_VERSION}
SOVERSION ${ANTLR_VERSION}
OUTPUT_NAME "antlr4-runtime${static_lib_suffix}"
COMPILE_PDB_NAME "antlr4-runtime${static_lib_suffix}"
COMPILE_FLAGS "${disabled_compile_warnings} ${extra_static_compile_flags}")
endif()

if (ANTLR_BUILD_CPP_TESTS)
# Copy the generated binaries to dist folder (required by test suite)
if (TARGET antlr4_shared)
add_custom_command(
TARGET antlr4_shared
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_HOME_DIRECTORY}/dist
COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_FILE:antlr4_shared> ${CMAKE_HOME_DIRECTORY}/dist
COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_LINKER_FILE:antlr4_shared> ${CMAKE_HOME_DIRECTORY}/dist)
TARGET antlr4_shared
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_HOME_DIRECTORY}/dist
COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_FILE:antlr4_shared> ${CMAKE_HOME_DIRECTORY}/dist
COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_LINKER_FILE:antlr4_shared> ${CMAKE_HOME_DIRECTORY}/dist)
endif()

add_custom_command(
TARGET antlr4_static
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_HOME_DIRECTORY}/dist
COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_FILE:antlr4_static> ${CMAKE_HOME_DIRECTORY}/dist)
if (TARGET antlr4_static)
add_custom_command(
TARGET antlr4_static
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_HOME_DIRECTORY}/dist
COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_FILE:antlr4_static> ${CMAKE_HOME_DIRECTORY}/dist)
endif()
endif()

if (TARGET antlr4_shared)
install(TARGETS antlr4_shared
EXPORT antlr4-targets
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
endif()

install(TARGETS antlr4_shared
EXPORT antlr4-targets
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})

install(TARGETS antlr4_static
EXPORT antlr4-targets
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
if (TARGET antlr4_static)
install(TARGETS antlr4_static
EXPORT antlr4-targets
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
endif()

install(DIRECTORY "${PROJECT_SOURCE_DIR}/runtime/src/"
DESTINATION "include/antlr4-runtime"
Expand Down

0 comments on commit b6cb1cc

Please sign in to comment.