aboutsummaryrefslogtreecommitdiff
path: root/src/PkgConfig.cpp
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2017-12-10 01:10:48 +0100
committerdec05eba <dec05eba@protonmail.com>2017-12-10 01:12:08 +0100
commit1d3e221a7a20bfd03517e3ae1e35e4a309a69b6a (patch)
treefdb38039d12cf38e9ac6102118727b78437cf3db /src/PkgConfig.cpp
parent2ed7d0b09caa872e44e2eb09b09b2387e93f9b34 (diff)
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
Diffstat (limited to 'src/PkgConfig.cpp')
-rw-r--r--src/PkgConfig.cpp131
1 files changed, 131 insertions, 0 deletions
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<bool> PkgConfig::validatePackageExists(const string &name)
+ {
+ string command = "pkg-config --exists '";
+ command += name;
+ command += "'";
+ Result<ExecResult> execResult = exec(command.c_str());
+ if(execResult.isErr())
+ {
+ return Result<bool>::Err(execResult.getErrMsg());
+ }
+
+ if(execResult.unwrap().exitCode == 1)
+ {
+ string errMsg = "pkg-config dependency not found: ";
+ errMsg += name;
+ return Result<bool>::Err(errMsg);
+ }
+ else if(execResult.unwrap().exitCode == 127)
+ {
+ return Result<bool>::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<bool>::Err(errMsg);
+ }
+
+ return Result<bool>::Ok(true);
+ }
+
+ Result<bool> 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> execResult = exec(command.c_str());
+ if(execResult.isErr())
+ {
+ return Result<bool>::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<bool>::Err(errMsg);
+ }
+ else if(execResult.unwrap().exitCode == 127)
+ {
+ return Result<bool>::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<bool>::Err(errMsg);
+ }
+
+ return Result<bool>::Ok(true);
+ }
+
+ Result<string> PkgConfig::getDynamicLibsLinkerFlags(const vector<string> &libs)
+ {
+ if(libs.empty()) return Result<string>::Ok("");
+
+ string args;
+ for(const string &lib : libs)
+ {
+ args += " '";
+ args += lib;
+ args += "'";
+ }
+
+ string command = "pkg-config --libs";
+ command += args;
+ Result<ExecResult> execResult = exec(command.c_str());
+ if(execResult.isErr())
+ return Result<string>::Err(execResult.getErrMsg());
+
+ if(execResult.unwrap().exitCode == 0)
+ {
+ execResult.unwrap().execStdout = trimRight(execResult.unwrap().execStdout);
+ return Result<string>::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<string>::Err("Packages not found");
+ }
+ else if(execResult.unwrap().exitCode == 127)
+ {
+ return Result<string>::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<string>::Err(errMsg);
+ }
+ }
+} \ No newline at end of file