Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
208 lines (179 loc) · 7.64 KB
/
Copy pathmain.go
File metadata and controls
208 lines (179 loc) · 7.64 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package main
import (
"context"
"dagger/xs/internal/dagger"
)
type Xs struct{}
func (m *Xs) withCaches(container *dagger.Container, targetSuffix string) *dagger.Container {
// Separate caches per target
registryCache := dag.CacheVolume("dagger-cargo-registry-" + targetSuffix)
gitCache := dag.CacheVolume("dagger-cargo-git-" + targetSuffix)
targetCache := dag.CacheVolume("dagger-cargo-target-" + targetSuffix)
return container.
WithMountedCache("/root/.cargo/registry", registryCache).
WithMountedCache("/root/.cargo/git", gitCache).
WithMountedCache("/app/target", targetCache)
}
func (m *Xs) Upload(
ctx context.Context,
// +ignore=["**", "!Cargo.toml", "!Cargo.lock", "!src/**", "!xs.nu", "!scripts/**"]
src *dagger.Directory) *dagger.Directory {
return src
}
func (m *Xs) MacosEnv(
ctx context.Context,
src *dagger.Directory) *dagger.Container {
return m.withCaches(
dag.Container().
From("joseluisq/rust-linux-darwin-builder:latest").
WithEnvVariable("CC_aarch64_apple_darwin", "aarch64-apple-darwin22.4-clang").
WithEnvVariable("CXX_aarch64_apple_darwin", "aarch64-apple-darwin22.4-clang++").
WithEnvVariable("AR_aarch64_apple_darwin", "aarch64-apple-darwin22.4-ar").
WithEnvVariable("CFLAGS_aarch64_apple_darwin", "-fuse-ld=/usr/local/osxcross/target/bin/aarch64-apple-darwin22.4-ld").
WithMountedDirectory("/app", src).
WithWorkdir("/app"),
"darwin-arm64",
)
}
func (m *Xs) MacosBuild(ctx context.Context, src *dagger.Directory, version string) *dagger.File {
container := m.MacosEnv(ctx, src).
WithExec([]string{"rustup", "update", "stable"}).
WithExec([]string{"rustup", "default", "stable"}).
WithExec([]string{"rustup", "target", "add", "aarch64-apple-darwin"})
// First build attempt - this will likely fail due to libproc issue
container = container.WithExec([]string{"bash", "-c", `
cargo build --target aarch64-apple-darwin --release --color always 2>&1 | tee build.log
status=${PIPESTATUS[0]}
# Only the known libproc failure is recoverable. Any other non-zero exit
# must fail the step, never fall through to package an empty tarball.
if [ "$status" -ne 0 ]; then
if grep -q "osx_libproc_bindings.rs.*No such file" build.log; then
echo "Detected libproc issue, applying fix..."
# Find the libproc source file - try both possible paths
SOURCE_FILE=$(find /root/.cargo/registry/src/index.crates.io-* -name "libproc-*" -type d | head -1)/docs_rs/osx_libproc_bindings.rs
if [ ! -f "$SOURCE_FILE" ]; then
SOURCE_FILE=$(find /root/.cargo/registry/src/index.crates.io-* -name "libproc-*" -type d | head -1)/src/osx_libproc_bindings.rs
fi
# Find the destination directory
DEST_DIR=$(find target/aarch64-apple-darwin/release/build/ -name "libproc-*" -type d | head -1)/out
if [ -f "$SOURCE_FILE" ] && [ -d "$DEST_DIR" ]; then
echo "Copying $SOURCE_FILE to $DEST_DIR/"
cp "$SOURCE_FILE" "$DEST_DIR/"
echo "Retrying build..."
cargo build --target aarch64-apple-darwin --release --color always
else
echo "Error: Could not find source file or destination directory"
echo "Source: $SOURCE_FILE"
echo "Dest: $DEST_DIR"
exit 1
fi
else
echo "macOS build failed (exit $status), and it is not the known libproc issue."
exit "$status"
fi
fi
# Backstop: fail loudly if the binary is missing rather than packaging an
# empty tarball (covers a failed libproc retry, etc.).
if [ ! -f target/aarch64-apple-darwin/release/xs ]; then
echo "Error: xs binary not found after build"
exit 1
fi
# Clean up log file
rm -f build.log
`})
// Create tarball structure using provided version
container = container.WithExec([]string{"sh", "-c", `
set -e
mkdir -p /tmp/cross-stream-` + version + `
cp target/aarch64-apple-darwin/release/xs /tmp/cross-stream-` + version + `/
cd /tmp
tar -czf cross-stream-` + version + `-macos.tar.gz cross-stream-` + version + `
`})
return container.File("/tmp/cross-stream-" + version + "-macos.tar.gz")
}
func (m *Xs) LinuxArm64Env(
ctx context.Context,
src *dagger.Directory) *dagger.Container {
return m.withCaches(
dag.Container().
From("messense/rust-musl-cross:aarch64-musl").
WithMountedDirectory("/app", src).
WithWorkdir("/app"),
"linux-arm64",
)
}
func (m *Xs) LinuxArm64Build(ctx context.Context, src *dagger.Directory, version string) *dagger.File {
container := m.LinuxArm64Env(ctx, src).
WithExec([]string{"cargo", "build", "--release", "--target", "aarch64-unknown-linux-musl"})
// Create tarball structure using provided version
container = container.WithExec([]string{"sh", "-c", `
set -e
mkdir -p /tmp/cross-stream-` + version + `
cp target/aarch64-unknown-linux-musl/release/xs /tmp/cross-stream-` + version + `/
cd /tmp
tar -czf cross-stream-` + version + `-linux-arm64.tar.gz cross-stream-` + version + `
`})
return container.File("/tmp/cross-stream-" + version + "-linux-arm64.tar.gz")
}
func (m *Xs) LinuxAmd64Env(
ctx context.Context,
src *dagger.Directory) *dagger.Container {
return m.withCaches(
dag.Container().
From("messense/rust-musl-cross:x86_64-musl").
WithMountedDirectory("/app", src).
WithWorkdir("/app"),
"linux-amd64",
)
}
func (m *Xs) LinuxAmd64Build(ctx context.Context, src *dagger.Directory, version string) *dagger.File {
container := m.LinuxAmd64Env(ctx, src).
WithExec([]string{"cargo", "build", "--release", "--target", "x86_64-unknown-linux-musl"})
// Create tarball structure using provided version
container = container.WithExec([]string{"sh", "-c", `
set -e
mkdir -p /tmp/cross-stream-` + version + `
cp target/x86_64-unknown-linux-musl/release/xs /tmp/cross-stream-` + version + `/
cd /tmp
tar -czf cross-stream-` + version + `-linux-amd64.tar.gz cross-stream-` + version + `
`})
return container.File("/tmp/cross-stream-" + version + "-linux-amd64.tar.gz")
}
func (m *Xs) WindowsEnv(
ctx context.Context,
src *dagger.Directory) *dagger.Container {
return m.withCaches(
dag.Container().
From("joseluisq/rust-linux-darwin-builder:latest").
WithExec([]string{"apt", "update"}).
WithExec([]string{"apt", "install", "-y", "nasm", "gcc-mingw-w64-i686", "mingw-w64", "mingw-w64-tools"}).
WithExec([]string{"rustup", "target", "add", "x86_64-pc-windows-gnu"}).
WithEnvVariable("CARGO_BUILD_TARGET", "x86_64-pc-windows-gnu").
WithEnvVariable("CC_x86_64_pc_windows_gnu", "x86_64-w64-mingw32-gcc").
WithEnvVariable("CXX_x86_64_pc_windows_gnu", "x86_64-w64-mingw32-g++").
WithEnvVariable("AR_x86_64_pc_windows_gnu", "x86_64-w64-mingw32-gcc-ar").
WithEnvVariable("DLLTOOL_x86_64_pc_windows_gnu", "x86_64-w64-mingw32-dlltool").
WithEnvVariable("CFLAGS_x86_64_pc_windows_gnu", "-m64").
WithEnvVariable("ASM_NASM_x86_64_pc_windows_gnu", "/usr/bin/nasm").
WithEnvVariable("AWS_LC_SYS_PREBUILT_NASM", "0").
WithMountedDirectory("/app", src).
WithWorkdir("/app"),
"windows-amd64",
)
}
func (m *Xs) WindowsBuild(ctx context.Context, src *dagger.Directory, version string) *dagger.File {
container := m.WindowsEnv(ctx, src).
WithExec([]string{"rustup", "update", "stable"}).
WithExec([]string{"rustup", "default", "stable"}).
WithExec([]string{"rustup", "target", "add", "x86_64-pc-windows-gnu"}).
WithExec([]string{"cargo", "build", "--release", "--target", "x86_64-pc-windows-gnu"})
// Create tarball structure using provided version
container = container.WithExec([]string{"sh", "-c", `
set -e
mkdir -p /tmp/cross-stream-` + version + `
cp target/x86_64-pc-windows-gnu/release/xs.exe /tmp/cross-stream-` + version + `/
cd /tmp
tar -czf cross-stream-` + version + `-windows-amd64.tar.gz cross-stream-` + version + `
`})
return container.File("/tmp/cross-stream-" + version + "-windows-amd64.tar.gz")
}