aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Voss <mail@thomasvoss.com> 2023-12-30 22:55:20 +0100
committerThomas Voss <mail@thomasvoss.com> 2023-12-30 22:55:20 +0100
commit638f267d5b71a0eed37ae72a2760799fbe029988 (patch)
treea93159e34a8a7ffc44237b4b41d6c3f07819e335
parent5fb26cffd50b0b226483663ad3a0c7608d4aacc9 (diff)
Make rebuild() work from other directories
-rw-r--r--cbs.h39
1 files changed, 34 insertions, 5 deletions
diff --git a/cbs.h b/cbs.h
index 789f532..6be3504 100644
--- a/cbs.h
+++ b/cbs.h
@@ -54,6 +54,8 @@
#include <sys/wait.h>
#include <errno.h>
+#include <libgen.h>
+#include <limits.h>
#ifdef CBS_PTHREAD
# include <pthread.h>
#endif
@@ -210,10 +212,10 @@ static bool foutdatedv(const char *base, const char **p, size_t n);
foutdatedv(s, (const char **)_vtoa(__VA_ARGS__), \
lengthof(_vtoa(__VA_ARGS__)))
-/* Rebuild the build script if either it, or this header file have been
- modified, and execute the newly built script. You should call the rebuild()
- macro at the very beginning of main(), but right after cbsinit(). You
- probably don’t want to call _rebuild() directly. */
+/* Rebuild the build script if it has been modified, and execute the newly built
+ script. You should call the rebuild() macro at the very beginning of main(),
+ but right after cbsinit(). You probably don’t want to call _rebuild()
+ directly. */
static void _rebuild(char *);
#define rebuild() _rebuild(__FILE__)
@@ -571,13 +573,40 @@ foutdatedv(const char *src, const char **deps, size_t n)
return false;
}
+static char *
+_getcwd(void)
+{
+ char *buf = NULL;
+ size_t n = 0;
+
+ for (;;) {
+ n += PATH_MAX;
+ buf = bufalloc(buf, n, sizeof(char));
+ if (getcwd(buf, n))
+ break;
+ if (errno != ERANGE)
+ die("getcwd");
+ }
+
+ return buf;
+}
+
void
_rebuild(char *src)
{
+ char *cwd;
cmd_t cmd = {0};
- if (fmdnewer(*_cbs_argv, src) && fmdnewer(*_cbs_argv, __FILE__))
+ cwd = _getcwd();
+
+ if (chdir(dirname(src)) == -1)
+ die("chdir: %s", dirname(src));
+ if (foutdated(*_cbs_argv, src)) {
+ if (chdir(cwd) == -1)
+ die("chdir: %s", cwd);
return;
+ }
+ free(cwd);
cmdadd(&cmd, "cc");
#ifdef CBS_PTHREAD