Skip to content

Commit a4c6030

Browse files
committed
fix: preserve raw gn args for linux tls override
1 parent f89c4e3 commit a4c6030

File tree

1 file changed

+80
-12
lines changed

1 file changed

+80
-12
lines changed

build.rs

Lines changed: 80 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -348,18 +348,12 @@ fn build_v8(is_asan: bool) {
348348

349349
// Use the shared-library-safe TLS mode by default on Linux so downstream
350350
// cdylibs can link rusty_v8 archives.
351-
let mut has_tls_library_mode_define = false;
352-
if let Ok(args) = env::var("GN_ARGS") {
353-
for arg in args.split_whitespace() {
354-
if arg.contains("V8_TLS_USED_IN_LIBRARY") {
355-
has_tls_library_mode_define = true;
356-
}
357-
gn_args.push(arg.to_string());
358-
}
359-
}
360-
if target_os == "linux" && !has_tls_library_mode_define {
361-
gn_args.push(r#"extra_cflags=["-DV8_TLS_USED_IN_LIBRARY"]"#.to_string());
362-
}
351+
let raw_gn_args = env::var("GN_ARGS").ok();
352+
add_user_gn_args_and_linux_tls_default(
353+
&mut gn_args,
354+
target_os,
355+
raw_gn_args.as_deref(),
356+
);
363357
// cross-compilation setup
364358
if target_arch == "aarch64" {
365359
gn_args.push(r#"target_cpu="arm64""#.to_string());
@@ -443,6 +437,24 @@ fn build_v8(is_asan: bool) {
443437
build("rusty_v8", None);
444438
}
445439

440+
fn add_user_gn_args_and_linux_tls_default(
441+
gn_args: &mut Vec<String>,
442+
target_os: &str,
443+
raw_gn_args: Option<&str>,
444+
) {
445+
let raw_gn_args = raw_gn_args.filter(|args| !args.trim().is_empty());
446+
let has_tls_library_mode_define =
447+
raw_gn_args.is_some_and(|args| args.contains("V8_TLS_USED_IN_LIBRARY"));
448+
449+
if let Some(args) = raw_gn_args {
450+
gn_args.push(args.to_string());
451+
}
452+
453+
if target_os == "linux" && !has_tls_library_mode_define {
454+
gn_args.push(r#"extra_cflags=["-DV8_TLS_USED_IN_LIBRARY"]"#.to_string());
455+
}
456+
}
457+
446458
fn print_gn_args(gn_out_dir: &Path) {
447459
assert!(
448460
Command::new(gn())
@@ -1240,4 +1252,60 @@ edge [fontsize=10]
12401252
assert!(files.contains("../../../example/src/count_bytes.py"));
12411253
assert!(!files.contains("obj/hello/hello.o"));
12421254
}
1255+
1256+
#[test]
1257+
fn test_linux_tls_override_detected_in_spaced_array() {
1258+
let mut gn_args = Vec::new();
1259+
1260+
add_user_gn_args_and_linux_tls_default(
1261+
&mut gn_args,
1262+
"linux",
1263+
Some(r#"extra_cflags=[ "-O2", "-DV8_TLS_USED_IN_LIBRARY" ]"#),
1264+
);
1265+
1266+
assert_eq!(
1267+
gn_args,
1268+
vec![r#"extra_cflags=[ "-O2", "-DV8_TLS_USED_IN_LIBRARY" ]"#.to_string()]
1269+
);
1270+
}
1271+
1272+
#[test]
1273+
fn test_quoted_whitespace_gn_args_preserved_and_tls_default_injected() {
1274+
let mut gn_args = Vec::new();
1275+
1276+
add_user_gn_args_and_linux_tls_default(
1277+
&mut gn_args,
1278+
"linux",
1279+
Some(r#"clang_base_path="/tmp/clang with spaces""#),
1280+
);
1281+
1282+
assert_eq!(
1283+
gn_args,
1284+
vec![
1285+
r#"clang_base_path="/tmp/clang with spaces""#.to_string(),
1286+
r#"extra_cflags=["-DV8_TLS_USED_IN_LIBRARY"]"#.to_string(),
1287+
]
1288+
);
1289+
}
1290+
1291+
#[test]
1292+
fn test_whitespace_only_gn_args_skipped() {
1293+
let mut gn_args = Vec::new();
1294+
1295+
add_user_gn_args_and_linux_tls_default(&mut gn_args, "macos", Some(" "));
1296+
1297+
assert!(gn_args.is_empty());
1298+
}
1299+
1300+
#[test]
1301+
fn test_linux_tls_default_injected_without_override() {
1302+
let mut gn_args = Vec::new();
1303+
1304+
add_user_gn_args_and_linux_tls_default(&mut gn_args, "linux", None);
1305+
1306+
assert_eq!(
1307+
gn_args,
1308+
vec![r#"extra_cflags=["-DV8_TLS_USED_IN_LIBRARY"]"#.to_string()]
1309+
);
1310+
}
12431311
}

0 commit comments

Comments
 (0)