summaryrefslogtreecommitdiff
path: root/core/build.c
blob: d0a4627c4dbe4983c503b0742c0d3b035a35fb01 (plain)
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*
 * Copyright (c) 2026, Chloe M., et al.
 * Provided under the BSD-3 clause.
 */

#include <dirent.h>
#include <stdint.h>
#include <stdlib.h>
#include <stddef.h>
#include <fcntl.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
 */
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)
{
    char *trunc_ext, *cflags;
    char cmdbuf[256];
    char outpath[256];
    char outbuf[32];
    FILE *pipe;

    if (cfile_path == NULL || params == NULL) {
        return -1;
    }

    trunc_ext = trunc_extension(cfile_path);
    if (trunc_ext == NULL) {
        return -1;
    }

    cflags = (params->cflags == NULL) ? "" : params->cflags;
    snprintf(outpath, sizeof(outpath), "%s.o", trunc_ext);
    snprintf(cmdbuf, sizeof(cmdbuf), "%s -c %s %s -o %s", BOP_CC, cflags, cfile_path, outpath);

    if ((pipe = popen(cmdbuf, "r")) == NULL) {
        printf("fatal: failed to invoke cc\n");
        return -1;
    }

    printf("[CC]\t\t%s\n", cfile_path);
    while ((fgets(outbuf, sizeof(outbuf), pipe)) != NULL) {
        printf("%s", outbuf);
    }

    pclose(pipe);
    return 0;
}

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);
}

/*
 * Initialize the cflags field within build parameters
 *
 * @dirpath:  Target directory path
 * @params:   Build parameters
 *
 * Returns zero on success
 */
static int
build_init_cflags(const char *dirpath, struct build_params *params)
{
    char pathbuf[256], *databuf;
    size_t len, fsize;
    int fd;

    if (params == NULL) {
        return -1;
    }

    snprintf(pathbuf, sizeof(pathbuf), "%s/.bop/cflags", dirpath);
    if ((fd = open(pathbuf, O_RDONLY)) < 0) {
        /* Fake success, cflags are optional */
        return 0;
    }

    /* Grab the file size */
    fsize = lseek(fd, 0, SEEK_END);
    lseek(fd, 0, SEEK_SET);

    /* Allocate the data buffer */
    if ((databuf = malloc(fsize)) == NULL) {
        printf("fatal: out of memory\n");
        close(fd);
        return -1;
    }

    if ((len = read(fd, databuf, fsize)) < 0) {
        perror("read");
        printf("fatal: failed to read %s\n", pathbuf);
        close(fd);
        return -1;
    }

    databuf[fsize - 1] = '\0';
    params->cflags = databuf;
    close(fd);
    return 0;
}

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(&params, sizeof(params));
    if ((error = build_init_cflags(dirpath, &params)) < 0) {
        closedir(dir);
        return error;
    }

    while ((dirent = readdir(dir)) != NULL) {
        if (dirent->d_name[0] == '.') {
            continue;
        }

        switch (bop) {
        case BUILD_OP_BUILD:
            error = compile(dirpath, dirent, &params);
            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;
        }

    }

    if (params.cflags != NULL) {
        free(params.cflags);
        params.cflags = NULL;
    }

    return 0;
}