xref: /openbsd-src/gnu/llvm/clang/lib/Driver/ToolChains/InterfaceStubs.cpp (revision a9ac8606c53d55cee9c3a39778b249c51df111ef)
1e5dd7070Spatrick //===---  InterfaceStubs.cpp - Base InterfaceStubs Implementations C++  ---===//
2e5dd7070Spatrick //
3e5dd7070Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e5dd7070Spatrick // See https://llvm.org/LICENSE.txt for license information.
5e5dd7070Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e5dd7070Spatrick //
7e5dd7070Spatrick //===----------------------------------------------------------------------===//
8e5dd7070Spatrick 
9e5dd7070Spatrick #include "InterfaceStubs.h"
10e5dd7070Spatrick #include "CommonArgs.h"
11e5dd7070Spatrick #include "clang/Driver/Compilation.h"
12e5dd7070Spatrick #include "llvm/Support/Path.h"
13e5dd7070Spatrick 
14e5dd7070Spatrick namespace clang {
15e5dd7070Spatrick namespace driver {
16e5dd7070Spatrick namespace tools {
17e5dd7070Spatrick namespace ifstool {
ConstructJob(Compilation & C,const JobAction & JA,const InputInfo & Output,const InputInfoList & Inputs,const llvm::opt::ArgList & Args,const char * LinkingOutput) const18e5dd7070Spatrick void Merger::ConstructJob(Compilation &C, const JobAction &JA,
19e5dd7070Spatrick                           const InputInfo &Output, const InputInfoList &Inputs,
20e5dd7070Spatrick                           const llvm::opt::ArgList &Args,
21e5dd7070Spatrick                           const char *LinkingOutput) const {
22e5dd7070Spatrick   std::string Merger = getToolChain().GetProgramPath(getShortName());
23*a9ac8606Spatrick   // TODO: Use IFS library directly in the future.
24e5dd7070Spatrick   llvm::opt::ArgStringList CmdArgs;
25*a9ac8606Spatrick   CmdArgs.push_back("--input-format=IFS");
26e5dd7070Spatrick   const bool WriteBin = !Args.getLastArg(options::OPT_emit_merged_ifs);
27*a9ac8606Spatrick   CmdArgs.push_back(WriteBin ? "--output-format=ELF" : "--output-format=IFS");
28e5dd7070Spatrick   CmdArgs.push_back("-o");
29e5dd7070Spatrick 
30e5dd7070Spatrick   // Normally we want to write to a side-car file ending in ".ifso" so for
31e5dd7070Spatrick   // example if `clang -emit-interface-stubs -shared -o libhello.so` were
32e5dd7070Spatrick   // invoked then we would like to get libhello.so and libhello.ifso. If the
33e5dd7070Spatrick   // stdout stream is given as the output file (ie `-o -`), that is the one
34e5dd7070Spatrick   // exception where we will just append to the same filestream as the normal
35e5dd7070Spatrick   // output.
36e5dd7070Spatrick   SmallString<128> OutputFilename(Output.getFilename());
37e5dd7070Spatrick   if (OutputFilename != "-") {
38e5dd7070Spatrick     if (Args.hasArg(options::OPT_shared))
39e5dd7070Spatrick       llvm::sys::path::replace_extension(OutputFilename,
40e5dd7070Spatrick                                          (WriteBin ? "ifso" : "ifs"));
41e5dd7070Spatrick     else
42e5dd7070Spatrick       OutputFilename += (WriteBin ? ".ifso" : ".ifs");
43e5dd7070Spatrick   }
44e5dd7070Spatrick 
45e5dd7070Spatrick   CmdArgs.push_back(Args.MakeArgString(OutputFilename.c_str()));
46e5dd7070Spatrick 
47e5dd7070Spatrick   // Here we append the input files. If the input files are object files, then
48e5dd7070Spatrick   // we look for .ifs files present in the same location as the object files.
49e5dd7070Spatrick   for (const auto &Input : Inputs) {
50e5dd7070Spatrick     if (!Input.isFilename())
51e5dd7070Spatrick       continue;
52e5dd7070Spatrick     SmallString<128> InputFilename(Input.getFilename());
53e5dd7070Spatrick     if (Input.getType() == types::TY_Object)
54e5dd7070Spatrick       llvm::sys::path::replace_extension(InputFilename, ".ifs");
55e5dd7070Spatrick     CmdArgs.push_back(Args.MakeArgString(InputFilename.c_str()));
56e5dd7070Spatrick   }
57e5dd7070Spatrick 
58ec727ea7Spatrick   C.addCommand(std::make_unique<Command>(JA, *this, ResponseFileSupport::None(),
59ec727ea7Spatrick                                          Args.MakeArgString(Merger), CmdArgs,
60*a9ac8606Spatrick                                          Inputs, Output));
61e5dd7070Spatrick }
62e5dd7070Spatrick } // namespace ifstool
63e5dd7070Spatrick } // namespace tools
64e5dd7070Spatrick } // namespace driver
65e5dd7070Spatrick } // namespace clang
66