-
Notifications
You must be signed in to change notification settings - Fork 0
/
nob.cpp
186 lines (160 loc) · 7 KB
/
nob.cpp
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
// TODO add to build benchmark in adventofcode/2023/day/14/parabolic_reflector_dish_benchmark
#define NOBUILD_IMPLEMENTATION
#include "./nob.h"
#include <array>
#include <cstdlib>
#include <filesystem>
#include <queue>
#include <string_view>
#include <vector>
#define CFLAGS "-std=c2x", "-Wall", "-Wextra", "-pedantic"
#define CPPFLAGS "-std=c++23", "-Wall", "-Wconversion", "-Wextra", "-pedantic", "-fsanitize=address,pointer-overflow,signed-integer-overflow,undefined"
#define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
#define MAKE_CMD(...) \
({Cmd cmd = { \
.line = cstr_array_make(__VA_ARGS__, NULL) \
}; \
Cstr cmd_to_show = cmd_show(cmd); \
INFO("MAKE_CMD: %s", cmd_to_show); \
std::free(reinterpret_cast<void*>( \
const_cast<char*>(cmd_to_show))); \
cmd;}) \
namespace fs = std::filesystem;
using namespace std::string_literals; // for operator""s
using namespace std::literals; // for operator""sv
void build_kattis_c_file(std::string_view path) {
Cstr noextpath = NOEXT(path.data());
CMD("cc", CFLAGS, "-o", noextpath, path.data());
std::free(reinterpret_cast<void*>(const_cast<char *>(noextpath))); // :-O
}
void build_kattis_c_files() {
for (auto const& entry : fs::directory_iterator("kattis"))
if (fs::is_regular_file(entry.path())) {
const std::string& path = entry.path().string();
if (path.ends_with(".c")) build_kattis_c_file(path);
}
}
void build_custom_cpp_file(std::string_view filename) {
Cstr path = PATH("custom", filename.data());
CMD("g++", CPPFLAGS, "-o", NOEXT(path), path);
}
void build_custom_cpp_files() {
for (auto const& entry : fs::directory_iterator("custom"))
if (fs::is_regular_file(entry.path())) {
auto const filename = entry.path().filename().string();
if (filename.ends_with(".cpp") or filename.ends_with("cc"))
build_custom_cpp_file(filename);
}
}
template<size_t N>
void make_and_run_cmd(std::array<Cstr, N> strings)
{
Cmd cmd;
cmd.line.count = strings.size();
cmd.line.elems = strings.data();
//TODO FIX leaks.
INFO("make_and_run_cmd: %s", cmd_show(cmd));
cmd_run_sync(cmd);
}
void build_cpp_file(std::string_view filename)
{
Cstr path = PATH(filename.data());
make_and_run_cmd(std::array{"g++", CPPFLAGS, "-o", NOEXT(path), path});
}
void build_and_run_gtest_file(std::string_view filename)
{
Cstr path = PATH(filename.data());
CMD("g++", CPPFLAGS, "-o", NOEXT(path), path, "-lgtest");
CMD(NOEXT(path));
}
Pid build_gtest_file_async(std::string_view path)
{
Cstr noextpath = NOEXT(path.data());
Cmd cmd = MAKE_CMD("g++", CPPFLAGS, "-o", noextpath, path.data(), "-lgtest");
Pid pid = cmd_run_async(cmd, NULL, NULL);
std::free(reinterpret_cast<void*>(const_cast<char**>(cmd.line.elems)));
std::free(reinterpret_cast<void*>(const_cast<char *>(noextpath))); // :-O
return pid;
}
Pid run_gtest_file_async(std::string_view filename)
{
return cmd_run_async(MAKE_CMD(NOEXT(PATH(filename.data()))), NULL, NULL);
}
void work_out_leetcode()
{
std::vector<Pid> pids;
for (const auto& entry : fs::directory_iterator("leetcode")) if (fs::is_regular_file(entry.path())) {
const std::string& filename = entry.path().filename().string();
if (filename == "3012.cpp") {
Cstr path = PATH(("leetcode/" + filename).c_str());
CMD("g++", "-std=c++20", "-fmodules-ts", "-x", "c++-system-header", "array");
CMD("g++", "-std=c++20", "-fmodules-ts", "-x", "c++-system-header", "algorithm");
CMD("g++", "-std=c++20", "-fmodules-ts", "-Wall", "-Wextra", "-pedantic", "-Wconversion", "-o", NOEXT(path), path);
continue; }
if (filename.ends_with(".cpp")) pids.push_back(build_gtest_file_async("leetcode/" + filename)); }
for (const Pid& pid : pids) pid_wait(pid);
pids.clear();
for (const auto& entry : fs::directory_iterator("leetcode")) if (fs::is_regular_file(entry.path())) {
const std::string& filename = entry.path().filename().string();
if (filename == "3012.cpp") continue;
if (filename.ends_with(".cpp")) pids.push_back(run_gtest_file_async("leetcode/" + filename)); }
for (const Pid& pid : pids) pid_wait(pid);
}
void build_codeforces_cpp_files() {
std::queue<std::string> directoriesQ;
directoriesQ.push("codeforces"s);
while (!directoriesQ.empty()) {
for (auto const& entry : fs::directory_iterator(directoriesQ.front()))
if (fs::is_regular_file(entry.path())) {
auto const filename = entry.path().filename().string();
// FIXME: auto-detect when compilation fails with modern standards and if desired
// to keep the old file as it is, revert here to using older -std= option.
if (filename == "pocketbook.cc" or filename == "steps.cc" or filename == "phonenumbers.cc") {
Cstr path = PATH(std::string_view(directoriesQ.front() + "/" + filename).data());
CMD("g++", "-Wall", "-Wextra", "-std=gnu++17", "-pedantic", "-Wconversion", "-o", NOEXT(path), path);
continue;
} else if (filename == "marks.cc") {
Cstr path = PATH(std::string_view(directoriesQ.front() + "/" + filename).data());
CMD("g++", "-Wall", "-Wextra", "-std=gnu++11", "-pedantic", "-Wconversion", "-o", NOEXT(path), path);
continue;
}
//
if (filename.ends_with(".cc") or filename.ends_with(".cpp")) {
build_cpp_file(directoriesQ.front() + "/" + filename);
}
} else if (fs::is_directory(entry.path())) {
directoriesQ.push(directoriesQ.front() + "/" + entry.path().filename().string());
}
directoriesQ.pop();
}
}
void build_directory_cpp_files(std::string const& root_directory) {
std::queue<std::string> directoriesQ;
directoriesQ.push(root_directory);
while (!directoriesQ.empty()) {
for (auto const& entry : fs::directory_iterator(directoriesQ.front()))
if (fs::is_regular_file(entry.path())) {
auto const filename = entry.path().filename().string();
if (filename.ends_with(".cc") or filename.ends_with(".cpp"))
build_cpp_file(directoriesQ.front() + "/" + filename);
} else if (fs::is_directory(entry.path())) {
directoriesQ.push(directoriesQ.front() + "/" + entry.path().filename().string());
}
directoriesQ.pop();
}
}
int main(int argc, char* argv[])
{
GO_REBUILD_URSELF(argc, argv);
build_kattis_c_files();
//build_custom_cpp_files();
//build_directory_cpp_files("adventofcode");
//build_codeforces_cpp_files();
work_out_leetcode();
build_and_run_gtest_file("uva/summing_digits.cpp");
#if GCC_VERSION > 120000
build_cpp_file("adventofcode/2023/day/01/trebuchet.cpp");
#endif
build_cpp_file("adventofcode/2023/day/02/cube_conundrum.cpp");
build_cpp_file("adventofcode/2022/day/25/snafu.cpp");
}