aboutsummaryrefslogtreecommitdiff
path: root/src/main.cpp
blob: 05a6a030f3ec235c256576091b526a2c30a8e289 (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
#include <cstdio>
#include "../include/FileUtil.hpp"
#include "../include/Conf.hpp"
#include "../include/Dependency.hpp"
#include "../backend/ninja/Ninja.hpp"
#include <string>
#include <cassert>

using namespace std;
using namespace sibs;

void usage()
{
    printf("Simple build system for native languages\n");
    printf("usage:\n");
    printf("    sibs <project path>\n");
    printf("option:\n");
    printf("    project path: Path containing project.conf\n");
    printf("examples:\n");
    printf("    sibs myProject");
    exit(1);
}

void validateDirectoryPath(const char *projectPath)
{
    FileType projectPathFileType = getFileType(projectPath);
    if(projectPathFileType == FileType::FILE_NOT_FOUND)
    {
        perror(projectPath);
        exit(2);
    }
    else if(projectPathFileType == FileType::REGULAR)
    {
        printf("Expected project path (%s) to be a directory, was a file", projectPath);
        exit(3);
    }
}

void validateFilePath(const char *projectConfPath)
{
    FileType projectConfFileType = getFileType(projectConfPath);
    if(projectConfFileType == FileType::FILE_NOT_FOUND)
    {
        perror(projectConfPath);
        exit(4);
    }
    else if(projectConfFileType == FileType::DIRECTORY)
    {
        printf("Expected project path (%s) to be a file, was a directory", projectConfPath);
        exit(5);
    }
}

class SibsConfig : public ConfigCallback
{
public:
    SibsConfig() : finishedProcessing(false) {}

    const string& getPackageName() const
    {
        assert(finishedProcessing);
        return packageName;
    }

    const std::vector<Dependency>& getDependencies() const
    {
        return dependencies;
    }
protected:
    void processObject(StringView name) override
    {
        currentObject = name;
        printf("Process object: %.*s\n", name.size, name.data);
    }

    void processField(StringView name, const ConfigValue &value) override
    {
        printf("Process field: %.*s, value: ", name.size, name.data);
        if(value.isSingle())
        {
            printf("\"%.*s\"", value.asSingle().size, value.asSingle().data);
        }
        else
        {
            printf("[");
            int i = 0;
            for(auto listElement : value.asList())
            {
                if(i > 0)
                    printf(", ");
                printf("\"%.*s\"", listElement.size, listElement.data);
                ++i;
            }
            printf("]");
        }
        printf("\n");

        if(currentObject.equals("package") && name.equals("name"))
        {
            if(value.isSingle())
                packageName = string(value.asSingle().data, value.asSingle().size);
            else
                throw ParserException("Expected package.name to be a single value, was a list");
        }
        else if(currentObject.equals("dependencies"))
        {
            if(value.isSingle())
            {
                // TODO: Validate version is number in correct format
                Dependency dependency;
                dependency.name = string(name.data, name.size);
                dependency.version = string(value.asSingle().data, value.asSingle().size);
                dependencies.emplace_back(dependency);
            }
            else
                throw ParserException("Expected field under dependencies to be a single value, was a list");
        }
    }

    void finished() override
    {
        finishedProcessing = true;
    }
private:
    StringView currentObject;
    string packageName;
    std::vector<Dependency> dependencies;
    bool finishedProcessing;
};

const char *sourceFileExtensions[] = { "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;
}

int main(int argc, const char **argv)
{
    if(argc != 2)
        usage();

    string projectPath = argv[1];
    validateDirectoryPath(projectPath.c_str());

    string projectConfFilePath = projectPath;
    projectConfFilePath += "/project.conf";
    validateFilePath(projectConfFilePath.c_str());

    SibsConfig sibsConfig;
    Result<bool> result = Config::readFromFile(projectConfFilePath.c_str(), sibsConfig);
    if(result.isErr())
    {
        printf("Failed to read config: %s\n", result.getErrMsg().c_str());
        exit(6);
    }

    //string projectSrcPath = projectPath + "/src";
    //validateDirectoryPath(projectSrcPath.c_str());

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

    // TODO: Create build path if it doesn't exist
    string debugBuildPath = projectPath + "/build/debug";
    string buildFilePath = debugBuildPath + "/build.ninja";
    Result<bool> buildFileResult = ninja.createBuildFile(sibsConfig.getPackageName(), sibsConfig.getDependencies(), buildFilePath.c_str());
    if(buildFileResult.isErr())
    {
        printf("Failed to build ninja file: %s\n", buildFileResult.getErrMsg().c_str());
        exit(7);
    }

    Result<bool> buildResult = ninja.build(debugBuildPath.c_str());
    if(buildResult.isErr())
    {
        exit(8);
    }

    return 0;
}