Skip to content

Commit

Permalink
Release 1.2 version, and add more features to sys
Browse files Browse the repository at this point in the history
  • Loading branch information
Davichet-e committed Oct 17, 2024
1 parent 5f6fcda commit f868df4
Show file tree
Hide file tree
Showing 8 changed files with 8,598 additions and 4,740 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "sys/MobilityDB"]
path = sys/MobilityDB
url = https://github.com/MobilityDB/MobilityDB.git
13 changes: 11 additions & 2 deletions sys/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "meos-sys"
version = "0.1.4"
version = "0.1.3"
authors = ["David García Morillo <[email protected]>"]

repository = "https://github.com/MobilityDB/RustMEOS"
Expand All @@ -16,4 +16,13 @@ links = "meos"
[dependencies]

[build-dependencies]
pkg-config = "0.3.30"
pkg-config = "0.3.30"
cmake = "0.1"
bindgen = { version = "0.70.1", optional = true}

[features]
default = ["v1_2"]
v1_1 = []
v1_2 = []
buildtime_bindgen = ["dep:bindgen"]
bundled_proj = ["buildtime_bindgen"]
1 change: 1 addition & 0 deletions sys/MobilityDB
Submodule MobilityDB added at 60048b
11 changes: 9 additions & 2 deletions sys/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Currently the bindings are generated manually using bindgen. The commit of the r

## Build

By default, the build will use system-installed MEOS if available, `pkg-config` is used to automatically detect MEOS
The build by default will use system-installed MEOS, `pkg-config` is used to automatically detect MEOS

If MEOS is in a custom location, you can instead use the `MEOS_LIB_DIR` environment variable to
configure MEOS detection.
Expand All @@ -35,6 +35,13 @@ DYLD_FALLBACK_LIBRARY_PATH=<path to MEOS>/lib MEOS_LIB_DIR=<path to MEOS>/lib ME

```

---

You can also enable the `bundled_proj` feature to build MEOS from scratch, (note that you will need to already have installed `json-c`, `proj`, and `geos` in your system, see [here](https://github.com/MobilityDB/MobilityDB/?tab=readme-ov-file#requirements)).

## Binding generation
By default, meos-sys will use the pregenerated bindings for the 1.2 version, the 1.1 ones is also available. Alternatively, you can generate your own bindings from your `libmeos` installation by specifying the `bindgen` feature.

## Bindings

Pre-built bindings are available for 1.1
Pre-built bindings are available for 1.2 and 1.1
81 changes: 65 additions & 16 deletions sys/build.rs
Original file line number Diff line number Diff line change
@@ -1,35 +1,84 @@
use std::env;
use std::ffi::OsString;
use std::path::PathBuf;

/// Detect MEOS config parameters using pkg-config (not available for all GEOS
/// versions)
fn detect_meos_via_pkg_config() -> bool {
fn detect_meos_via_pkg_config() -> Option<PathBuf> {
use pkg_config::Config;

let meos_pkg_config = Config::new().probe("meos");

meos_pkg_config.is_ok()
meos_pkg_config.map(|pk| pk.include_paths[0].clone()).ok()
}

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

let success = detect_meos_via_pkg_config();
// If `bundled_proj` is on, use the git submodule to build MEOS from scratch
let include_path = if cfg!(feature = "bundled_proj") {
let dst = cmake::Config::new("MobilityDB").define("MEOS", "1").build();

// Try to find the library manually
if !success {
let lib_dir_env = env::var_os("MEOS_LIB_DIR")
.map(OsString::into_string)
.transpose()
.ok()
.flatten()
.unwrap_or(String::from("/usr/local/lib/"));

// Tell cargo to look for shared libraries in the specified directory
println!("cargo:rustc-link-search={lib_dir_env}");
println!(
"cargo:rustc-link-search=dylib={}",
dst.join("lib").display()
);

// Tell cargo to tell rustc to link the system meos shared library.
println!("cargo:rustc-link-lib=dylib=meos");
}
println!("cargo:rustc-link-lib=meos");

dst.join("lib")
// Else use pkg-config, using a default as a fallback
} else {
let pk_include_path = detect_meos_via_pkg_config();

// Try to find the library manually
if !pk_include_path.is_some() {
let default_include_path = String::from("/usr/local/lib/");
let lib_dir_env = env::var_os("MEOS_LIB_DIR")
.map(OsString::into_string)
.transpose()
.ok()
.flatten()
.unwrap_or(default_include_path.clone());

// Tell cargo to look for shared libraries in the specified directory
println!("cargo:rustc-link-search={lib_dir_env}");

// Tell cargo to tell rustc to link the system meos shared library.
println!("cargo:rustc-link-lib=dylib=meos");
PathBuf::from(default_include_path)
} else {
pk_include_path.unwrap()
}
};

#[cfg(feature = "buildtime_bindgen")]
generate_bindings(include_path).unwrap();

#[cfg(not(feature = "buildtime_bindgen"))]
let _ = include_path;
}

#[cfg(feature = "buildtime_bindgen")]
fn generate_bindings(include_path: std::path::PathBuf) -> Result<(), Box<dyn std::error::Error>> {
// The bindgen::Builder is the main entry point
// to bindgen, and lets you build up options for
// the resulting bindings.
let bindings = bindgen::Builder::default()
.clang_arg(format!("-I{}", include_path.to_string_lossy()))
// The input header we would like to generate
// bindings for.
.header("wrapper.h")
// Finish the builder and generate the bindings.
.generate()
// Unwrap the Result and panic on failure.
.expect("Unable to generate bindings");

// Write the bindings to the $OUT_DIR/bindings.rs file.
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
bindings.write_to_file(out_path.join("bindings.rs"))?;

Ok(())
}
Loading

0 comments on commit f868df4

Please sign in to comment.