aboutsummaryrefslogtreecommitdiff
path: root/src/Ninja.cpp
blob: 00d740d1eb035199481ad111782a55ab7a94b0d8 (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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#include "../include/ninja/Ninja.hpp"
#include <cassert>

namespace ninja
{
#if defined(_WIN32) || defined(_WIN64)
    static std::string escape_arg(const std::string &arg) {
        std::string escaped = "\"";
        for(char c : arg) {
            if(c == '"') {
                escaped += "\"\"";
            } else {
                escaped += c;
            }
        }
        escaped += "\"";
        return escaped;
    }
#else
    static std::string escape_arg(const std::string &arg) {
        std::string escaped = "\"";
        for(char c : arg) {
            if(c == '"') {
                escaped += "\\\"";
            } else if(c == '\\') {
                escaped += "\\\\";
            } else {
                escaped += c;
            }
        }
        escaped += "\"";
        return escaped;
    }
#endif

    static std::string joinArgs(const std::vector<NinjaArg> &args)
    {
        std::string result;
        for(const NinjaArg &arg : args)
        {
            if(!result.empty())
                result += ' ';

            NinjaArg::Type argType = arg.type;
            if(!arg.arg.empty() && arg.arg[0] == '$')
                argType = NinjaArg::Type::RAW;
            
            switch(argType)
            {
                case NinjaArg::Type::NONE:
                    break;
                case NinjaArg::Type::VALUE:
                {
                    result += escape_arg(arg.arg);
                    break;
                }
                case NinjaArg::Type::VARIABLE:
                {
                    result += '$' + arg.arg;
                    break;
                }
                case NinjaArg::Type::RAW:
                {
                    result += arg.arg;
                    break;
                }
            }
        }
        return result;
    }

    static bool isAlpha(char c)
    {
        return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
    }

    static bool isNum(char c)
    {
        return c >= '0' && c <= '9';
    }

    static bool isAlnum(char c)
    {
        return isAlpha(c) || isNum(c);
    }

    static bool isValidName(const std::string &name)
    {
        if(name.empty())
            return false;

        if(name.size() > 100)
            return false;

        if(!isAlpha(name[0]) && name[0] != '_')
            return false;

        for(size_t i = 1; i < name.size(); ++i)
        {
            if(!isAlnum(name[i]) && name[i] != '_')
                return false;
        }

        return true;
    }

    static std::string escape(const std::string &str)
    {
        std::string result;
        result.reserve(str.size());
        for(char c : str)
        {
            if(c == '\n')
                result += "$\n";
            else if(c == '$')
                result += "$$";
            else if(c == ' ')
                result += "$ ";
            else if(c == ':')
                result += "$:";
            else
                result += c;
        }
        return result;
    }

    static std::string combine_escape(const std::vector<std::string> &strs)
    {
        std::string result;
        for(const std::string &str : strs)
        {
            if(!result.empty())
                result += ' ';
            result += escape(str);
        }
        return result;
    }

    NinjaVariable::NinjaVariable(const std::string &_name) : 
        name(_name)
    {
        if(!isValidName(name))
            throw NinjaException(NinjaError::INVALID_NAME, "Invalid variable name, has to match [a-zA-Z_]{1}[a-zA-Z0-9_]{0,98}");
    }

    NinjaRule::NinjaRule(NinjaBuildFile *_buildFile, const std::string &_name, const std::string &_command) : 
        name(_name),
        command(_command),
        buildFile(_buildFile)
    {
        assert(buildFile);
    }

    NinjaBuildFile::~NinjaBuildFile()
    {
        for(auto &rule : rules)
        {
            delete rule.second;
        }

        for(NinjaBuild *build : builds)
        {
            delete build;
        }
    }

    NinjaBuild* NinjaRule::build(const std::string &in, const std::string &out, const std::vector<ninja::NinjaArgValue> &additionalArgs, const std::vector<NinjaBuild*> &dependsOnBuilds)
    {
        return buildFile->build(this, in, out, additionalArgs, dependsOnBuilds);
    }

    NinjaBuild* NinjaRule::build(const std::vector<std::string> &in, const std::string &out, const std::vector<ninja::NinjaArgValue> &additionalArgs, const std::vector<NinjaBuild*> &dependsOnBuilds)
    {
        return buildFile->build(this, in, out, additionalArgs, dependsOnBuilds);
    }

    void NinjaBuildFile::defineGlobalVariable(const std::string &name, const std::string &value)
    {
        if(!isValidName(name))
            throw NinjaException(NinjaError::INVALID_NAME, "Invalid global variable name, has to match [a-zA-Z_]{1}[a-zA-Z0-9_]{0,98}");

        auto it = globalVariables.find(name);
        if(it != globalVariables.end())
            throw NinjaException(NinjaError::VARIABLE_ALREADY_DEFINED, "Global variable already defined: " + name);
        globalVariables[name] = value;
    }

    NinjaRule* NinjaBuildFile::createRule(const std::string &name, const std::vector<NinjaArg> &commandArgs)
    {
        if(!isValidName(name))
            throw NinjaException(NinjaError::INVALID_NAME, "Invalid rule name, has to match [a-zA-Z_]{1}[a-zA-Z0-9_]{0,98}");

        auto it = rules.find(name);
        if(it != rules.end())
            throw NinjaException(NinjaError::RULE_ALREADY_DEFINED, "Rule already defined: " + name);

        NinjaRule *rule = new NinjaRule(this, name, joinArgs(commandArgs));
        rules[name] = rule;
        return rule;
    }

    NinjaBuild* NinjaBuildFile::build(const NinjaRule *rule, const std::string &in, const std::string &out, const std::vector<ninja::NinjaArgValue> &additionalArgs, const std::vector<NinjaBuild*> &dependsOnBuilds)
    {
        NinjaBuild *build = new NinjaBuild { rule, { in }, escape(out), additionalArgs, dependsOnBuilds };
        builds.push_back(build);
        return build;
    }

    NinjaBuild* NinjaBuildFile::build(const NinjaRule *rule, const std::vector<std::string> &in, const std::string &out, const std::vector<ninja::NinjaArgValue> &additionalArgs, const std::vector<NinjaBuild*> &dependsOnBuilds)
    {
        NinjaBuild *build = new NinjaBuild { rule, in, escape(out), additionalArgs, dependsOnBuilds };
        builds.push_back(build);
        return build;
    }

    std::string NinjaBuildFile::generate() const
    {
        std::string result;
        for(const auto &globalVar : globalVariables)
        {
            result += globalVar.first;
            result += " = ";
            result += globalVar.second;
            result += '\n';
        }

        if(!globalVariables.empty())
            result += '\n';
        
        for(const auto &rule : rules)
        {
            result += "rule ";
            result += rule.first;
            result += "\n  command = ";
            result += rule.second->command;
            if(!rule.second->depFile.empty())
            {
                result += "\n  depfile = ";
                result += rule.second->depFile;
            }
            if(!rule.second->deps.empty())
            {
                result += "\n  deps = ";
                result += rule.second->deps;
            }
            result += "\n\n";
        }

        result += "build always: phony\n\n";

        for(const NinjaBuild *build : builds)
        {
            result += "build ";
            result += build->out;
            result += ": ";
            result += build->rule->name;
            result += ' ';
            result += combine_escape(build->in);

            if(!build->dependsOnBuilds.empty())
            {
                result += " ||";
                for(const NinjaBuild *dependsOnBuild : build->dependsOnBuilds)
                {
                    result += ' ' + dependsOnBuild->out;
                }
            }

            if(build->alwaysRun)
                result += " | always";

            for(const NinjaArgValue &additionalArg : build->additionalArgs)
            {
                result += "\n  ";
                result += additionalArg.arg.name;
                result += " = ";
                result += additionalArg.value;

            }
            result += "\n\n";
        }
        return result;
    }
}