SB++
Sandbox applications
Loading...
Searching...
No Matches
libraries.hpp
1#pragma once
11
12#include "shared.hpp"
13#include "arguments.hpp"
14
15#include <fstream>
16
17namespace libraries {
18
19 using lib_t = shared::set;
20 extern lib_t directories;
21
22 inline std::filesystem::path hash_cache(const std::string_view& program, const std::string& hash) {
23 return std::filesystem::path(std::filesystem::path(shared::data)) / "sb" / program / "cache" / (hash + ".lib.cache");
24 }
25 inline std::filesystem::path hash_sof(const std::string_view& program, const std::string& hash) {
26 return std::filesystem::path(arg::get("sof")) / program / "lib" / hash;
27 }
28
37 void get(lib_t& libraries, const std::string_view& library, std::string directory = "");
38
46 void setup(const shared::set& libraries, const std::string_view& application, const std::filesystem::path& app_sof);
47 void setup(const shared::vector& libraries, const std::string_view& application, const std::filesystem::path& app_sof);
48
56 void symlink(shared::vector& command);
57
58 template <class C> void resolve(const C& required, const std::string_view& program, const std::string& l_hash, const bool& exclude = true) {
59 shared::log({"Resolving SOF"});
60
61 auto get_trimmed = [&required]() {
62 // Generate the list of invalid entries. Because
63 // we only read to the set, there is no risk in sharing it between
64 // the threads, so no mutex required.
65 shared::set exclusions = {};
66 for (const auto& [lib, mod] : arg::modlist("libraries")) {
67 if (mod == "x") {
68 if (lib.contains("*"))
69 exclusions.merge(shared::wildcard(lib, "/usr/lib", {"-maxdepth", "1", "-mindepth", "1", "-type", "f,l", "-executable"}));
70 else if (std::filesystem::is_directory(lib) && directories.contains(lib))
71 directories.erase(lib);
72 else exclusions.emplace(lib);
73 }
74 }
75
76 C trimmed = {};
77 for (const auto& lib : required) {
78 if (exclusions.contains(lib)) continue;
79 bool valid = true;
80 for (const auto& dir : directories) {
81 if (lib.starts_with(dir)) {
82 valid = false;
83 break;
84 }
85 }
86 if (valid) container::emplace(trimmed, lib);
87 }
88 return trimmed;
89 };
90
91 const auto& reduced = exclude ? get_trimmed() : required;
92 libraries::setup(reduced, program, hash_sof(program, l_hash));
93 auto lib_out = std::ofstream(hash_cache(program, l_hash));
94 for (const auto& arg : reduced) {
95 lib_out << arg << ' ';
96 }
97 lib_out.close();
98 }
99}
Shared-Library Dependency Resolution This header contains all the relevant functions for resolving sh...
Definition libraries.cpp:16
void get(lib_t &libraries, const std::string_view &library, std::string directory)
Recursively resolve all shared-libraries needed by a library.
Definition libraries.cpp:89
void symlink(vector &command)
Add symlink commands.
Definition libraries.cpp:215
set wildcard(const std::string_view &pattern, const std::string_view &path, const list &args)
Resolve wildcard patterns.
Definition shared.cpp:92
void log(const list &msg, const std::string &level)
Log output to console, if verbose.
Definition shared.cpp:40