aboutsummaryrefslogtreecommitdiff
path: root/src/GlobalLib.cpp
blob: d994a2759bcde208374004b77708a7404220df98 (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
#include "../include/GlobalLib.hpp"
#include "../include/FileUtil.hpp"
#include "../backend/ninja/Ninja.hpp"
#include "../include/Conf.hpp"
#include "../include/curl.hpp"
#include "../include/Archive.hpp"

using namespace std;

namespace sibs
{
    Result<bool> GlobalLib::validatePackageExists(const string &globalLibRootDir, const string &name)
    {
        string packageDir = globalLibRootDir + "/";
        packageDir += name;
        FileType packageDirFileType = getFileType(packageDir.c_str());
        switch(packageDirFileType)
        {
            case FileType::FILE_NOT_FOUND:
            {
                string errMsg = "Global lib dependency not found: ";
                errMsg += name;
                return Result<bool>::Err(errMsg, DependencyError::DEPENDENCY_NOT_FOUND);
            }
            case FileType::REGULAR:
            {
                string errMsg = "Corrupt library directory. ";
                errMsg += 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!");
            }
        }
    }

    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;
    }

    bool isPathSubPathOf(const char *path, const string &subPathOf)
    {
        return _tinydir_strncmp(path, subPathOf.c_str(), subPathOf.size()) == 0;
    }

    Result<string> GlobalLib::getLibsLinkerFlags(const SibsConfig &parentConfig, const string &globalLibRootDir, const string &name, const string &version, LinkerFlagCallbackFunc staticLinkerFlagCallbackFunc, LinkerFlagCallbackFunc dynamicLinkerFlagCallbackFunc)
    {
        Result<bool> packageExistsResult = validatePackageExists(globalLibRootDir, name);
        if(packageExistsResult.isErr())
            return Result<string>::Err(packageExistsResult);

        string packageDir = globalLibRootDir + "/";
        packageDir += name;

        // TODO: Instead of checking if version is exact match, check if package has same major version
        // and same or newer minor version
        string foundVersion;
        walkDir(packageDir.c_str(), [&foundVersion, &version](tinydir_file *file)
        {
            if(file->is_dir)
            {
                //printf("version: %s\n", file->name);
                if(_tinydir_strcmp(version.c_str(), file->name) == 0)
                    foundVersion = file->name;
            }
        });

        if(foundVersion.empty())
            return Result<string>::Err("Global lib dependency found, but version doesn't match dependency version", DependencyError::DEPENDENCY_VERSION_NO_MATCH);

        packageDir += "/";
        packageDir += version;

        string projectConfFilePath = packageDir;
        projectConfFilePath += "/project.conf";

        FileType projectConfFileType = getFileType(projectConfFilePath.c_str());
        switch(projectConfFileType)
        {
            case FileType::FILE_NOT_FOUND:
            {
                string errMsg = "Global lib dependency found: ";
                errMsg += packageDir;
                errMsg += ", but it's missing a project.conf file";
                return Result<string>::Err(errMsg);
            }
            case FileType::DIRECTORY:
            {
                string errMsg = "Global lib dependency found: ";
                errMsg += packageDir;
                errMsg += ", but it's corrupt (Found directory instead of file)";
                return Result<string>::Err(errMsg);
            }
        }

        SibsConfig sibsConfig(packageDir, parentConfig.getOptimizationLevel());
        Result<bool> result = Config::readFromFile(projectConfFilePath.c_str(), sibsConfig);
        if(result.isErr())
            return Result<string>::Err(result.getErrMsg());

        if(sibsConfig.getPackageName().empty())
            return Result<string>::Err("project.conf is missing required field package.name");

        if(sibsConfig.getPackageType() == PackageType::EXECUTABLE)
        {
            string errMsg = "The dependency ";
            errMsg += name;
            errMsg += " is an executable. Only libraries can be dependencies";
            return Result<string>::Err(errMsg);
        }

        backend::Ninja::LibraryType libraryType;
        switch(sibsConfig.getPackageType())
        {
            case PackageType::STATIC:
                libraryType = backend::Ninja::LibraryType::STATIC;
                break;
            case PackageType::DYNAMIC:
            case PackageType::LIBRARY:
                libraryType = backend::Ninja::LibraryType::DYNAMIC;
                break;
            default:
                assert(false);
                return Result<string>::Err("Unexpected error");
        }

        backend::Ninja ninja(libraryType);
        FileWalkCallbackFunc collectSourceFiles = [&ninja, &sibsConfig, &collectSourceFiles](tinydir_file *file)
        {
            if(file->is_reg)
            {
                if (isSourceFile(file))
                {
                    ninja.addSourceFile(file->path + sibsConfig.getProjectPath().size() + 1);
                }
                else
                {
                    //printf("Ignoring non-source file: %s\n", file->path + projectPath.size());
                }
            }
            else
            {
                // TODO: If compiling without "test" option, do not add test source dir to ninja. Ninja logic will then not build tests...
                // OR I believe there is no reason to run tests in dependencies. The main projects tests should cover that?
                // But you might want to know exactly which dependency is causing issue and which part of it...
                if(!sibsConfig.getTestPath().empty() && isPathSubPathOf(file->path, sibsConfig.getTestPath()))
                    ninja.addTestSourceDir(file->path);
                else
                    walkDir(file->path, collectSourceFiles);
            }
        };
        walkDir(packageDir.c_str(), collectSourceFiles);

        // TODO: Dont do this. Unit tests wont be built. Need to call the below ninja.createBuildFile
        if(ninja.getSourceFiles().empty())
        {
            return Result<string>::Ok("No source files in dependency (header only library?)");
        }
        else
        {
            string buildPath = packageDir + "/sibs-build/";
            switch(sibsConfig.getOptimizationLevel())
            {
                case OPT_LEV_DEBUG:
                    buildPath += "debug";
                    break;
                case OPT_LEV_RELEASE:
                    buildPath += "release";
                    break;
            }

            string libPath = buildPath;
            libPath += "/lib";
            libPath += name;
            if(libraryType == backend::Ninja::LibraryType::STATIC)
            {
                libPath += ".a";
                string libPathCmd = "'";
                libPathCmd += libPath;
                libPathCmd += "'";
                staticLinkerFlagCallbackFunc(libPathCmd);
                libPath += ".a";
            }
            else
            {
                libPath += ".so";
                string libPathCmd = "'";
                libPathCmd += libPath;
                libPathCmd += "'";
                dynamicLinkerFlagCallbackFunc(libPathCmd);
            }

            // TODO: Use different directories depending on the project type, but .o build files should be in the same directory
            // no matter what project type, since they are used for executables, static/dynamic libraries
            Result<bool> createBuildDirResult = createDirectoryRecursive(buildPath.c_str());
            if(createBuildDirResult.isErr())
                return Result<string>::Err(createBuildDirResult);

            Result<bool> buildFileResult = ninja.build(sibsConfig, buildPath.c_str(), staticLinkerFlagCallbackFunc, dynamicLinkerFlagCallbackFunc);
            if (buildFileResult.isErr())
                return Result<string>::Err(buildFileResult.getErrMsg());

            return Result<string>::Ok(libPath);
        }
    }

    Result<bool> GlobalLib::downloadDependency(const Dependency &dependency)
    {
        string url = "https://github.com/DEC05EBA/";
        url += dependency.name;
        url += "/archive/";
        url += dependency.version;
        url += ".tar.gz";

        string libPath = getHomeDir();
        libPath += "/.sibs/lib/";
        libPath += dependency.name;
        libPath += "/";
        libPath += dependency.version;

        string libArchivedFilePath = getHomeDir();
        libArchivedFilePath += "/.sibs/archive/";
        libArchivedFilePath += dependency.name;
        Result<bool> createArchiveDirResult = createDirectoryRecursive(libArchivedFilePath.c_str());
        if(createArchiveDirResult.isErr())
            return createArchiveDirResult;

        libArchivedFilePath += "/";
        libArchivedFilePath += dependency.version;
        Result<bool> downloadResult = curl::downloadFile(url.c_str(), libArchivedFilePath.c_str());
        if(downloadResult.isErr())
            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.isErr())
            return createLibDirResult;

        return Archive::extract(libArchivedFilePath.c_str(), libPath.c_str());
    }
}