From b480885973ac1361ed924759057a1ffc5cb87c8d Mon Sep 17 00:00:00 2001 From: erjuan Date: Tue, 29 Oct 2024 15:29:01 +0800 Subject: [PATCH 1/5] delete metadata_id from filename of libtest.so and libstd.so modify builder.rs and compile.rs to build std without metadata_id Signed-off-by: fengting --- src/bootstrap/builder.rs | 5 ++++- src/bootstrap/compile.rs | 21 ++++++++++++++------- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/src/bootstrap/builder.rs b/src/bootstrap/builder.rs index 05b66f94727..8ab64bcad54 100644 --- a/src/bootstrap/builder.rs +++ b/src/bootstrap/builder.rs @@ -1512,7 +1512,10 @@ pub fn cargo( Mode::Codegen => metadata.push_str("codegen"), _ => {} } - cargo.env("__CARGO_DEFAULT_LIB_METADATA", &metadata); + + if mode != Mode::Std { + cargo.env("__CARGO_DEFAULT_LIB_METADATA", &metadata); + } if cmd == "clippy" { rustflags.arg("-Zforce-unstable-if-unmarked"); diff --git a/src/bootstrap/compile.rs b/src/bootstrap/compile.rs index 14c3ef79a78..4cf2818ecb2 100644 --- a/src/bootstrap/compile.rs +++ b/src/bootstrap/compile.rs @@ -1743,22 +1743,22 @@ pub fn run_cargo( continue; } - let filename = Path::new(&*filename); + let filename_path = Path::new(&*filename); // If this was an output file in the "host dir" we don't actually // worry about it, it's not relevant for us - if filename.starts_with(&host_root_dir) { + if filename_path.starts_with(&host_root_dir) { // Unless it's a proc macro used in the compiler if crate_types.iter().any(|t| t == "proc-macro") { - deps.push((filename.to_path_buf(), DependencyType::Host)); + deps.push((filename_path.to_path_buf(), DependencyType::Host)); } continue; } // If this was output in the `deps` dir then this is a precise file // name (hash included) so we start tracking it. - if filename.starts_with(&target_deps_dir) { - deps.push((filename.to_path_buf(), DependencyType::Target)); + if filename_path.starts_with(&target_deps_dir) { + deps.push((filename_path.to_path_buf(), DependencyType::Target)); continue; } @@ -1772,13 +1772,17 @@ pub fn run_cargo( // `std-.dll.lib` on Windows. The aforementioned methods only // split the file name by the last extension (`.lib`) while we need // to split by all extensions (`.dll.lib`). - let expected_len = t!(filename.metadata()).len(); - let filename = filename.file_name().unwrap().to_str().unwrap(); + let expected_len = t!(filename_path.metadata()).len(); + let filename = filename_path.file_name().unwrap().to_str().unwrap(); let mut parts = filename.splitn(2, '.'); let file_stem = parts.next().unwrap().to_owned(); let extension = parts.next().unwrap().to_owned(); toplevel.push((file_stem, extension, expected_len)); + + if filename.contains("libstd.so") || filename.contains("libtest.so") { + deps.push((filename_path.to_path_buf(), DependencyType::Target)); + } } }); @@ -1794,6 +1798,9 @@ pub fn run_cargo( .map(|e| (e.path(), e.file_name().into_string().unwrap(), t!(e.metadata()))) .collect::>(); for (prefix, extension, expected_len) in toplevel { + if prefix.contains("libstd") || prefix.contains("libtest") { + continue; + } let candidates = contents.iter().filter(|&&(_, ref filename, ref meta)| { meta.len() == expected_len && filename -- Gitee From b6afb32a8690a112b57f911635b855f7aef08ce2 Mon Sep 17 00:00:00 2001 From: erjuan Date: Mon, 11 Nov 2024 14:46:43 +0800 Subject: [PATCH 2/5] delete metadata_id from filename of std.dll and test.dll of windows target modify compile.rs to build std without metadata_id in windows target Signed-off-by: fengting --- src/bootstrap/compile.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/bootstrap/compile.rs b/src/bootstrap/compile.rs index 4cf2818ecb2..96c1098c2c2 100644 --- a/src/bootstrap/compile.rs +++ b/src/bootstrap/compile.rs @@ -1780,7 +1780,8 @@ pub fn run_cargo( toplevel.push((file_stem, extension, expected_len)); - if filename.contains("libstd.so") || filename.contains("libtest.so") { + if filename.contains("libstd.so") || filename.contains("libtest.so") + || filename.contains("std.dll") || filename.contains("test.dll") { deps.push((filename_path.to_path_buf(), DependencyType::Target)); } } @@ -1798,7 +1799,7 @@ pub fn run_cargo( .map(|e| (e.path(), e.file_name().into_string().unwrap(), t!(e.metadata()))) .collect::>(); for (prefix, extension, expected_len) in toplevel { - if prefix.contains("libstd") || prefix.contains("libtest") { + if prefix.ends_with("std") || prefix.ends_with("test") { continue; } let candidates = contents.iter().filter(|&&(_, ref filename, ref meta)| { -- Gitee From b13850bd86f6c2c8795d7b2a67f5b8529e66e459 Mon Sep 17 00:00:00 2001 From: erjuan Date: Mon, 11 Nov 2024 14:52:59 +0800 Subject: [PATCH 3/5] delete metadata_id from filename of libtest.so and libstd.so another method to delete metadata_id Signed-off-by: fengting --- src/bootstrap/compile.rs | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/src/bootstrap/compile.rs b/src/bootstrap/compile.rs index 96c1098c2c2..c51a848bb70 100644 --- a/src/bootstrap/compile.rs +++ b/src/bootstrap/compile.rs @@ -1743,22 +1743,22 @@ pub fn run_cargo( continue; } - let filename_path = Path::new(&*filename); + let filename = Path::new(&*filename); // If this was an output file in the "host dir" we don't actually // worry about it, it's not relevant for us - if filename_path.starts_with(&host_root_dir) { + if filename.starts_with(&host_root_dir) { // Unless it's a proc macro used in the compiler if crate_types.iter().any(|t| t == "proc-macro") { - deps.push((filename_path.to_path_buf(), DependencyType::Host)); + deps.push((filename.to_path_buf(), DependencyType::Host)); } continue; } // If this was output in the `deps` dir then this is a precise file // name (hash included) so we start tracking it. - if filename_path.starts_with(&target_deps_dir) { - deps.push((filename_path.to_path_buf(), DependencyType::Target)); + if filename.starts_with(&target_deps_dir) { + deps.push((filename.to_path_buf(), DependencyType::Target)); continue; } @@ -1772,18 +1772,13 @@ pub fn run_cargo( // `std-.dll.lib` on Windows. The aforementioned methods only // split the file name by the last extension (`.lib`) while we need // to split by all extensions (`.dll.lib`). - let expected_len = t!(filename_path.metadata()).len(); - let filename = filename_path.file_name().unwrap().to_str().unwrap(); + let expected_len = t!(filename.metadata()).len(); + let filename = filename.file_name().unwrap().to_str().unwrap(); let mut parts = filename.splitn(2, '.'); let file_stem = parts.next().unwrap().to_owned(); let extension = parts.next().unwrap().to_owned(); toplevel.push((file_stem, extension, expected_len)); - - if filename.contains("libstd.so") || filename.contains("libtest.so") - || filename.contains("std.dll") || filename.contains("test.dll") { - deps.push((filename_path.to_path_buf(), DependencyType::Target)); - } } }); @@ -1799,14 +1794,11 @@ pub fn run_cargo( .map(|e| (e.path(), e.file_name().into_string().unwrap(), t!(e.metadata()))) .collect::>(); for (prefix, extension, expected_len) in toplevel { - if prefix.ends_with("std") || prefix.ends_with("test") { - continue; - } let candidates = contents.iter().filter(|&&(_, ref filename, ref meta)| { meta.len() == expected_len && filename .strip_prefix(&prefix[..]) - .map(|s| s.starts_with('-') && s.ends_with(&extension[..])) + .map(|s| s.ends_with(&extension[..])) .unwrap_or(false) }); let max = candidates.max_by_key(|&&(_, _, ref metadata)| { -- Gitee From 7bb320dfad30896e67187a7ad762e5cf35ce4f61 Mon Sep 17 00:00:00 2001 From: erjuan Date: Mon, 11 Nov 2024 15:00:11 +0800 Subject: [PATCH 4/5] revert deleting metadata id from filename of libstd.so revert deleting metadata id from filename of libstd.so Signed-off-by: fengting --- src/bootstrap/builder.rs | 5 +---- src/bootstrap/compile.rs | 2 +- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/src/bootstrap/builder.rs b/src/bootstrap/builder.rs index 8ab64bcad54..05b66f94727 100644 --- a/src/bootstrap/builder.rs +++ b/src/bootstrap/builder.rs @@ -1512,10 +1512,7 @@ pub fn cargo( Mode::Codegen => metadata.push_str("codegen"), _ => {} } - - if mode != Mode::Std { - cargo.env("__CARGO_DEFAULT_LIB_METADATA", &metadata); - } + cargo.env("__CARGO_DEFAULT_LIB_METADATA", &metadata); if cmd == "clippy" { rustflags.arg("-Zforce-unstable-if-unmarked"); diff --git a/src/bootstrap/compile.rs b/src/bootstrap/compile.rs index c51a848bb70..14c3ef79a78 100644 --- a/src/bootstrap/compile.rs +++ b/src/bootstrap/compile.rs @@ -1798,7 +1798,7 @@ pub fn run_cargo( meta.len() == expected_len && filename .strip_prefix(&prefix[..]) - .map(|s| s.ends_with(&extension[..])) + .map(|s| s.starts_with('-') && s.ends_with(&extension[..])) .unwrap_or(false) }); let max = candidates.max_by_key(|&&(_, _, ref metadata)| { -- Gitee From 135fe673454d95410871c7c15e2bd9b55e3164b6 Mon Sep 17 00:00:00 2001 From: erjuan Date: Mon, 11 Nov 2024 15:02:31 +0800 Subject: [PATCH 5/5] rename std by adding .dylib and deleting metadata id rename std by adding .dylib and deleting metadata id Signed-off-by: fengting --- src/bootstrap/dist.rs | 12 +++++++++ src/bootstrap/std_rename.sh | 54 +++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 src/bootstrap/std_rename.sh diff --git a/src/bootstrap/dist.rs b/src/bootstrap/dist.rs index b34a4b2dc63..0f989c0e590 100644 --- a/src/bootstrap/dist.rs +++ b/src/bootstrap/dist.rs @@ -668,6 +668,18 @@ fn run(self, builder: &Builder<'_>) -> Option { verify_uefi_rlib_format(builder, target, &stamp); copy_target_libs(builder, target, &tarball.image_dir(), &stamp); + // For std of linux-ohos target, + // we want to delete metadata-id from the filename, and add '.dylib' for `libstd` and `libtest`. + // After changing the filename, + // we modify the dependency of `libtest.dylib.so` from `libstd-{metadata-id}.so` to `libstd.dylib.so`. + if target.triple.ends_with("-linux-ohos") { + let script = builder.src.join("src/bootstrap/std_rename.sh"); + Command::new("bash") + .arg(script) + .arg(&target.triple) + .output() + .expect("Renaming for std fails."); + } Some(tarball.generate()) } } diff --git a/src/bootstrap/std_rename.sh b/src/bootstrap/std_rename.sh new file mode 100644 index 00000000000..634fb40e1c1 --- /dev/null +++ b/src/bootstrap/std_rename.sh @@ -0,0 +1,54 @@ +#!/bin/bash +set -e +readonly script_path=$(cd $(dirname $0);pwd) +readonly build_path="${script_path}/../../build" + +# $1 is target triple, such as `x86_64-unknown-linux-ohos`. +readonly lib_path="${build_path}/tmp/tarball/rust-std/$1/image/lib/rustlib/$1/lib" + +for file in $(find "${lib_path}" -name "lib*.*") +do + dir_name=${file%/*} + file_name=${file##*/} + file_prefix=$(echo "$file_name" | awk '{split($1, arr, "."); print arr[1]}') + file_suffix=$(echo "$file_name" | awk '{split($1, arr, "."); print arr[2]}') + + # Get filename without metadata-id. + file_prefix=$(echo "$file_prefix" | awk '{split($1, arr, "-"); print arr[1]}') + + if [[ "$file_suffix" != "rlib" && "$file_suffix" != "so" || "$file_prefix" == "librustc_demangle" || "$file_prefix" == "libcfg_if" || "$file_prefix" == "libunwind" ]] + then + continue + fi + if [[ "$file_suffix" == "rlib" ]] + then + if [[ "$file_prefix" == "libstd" || "$file_prefix" == "libtest" ]] + then + # Add '.dylib' for `libstd` and `libtest`. + newfile_name="$file_prefix.dylib.rlib" + else + newfile_name="$file_prefix.rlib" + fi + fi + + # `libstd` and `libtest` have both `so` and `rlib`, the other libs only have `rlib`. + if [[ "$file_suffix" == "so" ]] + then + newfile_name="$file_prefix.dylib.so" + if [[ "$file_prefix" == "libtest" ]] + then + # Modify the dependency of `libtest.dylib.so` from `libstd-{metadata-id}.so` to `libstd.dylib.so`. + readonly dynstr_section_vaddr=$(readelf -S "$file" | grep ".dynstr" | awk -F']' '{print $2}' | awk '{print strtonum("0x"$3)}') + readonly libstd_str_offset=$(readelf -p .dynstr "$file" | grep "libstd-[a-z0-9]\{16\}\.so" | awk -F'[' '{print $2}' | awk '{print $1}' | awk -F']' '{print strtonum("0x"$1)}') + readonly libstd_str_vaddr=`expr $dynstr_section_vaddr + $libstd_str_offset` + $(printf 'libstd.dylib.so\0\0\0\0\0\0\0\0\0\0\0' | dd of="$file" bs=1 seek=$libstd_str_vaddr count=26 conv=notrunc) + fi + fi + + if [[ "$file_name" == "$newfile_name" ]] + then + continue + fi + + mv $file "$dir_name/$newfile_name" +done \ No newline at end of file -- Gitee