• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_absl_check
64    absl_absl_log
65    absl_base
66    absl_core_headers
67    absl_fixed_array
68    absl_flags
69    absl_flat_hash_map
70    absl_flat_hash_set
71    absl_hash
72    absl_inlined_vector
73    absl_optional
74    absl_span
75    absl_str_format
76    absl_strings
77    absl_synchronization
78    )
79
80# If a top-level project has called add_directory(abseil-cpp) already (possibly
81# indirectly), let that take precedence over any copy of Abseil that might have
82# been installed on the system. And likewise for ICU, GoogleTest and Benchmark.
83if(NOT TARGET absl::base)
84  find_package(absl REQUIRED)
85endif()
86list(APPEND REQUIRES ${ABSL_DEPS})
87
88if(RE2_USE_ICU)
89  if(NOT TARGET ICU::uc)
90    find_package(ICU REQUIRED COMPONENTS uc)
91  endif()
92  add_definitions(-DRE2_USE_ICU)
93  list(APPEND REQUIRES icu-uc)
94endif()
95
96if(USEPCRE)
97  add_definitions(-DUSEPCRE)
98  list(APPEND EXTRA_TARGET_LINK_LIBRARIES pcre)
99endif()
100
101list(JOIN REQUIRES " " REQUIRES)
102
103set(RE2_SOURCES
104    re2/bitmap256.cc
105    re2/bitstate.cc
106    re2/compile.cc
107    re2/dfa.cc
108    re2/filtered_re2.cc
109    re2/mimics_pcre.cc
110    re2/nfa.cc
111    re2/onepass.cc
112    re2/parse.cc
113    re2/perl_groups.cc
114    re2/prefilter.cc
115    re2/prefilter_tree.cc
116    re2/prog.cc
117    re2/re2.cc
118    re2/regexp.cc
119    re2/set.cc
120    re2/simplify.cc
121    re2/tostring.cc
122    re2/unicode_casefold.cc
123    re2/unicode_groups.cc
124    util/rune.cc
125    util/strutil.cc
126    )
127
128set(RE2_HEADERS
129    re2/filtered_re2.h
130    re2/re2.h
131    re2/set.h
132    re2/stringpiece.h
133    )
134
135add_library(re2 ${RE2_SOURCES})
136target_compile_features(re2 PUBLIC cxx_std_14)
137target_include_directories(re2 PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>)
138# CMake gives "set_target_properties called with incorrect number of arguments."
139# errors if we don't quote ${RE2_HEADERS}, so quote it despite prevailing style.
140set_target_properties(re2 PROPERTIES PUBLIC_HEADER "${RE2_HEADERS}")
141set_target_properties(re2 PROPERTIES SOVERSION ${SONAME} VERSION ${SONAME}.0.0)
142add_library(re2::re2 ALIAS re2)
143
144if(APPLE AND RE2_BUILD_FRAMEWORK)
145  set_target_properties(re2 PROPERTIES
146                        FRAMEWORK TRUE
147                        FRAMEWORK_VERSION A
148                        MACOSX_FRAMEWORK_IDENTIFIER com.googlesource.code.re2)
149endif()
150
151if(UNIX)
152  target_link_libraries(re2 PUBLIC Threads::Threads)
153endif()
154
155foreach(dep ${ABSL_DEPS})
156  # Work around https://gitlab.kitware.com/cmake/cmake/-/issues/16899. >:(
157  string(PREPEND dep "^")
158  string(REGEX REPLACE "\\^absl_" "absl::" dep ${dep})
159  target_link_libraries(re2 PUBLIC ${dep})
160endforeach()
161
162if(RE2_USE_ICU)
163  target_link_libraries(re2 PUBLIC ICU::uc)
164endif()
165
166if(RE2_BUILD_TESTING)
167  if(NOT TARGET GTest::gtest)
168    find_package(GTest REQUIRED)
169  endif()
170  if(NOT TARGET benchmark::benchmark)
171    find_package(benchmark REQUIRED)
172  endif()
173
174  set(TESTING_SOURCES
175      re2/testing/backtrack.cc
176      re2/testing/dump.cc
177      re2/testing/exhaustive_tester.cc
178      re2/testing/null_walker.cc
179      re2/testing/regexp_generator.cc
180      re2/testing/string_generator.cc
181      re2/testing/tester.cc
182      util/pcre.cc
183      )
184
185  add_library(testing ${TESTING_SOURCES})
186  if(BUILD_SHARED_LIBS AND WIN32)
187    target_compile_definitions(testing PRIVATE -DRE2_BUILD_TESTING_DLL)
188  endif()
189  target_compile_features(testing PUBLIC cxx_std_14)
190  target_link_libraries(testing PUBLIC re2 GTest::gtest)
191
192  set(TEST_TARGETS
193      charclass_test
194      compile_test
195      filtered_re2_test
196      mimics_pcre_test
197      parse_test
198      possible_match_test
199      re2_test
200      re2_arg_test
201      regexp_test
202      required_prefix_test
203      search_test
204      set_test
205      simplify_test
206      string_generator_test
207
208      dfa_test
209      exhaustive1_test
210      exhaustive2_test
211      exhaustive3_test
212      exhaustive_test
213      random_test
214      )
215
216  set(BENCHMARK_TARGETS
217      regexp_benchmark
218      )
219
220  foreach(target ${TEST_TARGETS})
221    add_executable(${target} re2/testing/${target}.cc)
222    if(BUILD_SHARED_LIBS AND WIN32)
223      target_compile_definitions(${target} PRIVATE -DRE2_CONSUME_TESTING_DLL)
224    endif()
225    target_compile_features(${target} PUBLIC cxx_std_14)
226    target_link_libraries(${target} PUBLIC testing GTest::gtest_main ${EXTRA_TARGET_LINK_LIBRARIES})
227    add_test(NAME ${target} COMMAND ${target})
228  endforeach()
229
230  foreach(target ${BENCHMARK_TARGETS})
231    add_executable(${target} re2/testing/${target}.cc)
232    if(BUILD_SHARED_LIBS AND WIN32)
233      target_compile_definitions(${target} PRIVATE -DRE2_CONSUME_TESTING_DLL)
234    endif()
235    target_compile_features(${target} PUBLIC cxx_std_14)
236    target_link_libraries(${target} PUBLIC testing benchmark::benchmark_main ${EXTRA_TARGET_LINK_LIBRARIES})
237  endforeach()
238endif()
239
240install(TARGETS re2
241        EXPORT re2Targets
242        ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
243        LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
244        RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
245        FRAMEWORK DESTINATION ${CMAKE_INSTALL_LIBDIR}
246        PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/re2
247        INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
248install(EXPORT re2Targets
249        DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/re2
250        NAMESPACE re2::)
251
252configure_package_config_file(${CMAKE_CURRENT_SOURCE_DIR}/re2Config.cmake.in
253                              ${CMAKE_CURRENT_BINARY_DIR}/re2Config.cmake
254                              INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/re2)
255write_basic_package_version_file(${CMAKE_CURRENT_BINARY_DIR}/re2ConfigVersion.cmake
256                                 VERSION ${SONAME}.0.0
257                                 COMPATIBILITY SameMajorVersion)
258
259install(FILES ${CMAKE_CURRENT_BINARY_DIR}/re2Config.cmake
260              ${CMAKE_CURRENT_BINARY_DIR}/re2ConfigVersion.cmake
261        DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/re2)
262
263configure_file(${CMAKE_CURRENT_SOURCE_DIR}/re2.pc.in
264               ${CMAKE_CURRENT_BINARY_DIR}/re2.pc
265               @ONLY)
266
267install(FILES ${CMAKE_CURRENT_BINARY_DIR}/re2.pc
268        DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)
269