Skip to content

Commit 22bbb3b

Browse files
committed
Use only predefined environment variables to compute name prefixes.
Instead of having build.rs set a custom environment variable, just compute the value directly from environment variables that are guaranteed to be set. This way, non-Cargo build systems can avoid duplicating this custom logic. Move the consistency check of `links` and the package version so that the check is also done during pregeneration. Use `read_env_var()` for the relevant environment variables so that build.rs emits `cargo:rerun-if-env-changed`, as it was noticed that Cargo otherwise doesn't seem to rerun the build script when the Cargo.toml values are changed.
1 parent 5167fb5 commit 22bbb3b

File tree

3 files changed

+85
-38
lines changed

3 files changed

+85
-38
lines changed

Cargo.toml

+4-6
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,10 @@ version = "0.17.8"
1818

1919
# Keep in sync with `version` above.
2020
#
21-
# "ring_core_" + version, replacing punctuation with underscores.
22-
#
23-
# build.rs uses this to derive the prefix for FFI symbols and the file names
24-
# of the FFI libraries, so it must be a valid identifier prefix and a valid
25-
# filename prefix.
26-
links = "ring_core_0_17_8"
21+
# build.rs verifies that this equals "ring_core_{major}_{minor}_{patch}_{pre}"
22+
# as keeping this in sync with the symbol prefixing is crucial for ensuring
23+
# the safety of multiple versions of *ring* being used in a program.
24+
links = "ring_core_0_17_8_"
2725

2826
include = [
2927
"LICENSE",

build.rs

+56-31
Original file line numberDiff line numberDiff line change
@@ -269,19 +269,35 @@ fn main() {
269269
env::var_os("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR should always be set"),
270270
);
271271

272+
// Keep in sync with `core_name_and_version!` in prefixed.rs.
273+
let core_name_and_version = [
274+
&env::var("CARGO_PKG_NAME").unwrap(),
275+
"core",
276+
&env::var("CARGO_PKG_VERSION_MAJOR").unwrap(),
277+
&env::var("CARGO_PKG_VERSION_MINOR").unwrap(),
278+
&env::var("CARGO_PKG_VERSION_PATCH").unwrap(),
279+
&env::var("CARGO_PKG_VERSION_PRE").unwrap(), // Often empty
280+
]
281+
.join("_");
282+
// Ensure `links` in Cargo.toml is consistent with the version.
283+
assert_eq!(
284+
&env::var("CARGO_MANIFEST_LINKS").unwrap(),
285+
&core_name_and_version
286+
);
287+
272288
const RING_PREGENERATE_ASM: &str = "RING_PREGENERATE_ASM";
273289
match env::var_os(RING_PREGENERATE_ASM).as_deref() {
274290
Some(s) if s == "1" => {
275-
pregenerate_asm_main(&c_root_dir);
291+
pregenerate_asm_main(&c_root_dir, &core_name_and_version);
276292
}
277-
None => ring_build_rs_main(&c_root_dir),
293+
None => ring_build_rs_main(&c_root_dir, &core_name_and_version),
278294
_ => {
279295
panic!("${} has an invalid value", RING_PREGENERATE_ASM);
280296
}
281297
}
282298
}
283299

