summaryrefslogtreecommitdiff
path: root/core/build.c
diff options
context:
space:
mode:
authorChloe M <chloe@faracom.org>2026-08-15 23:01:43 -0500
committerChloe M <chloe@faracom.org>2026-08-15 23:02:02 -0500
commit708526c3dc1f0dace5a7f518c02c6ab7a6951664 (patch)
treefe379dae23eb1679b1202dea07c2728928448b48 /core/build.c
parent9c80ee7c449fc9ebdfe41c3e4c0dfaa9fb55ccc8 (diff)
build: Add support for cleaning object files
Signed-off-by: Chloe M <chloe@faracom.org>
Diffstat (limited to 'core/build.c')
-rw-r--r--core/build.c65
1 files changed, 53 insertions, 12 deletions
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, &params);
- 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, &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;
}
+
}
return 0;