1{
2 lib,
3 stdenv,
4 fetchFromGitHub,
5 python3,
6 nodejs,
7 closurecompiler,
8 jre,
9 binaryen,
10 llvmPackages,
11 symlinkJoin,
12 makeWrapper,
13 substituteAll,
14 buildNpmPackage,
15 emscripten,
16}:
17
18stdenv.mkDerivation rec {
19 pname = "emscripten";
20 version = "3.1.73";
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-bqxUlxpIH1IAx9RbnaMq4dZW8fy+M/Q02Q7VrW/AKNQ=";
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-QlC2k2rhF3/Pz+knnrlBDV8AfHHBSlGr7b9Ae6TNsxY=";
50 rev = version;
51 };
52
53 nativeBuildInputs = [ makeWrapper ];
54 buildInputs = [
55 nodejs
56 python3
57 ];
58
59 patches = [
60 (substituteAll {
61 src = ./0001-emulate-clang-sysroot-include-logic.patch;
62 resourceDir = "${llvmEnv}/lib/clang/${lib.versions.major llvmPackages.llvm.version}/";
63 })
64 ];
65
66 buildPhase = ''
67 runHook preBuild
68
69 patchShebangs .
70
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
73
74 # fixes cmake support
75 sed -i -e "s/print \('emcc (Emscript.*\)/sys.stderr.write(\1); sys.stderr.flush()/g" emcc.py
76
77 sed -i "/^def check_sanity/a\\ return" tools/shared.py
78
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
88
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
94
95 runHook postBuild
96 '';
97
98 installPhase = ''
99 runHook preInstall
100
101 appdir=$out/share/emscripten
102 mkdir -p $appdir
103 cp -r . $appdir
104 chmod -R +w $appdir
105
106 mkdir -p $appdir/node_modules
107 cp -r ${nodeModules}/* $appdir/node_modules
108
109 mkdir -p $out/bin
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
115 done
116
117 # precompile libc (etc.) in all variants:
118 pushd $TMPDIR
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
134 done
135 done
136 popd
137
138 export PYTHON=${python3}/bin/python
139 export NODE_PATH=${nodeModules}
140 pushd $appdir
141 python test/runner.py test_hello_world
142 popd
143
144 runHook postInstall
145 '';
146
147 passthru = {
148 # HACK: Make emscripten look more like a cc-wrapper to GHC
149 # when building the javascript backend.
150 targetPrefix = "em";
151 bintools = emscripten;
152 };
153
154 meta = with lib; {
155 homepage = "https://github.com/emscripten-core/emscripten";
156 description = "LLVM-to-JavaScript Compiler";
157 platforms = platforms.all;
158 maintainers = with maintainers; [
159 qknight
160 matthewbauer
161 raitobezarius
162 willcohen
163 ];
164 license = licenses.ncsa;
165 };
166}