Skip to content

Commit a43b3c3

Browse files
committed
🍔 Use a larger runner for the LRE workflow
(draft) Rewrites large parts of the packaging logic. The regular runner doesn't have enough memory. Also implement some cursed hacks to make `mojo package` work in remote execution.
1 parent e57adb9 commit a43b3c3

File tree

13 files changed

+250
-128
lines changed

13 files changed

+250
-128
lines changed

.github/workflows/local-remote-execution.yaml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414
strategy:
1515
fail-fast: false
1616
matrix:
17-
os: [ubuntu-24.04]
17+
os: [large-ubuntu-22.04]
1818
name: ${{ matrix.os }}
1919
runs-on: ${{ matrix.os }}
2020
timeout-minutes: 45
@@ -52,4 +52,8 @@ jobs:
5252
- name: Run Mojo tests against the cluster
5353
run: |
5454
nix develop --impure --command \
55-
bash -c "lre-bazel test --jobs=4 @mojo//... --verbose_failures"
55+
bash -c "lre-bazel test \
56+
--jobs=$(nproc) \
57+
--keep_going \
58+
--verbose_failures \
59+
@mojo//..."

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Brings Mojo to all Linux distributions.
3131
Also, the LRE framework is still actively under development (in fact,
3232
`rules_mojo` acts as a validation case for LRE), so expect larger scale
3333
refactors.
34-
- At the moment there is no way to chose the Mojo version. `rules_mojo` uses
34+
- At the moment there is no way to choose the Mojo version. `rules_mojo` uses
3535
a pinned `nightly` toolchain that happened to not crash because stable
3636
crashed.
3737

examples/BUILD.bazel

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ mojo_binary(
1717
)
1818

1919
mojo_test(
20-
name = "hello_world_test",
20+
name = "hello_test",
2121
srcs = ["hello.mojo"],
22+
deps = [":mypackage"],
2223
)

examples/hello.mojo

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from mypackage.mymodule import MyPair
22

3+
34
fn main():
45
var mine = MyPair(2, 4)
56
mine.dump()

flake.nix

Lines changed: 21 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -81,59 +81,37 @@
8181
, ...
8282
}:
8383
let
84+
tinfo6-bin = pkgs.callPackage ./tinfo6-bin.nix { };
85+
86+
ncurses6-bin = pkgs.callPackage ./ncurses6-bin.nix { };
87+
8488
native-cli = inputs.nativelink.packages.${system}.native-cli;
8589

