mathematica: Refactor install process with autoPatchelfHook (#154295)

- Call appropriate hooks at beginning/end of each phase

  - No longer assume current working dir in installPhase

  - Add support for udev rules installation (required for VernierLink)

  - Use -verbose with MathInstaller for detailed progress

  - Use patchShebangs or MathInstaller

  - Remove broken hostname call, root user check, and Avahi daemon
    check in MathInstaller

  - Add proper error checking after MathInstaller call

  - Skip configure phase

  - Create wrappers for relevant executables

  - Set $HOME to $TMPDIR/home temporarily so MathInstaller
    succeeds

  - Replace dd with tail for simplicity and performance
This commit is contained in:
Armeen Mahdian 2022-01-19 11:35:38 -06:00 committed by GitHub
parent 20ae0dae7a
commit 2d207146ee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,167 +1,172 @@
{ lib, stdenv { lib
, coreutils , stdenv
, patchelf , autoPatchelfHook
, requireFile , buildEnv
, callPackage
, makeWrapper , makeWrapper
, requireFile
, alsa-lib , alsa-lib
, cups
, dbus , dbus
, flite
, fontconfig , fontconfig
, freetype , freetype
, gcc , gcc-unwrapped
, glib , glib
, libssh2 , gmpxx
, keyutils
, libGL
, libGLU
, libpcap
, libtins
, libuuid
, libxkbcommon
, libxml2
, llvmPackages_12
, matio
, mpfr
, ncurses , ncurses
, opencv4 , opencv4
, openjdk11
, openssl , openssl
, pciutils
, tre
, unixODBC , unixODBC
, xkeyboard_config , xkeyboard_config
, xorg , xorg
, zlib , zlib
, libxml2
, libuuid
, lang ? "en" , lang ? "en"
, libGL
, libGLU
}: }:
let let
l10n = l10n = import ./l10ns.nix {
import ./l10ns.nix { inherit lib requireFile lang;
lib = lib; };
inherit requireFile lang; in stdenv.mkDerivation {
};
in
stdenv.mkDerivation rec {
inherit (l10n) version name src; inherit (l10n) version name src;
buildInputs = [ nativeBuildInputs = [
coreutils autoPatchelfHook
patchelf
makeWrapper makeWrapper
];
buildInputs = [
alsa-lib alsa-lib
coreutils cups.lib
dbus dbus
flite
fontconfig fontconfig
freetype freetype
gcc.cc
gcc.libc
glib glib
libssh2 gmpxx
ncurses keyutils.lib
opencv4
openssl
stdenv.cc.cc.lib
unixODBC
xkeyboard_config
libxml2
libuuid
zlib
libGL libGL
libGLU libGLU
libpcap
libtins
libuuid
libxkbcommon
libxml2
llvmPackages_12.libllvm.lib
matio
mpfr
ncurses
opencv4
openjdk11
openssl
pciutils
tre
unixODBC
xkeyboard_config
] ++ (with xorg; [ ] ++ (with xorg; [
libX11
libXext
libXtst
libXi
libXmu
libXrender
libxcb
libXcursor
libXfixes
libXrandr
libICE libICE
libSM libSM
libX11
libXScrnSaver
libXcomposite
libXcursor
libXdamage
libXext
libXfixes
libXi
libXinerama
libXmu
libXrandr
libXrender
libXtst
libxcb
]); ]);
ldpath = lib.makeLibraryPath buildInputs wrapProgramFlags = [
+ lib.optionalString (stdenv.hostPlatform.system == "x86_64-linux") "--prefix LD_LIBRARY_PATH : ${lib.makeLibraryPath [ gcc-unwrapped.lib zlib ]}"
(":" + lib.makeSearchPathOutput "lib" "lib64" buildInputs); "--prefix PATH : ${lib.makeBinPath [ stdenv.cc ]}"
# Fix libQt errors - #96490
"--set USE_WOLFRAM_LD_LIBRARY_PATH 1"
# Fix xkeyboard config path for Qt
"--set QT_XKB_CONFIG_ROOT ${xkeyboard_config}/share/X11/xkb"
];
unpackPhase = '' unpackPhase = ''
echo "=== Extracting makeself archive ===" runHook preUnpack
# find offset from file
# Find offset from file
offset=$(${stdenv.shell} -c "$(grep -axm1 -e 'offset=.*' $src); echo \$offset" $src) offset=$(${stdenv.shell} -c "$(grep -axm1 -e 'offset=.*' $src); echo \$offset" $src)
dd if="$src" ibs=$offset skip=1 | tar -xf - tail -c +$(($offset + 1)) $src | tar -xf -
cd Unix
runHook postUnpack
''; '';
installPhase = '' installPhase = ''
cd Installer runHook preInstall
# don't restrict PATH, that has already been done
sed -i -e 's/^PATH=/# PATH=/' MathInstaller
# Fix the installation script as follows: cd "$TMPDIR/Unix/Installer"
# 1. Adjust the shebang
# 2. Use the wrapper in the desktop items mkdir -p "$out/lib/udev/rules.d"
# Patch MathInstaller's shebangs and udev rules dir
patchShebangs MathInstaller
substituteInPlace MathInstaller \ substituteInPlace MathInstaller \
--replace "/bin/bash" "/bin/sh" \ --replace /etc/udev/rules.d $out/lib/udev/rules.d
--replace "Executables/Mathematica" "../../bin/mathematica"
# Install the desktop items # Remove PATH restriction, root and avahi daemon checks, and hostname call
export XDG_DATA_HOME="$out/share" sed -i '
s/^PATH=/# &/
s/isRoot="false"/# &/
s/^checkAvahiDaemon$/# &/
s/`hostname`/""/
' MathInstaller
echo "=== Running MathInstaller ===" # NOTE: some files placed under HOME may be useful
./MathInstaller -auto -createdir=y -execdir=$out/bin -targetdir=$out/libexec/Mathematica -silent XDG_DATA_HOME="$out/share" HOME="$TMPDIR/home" vernierLink=y \
./MathInstaller -execdir="$out/bin" -targetdir="$out/libexec/Mathematica" -auto -verbose -createdir=y
# Fix library paths # Check if MathInstaller produced any errors
cd $out/libexec/Mathematica/Executables errLog="$out/libexec/Mathematica/InstallErrors"
for path in mathematica MathKernel Mathematica WolframKernel wolfram math; do if [ -f "$errLog" ]; then
sed -i -e "2iexport LD_LIBRARY_PATH=${zlib}/lib:${stdenv.cc.cc.lib}/lib:${libssh2}/lib:\''${LD_LIBRARY_PATH}\n" $path echo "Installation errors:"
done cat "$errLog"
return 1
fi
# Fix xkeyboard config path for Qt runHook postInstall
for path in mathematica Mathematica; do
sed -i -e "2iexport QT_XKB_CONFIG_ROOT=\"${xkeyboard_config}/share/X11/xkb\"\n" $path
done
# Remove some broken libraries
rm -f $out/libexec/Mathematica/SystemFiles/Libraries/Linux-x86-64/libz.so*
# Set environment variable to fix libQt errors - see https://github.com/NixOS/nixpkgs/issues/96490
wrapProgram $out/bin/mathematica --set USE_WOLFRAM_LD_LIBRARY_PATH 1
''; '';
preFixup = '' preFixup = ''
echo "=== PatchElfing away ===" for bin in $out/libexec/Mathematica/Executables/*; do
# This code should be a bit forgiving of errors, unfortunately wrapProgram "$bin" ''${wrapProgramFlags[@]}
set +e
find $out/libexec/Mathematica/SystemFiles -type f -perm -0100 | while read f; do
type=$(readelf -h "$f" 2>/dev/null | grep 'Type:' | sed -e 's/ *Type: *\([A-Z]*\) (.*/\1/')
if [ -z "$type" ]; then
:
elif [ "$type" == "EXEC" ]; then
echo "patching $f executable <<"
patchelf --shrink-rpath "$f"
patchelf \
--set-interpreter "$(cat $NIX_CC/nix-support/dynamic-linker)" \
--set-rpath "$(patchelf --print-rpath "$f"):${ldpath}" \
"$f" \
&& patchelf --shrink-rpath "$f" \
|| echo unable to patch ... ignoring 1>&2
elif [ "$type" == "DYN" ]; then
echo "patching $f library <<"
patchelf \
--set-rpath "$(patchelf --print-rpath "$f"):${ldpath}" \
"$f" \
&& patchelf --shrink-rpath "$f" \
|| echo unable to patch ... ignoring 1>&2
else
echo "not patching $f <<: unknown elf type"
fi
done done
''; '';
dontConfigure = true;
dontBuild = true; dontBuild = true;
# This is primarily an IO bound build; there's little benefit to building remotely. # This is primarily an IO bound build; there's little benefit to building remotely
preferLocalBuild = true; preferLocalBuild = true;
# all binaries are already stripped # All binaries are already stripped
dontStrip = true; dontStrip = true;
# we did this in prefixup already # NOTE: Some deps are still not found; ignore for now
dontPatchELF = true; autoPatchelfIgnoreMissingDeps = true;
meta = with lib; { meta = with lib; {
description = "Wolfram Mathematica computational software system"; description = "Wolfram Mathematica computational software system";