Skip to content

Commit

Permalink
attic/build.rs: Fix warnings when nix_store is disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaofengli committed Sep 11, 2024
1 parent 2babfb7 commit 1bd0279
Showing 1 changed file with 70 additions and 67 deletions.
137 changes: 70 additions & 67 deletions attic/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,82 +2,85 @@
//!
//! We link against libnixstore to perform actions on the Nix Store.
use cc::Build;
use version_compare::Version;

struct NixDependency {
version: String,
fn main() {
#[cfg(feature = "nix_store")]
nix_store::build_bridge();
}

impl NixDependency {
fn detect() -> Self {
let library = pkg_config::Config::new()
.cargo_metadata(false)
.atleast_version("2.4")
.probe("nix-main")
.expect("Failed to find nix-main >=2.4 through pkg-config");
#[cfg(feature = "nix_store")]
mod nix_store {
use cc::Build;
use version_compare::Version;

struct NixDependency {
version: String,
}

Self {
version: library.version,
impl NixDependency {
fn detect() -> Self {
let library = pkg_config::Config::new()
.cargo_metadata(false)
.atleast_version("2.4")
.probe("nix-main")
.expect("Failed to find nix-main >=2.4 through pkg-config");

Self {
version: library.version,
}
}
}

fn apply_version_flags(&self, build: &mut Build) {
let version = Version::from(&self.version).unwrap();
fn apply_version_flags(&self, build: &mut Build) {
let version = Version::from(&self.version).unwrap();

if version >= Version::from("2.20").unwrap() {
build.flag("-DATTIC_NIX_2_20");
if version >= Version::from("2.20").unwrap() {
build.flag("-DATTIC_NIX_2_20");
}
}
}

fn emit_cargo_metadata(&self) {
pkg_config::Config::new()
.atleast_version("2.4")
.probe("nix-store")
.unwrap();
fn emit_cargo_metadata(&self) {
pkg_config::Config::new()
.atleast_version("2.4")
.probe("nix-store")
.unwrap();

pkg_config::Config::new()
.atleast_version("2.4")
.probe("nix-main")
.unwrap();
pkg_config::Config::new()
.atleast_version("2.4")
.probe("nix-main")
.unwrap();
}
}
}

fn main() {
#[cfg(feature = "nix_store")]
build_bridge();
}

#[cfg(feature = "nix_store")]
fn build_bridge() {
let nix_dep = NixDependency::detect();

// Temporary workaround for issue in <https://github.com/NixOS/nix/pull/8484>
let hacky_include = {
let dir = tempfile::tempdir().expect("Failed to create temporary directory for workaround");
std::fs::write(dir.path().join("uds-remote-store.md"), "\"\"").unwrap();
dir
};

let mut build = cxx_build::bridge("src/nix_store/bindings/mod.rs");
build
.file("src/nix_store/bindings/nix.cpp")
.flag("-std=c++2a")
.flag("-O2")
.flag("-include")
.flag("nix/config.h")
.flag("-idirafter")
.flag(hacky_include.path().to_str().unwrap())
// In Nix 2.19+, nix/args/root.hh depends on being able to #include "args.hh" (which is in its parent directory), for some reason
.flag("-I")
.flag(concat!(env!("NIX_INCLUDE_PATH"), "/nix"));

nix_dep.apply_version_flags(&mut build);

build.compile("nixbinding");

println!("cargo:rerun-if-changed=src/nix_store/bindings");

// the -l flags must be after -lnixbinding
nix_dep.emit_cargo_metadata();
pub fn build_bridge() {
let nix_dep = NixDependency::detect();

// Temporary workaround for issue in <https://github.com/NixOS/nix/pull/8484>
let hacky_include = {
let dir =
tempfile::tempdir().expect("Failed to create temporary directory for workaround");
std::fs::write(dir.path().join("uds-remote-store.md"), "\"\"").unwrap();
dir
};

let mut build = cxx_build::bridge("src/nix_store/bindings/mod.rs");
build
.file("src/nix_store/bindings/nix.cpp")
.flag("-std=c++2a")
.flag("-O2")
.flag("-include")
.flag("nix/config.h")
.flag("-idirafter")
.flag(hacky_include.path().to_str().unwrap())
// In Nix 2.19+, nix/args/root.hh depends on being able to #include "args.hh" (which is in its parent directory), for some reason
.flag("-I")
.flag(concat!(env!("NIX_INCLUDE_PATH"), "/nix"));

nix_dep.apply_version_flags(&mut build);

build.compile("nixbinding");

println!("cargo:rerun-if-changed=src/nix_store/bindings");

// the -l flags must be after -lnixbinding
nix_dep.emit_cargo_metadata();
}
}

0 comments on commit 1bd0279

Please sign in to comment.