86-
lre-mojo-cluster = import ./local-remote-execution/lre-mojo-cluster.nix {
90+
lre-mojo-cluster = pkgs.callPackage ./local-remote-execution/lre-mojo-cluster.nix {
8791
inherit native-cli;
88-
inherit (pkgs)
89-
curl
90-
git
91-
kubectl
92-
kustomize
93-
nix
94-
writeShellScriptBin;
9592
};
9693

9794
lre-cc = nativelink.packages.${system}.lre-cc;
9895
inherit (nix2container.packages.${system}.nix2container) buildImage;
9996

100-
mojo-sdk = import ./mojo-sdk.nix {
101-
inherit (pkgs)
102-
autoPatchelfHook
103-
fetchurl
104-
glibc
105-
icu
106-
libedit
107-
libgcc
108-
libxml2
109-
ncurses
110-
stdenv
111-
zlib
112-
zstd;
97+
mojo-sdk = pkgs.callPackage ./mojo-sdk.nix {
98+
inherit (pkgs.lib) makeBinPath makeLibraryPath;
99+
ncurses = ncurses6-bin;
100+
tinfo = tinfo6-bin;
113101
};
114102

115-
lre-mojo = import ./local-remote-execution/lre-mojo.nix {
116-
inherit (pkgs)
117-
gcc
118-
lib
119-
ncurses
120-
python312
121-
zlib;
122-
inherit buildImage lre-cc mojo-sdk;
103+
lre-mojo = pkgs.callPackage ./local-remote-execution/lre-mojo.nix {
104+
# ncurses = ncurses6-bin;
105+
# tinfo = ncurses6-bin;
106+
mojoEnv = self.lib.defaultMojoEnv {
107+
inherit pkgs mojo-sdk;
108+
};
109+
inherit buildImage lre-cc;
123110
};
124111

125-
lre-kill-the-mojo = import ./local-remote-execution/lre-kill-the-mojo.nix {
126-
inherit (pkgs) docker findutils kind writeShellScriptBin;
127-
};
112+
lre-kill-the-mojo = pkgs.callPackage ./local-remote-execution/lre-kill-the-mojo.nix { };
128113

129-
createWorker = import ./local-remote-execution/create-worker.nix {
130-
inherit (pkgs)
131-
bash
132-
buildEnv
133-
coreutils
134-
lib
135-
runCommand
136-
runtimeShell;
114+
createWorker = pkgs.callPackage ./local-remote-execution/create-worker.nix {
137115
inherit buildImage self;
138116
nativelink = nativelink.packages.${system}.nativelink-debug;
139117
};
@@ -143,11 +121,7 @@
143121
exec ${pkgs.bazelisk}/bin/bazelisk "$@"
144122
'';
145123

146-
lre-bazel = import ./lre-bazel.nix {
147-
inherit bazel;
148-
inherit (pkgs) kubectl writeShellScriptBin;
149-
};
150-
124+
lre-bazel = pkgs.callPackage ./lre-bazel.nix { inherit bazel; };
151125
in
152126
{
153127
_module.args.pkgs = import self.inputs.nixpkgs {
@@ -186,11 +160,12 @@
186160
bazel
187161
lre-bazel
188162
pkgs.kubectl
189-
pkgs.zlib
190163
pkgs.python312
191164
pkgs.tektoncd-cli
192165
pkgs.kind
193166
pkgs.vale
167+
pkgs.jq
168+
pkgs.dive
194169
];
195170

196171
shellHook = ''

local-remote-execution/lre-mojo.nix

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,28 @@
11
{ buildImage
2-
, gcc
3-
, lib
42
, lre-cc
5-
, mojo-sdk
6-
, ncurses
7-
, python312
8-
, zlib
3+
, mojoEnv
94
, ...
105
}:
116

127
let
138
# This environment is shared between toolchain autogen images and the final
149
# toolchain image.
15-
Env = [
16-
# Add all tooling here so that the generated toolchains use `/nix/store/*`
17-
# paths instead of `/bin` or `/usr/bin`. This way we're guaranteed to use
18-
# binary identical toolchains during local and remote execution.
19-
("PATH="
20-
+ (lib.strings.concatStringsSep ":" [
21-
"${gcc}/bin"
22-
"${mojo-sdk}/bin"
23-
]))
24-
"MODULAR_HOME=${mojo-sdk}"
25-
"MOJO_COMPILER=${mojo-sdk}/bin/mojo"
26-
"MOJO_CC_PATH=${gcc}/bin:${mojo-sdk}/bin"
27-
"MOJO_LIBRARY_PATH=${zlib}/lib:${ncurses}/lib"
28-
"MOJO_PYTHON_LIBRARY=${python312}/lib"
29-
];
10+
Env = mojoEnv;
11+
# [
12+
# # Add all tooling here so that the generated toolchains use `/nix/store/*`
13+
# # paths instead of `/bin` or `/usr/bin`. This way we're guaranteed to use
14+
# # binary identical toolchains during local and remote execution.
15+
# ("PATH="
16+
# + (lib.strings.concatStringsSep ":" [
17+
# "${gcc}/bin"
18+
# "${mojo-sdk}/bin"
19+
# ]))
20+
# "MODULAR_HOME=${mojo-sdk}"
21+
# "MOJO_COMPILER=${mojo-sdk}/bin/mojo"
22+
# "MOJO_CC_PATH=${gcc}/bin:${mojo-sdk}/bin"
23+
# "MOJO_LIBRARY_PATH=${zlib}/lib:${ncurses}/lib"
24+
# "MOJO_PYTHON_LIBRARY=${python312}/lib"
25+
# ];
3026
in
3127
buildImage {
3228
name = "lre-mojo";

modules/defaultMojoEnv.nix

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
{ pkgs, mojo-sdk, ... }:
22

33
[
4+
# "PATH=${pkgs.lib.makeBinPath mojo-sdk.passthru.mojoBinPath}"
45
"MODULAR_HOME=${mojo-sdk}"
56
"MOJO_COMPILER=${mojo-sdk}/bin/mojo"
6-
"MOJO_CC_PATH=${pkgs.gcc}/bin:${mojo-sdk}/bin"
7-
"MOJO_LIBRARY_PATH=${pkgs.zlib}/lib:${pkgs.ncurses}/lib"
7+
"MOJO_CC_PATH=${pkgs.lib.makeBinPath mojo-sdk.passthru.mojoBinPath}"
8+
"MOJO_LIBRARY_PATH=${pkgs.lib.makeLibraryPath mojo-sdk.passthru.mojoLibraryPath}"
9+
"LD_LIBRARY_PATH=${pkgs.lib.makeLibraryPath mojo-sdk.passthru.mojoLibraryPath}"
10+
11+
# "MOJO_LIBRARY_PATH=${pkgs.zlib-ng.override{withZlibCompat=true;}}/lib:${pkgs.ncurses}/lib"
812

913
# TODO(aaronmondal): This needs to be set during runtime. Let's just add it to
1014
# a mojo wrapper script.

mojo-sdk.nix

Lines changed: 83 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,23 @@
55
, libedit
66
, libgcc
77
, libxml2
8+
, makeLibraryPath
9+
, makeWrapper
10+
, mold
811
, ncurses
912
, stdenv
10-
, zlib
13+
, tinfo
14+
, uutils-coreutils-noprefix
15+
, zlib-ng
1116
, zstd
17+
, clang
1218
, ...
1319
}:
20+
let
21+
22+
# The zlib-ng library is a more modern implementation of Zlib and can act as drop-in replacement.
23+
zlib = zlib-ng.override { withZlibCompat = true; };
24+
in
1425

1526
stdenv.mkDerivation rec {
1627
pname = "mojo";
@@ -26,37 +37,96 @@ stdenv.mkDerivation rec {
2637
};
2738

2839
buildInputs = [
40+
glibc
41+
icu
42+
libedit
43+
libgcc
2944
libxml2
45+
mold
46+
tinfo
3047
ncurses
31-
libedit
32-
icu
3348
zlib
34-
glibc
35-
libgcc
3649
];
3750

38-
nativeBuildInputs = [ autoPatchelfHook zstd ];
51+
nativeBuildInputs = [ autoPatchelfHook zstd makeWrapper ];
52+
53+
# nativeBuildInputs = [ autoPatchelfHook zstd makeWrapper ];
3954

4055
unpackPhase = ''
4156
zstd -d $src -c | tar xvf -
4257
'';
4358

44-
installPhase = ''
45-
cp -r . $out
46-
59+
patchPhase = ''
4760
# Nixpkgs uses `libedit.so.0.0.72` for the version of libedit
4861
# that is `libedit.so.2.0.72` in Ubuntu.
49-
ln -s ${libedit}/lib/libedit.so.0 $out/lib/libedit.so.2
62+
ln -s ${libedit}/lib/libedit.so.0 lib/libedit.so.2
63+
64+
# Brute-force copy tinfo into the same directory as the mojo sources.
65+
# It appears that mojo can't figure out how to find libtinfo otherwise.
66+
cp ${tinfo}/lib/libtinfo.so.6.4 lib/libtinfo.so.6.4
67+
ln -s libtinfo.so.6.4 lib/libtinfo.so.6
5068
5169
# The mojo command uses this config file to determine the
5270
# locations to bundled dependencies. Remap it to /nix/store.
53-
sed -i "s|\$_self.install_path|$out|g" $out/modular.cfg
71+
sed -i "s|\$_self.install_path|$out|g" modular.cfg
5472
5573
# The nightly config has what looks like a typo, omitting the `_`.
56-
sed -i "s|\$self.install_path|$out|g" $out/modular.cfg
74+
sed -i "s|\$self.install_path|$out|g" modular.cfg
5775
5876
# Nightly builds require settings under `[mojo-nightly]`
59-
sed -i "s|\[mojo]|[mojo-nightly]|g" $out/modular.cfg
77+
sed -i "s|\[mojo]|[mojo-nightly]|g" modular.cfg
78+
79+
# This evil hack is a ~95% linktime speedup by using mold instead of ld.
80+
#
81+
# The mojo compiler somehow behaves differently when running in Bazel. There
82+
# it's necessary to have the setup below specified.
83+
#
84+
# To work around the fact that we can't resolve `-Wl,-rpath` properly
85+
# because of unknown wrapping logic, we use`-Xlinker,-rpath` which has
86+
# semantics like `-Xlinker <arg>`.
87+
#
88+
# WARNING: At the moment linking phase can't figure out how to link tinfo.
89+
# To work around this we allow leaving the tinfo symbols undefined and
90+
# unlinked. This is a very risky and dangerous practice that might trigger
91+
# random segfaults during runtime.
92+
sed -i "s|system_libs = -lrt,-ldl,-lpthread,-lm,-lz,-ltinfo;|system_libs = -fuse-ld=mold,-lrt,-ldl,-lpthread,-lm,-L,${zlib}/lib,-Xlinker,-rpath=${zlib}/lib,-Xlinker,-L,${tinfo}/lib,-Xlinker,-rpath=${tinfo}/lib,-Xlinker,-rpath=${stdenv.cc.cc.lib}/lib,--verbose,-Xlinker,--verbose,-Xlinker,-v,-Xlinker,--allow-shlib-undefined,-Xlinker,--unresolved-symbols=ignore-all;|g" modular.cfg
93+
'';
94+
95+
installPhase = ''
96+
cp -r . $out
97+
98+
# The autoPatchelfHook would run before the fixupPhase, so we need to call
99+
# wrapping logic in a custom postInstall phase. This way we run patchelf
100+
# after the wrappers have been created.
101+
runHook postInstall
102+
'';
60103

104+
# These fixups are not too pretty, but at the moment the `mojo` compiler
105+
# doesn't respect standard environment variables like `CC` and `LD`. Instead,
106+
# we have to add C++ tooling to the PATH. We also wrap certain executables
107+
# with `MODULAR_HOME` so that they work in mkShell environemnts.
108+
postInstall = ''
109+
wrap_with_env() {
110+
wrapProgram $1 \
111+
--prefix PATH : ${clang}/bin:${mold}/bin:${uutils-coreutils-noprefix}/bin \
112+
--prefix MODULAR_HOME : $out \
113+
--prefix LIBRARY_PATH : ${makeLibraryPath [ tinfo ncurses zlib stdenv.cc.cc.lib ]} \
114+
--prefix LD_LIBRARY_PATH : ${makeLibraryPath [ tinfo ncurses zlib stdenv.cc.cc.lib ]} \
115+
--set LD_LIBRARY_PATH ${makeLibraryPath [ tinfo ncurses zlib stdenv.cc.cc.lib ]}
116+
}
117+
118+
wrap_with_env $out/bin/mojo
119+
wrap_with_env $out/bin/mojo-lldb
120+
wrap_with_env $out/bin/mojo-lsp-server
121+
wrap_with_env $out/bin/lldb-argdumper
122+
wrap_with_env $out/bin/lldb-server
123+
wrap_with_env $out/bin/mojo-lldb-dap
61124
'';
125+
126+
passthru = {
127+
# These may be passed through to Bazel invocations and can be used to
128+
# construct remote execution toolchains for Mojo.
129+
mojoBinPath = [ clang mold uutils-coreutils-noprefix ];
130+
mojoLibraryPath = [ tinfo ncurses zlib stdenv.cc.cc.lib ];
131+
};
62132
}

mojo/BUILD.bazel

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ load("//mojo:toolchain.bzl", "mojo_toolchain")
1515
"MOJO_CC_PATH",
1616
"MOJO_LIBRARY_PATH",
1717
"MOJO_PYTHON_LIBRARY",
18+
"LD_LIBRARY_PATH",
19+
"MOJO_DYNAMIC_LINKER",
1820

1921
# Unset values default to an empty string.
2022
"MOJO_UNSET",

mojo/mojo.bzl

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,12 @@ def _mojo_library_impl(ctx):
129129
tools = [toolchain.symbolizer],
130130
use_default_shell_env = False,
131131
env = {
132-
"PATH": toolchain.MOJO_CC_PATH,
133-
"MODULAR_HOME": toolchain.MODULAR_HOME,
134-
"MOJO_PYTHON_LIBRARY": toolchain.MOJO_PYTHON_LIBRARY,
135-
"LLVM_SYMBOLIZER_PATH": toolchain.symbolizer.path,
132+
# "PATH": toolchain.MOJO_CC_PATH,
133+
# "MODULAR_HOME": toolchain.MODULAR_HOME,
134+
# "LIBRARY_PATH": toolchain.MOJO_LIBRARY_PATH,
135+
# "MOJO_PYTHON_LIBRARY": toolchain.MOJO_PYTHON_LIBRARY,
136+
# "LLVM_SYMBOLIZER_PATH": toolchain.symbolizer.path,
137+
# "LD_LIBRARY_PATH": toolchain.MOJO_LIBRARY_PATH,
136138
},
137139
)
138140

@@ -197,11 +199,12 @@ def _mojo_binary_impl(ctx):
197199
tools = [toolchain.symbolizer],
198200
use_default_shell_env = False,
199201
env = {
200-
"MODULAR_HOME": toolchain.MODULAR_HOME,
201-
"PATH": toolchain.MOJO_CC_PATH,
202-
"LIBRARY_PATH": toolchain.MOJO_LIBRARY_PATH,
203-
"MOJO_PYTHON_LIBRARY": toolchain.MOJO_PYTHON_LIBRARY,
204-
"LLVM_SYMBOLIZER_PATH": toolchain.symbolizer.path,
202+
# "MODULAR_HOME": toolchain.MODULAR_HOME,
203+
# "PATH": toolchain.MOJO_CC_PATH,
204+
# "LIBRARY_PATH": toolchain.MOJO_LIBRARY_PATH,
205+
# "LD_LIBRARY_PATH": toolchain.MOJO_LIBRARY_PATH,
206+
# "MOJO_PYTHON_LIBRARY": toolchain.MOJO_PYTHON_LIBRARY,
207+
# "LLVM_SYMBOLIZER_PATH": toolchain.symbolizer.path,
205208
},
206209
)
207210

0 commit comments

Comments
 (0)