Skip to content

Commit a30248e

Browse files
committed
Provide a bundled flag for gdal-sys
This commit introduces a new `gdal-src` crate which bundles gdal and builds a minimal version from source via `build.rs` Fixes #465
1 parent 87497bf commit a30248e

File tree

10 files changed

+94
-2
lines changed

10 files changed

+94
-2
lines changed

.gitmodules

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "gdal-src/source"]
2+
path = gdal-src/source
3+
url = https://github.com/OSGeo/gdal

CHANGES.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Changes
22

33
## Unreleased
4+
- Add a `bundled` feature for `gdal-sys` that allows to build and statically link a minimal bundled version of gdal during `cargo build`
45
- Add `DriverIterator` format to iterate through drivers, as well as `DriverManager::all()` method that provides the iterator.
56
- <https://github.com/georust/gdal/pull/512>
67

Cargo.toml

+6-1
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,16 @@ tempfile = "3.8"
3636
arrow2 = "0.18"
3737

3838
[workspace]
39-
members = ["gdal-sys"]
39+
members = [ "gdal-src","gdal-sys"]
4040

4141
# docs.rs-specific configuration
4242
[package.metadata.docs.rs]
4343
# include `array` feature in documentation
4444
features = ["array"]
4545
# define attribute `docsrs` for feature badges
4646
rustdoc-args = ["--cfg", "docsrs"]
47+
48+
49+
[patch.crates-io]
50+
proj-sys = { git = "https://github.com/GiGainfosystems/proj", rev = "54716dd8955d4f0561ce9bf8a83610b605e3c007" }
51+
libsqlite3-sys = { git = "https://github.com/rusqlite/rusqlite/", rev = "42a82fb" }

gdal-src/Cargo.toml

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[package]
2+
name = "gdal-src"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[dependencies]
9+
proj-sys = { version = "0.23.2", features = ["bundled_proj"] }
10+
libsqlite3-sys = { version = "0.27.0", features = ["bundled"] }
11+
12+
[build-dependencies]
13+
cmake = "0.1.50"

gdal-src/build.rs

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
fn main() {
2+
let sqlite3_include_dir =
3+
std::env::var("DEP_SQLITE3_INCLUDE").expect("This is set by libsqlite3-sys");
4+
let sqlite3_lib_dir = std::env::var("DEP_SQLITE3_LIB_DIR").expect("set by libsqlite3-sys");
5+
let proj_root = PathBuf::from(std::env::var("DEP_PROJ_ROOT").expect("set by proj-sys"));
6+
let proj_lib = if proj_root.join("lib").join("proj_d.lib").exists() {
7+
"proj_d.lib"
8+
} else if proj_root.join("lib").join("proj.lib").exists() {
9+
"proj.lib"
10+
} else {
11+
"libproj.a"
12+
};
13+
14+
let res = cmake::Config::new("source")
15+
.define("GDAL_BUILD_OPTIONAL_DRIVERS", "OFF")
16+
.define("OGR_BUILD_OPTIONAL_DRIVERS", "OFF")
17+
.define("GDAL_USE_INTERNAL_LIBS", "ON")
18+
.define("GDAL_USE_EXTERNAL_LIBS", "OFF")
19+
.define("BUILD_SHARED_LIBS", "OFF")
20+
.define("BUILD_STATIC_LIBS", "ON")
21+
.define("BUILD_APPS", "OFF")
22+
.define("BUILD_DOCS", "OFF")
23+
.define("BUILD_TESTING", "OFF")
24+
.define("BUILD_GMOCK", "OFF")
25+
.define("PROJ_INCLUDE_DIR", format!("{proj_root}/include"))
26+
.define("PROJ_LIBRARY", format!("{proj_root}/lib/{proj_lib}"))
27+
// enable the gpkg driver
28+
.define("GDAL_USE_SQLITE3", "ON")
29+
.define("SQLite3_INCLUDE_DIR", sqlite3_include_dir)
30+
.define("SQLite3_LIBRARY", format!("{sqlite3_lib_dir}/libsqlite3.a"))
31+
.define("OGR_ENABLE_DRIVER_GPKG", "ON")
32+
.pic(true)
33+
.build();
34+
35+
// sometimes it's lib and sometimes it's lib64 and sometimes `build/lib`
36+
let lib_dir = res.join("lib64");
37+
println!(
38+
"cargo:rustc-link-search=native={}",
39+
lib_dir.to_str().unwrap()
40+
);
41+
let lib_dir = res.join("lib");
42+
println!(
43+
"cargo:rustc-link-search=native={}",
44+
lib_dir.to_str().unwrap()
45+
);
46+
let lib_dir = res.join("build/lib");
47+
println!(
48+
"cargo:rustc-link-search=native={}",
49+
lib_dir.to_str().unwrap()
50+
);
51+
52+
//gdal likes to create gdal_d when configured as debug and on MSVC, so link to that one if it exists
53+
if res.join("lib").join("gdald.lib").exists() {
54+
println!("cargo:rustc-link-lib=static=gdald");
55+
} else {
56+
println!("cargo:rustc-link-lib=static=gdal");
57+
}
58+
}

gdal-src/source

Submodule source added at 654f490

gdal-src/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
extern crate libsqlite3_sys;
2+
extern crate proj_sys;

gdal-sys/Cargo.toml

+6
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,14 @@ links="gdal"
1212

1313
[dependencies]
1414
libc = "0.2"
15+
gdal-src = { path = "../gdal-src/", optional = true}
1516

1617
[build-dependencies]
1718
bindgen = { version = "0.69", optional = true }
1819
pkg-config = "0.3"
1920
semver = "1.0"
21+
22+
23+
[features]
24+
default = []
25+
bundled = ["dep:gdal-src"]

gdal-sys/build.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ fn main() {
6868

6969
// Hardcode a prebuilt binding version while generating docs.
7070
// Otherwise docs.rs will explode due to not actually having libgdal installed.
71-
if std::env::var("DOCS_RS").is_ok() {
71+
if std::env::var("DOCS_RS").is_ok() || cfg!(feature = "bundled") {
7272
let version = Version::parse("3.8.0").expect("invalid version for docs.rs");
7373
println!(
7474
"cargo:rustc-cfg=gdal_sys_{}_{}_{}",

gdal-sys/src/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,7 @@
44
#![allow(clippy::upper_case_acronyms)]
55
#![allow(rustdoc::bare_urls)]
66

7+
#[cfg(feature = "bundled")]
8+
extern crate gdal_src;
9+
710
include!(concat!(env!("OUT_DIR"), "/bindings.rs"));

0 commit comments

Comments
 (0)