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
|
/* redo.c
*
* Copyright (c) 2014-2016 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 <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <time.h>
#include <limits.h>
#include <unistd.h>
#include "build.h"
#include "util.h"
#include "dbg.h"
#include "filepath.h"
/* Returns the amount of digits a number n has in decimal. */
static inline unsigned digits(unsigned n) {
return n ? 1 + digits(n/10) : n;
}
void prepare_env() {
if (getenv("REDO_ROOT") && getenv("REDO_PARENT_TARGET")
&& getenv("REDO_MAGIC"))
return;
/* set REDO_ROOT */
char *cwd = getcwd(NULL, 0);
if (!cwd)
fatal("redo: failed to obtain cwd");
if (setenv("REDO_ROOT", cwd, 0))
fatal("redo: failed to setenv() REDO_ROOT to %s", cwd);
free(cwd);
/* set REDO_MAGIC */
char magic_str[digits(UINT_MAX) + 1];
sprintf(magic_str, "%u", rand());
if (setenv("REDO_MAGIC", magic_str, 0))
fatal("redo: failed to setenv() REDO_MAGIC to %s", magic_str);
}
int DBG_LVL;
int main(int argc, char *argv[]) {
srand(time(NULL));
char *argv_base = xbasename(argv[0]);
if (!strcmp(argv_base, "redo")) {
prepare_env();
if (argc < 2) {
update_target("all", 'a');
} else {
for (int i = 1; i < argc; ++i)
update_target(argv[i], 'a');
}
} else {
char ident;
char **temp;
if (!strcmp(argv_base, "redo-ifchange"))
ident = 'c';
else if (!strcmp(argv_base, "redo-ifcreate"))
ident = 'e';
else if (!strcmp(argv_base, "redo-always"))
ident = 'a';
else
die("redo: argv set to unkown value\n");
/* ensure the environment is correct */
char *parent = getenv("REDO_PARENT_TARGET");
char *root = getenv("REDO_ROOT");
char *magic = getenv("REDO_MAGIC");
if (!parent || !root || !magic)
die("%s must be called inside a .do script\n", argv[0]);
/* set DBG_LVL (for dbg.h) */
char *env = getenv("REDO_DEBUG");
if (env)
DBG_LVL = atoi(env);
if (ident == 'a')
add_prereq(parent, parent, ident);
else
for (int i = 1; i < argc; ++i) {
do {
temp = &argv[rand() % (argc-1) + 1];
} while (!*temp);
update_target(*temp, ident);
add_prereq_path(*temp, xbasename(parent), ident);
*temp = NULL;
}
}
return EXIT_SUCCESS;
}
|