aboutsummaryrefslogtreecommitdiff
path: root/src/PkgConfig.cpp
blob: 9a19387f5dd4fbb42e75a9805c9126dac4f98b7b (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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#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 "";
    }
    
#if OS_FAMILY == OS_FAMILY_POSIX
    Result<bool> PkgConfig::validatePkgConfigPackageVersionExists(const Dependency &dependency)
    {
        Result<bool> dependencyValidationResult = PkgConfig::validatePackageExists(dependency.name);
        if(dependencyValidationResult.isErr())
            return Result<bool>::Err(dependencyValidationResult.getErrMsg());

        Result<bool> dependencyVersionValidationResult = PkgConfig::validatePackageVersionAtLeast(dependency.name, dependency.version);
        if(dependencyVersionValidationResult.isErr())
            return Result<bool>::Err(dependencyVersionValidationResult.getErrMsg());

        return Result<bool>::Ok(true);
    }
#endif

    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);
        }
    }
    
    Result<string> PkgConfig::getDynamicLibsCflags(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 --cflags";
        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 cflags, Unknown error, exit code: ";
            errMsg += to_string(execResult.unwrap().exitCode);
            return Result<string>::Err(errMsg);
        }
    }
    
    Result<PkgConfigFlags> PkgConfig::getDynamicLibsFlags(const vector<Dependency> &libs)
    {
        PkgConfigFlags flags;
        
        Result<string> linkerFlagsResult = getDynamicLibsLinkerFlags(libs);
        if(!linkerFlagsResult)
            return Result<PkgConfigFlags>::Err(linkerFlagsResult);
        
        Result<string> cflagsResult = getDynamicLibsCflags(libs);
        if(!cflagsResult)
            return Result<PkgConfigFlags>::Err(cflagsResult);
        
        flags.linkerFlags = move(linkerFlagsResult.unwrap());
        flags.cflags = move(cflagsResult.unwrap());
        return Result<PkgConfigFlags>::Ok(flags);
    }
}
#endif // OS_FAMILY_POSIX