aboutsummaryrefslogtreecommitdiffstats
path: root/src/build.c
blob: 572815578caa249908d8178e026b59ed700b8dc1 (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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
/* build.c
 *
 * Copyright (c) 2014 Tharre
 *
 * This software may be modified and distributed under the terms
 * of the MIT license.  See the LICENSE file for details.
 */

#define _XOPEN_SOURCE 600
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include <libgen.h> /* dirname(), basename() */

#include "sha1.h"
#include "build.h"
#include "util.h"
#include "filepath.h"
#define _FILENAME "build.c"
#include "dbg.h"

#define HEADERSIZE 60

typedef struct do_attr {
	char *specific;
	char *general;
	char *chosen;
} do_attr;

typedef struct dep_info {
	const char *target;
	char *path;
	unsigned char *hash;
	unsigned int magic;
	int32_t flags;
#define DEP_SOURCE (1 << 1)
} dep_info;

static do_attr *get_doscripts(const char *target);
static void free_do_attr(do_attr *thing);
static char **parse_shebang(char *target, char *doscript, char *temp_output);
static char **parsecmd(char *cmd, size_t *i, size_t keep_free);
static char *get_relpath(const char *target);
static char *get_dep_path(const char *target);
static void write_dep_header(dep_info *dep);
static int handle_ident(dep_info *dep, int ident);
static int handle_c(dep_info *dep);
static unsigned char *hash_file(const char *target);


/* Build given target, using it's .do script. */
static int build_target(dep_info *dep) {
	int retval = 1;

	/* get the .do script which we are going to execute */
	do_attr *doscripts = get_doscripts(dep->target);
	if (!doscripts->chosen) {
		if (fexists(dep->target)) {
			/* if our target file has no .do script associated but exists,
			   then we treat it as a source */
			dep->flags |= DEP_SOURCE;
			dep->hash = hash_file(dep->target);
			write_dep_header(dep);
			goto exit;
		}

		die("%s couldn't be built as no suitable .do script exists\n",
				dep->target);
	}

	char *reltarget = get_relpath(dep->target);
	printf("\033[32mredo  \033[1m\033[37m%s\033[0m\n", reltarget);
	free(reltarget);

	/* remove old dependency record */
	if (remove(dep->path) && errno != ENOENT)
		fatal("redo: failed to remove %s", dep->path);

	char *temp_output = concat(2, dep->target, ".redoing.tmp");

	pid_t pid = fork();
	if (pid == -1) {
		/* failure */
		fatal("redo: failed to fork() new process");
	} else if (pid == 0) {
		/* child */

		/* change directory to our target */
		char *dirc = xstrdup(doscripts->chosen);
		char *ddoscript = dirname(dirc);
		if (chdir(ddoscript) == -1)
			fatal("redo: failed to change directory to %s", ddoscript);

		free(dirc);

		char **argv = parse_shebang(xbasename(dep->target),
				xbasename(doscripts->chosen), xbasename(temp_output));

		/* set "REDO_PARENT_TARGET" */
		if (setenv("REDO_PARENT_TARGET", dep->target, 1))
			fatal("redo: failed to setenv() REDO_PARENT_TARGET to %s",
					dep->target);

		/* excelp() has nearly everything we want: automatic parsing of the
		   shebang line through execve() and fallback to /bin/sh if no valid
		   shebang could be found. However, it fails if the target doesn't have
		   the executeable bit set, which is something we don't want. For this
		   reason we parse the shebang line ourselves. */
		execv(argv[0], argv);

		/* execv should never return */
		fatal("redo: failed to replace child process with %s", argv[0]);
	}

	/* parent */
	int status;
	if (waitpid(pid, &status, 0) == -1)
		fatal("redo: waitpid() failed");

	/* check how our child exited */
	if (WIFEXITED(status)) {
		if (WEXITSTATUS(status))
			die("redo: invoked .do script %s failed: %d\n", doscripts->chosen,
			    WEXITSTATUS(status));
	} else {
		/* something very wrong happened with the child */
		die("redo: invoked .do script did not terminate correctly\n");
	}

	/* check if our output file is > 0 bytes long */
	if (fsize(temp_output) > 0) {
		if (rename(temp_output, dep->target))
			fatal("redo: failed to rename %s to %s", temp_output, dep->target);

		unsigned char *new_hash = hash_file(dep->target);
		if (dep->hash) {
			retval = memcmp(new_hash, dep->hash, 20);
			if (retval)
				memcpy(dep->hash, new_hash, 20);

			free(new_hash);
		} else {
			dep->hash = new_hash;
		}

		write_dep_header(dep);
	} else {
		if (remove(temp_output) && errno != ENOENT)
			fatal("redo: failed to remove %s", temp_output);
	}

	/* depend on the .do script */
	dep_info dep2 = {
		.magic = dep->magic,
		.target = dep->target,
		.path = get_dep_path(doscripts->chosen),
		.hash = NULL,
		.flags = 0,
	};

	if (!fexists(dep2.path)) {
		dep2.hash = hash_file(doscripts->chosen);
		write_dep_header(&dep2);
		free(dep2.hash);
	}
	free(dep2.path);

	add_dep(doscripts->chosen, dep->target, 'c');

	/* redo-ifcreate on specific if general was chosen */
	if (doscripts->general == doscripts->chosen)
		add_dep(doscripts->specific, dep->target, 'e');

	free(temp_output);
exit:
	free_do_attr(doscripts);

	return retval;
}

/* Read and parse shebang and return an argv-like pointer array containing the
   arguments. If no valid shebang could be found assume "/bin/sh -e" instead. */
static char **parse_shebang(char *target, char *doscript, char *temp_output) {
	FILE *fp = fopen(doscript, "rb");
	if (!fp)
		fatal("redo: failed to open %s", doscript);

	char buf[1024];

	buf[ fread(buf, 1, sizeof(buf)-1, fp) ] = '\0';
	if (ferror(fp))
		fatal("redo: failed to read from %s", doscript);

	fclose(fp);

	char **argv;
	size_t i = 0;
	if (buf[0] == '#' && buf[1] == '!') {
		argv = parsecmd(&buf[2], &i, 5);
	} else {
		argv = xmalloc(7 * sizeof(char*));
		argv[i++] = "/bin/sh";
		argv[i++] = "-e";
	}

	argv[i++] = doscript;
	argv[i++] = target;
	char *basename = remove_ext(target);
	argv[i++] = basename;
	argv[i++] = temp_output;
	argv[i] = NULL;

	return argv;
}

/* Breaks cmd at spaces and stores a pointer to each argument in the returned
   array. The index i is incremented to point to the next free pointer. The
   returned array is guaranteed to have at least keep_free entries left. */
static char **parsecmd(char *cmd, size_t *i, size_t keep_free) {
	size_t argv_len = 16;
	char **argv = xmalloc(argv_len * sizeof(char*));
	size_t j = 0;
	bool prev_space = true;
	for (;; ++j) {
		switch (cmd[j]) {
		case ' ':
			cmd[j] = '\0';
			prev_space = true;
			break;
		case '\n':
		case '\r':
			cmd[j] = '\0';
		case '\0':
			return argv;
		default:
			if (!prev_space)
				break;
			/* check if we have enough space */
			while (*i+keep_free >= argv_len) {
				argv_len *= 2;
				argv = xrealloc(argv, argv_len * sizeof(char*));
			}

			prev_space = false;
			argv[*i] = &cmd[j];
			++*i;
		}
	}
}

/* Return a struct with all the possible .do scripts, and the chosen one. */
static do_attr *get_doscripts(const char *target) {
	do_attr *doscripts = xmalloc(sizeof(do_attr));

	doscripts->specific = concat(2, target, ".do");
	doscripts->general = concat(3, "default", take_extension(target), ".do");

	if (fexists(doscripts->specific))
		doscripts->chosen = doscripts->specific;
	else if (fexists(doscripts->general))
		doscripts->chosen = doscripts->general;
	else
		doscripts->chosen = NULL;

	return doscripts;
}

/* Free the do_attr struct. */
static void free_do_attr(do_attr *thing) {
	free(thing->specific);
	free(thing->general);
	free(thing);
}

/* Custom version of realpath that doesn't fail if the last part of path
   doesn't exist and allocates memory for the result itself. */
static char *xrealpath(const char *path) {
	char *dirc = xstrdup(path);
	char *dname = dirname(dirc);
	char *absdir = realpath(dname, NULL);
	if (!absdir)
		return NULL;
	char *abstarget = concat(3, absdir, "/", xbasename(path));

	free(dirc);
	free(absdir);
	return abstarget;
}

/* Return the relative path against "REDO_ROOT" of target. */
static char *get_relpath(const char *target) {
	char *root = getenv("REDO_ROOT");
	char *abstarget = xrealpath(target);

	if (!abstarget)
		fatal("redo: failed to get realpath() of %s", target);

	char *path = xstrdup(relpath(abstarget, root));
	free(abstarget);
	return path;
}

/* Return the dependency record path of target. */
static char *get_dep_path(const char *target) {
	char *root = getenv("REDO_ROOT");
	char *reltarget = get_relpath(target);
	char *dep_path;

	if (is_absolute(reltarget)) {
		dep_path = concat(3, root, "/.redo/abs/", reltarget);
	} else {
		dep_path = concat(3, root, "/.redo/rel/", reltarget);
	}

	/* create directory */
	mkpath(dep_path, 0755); /* TODO: should probably be somewhere else */

	free(reltarget);
	return dep_path;
}

/* Declare that `parent` depends on `target`. */
void add_dep(const char *target, const char *parent, int ident) {
	char *dep_path = get_dep_path(parent);

	if (strchr(target, '\n'))
		die("redo: newlines in targets are not supported\n");

	int fd = open(dep_path, O_WRONLY | O_APPEND);
	if (fd < 0) {
		if (errno != ENOENT)
			fatal("redo: failed to open %s", dep_path);

		/* no dependency record was found, so we create one */
		fd = open(dep_path, O_WRONLY | O_APPEND | O_CREAT, 0755);
		if (fd < 0)
			fatal("redo: failed to open %s", dep_path);
	}

	char garbage[HEADERSIZE];
	memset(garbage, 'Z', HEADERSIZE);

	/* skip header */
	if (lseek(fd, 0, SEEK_END) < (off_t) HEADERSIZE)
		pwrite(fd, garbage, HEADERSIZE, 0);

	char *reltarget = get_relpath(target);
	int bufsize = strlen(reltarget) + 3;
	char *buf = xmalloc(bufsize);
	buf[0] = ident;
	buf[1] = '\t';
	strcpy(buf+2, reltarget);
	buf[bufsize-1] = '\n';
	if (write(fd, buf, bufsize) < bufsize)
		fatal("redo: failed to write to %s", dep_path);

	if (close(fd))
		fatal("redo: failed to close %s", dep_path);

	free(buf);
	free(reltarget);
	free(dep_path);
}

/* Hash the target file, returning a pointer to the heap allocated hash. */
static unsigned char *hash_file(const char *target) {
	FILE *in = fopen(target, "rb");
	if (!in)
		fatal("redo: failed to open %s", target);

	unsigned char *hash = xmalloc(20);

	SHA_CTX context;
	unsigned char data[8192];
	size_t read;

	SHA1_Init(&context);
	while ((read = fread(data, 1, sizeof data, in)))
		SHA1_Update(&context, data, read);

	if (ferror(in))
		fatal("redo: failed to read from %s", target);
	SHA1_Final(hash, &context);
	fclose(in);

	return hash;
}

void sha1_to_hex(const unsigned char *sha1, char *buf) {
	static const char hex[] = "0123456789abcdef";

	for (int i = 0; i < 20; ++i) {
		char *pos = buf + i*2;
		*pos++ = hex[sha1[i] >> 4];
		*pos = hex[sha1[i] & 0xf];
	}
}

/* Write the dependency information into the specified path. */
static void write_dep_header(dep_info *dep) {
	mode_t mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH;
	int out = open(dep->path, O_WRONLY | O_CREAT, mode);
	if (out < 0)
		fatal("redo: failed to open %s", dep->path);

	char buf[60];
	sprintf(buf, "%010u", dep->magic);
	buf[10] = '\t';
	sha1_to_hex(dep->hash, buf+11);
	buf[51] = '\t';
	memset(buf+52, '-', 7);
	buf[59] = '\n';

	if (write(out, buf, sizeof buf) < (ssize_t) sizeof buf)
		fatal("redo: failed to write dependency record to '%s'", dep->path);

	if (close(out))
		fatal("redo: failed to close %s", dep->path);
}

int update_target(const char *target, int ident) {
	dep_info dep = {
		.magic = atoi(getenv("REDO_MAGIC")),
		.target = target,
		.path = get_dep_path(target),
		.hash = NULL,
		.flags = 0,
	};

	int retval = handle_ident(&dep, ident);
	free(dep.path);
	free(dep.hash);

	return retval;
}

static int handle_ident(dep_info *dep, int ident) {
	switch(ident) {
	case 'a':
		return build_target(dep);
	case 'e':
		if (fexists(dep->target))
			return build_target(dep);

		return 0;
	case 'c':
		return handle_c(dep);
	default:
		die("redo: unknown identifier '%c'\n", ident);
	}
}

static int handle_c(dep_info *dep) {
	FILE *fp = fopen(dep->path, "rb");
	if (!fp) {
		if (errno == ENOENT)
			/* dependency record does not exist */
			return build_target(dep);
		else
			fatal("redo: failed to open %s", dep->path);
	}

	char buf[FILENAME_MAX];

	if (fread(buf, 1, HEADERSIZE, fp) < HEADERSIZE)
		fatal("redo: failed to read %zu bytes from %s", HEADERSIZE, dep->path);

	errno = 0;
	buf[10] = '\0';
	long magic = strtol(buf, NULL, 10);
	if (errno)
		return build_target(dep);

	if (!fexists(dep->target)) {
		if (buf[52] == 'S') /* source flag set */
			/* target is a source and must not be rebuild */
			return 1;
		else
			return build_target(dep);
	}

	if (magic == dep->magic)
		/* magic number matches */
		return 1;

	char char_hash[40];

	unsigned char *hash = hash_file(dep->target);
	sha1_to_hex(hash, char_hash);
	free(hash);
	buf[51] = '\0';
	if (memcmp(char_hash, buf+11, 40))
		return build_target(dep);

	char *ptr;
	char *root = getenv("REDO_ROOT");
	bool rebuild = false;

	while (!feof(fp)) {
		ptr = buf;

		size_t read = fread(buf, 1, sizeof buf, fp);
		if (ferror(fp))
			fatal("redo: failed to read %zu bytes from descriptor", sizeof buf);

		for (size_t i = 0; i < read; ++i) {
			if (buf[i] != '\n')
				continue;
			buf[i] = '\0';
			if (!is_absolute(&ptr[2])) {
				/* if our path is relative we need to prefix it with the
				   root project directory or the path will be invalid */
				char *abs = concat(3, root, "/", &ptr[2]);
				if (update_target(abs, ptr[0]))
					rebuild = true;

				free(abs);
			} else {
				if (update_target(&ptr[2], ptr[0]))
					rebuild = true;
			}
			ptr = &buf[i+1];
		}

		if (read && buf[read-1] != '\n') {
			if (buf != ptr)
				memmove(buf, ptr, buf-ptr + sizeof buf);
			else
				die("redo: dependency record contains insanely long paths\n");
		}
	}

	fclose(fp);
	if (rebuild)
		return build_target(dep);

	return 0;
}