From 0f3bf78f0f3fa91234d9a594b164c701dc7ad323 Mon Sep 17 00:00:00 2001 From: Paul Wankadia Date: Thu, 1 Jun 2023 12:45:45 +0000 Subject: [PATCH] Export `PCRE::no_more_args` and the functors. To produce a DLL, CMake can automatically export code symbols, but not data symbols, so we have to annotate those manually... Set up CI to exercise `-D BUILD_SHARED_LIBS=ON` on everything. Fixes #429. Change-Id: Ibc232913e5f7121b9804748ea03422862893e482 Reviewed-on: https://code-review.googlesource.com/c/re2/+/61392 Reviewed-by: Paul Wankadia Reviewed-by: Perry Lorier --- .github/workflows/ci-cmake.yml | 56 ++++++++++++++++++++++++++++++++-- CMakeLists.txt | 9 ++++++ util/pcre.h | 20 +++++++++--- 3 files changed, 78 insertions(+), 7 deletions(-) diff --git a/.github/workflows/ci-cmake.yml b/.github/workflows/ci-cmake.yml index cd7356172..b4f034441 100644 --- a/.github/workflows/ci-cmake.yml +++ b/.github/workflows/ci-cmake.yml @@ -25,6 +25,25 @@ jobs: shell: bash - run: .github/cmake.sh shell: bash + build-linux-shared: + runs-on: ubuntu-latest + container: gcc:13 + strategy: + fail-fast: false + steps: + - uses: actions/checkout@v3 + - name: Install CMake + run: | + apt update -y + apt install -y cmake + shell: bash + - name: Install Abseil, GoogleTest and Benchmark + run: | + apt update -y + apt install -y libabsl-dev libgtest-dev libbenchmark-dev + shell: bash + - run: .github/cmake.sh -D BUILD_SHARED_LIBS=ON + shell: bash build-macos: runs-on: macos-latest strategy: @@ -36,7 +55,20 @@ jobs: brew update brew install abseil googletest google-benchmark shell: bash - - run: .github/cmake.sh -D RE2_BUILD_FRAMEWORK=ON + - run: .github/cmake.sh + shell: bash + build-macos-shared: + runs-on: macos-latest + strategy: + fail-fast: false + steps: + - uses: actions/checkout@v3 + - name: Install Abseil, GoogleTest and Benchmark + run: | + brew update + brew install abseil googletest google-benchmark + shell: bash + - run: .github/cmake.sh -D BUILD_SHARED_LIBS=ON shell: bash build-windows: runs-on: windows-latest @@ -52,5 +84,25 @@ jobs: vcpkg update vcpkg install abseil gtest benchmark shell: bash - - run: .github/cmake.sh -D CMAKE_TOOLCHAIN_FILE=C:/vcpkg/scripts/buildsystems/vcpkg.cmake + - run: | + .github/cmake.sh \ + -D CMAKE_TOOLCHAIN_FILE=C:/vcpkg/scripts/buildsystems/vcpkg.cmake + shell: bash + build-windows-shared: + runs-on: windows-latest + strategy: + fail-fast: false + steps: + - uses: actions/checkout@v3 + - name: Install Abseil, GoogleTest and Benchmark + run: | + # TODO: Remove this in September 2023, which is when the default triplet for + # vcpkg libraries will change from x86-windows to the detected host triplet. + export VCPKG_DEFAULT_TRIPLET=x64-windows + vcpkg update + vcpkg install abseil gtest benchmark + shell: bash + - run: | + .github/cmake.sh -D BUILD_SHARED_LIBS=ON \ + -D CMAKE_TOOLCHAIN_FILE=C:/vcpkg/scripts/buildsystems/vcpkg.cmake shell: bash diff --git a/CMakeLists.txt b/CMakeLists.txt index 473d2e660..393230496 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -183,6 +183,9 @@ if(RE2_BUILD_TESTING) ) add_library(testing ${TESTING_SOURCES}) + if(BUILD_SHARED_LIBS AND WIN32) + target_compile_definitions(testing -DRE2_BUILD_TESTING_DLL) + endif() target_compile_features(testing PUBLIC cxx_std_14) target_link_libraries(testing PUBLIC re2 GTest::gtest) @@ -216,6 +219,9 @@ if(RE2_BUILD_TESTING) foreach(target ${TEST_TARGETS}) add_executable(${target} re2/testing/${target}.cc) + if(BUILD_SHARED_LIBS AND WIN32) + target_compile_definitions(${target} -DRE2_CONSUME_TESTING_DLL) + endif() target_compile_features(${target} PUBLIC cxx_std_14) target_link_libraries(${target} PUBLIC testing GTest::gtest_main ${EXTRA_TARGET_LINK_LIBRARIES}) add_test(NAME ${target} COMMAND ${target}) @@ -223,6 +229,9 @@ if(RE2_BUILD_TESTING) foreach(target ${BENCHMARK_TARGETS}) add_executable(${target} re2/testing/${target}.cc) + if(BUILD_SHARED_LIBS AND WIN32) + target_compile_definitions(${target} -DRE2_CONSUME_TESTING_DLL) + endif() target_compile_features(${target} PUBLIC cxx_std_14) target_link_libraries(${target} PUBLIC testing benchmark::benchmark_main ${EXTRA_TARGET_LINK_LIBRARIES}) endforeach() diff --git a/util/pcre.h b/util/pcre.h index 16595cab3..846f30019 100644 --- a/util/pcre.h +++ b/util/pcre.h @@ -175,6 +175,16 @@ const bool UsingPCRE = false; } // namespace re2 #endif +// To produce a DLL, CMake can automatically export code symbols, +// but not data symbols, so we have to annotate those manually... +#if defined(RE2_BUILD_TESTING_DLL) +#define RE2_TESTING_DLL __declspec(dllexport) +#elif defined(RE2_CONSUME_TESTING_DLL) +#define RE2_TESTING_DLL __declspec(dllimport) +#else +#define RE2_TESTING_DLL +#endif + namespace re2 { class PCRE_Options; @@ -190,7 +200,7 @@ class PCRE { // Marks end of arg list. // ONLY USE IN OPTIONAL ARG DEFAULTS. // DO NOT PASS EXPLICITLY. - static Arg no_more_args; + RE2_TESTING_DLL static Arg no_more_args; // Options are same value as those in pcre. We provide them here // to avoid users needing to include pcre.h and also to isolate @@ -285,7 +295,7 @@ class PCRE { const Arg& ptr16 = no_more_args) const; }; - static const FullMatchFunctor FullMatch; + RE2_TESTING_DLL static const FullMatchFunctor FullMatch; // Exactly like FullMatch(), except that "pattern" is allowed to match // a substring of "text". @@ -309,7 +319,7 @@ class PCRE { const Arg& ptr16 = no_more_args) const; }; - static const PartialMatchFunctor PartialMatch; + RE2_TESTING_DLL static const PartialMatchFunctor PartialMatch; // Like FullMatch() and PartialMatch(), except that pattern has to // match a prefix of "text", and "input" is advanced past the matched @@ -334,7 +344,7 @@ class PCRE { const Arg& ptr16 = no_more_args) const; }; - static const ConsumeFunctor Consume; + RE2_TESTING_DLL static const ConsumeFunctor Consume; // Like Consume(..), but does not anchor the match at the beginning of the // string. That is, "pattern" need not start its match at the beginning of @@ -360,7 +370,7 @@ class PCRE { const Arg& ptr16 = no_more_args) const; }; - static const FindAndConsumeFunctor FindAndConsume; + RE2_TESTING_DLL static const FindAndConsumeFunctor FindAndConsume; // Replace the first match of "pattern" in "str" with "rewrite". // Within "rewrite", backslash-escaped digits (\1 to \9) can be