-
Notifications
You must be signed in to change notification settings - Fork 1
/
makefile.module.c
175 lines (141 loc) · 4.31 KB
/
makefile.module.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <unistd.h>
#include <libgen.h>
#include <sys/wait.h>
#include "parser/colors.h"
build depends "deps/hash/hash.c";
#include "deps/hash/hash.h"
import Package from "package/package.module.c";
import pkg_export from "package/export.module.c";
import pkg_import from "package/import.module.c";
import atomic from "package/atomic-stream.module.c";
import utils from "utils/utils.module.c";
import stream from "deps/stream/stream.module.c";
static const char * ops[] = {
":=",
"?=",
"+=",
};
typedef struct {
char * makefile;
char * makefile_dir;
char * makefile_base;
char * target;
char * cwd;
} makevars;
makevars get_makevars(Package.t * pkg, char * makefile) {
makevars v = {
.makefile = makefile,
.makefile_base = strdup(basename(makefile)),
.makefile_dir = strdup(dirname(makefile)),
.target = strdup(basename(pkg->generated)),
.cwd = getcwd(NULL, 0),
};
char * ext = strrchr(v.target, '.');
if (strcmp(pkg->name, "main") == 0){
*ext = 0;
} else {
ext[1] = 'a';
}
chdir(v.makefile_dir);
return v;
}
int clear_makevars(makevars v, int result, char * cmd) {
chdir(v.cwd);
free(v.cwd);
free(v.target);
free(v.makefile_dir);
free(v.makefile_base);
free(v.makefile);
free(cmd);
if (result == 0 || result == -1) return result;
if (result == 127) return -1;
return WEXITSTATUS(result);
}
export int make(Package.t * pkg, char * makefile) {
if (pkg == NULL) return -1;
makevars v = get_makevars(pkg, makefile);
char * cmd;
asprintf(&cmd, "make -f %s %s", v.makefile_base, v.target);
return clear_makevars(v, system(cmd), cmd);
}
export int clean(Package.t * pkg, char * makefile) {
if (pkg == NULL) return -1;
makevars v = get_makevars(pkg, makefile);
char * cmd;
asprintf(&cmd, "make -f %s CLEAN_%s", v.makefile_base, v.target);
return clear_makevars(v, system(cmd), cmd);
}
char * write_deps(Package.t * pkg, Package.t * root, stream.t * out, char * deps) {
if (pkg == NULL || pkg->exported) return deps;
pkg->exported = true;
char * source = utils.relative(root->generated, pkg->generated);
char * object = strdup(source);
object[strlen(source) - 1] = 'o';
size_t len = deps == NULL ? 0 : strlen(deps);
deps = realloc(deps, len + strlen(object) + 2);
sprintf(deps + len, " %s", object);
char * package_rel = utils.relative(root->source_abs, pkg->generated);
stream.printf(out, "#dependencies for package '%s'\n", package_rel);
global.free(package_rel);
int i;
for (i = 0; i < pkg->n_variables; i++) {
Package.var_t v = pkg->variables[i];
stream.printf(out, "%s %s %s\n", v.name, ops[v.operation], v.value);
}
stream.printf(out, "%s: %s", object, source);
global.free(source);
global.free(object);
if (pkg->deps == NULL) {
stream.printf(out,"\n\n");
return deps;
}
hash_each_val(pkg->deps, {
pkg_import.t * dep = (pkg_import.t *) val;
if (dep && dep->pkg && dep->pkg->header) {
char * path = utils.relative(root->source_abs, dep->pkg->header);
stream.printf(out, " %s", path);
global.free(path);
}
});
stream.printf(out,"\n\n");
hash_each_val(pkg->deps, {
pkg_import.t * dep = (pkg_import.t *)val;
deps = write_deps(dep->pkg, root, out, deps);
});
return deps;
}
char * get_makefile_name(const char * path) {
char * name = strdup(path);
name[strlen(name) - strlen("module.c")] = 0;
strcat(name, "mk");
return name;
}
export char * write(Package.t * pkg, const char * name) {
char * target = NULL;
char * mkfile_name = get_makefile_name(name);
stream.t * mkfile = atomic.open(mkfile_name);
char * deps = write_deps(pkg, pkg, mkfile, NULL);
if (strcmp(pkg->name, "main") == 0) {
char * buf = strdup(pkg->generated);
char * base = basename(buf);
asprintf(&target, "%.*s", (int)strlen(base) - 2, base);
free(buf);
stream.printf(mkfile, "%s:%s\n",target, deps);
stream.printf(mkfile, "\t$(CC) $(CFLAGS) $(LDFLAGS) %s -o %s $(LDLIBS)\n\n", deps, target);
} else {
target = malloc(strlen(pkg->name) + 3);
strcpy(target, pkg->name);
strcat(target, ".a");
stream.printf(mkfile, "%s:%s\n",target, deps);
stream.printf(mkfile, "\tar rcs $@ $^\n\n", deps, target);
}
stream.printf(mkfile, "CLEAN_%s:\n", target);
stream.printf(mkfile, "\trm -rf %s%s\n", target, deps);
free(target);
stream.close(mkfile);
free(deps);
return mkfile_name;
}