Skip to content

Commit 5748187

Browse files
authored
Add emscripten overlay with fixes needed for ghcjs (#609)
The newer versions of ghcjs use emscripten to deal with .c code. The version of emscripten that is available by default in nixpkgs does not include the "upstream" version of emscripten. This commit takes the existing emscripten from nixpkgs and adds the "upstream" version.
1 parent 341341c commit 5748187

File tree

8 files changed

+310
-0
lines changed

8 files changed

+310
-0
lines changed

overlays/default.nix

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ let
1212
armv6l-linux = import ./armv6l-linux.nix;
1313
musl = import ./musl.nix;
1414
tools = import ./tools.nix;
15+
emscripten = import ./emscripten.nix;
1516
};
1617

1718
composeExtensions = f: g: final: prev:
@@ -38,6 +39,7 @@ let
3839
armv6l-linux
3940
musl
4041
tools
42+
emscripten
4143
# Restore nixpkgs haskell and haskellPackages
4244
(_: prev: { inherit (prev.haskell-nix-prev) haskell haskellPackages; })
4345
];

overlays/emscripten.nix

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
final: prev: with final; {
2+
3+
binaryen = callPackage ./emscripten/binaryen.nix {};
4+
5+
emscriptenVersion = "1.39.1";
6+
7+
emscripten = callPackage ./emscripten { };
8+
9+
emscriptenfastcompPackages = dontRecurseIntoAttrs (callPackage ./emscripten/fastcomp { });
10+
11+
emscriptenfastcomp = emscriptenfastcompPackages.emscriptenfastcomp;
12+
13+
emscriptenupstreamPackages = dontRecurseIntoAttrs (callPackage ./emscripten/upstream { });
14+
15+
emscriptenupstream = emscriptenupstreamPackages.emscriptenupstream;
16+
17+
# emscriptenPackages = recurseIntoAttrs (callPackage ./emscripten-packages.nix { });
18+
19+
emscriptenStdenv = stdenv // { mkDerivation = buildEmscriptenPackage; };
20+
}

overlays/emscripten/binaryen.nix

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
{ stdenv, cmake, python3, fetchFromGitHub, emscriptenRev ? null, substituteAll }:
2+
3+
let
4+
defaultVersion = "93";
5+
6+
# Map from git revs to SHA256 hashes
7+
sha256s = {
8+
version_93 = "15y3703ha6mxcq168br0wlydas5rq66msrcsd650zrjz0ndxwsfx";
9+
"1.39.1" = "0ygm9m5322h4vfpf3j63q32qxk2l26yk62hh7dkb49j51zwl1y3y";
10+
};
11+
in
12+
13+
stdenv.mkDerivation rec {
14+
version = if emscriptenRev == null
15+
then defaultVersion
16+
else "emscripten-${emscriptenRev}";
17+
rev = if emscriptenRev == null
18+
then "version_${version}"
19+
else emscriptenRev;
20+
pname = "binaryen";
21+
22+
src = fetchFromGitHub {
23+
owner = "WebAssembly";
24+
repo = "binaryen";
25+
sha256 =
26+
if builtins.hasAttr rev sha256s
27+
then builtins.getAttr rev sha256s
28+
else null;
29+
inherit rev;
30+
};
31+
32+
patches = stdenv.lib.optional (emscriptenRev != null) (substituteAll {
33+
src = ./0001-Get-rid-of-git-dependency.patch;
34+
emscriptenv = "1.39.1";
35+
});
36+
37+
nativeBuildInputs = [ cmake python3 ];
38+
39+
meta = with stdenv.lib; {
40+
homepage = "https://github.com/WebAssembly/binaryen";
41+
description = "Compiler infrastructure and toolchain library for WebAssembly, in C++";
42+
platforms = platforms.all;
43+
maintainers = with maintainers; [ asppsa ];
44+
license = licenses.asl20;
45+
};
46+
}

