Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Static build #1

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
92 changes: 92 additions & 0 deletions raugeas_src/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
//! 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};
use std::process::Command;

/// Information about the locations of files generated by `build()`.
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");
println!("cargo:rustc-link-lib=static=fa");
println!("cargo:rustc-link-lib=static=xml2");
// println!("cargo:rustc-link-lib=static=lfl");
}
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");

// Bootstrap needs to run from the submodule dir as it uses git
// FIXME: autobootstrap does not work
/*let bootstrap_res = Command::new("./bootstrap")
.current_dir(&augeas_dir)
.status()
.expect("failed to execute bootstrap");
if !bootstrap_res.success() {
return Err("Bootstrap failed".to_string());
}*/

// Build in a clean dir
//prepare_sources(&augeas_dir, &build_dir);

autotools::Config::new(&augeas_dir)
.reconf("-ifv")
.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);
}
10 changes: 10 additions & 0 deletions raugeas_src/testcrate/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "testcrate"
version = "0.1.0"
authors = ["Alexis Mousset <[email protected]>"]
links = "augeas"
build = "build.rs"
edition = "2021"

[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"]
26 changes: 23 additions & 3 deletions raugeas_sys/build.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,39 @@
extern crate bindgen;
#[cfg(feature = "pkg-config")]
extern crate pkg_config;
#[cfg(feature = "bundled")]
extern crate raugeas_src;

use std::env;
use std::path::PathBuf;

fn main() {
#[cfg(feature = "bundled")]
fn build_bundled() -> Vec<String> {
let artifacts = raugeas_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
Loading