aboutsummaryrefslogtreecommitdiff
path: root/backend/ninja/Ninja.cpp
blob: ddc20dd4b6d2d1685f505a3166de01f5afc0165c (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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
#include <cstring>
#include "Ninja.hpp"
#include "../../include/FileUtil.hpp"
#include "../../include/Exec.hpp"
#include "../../include/PkgConfig.hpp"
#include "../../include/GlobalLib.hpp"

using namespace std;
using namespace sibs;

namespace backend
{
    string join(const vector<string> &list, const char *joinStr)
    {
        if(list.empty()) return "";
        string result;
        long stringSize = 0;
        long joinStrLen = strlen(joinStr);
        int i = 0;
        for(const string &str : list)
        {
            stringSize += str.size();
            if(!str.empty() && i > 0)
                stringSize += joinStrLen;
            ++i;
        }

        result.reserve(stringSize);

        i = 0;
        for(const string &str : list)
        {
            if(i > 0);
                result += joinStr;
            result += str;
            ++i;
        }

        return move(result);
    }

    Ninja::Ninja(LibraryType _libraryType) :
        libraryType(_libraryType)
    {

    }

    void Ninja::addSourceFile(const char *filepath)
    {
        string filePathStr = filepath ? filepath : "";
        if(filepath && !containsSourceFile(filePathStr))
        {
            sourceFiles.emplace_back(filePathStr);
            printf("Adding source file: %s\n", filepath);
        }
    }

    void Ninja::addTestSourceDir(const char *dir)
    {
        string dirStr = dir ? dir : "";
        if(dir && !containsTestSourceDir(dirStr))
        {
            testSourceDirs.emplace_back(dirStr);
            printf("Adding test source directory: %s\n", dir);
        }
    }

    void Ninja::addDependency(const std::string &binaryFile)
    {
        if(!containsDependency(binaryFile))
            binaryDependencies.emplace_back(binaryFile);
    }

    const std::vector<std::string>& Ninja::getSourceFiles() const
    {
        return sourceFiles;
    }

    bool Ninja::containsSourceFile(const string &filepath) const
    {
        for(const string &sourceFile : sourceFiles)
        {
            if(sourceFile == filepath)
                return true;
        }
        return false;
    }

    bool Ninja::containsTestSourceDir(const string &dir) const
    {
        for(const string &testSourceDir : testSourceDirs)
        {
            if(testSourceDir == dir)
                return true;
        }
        return false;
    }

    bool Ninja::containsDependency(const string &dependency) const
    {
        for(const string &binaryDependency : binaryDependencies)
        {
            if(binaryDependency == dependency)
                return true;
        }
        return false;
    }

    Result<bool> 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);
    }

    // TODO: First check if pkg-config is installed. If it's not, only check dependencies that exists in the dependencies sub directory.
    // If pkg-config is installed and dependency is not installed, check in dependencies sub directory.
    Result<bool> Ninja::getLinkerFlags(const SibsConfig &config, LinkerFlagCallbackFunc staticLinkerFlagCallbackFunc, LinkerFlagCallbackFunc dynamicLinkerFlagCallback) const
    {
        const vector<Dependency> &dependencies = config.getDependencies();
        if(dependencies.empty()) return Result<bool>::Ok(true);

        string globalLibDir = getHomeDir();
        globalLibDir += "/.sibs/lib";
        Result<bool> createGlobalLibDirResult = createDirectoryRecursive(globalLibDir.c_str());
        if(createGlobalLibDirResult.isErr())
            return createGlobalLibDirResult;

        vector<Dependency> pkgConfigDependencies;
        vector<Dependency> globalLibDependencies;
        for(const Dependency &dependency : dependencies)
        {
            Result<bool> pkgConfigDependencyValidation = validatePkgConfigPackageVersionExists(dependency);
            if(pkgConfigDependencyValidation.isOk())
            {
                pkgConfigDependencies.push_back(dependency);
            }
            else
            {
                globalLibDependencies.push_back(dependency);
            }
        }

        Result<string> pkgConfigLinkerFlagsResult = PkgConfig::getDynamicLibsLinkerFlags(pkgConfigDependencies);
        if(pkgConfigLinkerFlagsResult.isErr())
        {
            printf("%s, using global lib...\n", pkgConfigLinkerFlagsResult.getErrMsg().c_str());
            globalLibDependencies.reserve(globalLibDependencies.size() + pkgConfigDependencies.size());
            for(const Dependency &pkgConfigDependency : pkgConfigDependencies)
            {
                globalLibDependencies.push_back(pkgConfigDependency);
            }
            pkgConfigDependencies.clear();
        }
        else
        {
            if(!pkgConfigLinkerFlagsResult.unwrap().empty())
                dynamicLinkerFlagCallback(pkgConfigLinkerFlagsResult.unwrap());
        }

        for(const Dependency &globalLibDependency : globalLibDependencies)
        {
            printf("Dependency %s is missing from pkg-config, trying global lib\n", globalLibDependency.name.c_str());
            Result<string> globalLibLinkerFlagsResult = GlobalLib::getLibsLinkerFlags(config, globalLibDir, globalLibDependency.name, globalLibDependency.version, staticLinkerFlagCallbackFunc, dynamicLinkerFlagCallback);
            if(globalLibLinkerFlagsResult.isErr())
            {
                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);
                    if(downloadDependencyResult.isErr())
                        return downloadDependencyResult;

                    globalLibLinkerFlagsResult = GlobalLib::getLibsLinkerFlags(config, globalLibDir, globalLibDependency.name, globalLibDependency.version, staticLinkerFlagCallbackFunc, dynamicLinkerFlagCallback);
                    if(globalLibLinkerFlagsResult.isErr())
                        return Result<bool>::Err(globalLibLinkerFlagsResult);
                }
                else
                {
                    return Result<bool>::Err(globalLibLinkerFlagsResult);
                }
            }
        }

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

    Result<bool> Ninja::build(const SibsConfig &config, const char *savePath, LinkerFlagCallbackFunc staticLinkerFlagCallbackFunc, LinkerFlagCallbackFunc dynamicLinkerFlagCallback)
    {
        // TODO: Do not quit here if no source files are provided. The source-less project could have dependencies
        if(sourceFiles.empty())
            return Result<bool>::Err("No source files provided");

        Result<bool> createBuildDirResult = createDirectoryRecursive(savePath);
        if(createBuildDirResult.isErr())
            return createBuildDirResult;

        string ninjaBuildFilePath = savePath;
        ninjaBuildFilePath += "/build.ninja";

        string result;
        result.reserve(16384);

        string globalIncDir = getHomeDir();
        globalIncDir += "/.sibs/lib";

        result += "globalIncDir = '-I";
        result += globalIncDir;
        result += "'";
        for(const auto &includeDir : config.getIncludeDirs())
        {
            result += " '-I../../";
            result += includeDir;
            result += "'";
        }
        result += "\n\n";

        string buildJob;
        switch(libraryType)
        {
            case LibraryType::EXECUTABLE:
            {
                result += "rule cpp_COMPILER\n";
                result += "  command = ccache c++ $ARGS -c $in -o $out\n\n";

                result += "rule cpp_BUILD_EXEC\n";
                result += "  command = ccache c++ $ARGS -o $out $in $LINK_ARGS $aliasing\n\n";
                buildJob = "cpp_BUILD_EXEC";
                break;
            }
            case LibraryType::STATIC:
            {
                result += "rule cpp_COMPILER\n";
                result += "  command = ccache c++ $ARGS -c -fPIC $in -o $out\n\n";

                result += "rule cpp_BUILD_STATIC\n";
                result += "  command = ar rcs lib";
                result += config.getPackageName();
                result += ".a";
                result += " $in\n\n";
                buildJob = "cpp_BUILD_STATIC";
                break;
            }
            case LibraryType::DYNAMIC:
            {
                result += "rule cpp_COMPILER\n";
                result += "  command = ccache c++ $ARGS -c -fPIC $in -o $out\n\n";

                // --whole-archive
                result += "rule cpp_BUILD_DYNAMIC\n";
                result += "  command = ccache c++ $in -shared -o $out $LINK_ARGS $aliasing\n\n";
                buildJob = "cpp_BUILD_DYNAMIC";
                break;
            }
            default:
                assert(false);
                return Result<bool>::Err("Unexpected error");
        }

        string optimizationFlags;
        switch(config.getOptimizationLevel())
        {
            case OPT_LEV_DEBUG:
                optimizationFlags = "'-O0'";
                break;
            case OPT_LEV_RELEASE:
                optimizationFlags = "'-O3' '-DNDEBUG'";
                break;
        }

        vector<string> objectNames;
        objectNames.reserve(sourceFiles.size());
        for(const string &sourceFile : sourceFiles)
        {
            //string sourceFileEncoded = sourceFile;
            //replace(sourceFileEncoded, '/', '@');
            string objectName = config.getPackageName() + "@exe/" + sourceFile + ".o";
            result += "build ";
            result += objectName;
            result += ": cpp_COMPILER ../../";
            result += sourceFile;
            result += "\n";
            result += "  ARGS = $globalIncDir '-I" + config.getPackageName() + "@exe' '-I..' '-fdiagnostics-color=always' '-pipe' '-D_FILE_OFFSET_BITS=64' '-Wall' '-Winvalid-pch' '-Wnon-virtual-dtor' " + optimizationFlags + " '-g'\n\n";
            objectNames.emplace_back(objectName);
        }

        // TODO: Allow configuring default linking flags. Maybe have `package.useThreads = false` to disable this flag
        string allLinkerFlags = "-pthread";

        // TODO: Somehow check loading order, because it has to be correct to work.. Or does it for dynamic libraries?
        // Anyways it's required for static libraries (especially on Windows)
        for(const string &binaryDependency : binaryDependencies)
        {
            allLinkerFlags += " ";
            allLinkerFlags += binaryDependency;
        }

        if(!staticLinkerFlagCallbackFunc || libraryType == LibraryType::DYNAMIC)
        {
            staticLinkerFlagCallbackFunc = [&allLinkerFlags](const string &linkerFlag)
            {
                allLinkerFlags += " ";
                allLinkerFlags += linkerFlag;
            };
        }

        // TODO: If project contains no source files, then we shouldn't override this function
        dynamicLinkerFlagCallback = [&allLinkerFlags](const string &linkerFlag)
        {
            allLinkerFlags += " ";
            allLinkerFlags += linkerFlag;
        };

        Result<bool> linkerFlags = getLinkerFlags(config, staticLinkerFlagCallbackFunc, dynamicLinkerFlagCallback);
        if(linkerFlags.isErr())
            return Result<bool>::Err(linkerFlags.getErrMsg());

        string projectGeneratedBinary = allLinkerFlags;
        projectGeneratedBinary += " '";
        projectGeneratedBinary += savePath;
        projectGeneratedBinary += "/";
        switch(libraryType)
        {
            case LibraryType::EXECUTABLE:
            {
                result += "build ";
                result += config.getPackageName();
                result += ": " + buildJob + " ";
                result += join(objectNames, " ");
                result += "\n";
                result += "  LINK_ARGS = '-Wl,--no-undefined,--as-needed' ";
                if(!allLinkerFlags.empty())
                {
                    result += allLinkerFlags;
                }
                result += "\n\n";
                projectGeneratedBinary += config.getPackageName();
                break;
            }
            case LibraryType::STATIC:
            {
                result += "build ";
                result += config.getPackageName();
                result += ": " + buildJob + " ";
                result += join(objectNames, " ");
                result += "\n\n";
                projectGeneratedBinary += config.getPackageName() + ".a";
                break;
            }
            case LibraryType::DYNAMIC:
            {
                result += "build lib";
                result += config.getPackageName();
                result += ".so: " + buildJob + " ";
                result += join(objectNames, " ");
                result += "\n";
                result += "  LINK_ARGS = '-Wl,--no-undefined,--as-needed' ";
                if(!allLinkerFlags.empty())
                {
                    result += allLinkerFlags;
                    //result += " '-Wl,--no-whole-archive'";
                }
                result += "\n\n";
                projectGeneratedBinary += "lib" + config.getPackageName() + ".so";
                break;
            }
            default:
                assert(false);
                return Result<bool>::Err("Unexpected error");
        }
        projectGeneratedBinary += "'";

        Result<bool> fileOverwriteResult = sibs::fileOverwrite(ninjaBuildFilePath.c_str(), sibs::StringView(result.data(), result.size()));
        if(fileOverwriteResult.isErr())
            return fileOverwriteResult;

        printf("Created ninja build file: %s\n", ninjaBuildFilePath.c_str());

        Result<bool> buildResult = build(savePath);
        if(!buildResult)
            return buildResult;

        Result<bool> buildTestResult = buildTests(projectGeneratedBinary);
        if(!buildTestResult)
            return buildTestResult;

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

    const char *sourceFileExtensions[] = { "c", "cc", "cpp", "cxx" };
    bool isSourceFile(tinydir_file *file)
    {
        if(!file->is_reg)
            return false;

        for(const char *sourceFileExtension : sourceFileExtensions)
        {
            if(_tinydir_strcmp(sourceFileExtension, file->extension) == 0)
                return true;
        }

        return false;
    }

    Result<bool> Ninja::buildTests(const std::string &projectGeneratedBinary)
    {
        if(testSourceDirs.empty())
            return Result<bool>::Ok(true);

        // TODO: Include executable as dependency??? or compile project as dynamic library even if it's not a library
        if(libraryType == LibraryType::EXECUTABLE)
            return Result<bool>::Err("Unit tests are currently only supported in projects that compile to static/dynamic library");

        for(const string &testSourceDir : testSourceDirs)
        {
            string projectConfFilePath = testSourceDir;
            projectConfFilePath += "/project.conf";

            FileType projectConfFileType = getFileType(projectConfFilePath.c_str());
            SibsTestConfig sibsTestConfig(testSourceDir);
            if(projectConfFileType == FileType::REGULAR)
            {
                Result<bool> result = Config::readFromFile(projectConfFilePath.c_str(), sibsTestConfig);
                if(!result)
                    return result;
            }

            backend::Ninja ninja;
            ninja.addDependency(projectGeneratedBinary);
            walkDirFilesRecursive(testSourceDir.c_str(), [&ninja, &sibsTestConfig](tinydir_file *file)
            {
                if (isSourceFile(file))
                {
                    ninja.addSourceFile(file->path + sibsTestConfig.getProjectPath().size() + 1);
                }
                else
                {
                    //printf("Ignoring non-source file: %s\n", file->path + projectPath.size());
                }
            });

            if(!ninja.getSourceFiles().empty())
            {
                string debugBuildPath = testSourceDir + "/sibs-build/debug";
                Result<bool> buildFileResult = ninja.build(sibsTestConfig, debugBuildPath.c_str());
                if (!buildFileResult)
                    return buildFileResult;

                Result<bool> buildResult = ninja.build(debugBuildPath.c_str());
                if (!buildResult)
                    return buildResult;
            }
        }

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

    Result<bool> Ninja::build(const char *buildFilePath)
    {
        string command = "ninja -C '";
        command += buildFilePath;
        command += "'";
        Result<ExecResult> execResult = exec(command.c_str(), true);
        if(execResult.isOk())
        {
            if(execResult.unwrap().exitCode == 0)
                return Result<bool>::Ok(true);
            else
                return Result<bool>::Err(execResult.unwrap().execStdout);
        }
        else
            return Result<bool>::Err(execResult.getErrMsg());
    }
}