2021-02-05 17:12:51 +00:00
|
|
|
{ lib, stdenv, fetchFromGitHub, python3, nodejs, closurecompiler
|
2020-09-25 04:45:31 +00:00
|
|
|
, jre, binaryen
|
2021-08-27 14:25:00 +00:00
|
|
|
, llvmPackages
|
2023-07-15 17:15:38 +00:00
|
|
|
, symlinkJoin, makeWrapper, substituteAll
|
2023-01-20 10:41:00 +00:00
|
|
|
, buildNpmPackage
|
2023-01-11 07:51:40 +00:00
|
|
|
, emscripten
|
2020-04-24 23:36:52 +00:00
|
|
|
}:
|
|
|
|
|
2020-09-25 04:45:31 +00:00
|
|
|
stdenv.mkDerivation rec {
|
|
|
|
pname = "emscripten";
|
2024-01-13 08:15:51 +00:00
|
|
|
version = "3.1.51";
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2020-09-25 04:45:31 +00:00
|
|
|
llvmEnv = symlinkJoin {
|
|
|
|
name = "emscripten-llvm-${version}";
|
2021-08-27 14:25:00 +00:00
|
|
|
paths = with llvmPackages; [ clang-unwrapped clang-unwrapped.lib lld llvm ];
|
2020-09-25 04:45:31 +00:00
|
|
|
};
|
|
|
|
|
2023-01-20 10:41:00 +00:00
|
|
|
nodeModules = buildNpmPackage {
|
2020-09-25 04:45:31 +00:00
|
|
|
name = "emscripten-node-modules-${version}";
|
2023-01-20 10:41:00 +00:00
|
|
|
inherit pname version src;
|
|
|
|
|
2024-01-13 08:15:51 +00:00
|
|
|
npmDepsHash = "sha256-N7WbxzKvW6FljY6g3R//9RdNiezhXGEvKPbOSJgdA0g=";
|
2023-01-20 10:41:00 +00:00
|
|
|
|
|
|
|
dontBuild = true;
|
|
|
|
|
|
|
|
# Copy node_modules directly.
|
|
|
|
installPhase = ''
|
|
|
|
cp -r node_modules $out/
|
|
|
|
'';
|
2020-09-25 04:45:31 +00:00
|
|
|
};
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
src = fetchFromGitHub {
|
|
|
|
owner = "emscripten-core";
|
|
|
|
repo = "emscripten";
|
2024-01-13 08:15:51 +00:00
|
|
|
hash = "sha256-oXecS6B0u8YLeoybjxLwx5INGj/Kp/8GA6s3A1S0y4k=";
|
2020-09-25 04:45:31 +00:00
|
|
|
rev = version;
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
2020-09-25 04:45:31 +00:00
|
|
|
nativeBuildInputs = [ makeWrapper ];
|
|
|
|
buildInputs = [ nodejs python3 ];
|
|
|
|
|
2021-08-27 14:25:00 +00:00
|
|
|
patches = [
|
|
|
|
(substituteAll {
|
|
|
|
src = ./0001-emulate-clang-sysroot-include-logic.patch;
|
2024-01-02 11:29:13 +00:00
|
|
|
resourceDir = "${llvmEnv}/lib/clang/17/";
|
2023-02-02 18:25:31 +00:00
|
|
|
})
|
2021-08-27 14:25:00 +00:00
|
|
|
];
|
|
|
|
|
2020-09-25 04:45:31 +00:00
|
|
|
buildPhase = ''
|
2021-08-27 14:25:00 +00:00
|
|
|
runHook preBuild
|
|
|
|
|
2020-09-25 04:45:31 +00:00
|
|
|
patchShebangs .
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2024-01-02 11:29:13 +00:00
|
|
|
# emscripten 3.1.50 requires LLVM tip-of-tree instead of LLVM 17
|
|
|
|
sed -i -e "s/EXPECTED_LLVM_VERSION = 18/EXPECTED_LLVM_VERSION = 17.0/g" tools/shared.py
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
# fixes cmake support
|
2020-09-25 04:45:31 +00:00
|
|
|
sed -i -e "s/print \('emcc (Emscript.*\)/sys.stderr.write(\1); sys.stderr.flush()/g" emcc.py
|
|
|
|
|
|
|
|
sed -i "/^def check_sanity/a\\ return" tools/shared.py
|
|
|
|
|
2021-07-24 12:14:16 +00:00
|
|
|
echo "EMSCRIPTEN_ROOT = '$out/share/emscripten'" > .emscripten
|
2020-09-25 04:45:31 +00:00
|
|
|
echo "LLVM_ROOT = '${llvmEnv}/bin'" >> .emscripten
|
|
|
|
echo "NODE_JS = '${nodejs}/bin/node'" >> .emscripten
|
|
|
|
echo "JS_ENGINES = [NODE_JS]" >> .emscripten
|
|
|
|
echo "CLOSURE_COMPILER = ['${closurecompiler}/bin/closure-compiler']" >> .emscripten
|
|
|
|
echo "JAVA = '${jre}/bin/java'" >> .emscripten
|
2020-04-24 23:36:52 +00:00
|
|
|
# to make the test(s) below work
|
2020-09-25 04:45:31 +00:00
|
|
|
# echo "SPIDERMONKEY_ENGINE = []" >> .emscripten
|
|
|
|
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-27 14:25:00 +00:00
|
|
|
runHook postBuild
|
2020-09-25 04:45:31 +00:00
|
|
|
'';
|
|
|
|
|
|
|
|
installPhase = ''
|
2021-08-27 14:25:00 +00:00
|
|
|
runHook preInstall
|
|
|
|
|
2020-09-25 04:45:31 +00:00
|
|
|
appdir=$out/share/emscripten
|
|
|
|
mkdir -p $appdir
|
|
|
|
cp -r . $appdir
|
|
|
|
chmod -R +w $appdir
|
|
|
|
|
2023-11-16 04:20:00 +00:00
|
|
|
mkdir -p $appdir/node_modules
|
|
|
|
cp -r ${nodeModules}/* $appdir/node_modules
|
|
|
|
|
2020-09-25 04:45:31 +00:00
|
|
|
mkdir -p $out/bin
|
2021-08-27 14:25:00 +00:00
|
|
|
for b in em++ em-config emar embuilder.py emcc emcmake emconfigure emmake emranlib emrun emscons emsize; do
|
2020-09-25 04:45:31 +00:00
|
|
|
makeWrapper $appdir/$b $out/bin/$b \
|
2023-01-20 10:41:00 +00:00
|
|
|
--set NODE_PATH ${nodeModules} \
|
2020-09-25 04:45:31 +00:00
|
|
|
--set EM_EXCLUSIVE_CACHE_ACCESS 1 \
|
|
|
|
--set PYTHON ${python3}/bin/python
|
|
|
|
done
|
|
|
|
|
|
|
|
# precompile libc (etc.) in all variants:
|
|
|
|
pushd $TMPDIR
|
2023-07-15 17:15:38 +00:00
|
|
|
echo 'int __main_argc_argv( int a, int b ) { return 42; }' >test.c
|
2020-09-25 04:45:31 +00:00
|
|
|
for LTO in -flto ""; do
|
2021-08-05 21:33:18 +00:00
|
|
|
for BIND in "" "--bind"; do
|
2023-07-15 17:15:38 +00:00
|
|
|
# starting with emscripten 3.1.32+,
|
|
|
|
# if pthreads and relocatable are both used,
|
|
|
|
# _emscripten_thread_exit_joinable must be exported
|
|
|
|
# (see https://github.com/emscripten-core/emscripten/pull/18376)
|
|
|
|
# TODO: get library cache to build with both enabled and function exported
|
|
|
|
$out/bin/emcc $LTO $BIND test.c
|
|
|
|
$out/bin/emcc $LTO $BIND -s RELOCATABLE test.c
|
2024-01-02 11:29:13 +00:00
|
|
|
# starting with emscripten 3.1.48+,
|
|
|
|
# to use pthreads, _emscripten_check_mailbox must be exported
|
|
|
|
# (see https://github.com/emscripten-core/emscripten/pull/20604)
|
|
|
|
# TODO: get library cache to build with pthreads at all
|
|
|
|
# $out/bin/emcc $LTO $BIND -s USE_PTHREADS test.c
|
2020-09-25 04:45:31 +00:00
|
|
|
done
|
|
|
|
done
|
|
|
|
popd
|
|
|
|
|
|
|
|
export PYTHON=${python3}/bin/python
|
2023-01-20 10:41:00 +00:00
|
|
|
export NODE_PATH=${nodeModules}
|
2020-09-25 04:45:31 +00:00
|
|
|
pushd $appdir
|
2023-01-20 10:41:00 +00:00
|
|
|
python test/runner.py test_hello_world
|
2020-09-25 04:45:31 +00:00
|
|
|
popd
|
2021-08-27 14:25:00 +00:00
|
|
|
|
|
|
|
runHook postInstall
|
2020-04-24 23:36:52 +00:00
|
|
|
'';
|
|
|
|
|
2023-01-11 07:51:40 +00:00
|
|
|
passthru = {
|
|
|
|
# HACK: Make emscripten look more like a cc-wrapper to GHC
|
|
|
|
# when building the javascript backend.
|
|
|
|
targetPrefix = "em";
|
|
|
|
bintools = emscripten;
|
|
|
|
};
|
|
|
|
|
2021-02-05 17:12:51 +00:00
|
|
|
meta = with lib; {
|
2020-04-24 23:36:52 +00:00
|
|
|
homepage = "https://github.com/emscripten-core/emscripten";
|
|
|
|
description = "An LLVM-to-JavaScript Compiler";
|
|
|
|
platforms = platforms.all;
|
2024-01-02 11:29:13 +00:00
|
|
|
maintainers = with maintainers; [ qknight matthewbauer raitobezarius willcohen ];
|
2020-04-24 23:36:52 +00:00
|
|
|
license = licenses.ncsa;
|
|
|
|
};
|
|
|
|
}
|