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
|
/*
* Copyright (c) 2026, Chloe M., et al.
* Provided under the BSD-3 clause.
*/
#include <sys/wait.h>
#include <dirent.h>
#include <stdint.h>
#include <stdlib.h>
#include <stddef.h>
#include <stdbool.h>
#include <strings.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include "bop/common.h"
#include "bop/build.h"
/*
* Build parameters
*
* @cflags: List of CFLAGS
*
* TODO: Use this instead of BOP_DEFAULT_CFLAGS and similar
*/
struct build_params {
char **cflags;
};
/*
* Obtain the file extension of a file
*
* @filename: Filename to obtain extension of
*/
static const char *
get_extension(const char *filename)
{
const char *p;
bool have_dot = false;
if (filename == NULL) {
return NULL;
}
p = filename;
while (*p != '\0') {
if (*p == '.') {
have_dot = true;
} else if (have_dot) {
--p;
return p;
}
++p;
}
return NULL;
}
/*
* Truncate a file extension
*
* @filename: Filename to truncate file extension from
*
* XXX: Returns memory allocated with strdup(), remember to free!
*/
static char *
trunc_extension(const char *filename)
{
char *p, *p1;
if ((p = strdup(filename)) == NULL) {
return NULL;
}
p1 = p;
while (*p1 != '\0') {
if (*p1 == '.') {
*p1 = '\0';
return p;
}
++p1;
}
free(p);
return NULL;
}
static int
invoke_cc(const char *cfile_path, struct build_params *params)
{
pid_t child;
int status;
char *trunc_ext;
char outpath[256];
if (cfile_path == NULL || params == NULL) {
return -1;
}
trunc_ext = trunc_extension(cfile_path);
if (trunc_ext == NULL) {
return -1;
}
snprintf(outpath, sizeof(outpath), "%s.o", trunc_ext);
child = fork();
if (child == 0) {
printf("[CC]\t\t%s\n", cfile_path);
execl(BOP_CC, BOP_CC, "-c", cfile_path, "-o", outpath, BOP_DEFAULT_CFLAGS, NULL);
} else {
waitpid(child, &status, 0);
free(trunc_ext);
return status;
}
return -1;
}
static int
compile(const char *dirpath, struct dirent *dirent, struct build_params *params)
{
const char *filename, *ext;
char pathbuf[256];
int error;
if (dirpath == NULL || dirent == NULL) {
return -1;
}
/* Ignore non regulars */
if (dirent->d_type != DT_REG) {
return 0;
}
filename = dirent->d_name;
ext = get_extension(filename);
/* Ignore files with no extensions */
if (ext == NULL) {
return 0;
}
snprintf(pathbuf, sizeof(pathbuf), "%s/%s", dirpath, filename);
/* Build all CFILES */
if (strcmp(ext, ".c") == 0) {
error = invoke_cc(pathbuf, params);
if (error < 0)
printf("fatal: cc invocation failure\n");
return error;
}
return 0;
}
static void
clean_object(const char *dirpath, struct dirent *dirent)
{
char pathbuf[256];
const char *filename, *ext;
if (dirpath == NULL || dirent == NULL) {
return;
}
/* Ignore non regulars */
if (dirent->d_type != DT_REG) {
return;
}
/* Ignore files without an extension */
filename = dirent->d_name;
ext = get_extension(filename);
if (ext == NULL) {
return;
}
/* Ignore non object files */
if (strcmp(ext, ".o") != 0) {
return;
}
snprintf(pathbuf, sizeof(pathbuf), "%s/%s", dirpath, dirent->d_name);
printf("clean %s\n", pathbuf);
remove(pathbuf);
}
int
bop_build_dir(const char *dirpath, build_op_t bop)
{
struct build_params params;
struct dirent *dirent;
DIR *dir;
int error;
dir = opendir(dirpath);
if (dir == NULL) {
perror("opendir");
return -1;
}
bzero(¶ms, sizeof(params));
while ((dirent = readdir(dir)) != NULL) {
if (dirent->d_name[0] == '.') {
continue;
}
switch (bop) {
case BUILD_OP_BUILD:
error = compile(dirpath, dirent, ¶ms);
if (error < 0) {
printf("fatal: failed to build '%s'\n", dirent->d_name);
return error;
}
break;
case BUILD_OP_CLEAN:
clean_object(dirpath, dirent);
break;
}
}
return 0;
}
|