|
| 1 | +// |
| 2 | +// Licensed under the Apache License v2.0 with LLVM Exceptions. |
| 3 | +// See https://llvm.org/LICENSE.txt for license information. |
| 4 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 5 | +// |
| 6 | +// Copyright (c) 2024 Fernando Pelliccioni ([email protected]) |
| 7 | +// |
| 8 | +// Official repository: https://github.com/cppalliance/mrdocs |
| 9 | +// |
| 10 | + |
| 11 | +#include "lib/Lib/CMakeExecution.hpp" |
| 12 | + |
| 13 | +#include <llvm/Support/FileSystem.h> |
| 14 | +#include <llvm/Support/MemoryBuffer.h> |
| 15 | +#include <llvm/Support/Path.h> |
| 16 | +#include <llvm/Support/Program.h> |
| 17 | + |
| 18 | +namespace clang { |
| 19 | +namespace mrdocs { |
| 20 | + |
| 21 | +namespace { |
| 22 | + |
| 23 | +Expected<std::string> |
| 24 | +getCmakePath() { |
| 25 | + auto const path = llvm::sys::findProgramByName("cmake"); |
| 26 | + MRDOCS_CHECK(path, "CMake executable not found"); |
| 27 | + std::optional<llvm::StringRef> const redirects[] = {llvm::StringRef(), llvm::StringRef(), llvm::StringRef()}; |
| 28 | + std::vector<llvm::StringRef> const args = {*path, "--version"}; |
| 29 | + int const result = llvm::sys::ExecuteAndWait(*path, args, std::nullopt, redirects); |
| 30 | + MRDOCS_CHECK(result == 0, "CMake execution failed when checking version"); |
| 31 | + return *path; |
| 32 | +} |
| 33 | + |
| 34 | +Expected<std::string> |
| 35 | +executeCmakeHelp(llvm::StringRef cmakePath) |
| 36 | +{ |
| 37 | + llvm::SmallString<128> outputPath; |
| 38 | + MRDOCS_CHECK(!llvm::sys::fs::createTemporaryFile("cmake-help", "txt", outputPath), |
| 39 | + "Failed to create temporary file"); |
| 40 | + std::optional<llvm::StringRef> const redirects[] = {llvm::StringRef(), outputPath.str(), llvm::StringRef()}; |
| 41 | + std::vector<llvm::StringRef> const args = {cmakePath, "--help"}; |
| 42 | + llvm::ArrayRef<llvm::StringRef> emptyEnv; |
| 43 | + int const result = llvm::sys::ExecuteAndWait(cmakePath, args, emptyEnv, redirects); |
| 44 | + MRDOCS_CHECK(result == 0, "CMake execution failed when trying to get help"); |
| 45 | + |
| 46 | + auto const bufferOrError = llvm::MemoryBuffer::getFile(outputPath); |
| 47 | + MRDOCS_CHECK(bufferOrError, "Failed to read CMake help output"); |
| 48 | + |
| 49 | + return bufferOrError.get()->getBuffer().str(); |
| 50 | +} |
| 51 | + |
| 52 | +Expected<std::string> |
| 53 | +getCmakeDefaultGenerator(llvm::StringRef cmakePath) |
| 54 | +{ |
| 55 | + MRDOCS_TRY(auto const cmakeHelp, executeCmakeHelp(cmakePath)); |
| 56 | + |
| 57 | + std::istringstream stream(cmakeHelp); |
| 58 | + std::string line; |
| 59 | + std::string defaultGenerator; |
| 60 | + |
| 61 | + while (std::getline(stream, line)) { |
| 62 | + if (line[0] == '*' && line[1] == ' ') { |
| 63 | + size_t const start = 2; |
| 64 | + size_t const end = line.find("=", start); |
| 65 | + if (end == std::string::npos) { |
| 66 | + continue; |
| 67 | + } |
| 68 | + return line.substr(start, end - start); |
| 69 | + } |
| 70 | + } |
| 71 | + return Unexpected(Error("Default CMake generator not found")); |
| 72 | +} |
| 73 | + |
| 74 | +Expected<bool> |
| 75 | +cmakeDefaultGeneratorIsVisualStudio(llvm::StringRef cmakePath) |
| 76 | +{ |
| 77 | + MRDOCS_TRY(auto const defaultGenerator, getCmakeDefaultGenerator(cmakePath)); |
| 78 | + return defaultGenerator.starts_with("Visual Studio"); |
| 79 | +} |
| 80 | + |
| 81 | +std::vector<std::string> |
| 82 | +parseCmakeArgs(std::string const& cmakeArgsStr) { |
| 83 | + std::vector<std::string> args; |
| 84 | + std::string currentArg; |
| 85 | + char quoteChar = '\0'; |
| 86 | + bool escapeNextChar = false; |
| 87 | + |
| 88 | + for (char ch : cmakeArgsStr) |
| 89 | + { |
| 90 | + if (escapeNextChar) |
| 91 | + { |
| 92 | + currentArg += ch; |
| 93 | + escapeNextChar = false; |
| 94 | + } |
| 95 | + else if (ch == '\\') |
| 96 | + { |
| 97 | + escapeNextChar = true; |
| 98 | + } |
| 99 | + else if ((ch == '"' || ch == '\'')) |
| 100 | + { |
| 101 | + if (quoteChar == '\0') |
| 102 | + { |
| 103 | + quoteChar = ch; |
| 104 | + } |
| 105 | + else if (ch == quoteChar) |
| 106 | + { |
| 107 | + quoteChar = '\0'; |
| 108 | + } |
| 109 | + else |
| 110 | + { |
| 111 | + currentArg.push_back(ch); |
| 112 | + } |
| 113 | + } else if (std::isspace(ch)) |
| 114 | + { |
| 115 | + if (quoteChar != '\0') |
| 116 | + { |
| 117 | + currentArg.push_back(ch); |
| 118 | + } |
| 119 | + else |
| 120 | + { |
| 121 | + if ( ! currentArg.empty()) |
| 122 | + { |
| 123 | + args.push_back(currentArg); |
| 124 | + currentArg.clear(); |
| 125 | + } |
| 126 | + } |
| 127 | + } else |
| 128 | + { |
| 129 | + currentArg += ch; |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + if ( ! currentArg.empty()) |
| 134 | + { |
| 135 | + args.push_back(currentArg); |
| 136 | + } |
| 137 | + |
| 138 | + return args; |
| 139 | +} |
| 140 | + |
| 141 | +} // anonymous namespace |
| 142 | + |
| 143 | +Expected<std::string> |
| 144 | +executeCmakeExportCompileCommands(llvm::StringRef projectPath, llvm::StringRef cmakeArgs) |
| 145 | +{ |
| 146 | + MRDOCS_CHECK(llvm::sys::fs::exists(projectPath), "Project path does not exist"); |
| 147 | + MRDOCS_TRY(auto const cmakePath, getCmakePath()); |
| 148 | + |
| 149 | + llvm::SmallString<128> tempDir; |
| 150 | + MRDOCS_CHECK(!llvm::sys::fs::createUniqueDirectory("compile_commands", tempDir), "Failed to create temporary directory"); |
| 151 | + |
| 152 | + llvm::SmallString<128> errorPath; |
| 153 | + MRDOCS_CHECK(!llvm::sys::fs::createTemporaryFile("cmake-error", "txt", errorPath), |
| 154 | + "Failed to create temporary file"); |
| 155 | + |
| 156 | + std::optional<llvm::StringRef> const redirects[] = {llvm::StringRef(), llvm::StringRef(), errorPath.str()}; |
| 157 | + std::vector<llvm::StringRef> args = {cmakePath, "-S", projectPath, "-B", tempDir.str(), "-DCMAKE_EXPORT_COMPILE_COMMANDS=ON"}; |
| 158 | + |
| 159 | + auto const additionalArgs = parseCmakeArgs(cmakeArgs.str()); |
| 160 | + |
| 161 | + bool visualStudioFound = false; |
| 162 | + for (size_t i = 0; i < additionalArgs.size(); ++i) |
| 163 | + { |
| 164 | + auto const& arg = additionalArgs[i]; |
| 165 | + if (arg.starts_with("-G")) |
| 166 | + { |
| 167 | + if (arg.size() == 2) |
| 168 | + { |
| 169 | + if (i + 1 < additionalArgs.size()) |
| 170 | + { |
| 171 | + auto const& generatorName = additionalArgs[i + 1]; |
| 172 | + if (generatorName.starts_with("Visual Studio")) |
| 173 | + { |
| 174 | + args.push_back("-GNinja"); |
| 175 | + visualStudioFound = true; |
| 176 | + ++i; |
| 177 | + continue; |
| 178 | + } |
| 179 | + } |
| 180 | + } else { |
| 181 | + if (arg.find("Visual Studio", 2) != std::string::npos) |
| 182 | + { |
| 183 | + args.push_back("-GNinja"); |
| 184 | + visualStudioFound = true; |
| 185 | + continue; |
| 186 | + } |
| 187 | + } |
| 188 | + } |
| 189 | + |
| 190 | + if (arg.starts_with("-D")) |
| 191 | + { |
| 192 | + if (arg.size() == 2) |
| 193 | + { |
| 194 | + if (i + 1 < additionalArgs.size()) |
| 195 | + { |
| 196 | + auto const& optionName = additionalArgs[i + 1]; |
| 197 | + if (optionName.starts_with("CMAKE_EXPORT_COMPILE_COMMANDS")) |
| 198 | + { |
| 199 | + ++i; |
| 200 | + continue; |
| 201 | + } |
| 202 | + } |
| 203 | + } else { |
| 204 | + if (arg.find("CMAKE_EXPORT_COMPILE_COMMANDS", 2) != std::string::npos) |
| 205 | + { |
| 206 | + continue; |
| 207 | + } |
| 208 | + } |
| 209 | + } |
| 210 | + args.push_back(arg); |
| 211 | + } |
| 212 | + |
| 213 | + if ( ! visualStudioFound) |
| 214 | + { |
| 215 | + MRDOCS_TRY(auto const cmakeDefaultGeneratorIsVisualStudio, cmakeDefaultGeneratorIsVisualStudio(cmakePath)); |
| 216 | + if (cmakeDefaultGeneratorIsVisualStudio) |
| 217 | + { |
| 218 | + args.push_back("-GNinja"); |
| 219 | + } |
| 220 | + } |
| 221 | + |
| 222 | + int const result = llvm::sys::ExecuteAndWait(cmakePath, args, std::nullopt, redirects); |
| 223 | + if (result != 0) { |
| 224 | + auto bufferOrError = llvm::MemoryBuffer::getFile(errorPath); |
| 225 | + MRDOCS_CHECK(bufferOrError, "CMake execution failed (no error output available)"); |
| 226 | + return Unexpected(Error("CMake execution failed: \n" + bufferOrError.get()->getBuffer().str())); |
| 227 | + } |
| 228 | + |
| 229 | + llvm::SmallString<128> compileCommandsPath(tempDir); |
| 230 | + llvm::sys::path::append(compileCommandsPath, "compile_commands.json"); |
| 231 | + |
| 232 | + return compileCommandsPath.str().str(); |
| 233 | +} |
| 234 | + |
| 235 | +} // mrdocs |
| 236 | +} // clang |
0 commit comments