- [NEW] Llevada opció "--clean" per ara

- [NEW] Soport per a varies configuracions
- [NEW] Soport per a configuració per defecte
- [FIX] Arreglats alguns bugs en la detecció de includes
This commit is contained in:
2026-04-01 18:29:23 +02:00
parent 7f018ba2a9
commit 32134907ae

364
main.cpp
View File

@@ -1,5 +1,6 @@
#include <string> #include <string>
#include <iostream> #include <iostream>
#include <fstream>
#include <filesystem> #include <filesystem>
#include <vector> #include <vector>
#include <chrono> #include <chrono>
@@ -65,61 +66,71 @@ char *getBufferFromFile(const char* filename)
return buffer; return buffer;
} }
#define IGNOREWHITESPACE while (*p!=0 && *p <= 32) { p++; } static inline void trim(std::string &s) {
void loadLagueirtoFile() while (!s.empty() && isspace(s.front())) s.erase(s.begin());
while (!s.empty() && isspace(s.back())) s.pop_back();
}
static inline std::string trim_copy(std::string s) {
trim(s);
return s;
}
bool loadLagueirtoFile(const std::string &section_to_load)
{ {
char *buffer = getBufferFromFile("lagueirtofile"); std::ifstream f("lagueirtofile");
char *p = buffer; if (!f) { std::cerr << "Cannot open lagueirtofile\n"; exit(1); }
char token[255];
while (*p!=0) std::string line;
std::string current_section {"@none@"};
bool found_any_section = false;
bool found_default = false;
bool active = false;
while (std::getline(f, line))
{ {
IGNOREWHITESPACE; // Ignore whitespace // Quitar espacios al inicio y final
int i=0; trim(line);
while (*p>32 && *p!='=') { token[i]=*p; i++; p++; }
IGNOREWHITESPACE if (line.empty() || line[0] == '#')
if (*p=='=') continue;
// Detectar sección
if (line.front() == '[')
{ {
token[i]=0; auto end = line.find(']');
int tokennum = -1; if (end == std::string::npos) continue;
for (int j=0;j<keys.size();++j) if (keys[j]==token) tokennum = j;
if (tokennum!=-1) current_section = line.substr(1, end - 1);
{
p++; // Detectar si es default
IGNOREWHITESPACE std::string rest = trim_copy(line.substr(end + 1));
i=0; found_any_section = true;
while (*p!='\r' && *p!='\n' && *p!=0) { token[i]=*p; i++; p++; } if (rest == "default") found_default = true;
token[i]=0; if (section_to_load.empty() && rest == "default") active = true;
switch (tokennum) else active = (current_section == section_to_load);
{
case LIBS: continue;
libs = token;
//std::cout << "LIBS: " << libs << std::endl;
break;
case CPPFLAGS:
cppflags = token;
//std::cout << "CPPFLAGS: " << cppflags << std::endl;
break;
case EXECUTABLE:
executable = token;
//std::cout << "EXECUTABLE: " << executable << std::endl;
break;
case SOURCEPATH:
source_path = token;
//std::cout << "SOURCEPATH: " << source_path << std::endl;
break;
case BUILDPATH:
build_path = token;
//std::cout << "BUILDPATH: " << build_path << std::endl;
break;
case EXCLUDE:
exclude = split(token);
//std::cout << "BUILDPATH: " << build_path << std::endl;
break;
}
}
} }
// Clave = valor
auto pos = line.find('=');
if (pos == std::string::npos)
continue;
std::string key = trim_copy(line.substr(0, pos));
std::string value = trim_copy(line.substr(pos + 1));
if (!active && current_section != "@none@") continue;
if (key == "libs") libs = value;
else if (key == "cppflags") cppflags = value;
else if (key == "executable") executable = value;
else if (key == "sourcepath") source_path = value;
else if (key == "buildpath") build_path = value;
else if (key == "exclude") exclude = split(value);
} }
free(buffer); return !(found_any_section && section_to_load.empty() && !found_default);
} }
std::string getFileExtension(std::string path) std::string getFileExtension(std::string path)
@@ -154,73 +165,64 @@ const bool textFound(char *buffer, const char *text)
} }
std::vector<std::string> getIncludes(std::string filename) std::vector<std::string> getIncludes(const std::string &filename) {
{ std::ifstream f(filename);
char *buffer = getBufferFromFile(filename.c_str()); if (!f) return {};
std::vector<std::string> includes; std::vector<std::string> includes;
std::string line;
char *p = buffer; while (std::getline(f, line)) {
while (*p != 0) // Quitar comentarios de línea
{ auto pos = line.find("//");
// Ignore commented lines if (pos != std::string::npos)
if ( (*p=='/') && (*(p+1)!=0) && (*(p+1)=='/')) while( (*p != 0) && (*p != 13) ) p++; line.resize(pos);
// Ignore comment blocks // Buscar #include
if ( (*p=='/') && (*(p+1)!=0) && (*(p+1)=='*')) { pos = line.find("#include");
p+=2; if (pos == std::string::npos)
while( (*p != 0) && (*p != '*') && (*(p+1)!=0) && (*(p+1)!='/') ) p++; continue;
}
if (*p=='#') // Avanzar tras "#include"
{ pos += 8;
if (textFound(p, "#include"))
{ // Saltar espacios
p+=8; while (pos < line.size() && (line[pos] == ' ' || line[pos] == '\t'))
while(strchr(" \t", *p)) p++; // Ignore whitespace (spaces and tabs) pos++;
if (*p == '"')
{ // Solo queremos includes con comillas
p++; if (pos >= line.size() || line[pos] != '"')
char *p2 = p; continue;
while (*p2!='"' && *p2!=0 ) p2++;
if (*p2!=0) { pos++; // saltar la primera comilla
*p2=0; size_t start = pos;
includes.push_back(std::string(p));
p = p2+1; // Buscar la comilla de cierre
} while (pos < line.size() && line[pos] != '"')
} pos++;
}
} if (pos < line.size())
p++; includes.push_back(line.substr(start, pos - start));
} }
free(buffer);
return includes; return includes;
} }
bool HeadersNeedRecompile(std::string file, std::filesystem::file_time_type object_file_write_time) bool HeadersNeedRecompile(std::string file, std::filesystem::file_time_type object_file_write_time) {
{
auto include_files = getIncludes(file); auto include_files = getIncludes(file);
for (auto include : include_files) for (auto include : include_files) {
{
std::filesystem::path fullpath(file); std::filesystem::path fullpath(file);
auto path_without_filename = fullpath.remove_filename(); auto path_without_filename = fullpath.remove_filename();
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)) if (!std::filesystem::exists(include_file)) {
{
std::cout << "WARNING: Include file '" << include_file << "' not found." << std::endl; std::cout << "WARNING: Include file '" << include_file << "' not found." << 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) {
{
return true; return true;
} } else {
else
{
if (HeadersNeedRecompile(include_file, object_file_write_time)) return true; if (HeadersNeedRecompile(include_file, object_file_write_time)) return true;
} }
} }
@@ -228,39 +230,29 @@ bool HeadersNeedRecompile(std::string file, std::filesystem::file_time_type obje
return false; return false;
} }
bool MustRecompile(std::string source_file) bool MustRecompile(std::string source_file) {
{
std::string object_file = build_path + folder_char + getFileNameWithoutExtension(source_file)+".o"; std::string object_file = build_path + folder_char + getFileNameWithoutExtension(source_file)+".o";
// si el objecte no existeix, fa falta recompilar // si el objecte no existeix, fa falta recompilar
if (!std::filesystem::exists(object_file)) if (!std::filesystem::exists(object_file)) {
{
return true; return true;
} } else {
else
{
// Si sí que existeix, agafem la data de modificació // Si sí que existeix, agafem la data de modificació
auto object_file_write_time = std::filesystem::last_write_time(object_file); auto object_file_write_time = std::filesystem::last_write_time(object_file);
// Si la data de modificació del cpp es major que la del objecte, fa falta recompilar // Si la data de modificació del cpp es major que la del objecte, fa falta recompilar
auto source_file_write_time = std::filesystem::last_write_time(source_file); auto source_file_write_time = std::filesystem::last_write_time(source_file);
if (source_file_write_time > object_file_write_time) if (source_file_write_time > object_file_write_time) {
{
return true; return true;
} } else {
else if (HeadersNeedRecompile(source_file, object_file_write_time)) return true;
{
if (HeadersNeedRecompile(source_file, object_file_write_time))
return true;
} }
} }
return false; return false;
} }
void MaybeRecompile(std::string source_file) void MaybeRecompile(std::string source_file) {
{ if (MustRecompile(source_file)) {
if (MustRecompile(source_file))
{
std::string object_file = build_path + folder_char + getFileNameWithoutExtension(source_file)+".o"; std::string object_file = build_path + folder_char + getFileNameWithoutExtension(source_file)+".o";
must_link = true; must_link = true;
std::string command = "g++ " + source_file + " " + cppflags + " -c -o " + object_file; std::string command = "g++ " + source_file + " " + cppflags + " -c -o " + object_file;
@@ -269,116 +261,94 @@ void MaybeRecompile(std::string source_file)
std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now(); std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now();
system(command.c_str()); system(command.c_str());
std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now(); std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
float t = float(std::chrono::duration_cast<std::chrono::microseconds>(end - begin).count())/1000000; float t = float(std::chrono::duration_cast<std::chrono::microseconds>(end - begin).count())/1000000;
std::cout << "(" << t << " seconds)" << std::endl; std::cout << "(" << t << " seconds)" << std::endl;
} } else {
else
{
//std::cout << object_file << " està actualitzat" << std::endl; //std::cout << object_file << " està actualitzat" << std::endl;
} }
} }
void processCommand(std::string arg) {
// Do nothing for now
}
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
if (argc>1) { std::string configuration_to_use;
for (int i=1; i<argc; ++i) { for (int i = 1; i < argc; ++i)
if (argv[i][0] == '-') { {
if (strcmp(argv[i], "--clean")==0) { std::string arg = argv[i];
system("rm -rf build"); if (!arg.empty() && arg[0] == '-') processCommand(arg);
} else configuration_to_use = arg;
} else {
std::filesystem::current_path(argv[i]);
}
}
} }
loadLagueirtoFile(); if (!loadLagueirtoFile(configuration_to_use)) {
std::cerr << "No default section found.\n";
exit(1);
}
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)) if (!std::filesystem::is_directory(build_path)) std::filesystem::create_directory(build_path);
{ auto source_paths = split(source_path);
std::filesystem::create_directory(build_path);
}
auto source_paths = split(source_path); for (auto src_path : source_paths) {
bool recursive = false;
for (auto src_path : source_paths) if (!src_path.empty() && src_path.back() == '+') {
{ recursive = true;
bool recursive = false; src_path.pop_back();
}
if (!src_path.empty() && src_path.back() == '+') #ifdef _WIN32
{ std::replace(src_path.begin(), src_path.end(), '/', '\\');
recursive = true; #endif
src_path.pop_back();
}
#ifdef _WIN32 if (!std::filesystem::is_directory(src_path)) {
std::replace(src_path.begin(), src_path.end(), '/', '\\'); if (std::filesystem::is_regular_file(src_path)) {
#endif std::string ext = getFileExtension(src_path);
if (!std::filesystem::is_directory(src_path)) if (ext == "cpp" || ext == "c") {
{ MaybeRecompile(src_path);
if (std::filesystem::is_regular_file(src_path)) } else {
{ std::cout << "ERROR: '" << src_path << "' is not a .c/.cpp file." << std::endl;
std::string ext = getFileExtension(src_path); exit(1);
if (ext == "cpp" || ext == "c") }
{ } else {
MaybeRecompile(src_path); std::cout << "ERROR: '" << src_path << "' does not exist." << std::endl;
}
else
{
std::cout << "ERROR: '" << src_path << "' is not a .c/.cpp file." << std::endl;
exit(1); exit(1);
} }
} } else {
else std::string path = "." + folder_char + src_path;
{
std::cout << "ERROR: '" << src_path << "' does not exist." << std::endl;
exit(1);
}
}
else
{
std::string path = "." + folder_char + src_path;
if (recursive) if (recursive) {
{ for (const auto &entry : std::filesystem::recursive_directory_iterator(path)) {
for (const auto &entry : std::filesystem::recursive_directory_iterator(path)) if (!entry.is_regular_file()) continue;
{
if (!entry.is_regular_file()) continue;
std::string source_file = entry.path().string(); std::string source_file = entry.path().string();
std::string ext = getFileExtension(source_file); std::string ext = getFileExtension(source_file);
if ((ext == "cpp" || ext == "c") && if ((ext == "cpp" || ext == "c") && !contains(exclude, entry.path().filename())) {
!contains(exclude, entry.path().filename())) MaybeRecompile(source_file);
{ }
MaybeRecompile(source_file);
} }
} } else {
} for (const auto &entry : std::filesystem::directory_iterator(path)) {
else if (!entry.is_regular_file()) continue;
{
for (const auto &entry : std::filesystem::directory_iterator(path))
{
if (!entry.is_regular_file()) continue;
std::string source_file = entry.path().string(); std::string source_file = entry.path().string();
std::string ext = getFileExtension(source_file); std::string ext = getFileExtension(source_file);
if ((ext == "cpp" || ext == "c") && if ((ext == "cpp" || ext == "c") && !contains(exclude, entry.path().filename())) {
!contains(exclude, entry.path().filename())) MaybeRecompile(source_file);
{ }
MaybeRecompile(source_file);
} }
} }
} }
} }
}
if (must_link) if (must_link) {
{
std::string command = "g++ " + build_path + folder_char + "*.o " + libs + " -o " + executable; std::string command = "g++ " + build_path + folder_char + "*.o " + libs + " -o " + executable;
std::cout << command << std::endl; std::cout << command << std::endl;
if (system(command.c_str()) != 0) { if (system(command.c_str()) != 0) {
@@ -388,9 +358,7 @@ for (auto src_path : source_paths)
std::chrono::steady_clock::time_point end_all = std::chrono::steady_clock::now(); std::chrono::steady_clock::time_point end_all = std::chrono::steady_clock::now();
float t = float(std::chrono::duration_cast<std::chrono::microseconds>(end_all - begin_all).count())/1000000; float t = float(std::chrono::duration_cast<std::chrono::microseconds>(end_all - begin_all).count())/1000000;
std::cout << "(" << t << " seconds)" << std::endl; std::cout << "(" << t << " seconds)" << std::endl;
} } else {
else
{
std::cout << "Everything is up to date. Nothing to do." << std::endl; std::cout << "Everything is up to date. Nothing to do." << std::endl;
} }
} }