1{ lib, stdenv, fetchFromGitHub, python3, nodejs, closurecompiler
2, jre, binaryen
3, llvmPackages
4, symlinkJoin, makeWrapper, substituteAll
5, buildNpmPackage
6, emscripten
7}:
8
9stdenv.mkDerivation rec {
10 pname = "emscripten";
11 version = "3.1.51";
12
13 llvmEnv = symlinkJoin {
14 name = "emscripten-llvm-${version}";
15 paths = with llvmPackages; [ clang-unwrapped clang-unwrapped.lib lld llvm ];
16 };
17
18 nodeModules = buildNpmPackage {
19 name = "emscripten-node-modules-${version}";
20 inherit pname version src;
21
22 npmDepsHash = "sha256-N7WbxzKvW6FljY6g3R//9RdNiezhXGEvKPbOSJgdA0g=";
23
24 dontBuild = true;
25
26 # Copy node_modules directly.
27 installPhase = ''
28 cp -r node_modules $out/
29 '';
30 };
31
32 src = fetchFromGitHub {
33 owner = "emscripten-core";
34 repo = "emscripten";
35 hash = "sha256-oXecS6B0u8YLeoybjxLwx5INGj/Kp/8GA6s3A1S0y4k=";
36 rev = version;
37 };
38
39 nativeBuildInputs = [ makeWrapper ];
40 buildInputs = [ nodejs python3 ];
41
42 patches = [
43 (substituteAll {
44 src = ./0001-emulate-clang-sysroot-include-logic.patch;
45 resourceDir = "${llvmEnv}/lib/clang/17/";
46 })
47 ];
48
49 buildPhase = ''
50 runHook preBuild
51
52 patchShebangs .
53
54 # emscripten 3.1.50 requires LLVM tip-of-tree instead of LLVM 17
55 sed -i -e "s/EXPECTED_LLVM_VERSION = 18/EXPECTED_LLVM_VERSION = 17.0/g" tools/shared.py
56
57 # fixes cmake support
58 sed -i -e "s/print \('emcc (Emscript.*\)/sys.stderr.write(\1); sys.stderr.flush()/g" emcc.py
59
60 sed -i "/^def check_sanity/a\\ return" tools/shared.py
61
62 echo "EMSCRIPTEN_ROOT = '$out/share/emscripten'" > .emscripten
63 echo "LLVM_ROOT = '${llvmEnv}/bin'" >> .emscripten
64 echo "NODE_JS = '${nodejs}/bin/node'" >> .emscripten
65 echo "JS_ENGINES = [NODE_JS]" >> .emscripten
66 echo "CLOSURE_COMPILER = ['${closurecompiler}/bin/closure-compiler']" >> .emscripten
67 echo "JAVA = '${jre}/bin/java'" >> .emscripten
68 # to make the test(s) below work
69 # echo "SPIDERMONKEY_ENGINE = []" >> .emscripten
70 echo "BINARYEN_ROOT = '${binaryen}'" >> .emscripten
71
72 # make emconfigure/emcmake use the correct (wrapped) binaries
73 sed -i "s|^EMCC =.*|EMCC='$out/bin/emcc'|" tools/shared.py
74 sed -i "s|^EMXX =.*|EMXX='$out/bin/em++'|" tools/shared.py
75 sed -i "s|^EMAR =.*|EMAR='$out/bin/emar'|" tools/shared.py
76 sed -i "s|^EMRANLIB =.*|EMRANLIB='$out/bin/emranlib'|" tools/shared.py
77
78 runHook postBuild
79 '';
80
81 installPhase = ''
82 runHook preInstall
83
84 appdir=$out/share/emscripten
85 mkdir -p $appdir
86 cp -r . $appdir
87 chmod -R +w $appdir
88
89 mkdir -p $appdir/node_modules
90 cp -r ${nodeModules}/* $appdir/node_modules
91
92 mkdir -p $out/bin
93 for b in em++ em-config emar embuilder.py emcc emcmake emconfigure emmake emranlib emrun emscons emsize; do
94 makeWrapper $appdir/$b $out/bin/$b \
95 --set NODE_PATH ${nodeModules} \
96 --set EM_EXCLUSIVE_CACHE_ACCESS 1 \
97 --set PYTHON ${python3}/bin/python
98 done
99
100 # precompile libc (etc.) in all variants:
101 pushd $TMPDIR
102 echo 'int __main_argc_argv( int a, int b ) { return 42; }' >test.c
103 for LTO in -flto ""; do
104 for BIND in "" "--bind"; do
105 # starting with emscripten 3.1.32+,
106 # if pthreads and relocatable are both used,
107 # _emscripten_thread_exit_joinable must be exported
108 # (see https://github.com/emscripten-core/emscripten/pull/18376)
109 # TODO: get library cache to build with both enabled and function exported
110 $out/bin/emcc $LTO $BIND test.c
111 $out/bin/emcc $LTO $BIND -s RELOCATABLE test.c
112 # starting with emscripten 3.1.48+,
113 # to use pthreads, _emscripten_check_mailbox must be exported
114 # (see https://github.com/emscripten-core/emscripten/pull/20604)
115 # TODO: get library cache to build with pthreads at all
116 # $out/bin/emcc $LTO $BIND -s USE_PTHREADS test.c
117 done
118 done
119 popd
120
121 export PYTHON=${python3}/bin/python
122 export NODE_PATH=${nodeModules}
123 pushd $appdir
124 python test/runner.py test_hello_world
125 popd
126
127 runHook postInstall
128 '';
129
130 passthru = {
131 # HACK: Make emscripten look more like a cc-wrapper to GHC
132 # when building the javascript backend.
133 targetPrefix = "em";
134 bintools = emscripten;
135 };
136
137 meta = with lib; {
138 homepage = "https://github.com/emscripten-core/emscripten";
139 description = "An LLVM-to-JavaScript Compiler";
140 platforms = platforms.all;
141 maintainers = with maintainers; [ qknight matthewbauer raitobezarius willcohen ];
142 license = licenses.ncsa;
143 };
144}