overlays/emscripten/default.nix

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
{ emscriptenVersion, stdenv, fetchFromGitHub, emscriptenfastcomp, python, nodejs, closurecompiler
2+
, jre, binaryen, enableWasm ? true , cmake, emscriptenBackend ? emscriptenfastcomp
3+
}:
4+
5+
let
6+
rev = emscriptenVersion;
7+
appdir = "share/emscripten";
8+
binaryenVersioned = binaryen;
9+
in
10+
11+
stdenv.mkDerivation {
12+
name = "emscripten-${rev}";
13+
14+
src = fetchFromGitHub {
15+
owner = "emscripten-core";
16+
repo = "emscripten";
17+
sha256 = "10f2mlwh4s6v3rqghb7zdmv4ry0a4si915x4v17vp4nv7swkkwhn";
18+
rev = "fc6a4bb97b421b6dcf5806d0dc42ab8ddd4da026";
19+
};
20+
21+
buildInputs = [ nodejs cmake python ];
22+
23+
buildCommand = ''
24+
mkdir -p $out/${appdir}
25+
cp -r $src/* $out/${appdir}
26+
chmod -R +w $out/${appdir}
27+
grep -rl '^#!/usr.*python' $out/${appdir} | xargs sed -i -s 's@^#!/usr.*python.*@#!${python}/bin/python@'
28+
sed -i -e "s,EM_CONFIG = '~/.emscripten',EM_CONFIG = '$out/${appdir}/config'," $out/${appdir}/tools/shared.py
29+
sed -i -e 's,^.*did not see a source tree above the LLVM.*$, return True,' $out/${appdir}/tools/shared.py
30+
sed -i -e 's,def check_sanity(force=False):,def check_sanity(force=False):\n return,' $out/${appdir}/tools/shared.py
31+
# fixes cmake support
32+
sed -i -e "s/print \('emcc (Emscript.*\)/sys.stderr.write(\1); sys.stderr.flush()/g" $out/${appdir}/emcc.py
33+
mkdir $out/bin
34+
ln -s $out/${appdir}/{em++,em-config,emar,emar.py,embuilder.py,emcc,emcc.py,emcmake,emconfigure,emlink.py,emmake,emranlib,emranlib.py,emrun,emscons} $out/bin
35+
36+
echo "EMSCRIPTEN_ROOT = '$out/${appdir}'" > $out/${appdir}/config
37+
echo "LLVM_ROOT = '${emscriptenBackend}/bin'" >> $out/${appdir}/config
38+
echo "PYTHON = '${python}/bin/python'" >> $out/${appdir}/config
39+
echo "NODE_JS = '${nodejs}/bin/node'" >> $out/${appdir}/config
40+
echo "JS_ENGINES = [NODE_JS]" >> $out/${appdir}/config
41+
echo "COMPILER_ENGINE = NODE_JS" >> $out/${appdir}/config
42+
echo "CLOSURE_COMPILER = '${closurecompiler}/share/java/closure-compiler-v${closurecompiler.version}.jar'" >> $out/${appdir}/config
43+
echo "JAVA = '${jre}/bin/java'" >> $out/${appdir}/config
44+
# to make the test(s) below work
45+
echo "SPIDERMONKEY_ENGINE = []" >> $out/${appdir}/config
46+
echo "EMCC_FAST_COMPILER = 0" >> $out/${appdir}/config
47+
''
48+
+ stdenv.lib.optionalString enableWasm ''
49+
echo "BINARYEN_ROOT = '${binaryenVersioned}'" >> $out/share/emscripten/config
50+
''
51+
+
52+
''
53+
echo "--------------- running test -----------------"
54+
# quick hack to get the test working
55+
HOME=$TMPDIR
56+
cp $out/${appdir}/config $HOME/.emscripten
57+
export PATH=$PATH:$out/bin
58+
59+
#export EMCC_DEBUG=2
60+
${python}/bin/python $src/tests/runner.py test_hello_world
61+
echo "--------------- /running test -----------------"
62+
'';
63+
64+
meta = with stdenv.lib; {
65+
homepage = "https://github.com/emscripten-core/emscripten";
66+
description = "An LLVM-to-JavaScript Compiler";
67+
platforms = platforms.all;
68+
maintainers = with maintainers; [ qknight matthewbauer ];
69+
license = licenses.ncsa;
70+
};
71+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{ newScope, stdenv, binutils, wrapCCWith, symlinkJoin }:
2+
let
3+
callPackage = newScope (self // {inherit stdenv;});
4+
5+
self = {
6+
emscriptenfastcomp-unwrapped = callPackage ./emscripten-fastcomp.nix {};
7+
emscriptenfastcomp-wrapped = wrapCCWith {
8+
cc = self.emscriptenfastcomp-unwrapped;
9+
# Never want Apple's cctools for WASM target
10+
bintools = binutils;
11+
libc = stdenv.cc.libc;
12+
extraBuildCommands = ''
13+
# hardening flags break WASM support
14+
cat > $out/nix-support/add-hardening.sh
15+
'';
16+
};
17+
emscriptenfastcomp = symlinkJoin {
18+
name = "emscriptenfastcomp-${stdenv.lib.getVersion self.emscriptenfastcomp-unwrapped}";
19+
paths = [ self.emscriptenfastcomp-wrapped self.emscriptenfastcomp-unwrapped ];
20+
preferLocalBuild = false;
21+
allowSubstitutes = true;
22+
postBuild = ''
23+
# replace unwrapped clang-3.9 binary by wrapper
24+
ln -sf $out/bin/clang $out/bin/clang-[0-9]*
25+
'';
26+
};
27+
};
28+
in self
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
{ emscriptenVersion, stdenv, fetchFromGitHub, cmake, python, gtest, ... }:
2+
3+
let
4+
rev = emscriptenVersion;
5+
gcc = if stdenv.cc.isGNU then stdenv.cc.cc else stdenv.cc.cc.gcc;
6+
in
7+
stdenv.mkDerivation rec {
8+
name = "emscripten-fastcomp-${rev}";
9+
10+
src = fetchFromGitHub {
11+
owner = "emscripten-core";
12+
repo = "emscripten-fastcomp";
13+
sha256 = "0bd0l5k2fa4k0nax2cpxi003pqffqivx4z4m2j5xdha1a12sid8i";
14+
inherit rev;
15+
};
16+
17+
srcFL = fetchFromGitHub {
18+
owner = "emscripten-core";
19+
repo = "emscripten-fastcomp-clang";
20+
sha256 = "1iw2qplhp489qzw0rma73sab7asnm27g4m95sr36c6kq9cq6agri";
21+
inherit rev;
22+
};
23+
24+
nativeBuildInputs = [ cmake python gtest ];
25+
preConfigure = ''
26+
cp -Lr ${srcFL} tools/clang
27+
chmod +w -R tools/clang
28+
'';
29+
cmakeFlags = [
30+
"-DCMAKE_BUILD_TYPE=Release"
31+
"-DLLVM_TARGETS_TO_BUILD='X86;JSBackend'"
32+
"-DLLVM_INCLUDE_EXAMPLES=OFF"
33+
"-DLLVM_INCLUDE_TESTS=ON"
34+
#"-DLLVM_CONFIG=${llvm}/bin/llvm-config"
35+
"-DLLVM_BUILD_TESTS=ON"
36+
"-DCLANG_INCLUDE_TESTS=ON"
37+
] ++ (stdenv.lib.optional stdenv.isLinux
38+
# necessary for clang to find crtend.o
39+
"-DGCC_INSTALL_PREFIX=${gcc}"
40+
);
41+
enableParallelBuilding = true;
42+
43+
passthru = {
44+
isClang = true;
45+
inherit gcc;
46+
};
47+
48+
meta = with stdenv.lib; {
49+
homepage = "https://github.com/emscripten-core/emscripten-fastcomp";
50+
description = "Emscripten LLVM";
51+
platforms = platforms.all;
52+
maintainers = with maintainers; [ qknight matthewbauer ];
53+
license = stdenv.lib.licenses.ncsa;
54+
};
55+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{ newScope, stdenv, binutils, wrapCCWith, symlinkJoin }:
2+
let
3+
callPackage = newScope (self // {inherit stdenv;});
4+
5+
self = {
6+
emscriptenupstream-unwrapped = callPackage ./emscripten-upstream.nix {};
7+
emscriptenupstream-wrapped = wrapCCWith {
8+
cc = self.emscriptenupstream-unwrapped;
9+
# Never want Apple's cctools for WASM target
10+
bintools = binutils;
11+
libc = stdenv.cc.libc;
12+
extraBuildCommands = ''
13+
# hardening flags break WASM support
14+
cat > $out/nix-support/add-hardening.sh
15+
'';
16+
};
17+
emscriptenupstream = symlinkJoin {
18+
name = "emscriptenupstream-${stdenv.lib.getVersion self.emscriptenupstream-unwrapped}";
19+
paths = [ self.emscriptenupstream-wrapped self.emscriptenupstream-unwrapped ];
20+
preferLocalBuild = false;
21+
allowSubstitutes = true;
22+
postBuild = ''
23+
# replace unwrapped clang-3.9 binary by wrapper
24+
ln -sf $out/bin/clang $out/bin/clang-[0-9]*
25+
'';
26+
};
27+
};
28+
in self
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
{ emscriptenVersion, stdenv, fetchFromGitHub, cmake, python, gtest, ... }:
2+
3+
let
4+
gcc = if stdenv.cc.isGNU then stdenv.cc.cc else stdenv.cc.cc.gcc;
5+
in
6+
stdenv.mkDerivation rec {
7+
name = "emscripten-upstream";
8+
9+
src = (fetchFromGitHub {
10+
owner = "llvm";
11+
repo = "llvm-project";
12+
rev = "5f7ea85e789d5b5f3f463e538a28c040e373620b";
13+
sha256 = "1kik1zdl1igkdri9zkdri7jwjjfjwhzniaw2a57lr80ajhs60kr7";
14+
});
15+
16+
nativeBuildInputs = [ cmake python gtest ];
17+
setSourceRoot = ''
18+
sourceRoot=$(echo */llvm)
19+
'';
20+
cmakeFlags = [
21+
"-DCMAKE_CXX_FLAGS=-Wno-nonportable-include-path"
22+
"-DLLVM_ENABLE_LIBXML2=OFF"
23+
"-DLLVM_INCLUDE_EXAMPLES=OFF"
24+
"-DCOMPILER_RT_BUILD_XRAY=OFF"
25+
"-DCOMPILER_RT_INCLUDE_TESTS=OFF"
26+
"-DCOMPILER_RT_ENABLE_IOS=OFF"
27+
"-DLLVM_BUILD_LLVM_DYLIB=ON"
28+
"-DLLVM_LINK_LLVM_DYLIB=ON"
29+
"-DLLVM_TOOL_LTO_BUILD=OFF"
30+
# "-DLLVM_INSTALL_TOOLCHAIN_ONLY=ON"
31+
"-DLLVM_ENABLE_ASSERTIONS=ON"
32+
"-DLLVM_TARGETS_TO_BUILD=X86;WebAssembly"
33+
"-DLLVM_ENABLE_PROJECTS=lld;clang"
34+
"-DLLVM_ENABLE_TERMINFO=0"
35+
36+
"-DCMAKE_BUILD_TYPE=Release"
37+
"-DLLVM_INCLUDE_EXAMPLES=OFF"
38+
"-DLLVM_INCLUDE_TESTS=ON"
39+
#"-DLLVM_CONFIG=${llvm}/bin/llvm-config"
40+
"-DLLVM_BUILD_TESTS=ON"
41+
"-DCLANG_INCLUDE_TESTS=OFF"
42+
] ++ (stdenv.lib.optional stdenv.isLinux
43+
# necessary for clang to find crtend.o
44+
"-DGCC_INSTALL_PREFIX=${gcc}"
45+
);
46+
enableParallelBuilding = true;
47+
48+
passthru = {
49+
isClang = true;
50+
inherit gcc;
51+
};
52+
53+
meta = with stdenv.lib; {
54+
homepage = "http://github.com/llvm/llvm-project";
55+
description = "LLVM";
56+
platforms = platforms.all;
57+
maintainers = with maintainers; [ ];
58+
license = stdenv.lib.licenses.ncsa;
59+
};
60+
}

0 commit comments

Comments
 (0)