This repository has been archived on 2019-10-11. You can view files and clone it, but cannot push or open issues or pull requests.
compilescript/src/main.cpp

295 lines
7.7 KiB
C++
Raw Normal View History

/* 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
*/
2019-10-01 11:10:11 +02:00
#include "version.hpp"
#include "xdgcfg.hpp"
#include <libconfig.h++>
#include <basedir.h>
#include <unistd.h>
#include <chrono>
#include <cstdlib>
#include <exception>
2019-10-01 08:23:23 +02:00
#include <experimental/filesystem>
2019-10-01 11:10:11 +02:00
#include <fstream>
2018-12-29 05:13:58 +01:00
#include <iostream>
2019-10-10 22:01:36 +02:00
#include <regex>
2018-12-29 05:13:58 +01:00
#include <string>
#include <system_error>
2019-10-01 11:10:11 +02:00
#include <vector>
2018-12-28 13:56:34 +01:00
2019-10-01 08:23:23 +02:00
namespace fs = std::experimental::filesystem;
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;
using std::chrono::system_clock;
using std::chrono::hours;
2018-12-31 13:58:23 +01:00
using std::chrono::duration_cast;
2019-10-01 11:10:11 +02:00
using std::string;
using std::vector;
class Compilescript
{
private:
string _compiler;
fs::path _cache_dir;
int _clean_after_hours;
2018-12-29 05:13:58 +01:00
2019-10-01 11:10:11 +02:00
public:
Compilescript();
2018-12-29 05:13:58 +01:00
//! Read settings and set member variables.
2019-10-01 11:10:11 +02:00
void read_settings();
//! Remove files older than `_clean_after_hours` from the cache.
2019-10-01 11:10:11 +02:00
void cleanup();
//! Compile `filename` if necessary and return filename of binary.
string compile(const string &filename);
//! Run binary. Pass original `argv`, the 1st entry is ignored.
void run(const string &filename, char *argv[]);
//! Print version, copyright and license.
2019-10-01 11:10:11 +02:00
void print_version();
//! Set compiler command.
void set_compiler(const string &command);
2019-10-01 11:10:11 +02:00
};
Compilescript::Compilescript()
: _compiler("g++ -x c++")
, _clean_after_hours(30 * 24)
{}
void Compilescript::read_settings()
2018-12-29 05:13:58 +01:00
{
2018-12-29 05:56:26 +01:00
bool need_save = false;
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"))
{
2019-10-01 11:10:11 +02:00
_compiler = cfg["compiler"].c_str();
2018-12-29 05:13:58 +01:00
}
else
{
2019-10-01 11:10:11 +02:00
cfg.add("compiler", libconfig::Setting::TypeString) = _compiler;
2018-12-29 05:13:58 +01:00
need_save = true;
}
if (cfg.exists("clean_after_hours"))
{
2019-10-01 11:10:11 +02:00
cfg.lookupValue("clean_after_hours", _clean_after_hours);
}
else
{
cfg.add("clean_after_hours",
2019-10-01 11:10:11 +02:00
libconfig::Setting::TypeInt) = _clean_after_hours;
need_save = true;
}
2018-12-29 05:13:58 +01:00
if (cfg.exists("cache_dir"))
{
2019-10-01 11:10:11 +02:00
_cache_dir = cfg["cache_dir"].c_str();
2018-12-29 05:13:58 +01:00
}
else
{
xdgHandle xdg;
xdgInitHandle(&xdg);
2019-10-01 11:10:11 +02:00
_cache_dir = xdgCacheHome(&xdg);
_cache_dir /= "compilescript";
2018-12-29 05:13:58 +01:00
xdgWipeHandle(&xdg);
}
2019-10-01 11:10:11 +02:00
if (!fs::is_directory(_cache_dir))
2018-12-29 05:13:58 +01:00
{
2019-10-01 11:10:11 +02:00
fs::create_directories(_cache_dir);
2018-12-29 05:13:58 +01:00
}
if (need_save)
{
config.write();
}
}
2019-10-01 11:10:11 +02:00
void Compilescript::cleanup()
{
2019-10-01 11:10:11 +02:00
for (const auto &entry : fs::recursive_directory_iterator(_cache_dir))
{
2019-10-01 11:10:11 +02:00
if (!fs::is_regular_file(entry))
{
2019-10-01 11:10:11 +02:00
continue;
}
const auto diff = system_clock::now() - fs::last_write_time(entry);
if (duration_cast<hours>(diff).count() > _clean_after_hours)
{
fs::path current_path = entry.path();
std::error_code e;
2019-10-01 11:10:11 +02:00
while (fs::remove(current_path, e))
{
current_path = current_path.parent_path();
if (current_path == _cache_dir)
{
2019-10-01 11:10:11 +02:00
break;
}
}
}
}
}
string Compilescript::compile(const string &filename)
2018-12-28 13:56:34 +01:00
{
2019-10-01 11:10:11 +02:00
const fs::path original = fs::canonical(filename);
const fs::path source = _cache_dir / original;
const fs::path binary = (source.string() + ".bin");
string compiler_arguments;
fs::create_directories(binary.parent_path());
2018-12-29 05:13:58 +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);
2019-10-10 22:01:36 +02:00
const std::regex re("^(//|#|;) ?compilescript:");
std::smatch match;
if (std::regex_search(buf, match, re))
2019-10-01 11:10:11 +02:00
{
2019-10-10 22:01:36 +02:00
compiler_arguments = match.suffix();
2019-01-25 04:18:04 +01:00
}
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
{
throw std::runtime_error("Could not open file: "
+ source.string());
2018-12-29 05:13:58 +01:00
}
}
else
{
throw std::runtime_error("Could not open file: "
+ original.string());
2018-12-29 05:13:58 +01:00
}
2019-10-01 11:10:11 +02:00
const string command = _compiler + " " + source.string() + " "
+ compiler_arguments + " -o " + binary.string();
2019-10-10 22:00:22 +02:00
int ret = std::system(command.c_str()); // NOLINT(cert-env33-c)
2019-01-01 09:29:16 +01:00
if (ret != 0)
{
throw std::runtime_error("Compilation failed.");
2019-01-01 09:29:16 +01:00
}
2018-12-29 05:13:58 +01:00
}
return binary.string();
}
2019-10-01 11:10:11 +02:00
void Compilescript::run(const string &filename, char *argv[])
{
execvp(filename.c_str(), argv);
2019-10-01 11:10:11 +02:00
}
void Compilescript::print_version()
{
cout << "compilescript " << global::version << '\n' <<
"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"
"This program comes with ABSOLUTELY NO WARRANTY. This is free software,"
"\nand you are welcome to redistribute it under certain conditions.\n";
}
void Compilescript::set_compiler(const string &command)
{
_compiler = command;
}
2019-10-01 11:10:11 +02:00
int main(int argc, char *argv[])
{
const vector<string> args(argv, argv + argc);
try
{
Compilescript App;
App.read_settings();
if (args.size() <= 1)
{
cerr << "usage: " << args[0]
<< " [file|--cleanup|--version|--compiler command] "
<< "[arguments]\n";
return 1;
}
if (args[1] == "--cleanup")
{
App.cleanup();
return 0;
}
if (args[1] == "--version")
{
App.print_version();
return 0;
}
if (args[1] == "--compiler")
{
if (args.size() <= 3)
{
cerr << "Error: You need to specify a command.\n";
return 1;
}
App.set_compiler(args[2]);
const string binary = App.compile(args[3]);
// We know that argv[3] exists.
// NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
App.run(binary, &argv[3]);
return 0;
}
const string binary = App.compile(args[1]);
// We know that argv[1] exists.
// NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
App.run(binary, &argv[1]);
2019-10-01 11:10:11 +02:00
}
catch (const std::exception &e)
{
cerr << "Error: " << e.what() << endl;
return 1;
}
2018-12-28 13:56:34 +01:00
return 0;
}