xref: /netbsd-src/external/apache2/llvm/dist/clang/lib/Driver/ToolChains/InterfaceStubs.cpp (revision e038c9c4676b0f19b1b7dd08a940c6ed64a6d5ae)
17330f729Sjoerg //===---  InterfaceStubs.cpp - Base InterfaceStubs Implementations C++  ---===//
27330f729Sjoerg //
37330f729Sjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
47330f729Sjoerg // See https://llvm.org/LICENSE.txt for license information.
57330f729Sjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
67330f729Sjoerg //
77330f729Sjoerg //===----------------------------------------------------------------------===//
87330f729Sjoerg 
97330f729Sjoerg #include "InterfaceStubs.h"
107330f729Sjoerg #include "CommonArgs.h"
117330f729Sjoerg #include "clang/Driver/Compilation.h"
12*e038c9c4Sjoerg #include "llvm/Support/Path.h"
137330f729Sjoerg 
147330f729Sjoerg namespace clang {
157330f729Sjoerg namespace driver {
167330f729Sjoerg namespace tools {
177330f729Sjoerg namespace ifstool {
ConstructJob(Compilation & C,const JobAction & JA,const InputInfo & Output,const InputInfoList & Inputs,const llvm::opt::ArgList & Args,const char * LinkingOutput) const187330f729Sjoerg void Merger::ConstructJob(Compilation &C, const JobAction &JA,
197330f729Sjoerg                           const InputInfo &Output, const InputInfoList &Inputs,
207330f729Sjoerg                           const llvm::opt::ArgList &Args,
217330f729Sjoerg                           const char *LinkingOutput) const {
227330f729Sjoerg   std::string Merger = getToolChain().GetProgramPath(getShortName());
237330f729Sjoerg   llvm::opt::ArgStringList CmdArgs;
247330f729Sjoerg   CmdArgs.push_back("-action");
25*e038c9c4Sjoerg   const bool WriteBin = !Args.getLastArg(options::OPT_emit_merged_ifs);
26*e038c9c4Sjoerg   CmdArgs.push_back(WriteBin ? "write-bin" : "write-ifs");
277330f729Sjoerg   CmdArgs.push_back("-o");
28*e038c9c4Sjoerg 
29*e038c9c4Sjoerg   // Normally we want to write to a side-car file ending in ".ifso" so for
30*e038c9c4Sjoerg   // example if `clang -emit-interface-stubs -shared -o libhello.so` were
31*e038c9c4Sjoerg   // invoked then we would like to get libhello.so and libhello.ifso. If the
32*e038c9c4Sjoerg   // stdout stream is given as the output file (ie `-o -`), that is the one
33*e038c9c4Sjoerg   // exception where we will just append to the same filestream as the normal
34*e038c9c4Sjoerg   // output.
35*e038c9c4Sjoerg   SmallString<128> OutputFilename(Output.getFilename());
36*e038c9c4Sjoerg   if (OutputFilename != "-") {
37*e038c9c4Sjoerg     if (Args.hasArg(options::OPT_shared))
38*e038c9c4Sjoerg       llvm::sys::path::replace_extension(OutputFilename,
39*e038c9c4Sjoerg                                          (WriteBin ? "ifso" : "ifs"));
40*e038c9c4Sjoerg     else
41*e038c9c4Sjoerg       OutputFilename += (WriteBin ? ".ifso" : ".ifs");
42*e038c9c4Sjoerg   }
43*e038c9c4Sjoerg 
44*e038c9c4Sjoerg   CmdArgs.push_back(Args.MakeArgString(OutputFilename.c_str()));
45*e038c9c4Sjoerg 
46*e038c9c4Sjoerg   // Here we append the input files. If the input files are object files, then
47*e038c9c4Sjoerg   // we look for .ifs files present in the same location as the object files.
48*e038c9c4Sjoerg   for (const auto &Input : Inputs) {
49*e038c9c4Sjoerg     if (!Input.isFilename())
50*e038c9c4Sjoerg       continue;
51*e038c9c4Sjoerg     SmallString<128> InputFilename(Input.getFilename());
52*e038c9c4Sjoerg     if (Input.getType() == types::TY_Object)
53*e038c9c4Sjoerg       llvm::sys::path::replace_extension(InputFilename, ".ifs");
54*e038c9c4Sjoerg     CmdArgs.push_back(Args.MakeArgString(InputFilename.c_str()));
55*e038c9c4Sjoerg   }
56*e038c9c4Sjoerg 
57*e038c9c4Sjoerg   C.addCommand(std::make_unique<Command>(JA, *this, ResponseFileSupport::None(),
58*e038c9c4Sjoerg                                          Args.MakeArgString(Merger), CmdArgs,
59*e038c9c4Sjoerg                                          Inputs, Output));
607330f729Sjoerg }
617330f729Sjoerg } // namespace ifstool
627330f729Sjoerg } // namespace tools
637330f729Sjoerg } // namespace driver
647330f729Sjoerg } // namespace clang
65