From 708526c3dc1f0dace5a7f518c02c6ab7a6951664 Mon Sep 17 00:00:00 2001 From: Chloe M Date: Sat, 15 Aug 2026 23:01:43 -0500 Subject: build: Add support for cleaning object files Signed-off-by: Chloe M --- core/bop.c | 81 ++++++++++++++++++++++++++++++++++++------------------------ core/build.c | 65 +++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 102 insertions(+), 44 deletions(-) (limited to 'core') diff --git a/core/bop.c b/core/bop.c index 02c0324..2248775 100644 --- a/core/bop.c +++ b/core/bop.c @@ -26,14 +26,45 @@ help(void) 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(void) +run_build(build_op_t bop) { char pathbuf[256]; char *build_dir; @@ -44,55 +75,40 @@ run_build(void) 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. - */ - snprintf(pathbuf, sizeof(pathbuf), "%s/.bop/prehook.sh", build_dir); - if (access(pathbuf, F_OK) == 0) { - bop_shell_hook(pathbuf); + * 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); + 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 */ - snprintf(pathbuf, sizeof(pathbuf), "%s/.bop/posthook.sh", build_dir); - if (access(pathbuf, F_OK) == 0) { - bop_shell_hook(pathbuf); + 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; } -static int -do_build_op(const char *op) -{ - if (op == NULL) { - return -1; - } - - switch (*op) { - case 'b': - if (strcmp(op, "build") == 0) { - return run_build(); - } - - break; - } - - return -1; -} - int main(int argc, char **argv) { int opt; char *build_opt; + build_op_t bop; if (argc < 2) { printf("fatal: too few arguments\n"); @@ -118,5 +134,6 @@ main(int argc, char **argv) } build_opt = argv[optind++]; - return do_build_op(build_opt); + bop = opstr_to_op(build_opt); + return run_build(bop); } diff --git a/core/build.c b/core/build.c index 78ad65a..f4ec00b 100644 --- a/core/build.c +++ b/core/build.c @@ -67,7 +67,7 @@ static char * trunc_extension(const char *filename) { char *p, *p1; - + if ((p = strdup(filename)) == NULL) { return NULL; } @@ -80,7 +80,7 @@ trunc_extension(const char *filename) } ++p1; } - + free(p); return NULL; } @@ -125,12 +125,12 @@ compile(const char *dirpath, struct dirent *dirent, struct build_params *params) 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); @@ -140,8 +140,8 @@ compile(const char *dirpath, struct dirent *dirent, struct build_params *params) } snprintf(pathbuf, sizeof(pathbuf), "%s/%s", dirpath, filename); - - /* Build all CFILES */ + + /* Build all CFILES */ if (strcmp(ext, ".c") == 0) { error = invoke_cc(pathbuf, params); if (error < 0) @@ -153,8 +153,40 @@ compile(const char *dirpath, struct dirent *dirent, struct build_params *params) } +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) +bop_build_dir(const char *dirpath, build_op_t bop) { struct build_params params; struct dirent *dirent; @@ -172,12 +204,21 @@ bop_build_dir(const char *dirpath) if (dirent->d_name[0] == '.') { continue; } - - error = compile(dirpath, dirent, ¶ms); - if (error < 0) { - printf("fatal: failed to build '%s'\n", dirent->d_name); - return error; + + 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; -- cgit v1.2.3