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
|
#define _GNU_SOURCE
#include <errno.h>
#include <glob.h>
#include <langinfo.h>
#include <libgen.h>
#include <limits.h>
#include <locale.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#if __has_include(<features.h>)
# include <features.h>
#endif
#include "cbs.h"
#define flagset(o) (flags & UINT32_C(1)<<(o)-'a')
#define EXEC(cmd) \
do { \
cmdput(cmd); \
if (cmdexec(cmd) != EXIT_SUCCESS) \
exit(EXIT_FAILURE); \
strszero(&cmd); \
} while (false)
[[noreturn, gnu::format(printf, 1, 2)]]
static void err(const char *, ...);
static void build_mlib(void);
static uint32_t flags;
static const char *argv0;
static char *cflags_req[] = {
"-Wall",
"-Wextra",
"-Wpedantic",
"-Wvla",
"-Wno-attributes",
"-Wno-empty-body", /* Annoying when debugging */
"-Wno-pointer-sign",
"-Wno-parentheses",
"-Ivendor/mlib/include",
"-pipe",
"-std=c23",
#ifdef __linux__
"-D_FILE_OFFSET_BITS=64",
#endif
#ifdef __GLIBC__
"-D_GNU_SOURCE",
#endif
"-DPCRE2_CODE_UNIT_WIDTH=8",
};
static char *cflags_dbg[] = {
"-DDEBUG=1",
"-fsanitize=address,undefined",
#if __GNUC__ && __APPLE__
"-ggdb2",
#else
"-ggdb3",
#endif
"-O0",
};
static char *cflags_rls[] = {
"-DNDEBUG=1",
"-flto",
#ifdef __APPLE__
"-mcpu=native",
#else
"-march=native",
"-mtune=native",
#endif
"-O3",
};
int
main(int argc, char **argv)
{
cbsinit(argc, argv);
rebuild();
setlocale(LC_ALL, "");
argv0 = basename(argv[0]);
int opt;
while ((opt = getopt(argc, argv, "mr")) != -1) {
switch (opt) {
case '?':
usage:
fprintf(stderr,
"Usage: %s [-mr]\n"
" %s clean | distclean | install\n",
*argv, *argv);
exit(EXIT_FAILURE);
default:
flags |= UINT32_C(1) << opt-'a';
}
}
argc -= optind;
argv += optind;
if (argc > 1)
goto usage;
if (argc == 1) {
struct strs cmd = {};
if (strcmp(argv[0], "clean") == 0) {
strspushl(&cmd, "rm", "-f", "grab", "git-grab");
} else if (strcmp(argv[0], "distclean") == 0) {
strspushl(&cmd, "rm", "-f", "grab", "git-grab", "make");
if (fexists("vendor/mlib/make")) {
EXEC(cmd);
strspushl(&cmd, "vendor/mlib/make", "clean");
}
} else if (strcmp(argv[0], "install") == 0) {
char *p;
const char *ev;
static char prefix[PATH_MAX + 1];
static char bindir[PATH_MAX + 1];
static char mandir[PATH_MAX + 1];
p = prefix;
if ((ev = getenv("DESTDIR")) != nullptr)
p = stpncpy(p, ev, sizeof(prefix) - 1);
if ((ev = getenv("PREFIX")) != nullptr)
strncpy(p, ev, sizeof(prefix) - 1 - (p - prefix));
else
strncpy(p, "/usr/local", sizeof(prefix) - 1 - (p - prefix));
snprintf(bindir, sizeof(bindir), "%s/%s", prefix, "bin");
snprintf(mandir, sizeof(mandir), "%s/%s", prefix, "share/man/man1");
if (binexists("strip")) {
strspushl(&cmd, "strip", "-s", "grab", "git-grab");
EXEC(cmd);
}
strspushl(&cmd, "mkdir", "-p", bindir, mandir);
EXEC(cmd);
strspushl(&cmd, "cp", "grab", "git-grab", bindir);
EXEC(cmd);
strspushl(&cmd, "cp", "man/grab.1", "man/git-grab.1", mandir);
EXEC(cmd);
strsfree(&cmd);
return EXIT_SUCCESS;
} else {
err(strcmp(nl_langinfo(CODESET), "UTF-8") == 0
? "invalid subcommand ā ā%sā"
: "invalid subcommand -- `%s'",
argv[0]);
}
cmdput(cmd);
return cmdexec(cmd);
}
build_mlib();
glob_t g;
if (glob("src/*.c", 0, nullptr, &g))
err("glob:");
struct strs cmd = {};
for (char ch = '0'; ch <= '1'; ch++) {
strspushenvl(&cmd, "CC", "cc");
strspush(&cmd, cflags_req, lengthof(cflags_req));
if (flagset('r'))
strspushenv(&cmd, "CFLAGS", cflags_rls, lengthof(cflags_rls));
else
strspushenv(&cmd, "CFLAGS", cflags_dbg, lengthof(cflags_dbg));
char buf[] = "-DGIT_GRAB=X";
buf[sizeof(buf) - 2] = ch;
strspushl(&cmd, buf);
if (!pcquery(&cmd, "libpcre2-8", PC_CFLAGS | PC_LIBS))
strspushl(&cmd, "-lpcre2-8");
strspushl(&cmd, "-o", ch == '0' ? "grab" : "git-grab");
strspush(&cmd, g.gl_pathv, g.gl_pathc);
strspushl(&cmd, "vendor/mlib/libmlib.a");
cmdput(cmd);
if (cmdexec(cmd) != EXIT_SUCCESS)
exit(EXIT_FAILURE);
strszero(&cmd);
}
return EXIT_SUCCESS;
}
void
build_mlib(void)
{
struct strs cmd = {};
if (flagset('m') || !fexists("vendor/mlib/make")) {
strspushenvl(&cmd, "CC", "cc");
strspushl(&cmd, "-std=c23", "-o", "vendor/mlib/make", "vendor/mlib/make.c");
cmdput(cmd);
if (cmdexec(cmd) != EXIT_SUCCESS)
exit(EXIT_FAILURE);
strszero(&cmd);
}
strspushl(&cmd, "./vendor/mlib/make");
if (flagset('m'))
strspushl(&cmd, "-f");
if (flagset('r'))
strspushl(&cmd, "-r");
cmdput(cmd);
if (cmdexec(cmd) != EXIT_SUCCESS)
exit(EXIT_FAILURE);
strsfree(&cmd);
}
void
err(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
int save = errno;
flockfile(stderr);
fprintf(stderr, "%s: ", argv0);
vfprintf(stderr, fmt, ap);
if (fmt[strlen(fmt) - 1] == ':')
fprintf(stderr, " %s", strerror(save));
fputc('\n', stderr);
funlockfile(stderr);
va_end(ap);
exit(EXIT_FAILURE);
}
|