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# Old enough to support Ubuntu Xenial. 6cmake_minimum_required(VERSION 3.5.1) 7 8if(POLICY CMP0048) 9 cmake_policy(SET CMP0048 NEW) 10endif() 11 12project(RE2 CXX) 13include(CTest) 14include(GNUInstallDirs) 15 16if(NOT CMAKE_CXX_STANDARD) 17 set(CMAKE_CXX_STANDARD 11) 18 set(CMAKE_CXX_STANDARD_REQUIRED ON) 19endif() 20 21option(BUILD_SHARED_LIBS "build shared libraries" OFF) 22option(USEPCRE "use PCRE in tests and benchmarks" 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" ON) 27 28set(EXTRA_TARGET_LINK_LIBRARIES) 29 30if(CMAKE_CXX_COMPILER_ID MATCHES "MSVC") 31 if(MSVC_VERSION LESS 1900) 32 message(FATAL_ERROR "you need Visual Studio 2015 or later") 33 endif() 34 if(BUILD_SHARED_LIBS) 35 # See http://www.kitware.com/blog/home/post/939 for details. 36 set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON) 37 endif() 38 # CMake defaults to /W3, but some users like /W4 (or /Wall) and /WX, 39 # so we disable various warnings that aren't particularly helpful. 40 add_compile_options(/wd4100 /wd4201 /wd4456 /wd4457 /wd4702 /wd4815) 41 # Without a byte order mark (BOM), Visual Studio assumes that the source 42 # file is encoded using the current user code page, so we specify UTF-8. 43 add_compile_options(/utf-8) 44endif() 45 46if(WIN32) 47 add_definitions(-DUNICODE -D_UNICODE -DSTRICT -DNOMINMAX) 48 add_definitions(-D_CRT_SECURE_NO_WARNINGS -D_SCL_SECURE_NO_WARNINGS) 49elseif(UNIX) 50 add_compile_options(-pthread) 51 list(APPEND EXTRA_TARGET_LINK_LIBRARIES -pthread) 52endif() 53 54if(USEPCRE) 55 add_definitions(-DUSEPCRE) 56 list(APPEND EXTRA_TARGET_LINK_LIBRARIES pcre) 57endif() 58 59include_directories(${CMAKE_CURRENT_SOURCE_DIR}) 60 61set(RE2_SOURCES 62 re2/bitstate.cc 63 re2/compile.cc 64 re2/dfa.cc 65 re2/filtered_re2.cc 66 re2/mimics_pcre.cc 67 re2/nfa.cc 68 re2/onepass.cc 69 re2/parse.cc 70 re2/perl_groups.cc 71 re2/prefilter.cc 72 re2/prefilter_tree.cc 73 re2/prog.cc 74 re2/re2.cc 75 re2/regexp.cc 76 re2/set.cc 77 re2/simplify.cc 78 re2/stringpiece.cc 79 re2/tostring.cc 80 re2/unicode_casefold.cc 81 re2/unicode_groups.cc 82 util/rune.cc 83 util/strutil.cc 84 ) 85 86add_library(re2 ${RE2_SOURCES}) 87add_library(re2::re2 ALIAS re2) 88 89if(RE2_BUILD_TESTING) 90 set(TESTING_SOURCES 91 re2/testing/backtrack.cc 92 re2/testing/dump.cc 93 re2/testing/exhaustive_tester.cc 94 re2/testing/null_walker.cc 95 re2/testing/regexp_generator.cc 96 re2/testing/string_generator.cc 97 re2/testing/tester.cc 98 util/pcre.cc 99 ) 100 101 add_library(testing STATIC ${TESTING_SOURCES}) 102 103 set(TEST_TARGETS 104 charclass_test 105 compile_test 106 filtered_re2_test 107 mimics_pcre_test 108 parse_test 109 possible_match_test 110 re2_test 111 re2_arg_test 112 regexp_test 113 required_prefix_test 114 search_test 115 set_test 116 simplify_test 117 string_generator_test 118 119 dfa_test 120 exhaustive1_test 121 exhaustive2_test 122 exhaustive3_test 123 exhaustive_test 124 random_test 125 ) 126 127 set(BENCHMARK_TARGETS 128 regexp_benchmark 129 ) 130 131 foreach(target ${TEST_TARGETS}) 132 add_executable(${target} re2/testing/${target}.cc util/test.cc) 133 target_link_libraries(${target} testing re2 ${EXTRA_TARGET_LINK_LIBRARIES}) 134 add_test(NAME ${target} COMMAND ${target}) 135 endforeach(target) 136 137 foreach(target ${BENCHMARK_TARGETS}) 138 add_executable(${target} re2/testing/${target}.cc util/benchmark.cc) 139 target_link_libraries(${target} testing re2 ${EXTRA_TARGET_LINK_LIBRARIES}) 140 endforeach(target) 141endif() 142 143set(RE2_HEADERS 144 re2/filtered_re2.h 145 re2/re2.h 146 re2/set.h 147 re2/stringpiece.h 148 ) 149 150install(FILES ${RE2_HEADERS} 151 DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/re2) 152install(TARGETS re2 EXPORT re2Config 153 ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} 154 LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} 155 RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} 156 INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) 157install(EXPORT re2Config 158 DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/re2 NAMESPACE re2::) 159