|
| 1 | +/** |
| 2 | + * |
| 3 | + * @file CompileCommands.hpp |
| 4 | + * @author Gaspard Kirira |
| 5 | + * |
| 6 | + * Copyright 2026, Gaspard Kirira. All rights reserved. |
| 7 | + * https://github.com/vixcpp/vix |
| 8 | + * Use of this source code is governed by a MIT license |
| 9 | + * that can be found in the License file. |
| 10 | + * |
| 11 | + * Vix.cpp |
| 12 | + * |
| 13 | + * compile_commands.json parser |
| 14 | + * |
| 15 | + */ |
| 16 | + |
| 17 | +#ifndef VIX_CLI_BUILD_COMPILE_COMMANDS_HPP |
| 18 | +#define VIX_CLI_BUILD_COMPILE_COMMANDS_HPP |
| 19 | + |
| 20 | +#include <filesystem> |
| 21 | +#include <optional> |
| 22 | +#include <string> |
| 23 | +#include <vector> |
| 24 | + |
| 25 | +namespace vix::cli::build |
| 26 | +{ |
| 27 | + namespace fs = std::filesystem; |
| 28 | + |
| 29 | + /** |
| 30 | + * @brief One entry from compile_commands.json. |
| 31 | + * |
| 32 | + * CMake/Ninja can generate compile_commands.json with the exact compiler |
| 33 | + * command used for every translation unit. Vix imports this data to build |
| 34 | + * graph tasks without guessing include paths, defines, flags, object paths, |
| 35 | + * or compiler arguments. |
| 36 | + */ |
| 37 | + struct CompileCommandEntry |
| 38 | + { |
| 39 | + fs::path directory; ///< Working directory used by the compiler |
| 40 | + fs::path source; ///< Source file being compiled |
| 41 | + fs::path output; ///< Object file when available |
| 42 | + std::vector<std::string> arguments; ///< Compiler argv |
| 43 | + std::string rawCommand; ///< Raw command string when provided |
| 44 | + |
| 45 | + /** |
| 46 | + * @brief Check whether this entry has enough data to create a compile task. |
| 47 | + * |
| 48 | + * @return true when directory, source and arguments are present |
| 49 | + */ |
| 50 | + bool valid() const; |
| 51 | + |
| 52 | + /** |
| 53 | + * @brief Check whether the entry has an object output path. |
| 54 | + * |
| 55 | + * @return true when output is not empty |
| 56 | + */ |
| 57 | + bool has_output() const; |
| 58 | + }; |
| 59 | + |
| 60 | + /** |
| 61 | + * @brief Return the default compile_commands.json path for a build directory. |
| 62 | + * |
| 63 | + * @param buildDir Build directory |
| 64 | + * @return buildDir / compile_commands.json |
| 65 | + */ |
| 66 | + fs::path default_compile_commands_path(const fs::path &buildDir); |
| 67 | + |
| 68 | + /** |
| 69 | + * @brief Split a compiler command string into argv tokens. |
| 70 | + * |
| 71 | + * This supports the shell forms usually emitted by CMake: |
| 72 | + * - whitespace separated tokens |
| 73 | + * - single quotes |
| 74 | + * - double quotes |
| 75 | + * - backslash escaping |
| 76 | + * |
| 77 | + * It does not expand environment variables, globs, or shell substitutions. |
| 78 | + * |
| 79 | + * @param command Raw command string |
| 80 | + * @return Parsed argv tokens |
| 81 | + */ |
| 82 | + std::vector<std::string> split_compile_command(const std::string &command); |
| 83 | + |
| 84 | + /** |
| 85 | + * @brief Parse compile_commands.json content. |
| 86 | + * |
| 87 | + * The parser accepts both formats supported by the compilation database: |
| 88 | + * - "command": "c++ ..." |
| 89 | + * - "arguments": ["c++", "..."] |
| 90 | + * |
| 91 | + * @param text JSON content |
| 92 | + * @param sourcePath Path of the compile_commands.json file |
| 93 | + * @return Parsed entries, or std::nullopt on invalid JSON/schema |
| 94 | + */ |
| 95 | + std::optional<std::vector<CompileCommandEntry>> parse_compile_commands_text( |
| 96 | + const std::string &text, |
| 97 | + const fs::path &sourcePath = {}); |
| 98 | + |
| 99 | + /** |
| 100 | + * @brief Read and parse compile_commands.json from disk. |
| 101 | + * |
| 102 | + * @param path Path to compile_commands.json |
| 103 | + * @return Parsed entries, or std::nullopt when missing/invalid |
| 104 | + */ |
| 105 | + std::optional<std::vector<CompileCommandEntry>> read_compile_commands( |
| 106 | + const fs::path &path); |
| 107 | + |
| 108 | + /** |
| 109 | + * @brief Resolve a possibly relative path against a base directory. |
| 110 | + * |
| 111 | + * @param base Base directory |
| 112 | + * @param path Path to resolve |
| 113 | + * @return Absolute normalized path when possible |
| 114 | + */ |
| 115 | + fs::path resolve_compile_command_path( |
| 116 | + const fs::path &base, |
| 117 | + const fs::path &path); |
| 118 | + |
| 119 | + /** |
| 120 | + * @brief Extract the object output path from compiler argv. |
| 121 | + * |
| 122 | + * Supports: |
| 123 | + * - -o file.o |
| 124 | + * - -ofile.o |
| 125 | + * |
| 126 | + * @param arguments Compiler argv |
| 127 | + * @param workingDirectory Working directory used to resolve relative paths |
| 128 | + * @return Object path, or empty path if not found |
| 129 | + */ |
| 130 | + fs::path extract_compile_output_path( |
| 131 | + const std::vector<std::string> &arguments, |
| 132 | + const fs::path &workingDirectory); |
| 133 | + |
| 134 | +} // namespace vix::cli::build |
| 135 | + |
| 136 | +#endif |
0 commit comments