Merge pull request #146162 from Kha/elan-wrap-lld

elan: wrap embedded linker
This commit is contained in:
Gabriel Ebner 2021-11-15 18:07:14 +01:00 committed by GitHub
commit f4e3542f5d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 34 deletions

View file

@ -1,24 +1,19 @@
diff --git a/src/elan-dist/src/component/package.rs b/src/elan-dist/src/component/package.rs
index c51e76d..d0a26d7 100644
index c51e76d..ae8159e 100644
--- a/src/elan-dist/src/component/package.rs
+++ b/src/elan-dist/src/component/package.rs
@@ -56,11 +56,35 @@ fn unpack_without_first_dir<R: Read>(archive: &mut tar::Archive<R>, path: &Path)
@@ -56,6 +56,30 @@ fn unpack_without_first_dir<R: Read>(archive: &mut tar::Archive<R>, path: &Path)
entry
.unpack(&full_path)
.chain_err(|| ErrorKind::ExtractingPackage)?;
+ nix_patchelf_if_needed(&full_path);
}
Ok(())
}
+fn nix_patchelf_if_needed(dest_path: &Path) {
+ let (is_bin, is_lib) = if let Some(p) = dest_path.parent() {
+ (p.ends_with("bin"), p.ends_with("lib"))
+ } else {
+ (false, false)
+ };
+ nix_patch_if_needed(&full_path)?;
+ }
+
+ Ok(())
+}
+
+fn nix_patch_if_needed(dest_path: &Path) -> Result<()> {
+ let is_bin = matches!(dest_path.parent(), Some(p) if p.ends_with("bin"));
+ if is_bin {
+ let _ = ::std::process::Command::new("@patchelf@/bin/patchelf")
+ .arg("--set-interpreter")
@ -26,15 +21,15 @@ index c51e76d..d0a26d7 100644
+ .arg(dest_path)
+ .output();
+ }
+ else if is_lib {
+ let _ = ::std::process::Command::new("@patchelf@/bin/patchelf")
+ .arg("--set-rpath")
+ .arg("@libPath@")
+ .arg(dest_path)
+ .output();
+ }
+}
+
#[derive(Debug)]
pub struct ZipPackage<'a>(temp::Dir<'a>);
+ if dest_path.extension() == Some(::std::ffi::OsStr::new("lld")) {
+ use std::os::unix::fs::PermissionsExt;
+ let new_path = dest_path.with_extension("orig");
+ ::std::fs::rename(dest_path, &new_path)?;
+ ::std::fs::write(dest_path, format!(r#"#! @shell@
+exec -a "$0" {} "$@" --dynamic-linker=@dynamicLinker@
+"#, new_path.to_str().unwrap()))?;
+ ::std::fs::set_permissions(dest_path, ::std::fs::Permissions::from_mode(0o755))?;
}
Ok(())

View file

@ -1,9 +1,5 @@
{ stdenv, lib, runCommand, patchelf, makeWrapper, pkg-config, curl
, openssl, gmp, zlib, fetchFromGitHub, rustPlatform, libiconv }:
let
libPath = lib.makeLibraryPath [ gmp ];
in
{ stdenv, lib, runCommand, patchelf, makeWrapper, pkg-config, curl, runtimeShell
, openssl, zlib, fetchFromGitHub, rustPlatform, libiconv }:
rustPlatform.buildRustPackage rec {
pname = "elan";
@ -32,13 +28,13 @@ rustPlatform.buildRustPackage rec {
(runCommand "0001-dynamically-patchelf-binaries.patch" {
CC = stdenv.cc;
patchelf = patchelf;
libPath = "$ORIGIN/../lib:${libPath}";
shell = runtimeShell;
} ''
export dynamicLinker=$(cat $CC/nix-support/dynamic-linker)
substitute ${./0001-dynamically-patchelf-binaries.patch} $out \
--subst-var patchelf \
--subst-var dynamicLinker \
--subst-var libPath
--subst-var shell
'')
];
@ -50,8 +46,6 @@ rustPlatform.buildRustPackage rec {
done
popd
wrapProgram $out/bin/elan --prefix "LD_LIBRARY_PATH" : "${libPath}"
# tries to create .elan
export HOME=$(mktemp -d)
mkdir -p "$out/share/"{bash-completion/completions,fish/vendor_completions.d,zsh/site-functions}