From 1d3e221a7a20bfd03517e3ae1e35e4a309a69b6a Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sun, 10 Dec 2017 01:10:48 +0100 Subject: Add support for dependencies in global lib dir Global lib dir is located at ~/.sibs/lib TODO: If global lib dir doesn't exist, download it from github/server --- src/PkgConfig.cpp | 131 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 131 insertions(+) create mode 100644 src/PkgConfig.cpp (limited to 'src/PkgConfig.cpp') diff --git a/src/PkgConfig.cpp b/src/PkgConfig.cpp new file mode 100644 index 0000000..11e1cf0 --- /dev/null +++ b/src/PkgConfig.cpp @@ -0,0 +1,131 @@ +#include "../include/PkgConfig.hpp" +#include "../include/Exec.hpp" + +using namespace std; + +namespace sibs +{ + string trimRight(const string &input) + { + for(int i = input.size() - 1; i >= 0; --i) + { + if(!isspace(input[i])) + return input.substr(0, i + 1); + } + return input; + } + + Result PkgConfig::validatePackageExists(const string &name) + { + string command = "pkg-config --exists '"; + command += name; + command += "'"; + Result execResult = exec(command.c_str()); + if(execResult.isErr()) + { + return Result::Err(execResult.getErrMsg()); + } + + if(execResult.unwrap().exitCode == 1) + { + string errMsg = "pkg-config dependency not found: "; + errMsg += name; + return Result::Err(errMsg); + } + else if(execResult.unwrap().exitCode == 127) + { + return Result::Err("pkg-config is not installed"); + } + else if(execResult.unwrap().exitCode != 0) + { + string errMsg = "Failed to check if dependency exists, Unknown error, exit code: "; + errMsg += to_string(execResult.unwrap().exitCode); + return Result::Err(errMsg); + } + + return Result::Ok(true); + } + + Result PkgConfig::validatePackageVersionAtLeast(const string &name, const string &version) + { + // TODO: Instead of checking if package version is same or newer, check if package is same major version + // and same or newer minor version + + // Use --modversion instead and check if the version returned is newer or equal to dependency version. + // This way we can output installed version vs expected dependency version + string command = "pkg-config '--atleast-version="; + command += version; + command += "' '"; + command += name; + command += "'"; + Result execResult = exec(command.c_str()); + if(execResult.isErr()) + { + return Result::Err(execResult.getErrMsg()); + } + + if(execResult.unwrap().exitCode == 1) + { + string errMsg = "Dependency "; + errMsg += name; + errMsg += " is installed but the version older than "; + errMsg += version; + return Result::Err(errMsg); + } + else if(execResult.unwrap().exitCode == 127) + { + return Result::Err("pkg-config is not installed"); + } + else if(execResult.unwrap().exitCode != 0) + { + string errMsg = "Failed to check dependency version, Unknown error, exit code: "; + errMsg += to_string(execResult.unwrap().exitCode); + return Result::Err(errMsg); + } + + return Result::Ok(true); + } + + Result PkgConfig::getDynamicLibsLinkerFlags(const vector &libs) + { + if(libs.empty()) return Result::Ok(""); + + string args; + for(const string &lib : libs) + { + args += " '"; + args += lib; + args += "'"; + } + + string command = "pkg-config --libs"; + command += args; + Result execResult = exec(command.c_str()); + if(execResult.isErr()) + return Result::Err(execResult.getErrMsg()); + + if(execResult.unwrap().exitCode == 0) + { + execResult.unwrap().execStdout = trimRight(execResult.unwrap().execStdout); + return Result::Ok(execResult.unwrap().execStdout); + } + else if(execResult.unwrap().exitCode == 1) + { + // TODO: This shouldn't happen because we check if each dependency is installed before this, + // but maybe the package is uninstalled somewhere between here... + // Would be better to recheck if each package is installed here again + // to know which package was uninstalled + return Result::Err("Packages not found"); + } + else if(execResult.unwrap().exitCode == 127) + { + return Result::Err("pkg-config is not installed"); + } + else + { + string errMsg = "Failed to get package dynamic lib linking flags, Unknown error, exit code: "; + errMsg += to_string(execResult.unwrap().exitCode); + return Result::Err(errMsg); + } + } +} \ No newline at end of file -- cgit v1.2.3