1
1
option (ANTLR_BUILD_CPP_TESTS "Build C++ tests." ON )
2
2
option (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 ()
3
9
4
10
include_directories (
5
11
${PROJECT_SOURCE_DIR} /runtime/src
@@ -26,15 +32,24 @@ file(GLOB libantlrcpp_SRC
26
32
"${PROJECT_SOURCE_DIR} /runtime/src/tree/xpath/*.cpp"
27
33
)
28
34
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 ()
31
41
32
42
# Make sure to link against threads (pthreads) library in order to be able to
33
43
# make use of std::call_once in the code without producing runtime errors
34
44
# (see also https://github.com/antlr/antlr4/issues/3708 and/or https://stackoverflow.com/q/51584960).
35
45
find_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 ()
38
53
39
54
IF (TRACE_ATN )
40
55
ADD_DEFINITIONS (-DTRACE_ATN_SIM=1 )
@@ -65,7 +80,7 @@ if (ANTLR_BUILD_CPP_TESTS)
65
80
66
81
target_link_libraries (
67
82
antlr4_tests
68
- antlr4_static
83
+ $< IF: $< TARGET_EXISTS: antlr4_static> ,antlr4_static,antlr4_shared >
69
84
gtest_main
70
85
)
71
86
@@ -75,8 +90,12 @@ if (ANTLR_BUILD_CPP_TESTS)
75
90
endif ()
76
91
77
92
if (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 ()
80
99
endif ()
81
100
82
101
if (CMAKE_CXX_COMPILER_ID MATCHES "MSVC" )
@@ -98,54 +117,70 @@ set(static_lib_suffix "")
98
117
99
118
if (WIN32 )
100
119
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 ()
103
126
if (CMAKE_CXX_COMPILER_ID MATCHES "MSVC" )
104
127
set (extra_share_compile_flags "-MP /wd4251" )
105
128
set (extra_static_compile_flags "-MP" )
106
129
endif ()
107
130
endif ()
108
131
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 ()
114
139
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 ()
121
148
122
149
if (ANTLR_BUILD_CPP_TESTS )
123
150
# Copy the generated binaries to dist folder (required by test suite)
151
+ if (TARGET antlr4_shared )
124
152
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 ()
130
159
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} )
136
175
endif ()
137
176
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 ()
149
184
150
185
install (DIRECTORY "${PROJECT_SOURCE_DIR} /runtime/src/"
151
186
DESTINATION "include/antlr4-runtime"
0 commit comments