Skip to content

Commit 88b7f0d

Browse files
committed
refactor build script to use a function to set cfgs
1 parent da4f8f8 commit 88b7f0d

File tree

1 file changed

+27
-25
lines changed

1 file changed

+27
-25
lines changed

build.rs

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -25,94 +25,92 @@ fn main() {
2525
// On CI, we detect the actual FreeBSD version and match its ABI exactly,
2626
// running tests to ensure that the ABI is correct.
2727
match which_freebsd() {
28-
Some(10) if libc_ci || rustc_dep_of_std => {
29-
println!("cargo:rustc-cfg=freebsd10")
30-
}
31-
Some(11) if libc_ci => println!("cargo:rustc-cfg=freebsd11"),
32-
Some(12) if libc_ci => println!("cargo:rustc-cfg=freebsd12"),
33-
Some(13) if libc_ci => println!("cargo:rustc-cfg=freebsd13"),
34-
Some(14) if libc_ci => println!("cargo:rustc-cfg=freebsd14"),
35-
Some(_) | None => println!("cargo:rustc-cfg=freebsd11"),
28+
Some(10) if libc_ci || rustc_dep_of_std => set_cfg("freebsd10"),
29+
Some(11) if libc_ci => set_cfg("freebsd11"),
30+
Some(12) if libc_ci => set_cfg("freebsd12"),
31+
Some(13) if libc_ci => set_cfg("freebsd13"),
32+
Some(14) if libc_ci => set_cfg("freebsd14"),
33+
Some(_) | None => set_cfg("freebsd11"),
3634
}
3735

3836
// On CI: deny all warnings
3937
if libc_ci {
40-
println!("cargo:rustc-cfg=libc_deny_warnings");
38+
set_cfg("libc_deny_warnings");
4139
}
4240

4341
// Rust >= 1.15 supports private module use:
4442
if rustc_minor_ver >= 15 || rustc_dep_of_std {
45-
println!("cargo:rustc-cfg=libc_priv_mod_use");
43+
set_cfg("libc_priv_mod_use");
4644
}
4745

4846
// Rust >= 1.19 supports unions:
4947
if rustc_minor_ver >= 19 || rustc_dep_of_std {
50-
println!("cargo:rustc-cfg=libc_union");
48+
set_cfg("libc_union");
5149
}
5250

5351
// Rust >= 1.24 supports const mem::size_of:
5452
if rustc_minor_ver >= 24 || rustc_dep_of_std {
55-
println!("cargo:rustc-cfg=libc_const_size_of");
53+
set_cfg("libc_const_size_of");
5654
}
5755

5856
// Rust >= 1.25 supports repr(align):
5957
if rustc_minor_ver >= 25 || rustc_dep_of_std || align_cargo_feature {
60-
println!("cargo:rustc-cfg=libc_align");
58+
set_cfg("libc_align");
6159
}
6260

6361
// Rust >= 1.26 supports i128 and u128:
6462
if rustc_minor_ver >= 26 || rustc_dep_of_std {
65-
println!("cargo:rustc-cfg=libc_int128");
63+
set_cfg("libc_int128");
6664
}
6765

6866
// Rust >= 1.30 supports `core::ffi::c_void`, so libc can just re-export it.
6967
// Otherwise, it defines an incompatible type to retaining
7068
// backwards-compatibility.
7169
if rustc_minor_ver >= 30 || rustc_dep_of_std {
72-
println!("cargo:rustc-cfg=libc_core_cvoid");
70+
set_cfg("libc_core_cvoid");
7371
}
7472

7573
// Rust >= 1.33 supports repr(packed(N)) and cfg(target_vendor).
7674
if rustc_minor_ver >= 33 || rustc_dep_of_std {
77-
println!("cargo:rustc-cfg=libc_packedN");
78-
println!("cargo:rustc-cfg=libc_cfg_target_vendor");
75+
set_cfg("libc_packedN");
76+
set_cfg("libc_cfg_target_vendor");
7977
}
8078

8179
// Rust >= 1.40 supports #[non_exhaustive].
8280
if rustc_minor_ver >= 40 || rustc_dep_of_std {
83-
println!("cargo:rustc-cfg=libc_non_exhaustive");
81+
set_cfg("libc_non_exhaustive");
8482
}
8583

8684
// Rust >= 1.47 supports long array:
8785
if rustc_minor_ver >= 47 || rustc_dep_of_std {
88-
println!("cargo:rustc-cfg=libc_long_array");
86+
set_cfg("libc_long_array");
8987
}
9088

9189
if rustc_minor_ver >= 51 || rustc_dep_of_std {
92-
println!("cargo:rustc-cfg=libc_ptr_addr_of");
90+
set_cfg("libc_ptr_addr_of");
9391
}
9492

9593
// Rust >= 1.37.0 allows underscores as anonymous constant names.
9694
if rustc_minor_ver >= 37 || rustc_dep_of_std {
97-
println!("cargo:rustc-cfg=libc_underscore_const_names");
95+
set_cfg("libc_underscore_const_names");
9896
}
9997

10098
// #[thread_local] is currently unstable
10199
if rustc_dep_of_std {
102-
println!("cargo:rustc-cfg=libc_thread_local");
100+
set_cfg("libc_thread_local");
103101
}
104102

105103
// Rust >= 1.62.0 allows to use `const_extern_fn` for "Rust" and "C".
106104
if rustc_minor_ver >= 62 {
107-
println!("cargo:rustc-cfg=libc_const_extern_fn");
105+
set_cfg("libc_const_extern_fn");
108106
} else {
109107
// Rust < 1.62.0 requires a crate feature and feature gate.
110108
if const_extern_fn_cargo_feature {
111109
if !is_nightly || rustc_minor_ver < 40 {
112110
panic!("const-extern-fn requires a nightly compiler >= 1.40");
113111
}
114-
println!("cargo:rustc-cfg=libc_const_extern_fn_unstable");
115-
println!("cargo:rustc-cfg=libc_const_extern_fn");
112+
set_cfg("libc_const_extern_fn_unstable");
113+
set_cfg("libc_const_extern_fn");
116114
}
117115
}
118116
}
@@ -181,3 +179,7 @@ fn which_freebsd() -> Option<i32> {
181179
_ => None,
182180
}
183181
}
182+
183+
fn set_cfg(cfg: &str) {
184+
println!("cargo:rustc-cfg={}", cfg);
185+
}

0 commit comments

Comments
 (0)