18 stdenv.mkDerivation rec {
22 llvmEnv = symlinkJoin {
23 name = "emscripten-llvm-${version}";
24 paths = with llvmPackages; [
26 (lib.getLib clang-unwrapped)
32 nodeModules = buildNpmPackage {
33 name = "emscripten-node-modules-${version}";
34 inherit pname version src;
36 npmDepsHash = "sha256-bqxUlxpIH1IAx9RbnaMq4dZW8fy+M/Q02Q7VrW/AKNQ=";
40 # Copy node_modules directly.
42 cp -r node_modules $out/
46 src = fetchFromGitHub {
47 owner = "emscripten-core";
49 hash = "sha256-QlC2k2rhF3/Pz+knnrlBDV8AfHHBSlGr7b9Ae6TNsxY=";
53 nativeBuildInputs = [ makeWrapper ];
61 src = ./0001-emulate-clang-sysroot-include-logic.patch;
62 resourceDir = "${llvmEnv}/lib/clang/${lib.versions.major llvmPackages.llvm.version}/";
71 # emscripten 3.1.67 requires LLVM tip-of-tree instead of LLVM 18
72 sed -i -e "s/EXPECTED_LLVM_VERSION = 20/EXPECTED_LLVM_VERSION = 19/g" tools/shared.py
75 sed -i -e "s/print \('emcc (Emscript.*\)/sys.stderr.write(\1); sys.stderr.flush()/g" emcc.py
77 sed -i "/^def check_sanity/a\\ return" tools/shared.py
79 echo "EMSCRIPTEN_ROOT = '$out/share/emscripten'" > .emscripten
80 echo "LLVM_ROOT = '${llvmEnv}/bin'" >> .emscripten
81 echo "NODE_JS = '${nodejs}/bin/node'" >> .emscripten
82 echo "JS_ENGINES = [NODE_JS]" >> .emscripten
83 echo "CLOSURE_COMPILER = ['${closurecompiler}/bin/closure-compiler']" >> .emscripten
84 echo "JAVA = '${jre}/bin/java'" >> .emscripten
85 # to make the test(s) below work
86 # echo "SPIDERMONKEY_ENGINE = []" >> .emscripten
87 echo "BINARYEN_ROOT = '${binaryen}'" >> .emscripten
89 # make emconfigure/emcmake use the correct (wrapped) binaries
90 sed -i "s|^EMCC =.*|EMCC='$out/bin/emcc'|" tools/shared.py
91 sed -i "s|^EMXX =.*|EMXX='$out/bin/em++'|" tools/shared.py
92 sed -i "s|^EMAR =.*|EMAR='$out/bin/emar'|" tools/shared.py
93 sed -i "s|^EMRANLIB =.*|EMRANLIB='$out/bin/emranlib'|" tools/shared.py
101 appdir=$out/share/emscripten
106 mkdir -p $appdir/node_modules
107 cp -r ${nodeModules}/* $appdir/node_modules
110 for b in em++ em-config emar embuilder.py emcc emcmake emconfigure emmake emranlib emrun emscons emsize; do
111 makeWrapper $appdir/$b $out/bin/$b \
112 --set NODE_PATH ${nodeModules} \
113 --set EM_EXCLUSIVE_CACHE_ACCESS 1 \
114 --set PYTHON ${python3}/bin/python
117 # precompile libc (etc.) in all variants:
119 echo 'int __main_argc_argv( int a, int b ) { return 42; }' >test.c
120 for LTO in -flto ""; do
121 for BIND in "" "--bind"; do
122 # starting with emscripten 3.1.32+,
123 # if pthreads and relocatable are both used,
124 # _emscripten_thread_exit_joinable must be exported
125 # (see https://github.com/emscripten-core/emscripten/pull/18376)
126 # TODO: get library cache to build with both enabled and function exported
127 $out/bin/emcc $LTO $BIND test.c
128 $out/bin/emcc $LTO $BIND -s RELOCATABLE test.c
129 # starting with emscripten 3.1.48+,
130 # to use pthreads, _emscripten_check_mailbox must be exported
131 # (see https://github.com/emscripten-core/emscripten/pull/20604)
132 # TODO: get library cache to build with pthreads at all
133 # $out/bin/emcc $LTO $BIND -s USE_PTHREADS test.c
138 export PYTHON=${python3}/bin/python
139 export NODE_PATH=${nodeModules}
141 python test/runner.py test_hello_world
148 # HACK: Make emscripten look more like a cc-wrapper to GHC
149 # when building the javascript backend.
151 bintools = emscripten;
155 homepage = "https://github.com/emscripten-core/emscripten";
156 description = "LLVM-to-JavaScript Compiler";
157 platforms = platforms.all;
158 maintainers = with maintainers; [
164 license = licenses.ncsa;