-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompile.c
More file actions
242 lines (200 loc) · 6.71 KB
/
compile.c
File metadata and controls
242 lines (200 loc) · 6.71 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
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
// Copyright (C) 2026 p1k0chu
// SPDX-License-Identifier: LGPL-3.0-or-later
#include "cbuild/compile.h"
#include "cbuild/link_target.h"
#include "mtime.h"
#include "target_private.h"
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>
[[noreturn]] static void run_ar(cbuild_link_target_t *target, int flags);
[[noreturn]] static void run_gcc(cbuild_link_target_t *target, int flags);
static void print_cmd(char **cmd, size_t len) {
for (size_t i = 0; i < len; ++i) {
printf("%s ", cmd[i]);
}
putchar('\n');
}
static pid_t cbuild_obj_compile(cbuild_obj_t *obj, int flags);
static pid_t cbuild_link_target_compile(cbuild_link_target_t *target, int flags);
pid_t cbuild_target_compile(cbuild_target_t *target, int flags) {
if (target->flags & CBUILD_TARGET_ISCOMPILED) {
if (flags & CBUILD_COMPILE_DRYRUN) {
return -69;
} else if (flags & CBUILD_COMPILE_FORCE) {
return 0;
}
}
char updateddeps = flags & CBUILD_COMPILE_FORCE;
for (size_t i = 0; i < target->deps.len; ++i) {
cbuild_target_t *obj = target->deps.ptr[i];
pid_t child = cbuild_target_compile(obj, flags);
if (child == -69 && (flags & CBUILD_COMPILE_DRYRUN)) {
updateddeps |= 1;
} else if (child < 0)
err(EXIT_FAILURE, "fork");
else if (child > 0) {
updateddeps |= 1;
int wstatus;
waitpid(child, &wstatus, 0);
if (!WIFEXITED(wstatus))
errx(EXIT_FAILURE, "child exited abnormally");
int code = WEXITSTATUS(wstatus);
if (code != 0)
errx(EXIT_FAILURE, "child returned with error code %d", code);
}
if (!updateddeps) {
updateddeps |= cbuild__mtimecmp(target->outpath, obj->outpath) < 0;
}
}
switch (target->type) {
case CBUILD_TARGET_EXE:
case CBUILD_TARGET_SHAREDLIB:
case CBUILD_TARGET_STATICLIB:
if (updateddeps) {
target->flags |= CBUILD_TARGET_ISCOMPILED;
return cbuild_link_target_compile((cbuild_link_target_t *)target, flags);
}
break;
case CBUILD_TARGET_OBJECT:
if (updateddeps || cbuild__mtimecmp(target->outpath, ((cbuild_obj_t *)target)->src) < 0) {
target->flags |= CBUILD_TARGET_ISCOMPILED;
return cbuild_obj_compile((cbuild_obj_t *)target, flags);
}
break;
case CBUILD_TARGET_CUSTOM:
cbuild_custom_target_t *ct = (void *)target;
if (updateddeps ||
(ct->inpath == NULL || cbuild__mtimecmp(target->outpath, ct->inpath) < 0)) {
target->flags |= CBUILD_TARGET_ISCOMPILED;
return cbuild__custom_target_compile((cbuild_custom_target_t *)target, flags);
}
break;
default:
errx(1, "target type is invalid value (not enum)");
}
return 0;
}
static pid_t cbuild_link_target_compile(cbuild_link_target_t *target, int flags) {
pid_t cpid = fork();
if (cpid < 0) {
err(1, "fork");
} else if (cpid > 0) {
return cpid;
}
switch (target->base.type) {
case CBUILD_TARGET_STATICLIB:
run_ar(target, flags);
default:
run_gcc(target, flags);
}
}
static pid_t cbuild_obj_compile(cbuild_obj_t *obj, int flags) {
pid_t cpid = fork();
if (cpid < 0) {
err(1, "fork");
} else if (cpid > 0) {
return cpid;
}
char *cmd[6 + obj->cflags.len];
size_t i = 0;
cmd[i++] = "gcc";
for (size_t j = 0; j < obj->cflags.len; ++j) {
char *s = (char *)obj->cflags.ptr[j];
if (s)
cmd[i++] = s;
}
cmd[i++] = "-c";
cmd[i++] = "-o";
cmd[i++] = (char *)obj->base.outpath;
cmd[i++] = (char *)obj->src;
cmd[i++] = NULL;
print_cmd(cmd, i - 1);
if (flags & CBUILD_COMPILE_DRYRUN)
exit(0);
execvp("gcc", cmd);
err(EXIT_FAILURE, "child failed to exec");
}
[[noreturn]] static void run_gcc(cbuild_link_target_t *target, int flags) {
const bool isshared = target->base.type == CBUILD_TARGET_SHAREDLIB;
const size_t n = (isshared ? 5 : 4) + target->ldflags.len + target->base.deps.len;
char *cmd[n];
size_t i = 0;
cmd[i++] = "gcc";
// separate loops, because objects go first, before potential
// libraries
for (size_t j = 0; j < target->base.deps.len; ++j) {
cbuild_target_t *d = target->base.deps.ptr[j];
switch (d->type) {
case CBUILD_TARGET_EXE:
case CBUILD_TARGET_SHAREDLIB:
case CBUILD_TARGET_STATICLIB:
case CBUILD_TARGET_CUSTOM:
break;
case CBUILD_TARGET_OBJECT:
cmd[i++] = (char *)d->outpath;
break;
}
}
for (size_t j = 0; j < target->base.deps.len; ++j) {
cbuild_target_t *d = target->base.deps.ptr[j];
switch (d->type) {
case CBUILD_TARGET_SHAREDLIB:
case CBUILD_TARGET_STATICLIB:
// make a -l:libexample.a kind of argument out of d->outpath
const ssize_t size = snprintf(NULL, 0, "-l:%s", d->outpath);
if (size < 0)
err(1, "snprintf");
// we never free anything because of exec/exit
char *buf = malloc((size_t)size + 1);
if (buf == NULL)
err(1, "malloc");
const ssize_t size2 = snprintf(buf, (size_t)size + 1, "-l:%s", d->outpath);
if (size2 < 0)
err(1, "snprintf");
if (size2 >= size)
warnx("snprintf truncated: %s", buf);
cmd[i++] = buf;
break;
case CBUILD_TARGET_OBJECT:
case CBUILD_TARGET_EXE:
case CBUILD_TARGET_CUSTOM:
break;
}
}
if (isshared)
cmd[i++] = "-shared";
for (size_t j = 0; j < target->ldflags.len; ++j) {
char *s = (char *)target->ldflags.ptr[j];
if (s)
cmd[i++] = s;
}
cmd[i++] = "-o";
cmd[i++] = (char *)target->base.outpath;
cmd[i++] = NULL;
print_cmd(cmd, i - 1);
if (flags & CBUILD_COMPILE_DRYRUN)
exit(0);
execvp("gcc", cmd);
err(EXIT_FAILURE, "failed to exec gcc");
}
static void run_ar(cbuild_link_target_t *target, int flags) {
char *cmd[4 + target->base.deps.len];
cmd[0] = "ar";
cmd[1] = "rcs";
cmd[2] = (char *)target->base.outpath;
size_t i = 3;
for (size_t j = 0; j < target->base.deps.len; ++j) {
cbuild_target_t *d = target->base.deps.ptr[j];
if (d->type == CBUILD_TARGET_OBJECT)
cmd[i++] = (char *)d->outpath;
}
cmd[i++] = NULL;
print_cmd(cmd, i - 1);
if (flags & CBUILD_COMPILE_DRYRUN)
exit(0);
execvp("ar", cmd);
err(EXIT_FAILURE, "failed to exec ar");
}