Clone of https://github.com/NixOS/nixpkgs.git (to stress-test knotserver)
1{
2 lib,
3 stdenv,
4 fetchFromGitHub,
5 fetchpatch,
6 python3,
7 nodejs,
8 closurecompiler,
9 jre,
10 binaryen,
11 llvmPackages,
12 symlinkJoin,
13 makeWrapper,
14 replaceVars,
15 buildNpmPackage,
16 emscripten,
17}:
18
19stdenv.mkDerivation rec {
20 pname = "emscripten";
21 version = "4.0.11";
22
23 llvmEnv = symlinkJoin {
24 name = "emscripten-llvm-${version}";
25 paths = with llvmPackages; [
26 clang-unwrapped
27 (lib.getLib clang-unwrapped)
28 lld
29 llvm
30 ];
31 };
32
33 nodeModules = buildNpmPackage {
34 name = "emscripten-node-modules-${version}";
35 inherit pname version src;
36
37 npmDepsHash = "sha256-Pos7pSboTIpGKtlBm56hJPYb1lDydmUwW1urHetFfeQ=";
38
39 dontBuild = true;
40
41 # Copy node_modules directly.
42 installPhase = ''
43 cp -r node_modules $out/
44 '';
45 };
46
47 src = fetchFromGitHub {
48 owner = "emscripten-core";
49 repo = "emscripten";
50 hash = "sha256-QNbXgTkzI0fqvg3nU2TAE8A8h6MNMm2TsTzOXBdpqYA=";
51 rev = version;
52 };
53
54 nativeBuildInputs = [ makeWrapper ];
55 buildInputs = [
56 nodejs
57 python3
58 ];
59
60 patches = [
61 (replaceVars ./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 4 requires LLVM tip-of-tree instead of LLVM 20
72 sed -i -e "s/EXPECTED_LLVM_VERSION = 21/EXPECTED_LLVM_VERSION = 20.1/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/.bin
107 cp -r ${nodeModules}/* $appdir/node_modules
108 cp -r ${nodeModules}/* $appdir/node_modules/.bin
109
110 cp ${./locate_cache.sh} $appdir/locate_cache.sh
111 chmod +x $appdir/locate_cache.sh
112
113 export EM_CACHE=$out/share/emscripten/cache
114
115 mkdir -p $out/bin
116 for b in em++ em-config emar embuilder.py emcc emcmake emconfigure emmake emranlib emrun emscons emsize; do
117 makeWrapper $appdir/$b $out/bin/$b \
118 --set NODE_PATH ${nodeModules} \
119 --set EM_EXCLUSIVE_CACHE_ACCESS 1 \
120 --set PYTHON ${python3}/bin/python \
121 --run "source $appdir/locate_cache.sh"
122 done
123
124 # precompile libc (etc.) in all variants:
125 pushd $TMPDIR
126 echo 'int __main_argc_argv( int a, int b ) { return 42; }' >test.c
127 for LTO in -flto ""; do
128 for BIND in "" "--bind"; do
129 # starting with emscripten 3.1.32+,
130 # if pthreads and relocatable are both used,
131 # _emscripten_thread_exit_joinable must be exported
132 # (see https://github.com/emscripten-core/emscripten/pull/18376)
133 # TODO: get library cache to build with both enabled and function exported
134 $out/bin/emcc $LTO $BIND test.c
135 $out/bin/emcc $LTO $BIND -s RELOCATABLE test.c
136 # starting with emscripten 3.1.48+,
137 # to use pthreads, _emscripten_check_mailbox must be exported
138 # (see https://github.com/emscripten-core/emscripten/pull/20604)
139 # TODO: get library cache to build with pthreads at all
140 # $out/bin/emcc $LTO $BIND -s USE_PTHREADS test.c
141 done
142 done
143 popd
144
145 export PYTHON=${python3}/bin/python
146 export NODE_PATH=${nodeModules}
147 pushd $appdir
148 python test/runner.py test_hello_world
149 popd
150
151 runHook postInstall
152 '';
153
154 passthru = {
155 # HACK: Make emscripten look more like a cc-wrapper to GHC
156 # when building the javascript backend.
157 targetPrefix = "em";
158 bintools = emscripten;
159 };
160
161 meta = with lib; {
162 homepage = "https://github.com/emscripten-core/emscripten";
163 description = "LLVM-to-JavaScript Compiler";
164 platforms = platforms.all;
165 maintainers = with maintainers; [
166 qknight
167 matthewbauer
168 raitobezarius
169 willcohen
170 ];
171 license = licenses.ncsa;
172 };
173}