/
build.rs
134 lines (128 loc) · 5.37 KB
/
build.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#[cfg(feature = "manpage")]
use clap_mangen::Man;
#[cfg(any(feature = "static", feature = "completions"))]
use std::path::Path;
use std::{collections, env};
use vergen::EmitBuilder;
#[cfg(feature = "completions")]
use {
clap::CommandFactory,
clap_complete::generator::generate_to,
clap_complete::shells::{Bash, Elvish, Fish, PowerShell, Zsh},
std::fs,
};
#[cfg(feature = "completions")]
include!("../src/cli.rs");
fn main() {
if let Ok(val) = env::var("AUTOTOOLS_DEPENDENCIES") {
for dependency in val.split(' ') {
println!("cargo:rerun-if-changed={dependency}");
}
}
let mut builder = EmitBuilder::builder();
// If passed a version from automake, use that instead of vergen's formatting
if let Ok(val) = env::var("VERSION_FROM_AUTOTOOLS") {
println!("cargo:rustc-env=VERGEN_GIT_DESCRIBE={val}")
} else {
builder = *builder.git_describe(true, true, None);
};
builder.emit().expect("Unable to generate the cargo keys!");
pass_on_configure_details();
#[cfg(feature = "manpage")]
generate_manpage();
#[cfg(feature = "completions")]
generate_shell_completions();
#[cfg(feature = "static")]
{
let dir = env::var("CARGO_MANIFEST_DIR").unwrap();
println!(
"cargo:rustc-link-search=native={}",
Path::new(&dir).join("justenough").join(".libs").display()
);
println!(
"cargo:rustc-link-search=native={}",
Path::new(&dir).join("libtexpdf").join(".libs").display()
);
println!("cargo:rustc-link-arg=-l:fontmetrics.a");
println!("cargo:rustc-link-arg=-l:justenoughfontconfig.a");
println!("cargo:rustc-link-arg=-l:justenoughharfbuzz.a");
println!("cargo:rustc-link-arg=-l:justenoughicu.a");
println!("cargo:rustc-link-arg=-l:justenoughlibtexpdf.a");
println!("cargo:rustc-link-arg=-l:svg.a");
println!("cargo:rustc-link-arg=-l:libtexpdf.a");
// These are automatically linked by Cargo, but sadly *previous* to our links above
println!("cargo:rustc-link-arg=-lharfbuzz"); // needed by justenoughharfbuzz
#[cfg(feature = "variations")]
println!("cargo:rustc-link-arg=-lharfbuzz-subset"); // needed by justenoughharfbuzz
println!("cargo:rustc-link-arg=-lfontconfig"); // needed by justenoughfontconfig
println!("cargo:rustc-link-arg=-licui18n"); // needed by justenoughicu
println!("cargo:rustc-link-arg=-licuuc"); // needed by justenoughicu
println!("cargo:rustc-link-arg=-lm"); // needed by svg
println!("cargo:rustc-link-arg=-lz"); // needed by libtexpdf
println!("cargo:rustc-link-arg=-lpng"); // needed by libtexpdf
}
}
/// Generate man page
#[cfg(feature = "manpage")]
fn generate_manpage() {
let out_dir = match env::var_os("OUT_DIR") {
None => return,
Some(out_dir) => out_dir,
};
let manpage_dir = Path::new(&out_dir);
fs::create_dir_all(manpage_dir).expect("Unable to create directory for generated manpages");
let app = Cli::command();
let bin_name: &str = app
.get_bin_name()
.expect("Could not retrieve bin-name from generated Clap app");
let app = Cli::command();
let man = Man::new(app);
let mut buffer: Vec<u8> = Default::default();
man.render(&mut buffer)
.expect("Unable to render man page to UTF-8 string");
fs::write(manpage_dir.join(format!("{bin_name}.1")), buffer)
.expect("Unable to write manepage to file");
}
/// Generate shell completion files from CLI interface
#[cfg(feature = "completions")]
fn generate_shell_completions() {
let out_dir = match env::var_os("OUT_DIR") {
None => return,
Some(out_dir) => out_dir,
};
let completions_dir = Path::new(&out_dir).join("completions");
fs::create_dir_all(&completions_dir)
.expect("Could not create directory in which to place completions");
let app = Cli::command();
let bin_name: &str = app
.get_bin_name()
.expect("Could not retrieve bin-name from generated Clap app");
let mut app = Cli::command();
#[cfg(feature = "bash")]
generate_to(Bash, &mut app, bin_name, &completions_dir)
.expect("Unable to generate bash completions");
#[cfg(feature = "elvish")]
generate_to(Elvish, &mut app, bin_name, &completions_dir)
.expect("Unable to generate elvish completions");
#[cfg(feature = "fish")]
generate_to(Fish, &mut app, bin_name, &completions_dir)
.expect("Unable to generate fish completions");
#[cfg(feature = "powershell")]
generate_to(PowerShell, &mut app, bin_name, &completions_dir)
.expect("Unable to generate powershell completions");
#[cfg(feature = "zsh")]
generate_to(Zsh, &mut app, bin_name, &completions_dir)
.expect("Unable to generate zsh completions");
}
/// Pass through some variables set by autoconf/automake about where we're installed to cargo for
/// use in finding resources at runtime
fn pass_on_configure_details() {
let mut autoconf_vars = collections::HashMap::new();
autoconf_vars.insert("CONFIGURE_PREFIX", String::from("./"));
autoconf_vars.insert("CONFIGURE_BINDIR", String::from("./"));
autoconf_vars.insert("CONFIGURE_DATADIR", String::from("./"));
for (var, default) in autoconf_vars {
let val = env::var(var).unwrap_or(default);
println!("cargo:rustc-env={var}={val}");
}
}