aboutsummaryrefslogtreecommitdiff
path: root/src/GlobalLib.cpp
blob: 1a795bf0d48d6b4c75290d0590ef520f79aefad3 (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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#include "../include/GlobalLib.hpp"
#include "../include/FileUtil.hpp"
#include "../backend/BackendUtils.hpp"
#include "../backend/ninja/Ninja.hpp"
#include "../include/Conf.hpp"
#include "../include/curl.hpp"
#include "../include/Archive.hpp"
#include "../include/CmakeModule.hpp"
#include "../include/Dependency.hpp"
#include "../include/GitRepository.hpp"
#include "../include/VersionParser.hpp"

using namespace std;

namespace sibs
{
    Result<bool> GlobalLib::validatePackageExists(const FileString &globalLibRootDir, const std::string &name)
    {
        FileString packageDir = globalLibRootDir + TINYDIR_STRING("/");
#if OS_FAMILY == OS_FAMILY_POSIX
        packageDir += name;
#else
        packageDir += utf8To16(name);
#endif
        FileType packageDirFileType = getFileType(packageDir.c_str());
        switch(packageDirFileType)
        {
            case FileType::FILE_NOT_FOUND:
            {
                string errMsg = "Global lib dependency not found: ";
                errMsg += toUtf8(packageDir);
                return Result<bool>::Err(errMsg, DependencyError::DEPENDENCY_NOT_FOUND);
            }
            case FileType::REGULAR:
            {
                string errMsg = "Corrupt library directory. ";
                errMsg += toUtf8(packageDir);
                errMsg += " is a file, expected it to be a directory";
                return Result<bool>::Err(errMsg);
            }
            case FileType::DIRECTORY:
            {
                return Result<bool>::Ok(true);
            }
            default:
            {
                return Result<bool>::Err("Unexpected error!");
            }
        }
    }
    
    Result<bool> GlobalLib::getLibs(const std::vector<PackageListDependency*> &libs, const SibsConfig &parentConfig, const FileString &globalLibRootDir, LinkerFlagCallbackFunc staticLinkerFlagCallbackFunc, LinkerFlagCallbackFunc dynamicLinkerFlagCallbackFunc, GlobalIncludeDirCallbackFunc globalIncludeDirCallback)
    {
        for(PackageListDependency *globalLibDependency : libs)
        {
            if(!parentConfig.packaging)
                printf("Dependency %s in version range %s is missing from pkg-config, trying global lib\n", globalLibDependency->name.c_str(), globalLibDependency->version.toString().c_str());
            Result<bool> globalLibLinkerFlagsResult = GlobalLib::getLibsLinkerFlags(parentConfig, globalLibRootDir, globalLibDependency->name, globalLibDependency->version, staticLinkerFlagCallbackFunc, dynamicLinkerFlagCallbackFunc, globalIncludeDirCallback);
            if(!globalLibLinkerFlagsResult)
            {
                if(globalLibLinkerFlagsResult.getErrorCode() == GlobalLib::DependencyError::DEPENDENCY_NOT_FOUND || globalLibLinkerFlagsResult.getErrorCode() == GlobalLib::DependencyError::DEPENDENCY_VERSION_NO_MATCH)
                {
                    printf("Dependency not found in global lib, trying to download from github\n");
                    // TODO: Download several dependencies at the same time by adding them to a list
                    // and then iterate them and download them all using several threads.
                    // All dependecies should be downloaded at the same time, this includes dependencies of dependencies.
                    // If a dependency is missing, fail build BEFORE downloading dependencies and before compiling anything.
                    // You do not want to possibly wait several minutes only for build to fail when there is no compilation error.

                    // TODO: If return error is invalid url, then the message should be converted to
                    // invalid package name/version. A check should be done if it is the name or version
                    // that is invalid.
                    Result<bool> downloadDependencyResult = GlobalLib::downloadDependency(globalLibDependency, parentConfig.platform);
                    if(!downloadDependencyResult)
                        return downloadDependencyResult;

                    globalLibLinkerFlagsResult = GlobalLib::getLibsLinkerFlags(parentConfig, globalLibRootDir, globalLibDependency->name, globalLibDependency->version, staticLinkerFlagCallbackFunc, dynamicLinkerFlagCallbackFunc, globalIncludeDirCallback);
                    if(!globalLibLinkerFlagsResult)
                        return globalLibLinkerFlagsResult;
                }
                else
                {
                    return globalLibLinkerFlagsResult;
                }
            }
        }
        
        for(GitDependency *gitDependency : parentConfig.getGitDependencies())
        {
            Result<bool> gitLibLinkerFlagsResult = GlobalLib::getLibsLinkerFlags(parentConfig, globalLibRootDir, gitDependency, staticLinkerFlagCallbackFunc, dynamicLinkerFlagCallbackFunc, globalIncludeDirCallback);
            if(!gitLibLinkerFlagsResult)
            {
                if(gitLibLinkerFlagsResult.getErrorCode() == GlobalLib::DependencyError::DEPENDENCY_NOT_FOUND)
                {
                    printf("Dependency %s not found in global lib, trying to download from git\n", gitDependency->name.c_str());
                    // TODO: Download several dependencies at the same time by adding them to a list
                    // and then iterate them and download them all using several threads.
                    // All dependecies should be downloaded at the same time, this includes dependencies of dependencies.
                    // If a dependency is missing, fail build BEFORE downloading dependencies and before compiling anything.
                    // You do not want to possibly wait several minutes only for build to fail when there is no compilation error.

                    // TODO: If return error is invalid url, then the message should be converted to
                    // invalid package name/version. A check should be done if it is the name or version
                    // that is invalid.
                    Result<bool> downloadDependencyResult = GlobalLib::downloadDependency(gitDependency, parentConfig.platform);
                    if(!downloadDependencyResult)
                        return downloadDependencyResult;

                    gitLibLinkerFlagsResult = GlobalLib::getLibsLinkerFlags(parentConfig, globalLibRootDir, gitDependency, staticLinkerFlagCallbackFunc, dynamicLinkerFlagCallbackFunc, globalIncludeDirCallback);
                    if(!gitLibLinkerFlagsResult)
                        return gitLibLinkerFlagsResult;
                }
                else
                {
                    return gitLibLinkerFlagsResult;
                }
            }
        }
        return Result<bool>::Ok(true);
    }

    Result<bool> GlobalLib::getLibsLinkerFlags(const SibsConfig &parentConfig, const FileString &globalLibRootDir, const std::string &name, const PackageVersionRange &versionRange, LinkerFlagCallbackFunc staticLinkerFlagCallbackFunc, LinkerFlagCallbackFunc dynamicLinkerFlagCallbackFunc, GlobalIncludeDirCallbackFunc globalIncludeDirCallback)
    {
        Result<bool> packageExistsResult = validatePackageExists(globalLibRootDir, name);
        if (packageExistsResult.isErr())
            return packageExistsResult;

#if OS_FAMILY == OS_FAMILY_POSIX
        FileString namePlatformNative = name;
#else
        FileString namePlatformNative = utf8To16(name);
#endif

        FileString packageDir = globalLibRootDir + TINYDIR_STRING("/");
        packageDir += namePlatformNative;

        FileString foundVersion;
        walkDir(packageDir.c_str(), [&foundVersion, &versionRange](tinydir_file *file)
        {
            if(file->is_dir)
            {
                string versionUtf8 = toUtf8(file->name);
                Result<PackageVersion> versionResult = parsePackageVersion({ versionUtf8.data(), versionUtf8.size() }, nullptr);
                if(versionResult && versionRange.isInRange(versionResult.unwrap()))
                {
                    foundVersion = file->name;
                    return false;
                }
            }
            return true;
        });

        if(foundVersion.empty())
            return Result<bool>::Err("Global lib dependency found, but version isn't in range of version", DependencyError::DEPENDENCY_VERSION_NO_MATCH);

        packageDir += TINYDIR_STRING("/");
        packageDir += foundVersion;

        return GlobalLib::getLibsLinkerFlagsCommon(parentConfig, packageDir, name, staticLinkerFlagCallbackFunc, dynamicLinkerFlagCallbackFunc, globalIncludeDirCallback);
    }
    
    Result<bool> GlobalLib::getLibsLinkerFlags(const SibsConfig &parentConfig, const FileString &globalLibRootDir, GitDependency *gitDependency, LinkerFlagCallbackFunc staticLinkerFlagCallbackFunc, LinkerFlagCallbackFunc dynamicLinkerFlagCallbackFunc, GlobalIncludeDirCallbackFunc globalIncludeDirCallback)
    {
        Result<bool> packageExistsResult = validatePackageExists(globalLibRootDir, gitDependency->name);
        if (packageExistsResult.isErr())
            return packageExistsResult;

#if OS_FAMILY == OS_FAMILY_POSIX
        FileString namePlatformNative = gitDependency->name;
        FileString versionPlatformNative = gitDependency->revision;
#else
        FileString namePlatformNative = utf8To16(gitDependency->name);
        FileString versionPlatformNative = utf8To16(gitDependency->revision);
#endif

        FileString packageDir = globalLibRootDir + TINYDIR_STRING("/");
        packageDir += namePlatformNative;
        packageDir += TINYDIR_STRING("/");
        packageDir += versionPlatformNative;
        
        // TODO: Check path is valid git repository by using git_repository_open_ext
        
        // TODO: Pull if revision == HEAD, fail build if there are conflicts.
        // TODO: When building a sibs project, put a symlink in libs directory.
        // This allows you to have dependency on a project and make changes to it without pushing
        // to remote before the dependant project can see the changes.
        //GitRepository gitRepo;
        //gitRepo.pull(gitDependency, packageDir);

        return GlobalLib::getLibsLinkerFlagsCommon(parentConfig, packageDir, gitDependency->name, staticLinkerFlagCallbackFunc, dynamicLinkerFlagCallbackFunc, globalIncludeDirCallback);
    }
    
    Result<bool> GlobalLib::getLibsLinkerFlagsCommon(const SibsConfig &parentConfig, const FileString &packageDir, const string &dependencyName, LinkerFlagCallbackFunc staticLinkerFlagCallbackFunc, LinkerFlagCallbackFunc dynamicLinkerFlagCallbackFunc, GlobalIncludeDirCallbackFunc globalIncludeDirCallback)
    {
        FileString projectConfFilePath = packageDir;
        projectConfFilePath += TINYDIR_STRING("/project.conf");

        FileType projectConfFileType = getFileType(projectConfFilePath.c_str());
        if(projectConfFileType != FileType::REGULAR)
        {
            string errMsg = "Dependency not found: ";
            errMsg += toUtf8(packageDir);
            return Result<bool>::Err(errMsg, DependencyError::DEPENDENCY_NOT_FOUND);
        }
        
        SibsConfig sibsConfig(parentConfig.getCompiler(), packageDir, parentConfig.getOptimizationLevel(), false);
        sibsConfig.platform = parentConfig.platform;
        sibsConfig.packaging = parentConfig.packaging;
        sibsConfig.bundling = parentConfig.bundling;
        Result<bool> result = Config::readFromFile(projectConfFilePath.c_str(), sibsConfig);
        if (result.isErr())
            return result;

        if(sibsConfig.getPackageType() == PackageType::EXECUTABLE)
        {
            string errMsg = "The dependency ";
            errMsg += dependencyName;
            errMsg += " is an executable. Only libraries can be dependencies";
            return Result<bool>::Err(errMsg);
        }
        
        FileString buildPath = packageDir + TINYDIR_STRING("/sibs-build/") + toFileString(asString(sibsConfig.platform)) + TINYDIR_STRING("/");
        switch (sibsConfig.getOptimizationLevel())
        {
            case OPT_LEV_DEBUG:
            {
                buildPath += TINYDIR_STRING("debug");
                // TODO: Check if this dependency is static or dynamic and decide which lib path to use from that
                for(const string &staticLib : sibsConfig.getDebugStaticLibs())
                {
                    string staticLibCmd = "\"";
                    staticLibCmd += staticLib;
                    staticLibCmd += "\"";
                    staticLinkerFlagCallbackFunc(staticLibCmd);
                }
                break;
            }
            case OPT_LEV_RELEASE:
            {
                buildPath += TINYDIR_STRING("release");
                // TODO: Check if this dependency is static or dynamic and decide which lib path to use from that
                for (const string &staticLib : sibsConfig.getReleaseStaticLibs())
                {
                    string staticLibCmd = "\"";
                    staticLibCmd += staticLib;
                    staticLibCmd += "\"";
                    staticLinkerFlagCallbackFunc(staticLibCmd);
                }
                break;
            }
        }
        
        if(sibsConfig.shouldUseCmake())
        {
            CmakeModule cmakeModule;
            return cmakeModule.compile(sibsConfig, buildPath, staticLinkerFlagCallbackFunc, dynamicLinkerFlagCallbackFunc, globalIncludeDirCallback);
        }
        else
        {
            backend::Ninja ninja;
            backend::BackendUtils::collectSourceFiles(packageDir.c_str(), &ninja, sibsConfig);
            return ninja.build(sibsConfig, buildPath.c_str(), staticLinkerFlagCallbackFunc, dynamicLinkerFlagCallbackFunc, globalIncludeDirCallback);
        }
    }

    Result<bool> GlobalLib::downloadDependency(PackageListDependency *dependency, Platform platform)
    {
        Result<PackageMetadata> packageResult = Package::getPackage(dependency->name.c_str(), dependency->version, platform);
        if(!packageResult)
            return Result<bool>::Err(packageResult);
        
        const PackageMetadata &package = packageResult.unwrap();

        Result<FileString> libPathResult = getHomeDir();
        if (!libPathResult)
            return Result<bool>::Err(libPathResult);
        FileString libPath = libPathResult.unwrap();
        libPath += TINYDIR_STRING("/.cache/sibs/lib/");
        libPath += toFileString(asString(platform));
        libPath += TINYDIR_STRING("/");
        libPath += toFileString(dependency->name);
        libPath += TINYDIR_STRING("/");
        libPath += toFileString(package.version.toString());

        FileString libArchivedFilePath = libPathResult.unwrap();
        libArchivedFilePath += TINYDIR_STRING("/.cache/sibs/archive/");
        libArchivedFilePath += toFileString(dependency->name);
        Result<bool> createArchiveDirResult = createDirectoryRecursive(libArchivedFilePath.c_str());
        if(!createArchiveDirResult)
            return createArchiveDirResult;

        libArchivedFilePath += TINYDIR_STRING("/");
        libArchivedFilePath += toFileString(package.version.toString());
        Result<bool> downloadResult = curl::downloadFile(package.urls[0].c_str(), libArchivedFilePath.c_str());
        if(!downloadResult)
            return downloadResult;

        // Create build path. This is done here because we dont want to create it if download fails
        Result<bool> createLibDirResult = createDirectoryRecursive(libPath.c_str());
        if(!createLibDirResult)
            return createLibDirResult;

        Result<bool> archiveExtractResult = Archive::extract(libArchivedFilePath.c_str(), libPath.c_str());
        // We have extracted the archive, we dont need to cache it. If remove fails, it doesn't really matter, user can remove it himself
#if OS_FAMILY == OS_FAMILY_POSIX
        remove(libArchivedFilePath.c_str());
#else
		_wremove(libArchivedFilePath.c_str());
#endif
        return archiveExtractResult;
    }
    
    Result<bool> GlobalLib::downloadDependency(GitDependency *dependency, Platform platform)
    {
        Result<FileString> libPathResult = getHomeDir();
        if (!libPathResult)
            return Result<bool>::Err(libPathResult);
        FileString libPath = libPathResult.unwrap();
        libPath += TINYDIR_STRING("/.cache/sibs/lib/");
        libPath += toFileString(asString(platform));
        libPath += TINYDIR_STRING("/");
        libPath += toFileString(dependency->name);
        
        // We dont care if the directory already exists. Nothing will happen if it does
        Result<bool> createLibDirResult = createDirectoryRecursive(libPath.c_str());
        if(!createLibDirResult)
            return createLibDirResult;
        
        libPath += TINYDIR_STRING("/");
        libPath += toFileString(dependency->revision);

        GitRepository gitRepo;
        return gitRepo.clone(dependency, libPath);
    }
}