aboutsummaryrefslogtreecommitdiff
path: root/src/PkgConfig.cpp
blob: a66d5b3d880af3728e7a0a68c869ea4acf48e73b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include "../include/PkgConfig.hpp"
#if OS_FAMILY == OS_FAMILY_POSIX
#include "../include/Exec.hpp"

using namespace std;

// TODO: Do not use pkg-config program. The same functionality can easily be achieved
// by reading files in /usr/bin/pkgconfig
// Or is there no downside to calling pkg-config program?
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<Dependency> &libs)
    {
        if(libs.empty()) return Result<string>::Ok("");

        string args;
        for(const Dependency &lib : libs)
        {
            args += " '";
            args += lib.name;
            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);
        }
    }
}
#endif // OS_FAMILY_POSIX