nixpkgs/pkgs/development/compilers/emscripten/default.nix

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

150 lines
4.5 KiB
Nix
Raw Normal View History

{ lib, stdenv, fetchFromGitHub, python3, nodejs, closurecompiler
2020-08-28 18:19:50 +00:00
, jre, binaryen
2021-08-07 07:45:26 +00:00
, llvmPackages
, symlinkJoin, makeWrapper, substituteAll, fetchpatch
2022-12-14 02:24:38 +00:00
, buildNpmPackage
, emscripten
}:
2020-08-28 18:19:50 +00:00
stdenv.mkDerivation rec {
pname = "emscripten";
2022-12-14 02:24:38 +00:00
version = "3.1.24";
2020-08-28 18:19:50 +00:00
llvmEnv = symlinkJoin {
name = "emscripten-llvm-${version}";
2021-08-07 07:45:26 +00:00
paths = with llvmPackages; [ clang-unwrapped clang-unwrapped.lib lld llvm ];
2020-08-28 18:19:50 +00:00
};
2022-12-14 02:24:38 +00:00
nodeModules = buildNpmPackage {
name = "emscripten-node-modules-${version}";
2022-12-14 02:24:38 +00:00
inherit pname version src;
npmDepsHash = "sha256-ejuHR2BpAUStWjuvQuGE6ko4byF4GBl6FJBshxlknQk=";
dontBuild = true;
# Copy node_modules directly.
installPhase = ''
cp -r node_modules $out/
'';
};
src = fetchFromGitHub {
2019-02-25 12:13:55 +00:00
owner = "emscripten-core";
repo = "emscripten";
2022-12-14 02:24:38 +00:00
sha256 = "sha256-1jW6ThxK6dThOO90l4Mc5yehVF3tI4HWipBWZAOztrk=";
2020-08-28 18:19:50 +00:00
rev = version;
};
2020-08-28 18:19:50 +00:00
nativeBuildInputs = [ makeWrapper ];
buildInputs = [ nodejs python3 ];
2021-08-07 07:45:26 +00:00
patches = [
(substituteAll {
src = ./0001-emulate-clang-sysroot-include-logic.patch;
resourceDir = "${llvmEnv}/lib/clang/${llvmPackages.release_version}/";
})
# https://github.com/emscripten-core/emscripten/pull/18219
(fetchpatch {
url = "https://github.com/emscripten-core/emscripten/commit/afbc14950f021513c59cbeaced8807ef8253530a.patch";
sha256 = "sha256-+gJNTQJng9rWcGN3GAcMBB0YopKPnRp/r8CN9RSTClU=";
})
# https://github.com/emscripten-core/emscripten/pull/18220
(fetchpatch {
url = "https://github.com/emscripten-core/emscripten/commit/852982318f9fb692ba1dd1173f62e1eb21ae61ca.patch";
sha256 = "sha256-hmIOtpRx3PD3sDAahUcreSydydqcdSqArYvyLGgUgd8=";
})
2021-08-07 07:45:26 +00:00
];
2020-08-28 18:19:50 +00:00
buildPhase = ''
2021-08-07 07:45:26 +00:00
runHook preBuild
2020-08-28 18:19:50 +00:00
patchShebangs .
2020-08-29 10:51:38 +00:00
# fixes cmake support
2020-08-28 18:19:50 +00:00
sed -i -e "s/print \('emcc (Emscript.*\)/sys.stderr.write(\1); sys.stderr.flush()/g" emcc.py
2020-08-29 10:51:38 +00:00
# disables cache in user home, use installation directory instead
2021-08-07 07:45:26 +00:00
sed -i '/^def/!s/root_is_writable()/True/' tools/config.py
2020-08-29 10:51:38 +00:00
sed -i "/^def check_sanity/a\\ return" tools/shared.py
# required for wasm2c
2022-12-14 02:24:38 +00:00
ln -s ${nodeModules} node_modules
2020-08-29 10:51:38 +00:00
echo "EMSCRIPTEN_ROOT = '$out/share/emscripten'" > .emscripten
2020-08-28 18:19:50 +00:00
echo "LLVM_ROOT = '${llvmEnv}/bin'" >> .emscripten
echo "NODE_JS = '${nodejs}/bin/node'" >> .emscripten
echo "JS_ENGINES = [NODE_JS]" >> .emscripten
2020-08-29 10:51:38 +00:00
echo "CLOSURE_COMPILER = ['${closurecompiler}/bin/closure-compiler']" >> .emscripten
2020-08-28 18:19:50 +00:00
echo "JAVA = '${jre}/bin/java'" >> .emscripten
# to make the test(s) below work
2020-08-29 10:51:38 +00:00
# echo "SPIDERMONKEY_ENGINE = []" >> .emscripten
2020-08-28 18:19:50 +00:00
echo "BINARYEN_ROOT = '${binaryen}'" >> .emscripten
# make emconfigure/emcmake use the correct (wrapped) binaries
sed -i "s|^EMCC =.*|EMCC='$out/bin/emcc'|" tools/shared.py
sed -i "s|^EMXX =.*|EMXX='$out/bin/em++'|" tools/shared.py
sed -i "s|^EMAR =.*|EMAR='$out/bin/emar'|" tools/shared.py
sed -i "s|^EMRANLIB =.*|EMRANLIB='$out/bin/emranlib'|" tools/shared.py
2021-08-07 07:45:26 +00:00
runHook postBuild
2020-08-28 18:19:50 +00:00
'';
installPhase = ''
2021-08-07 07:45:26 +00:00
runHook preInstall
appdir=$out/share/emscripten
mkdir -p $appdir
2020-08-28 18:19:50 +00:00
cp -r . $appdir
chmod -R +w $appdir
mkdir -p $out/bin
2021-08-07 07:45:26 +00:00
for b in em++ em-config emar embuilder.py emcc emcmake emconfigure emmake emranlib emrun emscons emsize; do
2020-08-28 18:19:50 +00:00
makeWrapper $appdir/$b $out/bin/$b \
2022-12-14 02:24:38 +00:00
--set NODE_PATH ${nodeModules} \
2020-08-29 10:51:38 +00:00
--set EM_EXCLUSIVE_CACHE_ACCESS 1 \
2020-08-28 18:19:50 +00:00
--set PYTHON ${python3}/bin/python
done
# precompile libc (etc.) in all variants:
pushd $TMPDIR
2022-06-22 19:15:18 +00:00
echo 'int __main_argc_argv() { return 42; }' >test.c
for LTO in -flto ""; do
# wasm2c doesn't work with PIC
$out/bin/emcc -s WASM2C -s STANDALONE_WASM $LTO test.c
for BIND in "" "--bind"; do
for MT in "" "-s USE_PTHREADS"; do
for RELOCATABLE in "" "-s RELOCATABLE"; do
$out/bin/emcc $RELOCATABLE $BIND $MT $LTO test.c
done
2021-08-03 15:56:24 +00:00
done
done
done
popd
2020-08-28 18:19:50 +00:00
export PYTHON=${python3}/bin/python
2022-12-14 02:24:38 +00:00
export NODE_PATH=${nodeModules}
2020-08-29 10:51:38 +00:00
pushd $appdir
2022-12-14 02:24:38 +00:00
python test/runner.py test_hello_world
2020-08-29 10:51:38 +00:00
popd
2021-08-07 07:45:26 +00:00
runHook postInstall
'';
passthru = {
# HACK: Make emscripten look more like a cc-wrapper to GHC
# when building the javascript backend.
targetPrefix = "em";
bintools = emscripten;
};
meta = with lib; {
homepage = "https://github.com/emscripten-core/emscripten";
description = "An LLVM-to-JavaScript Compiler";
platforms = platforms.all;
maintainers = with maintainers; [ qknight matthewbauer raitobezarius ];
license = licenses.ncsa;
};
}