Skip to content

Commit

Permalink
Static build
Browse files Browse the repository at this point in the history
  • Loading branch information
amousset committed Dec 24, 2024
1 parent ed4cd6d commit 389dab4
Show file tree
Hide file tree
Showing 12 changed files with 197 additions and 12 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "raugeas_src/augeas"]
path = raugeas_src/augeas
url = https://github.com/hercules-team/augeas
45 changes: 39 additions & 6 deletions Cargo.lock

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

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ resolver = "2"
members = [
"raugeas",
"raugeas_sys",
]
"raugeas_src",
"raugeas_src/testcrate",
]
4 changes: 4 additions & 0 deletions raugeas/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@ rust-version = "1.77"
raugeas_sys = { path = "../raugeas_sys", version = "0.2.0" }
bitflags = "2.6.0"
libc = "0.2.43"

[features]
bundled = ["raugeas_sys/bundled"]
default = []
13 changes: 13 additions & 0 deletions raugeas_src/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "raugeas_src"
version = "0.2.0+augeas-1.14.1"
authors = ["panicbit <[email protected]>", "Alexis Mousset <[email protected]>"]
description = "Low level bindings for augeas"
license = "MIT"
keywords = ["augeas", "bindings"]
repository = "https://github.com/amousset/rust-augeas"
edition = "2021"
rust-version = "1.77"

[dependencies]
autotools = "0.2"
1 change: 1 addition & 0 deletions raugeas_src/augeas
Submodule augeas added at cd37b0
79 changes: 79 additions & 0 deletions raugeas_src/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
//! This crate aims to encapsulate the logic required for building `augeas`
//! from source (so that `augeas-sys` can use it).
use std::env;
use std::fs;
use std::path::{Path, PathBuf};

/// Information about the locations of files generated by `build()`.
///
/// After the jq sources have been compiled, the fields in this struct
/// represent where the various files ended up, and what sort of build was
/// done (ie, static or dynamic).
pub struct Artifacts {
include_dir: PathBuf,
lib_dir: PathBuf,
}

impl Artifacts {
/// Prints cargo instructions for linking to the bundled `augeas` lib.
pub fn print_cargo_metadata(&self) {
println!("cargo:include={}", self.include_dir.display());
println!("cargo:rustc-link-search=native={}", self.lib_dir.display());
println!("cargo:rustc-link-lib=static={}", "augeas");
}
pub fn include_dir(&self) -> &Path {
&self.include_dir
}
pub fn lib_dir(&self) -> &Path {
&self.lib_dir
}
}

/// Entry point for callers to run the build.
pub fn build() -> Result<Artifacts, String> {
let out_dir = env::var_os("OUT_DIR")
.map(PathBuf::from)
.expect("OUT_DIR not set");

let augeas_dir = Path::new(env!("CARGO_MANIFEST_DIR")).join("augeas");

autotools::Config::new(&augeas_dir)
.reconf("-iv")
.out_dir(&out_dir)
.enable_static()
.disable_shared()
.try_build()?;

Ok(Artifacts {
lib_dir: out_dir.join("lib"),
include_dir: out_dir.join("include"),
})
}

/// Recursive file copy
fn cp_r(src: &Path, dst: &Path) {
for f in fs::read_dir(src).unwrap() {
let f = f.unwrap();
let path = f.path();
let name = path.file_name().unwrap();
let dst = dst.join(name);
if f.file_type().unwrap().is_dir() {
fs::create_dir_all(&dst).unwrap();
cp_r(&path, &dst);
} else {
let _ = fs::remove_file(&dst);
fs::copy(&path, &dst).unwrap();
}
}
}

/// Cleanup old sources (left from a previous build attempt) then copy from
/// the git submodule into the location where the build will happen.
fn prepare_sources(src: &Path, dst: &Path) {
if dst.exists() {
fs::remove_dir_all(dst).unwrap();
}
fs::create_dir_all(dst).unwrap();
cp_r(src, dst);
}
9 changes: 9 additions & 0 deletions raugeas_src/testcrate/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "testcrate"
version = "0.1.0"
authors = ["Alexis Mousset <[email protected]>"]
links = "augeas"
build = "build.rs"

[build-dependencies]
raugeas_src = { path = ".." }
6 changes: 6 additions & 0 deletions raugeas_src/testcrate/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
extern crate raugeas_src;

fn main() {
let artifacts = raugeas_src::build().unwrap();
artifacts.print_cargo_metadata();
}
7 changes: 7 additions & 0 deletions raugeas_src/testcrate/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#[cfg(test)]
mod tests {
#[test]
fn it_works() {
assert_eq!(2 + 2, 4);
}
}
11 changes: 10 additions & 1 deletion raugeas_sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,13 @@ rust-version = "1.77"

[build-dependencies]
bindgen = "0.70.1"
pkg-config = "0.3.14"
pkg-config = { version = "0.3.14", optional = true }
raugeas_src = { path = "../raugeas_src", optional = true }

[features]
default = ["pkg-config"]
bundled = ["raugeas_src"]

[package.metadata.docs.rs]
no-default-features = true
features = ["bundled"]
27 changes: 23 additions & 4 deletions raugeas_sys/build.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,38 @@
#[cfg(feature = "bundled")]
extern crate augeas_src;
extern crate bindgen;
#[cfg(feature = "pkg-config")]
extern crate pkg_config;

use std::env;
use std::path::PathBuf;
use std::path::{PathBuf};

fn main() {
#[cfg(feature = "bundled")]
fn build_bundled() -> Vec<String> {
let artifacts = augeas_src::build().expect("autotools build");
artifacts.print_cargo_metadata();
vec![artifacts.include_dir().to_str().unwrap().to_string()]
}

#[cfg(feature = "pkg-config")]
fn build_shared() -> Vec<String> {
let augeas = pkg_config::Config::new()
.atleast_version("1.13.0")
.probe("augeas")
.unwrap();

let include_paths = augeas
augeas
.include_paths
.iter()
.map(|path| format!("-I{}", path.display()));
.map(|path| format!("-I{}", path.display())).collect()
}

fn main() {
#[cfg(feature = "bundled")]
let include_paths = build_bundled();

#[cfg(feature = "pkg-config")]
let include_paths = build_shared();

let bindings = bindgen::Builder::default()
.header("wrapper.h")
Expand Down

0 comments on commit 389dab4

Please sign in to comment.