aboutsummaryrefslogtreecommitdiff
path: root/backend/BackendUtils.cpp
blob: 68794f52276a2f8042d5742bdab42c0789d0fd6f (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
#include "BackendUtils.hpp"
#include "../include/FileUtil.hpp"
#include "ninja/Ninja.hpp"

using namespace std;
using namespace sibs;

namespace backend
{
    bool isPathSubPathOf(const FileString &path, const FileString &subPathOf)
    {
        return _tinydir_strncmp(path.c_str(), subPathOf.c_str(), subPathOf.size()) == 0;
    }
    
    const _tinydir_char_t *sourceFileExtensions[] = { TINYDIR_STRING("c"), TINYDIR_STRING("cc"), TINYDIR_STRING("cpp"), TINYDIR_STRING("cxx"), TINYDIR_STRING("c++") };
    bool BackendUtils::isSourceFile(tinydir_file *file)
    {
        if(!file->is_reg)
            return false;

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

        return false;
    }
    
    void BackendUtils::collectSourceFiles(const _tinydir_char_t *projectPath, Ninja *ninjaProject, const SibsConfig &sibsConfig)
    {
        walkDir(projectPath, [ninjaProject, &sibsConfig](tinydir_file *file)
        {
            FileString pathNative = file->path;
            #if OS_FAMILY == OS_FAMILY_WINDOWS
            replaceChar(pathNative, L'/', L'\\');
            #endif
            if(file->is_reg)
            {
                if (isSourceFile(file))
                {
                    string filePathUtf8 = toUtf8(pathNative.c_str() + sibsConfig.getProjectPath().size());
                    ninjaProject->addSourceFile(filePathUtf8.c_str());
                }
                else
                {
                    //printf("Ignoring non-source file: %s\n", file->path + projectPath.size());
                }
            }
            else
            {
                if (!sibsConfig.getTestPath().empty() && isPathSubPathOf(pathNative.c_str(), sibsConfig.getTestPath()))
                {
                    string filePathUtf8 = toUtf8(pathNative.c_str());
                    ninjaProject->addTestSourceDir(filePathUtf8.c_str());
                }
                else if(!directoryToIgnore(pathNative, sibsConfig.getIgnoreDirs()))
                {
                    FileString projectConfPath = file->path;
                    #if OS_FAMILY == OS_FAMILY_WINDOWS
                    projectConfPath += L'\\';
                    #else
                    projectConfPath += '/';
                    #endif
                    projectConfPath += TINYDIR_STRING("project.conf");
                    
                    auto projectConfFileType = getFileType(projectConfPath.c_str());
                    if(projectConfFileType == FileType::REGULAR)
                    {
                        backend::Ninja *subProject = new backend::Ninja();
                        
                        SibsConfig *subProjectConfig = new SibsConfig(sibsConfig.getCompiler(), file->path, sibsConfig.getOptimizationLevel(), false);
                        FileString subProjectBuildPath;
                        readSibsConfig(file->path, projectConfPath, *subProjectConfig, subProjectBuildPath);

                        collectSourceFiles(file->path, subProject, *subProjectConfig);
                        ninjaProject->addSubProject(subProject, subProjectConfig, move(subProjectBuildPath));
                    }
                    else
                        collectSourceFiles(file->path, ninjaProject, sibsConfig);
                }
            }
        });
    }
}