-
Notifications
You must be signed in to change notification settings - Fork 1
/
flake.nix
104 lines (90 loc) · 3.88 KB
/
flake.nix
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
{
description = "LLVM Flang for WebAssembly";
inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixos-23.11";
};
outputs = { self, nixpkgs }:
let
allSystems =
[ "x86_64-linux" "aarch64-linux" "x86_64-darwin" "aarch64-darwin" ];
# Helper to provide system-specific attributes
forAllSystems = f:
nixpkgs.lib.genAttrs allSystems (system:
f {
pkgs = import nixpkgs { inherit system; };
inherit system;
flang-source = nixpkgs.legacyPackages.${system}.fetchgit {
url = "https://github.com/r-wasm/llvm-project";
# This is the tip of the wasm branch.
rev = "dd6047a18c5fc1c84978d141c5d820cf28e3c952";
hash = "sha256-iQ7GIFkzATKmTOzJB72ydQQZGcQnp2g29qR5tG7pLqw=";
};
});
in {
packages = forAllSystems ({ pkgs, flang-source, ... }: {
default = pkgs.stdenv.mkDerivation {
name = "flang-wasm";
src = ./.;
nativeBuildInputs = with pkgs; [
cacert # Needed for git clone to work on https repos
cmake
emscripten
git
libxml2
llvmPackages_16.bintools
llvmPackages_16.clang
ninja
python3
zlib
];
# The automatic configuration by stdenv.mkDerivation tries to do some
# cmake configuration, which causes the build to fail.
dontConfigure = true;
buildPhase = ''
if [ ! -d $(pwd)/.emscripten_cache-${pkgs.emscripten.version} ]; then
cp -R ${pkgs.emscripten}/share/emscripten/cache/ $(pwd)/.emscripten_cache-${pkgs.emscripten.version}
chmod u+rwX -R $(pwd)/.emscripten_cache-${pkgs.emscripten.version}
fi
export EM_CACHE=$(pwd)/.emscripten_cache-${pkgs.emscripten.version}
echo emscripten cache dir: $EM_CACHE
CMAKE_BUILD_PARALLEL_LEVEL=$NIX_BUILD_CORES make \
SOURCE=${flang-source} \
PREFIX=$out \
FLANG_WASM_CMAKE_VARS="-DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ -DLLVM_USE_LINKER=lld"
'';
installPhase = ''
make SOURCE=${flang-source} PREFIX=$out install
'';
};
});
# Development environment output
devShells = forAllSystems ({ pkgs, system, ... }: {
default = pkgs.mkShell {
# Get the nativeBuildInputs from packages.default
inputsFrom = [ self.packages.${system}.default ];
# Any additional Nix packages provided in the environment
packages = with pkgs; [ ];
# This is a workaround for nix emscripten cache directory not being
# writable. Borrowed from:
# https://discourse.nixos.org/t/improving-an-emscripten-yarn-dev-shell-flake/33045
# Issue at https://github.com/NixOS/nixpkgs/issues/139943
#
# Also note that `nix develop` must be run in the top-level directory
# of the project; otherwise this script will create the cache dir
# inside of the current working dir. Currently there isn't a way to
# the top-level dir from within this file, but there is an open issue
# for it. After that issue is fixed and the fixed version of nix is in
# widespread use, we'll be able to use
# https://github.com/NixOS/nix/issues/8034
shellHook = ''
if [ ! -d $(pwd)/.emscripten_cache-${pkgs.emscripten.version} ]; then
cp -R ${pkgs.emscripten}/share/emscripten/cache/ $(pwd)/.emscripten_cache-${pkgs.emscripten.version}
chmod u+rwX -R $(pwd)/.emscripten_cache-${pkgs.emscripten.version}
fi
export EM_CACHE=$(pwd)/.emscripten_cache-${pkgs.emscripten.version}
echo emscripten cache dir: $EM_CACHE
'';
};
});
};
}