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
|
#define _GNU_SOURCE
#include <errno.h>
#include <glob.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define CBS_PTHREAD
#include "cbs.h"
#define CC "gcc"
#define CFLAGS_ALL WARNINGS, "-pipe", "-std=c23", "-Iinclude"
#define CFLAGS_DBG CFLAGS_ALL, "-g", "-ggdb3", "-Og"
#ifdef __APPLE__
# define CFLAGS_RLS CFLAGS_ALL, "-O3", "-flto"
#else
# define CFLAGS_RLS \
CFLAGS_ALL, "-O3", "-flto", "-march=native", "-mtune=native"
#endif
#define LIBNAME "libmlib"
#define WARNINGS \
"-Wall", "-Wextra", "-Wpedantic", "-Werror", "-Wno-attributes", \
"-Wno-pointer-sign"
#define CMDPRC(c) \
do { \
int ec; \
cmdput(c); \
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 work(void *);
static unsigned long flags;
int
main(int argc, char **argv)
{
int opt, procs = -1;
cbsinit(argc, argv);
rebuild();
while ((opt = getopt(argc, argv, "fj:r")) != -1) {
switch (opt) {
case '?':
fprintf(stderr, "Usage: %s [-j procs] [-fr]\n", *argv);
exit(EXIT_FAILURE);
case 'j':
procs = atoi(optarg);
break;
default:
flags |= 1 << (opt - 'a');
}
}
argc -= optind;
argv += optind;
if (argc >= 1) {
if (!streq(*argv, "clean")) {
diex("invalid subcommand ā ā%sā", *argv);
exit(EXIT_FAILURE);
}
cmd_t c = {};
cmdadd(&c, "find", ".", "(", "-name", "*.[ao]", "-or", "-name", "*.so",
")", "-delete");
CMDPRC(c);
} else {
cmd_t c = {};
glob_t g;
tpool_t tp;
if (glob("lib/*/*.c", 0, globerr, &g))
die("glob");
if (procs == -1 && (procs = nproc()) == -1) {
if (errno)
die("nproc");
procs = 8;
}
tpinit(&tp, procs);
for (size_t i = 0; i < g.gl_pathc; i++)
tpenq(&tp, work, 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(LIBNAME ".a", (const char **)g.gl_pathv, g.gl_pathc))
{
cmdadd(&c, "ar", "rcs", LIBNAME ".a");
cmdaddv(&c, g.gl_pathv, g.gl_pathc);
CMDPRC(c);
}
if (flagset('f')
|| foutdatedv(LIBNAME ".so", (const char **)g.gl_pathv, g.gl_pathc))
{
struct strv sv = {};
env_or_default(&sv, "CC", CC);
cmdaddv(&c, sv.buf, sv.len);
cmdadd(&c, "-shared", "-o", LIBNAME ".so");
cmdaddv(&c, g.gl_pathv, g.gl_pathc);
CMDPRC(c);
}
globfree(&g);
}
return EXIT_SUCCESS;
}
void
work(void *p)
{
char *dst, *src = p;
cmd_t c = {};
struct strv sv = {};
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);
cmdadd(&c, "-Iinclude", "-fPIC", "-o", dst, "-c", src);
CMDPRC(c);
}
free(dst);
}
int
globerr(const char *s, int e)
{
errno = e;
die("glob: %s", s);
}
|