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