Skip to content

Commit

Permalink
Provide a bundled flag for gdal-sys
Browse files Browse the repository at this point in the history
This commit introduces a new `gdal-src` crate which bundles gdal and
builds a minimal version from source via `build.rs`

Fixes georust#465
  • Loading branch information
weiznich committed Jan 25, 2024
1 parent 87497bf commit cd7f55c
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "gdal-src/source"]
path = gdal-src/source
url = https://github.com/OSGeo/gdal
8 changes: 7 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,17 @@ tempfile = "3.8"
arrow2 = "0.18"

[workspace]
members = ["gdal-sys"]
members = [ "gdal-src","gdal-sys"]

# docs.rs-specific configuration
[package.metadata.docs.rs]
# include `array` feature in documentation
features = ["array"]
# define attribute `docsrs` for feature badges
rustdoc-args = ["--cfg", "docsrs"]


[patch.crates-io]
proj-sys = { git = "https://github.com/GiGainfosystems/proj", rev = "73797d3b4601b5770b16f0e1cdb656d51295b471" }
libsqlite3-sys = { git = "https://github.com/GiGainfosystems/rusqlite/", rev = "e60d993" }

13 changes: 13 additions & 0 deletions gdal-src/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "gdal-src"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
proj-sys = { version = "0.23.2", features = ["bundled_proj"] }
libsqlite3-sys = { version = "0.27.0", features = ["bundled"] }

[build-dependencies]
cmake = "0.1.50"
46 changes: 46 additions & 0 deletions gdal-src/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
fn main() {
let sqlite3_include_dir =
std::env::var("DEP_SQLITE3_INCLUDE").expect("This is set by libsqlite3-sys");
let sqlite3_lib_dir = std::env::var("DEP_SQLITE3_LIB_DIR").expect("set by libsqlite3-sys");
let proj_root = std::env::var("DEP_PROJ_ROOT").expect("set by proj-sys");

let res = cmake::Config::new("source")
.define("GDAL_BUILD_OPTIONAL_DRIVERS", "OFF")
.define("OGR_BUILD_OPTIONAL_DRIVERS", "OFF")
.define("GDAL_USE_INTERNAL_LIBS", "ON")
.define("GDAL_USE_EXTERNAL_LIBS", "OFF")
.define("BUILD_SHARED_LIBS", "OFF")
.define("BUILD_STATIC_LIBS", "ON")
.define("BUILD_APPS", "OFF")
.define("BUILD_DOCS", "OFF")
.define("BUILD_TESTING", "OFF")
.define("BUILD_GMOCK", "OFF")
.define("PROJ_INCLUDE_DIRS", format!("{proj_root}/include"))
.define("PROJ_LIBRARY_DIRS", format!("{proj_root}/lib64/"))
// enable the gpkg driver
.define("GDAL_USE_SQLITE3", "ON")
.define("SQLite3_INCLUDE_DIR", sqlite3_include_dir)
.define("SQLite3_LIBRARY", format!("{sqlite3_lib_dir}/libsqlite3.a"))
.define("OGR_ENABLE_DRIVER_GPKG", "ON")
.pic(true)
.build();

// sometimes it's lib and sometimes it's lib64 and sometimes `build/lib`
let lib_dir = res.join("lib64");
println!(
"cargo:rustc-link-search=native={}",
lib_dir.to_str().unwrap()
);
let lib_dir = res.join("lib");
println!(
"cargo:rustc-link-search=native={}",
lib_dir.to_str().unwrap()
);
let lib_dir = res.join("build/lib");
println!(
"cargo:rustc-link-search=native={}",
lib_dir.to_str().unwrap()
);

println!("cargo:rustc-link-lib=static={}", "gdal");
}
1 change: 1 addition & 0 deletions gdal-src/source
Submodule source added at 654f49
2 changes: 2 additions & 0 deletions gdal-src/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
extern crate libsqlite3_sys;
extern crate proj_sys;
6 changes: 6 additions & 0 deletions gdal-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,14 @@ links="gdal"

[dependencies]
libc = "0.2"
gdal-src = { path = "../gdal-src/", optional = true}

[build-dependencies]
bindgen = { version = "0.69", optional = true }
pkg-config = "0.3"
semver = "1.0"


[features]
default = []
bundled = ["dep:gdal-src"]
2 changes: 1 addition & 1 deletion gdal-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ fn main() {

// Hardcode a prebuilt binding version while generating docs.
// Otherwise docs.rs will explode due to not actually having libgdal installed.
if std::env::var("DOCS_RS").is_ok() {
if std::env::var("DOCS_RS").is_ok() || cfg!(feature = "bundled") {
let version = Version::parse("3.8.0").expect("invalid version for docs.rs");
println!(
"cargo:rustc-cfg=gdal_sys_{}_{}_{}",
Expand Down
3 changes: 3 additions & 0 deletions gdal-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@
#![allow(clippy::upper_case_acronyms)]
#![allow(rustdoc::bare_urls)]

#[cfg(feature = "bundled")]
extern crate gdal_src;

include!(concat!(env!("OUT_DIR"), "/bindings.rs"));

0 comments on commit cd7f55c

Please sign in to comment.