- [FIX] Ignorar els #include en comentaris de bloc

- [FIX] Ara troba includes que se li atragantaven
This commit is contained in:
2026-04-27 14:03:07 +02:00
parent bcaa0849b8
commit 598a6ad6ab

View File

@@ -12,6 +12,7 @@ std::string libs = "";
std::string cppflags = ""; std::string cppflags = "";
std::string executable = "out"; std::string executable = "out";
std::string source_path = ""; std::string source_path = "";
std::vector<std::string> source_paths;
std::string build_path = ""; std::string build_path = "";
#ifdef _WIN32 #ifdef _WIN32
std::string folder_char = "\\"; std::string folder_char = "\\";
@@ -164,45 +165,70 @@ const bool textFound(char *buffer, const char *text)
return equal; return equal;
} }
std::vector<std::string> getIncludes(const std::string &filename) { std::vector<std::string> getIncludes(const std::string &filename) {
std::ifstream f(filename); std::ifstream f(filename);
if (!f) return {}; if (!f) return {};
std::vector<std::string> includes; std::vector<std::string> includes;
std::string line; std::string line;
bool inBlockComment = false;
while (std::getline(f, line)) { while (std::getline(f, line)) {
// Quitar comentarios de línea std::string processed;
auto pos = line.find("//"); processed.reserve(line.size());
if (pos != std::string::npos)
line.resize(pos);
// Buscar #include for (size_t i = 0; i < line.size(); ++i) {
pos = line.find("#include"); // Detectar inicio de comentario de bloque
if (pos == std::string::npos) if (!inBlockComment && i + 1 < line.size() && line[i] == '/' && line[i+1] == '*') {
inBlockComment = true;
i++; // saltar '*'
continue;
}
// Detectar fin de comentario de bloque
if (inBlockComment && i + 1 < line.size() && line[i] == '*' && line[i+1] == '/') {
inBlockComment = false;
i++; // saltar '/'
continue;
}
// Si estamos dentro de un bloque, ignorar todo
if (inBlockComment)
continue; continue;
// Avanzar tras "#include" // Detectar comentario de línea
if (i + 1 < line.size() && line[i] == '/' && line[i+1] == '/') {
break; // ignorar el resto de la línea
}
processed.push_back(line[i]);
}
// Si la línea quedó vacía tras limpiar comentarios, saltar
if (processed.find("#include") == std::string::npos)
continue;
// Buscar #include
size_t pos = processed.find("#include");
pos += 8; pos += 8;
// Saltar espacios // Saltar espacios
while (pos < line.size() && (line[pos] == ' ' || line[pos] == '\t')) while (pos < processed.size() && (processed[pos] == ' ' || processed[pos] == '\t'))
pos++; pos++;
// Solo queremos includes con comillas // Solo includes con comillas
if (pos >= line.size() || line[pos] != '"') if (pos >= processed.size() || processed[pos] != '"')
continue; continue;
pos++; // saltar la primera comilla pos++; // saltar comilla inicial
size_t start = pos; size_t start = pos;
// Buscar la comilla de cierre // Buscar comilla final
while (pos < line.size() && line[pos] != '"') while (pos < processed.size() && processed[pos] != '"')
pos++; pos++;
if (pos < line.size()) if (pos < processed.size())
includes.push_back(line.substr(start, pos - start)); includes.push_back(processed.substr(start, pos - start));
} }
return includes; return includes;
@@ -216,8 +242,18 @@ bool HeadersNeedRecompile(std::string file, std::filesystem::file_time_type obje
std::string src_path = path_without_filename.string(); std::string src_path = path_without_filename.string();
std::string include_file = src_path + include; std::string include_file = src_path + include;
if (!std::filesystem::exists(include_file)) { bool found = std::filesystem::exists(include_file);
std::cout << "WARNING: Include file '" << include_file << "' not found." << std::endl; if (!found) {
for (auto path : source_paths) {
include_file = path + "/" + include;
if (std::filesystem::exists(include_file)) {
found = true;
break;
}
}
}
if (!found) {
std::cout << "WARNING: Include file '" << include << "' not found in '" << file << "'." << std::endl;
} else { } else {
auto include_file_write_time = std::filesystem::last_write_time(include_file); auto include_file_write_time = std::filesystem::last_write_time(include_file);
if (include_file_write_time > object_file_write_time) { if (include_file_write_time > object_file_write_time) {
@@ -296,9 +332,9 @@ int main(int argc, char *argv[])
std::chrono::steady_clock::time_point begin_all = std::chrono::steady_clock::now(); std::chrono::steady_clock::time_point begin_all = std::chrono::steady_clock::now();
if (!std::filesystem::is_directory(build_path)) std::filesystem::create_directory(build_path); if (!std::filesystem::is_directory(build_path)) std::filesystem::create_directory(build_path);
auto source_paths = split(source_path); source_paths = split(source_path);
for (auto src_path : source_paths) { for (auto &src_path : source_paths) {
bool recursive = false; bool recursive = false;
if (!src_path.empty() && src_path.back() == '+') { if (!src_path.empty() && src_path.back() == '+') {