1 //===--- CompileCommands.cpp ----------------------------------------------===// 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 "CompileCommands.h" 10 #include "Config.h" 11 #include "support/Logger.h" 12 #include "clang/Frontend/CompilerInvocation.h" 13 #include "clang/Tooling/ArgumentsAdjusters.h" 14 #include "llvm/Support/FileSystem.h" 15 #include "llvm/Support/FileUtilities.h" 16 #include "llvm/Support/MemoryBuffer.h" 17 #include "llvm/Support/Path.h" 18 #include "llvm/Support/Program.h" 19 20 namespace clang { 21 namespace clangd { 22 namespace { 23 24 // Query apple's `xcrun` launcher, which is the source of truth for "how should" 25 // clang be invoked on this system. 26 llvm::Optional<std::string> queryXcrun(llvm::ArrayRef<llvm::StringRef> Argv) { 27 auto Xcrun = llvm::sys::findProgramByName("xcrun"); 28 if (!Xcrun) { 29 log("Couldn't find xcrun. Hopefully you have a non-apple toolchain..."); 30 return llvm::None; 31 } 32 llvm::SmallString<64> OutFile; 33 llvm::sys::fs::createTemporaryFile("clangd-xcrun", "", OutFile); 34 llvm::FileRemover OutRemover(OutFile); 35 llvm::Optional<llvm::StringRef> Redirects[3] = { 36 /*stdin=*/{""}, /*stdout=*/{OutFile}, /*stderr=*/{""}}; 37 vlog("Invoking {0} to find clang installation", *Xcrun); 38 int Ret = llvm::sys::ExecuteAndWait(*Xcrun, Argv, 39 /*Env=*/llvm::None, Redirects, 40 /*SecondsToWait=*/10); 41 if (Ret != 0) { 42 log("xcrun exists but failed with code {0}. " 43 "If you have a non-apple toolchain, this is OK. " 44 "Otherwise, try xcode-select --install.", 45 Ret); 46 return llvm::None; 47 } 48 49 auto Buf = llvm::MemoryBuffer::getFile(OutFile); 50 if (!Buf) { 51 log("Can't read xcrun output: {0}", Buf.getError().message()); 52 return llvm::None; 53 } 54 StringRef Path = Buf->get()->getBuffer().trim(); 55 if (Path.empty()) { 56 log("xcrun produced no output"); 57 return llvm::None; 58 } 59 return Path.str(); 60 } 61 62 // Resolve symlinks if possible. 63 std::string resolve(std::string Path) { 64 llvm::SmallString<128> Resolved; 65 if (llvm::sys::fs::real_path(Path, Resolved)) { 66 log("Failed to resolve possible symlink {0}", Path); 67 return Path; 68 } 69 return std::string(Resolved.str()); 70 } 71 72 // Get a plausible full `clang` path. 73 // This is used in the fallback compile command, or when the CDB returns a 74 // generic driver with no path. 75 std::string detectClangPath() { 76 // The driver and/or cc1 sometimes depend on the binary name to compute 77 // useful things like the standard library location. 78 // We need to emulate what clang on this system is likely to see. 79 // cc1 in particular looks at the "real path" of the running process, and 80 // so if /usr/bin/clang is a symlink, it sees the resolved path. 81 // clangd doesn't have that luxury, so we resolve symlinks ourselves. 82 83 // On Mac, `which clang` is /usr/bin/clang. It runs `xcrun clang`, which knows 84 // where the real clang is kept. We need to do the same thing, 85 // because cc1 (not the driver!) will find libc++ relative to argv[0]. 86 #ifdef __APPLE__ 87 if (auto MacClang = queryXcrun({"xcrun", "--find", "clang"})) 88 return resolve(std::move(*MacClang)); 89 #endif 90 // On other platforms, just look for compilers on the PATH. 91 for (const char *Name : {"clang", "gcc", "cc"}) 92 if (auto PathCC = llvm::sys::findProgramByName(Name)) 93 return resolve(std::move(*PathCC)); 94 // Fallback: a nonexistent 'clang' binary next to clangd. 95 static int Dummy; 96 std::string ClangdExecutable = 97 llvm::sys::fs::getMainExecutable("clangd", (void *)&Dummy); 98 SmallString<128> ClangPath; 99 ClangPath = llvm::sys::path::parent_path(ClangdExecutable); 100 llvm::sys::path::append(ClangPath, "clang"); 101 return std::string(ClangPath.str()); 102 } 103 104 // On mac, /usr/bin/clang sets SDKROOT and then invokes the real clang. 105 // The effect of this is to set -isysroot correctly. We do the same. 106 const llvm::Optional<std::string> detectSysroot() { 107 #ifndef __APPLE__ 108 return llvm::None; 109 #endif 110 111 // SDKROOT overridden in environment, respect it. Driver will set isysroot. 112 if (::getenv("SDKROOT")) 113 return llvm::None; 114 return queryXcrun({"xcrun", "--show-sdk-path"}); 115 return llvm::None; 116 } 117 118 std::string detectStandardResourceDir() { 119 static int Dummy; // Just an address in this process. 120 return CompilerInvocation::GetResourcesPath("clangd", (void *)&Dummy); 121 } 122 123 // The path passed to argv[0] is important: 124 // - its parent directory is Driver::Dir, used for library discovery 125 // - its basename affects CLI parsing (clang-cl) and other settings 126 // Where possible it should be an absolute path with sensible directory, but 127 // with the original basename. 128 static std::string resolveDriver(llvm::StringRef Driver, bool FollowSymlink, 129 llvm::Optional<std::string> ClangPath) { 130 auto SiblingOf = [&](llvm::StringRef AbsPath) { 131 llvm::SmallString<128> Result = llvm::sys::path::parent_path(AbsPath); 132 llvm::sys::path::append(Result, llvm::sys::path::filename(Driver)); 133 return Result.str().str(); 134 }; 135 136 // First, eliminate relative paths. 137 std::string Storage; 138 if (!llvm::sys::path::is_absolute(Driver)) { 139 // If it's working-dir relative like bin/clang, we can't resolve it. 140 // FIXME: we could if we had the working directory here. 141 // Let's hope it's not a symlink. 142 if (llvm::any_of(Driver, 143 [](char C) { return llvm::sys::path::is_separator(C); })) 144 return Driver.str(); 145 // If the driver is a generic like "g++" with no path, add clang dir. 146 if (ClangPath && 147 (Driver == "clang" || Driver == "clang++" || Driver == "gcc" || 148 Driver == "g++" || Driver == "cc" || Driver == "c++")) { 149 return SiblingOf(*ClangPath); 150 } 151 // Otherwise try to look it up on PATH. This won't change basename. 152 auto Absolute = llvm::sys::findProgramByName(Driver); 153 if (Absolute && llvm::sys::path::is_absolute(*Absolute)) 154 Driver = Storage = std::move(*Absolute); 155 else if (ClangPath) // If we don't find it, use clang dir again. 156 return SiblingOf(*ClangPath); 157 else // Nothing to do: can't find the command and no detected dir. 158 return Driver.str(); 159 } 160 161 // Now we have an absolute path, but it may be a symlink. 162 assert(llvm::sys::path::is_absolute(Driver)); 163 if (FollowSymlink) { 164 llvm::SmallString<256> Resolved; 165 if (!llvm::sys::fs::real_path(Driver, Resolved)) 166 return SiblingOf(Resolved); 167 } 168 return Driver.str(); 169 } 170 171 } // namespace 172 173 CommandMangler CommandMangler::detect() { 174 CommandMangler Result; 175 Result.ClangPath = detectClangPath(); 176 Result.ResourceDir = detectStandardResourceDir(); 177 Result.Sysroot = detectSysroot(); 178 return Result; 179 } 180 181 CommandMangler CommandMangler::forTests() { 182 return CommandMangler(); 183 } 184 185 void CommandMangler::adjust(std::vector<std::string> &Cmd) const { 186 for (auto &Edit : Config::current().CompileFlags.Edits) 187 Edit(Cmd); 188 189 // Check whether the flag exists, either as -flag or -flag=* 190 auto Has = [&](llvm::StringRef Flag) { 191 for (llvm::StringRef Arg : Cmd) { 192 if (Arg.consume_front(Flag) && (Arg.empty() || Arg[0] == '=')) 193 return true; 194 } 195 return false; 196 }; 197 198 // clangd should not write files to disk, including dependency files 199 // requested on the command line. 200 Cmd = tooling::getClangStripDependencyFileAdjuster()(Cmd, ""); 201 // Strip plugin related command line arguments. Clangd does 202 // not support plugins currently. Therefore it breaks if 203 // compiler tries to load plugins. 204 Cmd = tooling::getStripPluginsAdjuster()(Cmd, ""); 205 Cmd = tooling::getClangSyntaxOnlyAdjuster()(Cmd, ""); 206 207 if (ResourceDir && !Has("-resource-dir")) 208 Cmd.push_back(("-resource-dir=" + *ResourceDir)); 209 210 // Don't set `-isysroot` if it is already set or if `--sysroot` is set. 211 // `--sysroot` is a superset of the `-isysroot` argument. 212 if (Sysroot && !Has("-isysroot") && !Has("--sysroot")) { 213 Cmd.push_back("-isysroot"); 214 Cmd.push_back(*Sysroot); 215 } 216 217 if (!Cmd.empty()) { 218 bool FollowSymlink = !Has("-no-canonical-prefixes"); 219 Cmd.front() = 220 (FollowSymlink ? ResolvedDrivers : ResolvedDriversNoFollow) 221 .get(Cmd.front(), [&, this] { 222 return resolveDriver(Cmd.front(), FollowSymlink, ClangPath); 223 }); 224 } 225 } 226 227 CommandMangler::operator clang::tooling::ArgumentsAdjuster() && { 228 // ArgumentsAdjuster is a std::function and so must be copyable. 229 return [Mangler = std::make_shared<CommandMangler>(std::move(*this))]( 230 const std::vector<std::string> &Args, llvm::StringRef File) { 231 auto Result = Args; 232 Mangler->adjust(Result); 233 return Result; 234 }; 235 } 236 237 } // namespace clangd 238 } // namespace clang 239