kit

kit
git clone https://git.ryansepassi.com/git/kit.git
Log | Files | Refs | README

commit 607986aef63ad1b10404361bf82862b70cb8ba65
parent bff67bc795e315aa52e3a298aee4131f6ccd37f2
Author: Ryan Sepassi <rsepassi@gmail.com>
Date:   Wed, 27 May 2026 09:48:30 -0700

driver: add dist target and move rt cache to DriverEnv

- RELEASE=1 now defaults BUILD_DIR to build/release
- make dist builds release binary + rt sources into build/dist/cfree.tar.gz
  with layout cfree/{cfree,support/rt/{include,lib}}
- DriverEnv gains cache_dir, initialized from $XDG_CACHE_HOME/cfree or
  ~/.cache/cfree; rt_ensure_archive reads env->cache_dir instead of
  deriving the path from DriverRuntimeSupport.cache_root
- Remove DriverRuntimeSupport.cache_root and rt_build_cache_root
- rt_try_support_root now only probes root/rt, never root itself
- CWD rt probe changed from "rt" to "." to match the new layout rule

Diffstat:
MMakefile | 25++++++++++++++++++++++---
Mdriver/env.c | 15+++++++++++++++
Mdriver/env.h | 1+
Mdriver/runtime.c | 75+++++++++++++--------------------------------------------------------------
Mdriver/runtime.h | 2--
5 files changed, 51 insertions(+), 67 deletions(-)

