Skip to content

Commit 7e91c70

Browse files
Implement local target and linker options
1 parent f8c0f19 commit 7e91c70

File tree

1 file changed

+40
-22
lines changed

1 file changed

+40
-22
lines changed

index.js

Lines changed: 40 additions & 22 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,7 +163,7 @@ 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",

0 commit comments

Comments
 (0)