summaryrefslogtreecommitdiff
path: root/core/bop.c
blob: 2248775116368093f099d3795e988ed06aa43a21 (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
/*
 * Copyright (c) 2026, Chloe M., et al.
 * Provided under the BSD-3 clause.
 */

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include "bop/common.h"
#include "bop/hook.h"
#include "bop/build.h"

static void
version(void)
{
    printf("Version: %s\n", BOP_VERSION);
}

static void
help(void)
{
    printf("usage: %s=<dir> ./bop [flags] [build_op]\n", BOP_BUILD_ENV);
    printf("-- flags --\n");
    printf("[-h]    Display this help menu\n");
    printf("[-v]    Display the version\n");
    printf("-- build operations\n");
    printf("[build]     Build the directory specified by %s\n", BOP_BUILD_ENV);
    printf("[clean]     Clean all object files\n");
}

/*
 * Convert an operations string to a build_op_t type
 */
static inline build_op_t
opstr_to_op(const char *opstr)
{
    if (opstr == NULL) {
        return BUILD_OP_BUILD;
    }

    switch (*opstr) {
    case 'b':
        if (strcmp(opstr, "build") == 0) {
            return BUILD_OP_BUILD;
        }

        break;
    case 'c':
        if (strcmp(opstr, "clean") == 0) {
            return BUILD_OP_CLEAN;
        }

        break;
    }

    return BUILD_OP_BUILD;
}

/*
 * Discover any possible hooks to be ran in the desired build directory and
 * begin the build process.
 *
 * @bop:  Build operation
 */
static int
run_build(build_op_t bop)
{
    char pathbuf[256];
    char *build_dir;
    int error;

    build_dir = getenv(BOP_BUILD_ENV);
    if (build_dir == NULL) {
        printf("fatal: build directory not specified in %s\n", BOP_BUILD_ENV);
        return -1;
    }

    /*
     * There is a pre-build hook to be located within the [BUILD_DIR]/.bop/prehook.sh which
     * is simply a shell script to be executed. Only run it if we aren't cleaning up the build
     * space.
     */
    if (bop != BUILD_OP_CLEAN) {
        snprintf(pathbuf, sizeof(pathbuf), "%s/.bop/prehook.sh", build_dir);
        if (access(pathbuf, F_OK) == 0)
            bop_shell_hook(pathbuf);
    }

    error = bop_build_dir(build_dir, bop);
    if (error < 0) {
        printf("fatal: failed to build '%s'\n", build_dir);
        return error;
    }

    /* Run the post hook if we can */
    if (bop != BUILD_OP_CLEAN) {
        snprintf(pathbuf, sizeof(pathbuf), "%s/.bop/posthook.sh", build_dir);
        if (access(pathbuf, F_OK) == 0)
            bop_shell_hook(pathbuf);
    }

    return 0;
}

int
main(int argc, char **argv)
{
    int opt;
    char *build_opt;
    build_op_t bop;

    if (argc < 2) {
        printf("fatal: too few arguments\n");
        help();
        return -1;
    }

    while ((opt = getopt(argc, argv, "hv")) != -1) {
        switch (opt) {
        case 'h':
            help();
            return -1;
        case 'v':
            version();
            return -1;
        }
    }

    if (optind >= argc) {
        printf("fatal: please specify a build operation\n");
        help();
        return -1;
    }

    build_opt = argv[optind++];
    bop = opstr_to_op(build_opt);
    return run_build(bop);
}