clang 22.0.0git
UEFI.cpp
Go to the documentation of this file.
1//===--- UEFI.cpp - UEFI ToolChain Implementations -----------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "UEFI.h"
10#include "clang/Config/config.h"
13#include "clang/Driver/Driver.h"
16#include "llvm/Option/Arg.h"
17#include "llvm/Option/ArgList.h"
18#include "llvm/Support/VirtualFileSystem.h"
19#include "llvm/TargetParser/Host.h"
20
21using namespace clang::driver;
22using namespace clang::driver::toolchains;
23using namespace clang;
24using namespace llvm::opt;
25
26UEFI::UEFI(const Driver &D, const llvm::Triple &Triple, const ArgList &Args)
27 : ToolChain(D, Triple, Args) {}
28
29Tool *UEFI::buildLinker() const { return new tools::uefi::Linker(*this); }
30
31void UEFI::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
32 ArgStringList &CC1Args) const {
33 if (DriverArgs.hasArg(options::OPT_nostdinc))
34 return;
35
36 if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
37 SmallString<128> Dir(getDriver().ResourceDir);
38 llvm::sys::path::append(Dir, "include");
39 addSystemInclude(DriverArgs, CC1Args, Dir.str());
40 }
41
42 if (DriverArgs.hasArg(options::OPT_nostdlibinc))
43 return;
44
45 if (std::optional<std::string> Path = getStdlibIncludePath())
46 addSystemInclude(DriverArgs, CC1Args, *Path);
47}
48
50 const InputInfo &Output,
51 const InputInfoList &Inputs,
52 const ArgList &Args,
53 const char *LinkingOutput) const {
54 ArgStringList CmdArgs;
55 auto &TC = static_cast<const toolchains::UEFI &>(getToolChain());
56
57 assert((Output.isFilename() || Output.isNothing()) && "invalid output");
58 if (Output.isFilename())
59 CmdArgs.push_back(
60 Args.MakeArgString(std::string("-out:") + Output.getFilename()));
61
62 CmdArgs.push_back("-nologo");
63
64 // TODO: Other UEFI binary subsystems that are currently unsupported:
65 // efi_boot_service_driver, efi_rom, efi_runtime_driver.
66 CmdArgs.push_back("-subsystem:efi_application");
67
68 // Default entry function name according to the TianoCore reference
69 // implementation is EfiMain.
70 // TODO: Provide a flag to override the entry function name.
71 CmdArgs.push_back("-entry:EfiMain");
72
73 // "Terminal Service Aware" flag is not needed for UEFI applications.
74 CmdArgs.push_back("-tsaware:no");
75
76 if (Args.hasArg(options::OPT_g_Group, options::OPT__SLASH_Z7))
77 CmdArgs.push_back("-debug");
78
79 Args.AddAllArgValues(CmdArgs, options::OPT__SLASH_link);
80
81 AddLinkerInputs(TC, Inputs, Args, CmdArgs, JA);
82
83 // This should ideally be handled by ToolChain::GetLinkerPath but we need
84 // to special case some linker paths. In the case of lld, we need to
85 // translate 'lld' into 'lld-link'.
86 StringRef Linker = Args.getLastArgValue(options::OPT_fuse_ld_EQ,
87 TC.getDriver().getPreferredLinker());
88 if (Linker.empty() || Linker == "lld")
89 Linker = "lld-link";
90
91 auto LinkerPath = TC.GetProgramPath(Linker.str().c_str());
92 auto LinkCmd = std::make_unique<Command>(
94 Args.MakeArgString(LinkerPath), CmdArgs, Inputs, Output);
95 C.addCommand(std::move(LinkCmd));
96}
Compilation - A set of tasks to perform for a single driver invocation.
Definition Compilation.h:45
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
Definition Driver.h:99
InputInfo - Wrapper for information about an input source.
Definition InputInfo.h:22
const char * getFilename() const
Definition InputInfo.h:83
bool isNothing() const
Definition InputInfo.h:74
bool isFilename() const
Definition InputInfo.h:75
static void addSystemInclude(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args, const Twine &Path)
Utility function to add a system include directory to CC1 arguments.
const Driver & getDriver() const
Definition ToolChain.h:253
ToolChain(const Driver &D, const llvm::Triple &T, const llvm::opt::ArgList &Args)
Definition ToolChain.cpp:89
std::optional< std::string > getStdlibIncludePath() const
Tool - Information on a specific compilation tool.
Definition Tool.h:32
const ToolChain & getToolChain() const
Definition Tool.h:52
Tool * buildLinker() const override
Definition UEFI.cpp:29
UEFI(const Driver &D, const llvm::Triple &Triple, const llvm::opt::ArgList &Args)
Definition UEFI.cpp:26
void AddClangSystemIncludeArgs(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const override
Add the clang cc1 arguments for system include paths.
Definition UEFI.cpp:31
Linker(const ToolChain &TC)
Definition UEFI.h:20
void ConstructJob(Compilation &C, const JobAction &JA, const InputInfo &Output, const InputInfoList &Inputs, const llvm::opt::ArgList &TCArgs, const char *LinkingOutput) const override
ConstructJob - Construct jobs to perform the action JA, writing to Output and with Inputs,...
Definition UEFI.cpp:49
void AddLinkerInputs(const ToolChain &TC, const InputInfoList &Inputs, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs, const JobAction &JA)
SmallVector< InputInfo, 4 > InputInfoList
Definition Driver.h:50
The JSON file list parser is used to communicate input to InstallAPI.
static constexpr ResponseFileSupport AtFileUTF16()
Definition Job.h:99