From a2eccd422f2234afb452e5fa9c30a3cd4bb10131 Mon Sep 17 00:00:00 2001 From: hkc Date: Sat, 25 Nov 2023 19:21:10 +0300 Subject: [PATCH] macos and something else --- cbt.c | 7 +++-- cbt.h | 96 ++++++++++++++++++++++++++++++++++++++++------------------- 2 files changed, 70 insertions(+), 33 deletions(-) diff --git a/cbt.c b/cbt.c index e2d6a4b..42f91fe 100644 --- a/cbt.c +++ b/cbt.c @@ -1,4 +1,4 @@ -// x-run: python3 ./add-impl.py && ~/scripts/runc.sh % -Wall -Wextra +// x-run: python3 ./add-impl.py && ~/scripts/runc.sh % -Wall -Wextra -X-gdb #define CBT_IMPLEMENTATION #include "cbt.h" @@ -7,8 +7,9 @@ int main(int argc, char **argv) { cbt_shell(CBT_PROC_AUTO, "sh", "-c", "echo 'owo'"); - struct cbt_lib raylib = cbt_lib("raylib", "./raylib"); - cbt_lib_src(&raylib, "rcore.c"); + // example usage, won't actually work + struct cbt_lib raylib = cbt_lib("raylib"); + cbt_lib_src(&raylib, "./raylib/*.c"); cbt_lib_ldflags(&raylib, "-lm"); cbt_lib_cflags(&raylib, "-Wall", "-Wextra"); cbt_lib_build(raylib, NULL); diff --git a/cbt.h b/cbt.h index 9d9090e..a052240 100644 --- a/cbt.h +++ b/cbt.h @@ -15,7 +15,7 @@ enum cbt_loglevel { CBT_LOG_ALL }; -struct cbt_chararray { +struct cbt_strarr { char **items; size_t size, cap; }; @@ -40,7 +40,7 @@ bool cbt_needs_recompilation(const char *input, const char *output); const char *cbt_escape_shell(char *arg); const char *cbt_escape_args(char **args); -const char *cbt_escape_argsd(struct cbt_chararray arr); +const char *cbt_escape_argsd(struct cbt_strarr arr); #ifndef CBT_FMT_NBUFS #define CBT_FMT_NBUFS 64 @@ -74,21 +74,21 @@ enum cbt_proc_mode { #define cbt_shell(...) cbt_proc_wait(_cbt_proc_new(__VA_ARGS__, NULL)) struct cbt_proc _cbt_proc_new(enum cbt_proc_mode mode, ...); struct cbt_proc cbt_proc_newv(enum cbt_proc_mode mode, va_list args); +struct cbt_proc cbt_proc_newd(enum cbt_proc_mode mode, struct cbt_strarr args); int cbt_proc_wait(struct cbt_proc proc); enum cbt_libtype { CBT_LIB_STATIC, CBT_LIB_SHARED }; struct cbt_lib { enum cbt_libtype type; - const char *basepath; const char *libname; - struct cbt_chararray sources, cflags, ldflags; + struct cbt_strarr sources, cflags, ldflags; }; #define cbt_lib_src(LIB, ...) _cbt_lib_src(LIB, __VA_ARGS__, NULL) #define cbt_lib_cflags(LIB, ...) _cbt_lib_cflags(LIB, __VA_ARGS__, NULL) #define cbt_lib_ldflags(LIB, ...) _cbt_lib_ldflags(LIB, __VA_ARGS__, NULL) -struct cbt_lib cbt_lib(const char *libname, const char *basepath); +struct cbt_lib cbt_lib(const char *libname); void _cbt_lib_src(struct cbt_lib *lib, ...); void _cbt_lib_cflags(struct cbt_lib *lib, ...); void _cbt_lib_ldflags(struct cbt_lib *lib, ...); @@ -97,7 +97,7 @@ void cbt_lib_free(struct cbt_lib lib); struct cbt_binary { const char *out_path; - struct cbt_chararray sources, cflags, ldflags; + struct cbt_strarr sources, cflags, ldflags; }; #define cbt_binary_add_src(B, ...) _cbt_binary_add_src(B, __VA_ARGS__, NULL) @@ -141,12 +141,13 @@ void cbt_binary_free(struct cbt_binary bin); memmove(&OUT.items[offset], IN.items, sizeof(OUT.items[0]) * IN.size); \ } -#define cbt_da_copy(OUT, IN, T) { \ - OUT.size = IN.size; \ - OUT.cap = IN.size; \ - OUT.items = malloc(sizeof(IN.items[0]) * IN.size); \ - memcpy(OUT.items, IN.items, sizeof(IN.items[0]) * IN.size); \ -} +#define cbt_da_copy(OUT, IN, T) \ + { \ + OUT.size = IN.size; \ + OUT.cap = IN.size; \ + OUT.items = malloc(sizeof(IN.items[0]) * IN.size); \ + memcpy(OUT.items, IN.items, sizeof(IN.items[0]) * IN.size); \ + } #ifdef CBT_IMPLEMENTATION //-*- begin cbt_impl.c 5 @@ -155,7 +156,9 @@ void cbt_binary_free(struct cbt_binary bin); #include #include #include +#ifdef __APPLE__ #include +#endif #include #include #include @@ -182,6 +185,11 @@ void cbt_binary_free(struct cbt_binary bin); abort(); \ } + +#ifndef PATH_MAX +#define PATH_MAX 1024 +#endif + enum cbt_loglevel cbt_verbosity = CBT_LOG_INFO; char *cbt_cc = NULL; char cbt_location[PATH_MAX] = {0}; @@ -355,7 +363,7 @@ const char *cbt_escape_args(char **args) { return out; } -const char *cbt_escape_argsd(struct cbt_chararray arr) { +const char *cbt_escape_argsd(struct cbt_strarr arr) { if (arr.size == 0 || arr.items[0] == NULL) return cbt_fmt(""); char *out = (char *)cbt_escape_shell(arr.items[0]); @@ -373,8 +381,8 @@ unsigned long cbt_get_modtime(const char *filename) { if (ret == -1) return -1; - cbt_log(CBT_LOG_DEBUG, "stat(%s) -> %ld", filename, st.st_mtim.tv_sec); - return st.st_mtim.tv_sec; + cbt_log(CBT_LOG_DEBUG, "stat(%s) -> %ld", filename, st.st_mtime); + return st.st_mtime; } bool cbt_needs_recompilation(const char *input, const char *output) { @@ -414,9 +422,8 @@ struct cbt_proc _cbt_proc_new(enum cbt_proc_mode mode, ...) { } struct cbt_proc cbt_proc_newv(enum cbt_proc_mode mode, va_list args) { - struct cbt_proc procinfo = {0}; - struct cbt_chararray args_da = {0}; + struct cbt_strarr args_da = {0}; while (true) { const char *arg = va_arg(args, const char *); if (arg == NULL) @@ -425,8 +432,13 @@ struct cbt_proc cbt_proc_newv(enum cbt_proc_mode mode, va_list args) { } cbt_da_add(args_da, NULL); + return cbt_proc_newd(mode, args_da); +} + +struct cbt_proc cbt_proc_newd(enum cbt_proc_mode mode, struct cbt_strarr args) { + struct cbt_proc procinfo = {0}; cbt_log(CBT_LOG_DEBUG, "Spawning process with mode %d and args %s", mode, - cbt_escape_args(args_da.items)); + cbt_escape_args(args.items)); int pipe_fds_sout[2] = {0}, pipe_fds_serr[2] = {0}, pipe_fds_sin[2] = {0}; if (mode & CBT_PROC_W) @@ -447,7 +459,7 @@ struct cbt_proc cbt_proc_newv(enum cbt_proc_mode mode, va_list args) { CBT_FAIL(dup2(pipe_fds_sout[1], STDOUT_FILENO) == -1); CBT_FAIL(dup2(pipe_fds_serr[1], STDERR_FILENO) == -1); - execvp(args_da.items[0], (char *const *)args_da.items); + execvp(args.items[0], (char *const *)args.items); __builtin_unreachable(); } else { if (mode & CBT_PROC_W) @@ -471,10 +483,10 @@ struct cbt_proc cbt_proc_newv(enum cbt_proc_mode mode, va_list args) { cbt_proc_new_cleanup: cbt_log(CBT_LOG_DEBUG, "freeing up args"); - for (size_t i = 0; i < args_da.size; i++) { - free(args_da.items[i]); + for (size_t i = 0; i < args.size; i++) { + free(args.items[i]); } - free(args_da.items); + free(args.items); return procinfo; } @@ -522,7 +534,7 @@ void *cbt__line_processor(void *arg) { while (cbt_running) { pthread_mutex_lock(&_cbt__autoproc_mut); -#if 1 +#if 1 || defined(__APPLE__) int res = ppoll(cbt__autoproc_fdset.items, cbt__autoproc_fdset.size, &timeout, &sigmask); if (res < 0) { @@ -557,9 +569,8 @@ void *cbt__line_processor(void *arg) { return NULL; } -struct cbt_lib cbt_lib(const char *libname, const char *basepath) { +struct cbt_lib cbt_lib(const char *libname) { return (struct cbt_lib){.type = CBT_LIB_STATIC, - .basepath = basepath, .libname = libname, .cflags = {0}, .ldflags = {0}, @@ -578,8 +589,7 @@ void _cbt_lib_src(struct cbt_lib *lib, ...) { glob_t globbuf; for (char *arg = va_arg(args, char *); arg; arg = va_arg(args, char *)) { - glob(cbt_fmt("%s/%s", lib->basepath, arg), - GLOB_APPEND | GLOB_BRACE | GLOB_NOCHECK, _cbt_globerr, &globbuf); + glob(arg, GLOB_APPEND | GLOB_BRACE, _cbt_globerr, &globbuf); } for (size_t i = 0; i < globbuf.gl_pathc; i++) { @@ -610,13 +620,18 @@ void _cbt_lib_ldflags(struct cbt_lib *lib, ...) { } bool cbt_lib_build(struct cbt_lib lib, const char *compiler) { - cbt_log(CBT_LOG_INFO, "library: %s in %s", lib.libname, lib.basepath); + cbt_log(CBT_LOG_INFO, "library: %s", lib.libname); cbt_log(CBT_LOG_INFO, " cflags: %s", cbt_escape_argsd(lib.cflags)); cbt_log(CBT_LOG_INFO, " ldflags: %s", cbt_escape_argsd(lib.ldflags)); cbt_log(CBT_LOG_INFO, " sources: %s", cbt_escape_argsd(lib.sources)); cbt_log(CBT_LOG_INFO, " cc: %s", compiler ? compiler : cbt_cc); - struct cbt_chararray args = {0}; + if (lib.sources.size == 0) { + cbt_log(CBT_LOG_WARNING, "cbt_lib_build(): no sources for %s", lib.libname); + return false; + } + + struct cbt_strarr args = {0}; cbt_da_add(args, (char *)(compiler ? compiler : cbt_cc)); cbt_da_merge(args, lib.cflags); @@ -624,7 +639,28 @@ bool cbt_lib_build(struct cbt_lib lib, const char *compiler) { cbt_log(CBT_LOG_INFO, "args.size: %d", args.size); cbt_log(CBT_LOG_INFO, "CMD: %s", cbt_escape_argsd(args)); - CBT_TODO("cbt_lib_build()"); + cbt_da_add(args, NULL); + cbt_da_add(args, "-o"); + cbt_da_add(args, NULL); + + for (size_t i = 0; i < lib.sources.size; i++) { + args.items[args.size - 3] = lib.sources.items[i]; + args.items[args.size - 1] = ".o"; // TODO + struct cbt_proc proc = cbt_proc_newd(CBT_PROC_AUTO, args); + if (cbt_proc_wait(proc) != 0) { + cbt_log(CBT_LOG_ERROR, "build failed: %s", cbt_escape_argsd(args)); + return false; + } + } + + char **argp = &args.items[args.size - 1]; + cbt_da_merge(args, lib.ldflags); + + *argp = (char *)cbt_fmt("lib%s.so", lib.libname); + + // todo: link + + return false; } void cbt_lib_free(struct cbt_lib lib) {