#include "file.h" #include #include #include #include #include "env.h" namespace fs = std::filesystem; using namespace std::string_literals; const fs::path YMakefile{"YMakefile"}; // type of file can be built from dependencies bool is_buildable_by_extension(const fs::path& p) { fs::path ext{p.extension()}; return ext.empty() || ext == ".o" || is_dynamic_lib(p) || is_dynamic_lib_link(p) || is_static_lib(p) ; } namespace { std::unordered_set compile_unit_source_types{".cpp", ".c", ".cc", ".S", ".rs"}; } // type of file is source of compile unit (no included types like headers) bool is_compile_unit_source_by_extension(const fs::path& p) { fs::path ext{p.extension()}; return compile_unit_source_types.find(ext) != compile_unit_source_types.end(); } bool is_executable_target(const std::filesystem::path& p) { return p.extension().empty(); } // e.g. libxyz.so.1.2.3 (the shorter versions are links to this one) bool is_dynamic_lib(const std::filesystem::path& p) { std::string name{p.filename()}; return name.find(".so.") != name.npos && std::count(name.begin(), name.end(), '.') == 4; } // e.g. libxyz.so.1 libxyz.so bool is_dynamic_lib_link(const std::filesystem::path& p) { std::string name{p.filename()}; return name.find(".so") != name.npos && std::count(name.begin(), name.end(), '.') < 4; } // in: libxyz.so.1.2.3 // out: libxyz.so.1 std::filesystem::path soname_shorter(const std::filesystem::path& p) { std::string name{p.filename()}; if (std::count(name.begin(), name.end(), '.') != 4) { throw std::runtime_error("Unexpected soname: "s + name + ", 4 dots expected."); } size_t pos{0}; for (int i = 0; i < 3; ++i) { pos = name.find('.', pos); ++pos; } name = name.substr(0, pos - 1); fs::path result{p}; result.replace_filename(name); return result; } // in: libxyz.so.1.2.3 // out: libxyz.so std::filesystem::path soname_short(const std::filesystem::path& p) { std::string name{p.filename()}; if (std::count(name.begin(), name.end(), '.') != 4) { throw std::runtime_error("Unexpected soname: "s + name + ", 4 dots expected."); } size_t pos{0}; for (int i = 0; i < 2; ++i) { pos = name.find('.', pos); ++pos; } name = name.substr(0, pos - 1); fs::path result{p}; result.replace_filename(name); return result; } // e.g. // subdir1/libxyz.so.1.2.3 -> xyz // libabc.so -> abc // file1.so.1 -> file1 std::string dynamic_lib_name(const std::filesystem::path& p) { std::string result {p.stem().string()}; if (result.substr(0, 3) == "lib") { result = result.substr(3); } return result; } bool is_static_lib(const std::filesystem::path& p) { return p.extension() == ".a"; } std::filesystem::path simplified_path(const std::filesystem::path& p) { if (p.string().substr(0, 2) == "./") { return p.string().substr(2); } return p; } bool is_executable_command(const std::string& file) { std::vector paths {Reichwein::Stringhelper::split(env_value("PATH"), ":")}; for (const auto& i: paths) { fs::path path{i + "/" + file}; if (fs::exists(path) && (fs::status(path).permissions() & std::filesystem::perms::others_exec) != std::filesystem::perms::none) { return true; } } return false; } std::string find_executable(const std::vector& list) { std::vector paths {Reichwein::Stringhelper::split(env_value("PATH"), ":")}; for (const auto& i: list) { for (const auto& j: paths) { fs::path path{j + "/" + i}; if (fs::exists(path) && (fs::status(path).permissions() & std::filesystem::perms::others_exec) != std::filesystem::perms::none) { return i; } } } return {}; }