@@ -61,6 +61,13 @@ std::string wasm::Linker::getLinkerPath(const ArgList &Args) const {
61
61
return ToolChain.GetProgramPath (ToolChain.getDefaultLinker ());
62
62
}
63
63
64
+ static bool TargetBuildsComponents (const llvm::Triple &TargetTriple) {
65
+ // WASIp2 and above are all based on components, so test for WASI but exclude
66
+ // the original `wasi` target in addition to the `wasip1` name.
67
+ return TargetTriple.isOSWASI () && TargetTriple.getOSName () != " wasip1" &&
68
+ TargetTriple.getOSName () != " wasi" ;
69
+ }
70
+
64
71
void wasm::Linker::ConstructJob (Compilation &C, const JobAction &JA,
65
72
const InputInfo &Output,
66
73
const InputInfoList &Inputs,
@@ -158,46 +165,52 @@ void wasm::Linker::ConstructJob(Compilation &C, const JobAction &JA,
158
165
CmdArgs.push_back (" -o" );
159
166
CmdArgs.push_back (Output.getFilename ());
160
167
161
- if (Args.hasFlag (options::OPT_wasm_opt, options::OPT_no_wasm_opt, true )) {
162
- // When optimizing, if wasm-opt is available, run it.
163
- std::string WasmOptPath;
164
- if (Args.getLastArg (options::OPT_O_Group)) {
165
- WasmOptPath = ToolChain.GetProgramPath (" wasm-opt" );
166
- if (WasmOptPath == " wasm-opt" ) {
167
- WasmOptPath = {};
168
- }
168
+ // Don't use wasm-opt by default on `wasip2` as it doesn't have support for
169
+ // components at this time. Retain the historical default otherwise, though,
170
+ // of running `wasm-opt` by default.
171
+ bool WasmOptDefault = !TargetBuildsComponents (ToolChain.getTriple ());
172
+ bool RunWasmOpt = Args.hasFlag (options::OPT_wasm_opt,
173
+ options::OPT_no_wasm_opt, WasmOptDefault);
174
+
175
+ // If wasm-opt is enabled and optimizations are happening look for the
176
+ // `wasm-opt` program. If it's not found auto-disable it.
177
+ std::string WasmOptPath;
178
+ if (RunWasmOpt && Args.getLastArg (options::OPT_O_Group)) {
179
+ WasmOptPath = ToolChain.GetProgramPath (" wasm-opt" );
180
+ if (WasmOptPath == " wasm-opt" ) {
181
+ WasmOptPath = {};
169
182
}
183
+ }
170
184
171
- if (!WasmOptPath.empty ()) {
172
- CmdArgs.push_back (" --keep-section=target_features" );
173
- }
185
+ if (!WasmOptPath.empty ()) {
186
+ CmdArgs.push_back (" --keep-section=target_features" );
187
+ }
174
188
175
- C.addCommand (std::make_unique<Command>(JA, *this ,
176
- ResponseFileSupport::AtFileCurCP (),
177
- Linker, CmdArgs, Inputs, Output));
178
-
179
- if (Arg *A = Args.getLastArg (options::OPT_O_Group)) {
180
- if (!WasmOptPath.empty ()) {
181
- StringRef OOpt = " s" ;
182
- if (A->getOption ().matches (options::OPT_O4) ||
183
- A->getOption ().matches (options::OPT_Ofast))
184
- OOpt = " 4" ;
185
- else if (A->getOption ().matches (options::OPT_O0))
186
- OOpt = " 0" ;
187
- else if (A->getOption ().matches (options::OPT_O))
188
- OOpt = A->getValue ();
189
-
190
- if (OOpt != " 0" ) {
191
- const char *WasmOpt = Args.MakeArgString (WasmOptPath);
192
- ArgStringList OptArgs;
193
- OptArgs.push_back (Output.getFilename ());
194
- OptArgs.push_back (Args.MakeArgString (llvm::Twine (" -O" ) + OOpt));
195
- OptArgs.push_back (" -o" );
196
- OptArgs.push_back (Output.getFilename ());
197
- C.addCommand (std::make_unique<Command>(
198
- JA, *this , ResponseFileSupport::AtFileCurCP (), WasmOpt, OptArgs,
199
- Inputs, Output));
200
- }
189
+ C.addCommand (std::make_unique<Command>(JA, *this ,
190
+ ResponseFileSupport::AtFileCurCP (),
191
+ Linker, CmdArgs, Inputs, Output));
192
+
193
+ if (Arg *A = Args.getLastArg (options::OPT_O_Group)) {
194
+ if (!WasmOptPath.empty ()) {
195
+ StringRef OOpt = " s" ;
196
+ if (A->getOption ().matches (options::OPT_O4) ||
197
+ A->getOption ().matches (options::OPT_Ofast))
198
+ OOpt = " 4" ;
199
+ else if (A->getOption ().matches (options::OPT_O0))
200
+ OOpt = " 0" ;
201
+ else if (A->getOption ().matches (options::OPT_O))
202
+ OOpt = A->getValue ();
203
+
204
+ if (OOpt != " 0" ) {
205
+ const char *WasmOpt = Args.MakeArgString (WasmOptPath);
206
+ ArgStringList OptArgs;
207
+ OptArgs.push_back (Output.getFilename ());
208
+ OptArgs.push_back (Args.MakeArgString (llvm::Twine (" -O" ) + OOpt));
209
+ OptArgs.push_back (" -o" );
210
+ OptArgs.push_back (Output.getFilename ());
211
+ C.addCommand (std::make_unique<Command>(
212
+ JA, *this , ResponseFileSupport::AtFileCurCP (), WasmOpt, OptArgs,
213
+ Inputs, Output));
201
214
}
202
215
}
203
216
}
@@ -241,7 +254,7 @@ WebAssembly::WebAssembly(const Driver &D, const llvm::Triple &Triple,
241
254
}
242
255
243
256
const char *WebAssembly::getDefaultLinker () const {
244
- if (getOS () == " wasip2 " )
257
+ if (TargetBuildsComponents ( getTriple ()) )
245
258
return " wasm-component-ld" ;
246
259
return " wasm-ld" ;
247
260
}
0 commit comments