- First commit
This commit is contained in:
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
lagueirto
|
||||
.vscode/*
|
||||
5
lagueirtofile
Normal file
5
lagueirtofile
Normal file
@@ -0,0 +1,5 @@
|
||||
libs = -lSDL2 -lSDL2_mixer
|
||||
cppflags = -D DEBUG -g
|
||||
executable = thepool_debug
|
||||
sourcepath = source
|
||||
buildpath = build
|
||||
252
main.cpp
Normal file
252
main.cpp
Normal file
@@ -0,0 +1,252 @@
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include <filesystem>
|
||||
#include <vector>
|
||||
#include <chrono>
|
||||
|
||||
#include <string.h>
|
||||
|
||||
std::string libs = "";
|
||||
std::string cppflags = "";
|
||||
std::string executable = "out";
|
||||
std::string source_path = "";
|
||||
std::string build_path = "";
|
||||
|
||||
std::vector<std::string> keys = {"libs", "cppflags", "executable", "sourcepath", "buildpath"};
|
||||
enum tokens {LIBS, CPPFLAGS, EXECUTABLE, SOURCEPATH, BUILDPATH};
|
||||
|
||||
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<keys.size();++j) if (keys[j]==token) tokennum = j;
|
||||
if (tokennum!=-1)
|
||||
{
|
||||
p++;
|
||||
IGNOREWHITESPACE
|
||||
i=0;
|
||||
while (*p!='\r' && *p!='\n' && *p!=0) { token[i]=*p; i++; p++; }
|
||||
token[i]=0;
|
||||
switch (tokennum)
|
||||
{
|
||||
case LIBS:
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
free(buffer);
|
||||
}
|
||||
|
||||
std::string getFileExtension(std::string path)
|
||||
{
|
||||
std::size_t dotpos = path.find_last_of(".");
|
||||
return dotpos <= 0 ? "" : path.substr(dotpos + 1);
|
||||
}
|
||||
|
||||
std::string getFileNameWithoutExtension(std::string path)
|
||||
{
|
||||
std::size_t slashpos = path.find_last_of("/");
|
||||
std::string filename = path.substr(slashpos+1);
|
||||
|
||||
std::size_t dotpos = filename.find_last_of(".");
|
||||
if (dotpos <= 0) return filename;
|
||||
|
||||
return filename.substr(0, dotpos);
|
||||
}
|
||||
|
||||
|
||||
const bool textFound(char *buffer, const char *text)
|
||||
{
|
||||
const int strsize = strlen(text);
|
||||
int i = 0;
|
||||
bool equal=true;
|
||||
while (equal && i<strsize)
|
||||
{
|
||||
if (buffer[i]!=text[i]) equal = false;
|
||||
++i;
|
||||
}
|
||||
return equal;
|
||||
}
|
||||
|
||||
|
||||
std::vector<std::string> getIncludes(std::string filename)
|
||||
{
|
||||
char *buffer = getBufferFromFile(filename.c_str());
|
||||
|
||||
std::vector<std::string> 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;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
loadLagueirtoFile();
|
||||
bool must_link = false;
|
||||
if (!std::filesystem::is_directory(source_path))
|
||||
{
|
||||
std::cout << "ERROR: Directory '" << source_path <<"' does not exists." << std::endl;
|
||||
exit(1);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
std::string path = "./" + source_path;
|
||||
for (const auto & entry : std::filesystem::directory_iterator(path))
|
||||
{
|
||||
std::string source_file = entry.path();
|
||||
if (getFileExtension(source_file)=="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))
|
||||
{
|
||||
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 = source_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;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (must_link)
|
||||
{
|
||||
std::string command = "g++ " + build_path + "/*.o " + libs + " -o " + executable;
|
||||
std::cout << command << std::endl;
|
||||
system(command.c_str());
|
||||
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;
|
||||
std::cout << "(" << t << " seconds)" << std::endl;
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cout << "Everything is up to date. Nothing to do." << std::endl;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user