-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmake.c
More file actions
254 lines (214 loc) · 5.44 KB
/
Copy pathmake.c
File metadata and controls
254 lines (214 loc) · 5.44 KB
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
#define _GNU_SOURCE
#include <errno.h>
#if __has_include(<features.h>)
# include <features.h>
#endif
#include <glob.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define CBS_PTHREAD
#include "cbs.h"
#define CC "cc"
#define TARGET "andy"
#define CFLAGS_ALL \
"-lpthread", "-pipe", "-std=c23", "-Ivendor/mlib/include" GLIB_EXTRAS
#define CFLAGS_DBG "-g3", "-ggdb3", "-O0", "-fsanitize=address,undefined"
#define CFLAGS_RLS "-O3", "-flto", "-DNDEBUG" NOT_APPLE_EXTRAS
#define WARNINGS \
"-Wall", "-Wextra", "-Wpedantic", "-Wno-attributes", "-Wvla", \
"-Wno-pointer-sign", "-Wno-parentheses"
#ifdef __GLIBC__
# define GLIB_EXTRAS , "-D_GNU_SOURCE"
#else
# define GLIB_EXTRAS
#endif
#ifndef __APPLE__
# define NOT_APPLE_EXTRAS , "-march=native", "-mtune=native"
#else
# define NOT_APPLE_EXTRAS
#endif
#define CMDRC(c) \
do { \
int ec; \
if ((ec = cmdexec(c)) != EXIT_SUCCESS) \
diex("%s terminated with exit-code %d", *(c)._argv, ec); \
cmdclr(&(c)); \
} while (false)
#define flagset(o) (flags & (1 << ((o) - 'a')))
#define streq(x, y) (!strcmp(x, y))
static int globerr(const char *, int);
static void usage(void);
static void work_c(void *);
static void work_gperf(void *);
static bool dflag;
static char *argv0;
static unsigned long flags;
void
usage(void)
{
fprintf(stderr,
"Usage: %1$s [-afpr]\n"
" %1$s clean\n",
argv0);
exit(EXIT_FAILURE);
}
int
main(int argc, char **argv)
{
cbsinit(argc, argv);
rebuild();
argv0 = argv[0];
int opt;
while ((opt = getopt(argc, argv, "afpr")) != -1) {
switch (opt) {
case '?':
usage();
case 'a':
/* -a implies -f */
flags |= 1 << ('f' - 'a');
[[fallthrough]];
default:
flags |= 1 << (opt - 'a');
}
}
argc -= optind;
argv += optind;
if (argc >= 1) {
cmd_t c = {};
if (streq("clean", *argv)) {
cmdadd(&c, "find", ".", "(", "-name", TARGET, "-or", "-name",
"*.gen.c", "-or", "-name", "*.[ao]", ")", "-delete");
cmdput(c);
CMDRC(c);
} else {
fprintf(stderr, "%s: invalid subcommand — ‘%s’\n", argv0, *argv);
usage();
}
} else {
int err;
cmd_t c = {};
glob_t g;
tpool_t tp;
if (!fexists("./vendor/mlib/make")) {
struct strv sv = {};
env_or_default(&sv, "CC", CC);
cmdaddv(&c, sv.buf, sv.len);
cmdadd(&c, "-lpthread", "-o", "./vendor/mlib/make",
"./vendor/mlib/make.c");
CMDRC(c);
}
cmdadd(&c, "./vendor/mlib/make");
if (flagset('a'))
cmdadd(&c, "-f");
if (flagset('p'))
cmdadd(&c, "-p");
if (flagset('r'))
cmdadd(&c, "-r");
CMDRC(c);
int procs = nproc();
if (procs == -1) {
if (errno)
die("nproc");
procs = 8;
}
if ((err = glob("src/*.gperf", 0, globerr, &g)) != 0)
die("glob");
tpinit(&tp, procs);
for (size_t i = 0; i < g.gl_pathc; i++)
tpenq(&tp, work_gperf, g.gl_pathv[i], nullptr);
tpwait(&tp);
globfree(&g);
if ((err = glob("src/*.c", 0, globerr, &g)) != 0)
die("glob");
for (size_t i = 0; i < g.gl_pathc; i++)
tpenq(&tp, work_c, g.gl_pathv[i], nullptr);
tpwait(&tp);
tpfree(&tp);
for (size_t i = 0; i < g.gl_pathc; i++)
g.gl_pathv[i][strlen(g.gl_pathv[i]) - 1] = 'o';
if (flagset('f')
|| foutdatedv(TARGET, (const char **)g.gl_pathv, g.gl_pathc))
{
struct strv sv = {}, pc = {};
env_or_default(&sv, "CC", CC);
if (flagset('r'))
env_or_default(&sv, "CFLAGS", CFLAGS_RLS);
else
env_or_default(&sv, "CFLAGS", CFLAGS_DBG);
cmdaddv(&c, sv.buf, sv.len);
if (pcquery(&pc, "readline", PKGC_CFLAGS | PKGC_LIBS))
cmdaddv(&c, pc.buf, pc.len);
else
cmdadd(&c, "-lreadline");
cmdaddv(&c, g.gl_pathv, g.gl_pathc);
cmdadd(&c, "./vendor/mlib/libmlib.a", "-o", TARGET);
if (flagset('p'))
cmdput(c);
else
fprintf(stderr, "CC\t%s\n", TARGET);
CMDRC(c);
strvfree(&sv);
strvfree(&pc);
}
globfree(&g);
}
return EXIT_SUCCESS;
}
int
globerr(const char *s, int e)
{
errno = e;
die("glob: %s", s);
}
void
work_c(void *p)
{
char *dst, *src = p;
cmd_t c = {};
struct strv sv = {};
bool gperf = strstr(src, ".gen.c") != nullptr;
if (!(dst = strdup(src)))
die("strdup");
dst[strlen(dst) - 1] = 'o';
if (flagset('f') || foutdated(dst, src)) {
env_or_default(&sv, "CC", CC);
if (flagset('r'))
env_or_default(&sv, "CFLAGS", CFLAGS_RLS);
else
env_or_default(&sv, "CFLAGS", CFLAGS_DBG);
cmdaddv(&c, sv.buf, sv.len);
if (gperf)
cmdadd(&c, "-w");
else
cmdadd(&c, WARNINGS);
cmdadd(&c, CFLAGS_ALL, "-o", dst, "-c", src);
if (flagset('p'))
cmdput(c);
else
fprintf(stderr, "CC\t%s\n", dst);
CMDRC(c);
}
strvfree(&sv);
free(dst);
}
void
work_gperf(void *p)
{
char *dst, *src = p;
cmd_t c = {};
if (!(dst = strdup(src)))
die("strdup");
/* Just our luck! ‘.gen.c’ and ‘.gperf’ have the same length */
memcpy(strrchr(dst, '.'), ".gen.c", sizeof(".gperf") - 1);
/* Build the builtins hash table */
if (flagset('f') || foutdated(dst, src)) {
cmdadd(&c, "gperf", src, "--output-file", dst);
if (flagset('p'))
cmdput(c);
else
fprintf(stderr, "GPERF\t%s\n", dst);
CMDRC(c);
}
}