14std::string trim(std::string value) {
15 const auto not_space = [](
unsigned char c) {
return !std::isspace(c); };
16 value.erase(value.begin(), std::find_if(value.begin(), value.end(), not_space));
17 value.erase(std::find_if(value.rbegin(), value.rend(), not_space).base(), value.end());
21std::string command_output(
const std::string& command) {
22 std::array<char, 256> buffer{};
25 FILE* pipe = popen((command +
" 2>/dev/null").c_str(),
"r");
30 while (fgets(buffer.data(), buffer.size(), pipe) !=
nullptr) {
31 result += buffer.data();
34 const int status = pclose(pipe);
42std::filesystem::path find_libgfortran() {
43 const std::string from_gpp = command_output(
"g++ -print-file-name=libgfortran.so");
44 if (!from_gpp.empty() && from_gpp !=
"libgfortran.so" && std::filesystem::exists(from_gpp)) {
48 const std::string from_gfortran = command_output(
"gfortran -print-file-name=libgfortran.so");
49 if (!from_gfortran.empty() && from_gfortran !=
"libgfortran.so" && std::filesystem::exists(from_gfortran)) {
56std::filesystem::path require_libgfortran() {
57 const auto lib = find_libgfortran();
62 throw std::runtime_error(
63 "MARTY generated-code compilation requires libgfortran, but the linker "
64 "cannot find libgfortran.so.\n"
65 "Install the Fortran development package, for example on Ubuntu/Debian:\n"
66 " sudo apt update && sudo apt install gfortran\n"
68 " g++ -print-file-name=libgfortran.so\n"
69 "The command must print an absolute path, not just 'libgfortran.so'."
77 this->
compile(sourceFile, outputBinary);
90 const auto libgfortran = require_libgfortran();
96 std::string command_compile =
"g++ -o " + outputBinary +
" " + sourceFile
99 +
" -Wl,-rpath," + lib_dir
100 +
" -lmarty " + libgfortran_path;
bool executeCommand(const std::string &command)
Executes a shell command and captures its output.
Declares a compilation strategy using g++ directly.
virtual bool check_if_compile(const std::string &outputBinary)
Checks whether a compiled binary already exists and is non-empty.
std::string model
Model name.
std::string wilson
Wilson basis / label name.
static std::shared_ptr< FileNameManager > getInstance(const std::string &wilson="", const std::string &model="")
Retrieves a FileNameManager for a given (wilson, model) pair.
void compile(const std::string &sourceFile, const std::string &outputBinary) override
Compiles the given source file using g++ and links against MARTY.
void compile_run(const std::string &sourceFile, const std::string &outputBinary) override
Compiles if necessary and then runs the resulting binary.
static std::string shell_quote(const std::filesystem::path &path)
Shell-quote a path for command strings passed to std::system().
static InstallInfo require_available(const std::string &context)
Validate and return the active MARTY installation.