#include "pc_closure.hpp" #include namespace pc_closure { namespace { std::vector split_words(const std::string &text) { std::vector output; std::size_t position = 0; while (position < text.size()) { const auto start = text.find_first_not_of(" \t", position); if (start == std::string::npos) break; const auto end = text.find_first_of(" \t", start); output.push_back(text.substr(start, end == std::string::npos ? end : end - start)); if (end == std::string::npos) break; position = end; } return output; } std::string normalize_absolute(const std::string &path) { std::vector components; std::size_t position = 0; while (position < path.size()) { const auto start = path.find_first_not_of('/', position); if (start == std::string::npos) break; const auto end = path.find('/', start); const auto component = path.substr(start, end == std::string::npos ? end : end - start); if (component == "..") { if (!components.empty()) components.pop_back(); } else if (component != ".") components.push_back(component); if (end == std::string::npos) break; position = end; } std::string result; for (const auto &component : components) result += '/' + component; return result.empty() ? "/" : result; } std::string rooted(std::string path, const std::string &root) { if (path.empty() || path.front() != '/') return path; path = normalize_absolute(path); if (root.empty() || root == "/") return path; if (path == root || (path.size() > root.size() && path.compare(0, root.size(), root) == 0 && path[root.size()] == '/')) return path; return path == "/" ? root : root + path; } bool project_field(const std::string &text, const std::string &root, std::vector &output) { const auto input = split_words(text); for (std::size_t i = 0; i < input.size(); ++i) { const std::string &word = input[i]; if (word.size() >= 2 && word[0] == '-' && (word[1] == 'I' || word[1] == 'L')) { std::string directory; if (word.size() == 2) { if (++i == input.size()) return false; directory = input[i]; } else directory = word.substr(2); output.push_back(word.substr(0, 2) + rooted(directory, root)); } else output.push_back(word); } return true; } bool append_bounded(std::vector &destination, const std::vector &source, std::size_t &bytes) { for (const auto &token : source) { if (token.size() > 1048576 - bytes) return false; bytes += token.size(); destination.push_back(token); } return true; } } // namespace Status plan_flags(const Repository &repository, const Request &request, Plan &output) { output = {}; Closure closure; if (resolve_closure(repository, request, closure) != Status::ok) return Status::invalid; const std::string root = request.sysroot.empty() ? "" : normalize_absolute(request.sysroot); Repository staged = repository; for (auto &package : staged.packages) { for (std::size_t field = 3; field < 6; ++field) { std::vector tokens; if (!project_field(package.fields[field], root, tokens)) return Status::invalid; std::string expression; for (const auto &token : tokens) { if (!expression.empty()) expression += ' '; expression += token; } package.fields[field] = std::move(expression); } } Expansion expanded; if (expand_repository(staged, expanded) != Status::ok) return Status::invalid; std::map index; for (const auto &package : expanded.packages) index.emplace(package.name, &package); Plan draft; draft.packages = closure.packages; std::size_t bytes = 0; for (const auto &name : closure.packages) { const auto &package = *index.at(name); for (std::size_t field : {std::size_t(5), std::size_t(3), std::size_t(4)}) { if (field == 4 && request.mode != Mode::static_link) continue; std::vector tokens; tokens = split_words(package.fields[field]); auto &destination = field == 5 ? draft.cflags : draft.libs; if (!append_bounded(destination, tokens, bytes)) return Status::invalid; } } output = std::move(draft); return Status::ok; } } // namespace pc_closure