feat(compiler): resolve the install root from the binary's location

The engine runtime and the bundled ludic.* packages belong to the toolchain,
not to the project, so the compiler has to know where the toolchain lives. It
derived that from the directory the binary sits in, which is `bin` — so an
in-repo build only found runtime/native/cocoa.ll when the caller set
LUDIC_HOME=., and an installed compiler had no way to find it at all.

ludic_home() derives it properly instead: $LUDIC_HOME when set, else the
binary's directory with a trailing `bin/` stripped, else a $PATH scan for
argv[0] (an install is invoked by bare name, which carries no directory). Both
layouts that exist then have the same shape — ~/.ludic/{bin,runtime,packages}
and a repo checkout — so the same rule serves both and LUDIC_HOME becomes an
override rather than a requirement.

`import "ludic.core/…"` also falls back to $LUDIC_HOME/packages, after the
project's own ludic_modules/, so a project that has not fetched its own copy
gets the packages that shipped with the toolchain instead of a symlink farm.

The `ludic` multi-call name is dropped from the compiler: that name now belongs
to the CLI, and --run is the flag it drives.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Orkun ÇAKILKAYA 2026-09-05 22:01:39 +03:00
parent 7e07573026
commit 005cc39394
4 changed files with 20115 additions and 19835 deletions

View file

@ -554,6 +554,51 @@ function join_path(dir: pointer, rel: pointer) -> pointer {
return dir + rel
}
# The toolchain install root, with a trailing '/' ("" meaning the current
# directory). Everything the toolchain owns rather than the project — the engine
# runtime under runtime/native, the bundled ludic.* packages, the VERSION file,
# cocoa.ll — is resolved against it.
#
# $LUDIC_HOME wins when it is set. Otherwise it is derived from the compiler
# binary, whose directory is `bin` in both shapes that exist: an install
# (~/.ludic/bin/ludicc, root ~/.ludic) and a repo checkout (bin/ludicc, root the
# repo). So a directory named bin means the root is its parent; anything else is
# taken as the root itself, which is what a loose binary beside its runtime
# wants. Invoked through $PATH argv[0] carries no directory at all, so the PATH
# entries are searched for the binary first — an installed `ludicc mygame.ludic`
# has to find its own runtime with nothing in the environment.
function ludic_home() -> pointer {
let env = getenv("LUDIC_HOME")
if (env != null) { return ensure_slash(env) }
var d = dir_of(arg(0))
if (len(d) == 0) { d = path_lookup_dir(base_name(arg(0))) }
if (len(d) == 0) { return "" }
if (len(d) >= 4) and (d[len(d) - 4..len(d)] == "bin/") { return d[0..len(d) - 4] }
return d
}
# the first $PATH entry holding an openable file called `name`, with a trailing
# '/' — "" when $PATH is unset or nothing matches.
function path_lookup_dir(name: pointer) -> pointer {
let p = getenv("PATH")
if (p == null) { return "" }
let n = len(p)
var start = 0
var i = 0
while i <= n {
if (i == n) or (p[i] == ':') {
if i > start {
let dir = ensure_slash(p[start..i])
let f = file_open(dir + name, "rb")
if (f != null) { file_close(f); return dir }
}
start = i + 1
}
i += 1
}
return ""
}
var loaded_paths: []pointer
var cur_dir: pointer
var g_uses_regex: bool = false # a program mentioned Regex.* -> splice the regex runtime
@ -816,7 +861,7 @@ function parse_test() -> Node {
# under the package module root — $LUDIC_MODULES, default "ludic_modules" — so a
# fetched source package resolves by its import path, e.g.
# import "git.workshopsoft.io/user/pkg/foo.ludic"
# materialised by `x get` at ludic_modules/git.workshopsoft.io/user/pkg/foo.ludic.
# materialised by `ludic get` at ludic_modules/git.workshopsoft.io/user/pkg/foo.ludic.
# The fallback only fires when the local path does not exist, so every existing
# import resolves exactly as before (the emitted IR is byte-identical).
# true when `rel` is an engine-runtime splice path ("runtime/..."), as opposed to
@ -868,8 +913,7 @@ function do_import(rel: pointer) -> void {
# place main.ludic finds cocoa.ll / audio.ll. This is tried before the package
# module root, so $LUDIC_MODULES / ludic_modules holds only third-party packages.
if (src == null) and (rel[0] != '/') and is_runtime_path(rel) {
let home = ensure_slash(getenv_or("LUDIC_HOME", dir_of(arg(0))))
let hc = home + rel
let hc = ludic_home() + rel
let hs = read_file(hc)
if (hs != null) { full = hc; src = hs }
}
@ -878,6 +922,16 @@ function do_import(rel: pointer) -> void {
let s2 = read_file(cand)
if (s2 != null) { full = cand; src = s2 }
}
# The ludic.* packages ship with the toolchain too, so an installed compiler
# resolves `import "ludic.core/components.ludic"` from $LUDIC_HOME/packages
# with no ludic_modules/ to set up. It is the last candidate on purpose: a
# project that fetched its own copy of a package (ludic_modules, above) keeps
# the version it pinned.
if (src == null) and (rel[0] != '/') {
let pc = ludic_home() + "packages/" + rel
let ps = read_file(pc)
if (ps != null) { full = pc; src = ps }
}
if already_loaded(full) { return }
push(loaded_paths, full)
if (src == null) { perr(`cannot open import {full}`) }