284-
fn ring_build_rs_main(c_root_dir: &Path) {
300+
fn ring_build_rs_main(c_root_dir: &Path, core_name_and_version: &str) {
285301
let out_dir = env::var_os("OUT_DIR").unwrap();
286302
let out_dir = PathBuf::from(out_dir);
287303

@@ -323,7 +339,12 @@ fn ring_build_rs_main(c_root_dir: &Path) {
323339
let generated_dir = if !is_git {
324340
c_root_dir.join(PREGENERATED)
325341
} else {
326-
generate_sources_and_preassemble(&out_dir, asm_target.into_iter(), c_root_dir);
342+
generate_sources_and_preassemble(
343+
&out_dir,
344+
asm_target.into_iter(),
345+
c_root_dir,
346+
core_name_and_version,
347+
);
327348
out_dir.clone()
328349
};
329350

@@ -333,25 +354,31 @@ fn ring_build_rs_main(c_root_dir: &Path) {
333354
&generated_dir,
334355
c_root_dir,
335356
&out_dir,
336-
&ring_core_prefix(),
357+
core_name_and_version,
337358
);
338359
emit_rerun_if_changed()
339360
}
340361

341-
fn pregenerate_asm_main(c_root_dir: &Path) {
362+
fn pregenerate_asm_main(c_root_dir: &Path, core_name_and_version: &str) {
342363
println!("cargo:rustc-cfg=pregenerate_asm_only");
343364

344365
let pregenerated = c_root_dir.join(PREGENERATED);
345366
std::fs::create_dir(&pregenerated).unwrap();
346-
generate_sources_and_preassemble(&pregenerated, ASM_TARGETS.iter(), c_root_dir);
367+
generate_sources_and_preassemble(
368+
&pregenerated,
369+
ASM_TARGETS.iter(),
370+
c_root_dir,
371+
core_name_and_version,
372+
);
347373
}
348374

349375
fn generate_sources_and_preassemble<'a>(
350376
out_dir: &Path,
351377
asm_targets: impl Iterator<Item = &'a AsmTarget>,
352378
c_root_dir: &Path,
379+
core_name_and_version: &str,
353380
) {
354-
generate_prefix_symbols_headers(out_dir, &ring_core_prefix()).unwrap();
381+
generate_prefix_symbols_headers(out_dir, core_name_and_version).unwrap();
355382

356383
let perl_exe = get_perl_exe();
357384

@@ -391,10 +418,8 @@ fn build_c_code(
391418
generated_dir: &Path,
392419
c_root_dir: &Path,
393420
out_dir: &Path,
394-
ring_core_prefix: &str,
421+
core_name_and_version: &str,
395422
) {
396-
println!("cargo:rustc-env=RING_CORE_PREFIX={}", ring_core_prefix);
397-
398423
let (asm_srcs, obj_srcs) = if let Some(asm_target) = asm_target {
399424
let perlasm_src_dsts = perlasm_src_dsts(generated_dir, asm_target);
400425

@@ -436,21 +461,30 @@ fn build_c_code(
436461
let test_srcs = RING_TEST_SRCS.iter().map(PathBuf::from).collect::<Vec<_>>();
437462

438463
let libs = [
439-
("", &core_srcs[..], &asm_srcs[..], &obj_srcs[..]),
440-
("test", &test_srcs[..], &[], &[]),
464+
(
465+
core_name_and_version,
466+
&core_srcs[..],
467+
&asm_srcs[..],
468+
&obj_srcs[..],
469+
),
470+
(
471+
&(String::from(core_name_and_version) + "_test"),
472+
&test_srcs[..],
473+
&[],
474+
&[],
475+
),
441476
];
442477

443478
// XXX: Ideally, ring-test would only be built for `cargo test`, but Cargo
444479
// can't do that yet.
445480
libs.iter()
446-
.for_each(|&(lib_name_suffix, srcs, asm_srcs, obj_srcs)| {
447-
let lib_name = String::from(ring_core_prefix) + lib_name_suffix;
481+
.for_each(|&(lib_name, srcs, asm_srcs, obj_srcs)| {
448482
let srcs = srcs.iter().chain(asm_srcs);
449483
build_library(
450484
target,
451485
c_root_dir,
452486
out_dir,
453-
&lib_name,
487+
lib_name,
454488
srcs,
455489
generated_dir,
456490
obj_srcs,
@@ -740,25 +774,16 @@ fn walk_dir(dir: &Path, cb: &impl Fn(&DirEntry)) {
740774
}
741775
}
742776

743-
fn ring_core_prefix() -> String {
744-
let links = env::var("CARGO_MANIFEST_LINKS").unwrap();
745-
746-
let computed = {
747-
let name = env::var("CARGO_PKG_NAME").unwrap();
748-
let version = env::var("CARGO_PKG_VERSION").unwrap();
749-
name + "_core_" + &version.replace(&['-', '.'][..], "_")
750-
};
751-
752-
assert_eq!(links, computed);
753-
754-
links + "_"
755-
}
756-
757777
/// Creates the necessary header files for symbol renaming.
758778
///
759779
/// For simplicity, both non-Nasm- and Nasm- style headers are always
760780
/// generated, even though local non-packaged builds need only one of them.
761-
fn generate_prefix_symbols_headers(out_dir: &Path, prefix: &str) -> Result<(), std::io::Error> {
781+
fn generate_prefix_symbols_headers(
782+
out_dir: &Path,
783+
core_name_and_version: &str,
784+
) -> Result<(), std::io::Error> {
785+
let prefix = &(String::from(core_name_and_version) + "_");
786+
762787
generate_prefix_symbols_header(out_dir, "prefix_symbols.h", '#', None, prefix)?;
763788

764789
generate_prefix_symbols_header(

src/prefixed.rs

+25-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,27 @@
1+
// Keep in sync with `core_name_and_version` in build.rs.
2+
macro_rules! core_name_and_version {
3+
() => {
4+
concat!(
5+
env!("CARGO_PKG_NAME"),
6+
"_core_",
7+
env!("CARGO_PKG_VERSION_MAJOR"),
8+
"_",
9+
env!("CARGO_PKG_VERSION_MINOR"),
10+
"_",
11+
env!("CARGO_PKG_VERSION_PATCH"),
12+
"_",
13+
env!("CARGO_PKG_VERSION_PRE"), // Often empty
14+
)
15+
};
16+
}
17+
18+
// Keep in sync with `prefix` in build.rs.
19+
macro_rules! prefix {
20+
( ) => {
21+
concat!(core_name_and_version!(), "_")
22+
};
23+
}
24+
125
macro_rules! prefixed_extern {
226
// Functions.
327
{
@@ -70,7 +94,7 @@ macro_rules! prefixed_item {
7094
$name:ident
7195
{ $item:item }
7296
} => {
73-
#[$attr = concat!(env!("RING_CORE_PREFIX"), stringify!($name))]
97+
#[$attr = concat!(prefix!(), stringify!($name))]
7498
$item
7599
};
76100
}

0 commit comments

Comments
 (0)