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

using namespace std;
using namespace sibs;

// TODO: Fail if multiple versions of the same dependency is used
// as linking will fail because of multiple definitions of the same thing

// TODO: Detect recursive dependencies and give error.

void usage()
{
    printf("usage: sibs COMMAND\n\n");
    printf("Simple Build System for Native Languages\n\n");
    printf("Options:\n");
    printf("  -p\t\tPath to project (directory that contains main project.conf file)\n");
    printf("  -h\t\tPrint this help menu\n\n");
    printf("Commands:\n");
    printf("  build\t\tBuild a project that contains a project.conf file\n");
    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);
    }
}

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

void build(string projectPath)
{
    validateDirectoryPath(projectPath.c_str());
    if(projectPath.back() != '/')
        projectPath += "/";

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

    if(sibsConfig.getPackageName().empty())
    {
        printf("project.conf is missing required field package.name\n");
        exit(10);
    }

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

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

    // TODO: Create build path if it doesn't exist
    string debugBuildPath = projectPath + "/build/debug";
    Result<bool> buildFileResult = ninja.createBuildFile(sibsConfig.getPackageName(), sibsConfig.getDependencies(), debugBuildPath.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);
    }
}

int main(int argc, const char **argv)
{
    string projectPath;
    string command;
    for(int i = 1; i < argc; ++i)
    {
        const char *arg = argv[i];
        if(arg[0] == '-')
        {
            // Option
            if(strcmp(arg, "-p") == 0)
            {
                if(!projectPath.empty())
                {
                    printf("-p option defined twice, should only be defined once\n");
                    exit(1);
                }

                if(i < argc - 1)
                {
                    projectPath = argv[i + 1];
                    ++i;
                }
                else
                {
                    printf("Expected project path after option -p\n");
                    exit(1);
                }
            }
            else if(strcmp(arg, "-h") == 0)
            {
                usage();
            }
            else
            {
                printf("Invalid option %s, type sibs -h to see valid options\n", arg);
                exit(1);
            }
        }
        else
        {
            if(!command.empty())
            {
                printf("Found command twice. First as %s, then as %s\n", command.c_str(), arg);
                exit(1);
            }
            command = arg;
        }
    }

    if(projectPath.empty())
    {
        Result<string> projectPathResult = getCwd();
        if(projectPathResult.isErr())
        {
            printf("%s\n", projectPathResult.getErrMsg().c_str());
            exit(11);
        }
        projectPath = projectPathResult.unwrap();
    }

    if(command == "build")
        build(projectPath);
    else if(command.empty())
    {
        printf("No command provided, type sibs -h to see valid commands\n");
        exit(1);
    }
    else
    {
        printf("Invalid command %s, expected: build\n", command.c_str());
        exit(1);
    }

    return 0;
}