mirror of https://gitlab.com/cppit/jucipp
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
123 lines
4.8 KiB
123 lines
4.8 KiB
|
6 years ago
|
#include "meson.hpp"
|
||
|
|
#include "compile_commands.hpp"
|
||
|
|
#include "config.hpp"
|
||
|
|
#include "dialogs.hpp"
|
||
|
|
#include "filesystem.hpp"
|
||
|
|
#include "terminal.hpp"
|
||
|
8 years ago
|
#include <regex>
|
||
|
9 years ago
|
|
||
|
|
Meson::Meson(const boost::filesystem::path &path) {
|
||
|
8 years ago
|
const auto find_project = [](const boost::filesystem::path &file_path) {
|
||
|
|
for(auto &line : filesystem::read_lines(file_path)) {
|
||
|
8 years ago
|
const static std::regex project_regex(R"(^ *project *\(.*\r?$)", std::regex::icase);
|
||
|
9 years ago
|
std::smatch sm;
|
||
|
|
if(std::regex_match(line, sm, project_regex))
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
return false;
|
||
|
|
};
|
||
|
8 years ago
|
|
||
|
6 years ago
|
boost::system::error_code ec;
|
||
|
|
auto search_path = boost::filesystem::is_directory(path, ec) ? path : path.parent_path();
|
||
|
9 years ago
|
while(true) {
|
||
|
8 years ago
|
auto search_file = search_path / "meson.build";
|
||
|
6 years ago
|
if(boost::filesystem::exists(search_file, ec)) {
|
||
|
9 years ago
|
if(find_project(search_file)) {
|
||
|
8 years ago
|
project_path = search_path;
|
||
|
9 years ago
|
break;
|
||
|
|
}
|
||
|
|
}
|
||
|
8 years ago
|
if(search_path == search_path.root_directory())
|
||
|
9 years ago
|
break;
|
||
|
8 years ago
|
search_path = search_path.parent_path();
|
||
|
9 years ago
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
bool Meson::update_default_build(const boost::filesystem::path &default_build_path, bool force) {
|
||
|
6 years ago
|
boost::system::error_code ec;
|
||
|
|
if(project_path.empty() || !boost::filesystem::exists(project_path / "meson.build", ec) || default_build_path.empty())
|
||
|
8 years ago
|
return false;
|
||
|
|
|
||
|
6 years ago
|
if(!boost::filesystem::exists(default_build_path, ec)) {
|
||
|
9 years ago
|
boost::system::error_code ec;
|
||
|
|
boost::filesystem::create_directories(default_build_path, ec);
|
||
|
|
if(ec) {
|
||
|
8 years ago
|
Terminal::get().print("Error: could not create " + default_build_path.string() + ": " + ec.message() + "\n", true);
|
||
|
9 years ago
|
return false;
|
||
|
|
}
|
||
|
|
}
|
||
|
8 years ago
|
|
||
|
|
auto compile_commands_path = default_build_path / "compile_commands.json";
|
||
|
6 years ago
|
bool compile_commands_exists = boost::filesystem::exists(compile_commands_path, ec);
|
||
|
9 years ago
|
if(!force && compile_commands_exists)
|
||
|
|
return true;
|
||
|
8 years ago
|
|
||
|
9 years ago
|
Dialog::Message message("Creating/updating default build");
|
||
|
8 years ago
|
auto exit_status = Terminal::get().process(Config::get().project.meson.command + ' ' +
|
||
|
|
(compile_commands_exists ? "--internal regenerate " : "") +
|
||
|
7 years ago
|
"--buildtype plain " + filesystem::escape_argument(project_path.string()), default_build_path);
|
||
|
9 years ago
|
message.hide();
|
||
|
8 years ago
|
if(exit_status == EXIT_SUCCESS)
|
||
|
9 years ago
|
return true;
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
bool Meson::update_debug_build(const boost::filesystem::path &debug_build_path, bool force) {
|
||
|
6 years ago
|
boost::system::error_code ec;
|
||
|
|
if(project_path.empty() || !boost::filesystem::exists(project_path / "meson.build", ec) || debug_build_path.empty())
|
||
|
9 years ago
|
return false;
|
||
|
8 years ago
|
|
||
|
6 years ago
|
if(!boost::filesystem::exists(debug_build_path, ec)) {
|
||
|
9 years ago
|
boost::system::error_code ec;
|
||
|
|
boost::filesystem::create_directories(debug_build_path, ec);
|
||
|
|
if(ec) {
|
||
|
8 years ago
|
Terminal::get().print("Error: could not create " + debug_build_path.string() + ": " + ec.message() + "\n", true);
|
||
|
9 years ago
|
return false;
|
||
|
|
}
|
||
|
|
}
|
||
|
8 years ago
|
|
||
|
6 years ago
|
bool compile_commands_exists = boost::filesystem::exists(debug_build_path / "compile_commands.json", ec);
|
||
|
9 years ago
|
if(!force && compile_commands_exists)
|
||
|
|
return true;
|
||
|
8 years ago
|
|
||
|
9 years ago
|
Dialog::Message message("Creating/updating debug build");
|
||
|
8 years ago
|
auto exit_status = Terminal::get().process(Config::get().project.meson.command + ' ' +
|
||
|
|
(compile_commands_exists ? "--internal regenerate " : "") +
|
||
|
|
"--buildtype debug " + filesystem::escape_argument(project_path.string()), debug_build_path);
|
||
|
9 years ago
|
message.hide();
|
||
|
8 years ago
|
if(exit_status == EXIT_SUCCESS)
|
||
|
9 years ago
|
return true;
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
boost::filesystem::path Meson::get_executable(const boost::filesystem::path &build_path, const boost::filesystem::path &file_path) {
|
||
|
|
CompileCommands compile_commands(build_path);
|
||
|
8 years ago
|
|
||
|
|
size_t best_match_size = -1;
|
||
|
9 years ago
|
boost::filesystem::path best_match_executable;
|
||
|
8 years ago
|
for(auto &command : compile_commands.commands) {
|
||
|
|
auto command_file = filesystem::get_normal_path(command.file);
|
||
|
|
auto values = command.parameter_values("-o");
|
||
|
9 years ago
|
if(!values.empty()) {
|
||
|
|
size_t pos;
|
||
|
8 years ago
|
if((pos = values[0].find('@')) != std::string::npos) {
|
||
|
|
if(pos + 1 < values[0].size() && values[0].compare(pos + 1, 3, "exe") == 0) {
|
||
|
|
auto executable = build_path / values[0].substr(0, pos);
|
||
|
|
if(command_file == file_path)
|
||
|
9 years ago
|
return executable;
|
||
|
8 years ago
|
auto command_file_directory = command_file.parent_path();
|
||
|
9 years ago
|
if(filesystem::file_in_path(file_path, command_file_directory)) {
|
||
|
8 years ago
|
auto size = static_cast<size_t>(std::distance(command_file_directory.begin(), command_file_directory.end()));
|
||
|
|
if(best_match_size == static_cast<size_t>(-1) || best_match_size < size) {
|
||
|
|
best_match_size = size;
|
||
|
|
best_match_executable = executable;
|
||
|
9 years ago
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
8 years ago
|
|
||
|
9 years ago
|
return best_match_executable;
|
||
|
|
}
|