Skip compilation if binary is newer than source file.

This commit is contained in:
tastytea 2018-12-29 08:11:36 +01:00
parent b74db00ff3
commit db4d1fcd4b
Signed by: tastytea
GPG Key ID: CFC39497F1B26E07
3 changed files with 19 additions and 17 deletions

View File

@ -1,6 +1,6 @@
cmake_minimum_required (VERSION 3.2) cmake_minimum_required (VERSION 3.2)
project(compilescript project(compilescript
VERSION 0.1.0 VERSION 0.2.0
LANGUAGES CXX LANGUAGES CXX
) )

View File

@ -2,9 +2,8 @@
scripts. By default it uses g++. scripts. By default it uses g++.
It compiles the source file, stores the binary in It compiles the source file, stores the binary in
`${XDG_CACHE_HOME}/compilescript/` and executes it. `${XDG_CACHE_HOME}/compilescript/` and executes it. If the binary in cache is
newer than the source file, the compilation is skipped.
It does compile the file every time at the moment.
## Usage ## Usage

View File

@ -87,14 +87,21 @@ int main(int argc, char *argv[])
{ {
read_settings(); read_settings();
if (argc > 1) if (argc <= 1)
{ {
const fs::path path(argv[1]); cerr << "usage: " << argv[0] << " file [arguments]\n";
const fs::path binary = cache_dir / path.stem(); return 1;
const fs::path source = binary.string() + ".cpp"; }
string compiler_arguments;
std::ifstream in(path); const fs::path original(argv[1]);
const fs::path binary = cache_dir / original.stem();
const fs::path source = binary.string() + ".cpp";
string compiler_arguments;
if (!fs::exists(binary) ||
fs::last_write_time(original) > fs::last_write_time(binary))
{
std::ifstream in(original);
if (in.is_open()) if (in.is_open())
{ {
std::ofstream out(source); std::ofstream out(source);
@ -127,19 +134,15 @@ int main(int argc, char *argv[])
} }
else else
{ {
cerr << "ERROR: Could not open file: " << path << endl; cerr << "ERROR: Could not open file: " << original << endl;
std::exit(1); std::exit(1);
} }
std::system((compiler + " " + source.string() + " " + compiler_arguments std::system((compiler + " " + source.string() + " " + compiler_arguments
+ " -o " + binary.string()).c_str()); + " -o " + binary.string()).c_str());
execv(binary.c_str(), &argv[1]);
}
else
{
cerr << "usage: " << argv[0] << " file [arguments]\n";
std::exit(1);
} }
execv(binary.c_str(), &argv[1]);
return 0; return 0;
} }