diff --git a/Makefile b/Makefile @@ -1,9 +1,13 @@ CC = clang AR = ar LD = ld +RELEASE ?= 0 +ifeq ($(RELEASE),1) +BUILD_DIR ?= build/release +else BUILD_DIR ?= build +endif SYSROOT = $(shell xcrun --show-sdk-path) -RELEASE ?= 0 HOST_UNAME := $(shell uname -s) .DEFAULT_GOAL := all @@ -355,10 +359,14 @@ DRIVER_DEPS = $(DRIVER_OBJS:.o=.d) LIB_AR = $(BUILD_DIR)/libcfree.a BIN = $(BUILD_DIR)/cfree +DIST_STAGING = build/dist/cfree +DIST_TARBALL = build/dist/cfree.tar.gz + .PHONY: \ all \ lib \ bin \ + dist \ format \ clean \ bootstrap \ @@ -496,8 +504,19 @@ $(BOOTSTRAP_STAMP): $(BIN) $(BOOTSTRAP_RT_LIBS) $(BOOTSTRAP_MAKEFILES) bootstrap-test-toy: bootstrap-debug @CFREE='$(abspath $(BUILD_DIR)/debug/bootstrap/stage3/cfree)' test/toy/run.sh -bench-opt: bin - @bash scripts/opt_bench.sh +bench-opt: + $(MAKE) RELEASE=1 bin + @CFREE='$(abspath build/release/cfree)' bash scripts/opt_bench.sh + +dist: + $(MAKE) RELEASE=1 bin + @rm -rf $(DIST_STAGING) + @mkdir -p $(DIST_STAGING)/support/rt + cp build/release/cfree $(DIST_STAGING)/cfree + cp -r rt/include $(DIST_STAGING)/support/rt/include + cp -r rt/lib $(DIST_STAGING)/support/rt/lib + cd build/dist && tar czf cfree.tar.gz cfree + @echo "dist: $(abspath $(DIST_TARBALL))" format: find src include driver lang test rt -path test/pp -prune -o \( -name '*.c' -o -name '*.h' \) -print | xargs clang-format -i --style=google diff --git a/driver/env.c b/driver/env.c @@ -1033,6 +1033,8 @@ static void jit_tls_ctx_destroy(void* user, void* ctx_v) { static CfreeJitTls g_jit_tls_posix; +static char g_cache_dir[4096]; + /* ---------------- writer (fd-backed) ---------------- */ typedef struct DriverFdWriter { @@ -1264,6 +1266,19 @@ void driver_env_init(DriverEnv* e) { e->metrics = NULL; + { + const char* xdg = getenv("XDG_CACHE_HOME"); + const char* home = getenv("HOME"); + if (xdg && *xdg) { + snprintf(g_cache_dir, sizeof(g_cache_dir), "%s/cfree", xdg); + } else if (home && *home) { + snprintf(g_cache_dir, sizeof(g_cache_dir), "%s/.cache/cfree", home); + } else { + snprintf(g_cache_dir, sizeof(g_cache_dir), "build/cfree-cache"); + } + e->cache_dir = g_cache_dir; + } + /* Reproducible-build precedent: SOURCE_DATE_EPOCH wins over wall clock. * If neither is set or the env value doesn't parse, advertise -1 ("no * clock") and pp falls back to C11 placeholders. */ diff --git a/driver/env.h b/driver/env.h @@ -26,6 +26,7 @@ typedef struct DriverEnv { const CfreeJitTls* jit_tls; /* NULL unless `cfree run` w/ TLV paths run */ const CfreeMetrics* metrics; /* optional scoped metrics sink */ int64_t now; /* unix seconds; -1 = unknown */ + const char* cache_dir; /* base cache dir, e.g. ~/.cache/cfree */ } DriverEnv; void driver_env_init(DriverEnv*); diff --git a/driver/runtime.c b/driver/runtime.c @@ -163,41 +163,6 @@ static void rt_store_const_ptr(const char** slot, const char* value) { *slot = value; } -static int rt_basename_is(const char* path, const char* name) { - const char* base; - const char* end; - size_t n; - size_t len; - if (!path || !name) return 0; - end = path + driver_strlen(path); - while (end > path && end[-1] == '/') --end; - base = end; - while (base > path && base[-1] != '/') --base; - n = (size_t)(end - base); - len = driver_strlen(name); - if (n != len) return 0; - while (len) { - if (*base++ != *name++) return 0; - --len; - } - return 1; -} - -static char* rt_build_cache_root(DriverEnv* env, const char* support_root, - int runtime_root_is_support, - size_t* out_size) { - char* parent; - char* cache_root; - size_t parent_size = 0; - if (runtime_root_is_support && rt_basename_is(support_root, "rt")) { - parent = rt_join(env, support_root, "..", &parent_size); - if (!parent) return NULL; - cache_root = rt_join(env, parent, "build/rt", out_size); - driver_free(env, parent, parent_size); - return cache_root; - } - return rt_join(env, support_root, "build/rt", out_size); -} static void rt_free_str(DriverEnv* env, char** p, size_t* n) { if (*p) driver_free(env, *p, *n); @@ -220,14 +185,11 @@ static int rt_has_layout(const char* rt_root) { } static int rt_set_layout(DriverEnv* env, DriverRuntimeSupport* out, - const char* support_root, const char* rt_root, - const char* cache_root) { + const char* support_root, const char* rt_root) { out->support_root = rt_dup(env, support_root, &out->support_root_size); out->rt_root = rt_dup(env, rt_root, &out->rt_root_size); out->include_dir = rt_join(env, rt_root, "include", &out->include_dir_size); - out->cache_root = rt_dup(env, cache_root, &out->cache_root_size); - if (!out->support_root || !out->rt_root || !out->include_dir || - !out->cache_root) { + if (!out->support_root || !out->rt_root || !out->include_dir) { driver_runtime_support_fini(env, out); return 1; } @@ -237,28 +199,14 @@ static int rt_set_layout(DriverEnv* env, DriverRuntimeSupport* out, static int rt_try_support_root(DriverEnv* env, const char* root, DriverRuntimeSupport* out) { char* rt_root; - char* cache_root; size_t rt_root_size = 0; - size_t cache_root_size = 0; - int ok = 1; - - if (rt_has_layout(root)) { - cache_root = rt_build_cache_root(env, root, 1, &cache_root_size); - if (!cache_root) return 1; - ok = rt_set_layout(env, out, root, root, cache_root); - driver_free(env, cache_root, cache_root_size); - return ok; - } + int ok; rt_root = rt_join(env, root, "rt", &rt_root_size); - cache_root = rt_build_cache_root(env, root, 0, &cache_root_size); - if (!rt_root || !cache_root) ok = 0; - if (ok && rt_has_layout(rt_root)) - ok = rt_set_layout(env, out, root, rt_root, cache_root) == 0; - else - ok = 0; - if (rt_root) driver_free(env, rt_root, rt_root_size); - if (cache_root) driver_free(env, cache_root, cache_root_size); + if (!rt_root) return 1; + ok = rt_has_layout(rt_root) && + rt_set_layout(env, out, root, rt_root) == 0; + driver_free(env, rt_root, rt_root_size); return ok ? 0 : 1; } @@ -332,7 +280,7 @@ int driver_runtime_resolve(DriverEnv* env, const char* explicit_support_dir, if (explicit_support_dir) { rc = rt_try_support_root(env, explicit_support_dir, out); - } else if (rt_try_support_root(env, "rt", out) == 0) { + } else if (rt_try_support_root(env, ".", out) == 0) { rc = 0; } else if (rt_try_argv0_checkout_root(env, argv0, out) == 0) { rc = 0; @@ -344,7 +292,6 @@ int driver_runtime_resolve(DriverEnv* env, const char* explicit_support_dir, } void driver_runtime_support_fini(DriverEnv* env, DriverRuntimeSupport* s) { - rt_free_str(env, &s->cache_root, &s->cache_root_size); rt_free_str(env, &s->include_dir, &s->include_dir_size); rt_free_str(env, &s->rt_root, &s->rt_root_size); rt_free_str(env, &s->support_root, &s->support_root_size); @@ -761,7 +708,11 @@ int driver_runtime_ensure_archive(DriverEnv* env, driver_errf(RT_TOOL, "compiler runtime is not available for this target"); return 1; } - cache_dir = rt_join(env, support->cache_root, variant->key, &cache_dir_size); + if (!env->cache_dir) { + driver_errf(RT_TOOL, "no cache directory configured"); + return 1; + } + cache_dir = rt_join(env, env->cache_dir, variant->key, &cache_dir_size); if (!cache_dir) { driver_errf(RT_TOOL, "out of memory"); return 1; diff --git a/driver/runtime.h b/driver/runtime.h @@ -11,8 +11,6 @@ typedef struct DriverRuntimeSupport { size_t rt_root_size; char* include_dir; size_t include_dir_size; - char* cache_root; - size_t cache_root_size; const char* tool_path; } DriverRuntimeSupport;