From 48a5ffc9c81ad026d68e8bad106e6cd90ccce706 Mon Sep 17 00:00:00 2001 From: Chiichen <2531693734@qq.com> Date: Mon, 15 Apr 2024 13:27:57 +0800 Subject: [PATCH 1/7] chore: Fixed incorrect use of cfg! macro and update build.rs to support arm target --- build.rs | 80 +++++++++++++++++++++++------------------------------ tools/clang | 2 +- 2 files changed, 36 insertions(+), 46 deletions(-) diff --git a/build.rs b/build.rs index df085a9edf..556e4454e1 100644 --- a/build.rs +++ b/build.rs @@ -137,20 +137,19 @@ fn build_v8(is_asan: bool) { if need_gn_ninja_download() { download_ninja_gn_binaries(); } - + let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap(); + let target_arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap(); // On windows, rustc cannot link with a V8 debug build. - let mut gn_args = if is_debug() && !cfg!(target_os = "windows") { + let mut gn_args = if is_debug() && target_os != "windows" { // Note: When building for Android aarch64-qemu, use release instead of debug. vec!["is_debug=true".to_string()] } else { vec!["is_debug=false".to_string()] }; - if is_asan { gn_args.push("is_asan=true".to_string()); } - - if cfg!(not(feature = "use_custom_libcxx")) { + if let Err(_) = env::var("CARGO_FEATURE_USE_CUSTOM_LIBCXX") { gn_args.push("use_custom_libcxx=false".to_string()); } @@ -172,7 +171,7 @@ fn build_v8(is_asan: bool) { let clang_base_path = clang_download(); gn_args.push(format!("clang_base_path={:?}", clang_base_path)); - if cfg!(target_os = "android") && cfg!(target_arch = "aarch64") { + if target_os == "android" && target_arch == "aarch64" { gn_args.push("treat_warnings_as_errors=false".to_string()); } } @@ -194,24 +193,27 @@ fn build_v8(is_asan: bool) { gn_args.push(arg.to_string()); } } + // cross-compilation setup + if target_arch == "aarch64" { + gn_args.push(r#"target_cpu="arm64""#.to_string()); + gn_args.push("use_sysroot=true".to_string()); + maybe_install_sysroot("arm64"); + maybe_install_sysroot("amd64"); + } + if target_arch == "arm"{ + gn_args.push(r#"target_cpu="arm""#.to_string()); + gn_args.push(r#"v8_target_cpu="arm""#.to_string()); + gn_args.push("use_sysroot=true".to_string()); + maybe_install_sysroot("i386"); + maybe_install_sysroot("arm"); + } let target_triple = env::var("TARGET").unwrap(); // check if the target triple describes a non-native environment if target_triple != env::var("HOST").unwrap() { - // cross-compilation setup - if target_triple == "aarch64-unknown-linux-gnu" - || target_triple == "aarch64-linux-android" - { - gn_args.push(r#"target_cpu="arm64""#.to_string()); - gn_args.push("use_sysroot=true".to_string()); - maybe_install_sysroot("arm64"); - maybe_install_sysroot("amd64"); - }; - if target_triple == "aarch64-linux-android" { gn_args.push(r#"v8_target_cpu="arm64""#.to_string()); gn_args.push(r#"target_os="android""#.to_string()); - gn_args.push("treat_warnings_as_errors=false".to_string()); // NDK 23 and above removes libgcc entirely. @@ -289,24 +291,8 @@ fn maybe_install_sysroot(arch: &str) { } fn platform() -> String { - let os = if cfg!(target_os = "linux") { - "linux" - } else if cfg!(target_os = "macos") { - "mac" - } else if cfg!(target_os = "windows") { - "windows" - } else { - "unknown" - }; - - let arch = if cfg!(target_arch = "x86_64") { - "amd64" - } else if cfg!(target_arch = "aarch64") { - "arm64" - } else { - "unknown" - }; - + let os = env::var("CARGO_CFG_TARGET_OS").unwrap(); + let arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap(); format!("{os}-{arch}") } @@ -346,9 +332,9 @@ fn static_lib_url() -> String { env::var("RUSTY_V8_MIRROR").unwrap_or_else(|_| default_base.into()); let version = env::var("CARGO_PKG_VERSION").unwrap(); let target = env::var("TARGET").unwrap(); - + let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap(); // Note: we always use the release build on windows. - if cfg!(target_os = "windows") { + if target_os == "windows" { return format!("{}/v{}/rusty_v8_release_{}.lib.gz", base, version, target); } // Use v8 in release mode unless $V8_FORCE_DEBUG=true @@ -370,9 +356,11 @@ fn env_bool(key: &str) -> bool { } fn static_lib_name() -> &'static str { - match cfg!(target_os = "windows") { - true => "rusty_v8.lib", - false => "librusty_v8.a", + let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap(); + if target_os == "windows" { + "rusty_v8.lib" + } else { + "librusty_v8.a" } } @@ -583,8 +571,8 @@ fn copy_archive(url: &str, filename: &Path) { fn print_link_flags() { println!("cargo:rustc-link-lib=static=rusty_v8"); - - let should_dyn_link_libcxx = cfg!(not(feature = "use_custom_libcxx")) + let should_dyn_link_libcxx = env::var("CARGO_FEATURE_USE_CUSTOM_LIBCXX") + .is_err() || env::var("GN_ARGS").map_or(false, |gn_args| { gn_args .split_whitespace() @@ -613,16 +601,18 @@ fn print_link_flags() { } } } + let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap(); + let target_env = env::var("CARGO_CFG_TARGET_ENV").unwrap(); - if cfg!(target_os = "windows") { + if target_os == "windows" { println!("cargo:rustc-link-lib=dylib=winmm"); println!("cargo:rustc-link-lib=dylib=dbghelp"); } - if cfg!(target_env = "msvc") { + if target_env == "msvc" { // On Windows, including libcpmt[d]/msvcprt[d] explicitly links the C++ // standard library, which libc++ needs for exception_ptr internals. - if cfg!(target_feature = "crt-static") { + if env::var("CARGO_FEATURE_CRT_STATIC").is_ok() { println!("cargo:rustc-link-lib=libcpmt"); } else { println!("cargo:rustc-link-lib=dylib=msvcprt"); diff --git a/tools/clang b/tools/clang index 5016a8b16c..3344dd8997 160000 --- a/tools/clang +++ b/tools/clang @@ -1 +1 @@ -Subproject commit 5016a8b16c6e3c34138e776dce423fec7b6cf610 +Subproject commit 3344dd8997f422862a1c5477b490b3611be31351 From 6a62664cf4caa8b92f5568113787c2e343e63e46 Mon Sep 17 00:00:00 2001 From: Chiichen <2531693734@qq.com> Date: Tue, 16 Apr 2024 11:32:56 +0800 Subject: [PATCH 2/7] chore: fix the gn/ninja install path --- build.rs | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/build.rs b/build.rs index 556e4454e1..74c14b7b1b 100644 --- a/build.rs +++ b/build.rs @@ -200,7 +200,7 @@ fn build_v8(is_asan: bool) { maybe_install_sysroot("arm64"); maybe_install_sysroot("amd64"); } - if target_arch == "arm"{ + if target_arch == "arm" { gn_args.push(r#"target_cpu="arm""#.to_string()); gn_args.push(r#"v8_target_cpu="arm""#.to_string()); gn_args.push("use_sysroot=true".to_string()); @@ -290,9 +290,26 @@ fn maybe_install_sysroot(arch: &str) { } } -fn platform() -> String { - let os = env::var("CARGO_CFG_TARGET_OS").unwrap(); - let arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap(); +fn host_platform() -> String { + let os = if cfg!(target_os = "linux") { + "linux" + } else if cfg!(target_os = "macos") { + "mac" + } else if cfg!(target_os = "windows") { + "windows" + } else { + "unknown" + }; + + let arch = if cfg!(target_arch = "x86_64") { + "amd64" + } else if cfg!(target_arch = "aarch64") { + "arm64" + } else if cfg!(target_arch = "arm") { + "arm" + } else { + "unknown" + }; format!("{os}-{arch}") } @@ -300,7 +317,7 @@ fn download_ninja_gn_binaries() { let target_dir = build_dir(); let bin_dir = target_dir .join("ninja_gn_binaries-20221218") - .join(platform()); + .join(host_platform()); let gn = bin_dir.join("gn"); let ninja = bin_dir.join("ninja"); #[cfg(windows)] From 746ab7b03b304e71459740c00905334beb5ae79b Mon Sep 17 00:00:00 2001 From: Chiichen <2531693734@qq.com> Date: Tue, 16 Apr 2024 11:59:56 +0800 Subject: [PATCH 3/7] chore: revert clang submodule update --- tools/clang | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/clang b/tools/clang index 3344dd8997..5016a8b16c 160000 --- a/tools/clang +++ b/tools/clang @@ -1 +1 @@ -Subproject commit 3344dd8997f422862a1c5477b490b3611be31351 +Subproject commit 5016a8b16c6e3c34138e776dce423fec7b6cf610 From 4e30365d9ad0a2835d7e697eb19bbc3e5f4ef1de Mon Sep 17 00:00:00 2001 From: Chiichen <2531693734@qq.com> Date: Tue, 23 Apr 2024 09:58:46 +0800 Subject: [PATCH 4/7] style: resolve clippy warnings --- build.rs | 70 ++++++++++++++++++++++++++------------------------------ 1 file changed, 32 insertions(+), 38 deletions(-) diff --git a/build.rs b/build.rs index 8029c0f67c..e5327be368 100644 --- a/build.rs +++ b/build.rs @@ -149,7 +149,7 @@ fn build_v8(is_asan: bool) { if is_asan { gn_args.push("is_asan=true".to_string()); } - if let Err(_) = env::var("CARGO_FEATURE_USE_CUSTOM_LIBCXX") { + if env::var("CARGO_FEATURE_USE_CUSTOM_LIBCXX").is_err() { gn_args.push("use_custom_libcxx=false".to_string()); } @@ -210,29 +210,26 @@ fn build_v8(is_asan: bool) { let target_triple = env::var("TARGET").unwrap(); // check if the target triple describes a non-native environment - if target_triple != env::var("HOST").unwrap() { - if target_os == "android" { - let arch = if target_arch == "x86_64" { - "x64" - } else if target_arch == "aarch64" { - "arm64" - } else { - "unknown" - }; - - if target_arch == "x86_64" { - maybe_install_sysroot("amd64"); - } - - gn_args.push(format!(r#"v8_target_cpu="{}""#, arch).to_string()); - gn_args.push(format!(r#"target_cpu="{}""#, arch).to_string()); - gn_args.push(r#"target_os="android""#.to_string()); - gn_args.push("treat_warnings_as_errors=false".to_string()); - gn_args.push("use_sysroot=true".to_string()); + if target_triple != env::var("HOST").unwrap() && target_os == "android" { + let arch = if target_arch == "x86_64" { + "x64" + } else if target_arch == "aarch64" { + "arm64" + } else { + "unknown" + }; + if target_arch == "x86_64" { + maybe_install_sysroot("amd64"); + } + gn_args.push(format!(r#"v8_target_cpu="{}""#, arch).to_string()); + gn_args.push(format!(r#"target_cpu="{}""#, arch).to_string()); + gn_args.push(r#"target_os="android""#.to_string()); + gn_args.push("treat_warnings_as_errors=false".to_string()); + gn_args.push("use_sysroot=true".to_string()); - // NDK 23 and above removes libgcc entirely. - // https://github.com/rust-lang/rust/pull/85806 - if !Path::new("./third_party/android_ndk/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android24-clang++").exists() { + // NDK 23 and above removes libgcc entirely. + // https://github.com/rust-lang/rust/pull/85806 + if !Path::new("./third_party/android_ndk/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android24-clang++").exists() { assert!(Command::new("curl") .arg("-L") .arg("-o").arg("./third_party/android-ndk-r26c-linux.zip") @@ -253,21 +250,18 @@ fn build_v8(is_asan: bool) { fs::rename("./third_party/android-ndk-r26c", "./third_party/android_ndk").unwrap(); fs::remove_file("./third_party/android-ndk-r26c-linux.zip").unwrap(); } - - static CHROMIUM_URI: &str = "https://chromium.googlesource.com"; - - maybe_clone_repo( - "./third_party/android_platform", - &format!( - "{}/chromium/src/third_party/android_platform.git", - CHROMIUM_URI - ), - ); - maybe_clone_repo( - "./third_party/catapult", - &format!("{}/catapult.git", CHROMIUM_URI), - ); - }; + static CHROMIUM_URI: &str = "https://chromium.googlesource.com"; + maybe_clone_repo( + "./third_party/android_platform", + &format!( + "{}/chromium/src/third_party/android_platform.git", + CHROMIUM_URI + ), + ); + maybe_clone_repo( + "./third_party/catapult", + &format!("{}/catapult.git", CHROMIUM_URI), + ); } if target_triple.starts_with("i686-") { From a9ab682ea715fa7882f62e5a37abcd7b1e531efe Mon Sep 17 00:00:00 2001 From: Matt Mastracci Date: Tue, 23 Apr 2024 11:35:40 -0400 Subject: [PATCH 5/7] Update build.rs --- build.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/build.rs b/build.rs index e5327be368..48221b6e54 100644 --- a/build.rs +++ b/build.rs @@ -137,6 +137,10 @@ fn build_v8(is_asan: bool) { if need_gn_ninja_download() { download_ninja_gn_binaries(); } + // `#[cfg(...)]` attributes don't work as expected from build.rs -- they refer to the configuration + // of the host system which the build.rs script will be running on. In short, `cfg!(target_)` + // is actually the host os/arch instead of target os/arch while cross compiling. Instead, Environment variables + // are the officially approach to get the target os/arch in build.rs. let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap(); let target_arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap(); // On windows, rustc cannot link with a V8 debug build. From edf9b88c8ea36689d429191e0723d5d2481fd628 Mon Sep 17 00:00:00 2001 From: Chiichen <2531693734@qq.com> Date: Wed, 24 Apr 2024 16:44:48 +0800 Subject: [PATCH 6/7] ci: use macos12 to ensure that the host's architecture is x86-64 --- .github/workflows/ci.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 48c2e762e5..0027d7cdee 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -27,12 +27,12 @@ jobs: !contains(github.event.pull_request.labels.*.name, 'no-fail-fast') }} matrix: config: - - os: macOS-latest + - os: macos-12 target: x86_64-apple-darwin variant: debug cargo: cargo - - os: macOS-latest + - os: macos-12 target: x86_64-apple-darwin variant: release cargo: cargo From 0b5bf66330cdc0dc47f5d668685020a156d5aa72 Mon Sep 17 00:00:00 2001 From: Chiichen <2531693734@qq.com> Date: Wed, 24 Apr 2024 18:36:26 +0800 Subject: [PATCH 7/7] chore: update rust targets in rust-toolchain.toml --- rust-toolchain.toml | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/rust-toolchain.toml b/rust-toolchain.toml index a436857e58..b284bfb14b 100644 --- a/rust-toolchain.toml +++ b/rust-toolchain.toml @@ -1,3 +1,12 @@ [toolchain] channel = "1.76.0" components = ["rustfmt", "clippy"] +targets = [ + "x86_64-apple-darwin", + "aarch64-apple-darwin", + "x86_64-unknown-linux-gnu", + "aarch64-unknown-linux-gnu", + "x86_64-pc-windows-msvc", + "aarch64-linux-android", + "x86_64-linux-android", +]