@@ -61,6 +61,13 @@ std::string wasm::Linker::getLinkerPath(const ArgList &Args) const {
6161  return  ToolChain.GetProgramPath (ToolChain.getDefaultLinker ());
6262}
6363
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+ 
6471void  wasm::Linker::ConstructJob (Compilation &C, const  JobAction &JA,
6572                                const  InputInfo &Output,
6673                                const  InputInfoList &Inputs,
@@ -158,46 +165,52 @@ void wasm::Linker::ConstructJob(Compilation &C, const JobAction &JA,
158165  CmdArgs.push_back (" -o"  );
159166  CmdArgs.push_back (Output.getFilename ());
160167
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 = {};
169182    }
183+   }
170184
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+   }
174188
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));
201214      }
202215    }
203216  }
@@ -241,7 +254,7 @@ WebAssembly::WebAssembly(const Driver &D, const llvm::Triple &Triple,
241254}
242255
243256const  char  *WebAssembly::getDefaultLinker () const  {
244-   if  (getOS () ==  " wasip2 "  )
257+   if  (TargetBuildsComponents ( getTriple ()) )
245258    return  " wasm-component-ld"  ;
246259  return  " wasm-ld"  ;
247260}
0 commit comments