Skip to content

Commit d4ba316

Browse files
committed
Working rustBuild with targets
1 parent b7891be commit d4ba316

File tree

4 files changed

+107
-88
lines changed

4 files changed

+107
-88
lines changed

.dagger/src/index.ts

Lines changed: 107 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import {
66
func,
77
argument,
88
Secret,
9+
File,
10+
type Platform,
911
} from "@dagger.io/dagger";
1012

1113
const NODE_IMAGE = "node:24";
@@ -188,18 +190,34 @@ export class AtomicServer {
188190
}
189191

190192
@func()
191-
rustBuild(@argument() release: boolean = false): Container {
193+
/** Builds the Rust server binary on the host architecture */
194+
rustBuild(
195+
@argument() release: boolean = false,
196+
@argument() target: string = "x86_64-unknown-linux-musl"
197+
): Container {
192198
const source = this.source;
193199
const cargoCache = dag.cacheVolume("cargo");
194200

201+
let image = RUST_IMAGE;
202+
if (target === "x86_64-unknown-linux-musl") {
203+
image = "ghcr.io/rust-cross/rust-musl-cross:x86_64-musl";
204+
} else if (target === "aarch64-unknown-linux-musl") {
205+
image = "ghcr.io/rust-cross/rust-musl-cross:aarch64-musl";
206+
} else if (target === "armv7-unknown-linux-musleabihf") {
207+
image = "ghcr.io/rust-cross/rust-musl-cross:armv7-musleabihf";
208+
} else {
209+
throw new Error(
210+
`Unknown target: ${target}. Supported targets are: x86_64-unknown-linux-musl, aarch64-unknown-linux-musl, armv7-unknown-linux-musleabihf.`
211+
);
212+
}
213+
195214
const rustContainer = dag
196215
.container()
197-
.from(RUST_IMAGE)
216+
.from(image)
198217
.withExec(["apt-get", "update", "-qq"])
199218
.withExec(["apt", "install", "-y", "nasm"])
200219
.withExec(["rustup", "component", "add", "clippy"])
201220
.withExec(["rustup", "component", "add", "rustfmt"])
202-
.withExec(["cargo", "install", "cross"])
203221
.withExec(["cargo", "install", "cargo-nextest"])
204222
.withMountedCache("/usr/local/cargo/registry", cargoCache);
205223

@@ -223,22 +241,25 @@ export class AtomicServer {
223241
const buildArgs = release
224242
? ["cargo", "build", "--release"]
225243
: ["cargo", "build"];
226-
const binaryPath = release
227-
? "./target/release/atomic-server"
228-
: "./target/debug/atomic-server";
229244
const targetPath = release
230-
? "/code/target/release/atomic-server"
231-
: "/code/target/debug/atomic-server";
232-
233-
return containerWithAssets
234-
.withExec(buildArgs)
235-
.withExec([binaryPath, "--version"])
236-
.withExec(["cp", targetPath, "/atomic-server-binary"]);
245+
? `/code/target/${target}/release/atomic-server`
246+
: `/code/target/${target}/debug/atomic-server`;
247+
248+
return (
249+
containerWithAssets
250+
.withExec(buildArgs)
251+
// .withExec([targetPath, "--version"])
252+
.withExec(["cp", targetPath, "/atomic-server-binary"])
253+
);
237254
}
238255

239256
@func()
240-
rustReleaseBuild(): Container {
241-
return this.rustBuild(true);
257+
/** Returns the release binary */
258+
rustBuildRelease(
259+
@argument() target: string = "x86_64-unknown-linux-musl"
260+
): File {
261+
const container = this.rustBuild(true, target);
262+
return container.file("/atomic-server-binary");
242263
}
243264

244265
@func()
@@ -268,73 +289,74 @@ export class AtomicServer {
268289
return rustContainer.withExec(["cargo", "fmt", "--check"]).stdout();
269290
}
270291

271-
@func()
272-
/** Doesn't work on M1 macs */
273-
rustCrossBuild(@argument() target: string): Container {
274-
let engineSvc = dag.docker().engine();
275-
const source = this.source;
276-
277-
const sourceContainer = dag
278-
.container()
279-
.from("docker:cli")
280-
.withServiceBinding("docker", engineSvc)
281-
.withEnvVariable("DOCKER_HOST", "tcp://docker:2375")
282-
.withExec(["docker", "ps"])
283-
.withExec([
284-
"apk",
285-
"add",
286-
"--no-cache",
287-
// For installing rust
288-
"curl",
289-
// CC linker deps, compiling cross
290-
"build-base",
291-
"gcc",
292-
"musl-dev",
293-
"cmake",
294-
])
295-
.withExec([
296-
"sh",
297-
"-c",
298-
"curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --default-toolchain stable",
299-
])
300-
.withEnvVariable(
301-
"PATH",
302-
"/root/.cargo/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
303-
)
304-
.withExec(["docker", "ps"])
305-
.withExec(["cargo", "install", "cross"])
306-
.withExec(["rustup", "target", "add", target])
307-
.withExec([
308-
"rustup",
309-
"toolchain",
310-
"add",
311-
"stable-x86_64-unknown-linux-gnu",
312-
"--profile",
313-
"minimal",
314-
"--force-non-host",
315-
])
316-
.withFile("/home/rust/src/Cargo.toml", source.file("Cargo.toml"))
317-
.withFile("/home/rust/src/Cargo.lock", source.file("Cargo.lock"))
318-
.withDirectory("/home/rust/src/server", source.directory("server"))
319-
.withDirectory("/home/rust/src/lib", source.directory("lib"))
320-
.withDirectory("/home/rust/src/cli", source.directory("cli"))
321-
.withMountedCache("/home/rust/src/target", dag.cacheVolume("rust-target"))
322-
.withWorkdir("/home/rust/src");
323-
324-
// Include frontend assets for the server build
325-
const browserDir = this.jsBuild().directory("/app/data-browser/dist");
326-
const containerWithAssets = sourceContainer.withDirectory(
327-
"/home/rust/src/server/assets_tmp",
328-
browserDir
329-
);
330-
331-
// Build using native cargo with target specification
332-
const binaryPath = `./target/${target}/release/atomic-server`;
333-
334-
return containerWithAssets
335-
.withExec(["cross", "build", "--target", target, "--release"])
336-
.withExec(["cp", binaryPath, "/atomic-server-binary"]);
337-
}
292+
// @func()
293+
// /** Doesn't work on M1 macs */
294+
// rustCrossBuild(@argument() target: string): Container {
295+
// let engineSvc = dag.docker().engine();
296+
// const source = this.source;
297+
298+
// const sourceContainer = dag
299+
// // To allow cross-compilation to work on M1 macs
300+
// .container({ platform: "linux/amd64" as Platform })
301+
// .from("docker:cli")
302+
// .withServiceBinding("docker", engineSvc)
303+
// .withEnvVariable("DOCKER_HOST", "tcp://docker:2375")
304+
// .withExec(["docker", "ps"])
305+
// .withExec([
306+
// "apk",
307+
// "add",
308+
// "--no-cache",
309+
// // For installing rust
310+
// "curl",
311+
// // CC linker deps, compiling cross
312+
// "build-base",
313+
// "gcc",
314+
// "musl-dev",
315+
// "cmake",
316+
// ])
317+
// .withExec([
318+
// "sh",
319+
// "-c",
320+
// "curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --default-toolchain stable",
321+
// ])
322+
// .withEnvVariable(
323+
// "PATH",
324+
// "/root/.cargo/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
325+
// )
326+
// .withExec(["docker", "ps"])
327+
// .withExec(["cargo", "install", "cross"])
328+
// .withExec(["rustup", "target", "add", target])
329+
// .withExec([
330+
// "rustup",
331+
// "toolchain",
332+
// "add",
333+
// "stable-x86_64-unknown-linux-gnu",
334+
// "--profile",
335+
// "minimal",
336+
// "--force-non-host",
337+
// ])
338+
// .withFile("/home/rust/src/Cargo.toml", source.file("Cargo.toml"))
339+
// .withFile("/home/rust/src/Cargo.lock", source.file("Cargo.lock"))
340+
// .withDirectory("/home/rust/src/server", source.directory("server"))
341+
// .withDirectory("/home/rust/src/lib", source.directory("lib"))
342+
// .withDirectory("/home/rust/src/cli", source.directory("cli"))
343+
// .withMountedCache("/home/rust/src/target", dag.cacheVolume("rust-target"))
344+
// .withWorkdir("/home/rust/src");
345+
346+
// // Include frontend assets for the server build
347+
// const browserDir = this.jsBuild().directory("/app/data-browser/dist");
348+
// const containerWithAssets = sourceContainer.withDirectory(
349+
// "/home/rust/src/server/assets_tmp",
350+
// browserDir
351+
// );
352+
353+
// // Build using native cargo with target specification
354+
// const binaryPath = `./target/${target}/release/atomic-server`;
355+
356+
// return containerWithAssets
357+
// .withExec(["cross", "build", "--target", target, "--release"])
358+
// .withExec(["cp", binaryPath, "/atomic-server-binary"]);
359+
// }
338360

339361
@func()
340362
async endToEnd(@argument() netlifyAuthToken: Secret): Promise<string> {
@@ -427,10 +449,7 @@ export class AtomicServer {
427449
@argument() sshPrivateKey: Secret
428450
): Promise<string> {
429451
// Build the cross-compiled binary for x86_64-unknown-linux-musl
430-
const crossBuildContainer = this.rustCrossBuild(
431-
"x86_64-unknown-linux-musl"
432-
);
433-
const binaryFile = crossBuildContainer.file("/atomic-server-binary");
452+
const binaryFile = this.rustBuildRelease("x86_64-unknown-linux-musl");
434453

435454
// Create deployment container with SSH client
436455
const deployContainer = dag
@@ -483,7 +502,7 @@ export class AtomicServer {
483502
}
484503

485504
@func()
486-
async rustCrossMultiBuild(
505+
async releaseAssets(
487506
@argument()
488507
targets: string[] = [
489508
"x86_64-unknown-linux-musl",
@@ -492,7 +511,7 @@ export class AtomicServer {
492511
]
493512
): Promise<Directory> {
494513
const builds = targets.map((target) => {
495-
const container = this.rustCrossBuild(target);
514+
const container = this.rustBuild(true, target);
496515
return {
497516
target,
498517
binary: container.file("/atomic-server-binary"),
52.9 MB
Binary file not shown.
51 MB
Binary file not shown.
56.5 MB
Binary file not shown.

0 commit comments

Comments
 (0)