Skip to content

Commit 90e8707

Browse files
ecklfdglsparsons
andauthored
feat: add cargo build config and prebuilt support (#91)
* add cargo build config and prebuilt support * fix missing newline * add missing punctation * Update README.md Co-authored-by: Douglas Parsons <dglsparsons@users.noreply.github.com> --------- Co-authored-by: Douglas Parsons <dglsparsons@users.noreply.github.com>
1 parent eccd345 commit 90e8707

File tree

20 files changed

+1095
-17
lines changed

20 files changed

+1095
-17
lines changed

β€ŽCargo.lockβ€Ž

Lines changed: 13 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

β€ŽCargo.tomlβ€Ž

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,18 @@
22

33
members = [
44
"vercel_runtime",
5-
"examples/*",
5+
"examples/simple",
6+
"examples/cron",
7+
"examples/nextjs",
68
]
79

810
exclude = [
9-
"test/*",
11+
"test/fixtures/01-include-files",
12+
"test/fixtures/02-with-utility",
13+
"test/fixtures/03-with-function",
14+
"test/fixtures/04-with-parameter",
15+
"test/fixtures/05-with-similar-entrypaths",
16+
"test/fixtures/06-with-toolchain-override",
17+
"test/fixtures/07-with-cargo-configuration",
1018
]
19+

β€ŽREADME.mdβ€Ž

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,26 @@ This Builder supports installing dependencies defined in the `Cargo.toml` file.
9999

100100
Furthermore, more system dependencies can be installed at build time with the presence of a shell `build.sh` file in the same directory as the entrypoint file.
101101

102+
## Prebuilt Deployments
103+
104+
When creating a prebuilt deployment, the build output must be for x86_64 linux. To do this, create a Cargo build configuration at `.cargo/config.toml` with the following contents:
105+
106+
```toml
107+
[build]
108+
target = "x86_64-unknown-linux-musl"
109+
110+
# Uncomment below to support Rust cross-compilation from macOS to Linux
111+
# Follow these installation instructions: https://github.com/chinedufn/cross-compile-rust-from-mac-to-linux
112+
# [target.x86_64-unknown-linux-musl]
113+
# linker = "x86_64-unknown-linux-gnu-gcc"
114+
```
115+
116+
You then can build the file and trigger the deployment with the Vercel CLI.
117+
118+
```shell
119+
vercel build && vercel deploy --prebuilt
120+
```
121+
102122
## Local Development
103123

104124
With `vercel dev` and `vercel-rust`, you can develop your Rust-based lambdas on your own machine.

β€Žsrc/index.tsβ€Ž

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
getCargoMetadata,
1515
findBinaryName,
1616
findCargoWorkspace,
17+
findCargoBuildConfiguration,
1718
} from './lib/cargo';
1819
import {
1920
assertEnv,
@@ -46,7 +47,12 @@ async function buildHandler(options: BuildOptions): Promise<BuildResultV3> {
4647
env: rustEnv,
4748
cwd: path.dirname(entryPath),
4849
});
50+
4951
const binaryName = findBinaryName(cargoWorkspace, entryPath);
52+
const cargoBuildConfiguration = await findCargoBuildConfiguration(
53+
cargoWorkspace,
54+
);
55+
const buildTarget = cargoBuildConfiguration?.build.target ?? '';
5056

5157
await runUserScripts(workPath);
5258

@@ -72,11 +78,13 @@ async function buildHandler(options: BuildOptions): Promise<BuildResultV3> {
7278

7379
debug(`Building \`${binaryName}\` for \`${process.platform}\` completed`);
7480

75-
const { target_directory: targetDirectory } = await getCargoMetadata({
81+
let { target_directory: targetDirectory } = await getCargoMetadata({
7682
cwd: process.cwd(),
7783
env: rustEnv,
7884
});
7985

86+
targetDirectory = path.join(targetDirectory, buildTarget);
87+
8088
const bin = path.join(
8189
targetDirectory,
8290
BUILDER_DEBUG ? 'debug' : 'release',

β€Žsrc/lib/cargo.tsβ€Ž

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,35 @@ export async function findCargoWorkspace(
168168
};
169169
}
170170

171+
interface CargoBuildConfiguration {
172+
build: {
173+
target?: string;
174+
'target-dir'?: string;
175+
};
176+
target: Record<
177+
string,
178+
{
179+
linker?: string;
180+
}
181+
>;
182+
}
183+
184+
export async function findCargoBuildConfiguration(
185+
workspace: CargoWorkspace,
186+
): Promise<CargoBuildConfiguration | null> {
187+
const configPath = path.join(
188+
path.dirname(workspace.root),
189+
'.cargo/config.toml',
190+
);
191+
192+
if (!fs.existsSync(configPath)) {
193+
return null;
194+
}
195+
196+
const config = await toml.parse.stream(fs.createReadStream(configPath));
197+
return config as unknown as CargoBuildConfiguration;
198+
}
199+
171200
export function findBinaryName(
172201
workspace: CargoWorkspace,
173202
entryPath: string,

β€Žtest/fixtures.test.tsβ€Ž

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,9 @@ describe('vercel-rust', () => {
102102
it('deploy 06-with-toolchain-override', async () => {
103103
await expect(testFixture('06-with-toolchain-override')).resolves.toBe('ok');
104104
});
105+
it('deploy 07-with-cargo-configuration', async () => {
106+
await expect(testFixture('07-with-cargo-configuration')).resolves.toBe(
107+
'ok',
108+
);
109+
});
105110
});
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"functions": {
33
"api/**/*.rs": {
4-
"runtime": "vercel-rust@4.0.0-beta.3"
4+
"runtime": "vercel-dev-builder@0.0.5"
55
}
66
}
77
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
rustc 1.70.0-nightly (44f518058 2023-03-20)
1+
rustc 1.70.0-nightly (478cbb42b 2023-03-28)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[build]
2+
# target = "x86_64-unknown-linux-musl"
3+
4+
[target.x86_64-unknown-linux-musl]
5+
linker = "x86_64-unknown-linux-gnu-gcc"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.vercel

0 commit comments

Comments
Β (0)