2023-12-09 23:28:02 +01:00
|
|
|
#include "../libpkg/data/package.h"
|
2023-11-09 15:18:50 +01:00
|
|
|
#include "../libpkg/parser/binary.h"
|
|
|
|
|
|
|
|
#include "resources/config.h"
|
|
|
|
|
|
|
|
#include <c++utilities/application/argumentparser.h>
|
|
|
|
#include <c++utilities/conversion/stringbuilder.h>
|
|
|
|
#include <c++utilities/io/path.h>
|
|
|
|
#include <c++utilities/misc/math.h>
|
|
|
|
#include <c++utilities/misc/parseerror.h>
|
|
|
|
|
|
|
|
#include <reflective_rapidjson/json/reflector.h>
|
|
|
|
|
2023-11-17 01:00:12 +01:00
|
|
|
#include <atomic>
|
|
|
|
#include <cstdlib>
|
2023-11-09 15:18:50 +01:00
|
|
|
#include <iostream>
|
|
|
|
#include <mutex>
|
|
|
|
#include <thread>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
using namespace CppUtilities;
|
|
|
|
|
2023-11-17 01:00:12 +01:00
|
|
|
struct BinaryInfo : public ReflectiveRapidJSON::JsonSerializable<BinaryInfo> {
|
|
|
|
std::string name;
|
|
|
|
std::string architecture;
|
|
|
|
std::set<std::string> symbols;
|
|
|
|
std::set<std::string> requiredLibs;
|
|
|
|
std::string rpath;
|
|
|
|
std::string prefix;
|
|
|
|
bool isBigEndian = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct PacparseResults : public ReflectiveRapidJSON::JsonSerializable<PacparseResults> {
|
|
|
|
std::vector<std::shared_ptr<LibPkg::Package>> packages;
|
|
|
|
std::vector<BinaryInfo> binaries;
|
|
|
|
};
|
|
|
|
|
2023-11-09 15:18:50 +01:00
|
|
|
int main(int argc, const char *argv[])
|
|
|
|
{
|
|
|
|
SET_APPLICATION_INFO;
|
|
|
|
|
|
|
|
// read cli args
|
|
|
|
auto parser = ArgumentParser();
|
2023-11-17 01:00:12 +01:00
|
|
|
auto packagesArg = ConfigValueArgument("packages", 'p', "specifies the paths of the package files to parse", { "path" });
|
2023-11-09 15:18:50 +01:00
|
|
|
packagesArg.setRequiredValueCount(Argument::varValueCount);
|
|
|
|
packagesArg.setImplicit(true);
|
2023-11-17 01:00:12 +01:00
|
|
|
auto binariesArg = ConfigValueArgument("binaries", 'b', "specifies the paths of the binaries", { "path" });
|
|
|
|
binariesArg.setRequiredValueCount(Argument::varValueCount);
|
|
|
|
parser.setMainArguments({ &packagesArg, &binariesArg, &parser.helpArg() });
|
2023-11-09 15:18:50 +01:00
|
|
|
parser.setDefaultArgument(&parser.helpArg());
|
|
|
|
parser.parseArgs(argc, argv);
|
2023-11-17 01:00:12 +01:00
|
|
|
if (parser.helpArg().isPresent()) {
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
2023-11-09 15:18:50 +01:00
|
|
|
|
2023-11-17 01:00:12 +01:00
|
|
|
auto res = PacparseResults();
|
2023-11-09 15:18:50 +01:00
|
|
|
auto packageMutex = std::mutex();
|
2023-11-17 01:00:12 +01:00
|
|
|
auto binaryMutex = std::mutex();
|
|
|
|
auto returnCode = std::atomic<int>(EXIT_SUCCESS);
|
2023-11-09 15:18:50 +01:00
|
|
|
|
|
|
|
auto pi = std::vector<const char *>::const_iterator();
|
|
|
|
auto pend = std::vector<const char *>::const_iterator();
|
2023-11-17 01:00:12 +01:00
|
|
|
auto bi = std::vector<const char *>::const_iterator();
|
|
|
|
auto bend = std::vector<const char *>::const_iterator();
|
|
|
|
auto imutex = std::mutex();
|
2023-11-09 15:18:50 +01:00
|
|
|
if (packagesArg.isPresent()) {
|
|
|
|
const auto &packagePaths = packagesArg.values();
|
|
|
|
pi = packagePaths.begin();
|
|
|
|
pend = packagePaths.end();
|
2023-11-17 01:00:12 +01:00
|
|
|
res.packages.reserve(packagePaths.size());
|
|
|
|
}
|
|
|
|
if (binariesArg.isPresent()) {
|
|
|
|
const auto &binaryPaths = binariesArg.values();
|
|
|
|
bi = binaryPaths.begin();
|
|
|
|
bend = binaryPaths.end();
|
|
|
|
res.binaries.reserve(binaryPaths.size());
|
2023-11-09 15:18:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const auto processPackage = [&] {
|
|
|
|
for (;;) {
|
|
|
|
// get next package path
|
2023-11-17 01:00:12 +01:00
|
|
|
const char *path = nullptr;
|
|
|
|
auto isBinary = false;
|
|
|
|
auto ilock = std::unique_lock<std::mutex>(imutex);
|
|
|
|
if (pi != pend) {
|
|
|
|
path = *pi++;
|
|
|
|
} else if (bi != bend) {
|
|
|
|
isBinary = true;
|
|
|
|
path = *bi++;
|
|
|
|
} else {
|
2023-11-09 15:18:50 +01:00
|
|
|
return;
|
|
|
|
}
|
2023-11-17 01:00:12 +01:00
|
|
|
ilock.unlock();
|
2023-11-09 15:18:50 +01:00
|
|
|
|
|
|
|
// parse package
|
|
|
|
try {
|
2023-11-17 01:00:12 +01:00
|
|
|
if (isBinary) {
|
|
|
|
auto binary = LibPkg::Binary();
|
|
|
|
binary.load(path);
|
|
|
|
auto binaryLock = std::unique_lock<std::mutex>(binaryMutex);
|
|
|
|
auto &binaryInfo = res.binaries.emplace_back();
|
|
|
|
binaryInfo.prefix = binary.addPrefix(std::string_view());
|
|
|
|
binaryInfo.name = std::move(binary.name);
|
|
|
|
binaryInfo.architecture = std::move(binary.architecture);
|
|
|
|
binaryInfo.isBigEndian = binary.isBigEndian;
|
|
|
|
binaryInfo.rpath = std::move(binary.rpath);
|
|
|
|
binaryInfo.symbols = std::move(binary.symbols);
|
|
|
|
binaryInfo.requiredLibs = std::move(binary.requiredLibs);
|
|
|
|
} else {
|
|
|
|
auto package = LibPkg::Package::fromPkgFile(path);
|
|
|
|
auto binaryLock = std::unique_lock<std::mutex>(packageMutex);
|
|
|
|
res.packages.emplace_back(std::move(package));
|
|
|
|
}
|
2023-11-09 15:18:50 +01:00
|
|
|
} catch (const std::runtime_error &e) {
|
2023-11-17 01:00:12 +01:00
|
|
|
std::cerr << "Unable to parse \"" << path << "\": " << e.what() << '\n';
|
|
|
|
returnCode = EXIT_FAILURE;
|
2023-11-09 15:18:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-11-17 01:00:12 +01:00
|
|
|
auto threads = std::vector<std::thread>(
|
|
|
|
std::max<std::size_t>(std::min<std::size_t>(std::thread::hardware_concurrency(), res.packages.capacity() + res.binaries.capacity()), 1u) - 1);
|
2023-11-09 15:18:50 +01:00
|
|
|
for (auto &t : threads) {
|
|
|
|
t = std::thread(processPackage);
|
|
|
|
}
|
|
|
|
processPackage();
|
|
|
|
for (auto &t : threads) {
|
|
|
|
t.join();
|
|
|
|
}
|
|
|
|
|
2023-11-17 01:00:12 +01:00
|
|
|
const auto json = ReflectiveRapidJSON::JsonReflector::toJson(res);
|
2023-11-09 15:18:50 +01:00
|
|
|
std::cout << std::string_view(json.GetString(), json.GetSize());
|
2023-11-17 01:00:12 +01:00
|
|
|
return returnCode;
|
2023-11-09 15:18:50 +01:00
|
|
|
}
|
2023-11-17 01:00:12 +01:00
|
|
|
|
|
|
|
#include "reflection/main.h"
|