aboutsummaryrefslogtreecommitdiff
path: root/make.c
blob: f1ac06af680bd5bc8a5d6c8170e495b5b3552165 (plain) (blame)
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#define _GNU_SOURCE
#include <ctype.h>
#include <err.h>
#include <errno.h>
#include <getopt.h>
#include <glob.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#if __has_include(<features.h>)
#	include <features.h>
#endif

#include "cbs.h"

#define WARN \
	"-Wall", "-Wextra", "-Wpedantic", "-Werror", \
		"-Wno-attributes",  /* GNU attributes in headers */ \
		"-Wno-parentheses", /* if (x = foo()) */ \
		"-Wno-pointer-sign" /* char ↔ char8_t */

#define CC "cc"
#ifdef __GLIBC__
#	define CFLAGS WARN, "-std=c2x", "-D_GNU_SOURCE"
#else
#	define CFLAGS WARN, "-std=c2x"
#endif
#define CFLAGS_DBG CFLAGS, "-Og", "-ggdb3", "-DDEBUG=1"
#ifdef __APPLE__
#	define CFLAGS_RLS CFLAGS, "-O3"
#else
#	define CFLAGS_RLS CFLAGS, "-O3", "-march=native", "-mtune=native"
#endif

#define streq(x, y) (!strcmp(x, y))

#define FLAGMSK(f) ((uint64_t)1 << ((f) - ((f) < 'a' ? 'A' : 'G')))
#define FLAGSET(f) (flags & FLAGMSK(f))

#define _CMDPRC(c, f) \
	do { \
		int ec; \
		f(c); \
		if ((ec = cmdexec(c)) != EXIT_SUCCESS) \
			diex("%s terminated with exit-code %d", *c._argv, ec); \
		cmdclr(&c); \
	} while (0)
#define CMDPRC(c)  _CMDPRC(c, cmdput)
#define CMDPRC2(c) _CMDPRC(c, cmdput2)

static void build_ahoy(void);
static void build_c8asm(void);
static void build_common(void);
static void build_librune(void);
static int globerr(const char *, int);
static void cmdput2(cmd_t);

static uint64_t flags;
static const char *argv0;
static bool librune_built;

static void
usage(void)
{
	fprintf(stderr, "Usage: %s [-flr]\n", argv0);
	exit(EXIT_FAILURE);
}

int
main(int argc, char **argv)
{
	int opt;
	const struct option longopts[] = {
		{"force",   no_argument, nullptr, 'f'},
		{"lto",     no_argument, nullptr, 'l'},
		{"release", no_argument, nullptr, 'r'},
		{nullptr,   no_argument, nullptr, 0  },
	};

	cbsinit(argc, argv);
	rebuild();
	argv0 = *argv;

	while ((opt = getopt_long(argc, argv, "flr", longopts, nullptr)) != -1) {
		switch (opt) {
		case '?':
			usage();
		default:
			flags |= FLAGMSK(opt);
		}
	}

	argc -= optind;
	argv += optind;

	if (argc == 0) {
		build_common();
		build_c8asm();
		build_ahoy();
	} else if (streq(*argv, "clean")) {
		cmd_t c = {0};
		cmdadd(&c, "find", ".", "-type", "f", "(", "-name", "*.[ao]", "-or",
		       "-name", "c8asm", "-or", "-path", "./src/c8asm/autogen-lookup.h",
		       ")", "-delete");
		CMDPRC(c);
	} else {
		warnx("invalid subcommand -- '%s'", *argv);
		usage();
	}

	return EXIT_SUCCESS;
}

void
build_common(void)
{
	glob_t g;
	char **objs;
	cmd_t c = {0};
	struct strv sv = {0};

	if (glob("src/common/*.c", 0, globerr, &g))
		die("glob");

	objs = bufalloc(nullptr, g.gl_pathc, sizeof(*objs));
	for (size_t i = 0; i < g.gl_pathc; i++) {
		char *s = strdup(g.gl_pathv[i]);
		if (!s)
			die("strdup");
		s[strlen(s) - 1] = 'o';
		objs[i] = s;
	}

	env_or_default(&sv, "CC", CC);
	if (FLAGSET('r'))
		env_or_default(&sv, "CFLAGS", CFLAGS_RLS);
	else
		env_or_default(&sv, "CFLAGS", CFLAGS_DBG);

	for (size_t i = 0; i < g.gl_pathc; i++) {
		if (!FLAGSET('f') && !foutdated(objs[i], g.gl_pathv[i]))
			continue;

		c.dst = objs[i];
		cmdaddv(&c, sv.buf, sv.len);
		if (FLAGSET('l'))
			cmdadd(&c, "-flto");
		cmdadd(&c, "-c", g.gl_pathv[i], "-o", objs[i]);
		CMDPRC2(c);
	}

	globfree(&g);
	strvfree(&sv);
	for (size_t i = 0; i < g.gl_pathc; i++)
		free(objs[i]);
}

