# toolchain.ludic — building the toolchain's own binaries. Contributor-only: # this is what `ludic-dev build` does, and none of it ships to a user. # compile a Ludic source to a native binary in bin/ via ludicc + clang (-O2). # Returns true on success. Used for the CLI, the contributor tool and the editor # tools — every binary the toolchain builds of itself. function build_tool(name: pointer, src: pointer) -> bool { let ll = `build/{name}.ll` let out = `bin/{exe_name(name)}` if not shq(`bin/{exe_name("ludicc")} --unsafe {src} --emit-llvm -o {ll} 2>/dev/null`) { print(`build failed: {name} (compile)`); return false } # write to a temp then move, so a running bin/ludic can rebuild itself in place # (Windows lets a running executable be renamed, never replaced, so the old one # steps aside first) if not shq(`{cc()} -O2 {ll} -o {out}.tmp`) { print(`build failed: {name} (link)`); return false } if host_windows() { shell(`rm -f {out}.old; mv -f {out} {out}.old 2>/dev/null; mv -f {out}.tmp {out} && rm -f {ll} {out}.old 2>/dev/null`) } else { shell(`mv -f {out}.tmp {out} && rm -f {ll}`) } return true } # ---- build-cli: the self-hosted front-end binaries --------------------------- # Both ludicc and ludic are the SAME multi-call binary assembled from the IR seed # with clang alone; invoked as `ludic` it compiles-and-runs, as `ludicc` it just # compiles. function cmd_dev_build_cli() -> int { shell("mkdir -p bin build") let seed = seed_file() let bin = `bin/{exe_name("ludicc")}` print(`cc: {seed} -> {bin} (from the IR seed, no C compiler)`) if not shq(`{cc()} {seed} -o {bin}`) { err("ludic: the seed did not assemble\n"); return 1 } shell(`chmod +x {bin}`) print(`done. built {bin}`) return 0 } # ---- build: the whole toolchain -------------------------------------------- # bin/ludic is what a user installs; bin/ludic-dev is this tool, which exists # only in a checkout and is never part of a release. function cmd_dev_build() -> int { if cmd_dev_build_cli() != 0 { return 1 } print("ludicc: tools/ludic-cli/main.ludic -> bin/ludic (the CLI users install)") if not build_tool("ludic", "tools/ludic-cli/main.ludic") { return 1 } print("ludicc: tools/ludic-cli/dev.ludic -> bin/ludic-dev (this tool, rebuilding itself)") if not build_tool("ludic-dev", "tools/ludic-cli/dev.ludic") { return 1 } print("ludicc: tools/ludic-tools/fmt.ludic -> bin/ludic-fmt") if not build_tool("ludic-fmt", "tools/ludic-tools/fmt.ludic") { return 1 } print("ludicc: tools/ludic-tools/lsp.ludic -> bin/ludic-lsp") if not build_tool("ludic-lsp", "tools/ludic-tools/lsp.ludic") { return 1 } sync_vscode_grammar() print("done. toolchain in bin/ (ludicc, ludic, ludic-dev, ludic-fmt, ludic-lsp)") return 0 }