2018-12-29 06:08:33 +01:00
|
|
|
/* This file is part of compilescript.
|
2019-03-05 22:29:36 +01:00
|
|
|
* Copyright © 2018, 2019 tastytea <tastytea@tastytea.de>
|
2018-12-29 05:58:15 +01:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, version 3.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2018-12-29 05:13:58 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#if __cplusplus >= 201703L
|
|
|
|
#include <filesystem>
|
|
|
|
#else
|
|
|
|
#include <experimental/filesystem>
|
|
|
|
#endif
|
|
|
|
#include <iostream>
|
|
|
|
#include <string>
|
|
|
|
#include <fstream>
|
2018-12-30 11:46:29 +01:00
|
|
|
#include <chrono>
|
|
|
|
#include <system_error>
|
2018-12-29 05:13:58 +01:00
|
|
|
#include <cstdlib>
|
|
|
|
#include <unistd.h>
|
2018-12-29 06:19:53 +01:00
|
|
|
#include <libconfig.h++>
|
|
|
|
#include <basedir.h>
|
2018-12-29 05:13:58 +01:00
|
|
|
#include "xdgcfg.hpp"
|
2018-12-28 13:56:34 +01:00
|
|
|
#include "version.hpp"
|
|
|
|
|
2018-12-29 05:13:58 +01:00
|
|
|
#if __cplusplus >= 201703L
|
|
|
|
namespace fs = std::filesystem;
|
|
|
|
#else
|
|
|
|
namespace fs = std::experimental::filesystem;
|
|
|
|
#endif
|
2019-03-05 22:29:36 +01:00
|
|
|
using std::cout;
|
2018-12-29 05:13:58 +01:00
|
|
|
using std::cerr;
|
|
|
|
using std::endl;
|
2018-12-30 11:46:29 +01:00
|
|
|
using std::chrono::system_clock;
|
|
|
|
using std::chrono::hours;
|
2018-12-31 13:58:23 +01:00
|
|
|
using std::chrono::duration_cast;
|
2018-12-29 05:13:58 +01:00
|
|
|
|
2019-02-09 00:20:05 +01:00
|
|
|
string compiler = "g++ -x c++";
|
2018-12-29 05:13:58 +01:00
|
|
|
fs::path cache_dir;
|
2018-12-30 11:46:29 +01:00
|
|
|
int clean_after_hours = 30 * 24;
|
2018-12-29 05:13:58 +01:00
|
|
|
|
2018-12-31 14:00:39 +01:00
|
|
|
bool read_settings()
|
2018-12-29 05:13:58 +01:00
|
|
|
{
|
2018-12-29 05:56:26 +01:00
|
|
|
bool need_save = false;
|
2018-12-29 06:08:33 +01:00
|
|
|
xdgcfg config("compilescript.cfg");
|
2019-02-08 01:42:37 +01:00
|
|
|
config.read();
|
2018-12-29 05:13:58 +01:00
|
|
|
libconfig::Setting &cfg = config.get_cfg().getRoot();
|
|
|
|
|
|
|
|
if (cfg.exists("compiler"))
|
|
|
|
{
|
|
|
|
compiler = cfg["compiler"].c_str();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cfg.add("compiler", libconfig::Setting::TypeString) = compiler;
|
|
|
|
need_save = true;
|
|
|
|
}
|
|
|
|
|
2018-12-30 11:46:29 +01:00
|
|
|
if (cfg.exists("clean_after_hours"))
|
|
|
|
{
|
|
|
|
cfg.lookupValue("clean_after_hours", clean_after_hours);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cfg.add("clean_after_hours",
|
|
|
|
libconfig::Setting::TypeInt) = clean_after_hours;
|
|
|
|
need_save = true;
|
|
|
|
}
|
|
|
|
|
2018-12-29 05:13:58 +01:00
|
|
|
if (cfg.exists("cache_dir"))
|
|
|
|
{
|
|
|
|
cache_dir = cfg["cache_dir"].c_str();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
xdgHandle xdg;
|
|
|
|
xdgInitHandle(&xdg);
|
|
|
|
cache_dir = xdgCacheHome(&xdg);
|
2018-12-29 06:08:33 +01:00
|
|
|
cache_dir /= "compilescript";
|
2018-12-29 05:13:58 +01:00
|
|
|
xdgWipeHandle(&xdg);
|
|
|
|
}
|
|
|
|
if (!fs::is_directory(cache_dir))
|
|
|
|
{
|
|
|
|
fs::create_directories(cache_dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (need_save)
|
|
|
|
{
|
|
|
|
config.write();
|
|
|
|
}
|
2018-12-31 14:00:39 +01:00
|
|
|
|
|
|
|
return true;
|
2018-12-29 05:13:58 +01:00
|
|
|
}
|
|
|
|
|
2018-12-30 11:46:29 +01:00
|
|
|
void cleanup()
|
|
|
|
{
|
|
|
|
for (const fs::directory_entry &entry
|
|
|
|
: fs::recursive_directory_iterator(cache_dir))
|
|
|
|
{
|
|
|
|
if (fs::is_regular_file(entry))
|
|
|
|
{
|
2018-12-31 13:58:23 +01:00
|
|
|
auto diff = system_clock::now() - fs::last_write_time(entry);
|
|
|
|
if (duration_cast<hours>(diff).count() > clean_after_hours)
|
2018-12-30 11:46:29 +01:00
|
|
|
{
|
|
|
|
fs::path current_path = entry.path();
|
|
|
|
std::error_code e;
|
|
|
|
|
|
|
|
while (fs::remove(current_path, e))
|
|
|
|
{
|
|
|
|
current_path = current_path.parent_path();
|
|
|
|
if (current_path == cache_dir)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-28 13:56:34 +01:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2018-12-31 14:00:39 +01:00
|
|
|
if (!read_settings())
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
2018-12-29 05:13:58 +01:00
|
|
|
|
2018-12-29 08:11:36 +01:00
|
|
|
if (argc <= 1)
|
2018-12-29 05:13:58 +01:00
|
|
|
{
|
2019-03-05 22:29:36 +01:00
|
|
|
cerr << "usage: " << argv[0] << " [file|--cleanup|--version] [arguments]\n";
|
2018-12-29 08:11:36 +01:00
|
|
|
return 1;
|
|
|
|
}
|
2018-12-30 11:46:29 +01:00
|
|
|
if (string(argv[1]) == "--cleanup")
|
|
|
|
{
|
|
|
|
cleanup();
|
|
|
|
return 0;
|
|
|
|
}
|
2019-03-05 22:29:36 +01:00
|
|
|
if (string(argv[1]) == "--version")
|
|
|
|
{
|
|
|
|
cout << "compilescript " << global::version << endl <<
|
|
|
|
"Copyright (C) 2018, 2019 tastytea <tastytea@tastytea.de>\n"
|
|
|
|
"License GPLv3: GNU GPL version 3 <https://www.gnu.org/licenses/gpl-3.0.html>.\n"
|
2019-03-07 23:21:58 +01:00
|
|
|
"This program comes with ABSOLUTELY NO WARRANTY. This is free software,\n"
|
|
|
|
"and you are welcome to redistribute it under certain conditions.\n";
|
2019-03-05 22:29:36 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2018-12-29 08:11:36 +01:00
|
|
|
|
2018-12-30 03:14:32 +01:00
|
|
|
const fs::path original = fs::canonical(argv[1]);
|
|
|
|
const fs::path source = cache_dir / original;
|
2018-12-29 08:31:16 +01:00
|
|
|
const fs::path binary = (source.string() + ".bin");
|
2018-12-29 08:11:36 +01:00
|
|
|
string compiler_arguments;
|
2018-12-30 03:14:32 +01:00
|
|
|
fs::create_directories(binary.parent_path());
|
2018-12-29 05:13:58 +01:00
|
|
|
|
2018-12-29 08:11:36 +01:00
|
|
|
if (!fs::exists(binary) ||
|
|
|
|
fs::last_write_time(original) > fs::last_write_time(binary))
|
|
|
|
{
|
|
|
|
std::ifstream in(original);
|
2018-12-29 05:13:58 +01:00
|
|
|
if (in.is_open())
|
|
|
|
{
|
|
|
|
std::ofstream out(source);
|
|
|
|
if (out.is_open())
|
|
|
|
{
|
|
|
|
string buf;
|
2018-12-31 13:48:37 +01:00
|
|
|
|
|
|
|
std::getline(in, buf);
|
|
|
|
if (!(buf.substr(0, 2) == "#!"))
|
|
|
|
{
|
|
|
|
out << buf << endl;
|
|
|
|
}
|
|
|
|
|
2018-12-29 05:33:58 +01:00
|
|
|
std::getline(in, buf);
|
2018-12-31 13:48:37 +01:00
|
|
|
if (buf.substr(0, 16) == "//compilescript:")
|
2018-12-29 05:33:58 +01:00
|
|
|
{
|
2018-12-29 06:08:33 +01:00
|
|
|
compiler_arguments = buf.substr(16);
|
2018-12-29 05:33:58 +01:00
|
|
|
}
|
2019-01-25 04:18:04 +01:00
|
|
|
else if ((buf.substr(0, 15) == "#compilescript:") ||
|
|
|
|
(buf.substr(0, 15) == ";compilescript:"))
|
|
|
|
{
|
|
|
|
compiler_arguments = buf.substr(15);
|
|
|
|
}
|
2018-12-29 05:33:58 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
out << buf << endl;
|
|
|
|
}
|
2018-12-31 13:48:37 +01:00
|
|
|
|
2018-12-29 05:13:58 +01:00
|
|
|
while (!in.eof())
|
|
|
|
{
|
|
|
|
std::getline(in, buf);
|
|
|
|
out << buf << endl;
|
|
|
|
}
|
2018-12-31 13:48:37 +01:00
|
|
|
|
2018-12-29 05:13:58 +01:00
|
|
|
in.close();
|
|
|
|
out.close();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cerr << "ERROR: Could not open file: " << source << endl;
|
2018-12-31 13:48:37 +01:00
|
|
|
return 1;
|
2018-12-29 05:13:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-29 08:11:36 +01:00
|
|
|
cerr << "ERROR: Could not open file: " << original << endl;
|
2018-12-31 13:48:37 +01:00
|
|
|
return 1;
|
2018-12-29 05:13:58 +01:00
|
|
|
}
|
|
|
|
|
2019-01-01 09:29:16 +01:00
|
|
|
int ret = std::system((compiler + " " + source.string() + " "
|
|
|
|
+ compiler_arguments + " -o " + binary.string()).c_str());
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
|
|
|
cerr << "ERROR: Compilation failed.\n";
|
|
|
|
return 1;
|
|
|
|
}
|
2018-12-29 05:13:58 +01:00
|
|
|
}
|
2018-12-29 06:19:53 +01:00
|
|
|
|
2018-12-29 08:11:36 +01:00
|
|
|
execv(binary.c_str(), &argv[1]);
|
|
|
|
|
2018-12-28 13:56:34 +01:00
|
|
|
return 0;
|
|
|
|
}
|