Skip to content

Commit c5d5e71

Browse files
authored
Merge pull request softprops#112 from StefanNienhuis/local-target-linker-options
Local target linker options
2 parents d6686a6 + a0b0eb6 commit c5d5e71

File tree

4 files changed

+13464
-47
lines changed

4 files changed

+13464
-47
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,15 @@ custom:
100100
cargoFlags: '--features enable-awesome'
101101
# experimental! when set to true, artifacts are built locally outside of docker
102102
+ dockerless: true
103+
104+
# when using local builds (dockerless), optionally provide a different target and linker for the compiler
105+
# for example, allow local running on ARM macs
106+
target: aarch64-apple-darwin
107+
linker: clang
103108
```
104109

110+
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.
111+
105112
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.
106113

107114
In order to use this mode its expected that you install the `x86_64-unknown-linux-musl` target on all platforms locally with

index.js

Lines changed: 41 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,16 @@ class RustPlugin {
7373
this.custom.cargoFlags ||
7474
""
7575
).split(/\s+/);
76-
const targetArgs = MUSL_PLATFORMS.includes(platform)
77-
? ["--target", "x86_64-unknown-linux-musl"]
78-
: [];
76+
77+
78+
let target = (funcArgs || {}).target || this.custom.target
79+
80+
const targetArgs =
81+
target ?
82+
['--target', target]
83+
: MUSL_PLATFORMS.includes(platform)
84+
? ["--target", "x86_64-unknown-linux-musl"]
85+
: [];
7986
return [
8087
...defaultArgs,
8188
...profileArgs,
@@ -84,33 +91,44 @@ class RustPlugin {
8491
].filter((i) => i);
8592
}
8693

87-
localBuildEnv(env, platform) {
94+
localBuildEnv(funcArgs, env, platform) {
8895
const defaultEnv = { ...env };
96+
97+
let target = (funcArgs || {}).target || this.custom.target
98+
let linker = (funcArgs || {}).linker || this.custom.linker
99+
89100
const platformEnv =
90-
"win32" === platform
91-
? {
92-
RUSTFLAGS: (env["RUSTFLAGS"] || "") + " -Clinker=rust-lld",
93-
TARGET_CC: "rust-lld",
94-
CC_x86_64_unknown_linux_musl: "rust-lld",
95-
}
96-
: "darwin" === platform
97-
? {
98-
RUSTFLAGS:
99-
(env["RUSTFLAGS"] || "") + " -Clinker=x86_64-linux-musl-gcc",
100-
TARGET_CC: "x86_64-linux-musl-gcc",
101-
CC_x86_64_unknown_linux_musl: "x86_64-linux-musl-gcc",
102-
}
103-
: {};
101+
linker ?
102+
{
103+
RUSTFLAGS: (env["RUSTFLAGS"] || "") + ` -Clinker=${linker}`,
104+
TARGET_CC: linker,
105+
[`CC_${target || 'x86_64_unknown_linux_musl'}`]: linker,
106+
}
107+
: "win32" === platform
108+
? {
109+
RUSTFLAGS: (env["RUSTFLAGS"] || "") + " -Clinker=rust-lld",
110+
TARGET_CC: "rust-lld",
111+
CC_x86_64_unknown_linux_musl: "rust-lld",
112+
}
113+
: "darwin" === platform
114+
? {
115+
RUSTFLAGS:
116+
(env["RUSTFLAGS"] || "") + " -Clinker=x86_64-linux-musl-gcc",
117+
TARGET_CC: "x86_64-linux-musl-gcc",
118+
CC_x86_64_unknown_linux_musl: "x86_64-linux-musl-gcc",
119+
}
120+
: {};
104121
return {
105122
...defaultEnv,
106123
...platformEnv,
107124
};
108125
}
109126

110-
localSourceDir(profile, platform) {
127+
localSourceDir(funcArgs, profile, platform) {
111128
let executable = "target";
112129
if (MUSL_PLATFORMS.includes(platform)) {
113-
executable = path.join(executable, "x86_64-unknown-linux-musl");
130+
let target = (funcArgs || {}).target || this.custom.target
131+
executable = path.join(executable, target ? target : "x86_64-unknown-linux-musl");
114132
}
115133
return path.join(executable, profile !== "dev" ? "release" : "debug");
116134
}
@@ -132,7 +150,7 @@ class RustPlugin {
132150
platform()
133151
);
134152

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

138156
const buildResult = spawnSync("cargo", args, {
@@ -145,13 +163,13 @@ class RustPlugin {
145163
return buildResult;
146164
}
147165
// now rename binary and zip
148-
const sourceDir = this.localSourceDir(profile, platform());
166+
const sourceDir = this.localSourceDir(funcArgs, profile, platform());
149167
const zip = new AdmZip();
150168
zip.addFile(
151169
"bootstrap",
152170
readFileSync(path.join(sourceDir, binary)),
153171
"",
154-
0x755 << 16
172+
0o755
155173
);
156174
const targetDir = this.localArtifactDir(profile);
157175
try {

0 commit comments

Comments
 (0)