-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild_spec_graph.c
More file actions
184 lines (169 loc) · 5.21 KB
/
build_spec_graph.c
File metadata and controls
184 lines (169 loc) · 5.21 KB
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
// Written by:
// Logan Mahan, NetID: lmahan, CSID: mahan
// Sam Ware, NetID: sware2, CSID: sware
#include "build_spec_repr.h"
#include "linked_list.h"
#include "text_parsing.h"
#include "proc_creation_prog_exe.h"
#include "proj_utils.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/stat.h>
#include <string.h>
#include <unistd.h>
/*
* Converts a command to a tokenized list of strings
*/
Node* commandToArgs(char* command) {
// A list of all the tokenized strings, used to get the size
Node* temp_command = createList();
char* token = strtok(command, " \t");
while(token != NULL) {
char* copy = malloc((strlen(token) + 1) * sizeof(char));
mallocCheck(copy);
memset(copy, 0, (strlen(token) + 1) * sizeof(char));
strncpy(copy, token, strlen(token));
append(temp_command, copy);
token = strtok(NULL, " \t");
}
return temp_command;
}
/*
* Runs the commands for a given target
*/
void buildTarget(Buildspec* bs) {
Node* commands = getCommands(bs);
int line_number = getLine(bs) + 1;
for (int i = 0; i < size(commands); i++) {
//
Node* temp_command = commandToArgs((char*) getElement(commands,i));
char** args = (char**) malloc((size(temp_command) + 1) * sizeof(char*));
mallocCheck(args);
for (int i = 0; i < size(temp_command); i++) {
args[i] = getElement(temp_command, i);
}
// NULL terminator for use with exec() functions
args[size(temp_command)] = NULL;
//
int j = 0;
while(args[j] != NULL){
printf("%s ", args[j]);
j++;
}
printf("\n");
runCommand(args, getTarget(bs), line_number);
line_number++;
for(int j = 0; j < size(temp_command); j++) {
free(args[j]);
}
free(args);
freeList(temp_command);
}
}
/*
* Depth-first search to find cycles in the Buildspec graph
* Checks to see if there a exists a back path on a single ancestor tree
*/
void checkCycles(Buildspec* bs, Node* bs_list, Buildspec* prev) {
if (isMarked(bs)) {
fprintf(stderr, "Error: Cycle Detected: %s <- %s\n", getTarget(bs), getTarget(prev));
exit(-1);
} else {
mark(bs);
}
Node* deps = getDependencies(bs);
for (int i = 0; i < size(deps); i++) {
char* target = getElement(deps, i);
Buildspec* next = find(bs_list, target);
if (next != NULL) {
checkCycles(next, bs_list, bs);
}
}
unmark(bs);
}
/*
* Runs a post order traversal on the build graph to run a bulid spec and all
* its dependencies. Returns 1 if the target was built, 0 otherwise.
*/
int postOrder(Buildspec* bs, Node* bs_list) {
Node* deps = getDependencies(bs);
if (size(deps) == 0) {
buildTarget(bs);
return 1;
}
int build = 0; // Changes to 1 if any dependencies were built
time_t dep_mod_date = 0;
for (int i = 0; i < size(deps); i++) {
char* target = getElement(deps, i);
Buildspec* next = find(bs_list, target);
if (next == NULL) { // This means that the dep is a file
if (access(target, R_OK) < 0) {
fprintf(stderr, "%d: File does not exist: \"%s\"\n", getLine(bs), target);
exit(-1);
}
struct stat dep_stats;
if (stat(target, &dep_stats) < 0) {
fprintf(stderr, "Error: Unable to open file's stats: %s\n", target);
exit(-1);
}
// Sets dep_mod_date to the most recent modification date
if (dep_stats.st_mtime > dep_mod_date) {
dep_mod_date = dep_stats.st_mtime;
}
} else {
int depBuilt = postOrder(next, bs_list);
if (depBuilt) { // If a dependency was built, build target
build = 1;
}
}
}
if (access(getTarget(bs), R_OK) < 0) { // If the target doesn't exist
if (build || dep_mod_date > 0) {
buildTarget(bs);
return 1;
}
} else { // If the target exists
struct stat target_stats;
if (stat(getTarget(bs), &target_stats) < 0) {
fprintf(stderr, "Error: Unable to open file's stats: %s\n", getTarget(bs));
exit(-1);
}
time_t target_mod_date = target_stats.st_mtime;
// Build if a dependency was build or target is out of date
if (target_mod_date < dep_mod_date || build) {
buildTarget(bs);
return 1;
}
}
return 0;
}
/*
* Checks for cycles, and then tries to build the specified target
*/
void runMakefile(FILE* make, char* target) {
// Parses the makefile to get all the build specs
Node* bs_list = parseMakefile(make);
for (int i = 0; i < size(bs_list); i++) {
Buildspec* bs = getElement(bs_list, i);
checkCycles(bs, bs_list, NULL);
}
if(size(bs_list) == 0){
exit(0);
}
Buildspec* bs;
if(target == NULL){
bs = getElement(bs_list, 0);
} else {
bs = find(bs_list, target);
if(bs == NULL){
fprintf(stderr, "Error: Could not find target \"%s\"\n", target);
exit(-1);
}
}
int build = postOrder(bs, bs_list);
if (build == 0) {
printf("Target \"%s\" is up to date.\n", getTarget(bs));
}
}