-
Notifications
You must be signed in to change notification settings - Fork 1
/
configure.h
221 lines (193 loc) · 6.83 KB
/
configure.h
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
/*
Copyright 2023 Google LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include <assert.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <set>
#include <string>
#include <vector>
class NinjaBuilder {
public:
// Parameter for constructor, for specifying default values for rules.
//
// Additional linker rules can be specified with CclinkRule.
struct Config {
Config() {}
std::string cxxflags{
"-O2 -g --std=c++17 -Wall -Werror -D_FILE_OFFSET_BITS=64 -I."};
std::string ldflags{""};
std::string gxx{"g++"};
std::string gcc{"gcc"};
};
explicit NinjaBuilder(const Config& config)
: data_({
"cxxflags = " + config.cxxflags,
"ldflags = " + config.ldflags,
"gxx = " + config.gxx,
"gcc = " + config.gcc,
"rule cc",
" command = $gxx -MMD -MF $out.d $cxxflags -c $in -o $out",
" depfile = $out.d",
" deps = gcc",
"rule cclink",
" command = $gxx $in -o $out $ldflags",
"rule runtest",
" command = ./$in > $out.tmp 2>&1 && mv $out.tmp $out || ( cat "
"$out.tmp; exit 1 )",
"rule ninjagenerator",
" command = ./$in",
" generator = true",
"build build.ninja: ninjagenerator configure",
"build configure.o: cc configure.cc",
"build configure: cclink configure.o",
}) {}
~NinjaBuilder() {
// Emit.
int fd = open(filename_.c_str(), O_CREAT | O_WRONLY | O_TRUNC, 0666);
assert(fd != -1);
for (const auto& line : uniquify_build_lines_) {
data_.push_back(line);
}
std::string joined_data =
JoinStrings(data_, "", "", "\n") +
"\n"; // not sure but maybe I should add the trailing newline.
int ret = write(fd, joined_data.data(), joined_data.size());
assert((int)joined_data.size() == ret);
close(fd);
}
void CclinkRule(const char* cclink_name, const char* command) {
data_.push_back(std::string("rule ") + cclink_name + " ");
data_.push_back(std::string(" command = ") + command);
}
struct CompileLinkObject {
CompileLinkObject(const CompileLinkObject& c) = delete;
CompileLinkObject(CompileLinkObject&& c)
: target_(std::move(c.target_)),
sources_(std::move(c.sources_)),
cclink_(std::move(c.cclink_)),
parent_(std::move(c.parent_)) {}
~CompileLinkObject() {
// link
parent_->data_.push_back(
"build " + parent_->outdir_ + target_ + ": " + cclink_ + " " +
JoinStrings(sources_, parent_->outdir_, ".o", " "));
// compile
for (const auto& source : sources_) {
parent_->uniquify_build_lines_.insert("build " + parent_->outdir_ +
source + ".o" + ": cc " + source +
".cc");
}
parent_ = nullptr;
}
CompileLinkObject& Cclink(const char* s) {
cclink_ = std::string(s);
return *this;
}
private:
friend class NinjaBuilder;
explicit CompileLinkObject(NinjaBuilder* parent) : parent_(parent) {}
std::string target_{};
std::vector<std::string> sources_{};
std::string cclink_{"cclink"};
NinjaBuilder* parent_;
};
CompileLinkObject CompileLinkRunTest(
const char* target, std::initializer_list<const char*> sources,
std::initializer_list<const char*> extra_test_depends = {}) {
RunTest(target, extra_test_depends);
return CompileLink(target, sources);
}
CompileLinkObject CompileLink(const char* target,
std::initializer_list<const char*> sources) {
CompileLinkObject c(this);
c.sources_ = InitializerListToVector(sources);
c.target_ = target;
return c;
}
void RunTestScript(
const char* target,
std::initializer_list<const char*> extra_test_depends = {}) {
std::string test = target;
std::string stdout = outdir_ + test + ".result";
data_.push_back("build " + stdout + ": runtest " + test +
MaybeExtraDepends(extra_test_depends));
}
static std::string PopenAndReadOrDie(const std::string& command,
int* maybe_exit_code = nullptr) {
std::string retval;
std::string readbuf;
const int bufsize = 4096;
readbuf.resize(bufsize);
FILE* f = popen(command.c_str(), "r");
assert(f != NULL);
while (1) {
size_t read_length = fread(&readbuf[0], 1, bufsize, f);
if (read_length == 0) {
// end of file or error, stop reading.
assert(feof(f));
break;
}
retval += readbuf.substr(0, read_length);
}
int exit_code = pclose(f);
if (maybe_exit_code) {
*maybe_exit_code = exit_code;
} else {
assert(exit_code == 0);
}
return retval;
}
private:
std::vector<std::string> data_;
const std::string outdir_{"out/"};
const std::string filename_{"build.ninja"};
// build lines that may be emit multiple times and needs to be uniquified.
std::set<std::string> uniquify_build_lines_{};
std::string MaybeExtraDepends(
const std::initializer_list<const char*>& extra_test_depends) {
std::string result{};
if (extra_test_depends.size() > 0) {
result =
result + "| " +
JoinStrings(InitializerListToVector(extra_test_depends), "", "", " ");
}
return result;
}
std::vector<std::string> InitializerListToVector(
const std::initializer_list<const char*>& sources) {
std::vector<std::string> result;
for (const char* v : sources) {
result.push_back(v);
}
return result;
}
void RunTest(const char* target,
std::initializer_list<const char*> extra_test_depends = {}) {
std::string test = outdir_ + target;
std::string stdout = test + ".result";
data_.push_back("build " + stdout + ": runtest " + test +
MaybeExtraDepends(extra_test_depends));
}
static std::string JoinStrings(std::vector<std::string> ss,
std::string prefix, std::string postfix,
std::string split) {
std::string result;
for (size_t i = 0; i < ss.size(); ++i) {
if (i) result.append(split);
result.append(prefix + ss[i] + postfix);
}
return result;
}
};