|
1 | 1 | /** |
2 | 2 | * |
3 | | - * @file CMakeBuildErrors.hpp |
| 3 | + * @file CMakeBuildErrors.cpp |
4 | 4 | * @author Gaspard Kirira |
5 | 5 | * |
6 | 6 | * Copyright 2025, Gaspard Kirira. All rights reserved. |
|
12 | 12 | * |
13 | 13 | */ |
14 | 14 | #include <vix/cli/errors/build/CMakeBuildErrors.hpp> |
15 | | - |
16 | 15 | #include <iostream> |
| 16 | +#include <regex> |
17 | 17 | #include <string> |
18 | | - |
| 18 | +#include <string_view> |
| 19 | +#include <iostream> |
19 | 20 | #include <vix/cli/Style.hpp> |
20 | 21 |
|
21 | 22 | using namespace vix::cli::style; |
22 | 23 |
|
23 | 24 | namespace vix::cli::errors::build |
24 | 25 | { |
25 | | - bool handleCMakeBuildError(std::string_view log) |
| 26 | + namespace |
26 | 27 | { |
27 | | - const bool cacheDirMismatch = |
28 | | - log.find("The current CMakeCache.txt directory") != std::string_view::npos; |
| 28 | + bool handle_cache_mismatch(std::string_view log) |
| 29 | + { |
| 30 | + const bool cacheDirMismatch = |
| 31 | + log.find("The current CMakeCache.txt directory") != std::string_view::npos; |
| 32 | + const bool sourceMismatch = |
| 33 | + log.find("does not match the source") != std::string_view::npos && |
| 34 | + log.find("used to generate cache") != std::string_view::npos; |
| 35 | + |
| 36 | + if (!cacheDirMismatch && !sourceMismatch) |
| 37 | + return false; |
| 38 | + |
| 39 | + error("CMake configure failed: stale build cache detected."); |
| 40 | + hint("Your build directory was generated from another project location."); |
| 41 | + hint("Remove the old build directory and reconfigure."); |
| 42 | + hint("Recommended: vix build --clean"); |
| 43 | + hint("Manual fix: rm -rf build-ninja build-dev build-release"); |
| 44 | + return true; |
| 45 | + } |
| 46 | + |
| 47 | + bool handle_missing_link_target(std::string_view log) |
| 48 | + { |
| 49 | + const bool hasMissingTarget = |
| 50 | + log.find("but the target was not found") != std::string_view::npos && |
| 51 | + log.find("target_link_libraries") != std::string_view::npos; |
| 52 | + |
| 53 | + if (!hasMissingTarget) |
| 54 | + return false; |
| 55 | + |
| 56 | + std::string targetName; |
| 57 | + std::string missingLink; |
| 58 | + |
| 59 | + { |
| 60 | + const std::regex reTarget(R"re(Target\s+"([^"]+)")re"); |
| 61 | + std::match_results<std::string_view::const_iterator> m; |
29 | 62 |
|
30 | | - const bool sourceMismatch = |
31 | | - log.find("does not match the source") != std::string_view::npos && |
32 | | - log.find("used to generate cache") != std::string_view::npos; |
| 63 | + if (std::regex_search(log.begin(), log.end(), m, reTarget) && m.size() >= 2) |
| 64 | + targetName = m[1].str(); |
| 65 | + } |
33 | 66 |
|
34 | | - if (!cacheDirMismatch && !sourceMismatch) |
35 | | - return false; |
| 67 | + { |
| 68 | + const std::regex reMissing(R"re(links\s+to:\s*\n\s*\n\s*([^\s][^\n]*))re"); |
| 69 | + std::match_results<std::string_view::const_iterator> m; |
36 | 70 |
|
37 | | - error("CMake configure failed: stale build cache detected."); |
38 | | - hint("Your build directory was generated from another project location."); |
39 | | - hint("Remove the old build directory and reconfigure."); |
40 | | - hint("Recommended: vix build --clean"); |
41 | | - hint("Manual fix: rm -rf build-ninja build-dev build-release"); |
| 71 | + if (std::regex_search(log.begin(), log.end(), m, reMissing) && m.size() >= 2) |
| 72 | + missingLink = m[1].str(); |
| 73 | + } |
42 | 74 |
|
43 | | - return true; |
| 75 | + error("Build failed: unresolved CMake target."); |
| 76 | + |
| 77 | + if (!targetName.empty()) |
| 78 | + std::cerr << " target: " << targetName << "\n"; |
| 79 | + |
| 80 | + if (!missingLink.empty()) |
| 81 | + std::cerr << " missing: " << RED << missingLink << RESET << "\n"; |
| 82 | + |
| 83 | + std::cerr << "\n"; |
| 84 | + hint("Fix the target name or make sure it is defined before linking."); |
| 85 | + |
| 86 | + return true; |
| 87 | + } |
| 88 | + |
| 89 | + } // namespace |
| 90 | + |
| 91 | + bool handleCMakeBuildError(std::string_view log) |
| 92 | + { |
| 93 | + if (handle_cache_mismatch(log)) |
| 94 | + return true; |
| 95 | + if (handle_missing_link_target(log)) |
| 96 | + return true; |
| 97 | + return false; |
44 | 98 | } |
45 | | -} |
| 99 | + |
| 100 | +} // namespace vix::cli::errors::build |
0 commit comments