|
| 1 | +/** |
| 2 | + * |
| 3 | + * @file ScriptCMake.cpp |
| 4 | + * @author Gaspard Kirira |
| 5 | + * |
| 6 | + * Copyright 2025, 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 | + */ |
1 | 14 | #include <vix/cli/commands/run/detail/ScriptCMake.hpp> |
2 | 15 |
|
3 | 16 | #include <fstream> |
|
6 | 19 |
|
7 | 20 | namespace vix::commands::RunCommand::detail |
8 | 21 | { |
| 22 | + struct ScriptFeatures |
| 23 | + { |
| 24 | + bool usesVix = false; |
| 25 | + bool usesOrm = false; |
| 26 | + bool usesDb = false; |
| 27 | + bool usesMysql = false; |
| 28 | + }; |
| 29 | + |
| 30 | + static ScriptFeatures detect_script_features(const fs::path &cppPath) |
| 31 | + { |
| 32 | + ScriptFeatures f; |
| 33 | + |
| 34 | + std::ifstream ifs(cppPath); |
| 35 | + if (!ifs) |
| 36 | + return f; |
| 37 | + |
| 38 | + std::string line; |
| 39 | + while (std::getline(ifs, line)) |
| 40 | + { |
| 41 | + auto has = [&](const char *s) |
| 42 | + { return line.find(s) != std::string::npos; }; |
| 43 | + |
| 44 | + if (has("vix::") || has("Vix::") || has("#include <vix/") || has("#include \"vix/")) |
| 45 | + f.usesVix = true; |
| 46 | + |
| 47 | + if (has("#include <vix/orm/") || has("vix::orm") || has("using namespace vix::orm")) |
| 48 | + f.usesOrm = true; |
| 49 | + |
| 50 | + if (has("#include <vix/db/") || has("vix::db") || has("using namespace vix::db")) |
| 51 | + f.usesDb = true; |
| 52 | + |
| 53 | + if (has("make_mysql_factory") || has("Engine::MySQL") || has("mysqlcppconn")) |
| 54 | + f.usesMysql = true; |
| 55 | + |
| 56 | + if (!f.usesMysql && (has("tcp://") || has("3306") || has("MySQL"))) |
| 57 | + f.usesMysql = true; |
| 58 | + } |
| 59 | + |
| 60 | + if (f.usesOrm) |
| 61 | + f.usesDb = true; |
| 62 | + |
| 63 | + return f; |
| 64 | + } |
| 65 | + |
9 | 66 | ScriptLinkFlags parse_link_flags(const std::vector<std::string> &flags) |
10 | 67 | { |
11 | 68 | ScriptLinkFlags out; |
@@ -101,18 +158,25 @@ namespace vix::commands::RunCommand::detail |
101 | 158 | s += " set(CMAKE_BUILD_TYPE Debug CACHE STRING \"Build type\" FORCE)\n"; |
102 | 159 | s += "endif()\n\n"; |
103 | 160 |
|
| 161 | + auto feat = detect_script_features(cppPath); |
| 162 | + |
104 | 163 | // Options |
105 | 164 | s += "option(VIX_ENABLE_SANITIZERS \"Enable sanitizers (dev only)\" OFF)\n"; |
106 | 165 | s += "set(VIX_SANITIZER_MODE \"asan_ubsan\" CACHE STRING \"Sanitizer mode: asan_ubsan or ubsan\")\n"; |
107 | 166 | s += "set_property(CACHE VIX_SANITIZER_MODE PROPERTY STRINGS asan_ubsan ubsan)\n"; |
108 | 167 | s += "option(VIX_ENABLE_LIBCXX_ASSERTS \"Enable libstdc++ debug mode (_GLIBCXX_ASSERTIONS/_GLIBCXX_DEBUG)\" OFF)\n"; |
109 | 168 | s += "option(VIX_ENABLE_HARDENING \"Enable extra hardening flags (non-MSVC)\" OFF)\n"; |
110 | | - s += "option(VIX_USE_ORM \"Enable Vix ORM (requires vix::orm in install)\" ON)\n\n"; |
| 169 | + s += std::string("option(VIX_USE_ORM \"Enable Vix ORM (requires vix::orm)\" ") + (feat.usesOrm ? "ON" : "OFF") + ")\n"; |
111 | 170 |
|
112 | 171 | // Executable |
113 | 172 | s += "add_executable(" + exeName + " " + q(cppPath.string()) + ")\n\n"; |
114 | 173 | auto lf = parse_link_flags(scriptFlags); |
115 | 174 |
|
| 175 | + auto hasLib = [&](const std::string &name) -> bool |
| 176 | + { |
| 177 | + return std::find(lf.libs.begin(), lf.libs.end(), name) != lf.libs.end(); |
| 178 | + }; |
| 179 | + |
116 | 180 | if (!lf.libDirs.empty()) |
117 | 181 | { |
118 | 182 | s += "target_link_directories(" + exeName + " PRIVATE\n"; |
@@ -219,6 +283,20 @@ namespace vix::commands::RunCommand::detail |
219 | 283 | s += " message(FATAL_ERROR \"VIX_USE_ORM=ON but DB target is not available (vix::db/Vix::db)\")\n"; |
220 | 284 | s += " endif()\n"; |
221 | 285 | s += "endif()\n\n"; |
| 286 | + |
| 287 | + // Auto-link MySQL connector only when needed and not already provided |
| 288 | + if (feat.usesMysql && !hasLib("mysqlcppconn8") && !hasLib("mysqlcppconn")) |
| 289 | + { |
| 290 | + s += "# Auto-link MySQL connector when script uses MySQL\n"; |
| 291 | + s += "if (UNIX)\n"; |
| 292 | + s += " find_library(VIX_MYSQLCPPCONN_LIB NAMES mysqlcppconn8 mysqlcppconn)\n"; |
| 293 | + s += " if (VIX_MYSQLCPPCONN_LIB)\n"; |
| 294 | + s += " target_link_libraries(" + exeName + " PRIVATE ${VIX_MYSQLCPPCONN_LIB})\n"; |
| 295 | + s += " else()\n"; |
| 296 | + s += " message(WARNING \"MySQL connector lib not found (mysqlcppconn8/mysqlcppconn). If you get undefined references, pass: -- -lmysqlcppconn8\")\n"; |
| 297 | + s += " endif()\n"; |
| 298 | + s += "endif()\n\n"; |
| 299 | + } |
222 | 300 | } |
223 | 301 |
|
224 | 302 | // Sanitizers (mode-aware) |
|
0 commit comments