xref: /aosp_15_r20/external/cronet/third_party/re2/src/CMakeLists.txt (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1# Copyright 2015 The RE2 Authors.  All Rights Reserved.
2# Use of this source code is governed by a BSD-style
3# license that can be found in the LICENSE file.
4
5# https://github.com/google/oss-policies-info/blob/main/foundational-cxx-support-matrix.md
6cmake_minimum_required(VERSION 3.13)
7
8project(RE2 CXX)
9include(CMakePackageConfigHelpers)
10include(CTest)
11include(GNUInstallDirs)
12
13option(BUILD_SHARED_LIBS "build shared libraries" OFF)
14option(RE2_USE_ICU "build against ICU for full Unicode properties support" OFF)
15
16# For historical reasons, this is just "USEPCRE", not "RE2_USE_PCRE".
17option(USEPCRE "build against PCRE for testing and benchmarking" OFF)
18
19# See https://groups.google.com/g/re2-dev/c/P6_NM0YIWvA for details.
20# This has no effect unless RE2 is being built for an Apple platform
21# such as macOS or iOS.
22option(RE2_BUILD_FRAMEWORK "build RE2 as a framework" OFF)
23
24# CMake seems to have no way to enable/disable testing per subproject,
25# so we provide an option similar to BUILD_TESTING, but just for RE2.
26option(RE2_BUILD_TESTING "enable testing for RE2" OFF)
27
28# The pkg-config Requires: field.
29set(REQUIRES)
30
31# ABI version
32# http://tldp.org/HOWTO/Program-Library-HOWTO/shared-libraries.html
33set(SONAME 11)
34
35set(EXTRA_TARGET_LINK_LIBRARIES)
36
37if(CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
38  if(MSVC_VERSION LESS 1920)
39    message(FATAL_ERROR "you need Visual Studio 2019 or later")
40  endif()
41  if(BUILD_SHARED_LIBS)
42    set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
43  endif()
44  # CMake defaults to /W3, but some users like /W4 (or /Wall) and /WX,
45  # so we disable various warnings that aren't particularly helpful.
46  add_compile_options(/wd4100 /wd4201 /wd4456 /wd4457 /wd4702 /wd4815)
47  # Without a byte order mark (BOM), Visual Studio assumes that the source
48  # file is encoded using the current user code page, so we specify UTF-8.
49  add_compile_options(/utf-8)
50endif()
51
52if(WIN32)
53  add_definitions(-DUNICODE -D_UNICODE -DSTRICT -DNOMINMAX)
54  add_definitions(-D_CRT_SECURE_NO_WARNINGS -D_SCL_SECURE_NO_WARNINGS)
55endif()
56
57if(UNIX)
58  set(THREADS_PREFER_PTHREAD_FLAG ON)
59  find_package(Threads REQUIRED)
60endif()
61
62set(ABSL_DEPS
63    absl_base
64    absl_core_headers
65    absl_fixed_array
66    absl_flags
67    absl_flat_hash_map
68    absl_flat_hash_set
69    absl_inlined_vector
70    absl_optional
71    absl_span
72    absl_str_format
73    absl_strings
74    absl_synchronization
75    )
76
77# If a top-level project has called add_directory(abseil-cpp) already (possibly
78# indirectly), let that take precedence over any copy of Abseil that might have
79# been installed on the system. And likewise for ICU, GoogleTest and Benchmark.
80if(NOT TARGET absl::base)
81  find_package(absl REQUIRED)
82endif()
83list(APPEND REQUIRES ${ABSL_DEPS})
84
85if(RE2_USE_ICU)
86  if(NOT TARGET ICU::uc)
87    find_package(ICU REQUIRED COMPONENTS uc)
88  endif()
89  add_definitions(-DRE2_USE_ICU)
90  list(APPEND REQUIRES icu-uc)
91endif()
92
93if(USEPCRE)
94  add_definitions(-DUSEPCRE)
95  list(APPEND EXTRA_TARGET_LINK_LIBRARIES pcre)
96endif()
97
98list(JOIN REQUIRES " " REQUIRES)
99
100set(RE2_SOURCES
101    re2/bitmap256.cc
102    re2/bitstate.cc
103    re2/compile.cc
104    re2/dfa.cc
105    re2/filtered_re2.cc
106    re2/mimics_pcre.cc
107    re2/nfa.cc
108    re2/onepass.cc
109    re2/parse.cc
110    re2/perl_groups.cc
111    re2/prefilter.cc
112    re2/prefilter_tree.cc
113    re2/prog.cc
114    re2/re2.cc
115    re2/regexp.cc
116    re2/set.cc
117    re2/simplify.cc
118    re2/tostring.cc
119    re2/unicode_casefold.cc
120    re2/unicode_groups.cc
121    util/rune.cc
122    util/strutil.cc
123    )
124
125set(RE2_HEADERS
126    re2/filtered_re2.h
127    re2/re2.h
128    re2/set.h
129    re2/stringpiece.h
130    )
131
132add_library(re2 ${RE2_SOURCES})
133target_compile_features(re2 PUBLIC cxx_std_14)
134target_include_directories(re2 PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>)
135# CMake gives "set_target_properties called with incorrect number of arguments."
136# errors if we don't quote ${RE2_HEADERS}, so quote it despite prevailing style.
137set_target_properties(re2 PROPERTIES PUBLIC_HEADER "${RE2_HEADERS}")
138set_target_properties(re2 PROPERTIES SOVERSION ${SONAME} VERSION ${SONAME}.0.0)
139add_library(re2::re2 ALIAS re2)
140
141if(APPLE AND RE2_BUILD_FRAMEWORK)
142  set_target_properties(re2 PROPERTIES
143                        FRAMEWORK TRUE
144                        FRAMEWORK_VERSION A
145                        MACOSX_FRAMEWORK_IDENTIFIER com.googlesource.code.re2)
146endif()
147
148if(UNIX)
149  target_link_libraries(re2 PUBLIC Threads::Threads)
150endif()
151
152foreach(dep ${ABSL_DEPS})
153  string(REGEX REPLACE "^absl_" "absl::" dep ${dep})
154  target_link_libraries(re2 PUBLIC ${dep})
155endforeach()
156
157if(RE2_USE_ICU)
158  target_link_libraries(re2 PUBLIC ICU::uc)
159endif()
160
161if(RE2_BUILD_TESTING)
162  if(NOT TARGET GTest::gtest)
163    find_package(GTest REQUIRED)
164  endif()
165  if(NOT TARGET benchmark::benchmark)
166    find_package(benchmark REQUIRED)
167  endif()
168
169  set(TESTING_SOURCES
170      re2/testing/backtrack.cc
171      re2/testing/dump.cc
172      re2/testing/exhaustive_tester.cc
173      re2/testing/null_walker.cc
174      re2/testing/regexp_generator.cc
175      re2/testing/string_generator.cc
176      re2/testing/tester.cc
177      util/pcre.cc
178      )
179
180  add_library(testing ${TESTING_SOURCES})
181  if(BUILD_SHARED_LIBS AND WIN32)
182    target_compile_definitions(testing PRIVATE -DRE2_BUILD_TESTING_DLL)
183  endif()
184  target_compile_features(testing PUBLIC cxx_std_14)
185  target_link_libraries(testing PUBLIC re2 GTest::gtest)
186
187  set(TEST_TARGETS
188      charclass_test
189      compile_test
190      filtered_re2_test
191      mimics_pcre_test
192      parse_test
193      possible_match_test
194      re2_test
195      re2_arg_test
196      regexp_test
197      required_prefix_test
198      search_test
199      set_test
200      simplify_test
201      string_generator_test
202
203      dfa_test
204      exhaustive1_test
205      exhaustive2_test
206      exhaustive3_test
207      exhaustive_test
208      random_test
209      )
210
211  set(BENCHMARK_TARGETS
212      regexp_benchmark
213      )
214
215  foreach(target ${TEST_TARGETS})
216    add_executable(${target} re2/testing/${target}.cc)
217    if(BUILD_SHARED_LIBS AND WIN32)
218      target_compile_definitions(${target} PRIVATE -DRE2_CONSUME_TESTING_DLL)
219    endif()
220    target_compile_features(${target} PUBLIC cxx_std_14)
221    target_link_libraries(${target} PUBLIC testing GTest::gtest_main ${EXTRA_TARGET_LINK_LIBRARIES})
222    add_test(NAME ${target} COMMAND ${target})
223  endforeach()
224
225  foreach(target ${BENCHMARK_TARGETS})
226    add_executable(${target} re2/testing/${target}.cc)
227    if(BUILD_SHARED_LIBS AND WIN32)
228      target_compile_definitions(${target} PRIVATE -DRE2_CONSUME_TESTING_DLL)
229    endif()
230    target_compile_features(${target} PUBLIC cxx_std_14)
231    target_link_libraries(${target} PUBLIC testing benchmark::benchmark_main ${EXTRA_TARGET_LINK_LIBRARIES})
232  endforeach()
233endif()
234
235install(TARGETS re2
236        EXPORT re2Targets
237        ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
238        LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
239        RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
240        FRAMEWORK DESTINATION ${CMAKE_INSTALL_LIBDIR}
241        PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/re2
242        INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
243install(EXPORT re2Targets
244        DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/re2
245        NAMESPACE re2::)
246
247configure_package_config_file(${CMAKE_CURRENT_SOURCE_DIR}/re2Config.cmake.in
248                              ${CMAKE_CURRENT_BINARY_DIR}/re2Config.cmake
249                              INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/re2)
250write_basic_package_version_file(${CMAKE_CURRENT_BINARY_DIR}/re2ConfigVersion.cmake
251                                 VERSION ${SONAME}.0.0
252                                 COMPATIBILITY SameMajorVersion)
253
254install(FILES ${CMAKE_CURRENT_BINARY_DIR}/re2Config.cmake
255              ${CMAKE_CURRENT_BINARY_DIR}/re2ConfigVersion.cmake
256        DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/re2)
257
258configure_file(${CMAKE_CURRENT_SOURCE_DIR}/re2.pc.in
259               ${CMAKE_CURRENT_BINARY_DIR}/re2.pc
260               @ONLY)
261
262install(FILES ${CMAKE_CURRENT_BINARY_DIR}/re2.pc
263        DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)
264