fix bug when a compile failed

This commit is contained in:
vrd
2024-04-12 13:12:13 +03:00
parent d88212f6e7
commit 6dbe37e07b
+21 -20
View File
@@ -31,14 +31,20 @@ static fs::path get_cache_dir_path(const fs::path& src_file);
// map file in memory with write persmissions // map file in memory with write persmissions
static MappedFile mapfile_for_writing(const fs::path& file); static MappedFile mapfile_for_writing(const fs::path& file);
// check weather a src file or it's headers have changed // preprocess the src file and compare the result to the previous version, return weather it's changed
static bool is_src_file_changed(const fs::path& src_file, const fs::path& cache_dir); // remove cached bin if true
static bool preprocess_and_compare();
// returns bin path // only recompile if changes are present
static fs::path compile_file(const fs::path& src_file, const fs::path& cache_dir); static void compile_src_file();
static const char DEBUG_PREFIX[] = "__DBG"; static const char DEBUG_PREFIX[] = "__DBG";
// Context
static bool debug = false; static bool debug = false;
static fs::path src_file;
static fs::path cache_dir;
static fs::path bin; // cache bins to avoid recompiles
int main(int argc, char* argv[]) int main(int argc, char* argv[])
{ {
@@ -71,12 +77,13 @@ int main(int argc, char* argv[])
file_arg = 2; file_arg = 2;
} }
const fs::path src_file = find_path_to_cpp( argv[file_arg] ); // Init context
src_file = find_path_to_cpp( argv[file_arg] );
fs::path cache_dir( get_cache_dir_path(src_file) ); // cache bins to avoid recompiles cache_dir = get_cache_dir_path(src_file);
bin = cache_dir / (debug ? DEBUG_PREFIX : "") += src_file.filename();
// Compile the src // Compile the src
fs::path bin = compile_file(src_file, cache_dir); compile_src_file();
// Run the src file without forking // Run the src file without forking
Cmd run; Cmd run;
@@ -161,8 +168,7 @@ static MappedFile mapfile_for_writing(const fs::path& file)
return res; return res;
} }
// preprocess the file and comapre it to the old pped version static bool preprocess_and_compare()
static bool is_src_file_changed(const fs::path& src_file, const fs::path& cache_dir)
{ {
Cmd preprocess( Cmd preprocess(
"g++", "g++",
@@ -197,35 +203,31 @@ static bool is_src_file_changed(const fs::path& src_file, const fs::path& cache_
} }
else else
{ {
fs::remove(bin); // rm old bin
ofstream pp_file(old_pp_path); ofstream pp_file(old_pp_path);
pp_file << new_pp; pp_file << new_pp;
return true; return true;
} }
} }
else else
{ {
fs::remove(bin); // rm old bin
ofstream pp_file(old_pp_path); ofstream pp_file(old_pp_path);
pp_file << new_pp; pp_file << new_pp;
return true; return true;
} }
return true;
} }
static fs::path compile_file(const fs::path& src_file, const fs::path& cache_dir) static void compile_src_file()
{ {
// Only compile if the source is newer then the bin // Only compile if the source is newer then the bin
fs::path cache_bin = cache_dir / (debug ? DEBUG_PREFIX : "") += src_file.filename(); if(preprocess_and_compare() || !fs::exists(bin))
if(is_src_file_changed(src_file, cache_dir) || !fs::exists(cache_bin))
{ {
string preprocessed_file = cache_dir / src_file.stem() += ".ii"; // todo: duplicates with old_pp_path // todo: support C string preprocessed_file = cache_dir / src_file.stem() += ".ii"; // todo: duplicates with old_pp_path // todo: support C
Cmd compile( Cmd compile(
"g++", "g++",
preprocessed_file.c_str(), preprocessed_file.c_str(),
"-o", cache_bin.c_str(), "-o", bin.c_str(),
"-pipe", "-std=c++17", "-march=native", "-pipe", "-std=c++17", "-march=native",
"-Wall", "-Wextra", "-Wno-parentheses", "-Wall", "-Wextra", "-Wno-parentheses",
"-lcppipe" "-lcppipe"
@@ -248,5 +250,4 @@ static fs::path compile_file(const fs::path& src_file, const fs::path& cache_dir
if( !compile() ) // if failed to compile if( !compile() ) // if failed to compile
exit(1); exit(1);
} }
return cache_bin;
} }