@@ -379,20 +379,42 @@ func (m *ApoxyCli) BuildEdgeRuntime(
379379 platform string ,
380380 // +optional
381381 src * dagger.Directory ,
382+ // +optional
383+ sccacheToken * dagger.Secret ,
382384) * dagger.Container {
383385 if src == nil {
384386 src = dag .Git ("https://github.com/apoxy-dev/edge-runtime" ).
385387 Branch ("main" ).
386388 Tree ()
387389 }
388390 p := dagger .Platform (platform )
389- return dag .Container (dagger.ContainerOpts {Platform : p }).
391+ goarch := archOf (p )
392+ targetArch := canonArchFromGoArch (goarch )
393+
394+ builder := dag .Container (dagger.ContainerOpts {Platform : p }).
390395 From ("rust:1.82.0-bookworm" ).
391396 WithExec ([]string {"apt-get" , "update" }).
392- WithExec ([]string {"apt-get" , "install" , "-y" , "llvm-dev" , "libclang-dev" , "gcc" , "cmake" , "binutils" }).
397+ WithExec ([]string {"apt-get" , "install" , "-y" , "llvm-dev" , "libclang-dev" , "gcc" , "cmake" , "binutils" , "clang" , "mold" }).
398+ // Install sccache for compilation caching (0.11.0 is compatible with rustc 1.82.0).
399+ WithExec ([]string {"cargo" , "install" , "sccache" , "--version" , "0.11.0" , "--locked" }).
400+ WithEnvVariable ("SCCACHE_WEBDAV_ENDPOINT" , "https://cache.depot.dev" ).
401+ WithEnvVariable ("RUSTC_WRAPPER" , "/usr/local/cargo/bin/sccache" ).
402+ // Configure mold as linker for faster linking.
403+ WithEnvVariable ("CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_LINKER" , "clang" ).
404+ WithEnvVariable ("CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_RUSTFLAGS" , "-C link-arg=-fuse-ld=mold" ).
405+ WithEnvVariable ("CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER" , "clang" ).
406+ WithEnvVariable ("CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_RUSTFLAGS" , "-C link-arg=-fuse-ld=mold" ).
407+ WithMountedCache ("/usr/local/cargo/registry" , dag .CacheVolume ("cargo-registry-" + goarch )).
408+ WithMountedCache ("/src/target" , dag .CacheVolume ("cargo-target-" + goarch )).
409+ WithMountedCache ("/root/.cache/sccache" , dag .CacheVolume ("sccache-" + targetArch )).
393410 WithWorkdir ("/src" ).
394- WithDirectory ("/src" , src ).
395- WithExec ([]string {"cargo" , "build" , "--release" })
411+ WithDirectory ("/src" , src )
412+
413+ if sccacheToken != nil {
414+ builder = builder .WithSecretVariable ("SCCACHE_WEBDAV_TOKEN" , sccacheToken )
415+ }
416+
417+ return builder .WithExec ([]string {"cargo" , "build" , "--release" })
396418}
397419
398420// PullEdgeRuntime builds the Apoxy edge-runtime fork from source.
@@ -403,8 +425,11 @@ func (m *ApoxyCli) PullEdgeRuntime(
403425 platform dagger.Platform ,
404426 // +optional
405427 apoxyCliSrc * dagger.Directory ,
428+ // +optional
429+ sccacheToken * dagger.Secret ,
406430) * dagger.Container {
407431 goarch := archOf (platform )
432+ targetArch := canonArchFromGoArch (goarch )
408433
409434 // Build edge-runtime from source.
410435 edgeRuntimeSrc := dag .Git ("https://github.com/apoxy-dev/edge-runtime" ).
@@ -414,11 +439,27 @@ func (m *ApoxyCli) PullEdgeRuntime(
414439 builder := dag .Container (dagger.ContainerOpts {Platform : platform }).
415440 From ("rust:1.82.0-bookworm" ).
416441 WithExec ([]string {"apt-get" , "update" }).
417- WithExec ([]string {"apt-get" , "install" , "-y" , "llvm-dev" , "libclang-dev" , "gcc" , "cmake" , "binutils" }).
442+ WithExec ([]string {"apt-get" , "install" , "-y" , "llvm-dev" , "libclang-dev" , "gcc" , "cmake" , "binutils" , "clang" , "mold" }).
443+ // Install sccache for compilation caching (0.11.0 is compatible with rustc 1.82.0).
444+ WithExec ([]string {"cargo" , "install" , "sccache" , "--version" , "0.11.0" , "--locked" }).
445+ WithEnvVariable ("SCCACHE_WEBDAV_ENDPOINT" , "https://cache.depot.dev" ).
446+ WithEnvVariable ("RUSTC_WRAPPER" , "/usr/local/cargo/bin/sccache" ).
447+ // Configure mold as linker for faster linking.
448+ WithEnvVariable ("CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_LINKER" , "clang" ).
449+ WithEnvVariable ("CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_RUSTFLAGS" , "-C link-arg=-fuse-ld=mold" ).
450+ WithEnvVariable ("CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER" , "clang" ).
451+ WithEnvVariable ("CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_RUSTFLAGS" , "-C link-arg=-fuse-ld=mold" ).
418452 WithMountedCache ("/usr/local/cargo/registry" , dag .CacheVolume ("cargo-registry-" + goarch )).
419453 WithMountedCache ("/src/target" , dag .CacheVolume ("cargo-target-" + goarch )).
454+ WithMountedCache ("/root/.cache/sccache" , dag .CacheVolume ("sccache-" + targetArch )).
420455 WithWorkdir ("/src" ).
421- WithDirectory ("/src" , edgeRuntimeSrc ).
456+ WithDirectory ("/src" , edgeRuntimeSrc )
457+
458+ if sccacheToken != nil {
459+ builder = builder .WithSecretVariable ("SCCACHE_WEBDAV_TOKEN" , sccacheToken )
460+ }
461+
462+ builder = builder .
422463 WithExec ([]string {"cargo" , "build" , "--release" }).
423464 WithExec ([]string {"cp" , "/src/target/release/edge-runtime" , "/edge-runtime" })
424465
@@ -445,6 +486,8 @@ func (m *ApoxyCli) BuildAPIServer(
445486 src * dagger.Directory ,
446487 // +optional
447488 platform string ,
489+ // +optional
490+ sccacheToken * dagger.Secret ,
448491) * dagger.Container {
449492 if platform == "" {
450493 platform = runtime .GOOS + "/" + runtime .GOARCH
@@ -459,7 +502,7 @@ func (m *ApoxyCli) BuildAPIServer(
459502 WithEnvVariable ("CC" , fmt .Sprintf ("zig-wrapper cc --target=%s-linux-musl" , canonArchFromGoArch (goarch ))).
460503 WithExec ([]string {"go" , "build" , "-o" , "apiserver" , "./cmd/apiserver" })
461504
462- runtimeCtr := m .PullEdgeRuntime (ctx , p , src )
505+ runtimeCtr := m .PullEdgeRuntime (ctx , p , src , sccacheToken )
463506
464507 return dag .Container (dagger.ContainerOpts {Platform : p }).
465508 From ("cgr.dev/chainguard/wolfi-base:latest" ).
@@ -483,6 +526,8 @@ func (m *ApoxyCli) BuildBackplane(
483526 src * dagger.Directory ,
484527 // +optional
485528 platform string ,
529+ // +optional
530+ sccacheToken * dagger.Secret ,
486531) * dagger.Container {
487532 if platform == "" {
488533 platform = runtime .GOOS + "/" + runtime .GOARCH
@@ -514,7 +559,7 @@ func (m *ApoxyCli) BuildBackplane(
514559 WithExec ([]string {"go" , "build" , "-o" , "/src/" + otelOut }).
515560 WithWorkdir ("/src" )
516561
517- runtimeCtr := m .PullEdgeRuntime (ctx , p , src )
562+ runtimeCtr := m .PullEdgeRuntime (ctx , p , src , sccacheToken )
518563
519564 return dag .Container (dagger.ContainerOpts {Platform : p }).
520565 From ("cgr.dev/chainguard/wolfi-base:latest" ).
@@ -610,10 +655,12 @@ func (m *ApoxyCli) PublishImages(
610655 registryPassword * dagger.Secret ,
611656 tag string ,
612657 sha string ,
658+ // +optional
659+ sccacheToken * dagger.Secret ,
613660) error {
614661 var apiCtrs []* dagger.Container
615662 for _ , platform := range []string {"linux/amd64" , "linux/arm64" } {
616- apiCtrs = append (apiCtrs , m .BuildAPIServer (ctx , src , platform ))
663+ apiCtrs = append (apiCtrs , m .BuildAPIServer (ctx , src , platform , sccacheToken ))
617664 }
618665
619666 addr , err := dag .Container ().
@@ -633,7 +680,7 @@ func (m *ApoxyCli) PublishImages(
633680
634681 var bCtrs []* dagger.Container
635682 for _ , platform := range []string {"linux/amd64" , "linux/arm64" } {
636- bCtrs = append (bCtrs , m .BuildBackplane (ctx , src , platform ))
683+ bCtrs = append (bCtrs , m .BuildBackplane (ctx , src , platform , sccacheToken ))
637684 }
638685
639686 addr , err = dag .Container ().
0 commit comments