-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
160 lines (133 loc) · 3.07 KB
/
main.c
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
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>
#include "dict.h"
#include "files.h"
#include "graph.h"
#include "job_runner.h"
#include "list.h"
#include "macros.h"
#include "parse.h"
#include "string.h"
static void print_usage() {
fprintf(stderr, "Usage: "
"make [-C dir]... [-f makefile] "
"[-j jobs] [target]...\n");
}
#define ERROR_EXIT_CODE 2
int main(int argc, char **argv) {
int c;
const char *makefile = NULL;
long jobs = 1;
char *endptr;
while ((c = getopt(argc, argv, "C:f:j:")) != -1) {
switch (c) {
case 'C':
if (chdir(optarg)) {
fprintf(stderr,
"Could not change "
"directory to %s\n",
optarg);
return (ERROR_EXIT_CODE);
}
break;
case 'f':
if (makefile != NULL) {
fprintf(stderr, "Option -f specified "
"multiple times\n");
return (ERROR_EXIT_CODE);
}
makefile = optarg;
break;
case 'j':
errno = 0;
jobs = strtol(optarg, &endptr, 10);
if (errno != 0 || jobs < 1 || *endptr != '\0') {
fprintf(stderr, "Invalid numeric value "
"for option -j\n");
return (ERROR_EXIT_CODE);
}
break;
default:
print_usage();
return (ERROR_EXIT_CODE);
}
}
if (makefile == NULL) {
makefile = "Makefile";
}
{
struct graph *graph;
struct dict *macros;
int fd;
fd = open(makefile, O_RDONLY);
if (fd < 0) {
fprintf(stderr, "Could not open the makefile\n");
return (ERROR_EXIT_CODE);
}
graph = graph_init();
macros = dict_init();
populate_builtin_macros(macros);
populate_environment_variables(macros);
if (parse_file(fd, graph, macros)) {
close(fd);
graph_destroy(graph);
dict_destroy(macros);
return (ERROR_EXIT_CODE);
}
close(fd);
if (optind == argc) {
/* No targets specified, use the first target */
struct list *nodes;
struct list_item *item;
struct graph_node *node;
nodes = graph_get_nodes(graph);
item = list_head(nodes);
if (item == NULL) {
fprintf(stderr, "No target specified\n");
graph_destroy(graph);
dict_destroy(macros);
return (ERROR_EXIT_CODE);
}
node = list_get_data(item);
graph_node_mark_target(node);
} else {
/* Read the list of targets from the command line */
int i;
for (i = optind; i < argc; ++i) {
struct string *node_name;
struct graph_node *node;
node_name = string_init(argv[i]);
node = graph_get_node_by_name(graph, node_name);
string_destroy(node_name);
if (node == NULL) {
fprintf(stderr, "Unknown target "
"\"%s\"\n", argv[i]);
graph_destroy(graph);
dict_destroy(macros);
return (ERROR_EXIT_CODE);
}
graph_node_mark_target(node);
}
}
if (update_all_files_info(graph)) {
dict_destroy(macros);
graph_destroy(graph);
return (ERROR_EXIT_CODE);
}
if (graph_process(graph)) {
dict_destroy(macros);
graph_destroy(graph);
return (ERROR_EXIT_CODE);
}
{
int ret = run_jobs(graph, macros, (size_t)jobs);
dict_destroy(macros);
graph_destroy(graph);
return (ret);
}
}
}