Skip to content

Commit

Permalink
Merge pull request softprops#112 from StefanNienhuis/local-target-lin…
Browse files Browse the repository at this point in the history
…ker-options

Local target linker options
  • Loading branch information
softprops authored Jan 22, 2022
2 parents d6686a6 + a0b0eb6 commit c5d5e71
Show file tree
Hide file tree
Showing 4 changed files with 13,464 additions and 47 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,15 @@ custom:
cargoFlags: '--features enable-awesome'
# experimental! when set to true, artifacts are built locally outside of docker
+ dockerless: true
# when using local builds (dockerless), optionally provide a different target and linker for the compiler
# for example, allow local running on ARM macs
target: aarch64-apple-darwin
linker: clang
```

The following assumes that you have not specified a different target or linker. If you do, make sure have that you have installed the specified target (via `rustup`) and linker.

This will build and link your lambda as a static binary outside a container that can be deployed in to the lambda execution environment using [MUSL](https://doc.rust-lang.org/edition-guide/rust-2018/platform-and-target-support/musl-support-for-fully-static-binaries.html). The aim is that in future releases, this might become the default behavior.

In order to use this mode its expected that you install the `x86_64-unknown-linux-musl` target on all platforms locally with
Expand Down
64 changes: 41 additions & 23 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,16 @@ class RustPlugin {
this.custom.cargoFlags ||
""
).split(/\s+/);
const targetArgs = MUSL_PLATFORMS.includes(platform)
? ["--target", "x86_64-unknown-linux-musl"]
: [];


let target = (funcArgs || {}).target || this.custom.target

const targetArgs =
target ?
['--target', target]
: MUSL_PLATFORMS.includes(platform)
? ["--target", "x86_64-unknown-linux-musl"]
: [];
return [
...defaultArgs,
...profileArgs,
Expand All @@ -84,33 +91,44 @@ class RustPlugin {
].filter((i) => i);
}

localBuildEnv(env, platform) {
localBuildEnv(funcArgs, env, platform) {
const defaultEnv = { ...env };

let target = (funcArgs || {}).target || this.custom.target
let linker = (funcArgs || {}).linker || this.custom.linker

const platformEnv =
"win32" === platform
? {
RUSTFLAGS: (env["RUSTFLAGS"] || "") + " -Clinker=rust-lld",
TARGET_CC: "rust-lld",
CC_x86_64_unknown_linux_musl: "rust-lld",
}
: "darwin" === platform
? {
RUSTFLAGS:
(env["RUSTFLAGS"] || "") + " -Clinker=x86_64-linux-musl-gcc",
TARGET_CC: "x86_64-linux-musl-gcc",
CC_x86_64_unknown_linux_musl: "x86_64-linux-musl-gcc",
}
: {};
linker ?
{
RUSTFLAGS: (env["RUSTFLAGS"] || "") + ` -Clinker=${linker}`,
TARGET_CC: linker,
[`CC_${target || 'x86_64_unknown_linux_musl'}`]: linker,
}
: "win32" === platform
? {
RUSTFLAGS: (env["RUSTFLAGS"] || "") + " -Clinker=rust-lld",
TARGET_CC: "rust-lld",
CC_x86_64_unknown_linux_musl: "rust-lld",
}
: "darwin" === platform
? {
RUSTFLAGS:
(env["RUSTFLAGS"] || "") + " -Clinker=x86_64-linux-musl-gcc",
TARGET_CC: "x86_64-linux-musl-gcc",
CC_x86_64_unknown_linux_musl: "x86_64-linux-musl-gcc",
}
: {};
return {
...defaultEnv,
...platformEnv,
};
}

localSourceDir(profile, platform) {
localSourceDir(funcArgs, profile, platform) {
let executable = "target";
if (MUSL_PLATFORMS.includes(platform)) {
executable = path.join(executable, "x86_64-unknown-linux-musl");
let target = (funcArgs || {}).target || this.custom.target
executable = path.join(executable, target ? target : "x86_64-unknown-linux-musl");
}
return path.join(executable, profile !== "dev" ? "release" : "debug");
}
Expand All @@ -132,7 +150,7 @@ class RustPlugin {
platform()
);

const env = this.localBuildEnv(process.env, platform());
const env = this.localBuildEnv(funcArgs, process.env, platform());
this.serverless.cli.log(`Running local cargo build on ${platform()}`);

const buildResult = spawnSync("cargo", args, {
Expand All @@ -145,13 +163,13 @@ class RustPlugin {
return buildResult;
}
// now rename binary and zip
const sourceDir = this.localSourceDir(profile, platform());
const sourceDir = this.localSourceDir(funcArgs, profile, platform());
const zip = new AdmZip();
zip.addFile(
"bootstrap",
readFileSync(path.join(sourceDir, binary)),
"",
0x755 << 16
0o755
);
const targetDir = this.localArtifactDir(profile);
try {
Expand Down
Loading

0 comments on commit c5d5e71

Please sign in to comment.