1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
|
//===-- CacheBuildSession.cpp - cache-build-session tool ------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// Accepts a command that triggers a build and, while the command is running,
// all the compiler invocations that are part of that build will be sharing the
// same dependency scanning daemon.
//
//===----------------------------------------------------------------------===//
#include "CMakeFileAPI.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/StringSet.h"
#include "llvm/Config/config.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/InitLLVM.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/Process.h"
#include "llvm/Support/StringSaver.h"
using namespace llvm;
static Error inferCMakePathPrefixes(StringRef CMakeBuildPath,
SmallVectorImpl<StringRef> &PrefixMaps,
StringSaver &Saver) {
cmake_file_api::Index Index;
if (Error E = cmake_file_api::Index::fromPath(CMakeBuildPath).moveInto(Index))
return E;
cmake_file_api::CodeModel CodeModel;
if (Error E = Index.getCodeModel().moveInto(CodeModel))
return E;
StringRef BuildPath;
if (Error E = CodeModel.getBuildPath().moveInto(BuildPath))
return E;
StringRef SourcePath;
if (Error E = CodeModel.getSourcePath().moveInto(SourcePath))
return E;
SmallVector<StringRef, 8> ExtraSourcePaths;
if (Error E = CodeModel.getExtraTopLevelSourcePaths(ExtraSourcePaths))
return E;
StringSet<> SeenMapNames;
auto getUniqueMapName = [&](StringRef SourcePath) -> StringRef {
StringRef Filename = sys::path::filename(SourcePath);
StringRef MapName = Filename;
unsigned Count = 0;
while (SeenMapNames.contains(MapName)) {
MapName = Saver.save(Filename + "-" + utostr(++Count));
}
SeenMapNames.insert(MapName);
return MapName;
};
PrefixMaps.push_back(Saver.save(BuildPath + "=/^build"));
PrefixMaps.push_back(Saver.save(SourcePath + "=/^src"));
for (StringRef SourcePath : ExtraSourcePaths) {
PrefixMaps.push_back(
Saver.save(SourcePath + "=/^src-" + getUniqueMapName(SourcePath)));
}
return Error::success();
}
int main(int Argc, const char **Argv) {
InitLLVM X(Argc, Argv);
cl::OptionCategory OptCategory("cache-build-session options");
cl::extrahelp MoreHelp("\n"
"Accepts a command that triggers a build and, while "
"the command is running,\n"
"all the compiler invocations that are part of that "
"build will share the same\n"
"dependency scanning daemon.\n"
"\n");
cl::opt<bool> InferCMakePrefixMaps(
"prefix-map-cmake",
cl::desc("Infer source and build prefixes from a CMake build directory"),
cl::cat(OptCategory));
cl::opt<bool> Verbose("v", cl::desc("Verbose output"), cl::cat(OptCategory));
// This is here only to improve the help message (for "USAGE:" line).
cl::list<std::string> Inputs(cl::Positional, cl::desc("command ..."));
int CmdArgsI = 1;
while (CmdArgsI < Argc && Argv[CmdArgsI][0] == '-')
++CmdArgsI;
cl::HideUnrelatedOptions(OptCategory);
cl::ParseCommandLineOptions(CmdArgsI, Argv, "cache-build-session");
SmallVector<const char *, 256> CmdArgs(Argv + CmdArgsI, Argv + Argc);
if (CmdArgs.empty()) {
cl::PrintHelpMessage();
return 1;
}
auto setEnvVar = [&Verbose](const char *Var, const char *Value) {
#if HAVE_SETENV
::setenv(Var, Value, 1);
#elif defined(_WIN32)
_putenv_s(Var, Value);
#else
#error "unsupported environment"
#endif
if (Verbose) {
errs() << "note: setting " << Var << '=' << Value << '\n';
}
};
BumpPtrAllocator Alloc;
StringSaver Saver(Alloc);
if (InferCMakePrefixMaps) {
SmallString<128> WorkingDirectory;
if (Error E = errorCodeToError(sys::fs::current_path(WorkingDirectory))) {
errs() << "error: failed getting working directory: "
<< toString(std::move(E)) << '\n';
return 1;
}
SmallVector<StringRef, 8> PrefixMaps;
if (Error E = inferCMakePathPrefixes(WorkingDirectory, PrefixMaps, Saver)) {
errs() << "error: could not infer prefix map for CMake build: "
<< toString(std::move(E)) << '\n';
return 1;
}
if (!PrefixMaps.empty()) {
constexpr const char *PrefixMapEnvVar = "LLVM_CACHE_PREFIX_MAPS";
SmallString<128> PrefixMapsConcat;
const char *PriorPrefixMaps = ::getenv(PrefixMapEnvVar);
if (PriorPrefixMaps) {
// Merge the derived prefix maps with the pre-existing ones from the
// environment variable.
PrefixMapsConcat.append(PriorPrefixMaps);
PrefixMapsConcat.push_back(';');
}
for (StringRef PrefixMap : PrefixMaps) {
PrefixMapsConcat.append(PrefixMap);
PrefixMapsConcat.push_back(';');
}
assert(PrefixMapsConcat.back() == ';');
PrefixMapsConcat.pop_back();
setEnvVar(PrefixMapEnvVar, Saver.save(PrefixMapsConcat.str()).data());
}
}
// Set 'LLVM_CACHE_BUILD_SESSION_ID' to a unique identifier so that compiler
// invocations under the given command share the same depscan daemon while the
// command is running.
// Uses the process id to ensure parallel invocations of
// `cache-build-session` will not share the same identifier, and
// 'elapsed nanoseconds since epoch' to ensure the same for consecutive
// invocations.
SmallString<32> SessionId;
raw_svector_ostream(SessionId)
<< sys::Process::getProcessId() << '-'
<< std::chrono::system_clock::now().time_since_epoch().count();
setEnvVar("LLVM_CACHE_BUILD_SESSION_ID", SessionId.c_str());
ErrorOr<std::string> ExecPathOrErr = sys::findProgramByName(CmdArgs.front());
if (!ExecPathOrErr) {
errs() << "error: cannot find executable " << CmdArgs.front() << '\n';
return 1;
}
std::string ExecPath = std::move(*ExecPathOrErr);
CmdArgs[0] = ExecPath.c_str();
SmallVector<StringRef, 16> RefArgs;
RefArgs.reserve(CmdArgs.size());
for (const char *Arg : CmdArgs) {
RefArgs.push_back(Arg);
}
std::string ErrMsg;
int Result =
sys::ExecuteAndWait(RefArgs.front(), RefArgs, /*Env*/ std::nullopt,
/*Redirects*/ {}, /*SecondsToWait*/ 0,
/*MemoryLimit*/ 0, &ErrMsg);
if (!ErrMsg.empty()) {
errs() << "error: failed executing command: " << ErrMsg << '\n';
}
return Result;
}
|