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

using namespace std;
using namespace sibs;

static const char *cCompilerPath = nullptr;
static const char *cppCompilerPath = nullptr;
static const char *linkerPath = nullptr;

namespace backend
{
    static bool isPathSubPathOf(const FileString &path, const FileString &subPathOf)
    {
        return _tinydir_strncmp(path.c_str(), subPathOf.c_str(), subPathOf.size()) == 0;
    }

    const _tinydir_char_t *cFileExtensions[] = 
    { 
        TINYDIR_STRING("c"),
        TINYDIR_STRING("C"),
        TINYDIR_STRING("cc")
    };
    
    const _tinydir_char_t *cppFileExtensions[] = 
    { 
        TINYDIR_STRING("cp"),
        TINYDIR_STRING("cpp"),
        TINYDIR_STRING("cxx"),
        TINYDIR_STRING("c++")
    };

    sibs::FileString BackendUtils::getFileExtension(const sibs::FileString &filepath)
    {
        size_t indexOfDot = filepath.find_last_of('.');
        if(indexOfDot == sibs::FileString::npos)
            return TINYDIR_STRING("");
        
        indexOfDot += 1;
        return filepath.substr(indexOfDot);
    }

    sibs::Language BackendUtils::getFileLanguage(const _tinydir_char_t *extension)
    {
        for(const _tinydir_char_t *sourceFileExtension : cFileExtensions)
        {
            if(_tinydir_strcmp(sourceFileExtension, extension) == 0)
                return sibs::Language::C;
        }

        for(const _tinydir_char_t *sourceFileExtension : cppFileExtensions)
        {
            if(_tinydir_strcmp(sourceFileExtension, extension) == 0)
                return sibs::Language::CPP;
        }

        if(_tinydir_strcmp(TINYDIR_STRING("zig"), extension) == 0)
            return sibs::Language::ZIG;

        return sibs::Language::NONE;
    }

    sibs::Language BackendUtils::getFileLanguage(tinydir_file *file)
    {
        if(!file->is_reg)
            return sibs::Language::NONE;

        return getFileLanguage(file->extension);
    }
    
    void BackendUtils::collectSourceFiles(const _tinydir_char_t *projectPath, Ninja *ninjaProject, const SibsConfig &sibsConfig, bool recursive)
    {
        walkDir(projectPath, [ninjaProject, &sibsConfig, recursive](tinydir_file *file)
        {
            FileString pathNative = file->path;
            #if OS_FAMILY == OS_FAMILY_WINDOWS
            replaceChar(pathNative, L'/', L'\\');
            #endif
            if(file->is_reg)
            {
                sibs::Language fileLanguage = getFileLanguage(file);
                if (fileLanguage != sibs::Language::NONE)
                {
                    string filePathUtf8 = toUtf8(pathNative.c_str() + sibsConfig.getProjectPath().size());
                    ninjaProject->addSourceFile(fileLanguage, 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->setTestSourceDir(filePathUtf8.c_str());
                }
                else if(recursive && !directoryToIgnore(pathNative, sibsConfig.getIgnoreDirs()) && _tinydir_strcmp(file->name, TINYDIR_STRING("sibs-build")) != 0)
                {
                    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(!sibsConfig.isTest() && getFileType(projectConfPath.c_str()) == FileType::REGULAR)
                    {
                        backend::Ninja *subProject = new backend::Ninja();
                        
                        SibsConfig *subProjectConfig = new SibsConfig(sibsConfig.getCompiler(), file->path, sibsConfig.getOptimizationLevel(), false);
                        subProjectConfig->packaging = sibsConfig.packaging;
                        subProjectConfig->platform = sibsConfig.platform;
                        subProjectConfig->bundling = sibsConfig.bundling;
                        FileString subProjectBuildPath;
                        readSibsConfig(file->path, projectConfPath, *subProjectConfig, subProjectBuildPath);

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

    string BackendUtils::getCompilerCExecutable(Compiler compiler)
    {
        if(cCompilerPath)
            return cCompilerPath;

        char *cc = std::getenv("CC");
        if(cc)
        {
            cCompilerPath = cc;
            return cCompilerPath;
        }

        switch(compiler)
        {
            case Compiler::GCC:
                cCompilerPath = "ccache cc";
                break;
            case Compiler::MINGW_W64:
                cCompilerPath = "x86_64-w64-mingw32-cc";
                break;
            case Compiler::MSVC:
                cCompilerPath = "cl.exe";
                break;
        }
        return cCompilerPath;
    }

    string BackendUtils::getCompilerCppExecutable(Compiler compiler)
    {
        if(cppCompilerPath)
            return cppCompilerPath;

        char *cxx = std::getenv("CXX");
        if(cxx)
        {
            cppCompilerPath = cxx;
            return cppCompilerPath;
        }

        switch(compiler)
        {
            case Compiler::GCC:
                cppCompilerPath = "ccache c++";
                break;
            case Compiler::MINGW_W64:
                cppCompilerPath = "x86_64-w64-mingw32-c++";
                break;
            case Compiler::MSVC:
                cppCompilerPath = "cl.exe";
                break;
        }
        return cppCompilerPath;
    }

    string BackendUtils::getCompilerLinker(Compiler compiler)
    {
        if(linkerPath)
            return linkerPath;

        char *ar = std::getenv("AR");
        if(ar)
        {
            linkerPath = ar;
            return linkerPath;
        }

        switch(compiler)
        {
            case Compiler::GCC:
                linkerPath = "ar";
                break;
            case Compiler::MINGW_W64:
                linkerPath = "x86_64-w64-mingw32-ar";
                break;
            case Compiler::MSVC:
                linkerPath = "lib.exe";
                break;
        }
        return linkerPath;
    }

    RuntimeCompilerType BackendUtils::getCCompilerType(Compiler compiler)
    {
        RuntimeCompilerType cCompilerType = RuntimeCompilerType::NONE;
        Result<ExecResult> cCompilerVersion = exec(toFileString(getCompilerCExecutable(compiler)) + TINYDIR_STRING(" --version"));
        if(cCompilerVersion && cCompilerVersion.unwrap().exitCode == 0)
        {
            if(cCompilerVersion.unwrap().execStdout.find("Emscripten") != string::npos)
                cCompilerType = RuntimeCompilerType::EMSCRIPTEN;
            else if(cCompilerVersion.unwrap().execStdout.find("clang") != string::npos)
                cCompilerType = RuntimeCompilerType::CLANG;
            else
                cCompilerType = RuntimeCompilerType::OTHER;
        }
        return cCompilerType;
    }

    RuntimeCompilerType BackendUtils::getCppCompilerType(Compiler compiler)
    {
        RuntimeCompilerType cppCompilerType = RuntimeCompilerType::NONE;
        Result<ExecResult> cppCompilerVersion = exec(toFileString(getCompilerCppExecutable(compiler)) + TINYDIR_STRING(" --version"));
        if(cppCompilerVersion && cppCompilerVersion.unwrap().exitCode == 0)
        {
            if(cppCompilerVersion.unwrap().execStdout.find("Emscripten") != string::npos)
                cppCompilerType = RuntimeCompilerType::EMSCRIPTEN;
            else if(cppCompilerVersion.unwrap().execStdout.find("clang") != string::npos)
                cppCompilerType = RuntimeCompilerType::CLANG;
            else
                cppCompilerType = RuntimeCompilerType::OTHER;
        }
        return cppCompilerType;
    }
}