Skip to content

Commit 9b77a1e

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 cecbbd9 commit 9b77a1e

File tree

3 files changed

+87
-38
lines changed

3 files changed

+87
-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

+58-31
Original file line numberDiff line numberDiff line change
@@ -264,19 +264,37 @@ fn main() {
264264
std::env::var_os("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR should always be set"),
265265
);
266266

267+
let read_env_var_string = |name| read_env_var(name).unwrap().into_string().unwrap();
268+
269+
// Keep in sync with `core_name_and_version!` in prefixed.rs.
270+
let core_name_and_version = [
271+
&read_env_var_string("CARGO_PKG_NAME"),
272+
"core",
273+
&read_env_var_string("CARGO_PKG_VERSION_MAJOR"),
274+
&read_env_var_string("CARGO_PKG_VERSION_MINOR"),
275+
&read_env_var_string("CARGO_PKG_VERSION_PATCH"),
276+
&read_env_var_string("CARGO_PKG_VERSION_PRE"), // Often empty
277+
]
278+
.join("_");
279+
// Ensure `links` in Cargo.toml is consistent with the version.
280+
assert_eq!(
281+
&read_env_var_string("CARGO_MANIFEST_LINKS"),
282+
&core_name_and_version
283+
);
284+
267285
const RING_PREGENERATE_ASM: &str = "RING_PREGENERATE_ASM";
268286
match read_env_var(RING_PREGENERATE_ASM).as_deref() {
269287
Some(s) if s == "1" => {
270-
pregenerate_asm_main(&c_root_dir);
288+
pregenerate_asm_main(&c_root_dir, &core_name_and_version);
271289
}
272-
None => ring_build_rs_main(&c_root_dir),
290+
None => ring_build_rs_main(&c_root_dir, &core_name_and_version),
273291
_ => {
274292
panic!("${} has an invalid value", RING_PREGENERATE_ASM);
275293
}
276294
}
277295
}
278296

279-
fn ring_build_rs_main(c_root_dir: &Path) {
297+
fn ring_build_rs_main(c_root_dir: &Path, core_name_and_version: &str) {
280298
use std::env;
281299

282300
let out_dir = env::var_os("OUT_DIR").unwrap();
@@ -320,7 +338,12 @@ fn ring_build_rs_main(c_root_dir: &Path) {
320338
let generated_dir = if !is_git {
321339
PathBuf::from(env::var_os("CARGO_MANIFEST_DIR").unwrap()).join(PREGENERATED)
322340
} else {
323-
generate_sources_and_preassemble(&out_dir, asm_target.into_iter(), c_root_dir);
341+
generate_sources_and_preassemble(
342+
&out_dir,
343+
asm_target.into_iter(),
344+
c_root_dir,
345+
core_name_and_version,
346+
);
324347
out_dir.clone()
325348
};
326349

@@ -330,25 +353,31 @@ fn ring_build_rs_main(c_root_dir: &Path) {
330353
&generated_dir,
331354
c_root_dir,
332355
&out_dir,
333-
&ring_core_prefix(),
356+
core_name_and_version,
334357
);
335358
emit_rerun_if_changed()
336359
}
337360

338-
fn pregenerate_asm_main(c_root_dir: &Path) {
361+
fn pregenerate_asm_main(c_root_dir: &Path, core_name_and_version: &str) {
339362
println!("cargo:rustc-cfg=pregenerate_asm_only");
340363

341364
let pregenerated = c_root_dir.join(PREGENERATED);
342365
std::fs::create_dir(&pregenerated).unwrap();
343-
generate_sources_and_preassemble(&pregenerated, ASM_TARGETS.iter(), c_root_dir);
366+
generate_sources_and_preassemble(
367+
&pregenerated,
368+
ASM_TARGETS.iter(),
369+
c_root_dir,
370+
core_name_and_version,
371+
);
344372
}
345373

346374
fn generate_sources_and_preassemble<'a>(
347375
out_dir: &Path,
348376
asm_targets: impl Iterator<Item = &'a AsmTarget>,
349377
c_root_dir: &Path,
378+
core_name_and_version: &str,
350379
) {
351-
generate_prefix_symbols_headers(out_dir, &ring_core_prefix()).unwrap();
380+
generate_prefix_symbols_headers(out_dir, core_name_and_version).unwrap();
352381

353382
let perl_exe = get_perl_exe();
354383

@@ -388,10 +417,8 @@ fn build_c_code(
388417
generated_dir: &Path,
389418
c_root_dir: &Path,
390419
out_dir: &Path,
391-
ring_core_prefix: &str,
420+
core_name_and_version: &str,
392421
) {
393-
println!("cargo:rustc-env=RING_CORE_PREFIX={}", ring_core_prefix);
394-
395422
let (asm_srcs, obj_srcs) = if let Some(asm_target) = asm_target {
396423
let perlasm_src_dsts = perlasm_src_dsts(generated_dir, asm_target);
397424

@@ -433,21 +460,30 @@ fn build_c_code(
433460
let test_srcs = RING_TEST_SRCS.iter().map(PathBuf::from).collect::<Vec<_>>();
434461

435462
let libs = [
436-
("", &core_srcs[..], &asm_srcs[..], &obj_srcs[..]),
437-
("test", &test_srcs[..], &[], &[]),
463+
(
464+
core_name_and_version,
465+
&core_srcs[..],
466+
&asm_srcs[..],
467+
&obj_srcs[..],
468+
),
469+
(
470+
&(String::from(core_name_and_version) + "_test"),
471+
&test_srcs[..],
472+
&[],
473+
&[],
474+
),
438475
];
439476

440477
// XXX: Ideally, ring-test would only be built for `cargo test`, but Cargo
441478
// can't do that yet.
442479
libs.iter()
443-
.for_each(|&(lib_name_suffix, srcs, asm_srcs, obj_srcs)| {
444-
let lib_name = String::from(ring_core_prefix) + lib_name_suffix;
480+
.for_each(|&(lib_name, srcs, asm_srcs, obj_srcs)| {
445481
let srcs = srcs.iter().chain(asm_srcs);
446482
build_library(
447483
target,
448484
c_root_dir,
449485
out_dir,
450-
&lib_name,
486+
lib_name,
451487
srcs,
452488
generated_dir,
453489
obj_srcs,
@@ -737,25 +773,16 @@ fn walk_dir(dir: &Path, cb: &impl Fn(&DirEntry)) {
737773
}
738774
}
739775

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

761788
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)