Simple files (without flags) work.
This commit is contained in:
parent
017b390805
commit
d058f1f012
@ -1,12 +1,13 @@
|
||||
cmake_minimum_required (VERSION 3.2)
|
||||
project(cppscript
|
||||
VERSION 0.7.1
|
||||
VERSION 0
|
||||
LANGUAGES CXX
|
||||
)
|
||||
|
||||
include(GNUInstallDirs)
|
||||
find_package(PkgConfig REQUIRED)
|
||||
pkg_check_modules(LIBXDG_BASEDIR REQUIRED libxdg-basedir)
|
||||
pkg_check_modules(LIBCONFIG REQUIRED libconfig++)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 14)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
@ -17,8 +18,10 @@ set(CMAKE_CXX_FLAGS_DEBUG
|
||||
|
||||
include_directories(${PROJECT_BINARY_DIR})
|
||||
include_directories(${LIBXDG_BASEDIR_INCLUDE_DIRS})
|
||||
include_directories(${LIBCONFIG_INCLUDE_DIRS})
|
||||
|
||||
link_directories(${LIBXDG_BASEDIR_LIBRARY_DIRS})
|
||||
link_directories(${LIBCONFIG_LIBRARY_DIRS})
|
||||
|
||||
# Write version in header
|
||||
configure_file(
|
||||
@ -29,6 +32,7 @@ configure_file(
|
||||
file(GLOB sources src/*.cpp)
|
||||
add_executable(${CMAKE_PROJECT_NAME} "${sources}")
|
||||
target_link_libraries(${CMAKE_PROJECT_NAME}
|
||||
"${LIBXDG_BASEDIR_LDFLAGS} -lstdc++fs")
|
||||
"${LIBXDG_BASEDIR_LDFLAGS} ${LIBCONFIG_LDFLAGS}"
|
||||
"-lstdc++fs")
|
||||
install(TARGETS ${CMAKE_PROJECT_NAME} DESTINATION ${CMAKE_INSTALL_BINDIR})
|
||||
#install(FILES README.md DESTINATION ${CMAKE_INSTALL_DOCDIR})
|
||||
|
110
src/main.cpp
110
src/main.cpp
@ -1,6 +1,116 @@
|
||||
/* Public Domain / CC-0
|
||||
* Author: tastytea <tastytea@tastytea.de>
|
||||
*/
|
||||
|
||||
#if __cplusplus >= 201703L
|
||||
#include <filesystem>
|
||||
#else
|
||||
#include <experimental/filesystem>
|
||||
#endif
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <fstream>
|
||||
#include <libconfig.h++>
|
||||
#include <basedir.h>
|
||||
#include <cstdlib>
|
||||
#include <unistd.h>
|
||||
#include "xdgcfg.hpp"
|
||||
#include "version.hpp"
|
||||
|
||||
#if __cplusplus >= 201703L
|
||||
namespace fs = std::filesystem;
|
||||
#else
|
||||
namespace fs = std::experimental::filesystem;
|
||||
#endif
|
||||
using std::cout;
|
||||
using std::cerr;
|
||||
using std::endl;
|
||||
|
||||
bool need_save = false;
|
||||
string compiler = "g++";
|
||||
fs::path cache_dir;
|
||||
|
||||
void read_settings()
|
||||
{
|
||||
xdgcfg config("cppscript.cfg");
|
||||
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);
|
||||
cache_dir /= "cppscript";
|
||||
xdgWipeHandle(&xdg);
|
||||
}
|
||||
if (!fs::is_directory(cache_dir))
|
||||
{
|
||||
fs::create_directories(cache_dir);
|
||||
}
|
||||
|
||||
if (need_save)
|
||||
{
|
||||
config.write();
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
read_settings();
|
||||
|
||||
if (argc > 1)
|
||||
{
|
||||
const fs::path path(argv[1]);
|
||||
const fs::path binary = cache_dir / path.stem();
|
||||
const fs::path source = binary.string() + ".cpp";
|
||||
|
||||
std::ifstream in(path);
|
||||
if (in.is_open())
|
||||
{
|
||||
std::ofstream out(source);
|
||||
if (out.is_open())
|
||||
{
|
||||
string buf;
|
||||
std::getline(in, buf);
|
||||
while (!in.eof())
|
||||
{
|
||||
std::getline(in, buf);
|
||||
out << buf << endl;
|
||||
}
|
||||
in.close();
|
||||
out.close();
|
||||
}
|
||||
else
|
||||
{
|
||||
cerr << "ERROR: Could not open file: " << source << endl;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
cerr << "ERROR: Could not open file: " << path << endl;
|
||||
}
|
||||
|
||||
std::system((compiler + " " + source.string()
|
||||
+ " -o " + binary.string()).c_str());
|
||||
execv(binary.c_str(), argv);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
2
xdgcfg
2
xdgcfg
@ -1 +1 @@
|
||||
Subproject commit 6c0976baa74f959ed3218af56e56ff58202a5a05
|
||||
Subproject commit e22f82fc6f1c40cda3d3ce5e671299f26f622528
|
Reference in New Issue
Block a user