2018-12-29 06:08:33 +01:00
|
|
|
/* This file is part of compilescript.
|
2018-12-29 05:58:15 +01:00
|
|
|
* Copyright © 2018 tastytea <tastytea@tastytea.de>
|
|
|
|
*
|
|
|
|
* 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>
|
|
|
|
#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
|
|
|
|
using std::cerr;
|
|
|
|
using std::endl;
|
|
|
|
|
|
|
|
string compiler = "g++";
|
|
|
|
fs::path cache_dir;
|
|
|
|
|
|
|
|
void read_settings()
|
|
|
|
{
|
2018-12-29 05:56:26 +01:00
|
|
|
bool need_save = false;
|
2018-12-29 06:08:33 +01:00
|
|
|
xdgcfg config("compilescript.cfg");
|
2018-12-29 05:13:58 +01:00
|
|
|
if (config.read() != 0)
|
|
|
|
{
|
|
|
|
config.write();
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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-28 13:56:34 +01:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2018-12-29 05:13:58 +01:00
|
|
|
read_settings();
|
|
|
|
|
2018-12-29 08:11:36 +01:00
|
|
|
if (argc <= 1)
|
2018-12-29 05:13:58 +01:00
|
|
|
{
|
2018-12-29 08:11:36 +01:00
|
|
|
cerr << "usage: " << argv[0] << " file [arguments]\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
const fs::path original(argv[1]);
|
2018-12-29 08:16:05 +01:00
|
|
|
const fs::path source = cache_dir / original.filename();
|
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-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-29 06:19:53 +01:00
|
|
|
std::getline(in, buf); // Shebang
|
2018-12-29 05:33:58 +01:00
|
|
|
std::getline(in, buf);
|
2018-12-29 06:08:33 +01:00
|
|
|
if (buf.substr(0, 16).compare("//compilescript:") == 0)
|
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
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
out << buf << endl;
|
|
|
|
}
|
2018-12-29 05:13:58 +01:00
|
|
|
while (!in.eof())
|
|
|
|
{
|
|
|
|
std::getline(in, buf);
|
|
|
|
out << buf << endl;
|
|
|
|
}
|
|
|
|
in.close();
|
|
|
|
out.close();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cerr << "ERROR: Could not open file: " << source << endl;
|
2018-12-29 05:56:26 +01:00
|
|
|
std::exit(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-29 05:56:26 +01:00
|
|
|
std::exit(1);
|
2018-12-29 05:13:58 +01:00
|
|
|
}
|
|
|
|
|
2018-12-29 05:33:58 +01:00
|
|
|
std::system((compiler + " " + source.string() + " " + compiler_arguments
|
2018-12-29 05:13:58 +01:00
|
|
|
+ " -o " + binary.string()).c_str());
|
|
|
|
}
|
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;
|
|
|
|
}
|