From 313d566fe5d3be603ea4f6dbfd2af98c8bf6c00c Mon Sep 17 00:00:00 2001 From: mengning997 Date: Mon, 5 Sep 2022 15:04:30 +0800 Subject: [PATCH 1/3] support cmake --- CMakeLists.txt | 161 ++++++++++++++++++++++++++++++++++++++ Makefile | 11 ++- README.md | 11 +++ re2/re2.cc | 1 + regex-capi/CMakeLists.txt | 22 ++++++ regex-capi/Cargo.toml | 2 +- 6 files changed, 203 insertions(+), 5 deletions(-) create mode 100644 CMakeLists.txt create mode 100644 regex-capi/CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..20f47ce --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,161 @@ +# Copyright 2015 The RE2 Authors. All Rights Reserved. +# Use of this source code is governed by a BSD-style +# license that can be found in the LICENSE file. + +# Old enough to support Ubuntu Xenial. +cmake_minimum_required(VERSION 3.5.1) + +project(RE2 CXX) +include(CTest) +include(GNUInstallDirs) + +if(NOT CMAKE_CXX_STANDARD) + set(CMAKE_CXX_STANDARD 11) + set(CMAKE_CXX_STANDARD_REQUIRED ON) +endif() + +option(BUILD_SHARED_LIBS "build shared libraries" ON) +option(USEPCRE "use PCRE in tests and benchmarks" OFF) + +# CMake seems to have no way to enable/disable testing per subproject, +# so we provide an option similar to BUILD_TESTING, but just for RE2. +option(RE2_BUILD_TESTING "enable testing for RE2" OFF) + +# ABI version +# http://tldp.org/HOWTO/Program-Library-HOWTO/shared-libraries.html +set(SONAME 9) + +set(EXTRA_TARGET_LINK_LIBRARIES) + +if(CMAKE_CXX_COMPILER_ID MATCHES "MSVC") + if(MSVC_VERSION LESS 1900) + message(FATAL_ERROR "you need Visual Studio 2015 or later") + endif() + if(BUILD_SHARED_LIBS) + # See http://www.kitware.com/blog/home/post/939 for details. + set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON) + endif() + # CMake defaults to /W3, but some users like /W4 (or /Wall) and /WX, + # so we disable various warnings that aren't particularly helpful. + add_compile_options(/wd4100 /wd4201 /wd4456 /wd4457 /wd4702 /wd4815) + # Without a byte order mark (BOM), Visual Studio assumes that the source + # file is encoded using the current user code page, so we specify UTF-8. + add_compile_options(/utf-8) +endif() + +if(WIN32) + add_definitions(-DUNICODE -D_UNICODE -DSTRICT -DNOMINMAX) + add_definitions(-D_CRT_SECURE_NO_WARNINGS -D_SCL_SECURE_NO_WARNINGS) +elseif(UNIX) + set(THREADS_PREFER_PTHREAD_FLAG ON) + find_package(Threads REQUIRED) +endif() + +if(USEPCRE) + add_definitions(-DUSEPCRE) + list(APPEND EXTRA_TARGET_LINK_LIBRARIES pcre) +endif() + +add_subdirectory(regex-capi) +include_directories( + ${CMAKE_CURRENT_SOURCE_DIR}/regex-capi/include +) +link_directories(${PROJECT_BINARY_DIR}/target/release) + +set(RE2_SOURCES + re2/filtered_re2.cc + re2/re2.cc + re2/set.cc + re2/stringpiece.cc + util/rune.cc + util/strutil.cc + ) + +add_library(re2 ${RE2_SOURCES}) +target_include_directories(re2 PUBLIC $) +set_target_properties(re2 PROPERTIES SOVERSION ${SONAME} VERSION ${SONAME}.0.0) +add_library(re2::re2 ALIAS re2) + +add_dependencies(re2 librure) +target_link_libraries(re2 PRIVATE rure) + +if(UNIX) + target_link_libraries(re2 PUBLIC Threads::Threads) +endif() + +if(RE2_BUILD_TESTING) + set(TESTING_SOURCES + re2/testing/backtrack.cc + re2/testing/dump.cc + re2/testing/exhaustive_tester.cc + re2/testing/null_walker.cc + re2/testing/regexp_generator.cc + re2/testing/string_generator.cc + re2/testing/tester.cc + util/pcre.cc + ) + + add_library(testing STATIC ${TESTING_SOURCES}) + target_link_libraries(testing PUBLIC re2) + + set(TEST_TARGETS + charclass_test + compile_test + filtered_re2_test + mimics_pcre_test + parse_test + possible_match_test + re2_test + re2_arg_test + regexp_test + required_prefix_test + search_test + set_test + simplify_test + string_generator_test + + dfa_test + exhaustive1_test + exhaustive2_test + exhaustive3_test + exhaustive_test + random_test + ) + + set(BENCHMARK_TARGETS + regexp_benchmark + ) + + foreach(target ${TEST_TARGETS}) + add_executable(${target} re2/testing/${target}.cc util/test.cc) + target_link_libraries(${target} testing ${EXTRA_TARGET_LINK_LIBRARIES}) + add_test(NAME ${target} COMMAND ${target}) + endforeach() + + foreach(target ${BENCHMARK_TARGETS}) + add_executable(${target} re2/testing/${target}.cc util/benchmark.cc) + target_link_libraries(${target} testing ${EXTRA_TARGET_LINK_LIBRARIES}) + endforeach() +endif() + +set(RE2_HEADERS + re2/filtered_re2.h + re2/re2.h + re2/set.h + re2/stringpiece.h + ) + +install(FILES ${RE2_HEADERS} + DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/re2) +install(TARGETS re2 EXPORT re2Config + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} + INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) +# install(EXPORT re2Config +# DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/re2 NAMESPACE re2::) + +add_executable(testinstall testinstall.cc) +link_directories(/usr/local/lib/) +# add_dependencies(testinstall libre2) +target_link_libraries(testinstall re2) diff --git a/Makefile b/Makefile index f9c2807..ea3026f 100644 --- a/Makefile +++ b/Makefile @@ -316,17 +316,20 @@ shared: obj/so/libre2.$(SOEXT) shared-install: obj/so/libre2.$(SOEXT) common-install $(INSTALL) regex-capi/include/rure.h /usr/include/rure.h $(INSTALL) target/release/librure.so /usr/lib/librure.so + $(INSTALL) obj/so/libre2.$(SOEXT) $(DESTDIR)$(libdir)/libre2rust.$(SOEXTVER00) + ln -sf libre2rust.$(SOEXTVER00) $(DESTDIR)$(libdir)/libre2rust.$(SOEXTVER) + ln -sf libre2rust.$(SOEXTVER00) $(DESTDIR)$(libdir)/libre2rust.$(SOEXT) $(INSTALL) obj/so/libre2.$(SOEXT) $(DESTDIR)$(libdir)/libre2.$(SOEXTVER00) ln -sf libre2.$(SOEXTVER00) $(DESTDIR)$(libdir)/libre2.$(SOEXTVER) ln -sf libre2.$(SOEXTVER00) $(DESTDIR)$(libdir)/libre2.$(SOEXT) .PHONY: common-install common-install: - mkdir -p $(DESTDIR)$(includedir)/re2 $(DESTDIR)$(libdir)/pkgconfig + # mkdir -p $(DESTDIR)$(includedir)/re2 $(DESTDIR)$(libdir)/pkgconfig $(INSTALL_DATA) $(INSTALL_HFILES) $(DESTDIR)$(includedir)/re2 - $(INSTALL_DATA) re2.pc $(DESTDIR)$(libdir)/pkgconfig/re2.pc - $(SED_INPLACE) -e "s#@includedir@#$(includedir)#" $(DESTDIR)$(libdir)/pkgconfig/re2.pc - $(SED_INPLACE) -e "s#@libdir@#$(libdir)#" $(DESTDIR)$(libdir)/pkgconfig/re2.pc + # $(INSTALL_DATA) re2.pc $(DESTDIR)$(libdir)/pkgconfig/re2.pc + # $(SED_INPLACE) -e "s#@includedir@#$(includedir)#" $(DESTDIR)$(libdir)/pkgconfig/re2.pc + # $(SED_INPLACE) -e "s#@libdir@#$(libdir)#" $(DESTDIR)$(libdir)/pkgconfig/re2.pc .PHONY: testinstall testinstall: static-testinstall shared-testinstall diff --git a/README.md b/README.md index 58513ad..5cdf275 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,17 @@ $ cd re2-rust ### 编译、安装re2-rust +``` Shell +$ mkdir build && cd build +$ cmake .. +$ make --release +$ sudo make install +$ ./testinstall +``` + +or + + ``` Shell $ make $ sudo make install diff --git a/re2/re2.cc b/re2/re2.cc index 2b61522..e2e24a4 100644 --- a/re2/re2.cc +++ b/re2/re2.cc @@ -266,6 +266,7 @@ namespace re2 // 问题??? // rure_free和rure_captures_free是否要进行使用? // error_code_如何进行赋值,RegexpErrorToRE2删除了??? + error_code_ = RE2::NoError; // rure_free(re); } diff --git a/regex-capi/CMakeLists.txt b/regex-capi/CMakeLists.txt new file mode 100644 index 0000000..438b574 --- /dev/null +++ b/regex-capi/CMakeLists.txt @@ -0,0 +1,22 @@ +# check rust package manager +find_program(RUST_C rustc) + +if(NOT RUST_C) + message(FATAL_ERROR "Rust compiler 'rustc' not found.") +else() + execute_process(COMMAND ${RUST_C} --version OUTPUT_VARIABLE RUST_C_VERSION) + string(STRIP ${RUST_C_VERSION} RUST_C_VERSION) + message("-- Found rust compiler: ${RUST_C_VERSION}") +endif() + +# configure_file(${CMAKE_CURRENT_SOURCE_DIR}/.cargo/config.in ${CMAKE_CURRENT_SOURCE_DIR}/.cargo/config) + +# if(CMAKE_BUILD_TYPE MATCHES "^Release") + list(APPEND CARGO_BUILD_OPT "--release") +# endif() + +add_custom_target(librure ALL + # create rust regex library + COMMAND cargo build -v ${CARGO_BUILD_OPT} + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} +) diff --git a/regex-capi/Cargo.toml b/regex-capi/Cargo.toml index c9d971b..f25dd38 100644 --- a/regex-capi/Cargo.toml +++ b/regex-capi/Cargo.toml @@ -15,7 +15,7 @@ edition = "2018" [lib] name = "rure" -crate-type = ["staticlib", "cdylib"] +crate-type = ["staticlib"] [dependencies] libc = "0.2" -- Gitee From b356bee2bf3e6df379e3f3f06ff56f08117e0065 Mon Sep 17 00:00:00 2001 From: mengning997 Date: Mon, 5 Sep 2022 15:21:58 +0800 Subject: [PATCH 2/3] support cmake --- .gitignore | 4 ++++ CMakeLists.txt | 8 ++++---- README.md | 3 ++- regex-capi/CMakeLists.txt | 4 ++-- 4 files changed, 12 insertions(+), 7 deletions(-) diff --git a/.gitignore b/.gitignore index e43b0f9..5e221d8 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,5 @@ .DS_Store +build +target +Cargo.lock + diff --git a/CMakeLists.txt b/CMakeLists.txt index 20f47ce..4e4424f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -150,12 +150,12 @@ install(FILES ${RE2_HEADERS} install(TARGETS re2 EXPORT re2Config ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} - RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} + # RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) # install(EXPORT re2Config # DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/re2 NAMESPACE re2::) -add_executable(testinstall testinstall.cc) -link_directories(/usr/local/lib/) +# add_executable(testinstall testinstall.cc) +# link_directories(/usr/local/lib/) # add_dependencies(testinstall libre2) -target_link_libraries(testinstall re2) +# target_link_libraries(testinstall re2) diff --git a/README.md b/README.md index 5cdf275..a178753 100644 --- a/README.md +++ b/README.md @@ -14,8 +14,9 @@ $ cd re2-rust ``` Shell $ mkdir build && cd build $ cmake .. -$ make --release +$ make $ sudo make install +$ g++ ../testinstall.cc -o testinstall -lre2 $ ./testinstall ``` diff --git a/regex-capi/CMakeLists.txt b/regex-capi/CMakeLists.txt index 438b574..7d50d3a 100644 --- a/regex-capi/CMakeLists.txt +++ b/regex-capi/CMakeLists.txt @@ -12,11 +12,11 @@ endif() # configure_file(${CMAKE_CURRENT_SOURCE_DIR}/.cargo/config.in ${CMAKE_CURRENT_SOURCE_DIR}/.cargo/config) # if(CMAKE_BUILD_TYPE MATCHES "^Release") - list(APPEND CARGO_BUILD_OPT "--release") + # list(APPEND CARGO_BUILD_OPT "--release") # endif() add_custom_target(librure ALL # create rust regex library - COMMAND cargo build -v ${CARGO_BUILD_OPT} + COMMAND cargo build -v --release WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} ) -- Gitee From 7ffd86a2ca07c7f33843866df7e026d86f3614e9 Mon Sep 17 00:00:00 2001 From: mengning997 Date: Mon, 5 Sep 2022 15:45:29 +0800 Subject: [PATCH 3/3] make test works --- .gitignore | 2 ++ Makefile | 2 +- re2/re2.cc | 2 +- regex-capi/Cargo.toml | 2 +- 4 files changed, 5 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 5e221d8..e734ef7 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,6 @@ build target Cargo.lock +obj/ +regex-capi/.Cargo.toml.swp diff --git a/Makefile b/Makefile index ea3026f..3643ede 100644 --- a/Makefile +++ b/Makefile @@ -15,7 +15,7 @@ CXX?=g++ # can override CXXFLAGS?=-O3 -g -LDFLAGS?=-lrure +LDFLAGS?=-lrure -ldl # required RE2_CXXFLAGS?=-std=c++11 -pthread -Wall -Wextra -Wno-unused-parameter -Wno-missing-field-initializers -I. $(CCICU) $(CCPCRE) RE2_LDFLAGS?=-pthread $(LDICU) $(LDPCRE) diff --git a/re2/re2.cc b/re2/re2.cc index e2e24a4..0ab23c0 100644 --- a/re2/re2.cc +++ b/re2/re2.cc @@ -266,7 +266,7 @@ namespace re2 // 问题??? // rure_free和rure_captures_free是否要进行使用? // error_code_如何进行赋值,RegexpErrorToRE2删除了??? - error_code_ = RE2::NoError; + // error_code_ = RE2::NoError; // rure_free(re); } diff --git a/regex-capi/Cargo.toml b/regex-capi/Cargo.toml index f25dd38..c9d971b 100644 --- a/regex-capi/Cargo.toml +++ b/regex-capi/Cargo.toml @@ -15,7 +15,7 @@ edition = "2018" [lib] name = "rure" -crate-type = ["staticlib"] +crate-type = ["staticlib", "cdylib"] [dependencies] libc = "0.2" -- Gitee