void
build_ahoy(void)
{
	glob_t g;
	char **objs;
	cmd_t c = {0};
	struct strv sv = {0}, pc = {0};

	build_librune();

	if (glob("src/ahoy/*.c", 0, globerr, &g))
		die("glob");

	objs = bufalloc(nullptr, g.gl_pathc, sizeof(*objs));
	for (size_t i = 0; i < g.gl_pathc; i++) {
		char *s = strdup(g.gl_pathv[i]);
		if (!s)
			die("strdup");
		s[strlen(s) - 1] = 'o';
		objs[i] = s;
	}

	env_or_default(&sv, "CC", CC);
	if (FLAGSET('r'))
		env_or_default(&sv, "CFLAGS", CFLAGS_RLS);
	else
		env_or_default(&sv, "CFLAGS", CFLAGS_DBG);

	(void)pcquery(&pc, "sdl2", PKGC_CFLAGS);

	for (size_t i = 0; i < g.gl_pathc; i++) {
		if (!FLAGSET('f') && !foutdated(objs[i], g.gl_pathv[i]))
			continue;

		c.dst = objs[i];
		cmdaddv(&c, sv.buf, sv.len);
		if (FLAGSET('l'))
			cmdadd(&c, "-flto");
		cmdadd(&c, "-Isrc/common", "-Ivendor/da", "-Ivendor/librune/include");
		cmdaddv(&c, pc.buf, pc.len);
		cmdadd(&c, "-c", g.gl_pathv[i], "-o", objs[i]);
		CMDPRC2(c);
	}

	if (!FLAGSET('f') && !foutdatedv("ahoy", (const char **)objs, g.gl_pathc))
		goto out;

	strvfree(&sv);
	strvfree(&pc);
	env_or_default(&sv, "CC", CC);
	env_or_default(&sv, "LDFLAGS", nullptr);

	c.dst = "ahoy";
	cmdaddv(&c, sv.buf, sv.len);
	if (FLAGSET('l'))
		cmdadd(&c, "-flto");
	if (pcquery(&pc, "sdl2", PKGC_LIBS))
		cmdaddv(&c, pc.buf, pc.len);
	else
		cmdadd(&c, "-lSDL2");
	cmdadd(&c, "-o", c.dst);
	cmdaddv(&c, objs, g.gl_pathc);
	cmdadd(&c, "src/common/cerr.o", "vendor/librune/librune.a");
	CMDPRC2(c);

out:
	globfree(&g);
	strvfree(&sv);
	strvfree(&pc);
	for (size_t i = 0; i < g.gl_pathc; i++)
		free(objs[i]);
}

void
build_c8asm(void)
{
	glob_t g;
	char **objs;
	cmd_t c = {0};
	struct strv sv = {0};

	if (!binexists("gperf"))
		diex("gperf is required to build c8asm");

	build_librune();

	if (FLAGSET('f')
	    || foutdated("src/c8asm/autogen-lookup.h", "src/c8asm/instr.gperf"))
	{
		c.dst = "src/c8asm/autogen-lookup.h";
		cmdadd(&c, "gperf", "src/c8asm/instr.gperf", "--output-file", c.dst);
		CMDPRC2(c);
	}

	if (glob("src/c8asm/*.c", 0, globerr, &g))
		die("glob");

	objs = bufalloc(nullptr, g.gl_pathc, sizeof(*objs));
	for (size_t i = 0; i < g.gl_pathc; i++) {
		char *s = strdup(g.gl_pathv[i]);
		if (!s)
			die("strdup");
		s[strlen(s) - 1] = 'o';
		objs[i] = s;
	}

	env_or_default(&sv, "CC", CC);
	if (FLAGSET('r'))
		env_or_default(&sv, "CFLAGS", CFLAGS_RLS);
	else
		env_or_default(&sv, "CFLAGS", CFLAGS_DBG);

	for (size_t i = 0; i < g.gl_pathc; i++) {
		if (!FLAGSET('f') && !foutdated(objs[i], g.gl_pathv[i]))
			continue;

		c.dst = objs[i];
		cmdaddv(&c, sv.buf, sv.len);
		if (FLAGSET('l'))
			cmdadd(&c, "-flto");
		cmdadd(&c, "-Isrc/common", "-Ivendor/da", "-Ivendor/librune/include");
		cmdadd(&c, "-c", g.gl_pathv[i], "-o", objs[i]);
		CMDPRC2(c);
	}

	if (!FLAGSET('f') && !foutdatedv("c8asm", (const char **)objs, g.gl_pathc))
		goto out;

	strvfree(&sv);
	env_or_default(&sv, "CC", CC);
	env_or_default(&sv, "LDFLAGS", nullptr);

	c.dst = "c8asm";
	cmdaddv(&c, sv.buf, sv.len);
	if (FLAGSET('l'))
		cmdadd(&c, "-flto");
	cmdadd(&c, "-o", c.dst);
	cmdaddv(&c, objs, g.gl_pathc);
	cmdadd(&c, "src/common/cerr.o", "vendor/librune/librune.a");
	CMDPRC2(c);

out:
	globfree(&g);
	strvfree(&sv);
	for (size_t i = 0; i < g.gl_pathc; i++)
		free(objs[i]);
}

void
build_librune(void)
{
	cmd_t c = {0};
	struct strv sv = {0};

	if (librune_built)
		return;
	librune_built = true;

	env_or_default(&sv, "CC", CC);

	if (FLAGSET('f')
	    || foutdated("vendor/librune/make", "vendor/librune/make.c"))
	{
		c.dst = "vendor/librune/make";
		cmdaddv(&c, sv.buf, sv.len);
		cmdadd(&c, "-lpthread", "-o", c.dst, "vendor/librune/make.c");
		CMDPRC2(c);
	}

	if (FLAGSET('f') || !fexists("vendor/librune/librune.a")) {
		cmdadd(&c, "vendor/librune/make");
		if (FLAGSET('f'))
			cmdadd(&c, "-f");
		if (FLAGSET('r'))
			cmdadd(&c, "-r");
		if (FLAGSET('l'))
			cmdadd(&c, "-l");
		CMDPRC(c);
	}
}

int
globerr(const char *s, int e)
{
	errno = e;
	die("glob: %s", s);
}

void
cmdput2(cmd_t c)
{
	const char *p;

	flockfile(stderr);
	for (p = *c._argv; *p; p++)
		fputc(toupper(*p), stderr);
	fprintf(stderr, "\t%s\n", c.dst);
	funlockfile(stderr);
}