11option (ANTLR_BUILD_CPP_TESTS "Build C++ tests." ON )
22option (TRACE_ATN "Trace ATN simulation" OFF )
3+ option (ANTLR_BUILD_SHARED "Build the shared library of the ANTLR runtime" ON )
4+ option (ANTLR_BUILD_STATIC "Build the static library of the ANTLR runtime" ON )
5+
6+ if (NOT ANTLR_BUILD_SHARED AND NOT ANTLR_BUILD_STATIC)
7+ message (FATAL_ERROR "Options ANTLR_BUILD_SHARED and ANTLR_BUILD_STATIC can't both be OFF" )
8+ endif ()
39
410include_directories (
511 ${PROJECT_SOURCE_DIR} /runtime/src
@@ -26,15 +32,24 @@ file(GLOB libantlrcpp_SRC
2632 "${PROJECT_SOURCE_DIR} /runtime/src/tree/xpath/*.cpp"
2733)
2834
29- add_library (antlr4_shared SHARED ${libantlrcpp_SRC} )
30- add_library (antlr4_static STATIC ${libantlrcpp_SRC} )
35+ if (ANTLR_BUILD_SHARED)
36+ add_library (antlr4_shared SHARED ${libantlrcpp_SRC} )
37+ endif ()
38+ if (ANTLR_BUILD_STATIC)
39+ add_library (antlr4_static STATIC ${libantlrcpp_SRC} )
40+ endif ()
3141
3242# Make sure to link against threads (pthreads) library in order to be able to
3343# make use of std::call_once in the code without producing runtime errors
3444# (see also https://github.com/antlr/antlr4/issues/3708 and/or https://stackoverflow.com/q/51584960).
3545find_package (Threads REQUIRED)
36- target_link_libraries (antlr4_shared Threads::Threads)
37- target_link_libraries (antlr4_static Threads::Threads)
46+
47+ if (TARGET antlr4_shared)
48+ target_link_libraries (antlr4_shared Threads::Threads)
49+ endif ()
50+ if (TARGET antlr4_static)
51+ target_link_libraries (antlr4_static Threads::Threads)
52+ endif ()
3853
3954IF (TRACE_ATN)
4055 ADD_DEFINITIONS (-DTRACE_ATN_SIM=1)
@@ -65,7 +80,7 @@ if (ANTLR_BUILD_CPP_TESTS)
6580
6681 target_link_libraries (
6782 antlr4_tests
68- antlr4_static
83+ $<IF:$<TARGET_EXISTS: antlr4_static>,antlr4_static,antlr4_shared>
6984 gtest_main
7085 )
7186
@@ -75,8 +90,12 @@ if (ANTLR_BUILD_CPP_TESTS)
7590endif ()
7691
7792if (APPLE )
78- target_link_libraries (antlr4_shared ${COREFOUNDATION_LIBRARY} )
79- target_link_libraries (antlr4_static ${COREFOUNDATION_LIBRARY} )
93+ if (TARGET antlr4_shared)
94+ target_link_libraries (antlr4_shared ${COREFOUNDATION_LIBRARY} )
95+ endif ()
96+ if (TARGET antlr4_static)
97+ target_link_libraries (antlr4_static ${COREFOUNDATION_LIBRARY} )
98+ endif ()
8099endif ()
81100
82101if (CMAKE_CXX_COMPILER_ID MATCHES "MSVC" )
@@ -98,54 +117,70 @@ set(static_lib_suffix "")
98117
99118if (WIN32 )
100119 set (static_lib_suffix "-static" )
101- target_compile_definitions (antlr4_shared PUBLIC ANTLR4CPP_EXPORTS)
102- target_compile_definitions (antlr4_static PUBLIC ANTLR4CPP_STATIC)
120+ if (TARGET antlr4_shared)
121+ target_compile_definitions (antlr4_shared PUBLIC ANTLR4CPP_EXPORTS)
122+ endif ()
123+ if (TARGET antlr4_static)
124+ target_compile_definitions (antlr4_static PUBLIC ANTLR4CPP_STATIC)
125+ endif ()
103126 if (CMAKE_CXX_COMPILER_ID MATCHES "MSVC" )
104127 set (extra_share_compile_flags "-MP /wd4251" )
105128 set (extra_static_compile_flags "-MP" )
106129 endif ()
107130endif ()
108131
109- set_target_properties (antlr4_shared
110- PROPERTIES VERSION ${ANTLR_VERSION}
111- SOVERSION ${ANTLR_VERSION}
112- OUTPUT_NAME antlr4-runtime
113- COMPILE_FLAGS "${disabled_compile_warnings} ${extra_share_compile_flags} " )
132+ if (TARGET antlr4_shared)
133+ set_target_properties (antlr4_shared
134+ PROPERTIES VERSION ${ANTLR_VERSION}
135+ SOVERSION ${ANTLR_VERSION}
136+ OUTPUT_NAME antlr4-runtime
137+ COMPILE_FLAGS "${disabled_compile_warnings} ${extra_share_compile_flags} " )
138+ endif ()
114139
115- set_target_properties (antlr4_static
116- PROPERTIES VERSION ${ANTLR_VERSION}
117- SOVERSION ${ANTLR_VERSION}
118- OUTPUT_NAME "antlr4-runtime${static_lib_suffix} "
119- COMPILE_PDB_NAME "antlr4-runtime${static_lib_suffix} "
120- COMPILE_FLAGS "${disabled_compile_warnings} ${extra_static_compile_flags} " )
140+ if (TARGET antlr4_static)
141+ set_target_properties (antlr4_static
142+ PROPERTIES VERSION ${ANTLR_VERSION}
143+ SOVERSION ${ANTLR_VERSION}
144+ OUTPUT_NAME "antlr4-runtime${static_lib_suffix} "
145+ COMPILE_PDB_NAME "antlr4-runtime${static_lib_suffix} "
146+ COMPILE_FLAGS "${disabled_compile_warnings} ${extra_static_compile_flags} " )
147+ endif ()
121148
122149if (ANTLR_BUILD_CPP_TESTS)
123150 # Copy the generated binaries to dist folder (required by test suite)
151+ if (TARGET antlr4_shared)
124152 add_custom_command (
125- TARGET antlr4_shared
126- POST_BUILD
127- COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_HOME_DIRECTORY} /dist
128- COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_FILE:antlr4_shared> ${CMAKE_HOME_DIRECTORY} /dist
129- COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_LINKER_FILE:antlr4_shared> ${CMAKE_HOME_DIRECTORY} /dist)
153+ TARGET antlr4_shared
154+ POST_BUILD
155+ COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_HOME_DIRECTORY} /dist
156+ COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_FILE:antlr4_shared> ${CMAKE_HOME_DIRECTORY} /dist
157+ COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_LINKER_FILE:antlr4_shared> ${CMAKE_HOME_DIRECTORY} /dist)
158+ endif ()
130159
131- add_custom_command (
132- TARGET antlr4_static
133- POST_BUILD
134- COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_HOME_DIRECTORY} /dist
135- COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_FILE:antlr4_static> ${CMAKE_HOME_DIRECTORY} /dist)
160+ if (TARGET antlr4_static)
161+ add_custom_command (
162+ TARGET antlr4_static
163+ POST_BUILD
164+ COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_HOME_DIRECTORY} /dist
165+ COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_FILE:antlr4_static> ${CMAKE_HOME_DIRECTORY} /dist)
166+ endif ()
167+ endif ()
168+
169+ if (TARGET antlr4_shared)
170+ install (TARGETS antlr4_shared
171+ EXPORT antlr4-targets
172+ ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
173+ LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
174+ RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} )
136175endif ()
137176
138- install (TARGETS antlr4_shared
139- EXPORT antlr4-targets
140- ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
141- LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
142- RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} )
143-
144- install (TARGETS antlr4_static
145- EXPORT antlr4-targets
146- ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
147- LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
148- RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} )
177+ if (TARGET antlr4_static)
178+ install (TARGETS antlr4_static
179+ EXPORT antlr4-targets
180+ ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
181+ LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
182+ RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} )
183+ endif ()
149184
150185install (DIRECTORY "${PROJECT_SOURCE_DIR} /runtime/src/"
151186 DESTINATION "include/antlr4-runtime"
0 commit comments