Skip to content

Commit 1a86c38

Browse files
committed
Ability to collect Cargo directives instead of printing them directly
1 parent dc3b7b6 commit 1a86c38

File tree

1 file changed

+42
-3
lines changed

1 file changed

+42
-3
lines changed

src/lib.rs

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,14 @@ use std::env;
8989
use std::error;
9090
use std::ffi::{OsStr, OsString};
9191
use std::fmt;
92+
use std::fmt::Arguments;
9293
use std::fmt::Display;
9394
use std::io;
9495
use std::ops::{Bound, RangeBounds};
9596
use std::path::PathBuf;
9697
use std::process::{Command, Output};
9798
use std::str;
99+
use std::sync::Mutex;
98100

99101
/// Wrapper struct to polyfill methods introduced in 1.57 (`get_envs`, `get_args` etc).
100102
/// This is needed to reconstruct the pkg-config command for output in a copy-
@@ -106,7 +108,7 @@ struct WrappedCommand {
106108
args: Vec<OsString>,
107109
}
108110

109-
#[derive(Clone, Debug)]
111+
#[derive(Debug)]
110112
pub struct Config {
111113
statik: Option<bool>,
112114
min_version: Bound<String>,
@@ -116,6 +118,24 @@ pub struct Config {
116118
env_metadata: bool,
117119
print_system_libs: bool,
118120
print_system_cflags: bool,
121+
/// If `Some`, cargo directives will be buffered instead of being printed directly
122+
metadata_buffer: Option<Mutex<Vec<String>>>,
123+
}
124+
125+
impl Clone for Config {
126+
fn clone(&self) -> Config {
127+
Config {
128+
statik: self.statik,
129+
min_version: self.min_version.clone(),
130+
max_version: self.max_version.clone(),
131+
extra_args: self.extra_args.clone(),
132+
print_system_cflags: self.print_system_cflags,
133+
print_system_libs: self.print_system_libs,
134+
cargo_metadata: self.cargo_metadata,
135+
env_metadata: self.env_metadata,
136+
metadata_buffer: None,
137+
}
138+
}
119139
}
120140

121141
#[derive(Clone, Debug)]
@@ -463,6 +483,16 @@ impl Config {
463483
print_system_libs: true,
464484
cargo_metadata: true,
465485
env_metadata: true,
486+
metadata_buffer: None,
487+
}
488+
}
489+
490+
/// Emit buffered metadata, if any
491+
fn print_bufferred(&self) {
492+
if let Some(mut buf) = self.metadata_buffer.as_ref().and_then(|m| m.lock().ok()) {
493+
for line in buf.drain(..) {
494+
println!("cargo:{}", line);
495+
}
466496
}
467497
}
468498

@@ -643,11 +673,19 @@ impl Config {
643673

644674
fn env_var_os(&self, name: &str) -> Option<OsString> {
645675
if self.env_metadata {
646-
println!("cargo:rerun-if-env-changed={}", name);
676+
self.emit_cargo_directive(format_args!("rerun-if-env-changed={}", name));
647677
}
648678
env::var_os(name)
649679
}
650680

681+
fn emit_cargo_directive(&self, fmt: Arguments) {
682+
if let Some(mut buf) = self.metadata_buffer.as_ref().and_then(|m| m.lock().ok()) {
683+
buf.push(fmt.to_string());
684+
} else {
685+
println!("cargo:{}", fmt);
686+
}
687+
}
688+
651689
fn is_static(&self, name: &str) -> bool {
652690
self.statik.unwrap_or_else(|| self.infer_static(name))
653691
}
@@ -733,7 +771,7 @@ impl Config {
733771

734772
fn print_metadata(&self, s: &str) {
735773
if self.cargo_metadata {
736-
println!("cargo:{}", s);
774+
self.emit_cargo_directive(format_args!("{}", s));
737775
}
738776
}
739777

@@ -765,6 +803,7 @@ impl Default for Config {
765803
print_system_libs: false,
766804
cargo_metadata: false,
767805
env_metadata: false,
806+
metadata_buffer: None,
768807
}
769808
}
770809
}

0 commit comments

Comments
 (0)