#include #include #include #include #include #include std::string libs = ""; std::string cppflags = ""; std::string executable = "out"; std::string source_path = ""; std::string build_path = ""; std::vector keys = {"libs", "cppflags", "executable", "sourcepath", "buildpath"}; enum tokens {LIBS, CPPFLAGS, EXECUTABLE, SOURCEPATH, BUILDPATH}; bool must_link = false; char *getBufferFromFile(const char* filename) { FILE *f = fopen(filename, "rb"); fseek(f, 0, SEEK_END); long size = ftell(f); fseek(f, 0, SEEK_SET); char *buffer = (char*)malloc(size+1); fread(buffer, size, 1, f); buffer[size] = 0; return buffer; } #define IGNOREWHITESPACE while (*p!=0 && *p <= 32) { p++; } void loadLagueirtoFile() { char *buffer = getBufferFromFile("lagueirtofile"); char *p = buffer; char token[255]; while (*p!=0) { IGNOREWHITESPACE; // Ignore whitespace int i=0; while (*p>32 && *p!='=') { token[i]=*p; i++; p++; } IGNOREWHITESPACE if (*p=='=') { token[i]=0; int tokennum = -1; for (int j=0;j getIncludes(std::string filename) { char *buffer = getBufferFromFile(filename.c_str()); std::vector includes; char *p = buffer; while (*p != 0) { if (*p=='#') { if (textFound(buffer, "#include")) { p+=8; while(strchr(" \t", *p)) p++; // Ignore whitespace (spaces and tabs) if (*p == '"') { p++; char *p2 = p; while (*p2!='"' && *p2!=0 ) p2++; if (*p2!=0) { *p2=0; includes.push_back(std::string(p)); p = p2+1; } } } } p++; } free(buffer); return includes; } std::vector split(std::string str) { std::vector strings; char tmp[100]; int tmp_p = 0, str_p = 0; while (str[str_p]!=0) { if (str[str_p]!=32) tmp[tmp_p++] = str[str_p++]; else { tmp[tmp_p]=0; strings.push_back(tmp); tmp_p=0; while (str[str_p]==32) str_p++; } } tmp[tmp_p]=0; strings.push_back(tmp); return strings; } void MaybeRecompile(std::string source_file) { bool must_recompile = false; std::string object_file = build_path + "/" + getFileNameWithoutExtension(source_file)+".o"; // si el objecte no existeix, fa falta recompilar if (!std::filesystem::exists(object_file)) { must_recompile = true; //std::cout << "'" << object_file << "' No existeix. "; } else { // Si sí que existeix, agafem la data de modificació 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 auto source_file_write_time = std::filesystem::last_write_time(source_file); if (source_file_write_time > object_file_write_time) { //std::cout << "'" << source_file << "' es més nou. "; must_recompile = true; } else { // Sino, mirem els includes auto include_files = getIncludes(source_file); for (auto include : include_files) { std::filesystem::path fullpath(source_file); auto path_without_filename = fullpath.remove_filename(); std::string src_path = path_without_filename.string(); std::string include_file = src_path + "/" + include; if (!std::filesystem::exists(include_file)) { std::cout << "WARNING: Include file '" << include_file << "' not found." << std::endl; } else { auto include_file_write_time = std::filesystem::last_write_time(include_file); if (include_file_write_time > object_file_write_time) { //std::cout << "El include '" << include_file << "' es més nou. "; must_recompile = true; break; } } } } } if (must_recompile) { must_link = true; //std::cout << object_file << " s'ha de reompilar" << std::endl; std::string command = "g++ " + source_file + " " + cppflags + " -c -o " + object_file; std::cout << command << std::endl; std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now(); system(command.c_str()); std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now(); float t = float(std::chrono::duration_cast(end - begin).count())/1000000; std::cout << "(" << t << " seconds)" << std::endl; } else { //std::cout << object_file << " està actualitzat" << std::endl; } } int main(int argc, char *argv[]) { if (argc>1) { for (int i=1; i(end_all - begin_all).count())/1000000; std::cout << "(" << t << " seconds)" << std::endl; } else { std::cout << "Everything is up to date. Nothing to do." << std::endl; } }