diff --git a/CMakeLists.txt b/CMakeLists.txt index c6b66e8..d9375f7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required (VERSION 3.2) project(compilescript - VERSION 0.1.0 + VERSION 0.2.0 LANGUAGES CXX ) diff --git a/README.md b/README.md index b97285b..1a3d9ba 100644 --- a/README.md +++ b/README.md @@ -2,9 +2,8 @@ scripts. By default it uses g++. It compiles the source file, stores the binary in -`${XDG_CACHE_HOME}/compilescript/` and executes it. - -It does compile the file every time at the moment. +`${XDG_CACHE_HOME}/compilescript/` and executes it. If the binary in cache is +newer than the source file, the compilation is skipped. ## Usage diff --git a/src/main.cpp b/src/main.cpp index 56cc080..7d4c2a8 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -87,14 +87,21 @@ int main(int argc, char *argv[]) { read_settings(); - if (argc > 1) + if (argc <= 1) { - const fs::path path(argv[1]); - const fs::path binary = cache_dir / path.stem(); - const fs::path source = binary.string() + ".cpp"; - string compiler_arguments; + cerr << "usage: " << argv[0] << " file [arguments]\n"; + return 1; + } - 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()) { std::ofstream out(source); @@ -127,19 +134,15 @@ int main(int argc, char *argv[]) } else { - cerr << "ERROR: Could not open file: " << path << endl; + cerr << "ERROR: Could not open file: " << original << endl; std::exit(1); } std::system((compiler + " " + source.string() + " " + compiler_arguments + " -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; }