From b6cb1cc8cfbae6a926d761012c587b75ef73c656 Mon Sep 17 00:00:00 2001 From: Robert Adam Date: Wed, 30 Nov 2022 19:09:46 +0100 Subject: [PATCH] Cpp: Add cmake options to selectively disable shared/static build 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 --- runtime/Cpp/runtime/CMakeLists.txt | 117 +++++++++++++++++++---------- 1 file changed, 76 insertions(+), 41 deletions(-) diff --git a/runtime/Cpp/runtime/CMakeLists.txt b/runtime/Cpp/runtime/CMakeLists.txt index 777c31606b..c053fc3034 100644 --- a/runtime/Cpp/runtime/CMakeLists.txt +++ b/runtime/Cpp/runtime/CMakeLists.txt @@ -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 @@ -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) @@ -65,7 +80,7 @@ if (ANTLR_BUILD_CPP_TESTS) target_link_libraries( antlr4_tests - antlr4_static + $,antlr4_static,antlr4_shared> gtest_main ) @@ -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") @@ -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 $ ${CMAKE_HOME_DIRECTORY}/dist - COMMAND ${CMAKE_COMMAND} -E copy_if_different $ ${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 $ ${CMAKE_HOME_DIRECTORY}/dist + COMMAND ${CMAKE_COMMAND} -E copy_if_different $ ${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 $ ${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 $ ${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"