1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
|
{
lib,
stdenv,
fetchFromGitHub,
python3,
nodejs,
closurecompiler,
jre,
binaryen,
llvmPackages,
symlinkJoin,
makeWrapper,
replaceVars,
buildNpmPackage,
nix-update-script,
emscripten,
}:
let
pythonWithPsutil = python3.withPackages (ps: [ ps.psutil ]);
in
stdenv.mkDerivation rec {
pname = "emscripten";
version = "6.0.9";
llvmEnv = symlinkJoin {
name = "emscripten-llvm-${version}";
paths = with llvmPackages; [
clang-unwrapped
(lib.getLib clang-unwrapped)
lld
llvm
];
};
nodeModules = buildNpmPackage {
name = "emscripten-node-modules-${version}";
inherit pname version src;
npmDepsHash = "sha256-t0ekoKE9LQk6an4Sk51IOqKBOSmA7sbnoYLsOlsEZIo=";
dontBuild = true;
# Copy node_modules directly.
installPhase = ''
cp -r node_modules $out/
'';
};
src = fetchFromGitHub {
owner = "emscripten-core";
repo = "emscripten";
hash = "sha256-zfNbudFzII/nc0oyojalEaVppSWbCg7v1yiM7ENYQSE=";
rev = version;
};
strictDeps = true;
nativeBuildInputs = [
makeWrapper
python3
];
buildInputs = [
nodejs
];
patches = [
(replaceVars ./0001-emulate-clang-sysroot-include-logic.patch {
resourceDir = "${llvmEnv}/lib/clang/${lib.versions.major llvmPackages.llvm.version}/";
})
# Remove this patch when llvmPackages reaches LLVM 23
./0002-libunwind-restore-Unwind_CallPersonality.patch
];
buildPhase = ''
runHook preBuild
# Make Python scripts executable so patchShebangs will patch their shebangs
chmod +x *.py tools/*.py
patchShebangs .
# Emscripten requires an unreleased LLVM version. Set the check to the
# LLVM version that this package supplies. The --replace-fail flag stops
# the build if Emscripten changes this constant. A sed command for a
# fixed version does not give an error. It leaves the check at an LLVM
# version that this package does not have.
substituteInPlace tools/shared.py \
--replace-fail "EXPECTED_LLVM_VERSION = 24" \
"EXPECTED_LLVM_VERSION = ${lib.versions.major llvmPackages.llvm.version}"
# fixes cmake support
sed -i -e "s/print \('emcc (Emscript.*\)/sys.stderr.write(\1); sys.stderr.flush()/g" emcc.py
sed -i "/^def check_sanity/a\\ return" tools/shared.py
echo "EMSCRIPTEN_ROOT = '$out/share/emscripten'" > .emscripten
echo "LLVM_ROOT = '${llvmEnv}/bin'" >> .emscripten
echo "NODE_JS = '${nodejs}/bin/node'" >> .emscripten
echo "JS_ENGINES = [NODE_JS]" >> .emscripten
echo "CLOSURE_COMPILER = ['${closurecompiler}/bin/closure-compiler']" >> .emscripten
echo "JAVA = '${jre}/bin/java'" >> .emscripten
# to make the test(s) below work
# echo "SPIDERMONKEY_ENGINE = []" >> .emscripten
echo "BINARYEN_ROOT = '${binaryen}'" >> .emscripten
# make emconfigure/emcmake use the correct (wrapped) binaries
sed -i "s|^EMCC =.*|EMCC='$out/bin/emcc'|" tools/shared.py
sed -i "s|^EMXX =.*|EMXX='$out/bin/em++'|" tools/shared.py
sed -i "s|^EMAR =.*|EMAR='$out/bin/emar'|" tools/shared.py
sed -i "s|^EMRANLIB =.*|EMRANLIB='$out/bin/emranlib'|" tools/shared.py
# Fix /tmp symlink issue (macOS: /tmp -> /private/tmp) causing relpath miscalculation
sed -i 's/os\.path\.relpath(source_dir, build_dir)/os.path.relpath(source_dir, os.path.realpath(build_dir))/' tools/system_libs.py
sed -i 's/os\.path\.relpath(src, build_dir)/os.path.relpath(src, os.path.realpath(build_dir))/' tools/system_libs.py
# Verify the relpath fix was applied
grep -q 'os.path.realpath(build_dir)' tools/system_libs.py || (echo "ERROR: relpath fix not applied" && exit 1)
# Functional test: verify relpath resolves correctly through symlinks
${python3}/bin/python3 -c "
import os, tempfile
src = os.path.abspath('tools/system_libs.py')
with tempfile.TemporaryDirectory() as tmpdir:
build_dir_real = os.path.realpath(tmpdir)
relpath = os.path.relpath(src, build_dir_real)
resolved = os.path.normpath(os.path.join(build_dir_real, relpath))
assert resolved == src, f'relpath test failed: {resolved} != {src}'
print('relpath symlink fix test passed')
"
runHook postBuild
'';
installPhase = ''
runHook preInstall
appdir=$out/share/emscripten
mkdir -p $appdir
cp -r . $appdir
chmod -R +w $appdir
mkdir -p $appdir/node_modules/.bin
cp -r ${nodeModules}/* $appdir/node_modules
cp -r ${nodeModules}/* $appdir/node_modules/.bin
cp ${./locate_cache.sh} $appdir/locate_cache.sh
chmod +x $appdir/locate_cache.sh
export EM_CACHE=$out/share/emscripten/cache
mkdir -p $out/bin
# Wrap all tools consistently via their .py entry points
for b in em++ emcc em-config emar embuilder emcmake emconfigure emmake emranlib emrun emscons emsize; do
makeWrapper $appdir/$b.py $out/bin/$b \
--set NODE_PATH ${nodeModules} \
--set EM_EXCLUSIVE_CACHE_ACCESS 1 \
--set PYTHON ${python3}/bin/python \
--run "source $appdir/locate_cache.sh"
done
# Create extensionless aliases for tools that need them (e.g., file_packager)
for tool in file_packager; do
ln -sf $appdir/tools/$tool.py $appdir/tools/$tool
done
# Symlinks for CMake toolchain (expects tools in share/emscripten/)
for tool in emcc em++ em-config emar emranlib emcmake emconfigure; do
ln -sf $out/bin/$tool $appdir/$tool
done
# precompile libc (etc.) in all variants:
pushd $TMPDIR
echo 'int __main_argc_argv( int a, int b ) { return 42; }' >test.c
for LTO in -flto ""; do
for BIND in "" "--bind"; do
for PTHREAD in "" "-pthread"; do
$out/bin/emcc $LTO $BIND $PTHREAD test.c || true
done
done
done
popd
export PYTHON=${python3}/bin/python
export NODE_PATH=${nodeModules}
pushd $appdir
${pythonWithPsutil}/bin/python test/runner.py test_hello_world
popd
# fail if any .py files still have unpatched shebangs
if grep -l '#!/usr/bin/env' $appdir/*.py $appdir/tools/*.py 2>/dev/null; then
echo "ERROR: unpatched shebangs found in .py files"
exit 1
fi
runHook postInstall
'';
doInstallCheck = true;
# C++ exceptions with -fwasm-exceptions must compile and link
# see https://github.com/NixOS/nixpkgs/pull/556385
installCheckPhase = ''
runHook preInstallCheck
pushd $TMPDIR
echo 'int main() { try { throw 42; } catch (int) { return 0; } return 1; }' > throw.cpp
$out/bin/em++ -fwasm-exceptions throw.cpp -o throw.js
popd
runHook postInstallCheck
'';
passthru = {
# HACK: Make emscripten look more like a cc-wrapper to GHC
# when building the javascript backend.
targetPrefix = "em";
bintools = emscripten;
updateScript = nix-update-script {
extraArgs = [
"--subpackage"
"nodeModules"
];
};
};
meta = {
homepage = "https://github.com/emscripten-core/emscripten";
description = "LLVM-to-JavaScript Compiler";
platforms = lib.platforms.all;
maintainers = with lib.maintainers; [
qknight
willcohen
];
license = lib.licenses.ncsa;
};
}
|