- [NEW] Ara es poden especificar també arxius cpp solts a compilar (a part de directoris sencers, com abans)

This commit is contained in:
2025-05-28 15:53:32 +02:00
parent e33f21166b
commit eebc67e088

159
main.cpp
View File

@@ -15,6 +15,8 @@ std::string build_path = "";
std::vector<std::string> 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");
@@ -169,12 +171,83 @@ std::vector<std::string> split(std::string str)
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<std::chrono::microseconds>(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) std::filesystem::current_path(argv[1]);
loadLagueirtoFile();
bool must_link = false;
std::chrono::steady_clock::time_point begin_all = std::chrono::steady_clock::now();
@@ -189,79 +262,21 @@ int main(int argc, char *argv[])
{
if (!std::filesystem::is_directory(src_path))
{
std::cout << "ERROR: Directory '" << src_path <<"' does not exists." << std::endl;
exit(1);
}
std::string path = "./" + src_path;
for (const auto & entry : std::filesystem::directory_iterator(path))
{
std::string source_file = entry.path();
if (getFileExtension(source_file)=="cpp")
if (std::filesystem::is_regular_file(src_path) && getFileExtension(src_path)=="cpp")
{
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))
MaybeRecompile(src_path);
} else {
std::cout << "ERROR: '" << src_path <<"' does not exists." << std::endl;
exit(1);
}
} else {
std::string path = "./" + src_path;
for (const auto & entry : std::filesystem::directory_iterator(path))
{
std::string source_file = entry.path();
if (getFileExtension(source_file)=="cpp")
{
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::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<std::chrono::microseconds>(end - begin).count())/1000000;
std::cout << "(" << t << " seconds)" << std::endl;
}
else
{
//std::cout << object_file << " està actualitzat" << std::endl;
MaybeRecompile(source_file);
}
}
}