Skip to content

Commit

Permalink
Add tests with re2 and compiler feature detection.
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexhuszagh committed Nov 6, 2022
1 parent f35af56 commit 1702cfd
Show file tree
Hide file tree
Showing 11 changed files with 79 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "re2"]
path = re2
url = https://github.com/google/re2/
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,9 @@ edition = "2021"
build = "src/build.rs"

[build-dependencies]
cc = "1.0.73"
cmake = "0.1"

[features]
std = []
re2 = ["std"]
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

> Minimal example of using CMake from Rust
This tests compiler feature detection, as well as more complicated examples with thread support when the `re2` feature is enabled.

## License

Licensed under either of
Expand Down
2 changes: 2 additions & 0 deletions libsum/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ project(sum)
add_library(sum sum.c)
target_include_directories(sum PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
install(TARGETS sum DESTINATION lib)

target_compile_features(sum PUBLIC cxx_std_11)
9 changes: 9 additions & 0 deletions match.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include <re2/re2.h>

extern "C" {
bool is_match(const char* pattern, const char* string) {
re2::StringPiece regex(pattern);
re2::StringPiece input(string);
return re2::RE2::FullMatch(input, regex);
}
}
1 change: 1 addition & 0 deletions re2
Submodule re2 added at 5723bb
12 changes: 12 additions & 0 deletions src/bin/hello.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
#[cfg(feature = "re2")]
use rust_cmake_hello_world::hello_is_match;
use rust_cmake_hello_world::multiply_add;

pub fn main() {
println!("multiply_add(2, 3, 4)={:?}", multiply_add(2, 3, 4));
#[cfg(feature = "re2")]
{
let pattern = "Hello.*";
let input = "Hello World";
let matches = match hello_is_match(pattern, input) {
true => "matches",
false => "does not match",
};
println!("{pattern:?} {matches} the input {input:?}");
}
}
23 changes: 23 additions & 0 deletions src/build.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,30 @@
use cmake;

fn main() {
println!("cargo:rerun-if-changed=src/build.rs");

build_sum();
build_re2();
}

fn build_sum() {
let dst = cmake::build("libsum");
println!("cargo:rustc-link-search=native={}/lib", dst.display());
println!("cargo:rustc-link-lib=static=sum");
}

fn build_re2() {
if cfg!(feature = "re2") {
let dst = cmake::build("re2");

cc::Build::new()
.cpp(true)
.file("match.cc")
.include(dst.join("include"))
.compile("libmatch.a");

println!("cargo:rerun-if-changed=match.cc");
println!("cargo:rustc-link-search=native={}/build", dst.display());
println!("cargo:rustc-link-lib=static=re2");
}
}
8 changes: 7 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
#![no_std]
#![cfg_attr(not(feature = "std"), no_std)]

#[cfg(feature = "re2")]
mod re2;

#[cfg(feature = "re2")]
pub use self::re2::hello_is_match;

pub fn multiply_add(x: i32, y: i32, z: i32) -> i32 {
x * unsafe { sum(y, z) }
Expand Down
14 changes: 14 additions & 0 deletions src/re2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#![cfg(feature = "re2")]

use std::ffi::CString;
use std::os::raw::c_char;

pub fn hello_is_match(pattern: &str, input: &str) -> bool {
let pattern = CString::new(pattern).unwrap();
let input = CString::new(input).unwrap();
unsafe { is_match(pattern.as_ptr(), input.as_ptr()) }
}

extern "C" {
fn is_match(pattern: *const c_char, string: *const c_char) -> bool;
}

0 comments on commit 1702cfd

Please sign in to comment.