`ziran learn on|off|status` records the programs this machine builds to native code, as training data for improving Ziran's machine code. It is off by default; while off a build reads one small settings file. With it on, each new native `ziran build` keeps its verified .zx, ELF, `zx2native --emit map` JSON and LIR once, named by SHA-256, under $XDG_DATA_HOME/ziran/learn. A rebuild writes nothing, a compiler that emits different code adds a second ELF beside the first, and the corpus stops growing at its cap (--limit-mb, 1024 MB by default). ZIRAN_LEARN=on|off overrides the switch and ZIRAN_LEARN_DIR moves it. plans/2026-11-15-learning-compiler.md records the next steps: wider recording, instruction-window search, a cost model and Alpha's GPU.
1427 lines
62 KiB
Ziran
1427 lines
62 KiB
Ziran
#import "package_common"
|
|
#import "package_manifest"
|
|
#import "package_artifact"
|
|
#import "package_lock"
|
|
#import "package_map"
|
|
#import "package_add"
|
|
#import "package_guide"
|
|
#import "package_capabilities"
|
|
#import "package_features"
|
|
#import "package_explain"
|
|
#import "package_options"
|
|
#import "package_compile_commands"
|
|
#import "package_template"
|
|
#import "package_script"
|
|
#import "package_bundle_run"
|
|
#import "package_learn"
|
|
#import "file_linux"
|
|
|
|
#if CPU == .ARM64 {
|
|
PkgHostNativeTarget :: "aarch64"
|
|
} else {
|
|
// The current direct native registry has no 32-bit compiler-host CPU row;
|
|
// unsupported hosts therefore reach zx2native's explicit target gap.
|
|
PkgHostNativeTarget :: "x86_64"
|
|
}
|
|
|
|
// `ziran compile-commands [--compiler CC] [--include DIR] [-o FILE] DIR`
|
|
// describes the generated C in DIR to editors. The include directory
|
|
// defaults to the pinned toolchain's under --project, otherwise to the one
|
|
// beside this launcher.
|
|
PkgCompileCommands :: (arguments: []string, project: bool, locked: *PkgLock,
|
|
launcher: string) -> s32 {
|
|
compiler: string = PkgGetenv("CC")
|
|
if compiler.count == 0 { compiler = "cc" }
|
|
include: string = ""
|
|
output: string = ""
|
|
generated: string = ""
|
|
index: s32 = 0
|
|
while index < cast(s32)arguments.count {
|
|
part: string = arguments[index]
|
|
if (part == "--compiler" || part == "--include" || part == "-o") &&
|
|
index + 1 < cast(s32)arguments.count {
|
|
if part == "--compiler" { compiler = arguments[index + 1] }
|
|
if part == "--include" { include = arguments[index + 1] }
|
|
if part == "-o" { output = arguments[index + 1] }
|
|
index += 2
|
|
} else if generated.count == 0 && part.count > 0 && part[0] != cast(u8)45 {
|
|
generated = part
|
|
index += 1
|
|
} else {
|
|
return PkgFail("usage: ziran compile-commands [--compiler CC] [--include DIR] [-o FILE] GENERATED_DIR")
|
|
}
|
|
}
|
|
if generated.count == 0 {
|
|
return PkgFail("usage: ziran compile-commands [--compiler CC] [--include DIR] [-o FILE] GENERATED_DIR")
|
|
}
|
|
if include.count == 0 {
|
|
if project { include = PkgJoin(locked.tool_path, "/include") }
|
|
else {
|
|
// build/bin/ziran, or a variant build such as build/sanitize/bin.
|
|
include = PkgJoin(PkgLauncherDirectory(launcher), "/../../include")
|
|
if !PathExists(PkgJoin(include, "/zir_string.h")) {
|
|
include = PkgJoin(PkgLauncherDirectory(launcher), "/../../../include")
|
|
}
|
|
}
|
|
}
|
|
if !PathExists(PkgJoin(include, "/zir_string.h")) {
|
|
return PkgFail("cannot find Ziran's include directory; pass --include DIR")
|
|
}
|
|
if output.count == 0 { output = PkgJoin(generated, "/compile_commands.json") }
|
|
problem: string = WriteCompileCommands(include, compiler, generated, output)
|
|
if problem.count > 0 { return PkgFail(problem) }
|
|
return 0
|
|
}
|
|
|
|
PkgHasArgument :: (arguments: []string, expected: string) -> bool {
|
|
index: s32 = 0
|
|
while index < cast(s32)arguments.count {
|
|
if arguments[index] == expected { return true }
|
|
index += 1
|
|
}
|
|
return false
|
|
}
|
|
|
|
// A variant launcher built by this pinned compiler uses its own sibling
|
|
// tools. This keeps sanitizer and other variant builds together instead of
|
|
// rebuilding the default toolchain while independent checks are running.
|
|
PkgToolDirectory :: (launcher: string, tool_path: string, tool: string) -> string {
|
|
tool_root := PkgJoin(PkgCanonical(tool_path), "/")
|
|
own := PkgCanonical(launcher)
|
|
directory := PkgLauncherDirectory(launcher)
|
|
if tool_root.count > 1 && own.count > tool_root.count &&
|
|
own[0:tool_root.count] == tool_root &&
|
|
PathExists(PkgJoin3(directory, "/", tool)) {
|
|
return directory
|
|
}
|
|
return PkgJoin(tool_path, "/build/bin")
|
|
}
|
|
|
|
PkgPrepareSubmodules :: (directory: string, offline: bool) -> bool {
|
|
args: [7]string
|
|
args[0] = "git"
|
|
args[1] = "-C"
|
|
args[2] = directory
|
|
args[3] = "submodule"
|
|
args[4] = "status"
|
|
args[5] = "--recursive"
|
|
result: PkgProcessResult = PkgCaptureRaw(args[0:6], "")
|
|
if result.code != 0 { return false }
|
|
at: s64 = 0
|
|
while at < cast(s64)result.text.count {
|
|
if result.text[at] != cast(u8)32 {
|
|
if offline { return false }
|
|
args[4] = "update"
|
|
args[5] = "--init"
|
|
args[6] = "--recursive"
|
|
updated: PkgProcessResult = PkgCapture(args[:], "")
|
|
return updated.code == 0
|
|
}
|
|
while at < cast(s64)result.text.count &&
|
|
result.text[at] != cast(u8)10 { at += 1 }
|
|
at += 1
|
|
}
|
|
return true
|
|
}
|
|
|
|
// Shows each direct dependency, where it comes from, and the import path of
|
|
// every module it exports:
|
|
//
|
|
// kryon [email protected]:kryonlabs/kryon.git master 0123456789ab
|
|
// #import "kryon/Widgets" src/ui/Widgets/module.zi
|
|
PkgList :: (locked: *PkgLock, current: *PkgManifest, selector: string) -> s32 {
|
|
found: bool = false
|
|
index: s32 = 0
|
|
while index < current.dependency_count {
|
|
dependency: *PkgDependency = *current.dependencies[index]
|
|
index += 1
|
|
if selector.count > 0 && !PkgFoldEqual(dependency.alias, selector) {
|
|
continue
|
|
}
|
|
found = true
|
|
target: s32 = PkgFindById(locked, PkgEdgeValue(*locked.root_edges[0],
|
|
locked.root_edge_count, dependency.alias))
|
|
if target < 0 { return PkgFail("package is not locked; run ziran lock") }
|
|
item: *PkgLocked = *locked.packages[target]
|
|
PkgPrint(PkgJoin3(dependency.alias, " ", dependency.git), false)
|
|
PkgPrint(PkgJoin3(" ", dependency.ref, " "), false)
|
|
PkgPrint(item.commit[0:12], false)
|
|
PkgPrint(PkgJoin3("\n checkout ", item.path, "\n"), false)
|
|
if item.source {
|
|
PkgPrint(" source checkout; no Ziran modules\n", false)
|
|
continue
|
|
}
|
|
exported: s32 = 0
|
|
while exported < item.manifest.export_count {
|
|
name: PkgNamedValue = item.manifest.exports[exported]
|
|
line: string = PkgJoin3(" #import \"", dependency.alias, "/")
|
|
line = PkgJoin3(line, name.name, "\"")
|
|
while line.count < 36 { line = PkgJoin(line, " ") }
|
|
PkgPrint(PkgJoin3(line, " ", name.value), false)
|
|
PkgPrint("\n", false)
|
|
exported += 1
|
|
}
|
|
}
|
|
if selector.count > 0 && !found { return PkgFail("no dependency with that name") }
|
|
if selector.count == 0 {
|
|
PkgPrint(PkgJoin3("std ", locked.tool_path, "/std\n"), false)
|
|
PkgPrint(" #import \"std/NAME\" the standard library\n", false)
|
|
}
|
|
return 0
|
|
}
|
|
|
|
// Resolve all selectors before stdout; checkout and forwarding remain shared.
|
|
PkgPaths :: (locked: *PkgLock, selectors: []string, submodules: bool,
|
|
offline: bool) -> s32 {
|
|
if selectors.count == 0 { return PkgFail("usage: ziran pkg paths PACKAGE... [--submodules]") }
|
|
paths := PkgStrings(selectors.count)
|
|
if paths.count != selectors.count { return PkgFail("cannot allocate package paths") }
|
|
index: s32 = 0
|
|
while index < cast(s32)selectors.count {
|
|
selector := selectors[index]
|
|
if selector == "ziran" {
|
|
if submodules { return PkgFail("Ziran has no package submodules") }
|
|
paths[index] = locked.tool_path
|
|
} else {
|
|
selected := PkgSelect(locked, selector)
|
|
if selected < 0 { return PkgFail("package is not locked") }
|
|
paths[index] = locked.packages[selected].path
|
|
}
|
|
index += 1
|
|
}
|
|
if submodules {
|
|
for path: paths {
|
|
if !PkgPrepareSubmodules(path, offline) { return PkgFail("package submodules are missing") }
|
|
}
|
|
}
|
|
PkgPrint("[", false)
|
|
index = 0
|
|
while index < cast(s32)selectors.count {
|
|
if index > 0 { PkgPrint(",", false) }
|
|
PkgPrint("{", false)
|
|
PkgJsonString("selector")
|
|
PkgPrint(":", false)
|
|
PkgJsonString(selectors[index])
|
|
PkgPrint(",", false)
|
|
PkgJsonString("path")
|
|
PkgPrint(":", false)
|
|
PkgJsonString(paths[index])
|
|
PkgPrint("}", false)
|
|
index += 1
|
|
}
|
|
PkgPrint("]\n", false)
|
|
return 0
|
|
}
|
|
|
|
PkgDispatch :: (command: string, arguments: []string, project: bool,
|
|
root: string, locked: *PkgLock, current: *PkgManifest,
|
|
launcher: string, extra_names: []string, extra_values: []string) -> s32 {
|
|
executable: string = ""
|
|
check: bool = command == "check"
|
|
bundle: bool = command == "bundle" || command == "run"
|
|
transport: bool = command == "compact" || command == "expand"
|
|
build: bool = command == "build"
|
|
wasm_build: bool = false
|
|
native_build: bool = false
|
|
zx_build: bool = false
|
|
if check || command == "ir" { executable = "zi2zir" }
|
|
else if command == "api" { executable = "zi-api" }
|
|
else if command == "inspect" { executable = "zi-inspect" }
|
|
else if command == "fmt" { executable = "zi-fmt" }
|
|
else if command == "edit" || transport { executable = "zi-edit" }
|
|
else if bundle { executable = "zi2zx" }
|
|
else if build {
|
|
target: string = ""
|
|
index: s32 = 0
|
|
while index < cast(s32)arguments.count {
|
|
if arguments[index].count > 9 && arguments[index][0:9] == "--target=" {
|
|
target = arguments[index]
|
|
}
|
|
index += 1
|
|
}
|
|
if target == "--target=c" { executable = "zi2c" }
|
|
else if target == "--target=go" { executable = "zi2go" }
|
|
else if target == "--target=wasm" { executable = "zi2zx"; wasm_build = true }
|
|
else if target == "--target=zx" { executable = "zi2zx"; zx_build = true }
|
|
else if target.count == 0 && !project { executable = "zi2zx"; native_build = true }
|
|
else { return PkgFail("build target must be c, go, wasm, or zx") }
|
|
}
|
|
if executable.count == 0 { return PkgFail("unknown Ziran command") }
|
|
tool_directory: string = PkgLauncherDirectory(launcher)
|
|
if project { tool_directory = PkgToolDirectory(launcher, locked.tool_path, executable) }
|
|
if tool_directory.count == 0 { return PkgFail("cannot find Ziran binaries") }
|
|
binary: string = PkgJoin3(tool_directory, "/", executable)
|
|
names: [8]string
|
|
values: [8]string
|
|
names[0] = "ZIRAN_PACKAGE_MAP"
|
|
values[0] = ""
|
|
// The tool names the driver that chose it when it rejects a file another
|
|
// toolchain wrote.
|
|
names[1] = "ZIRAN_DRIVER"
|
|
values[1] = PkgCanonical(launcher)
|
|
environment: s32 = 2
|
|
while environment - 2 < cast(s32)extra_names.count && environment < 8 {
|
|
names[environment] = extra_names[environment - 2]
|
|
values[environment] = extra_values[environment - 2]
|
|
environment += 1
|
|
}
|
|
if project {
|
|
if !PathExists(binary) {
|
|
make_args: [5]string
|
|
make_args[0] = "make"
|
|
make_args[1] = "-C"
|
|
make_args[2] = locked.tool_path
|
|
make_args[3] = "all"
|
|
// The pinned launcher is build/bin/ziran whatever BUILD_DIR the
|
|
// calling make passed down, such as the sanitizer build's.
|
|
make_args[4] = "BUILD_DIR=build"
|
|
empty: [0]string
|
|
if PkgRunToStderr(make_args[:], "") != 0 {
|
|
return PkgFail("cannot build pinned Ziran compiler")
|
|
}
|
|
}
|
|
values[0] = PkgWriteMap(root, locked, current)
|
|
if values[0].count == 0 {
|
|
if pkg_failure_reported { return 2 }
|
|
return PkgFail("cannot build project import map")
|
|
}
|
|
}
|
|
// Every project build, including an explicit source/--exe build, uses
|
|
// the root manifest's native libraries alongside the caller's LDLIBS.
|
|
if project && build && current.link_count > 0 {
|
|
if environment >= 8 { return PkgFail("too many build environment values") }
|
|
libraries: string = PkgGetenv("LDLIBS")
|
|
library: s32 = 0
|
|
while library < current.link_count {
|
|
if libraries.count > 0 { libraries = PkgJoin(libraries, " ") }
|
|
libraries = PkgJoin3(libraries, "-l", current.links[library])
|
|
library += 1
|
|
}
|
|
names[environment] = "LDLIBS"
|
|
values[environment] = libraries
|
|
environment += 1
|
|
}
|
|
extra: s64 = 0
|
|
if current != null { extra = current.export_count }
|
|
child := PkgStrings(arguments.count + extra + 8)
|
|
if child.count != arguments.count + extra + 8 { return PkgFail("cannot allocate arguments") }
|
|
count: s32 = 0
|
|
child[count] = binary
|
|
count += 1
|
|
if check { child[count] = "--check-only"; count += 1 }
|
|
// `ziran build -o DIR` owns DIR: generated files from an earlier build
|
|
// that this one does not write are removed so they cannot be linked.
|
|
// C and Go own output directories. Wasm is deliberately two format
|
|
// conversions below: source/ZIR -> verified ZX -> WebAssembly.
|
|
if build && !wasm_build && !native_build && !zx_build {
|
|
child[count] = "--prune-stale"
|
|
count += 1
|
|
}
|
|
if wasm_build {
|
|
child[count] = "--target"
|
|
count += 1
|
|
child[count] = "wasm"
|
|
count += 1
|
|
}
|
|
// `zi2zx run` is a real subcommand. `ziran bundle` is the high-level
|
|
// spelling for zi2zx's default source/ZIR -> ZX conversion, so it does
|
|
// not forward a removed `bundle` subcommand.
|
|
if command == "run" || transport { child[count] = command; count += 1 }
|
|
// `ziran run --project FILE.zx` runs a bundle with the pinned toolchain
|
|
// that built it; a bundle takes no source root.
|
|
if project && command != "run" && !transport && !PkgHasArgument(arguments, "--root") {
|
|
child[count] = "--root"
|
|
count += 1
|
|
child[count] = PkgJoin3(root, "/", current.roots[0])
|
|
count += 1
|
|
}
|
|
has_source: bool = false
|
|
index: s32 = 0
|
|
while index < cast(s32)arguments.count {
|
|
part: string = arguments[index]
|
|
if !(build && (part == "--target=c" || part == "--target=go" ||
|
|
part == "--target=wasm" || part == "--target=zx")) {
|
|
child[count] = part
|
|
count += 1
|
|
if PkgEndsWith(part, ".zi") || PkgEndsWith(part, ".zir") {
|
|
has_source = true
|
|
}
|
|
}
|
|
index += 1
|
|
}
|
|
if project && !has_source &&
|
|
(check || command == "ir" || command == "api" || build || command == "bundle") {
|
|
entry: string = PkgJoin3(root, "/", current.entry)
|
|
// A library has no app entry; checking it checks what it exports.
|
|
if !PathExists(entry) && current.export_count > 0 &&
|
|
(check || command == "ir" || command == "api") {
|
|
index = 0
|
|
while index < current.export_count {
|
|
child[count] = PkgJoin3(root, "/", current.exports[index].value)
|
|
seen: bool = false
|
|
earlier: s32 = 0
|
|
while earlier < index {
|
|
if current.exports[earlier].value == current.exports[index].value {
|
|
seen = true
|
|
}
|
|
earlier += 1
|
|
}
|
|
if !seen { count += 1 }
|
|
index += 1
|
|
}
|
|
} else {
|
|
child[count] = entry
|
|
count += 1
|
|
}
|
|
}
|
|
if command == "run" && !has_source {
|
|
return PkgRunBundle(child[0:count], arguments,
|
|
names[0:environment], values[0:environment])
|
|
}
|
|
if wasm_build || native_build {
|
|
output: string = ""
|
|
output_index: s32 = -1
|
|
index = 0
|
|
while index + 1 < count {
|
|
if child[index] == "-o" { output = child[index + 1]; output_index = index + 1 }
|
|
index += 1
|
|
}
|
|
if native_build {
|
|
source: string = ""
|
|
for part: arguments {
|
|
if PkgEndsWith(part, ".zi") || PkgEndsWith(part, ".zir") { source = part }
|
|
}
|
|
if source.count == 0 { return PkgFail("native build needs a source file") }
|
|
add_output := output.count == 0
|
|
add_root := !PkgHasArgument(arguments, "--root")
|
|
add_entry := !PkgHasArgument(arguments, "--entry")
|
|
if add_output { output = PkgModuleName(source) }
|
|
extra_options: s32 = 0
|
|
if add_root { extra_options += 2 }
|
|
if add_entry { extra_options += 2 }
|
|
if add_output { extra_options += 2 }
|
|
// zi2zx options precede its first input. Insert inferred native
|
|
// options there so `ziran build FILE.zi` remains a complete form.
|
|
insertion: s32 = count
|
|
index = 1
|
|
while index < count {
|
|
if PkgEndsWith(child[index], ".zi") || PkgEndsWith(child[index], ".zir") {
|
|
insertion = index
|
|
break
|
|
}
|
|
index += 1
|
|
}
|
|
cursor := count
|
|
while cursor > insertion {
|
|
child[cursor + extra_options - 1] = child[cursor - 1]
|
|
cursor -= 1
|
|
}
|
|
if output_index >= insertion { output_index += extra_options }
|
|
position := insertion
|
|
if add_root { child[position] = "--root"; child[position + 1] = ParentDirectory(source); position += 2 }
|
|
if add_entry { child[position] = "--entry"; child[position + 1] = PkgJoin(PkgModuleName(source), ":main"); position += 2 }
|
|
if add_output { child[position] = "-o"; child[position + 1] = output; output_index = position + 1 }
|
|
count += extra_options
|
|
}
|
|
if output.count == 0 || output == "-" {
|
|
if native_build { return PkgFail("native build needs -o FILE") }
|
|
return PkgFail("WebAssembly build needs -o FILE")
|
|
}
|
|
verified := PkgTemporarySibling(output, ".zx")
|
|
if verified.count == 0 { return PkgFail("cannot name temporary verified ZX output") }
|
|
child[output_index] = verified
|
|
code := PkgRun(child[0:count], "", names[0:environment], values[0:environment])
|
|
if code == 0 && wasm_build {
|
|
wasm_tool := PkgJoin3(tool_directory, "/", "zx2wasm")
|
|
wasm: [4]string = .[wasm_tool, "-o", output, verified]
|
|
code = PkgRun(wasm[:], "", names[0:environment], values[0:environment])
|
|
}
|
|
if code == 0 && native_build {
|
|
native_tool := PkgJoin3(tool_directory, "/", "zx2native")
|
|
native: [10]string = .[native_tool, "--target", PkgHostNativeTarget,
|
|
"--platform", "linux", "--emit", "elf", "-o", output, verified]
|
|
code = PkgRun(native[:], "", names[0:environment], values[0:environment])
|
|
if code == 0 {
|
|
LearnRecord(tool_directory, verified, output, PkgHostNativeTarget,
|
|
names[0:environment], values[0:environment])
|
|
}
|
|
}
|
|
unused RemoveFile(verified)
|
|
return code
|
|
}
|
|
return PkgRun(child[0:count], "", names[0:environment], values[0:environment])
|
|
}
|
|
|
|
// True when an argument before `--` names a source, saved IR, or bundle
|
|
// file: the command then works on that file, not on the project's program.
|
|
PkgHasSource :: (arguments: []string) -> bool {
|
|
index: s32 = 0
|
|
while index < cast(s32)arguments.count {
|
|
part: string = arguments[index]
|
|
if part == "--" { return false }
|
|
if PkgEndsWith(part, ".zi") || PkgEndsWith(part, ".zir") ||
|
|
PkgEndsWith(part, ".zx") { return true }
|
|
index += 1
|
|
}
|
|
return false
|
|
}
|
|
|
|
PkgHasBundle :: (arguments: []string) -> bool {
|
|
index: s32 = 0
|
|
while index < cast(s32)arguments.count {
|
|
part: string = arguments[index]
|
|
if part == "--" { return false }
|
|
if PkgEndsWith(part, ".zx") { return true }
|
|
index += 1
|
|
}
|
|
return false
|
|
}
|
|
|
|
PkgHasTarget :: (arguments: []string) -> bool {
|
|
index: s32 = 0
|
|
while index < cast(s32)arguments.count {
|
|
part: string = arguments[index]
|
|
if part == "--" { return false }
|
|
if part.count >= 8 && part[0:8] == "--target" { return true }
|
|
index += 1
|
|
}
|
|
return false
|
|
}
|
|
|
|
// The module a source path defines: src/main.zi is main.
|
|
PkgModuleName :: (path: string) -> string {
|
|
start: s64 = cast(s64)path.count
|
|
while start > 0 && path[start - 1] != cast(u8)47 { start -= 1 }
|
|
name: string = path[start:]
|
|
if PkgEndsWith(name, ".zi") { name = name[0:cast(s64)name.count - 3] }
|
|
return name
|
|
}
|
|
|
|
// `ziran build` in a project without a tool: the entry module's main
|
|
// becomes build/NAME through C. [package] links adds -l libraries.
|
|
PkgNativeBuild :: (root: string, locked: *PkgLock, current: *PkgManifest,
|
|
launcher: string) -> string {
|
|
module: string = PkgModuleName(current.entry)
|
|
if !PathExists(PkgJoin3(root, "/", current.entry)) {
|
|
PkgFail(PkgJoin3("the package entry ", current.entry, " does not exist"))
|
|
return ""
|
|
}
|
|
generated: string = PkgJoin(root, "/build/generated/c")
|
|
if !PkgMkdirs(generated) {
|
|
PkgFail("cannot create build/generated/c")
|
|
return ""
|
|
}
|
|
arguments: [6]string
|
|
arguments[0] = "--target=c"
|
|
arguments[1] = "--exe"
|
|
arguments[2] = "--entry"
|
|
arguments[3] = PkgJoin(module, ":main")
|
|
arguments[4] = "-o"
|
|
arguments[5] = generated
|
|
empty: [0]string
|
|
if PkgDispatch("build", arguments[:], true, root, locked, current,
|
|
launcher, empty[:], empty[:]) != 0 { return "" }
|
|
program: string = PkgJoin3(root, "/build/", current.name)
|
|
// A rename replaces the program in one step, so a running copy keeps
|
|
// its own file.
|
|
if !RenamePath(PkgJoin3(generated, "/", module), program) {
|
|
PkgFail("cannot move the built program to build/")
|
|
return ""
|
|
}
|
|
return program
|
|
}
|
|
|
|
// `ziran install` for a project without a tool copies its program to
|
|
// PREFIX/bin/BIN. The copy replaces the old file in one step.
|
|
PkgNativeInstall :: (root: string, locked: *PkgLock, current: *PkgManifest,
|
|
launcher: string, prefix: string, bin: string) -> s32 {
|
|
program: string = PkgNativeBuild(root, locked, current, launcher)
|
|
if program.count == 0 { return 2 }
|
|
directory: string = PkgJoin(prefix, "/bin")
|
|
if !PkgMkdirs(directory) { return PkgFail(PkgJoin("cannot create ", directory)) }
|
|
target: string = PkgJoin3(directory, "/", bin)
|
|
staged: string = PkgJoin(target, ".ziran-new")
|
|
copy: [5]string
|
|
copy[0] = "cp"
|
|
copy[1] = "-f"
|
|
copy[2] = "--"
|
|
copy[3] = program
|
|
copy[4] = staged
|
|
empty: [0]string
|
|
if PkgRun(copy[:], "", empty[:], empty[:]) != 0 || !RenamePath(staged, target) {
|
|
return PkgFail(PkgJoin("cannot install ", target))
|
|
}
|
|
PkgPrint(PkgJoin3("installed ", target, "\n"), false)
|
|
return 0
|
|
}
|
|
|
|
// Arguments after the first `--` go to the program as they are; the others
|
|
// are the command's own.
|
|
PkgAfterSeparator :: (arguments: []string) -> s32 {
|
|
index: s32 = 0
|
|
while index < cast(s32)arguments.count {
|
|
if arguments[index] == "--" { return index + 1 }
|
|
index += 1
|
|
}
|
|
return cast(s32)arguments.count
|
|
}
|
|
|
|
// Runs a dependency's project tool from the project root with the resolved
|
|
// [tool.ALIAS] options. The tool is rebuilt when its sources changed, so a
|
|
// local override never runs a stale binary.
|
|
PkgRunTool :: (root: string, locked: *PkgLock, current: *PkgManifest,
|
|
selector: string, arguments: []string, extra_names: []string,
|
|
extra_values: []string) -> s32 {
|
|
selected: s32 = PkgSelect(locked, selector)
|
|
if selected < 0 { return PkgFail("package is not locked") }
|
|
alias: string = locked.packages[selected].name
|
|
index: s32 = 0
|
|
while index < current.dependency_count {
|
|
if PkgFoldEqual(current.dependencies[index].alias, selector) {
|
|
alias = current.dependencies[index].alias
|
|
}
|
|
index += 1
|
|
}
|
|
return PkgRunSelectedTool(root, locked, current, selected, alias,
|
|
arguments, extra_names, extra_values)
|
|
}
|
|
|
|
PkgRunSelectedTool :: (root: string, locked: *PkgLock, current: *PkgManifest,
|
|
selected: s32, alias: string, arguments: []string, extra_names: []string,
|
|
extra_values: []string) -> s32 {
|
|
options := PkgResolveOptions(root, alias, locked.packages[selected].path)
|
|
if options.count == 0 { return 2 }
|
|
return PkgRunSelectedToolOptions(root, locked, current, selected, alias,
|
|
arguments, extra_names, extra_values, options)
|
|
}
|
|
|
|
PkgRunSelectedToolOptions :: (root: string, locked: *PkgLock, current: *PkgManifest,
|
|
selected: s32, alias: string, arguments: []string, extra_names: []string,
|
|
extra_values: []string, options: string) -> s32 {
|
|
item: *PkgLocked = *locked.packages[selected]
|
|
relative: string = PkgJoin("", item.manifest.project_tool)
|
|
if relative.count == 0 || relative[0] == cast(u8)47 {
|
|
return PkgFail("package has no project tool")
|
|
}
|
|
at: s64 = 0
|
|
while at + 1 < cast(s64)relative.count {
|
|
if relative[at] == cast(u8)46 &&
|
|
relative[at + 1] == cast(u8)46 { return PkgFail("invalid project tool") }
|
|
at += 1
|
|
}
|
|
index: s32 = 0
|
|
make_args: [8]string
|
|
make_args[0] = "make"
|
|
make_args[1] = "--silent"
|
|
make_args[2] = "--no-print-directory"
|
|
make_args[3] = "-C"
|
|
make_args[4] = item.path
|
|
make_args[5] = relative
|
|
make_args[6] = PkgJoin("ZIRAN_DIR=", locked.tool_path)
|
|
make_args[7] = "-j1"
|
|
empty: [0]string
|
|
if PkgRunToStderr(make_args[:], "") != 0 {
|
|
return PkgFail("cannot build package tool")
|
|
}
|
|
names: [16]string
|
|
values: [16]string
|
|
names[0] = "ZIRAN_PROJECT_ROOT"
|
|
names[1] = "ZIRAN_PACKAGE_ROOT"
|
|
names[2] = "ZIRAN_PACKAGE_ID"
|
|
names[3] = "ZIRAN_TOOLCHAIN_ROOT"
|
|
names[4] = "ZIRAN_TOOL_OPTIONS"
|
|
names[5] = "ZIRAN_PROJECT_NAME"
|
|
names[6] = "ZIRAN_PROJECT_ENTRY"
|
|
names[7] = "ZIRAN_PROJECT_LINKS"
|
|
values[0] = root
|
|
values[1] = item.path
|
|
values[2] = item.id
|
|
values[3] = locked.tool_path
|
|
values[4] = options
|
|
values[5] = current.name
|
|
values[6] = current.entry
|
|
libraries: string = ""
|
|
index = 0
|
|
while index < current.link_count {
|
|
if index > 0 { libraries = PkgJoin(libraries, " ") }
|
|
libraries = PkgJoin(libraries, current.links[index])
|
|
index += 1
|
|
}
|
|
values[7] = libraries
|
|
count: s32 = 8
|
|
index = 0
|
|
while index < cast(s32)extra_names.count && count < 16 {
|
|
names[count] = extra_names[index]
|
|
values[count] = extra_values[index]
|
|
count += 1
|
|
index += 1
|
|
}
|
|
child := PkgStrings(arguments.count + 8)
|
|
if child.count != arguments.count + 8 { return PkgFail("cannot allocate arguments") }
|
|
child[0] = PkgJoin3(item.path, "/", relative)
|
|
index = 0
|
|
while index < cast(s32)arguments.count {
|
|
child[index + 1] = arguments[index]
|
|
index += 1
|
|
}
|
|
if PkgHasArgument(extra_names, "ZIRAN_ARTIFACT_OUTPUT") {
|
|
result := PkgCaptureEnv(child[0:index + 1], root, names[0:count], values[0:count])
|
|
if result.text.count > 0 { PkgPrint(PkgJoin(result.text, "\n"), true) }
|
|
return result.code
|
|
}
|
|
return PkgRun(child[0:index + 1], root, names[0:count], values[0:count])
|
|
}
|
|
|
|
// Checks each project --module-path FOLDER and records it, canonical, as a
|
|
// generated module root of CURRENT. Packages come from ziran.lock, so the
|
|
// NAME=DIR spelling is refused, and a folder inside a locked package or the
|
|
// toolchain cannot become the project's.
|
|
// The length of BASE when PATH is BASE or inside it, else -1.
|
|
PkgPathWithin :: (path: string, base: string) -> s64 {
|
|
if base.count == 0 { return -1 }
|
|
if path == base || (path.count > base.count && path[0:base.count] == base &&
|
|
path[base.count] == cast(u8)47) { return cast(s64)base.count }
|
|
return -1
|
|
}
|
|
|
|
PkgGeneratedRoots :: (root: string, locked: *PkgLock, current: *PkgManifest, folders: []string) -> bool {
|
|
current.generated_count = 0
|
|
current.generated = PkgStrings(folders.count)
|
|
if current.generated.count != folders.count { return false }
|
|
for folder: folders {
|
|
at: s64 = 0
|
|
while at < cast(s64)folder.count && folder[at] != cast(u8)61 &&
|
|
folder[at] != cast(u8)47 { at += 1 }
|
|
if at > 0 && at < cast(s64)folder.count && folder[at] == cast(u8)61 {
|
|
unused PkgFail(PkgJoin3("in a project, packages come from ziran.lock; --module-path takes a folder of the project's own modules, not ",
|
|
folder, ""))
|
|
return false
|
|
}
|
|
path: string = PkgCanonical(folder)
|
|
if path.count == 0 || !DirectoryExists(path) {
|
|
unused PkgFail(PkgJoin("--module-path folder does not exist: ", folder))
|
|
return false
|
|
}
|
|
// The compiler gives a file to the package with the longest path
|
|
// holding it; the folder must belong to the project, not a package.
|
|
owner: string = ""
|
|
best: s64 = PkgPathWithin(path, PkgCanonical(root))
|
|
index: s32 = -1
|
|
while index < locked.count {
|
|
base: string = locked.tool_path
|
|
name: string = "the Ziran toolchain"
|
|
if index >= 0 {
|
|
base = locked.packages[index].path
|
|
name = PkgJoin("locked package ", locked.packages[index].name)
|
|
}
|
|
index += 1
|
|
length: s64 = PkgPathWithin(path, PkgCanonical(base))
|
|
if length > best { best = length; owner = name }
|
|
}
|
|
if owner.count > 0 {
|
|
unused PkgFail(PkgJoin3(PkgJoin3("--module-path folder ", folder, " is inside "),
|
|
owner, "; a project adds only its own modules"))
|
|
return false
|
|
}
|
|
current.generated[current.generated_count] = path
|
|
current.generated_count += 1
|
|
}
|
|
return true
|
|
}
|
|
|
|
// One usage line per command. `ziran COMMAND --help` prints it and does
|
|
// nothing else; "" means the command is unknown.
|
|
PkgUsage :: (command: string) -> string {
|
|
if command == "version" { return "usage: ziran version\n" }
|
|
if command == "guide" { return "usage: ziran guide\n" }
|
|
if command == "features" { return "usage: ziran features [--json] [--id FEATURE_ID]\n" }
|
|
if command == "capabilities" { return "usage: ziran capabilities [--target=c|go|zx] [--json]\n" }
|
|
if command == "explain" { return "usage: ziran explain CODE [--json] | ziran explain --list [--json]\n" }
|
|
if command == "learn" { return LearnUsage() }
|
|
if command == "api" {
|
|
return "usage: ziran api [--json] [--diagnostics=text|json] [--module NAME] [--symbol NAME|ID] [--imports-only] [--max-symbols N] [--max-bytes N] [--module-path DIR] [--define NAME] --root DIR file.zi|file.zir ...\n"
|
|
}
|
|
if command == "new" {
|
|
return "usage: ziran new DIR [--template SOURCE[:NAME]] [--ref REF] [--name NAME] [--overrides FILE]\n"
|
|
}
|
|
if command == "init" {
|
|
return "usage: ziran init [--template SOURCE[:NAME]] [--ref REF] [--name NAME] [--overrides FILE]\n"
|
|
}
|
|
if command == "lock" { return "usage: ziran lock\n" }
|
|
if command == "update" { return "usage: ziran update [PACKAGE]\n" }
|
|
if command == "fetch" { return "usage: ziran fetch [--locked] [--offline]\n" }
|
|
if command == "add" {
|
|
return "usage: ziran add URL|OWNER/REPO [--name NAME] [--ref REF] [--source]\n"
|
|
}
|
|
if command == "pkg" {
|
|
return "usage: ziran pkg path PACKAGE [--submodules] | ziran pkg paths PACKAGE... [--submodules] | ziran pkg list [PACKAGE] | ziran pkg artifact PACKAGE:ARTIFACT --platform OS-ARCH-ABI [--option Alias:key=value]...\n"
|
|
}
|
|
if command == "tool" { return "usage: ziran tool PACKAGE COMMAND [ARGS]\n" }
|
|
if command == "install" { return "usage: ziran install [--prefix DIR] [--locked] [--offline]\n" }
|
|
if command == "check" {
|
|
return "usage: ziran check [--project] [--lint] [--root DIR] [--entry module:function] [--module-path DIR] [file.zi ...]\n"
|
|
}
|
|
if command == "ir" {
|
|
return "usage: ziran ir [--root DIR] [--entry module:function] [--module-path DIR] [-o FILE.zir] [file.zi ...]\n"
|
|
}
|
|
if command == "build" {
|
|
return "usage: ziran build [--target=c|go|wasm|zx] [--exe --entry module:function] [--root DIR] [--module-path DIR] [-o FILE|DIR] [file.zi ...]; in a project: ziran build\n"
|
|
}
|
|
if command == "bundle" {
|
|
return "usage: ziran bundle --entry module:function [--root DIR] [--module-path DIR] [--bind CALLER:CAPABILITY=PROVIDER:FUNCTION] [-o FILE] [file.zi ...]\n"
|
|
}
|
|
if command == "run" {
|
|
return "usage: ziran run FILE.zx | ziran run [--target=c [--no-cache]] [--root DIR] [--entry module:function] [FILE.zi [-- ARGS]]; in a project: ziran run [-- ARGS]\n"
|
|
}
|
|
if command == "fmt" { return "usage: ziran fmt [--check] FILE.zi ...\n" }
|
|
if command == "inspect" { return "usage: ziran inspect [--hex] FILE.zir|FILE.zx\n" }
|
|
if command == "compile-commands" {
|
|
return "usage: ziran compile-commands [--compiler CC] [--include DIR] [-o FILE] GENERATED_DIR\n"
|
|
}
|
|
if command == "edit" {
|
|
return "usage: ziran edit --root DIR [--module-path DIR] [--apply] request.json\n"
|
|
}
|
|
if command == "compact" || command == "expand" {
|
|
return "usage: ziran compact|expand [--map FILE.json] INPUT\n"
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// Where a command's own flags end. Arguments after `--` belong to the
|
|
// program or script it runs, `run` hands everything after its source file to
|
|
// that script, and `tool` hands everything after PACKAGE to the tool.
|
|
PkgFlagsEnd :: (command: string, arguments: []string) -> s32 {
|
|
end: s32 = PkgAfterSeparator(arguments)
|
|
if command == "run" {
|
|
script: s32 = ScriptArgumentEnd(arguments)
|
|
if script < end { end = script }
|
|
}
|
|
if command == "tool" && end > 2 { end = 2 }
|
|
return end
|
|
}
|
|
|
|
#program_export
|
|
PackageMain :: (launcher: string, arguments: []string) -> s32 {
|
|
// Source execution also supports #!/usr/bin/env ziran. Normalize before
|
|
// parsing so every word after the filename belongs to the script.
|
|
if arguments.count > 0 && PkgEndsWith(arguments[0], ".zi") {
|
|
script: Vec(string)
|
|
defer VecFree(script)
|
|
if !VecPush(script, "run") || !VecPush(script, "--target=c") { return 2 }
|
|
for argument: arguments {
|
|
if !VecPush(script, argument) { return 2 }
|
|
}
|
|
return PackageMain(launcher, VecSlice(script, 0, script.count))
|
|
}
|
|
PkgSetDiagnosticFormat(arguments)
|
|
if arguments.count == 0 || arguments[0] == "help" ||
|
|
arguments[0] == "--help" || arguments[0] == "-h" {
|
|
PkgPrint("usage: ziran version|guide|features|capabilities|explain|learn|api|new|init|lock|update|fetch|add|pkg|tool|install|check|ir|build|bundle|run|fmt|inspect|compile-commands ...\n", false)
|
|
PkgPrint("project commands: --project [--locked] [--offline]\n", false)
|
|
PkgPrint("in a project, run, build, check, and install without a file work on its program\n", false)
|
|
PkgPrint("scripts: ziran FILE.zi [ARGS] | ziran run --target=c [--no-cache] FILE.zi -- ARGS\n", false)
|
|
PkgPrint("new to Ziran? run ziran guide\n", false)
|
|
return 0
|
|
}
|
|
command: string = arguments[0]
|
|
// Asking for help never runs the command, whatever it would change.
|
|
own_flags: []string = arguments[1:PkgFlagsEnd(command, arguments)]
|
|
if PkgHasArgument(own_flags, "--help") || PkgHasArgument(own_flags, "-h") {
|
|
usage: string = PkgUsage(command)
|
|
if usage.count > 0 {
|
|
PkgPrint(usage, false)
|
|
return 0
|
|
}
|
|
}
|
|
if command == "new" || command == "init" {
|
|
return Template(launcher, command, arguments[1:])
|
|
}
|
|
if command == "guide" { return Guide(arguments[1:]) }
|
|
if command == "features" { return Features(arguments[1:]) }
|
|
if command == "capabilities" { return Capabilities(arguments[1:]) }
|
|
if command == "explain" { return Explain(arguments[1:]) }
|
|
if command == "learn" { return Learn(arguments[1:]) }
|
|
if command == "add" { return Add(launcher, arguments[1:]) }
|
|
separator: s32 = PkgAfterSeparator(arguments)
|
|
script_flags: []string = arguments[0:ScriptArgumentEnd(arguments)]
|
|
if command == "run" && PkgHasArgument(script_flags, "--target=c") {
|
|
separator = ScriptArgumentEnd(arguments)
|
|
}
|
|
flags: []string = arguments[0:separator]
|
|
root: string = ""
|
|
// `ziran run`, `build`, and `check` without a file, inside a project,
|
|
// work on the project's own program; with a file they resolve its
|
|
// imports through the project. An explicit --root or --module-path
|
|
// asks for a standalone command.
|
|
lifecycle: bool = (command == "run" || command == "build" ||
|
|
command == "check" || command == "install") &&
|
|
!PkgHasSource(flags[1:])
|
|
implied: bool = false
|
|
if (command == "run" || command == "build" || command == "check") &&
|
|
!PkgHasArgument(flags, "--project") &&
|
|
!PkgHasArgument(flags, "--root") &&
|
|
!PkgHasArgument(flags, "--module-path") &&
|
|
!PkgHasBundle(flags[1:]) {
|
|
root = PkgProjectRoot()
|
|
implied = root.count > 0
|
|
}
|
|
if command == "lock" || command == "update" || command == "fetch" ||
|
|
command == "tool" || command == "install" || command == "pkg" ||
|
|
PkgHasArgument(flags, "--project") {
|
|
root = PkgProjectRoot()
|
|
if root.count == 0 { return PkgFail("no ziran.toml found") }
|
|
}
|
|
if command == "lock" {
|
|
if PkgResolveLock(root, "") { return 0 }
|
|
return PkgFail("cannot resolve project lock")
|
|
}
|
|
if command == "update" {
|
|
if arguments.count > 2 { return PkgFail("usage: ziran update [PACKAGE]") }
|
|
refresh: string = "*"
|
|
if arguments.count == 2 { refresh = arguments[1] }
|
|
if PkgResolveLock(root, refresh) { return 0 }
|
|
return PkgFail("cannot update project lock")
|
|
}
|
|
project: bool = PkgHasArgument(flags, "--project") || implied
|
|
locked_mode: bool = PkgHasArgument(flags, "--locked")
|
|
offline: bool = PkgHasArgument(flags, "--offline")
|
|
filtered := PkgStrings(arguments.count)
|
|
if filtered.count != arguments.count { return PkgFail("cannot allocate arguments") }
|
|
count: s32 = 0
|
|
// In a project, --module-path DIR adds a folder of the root package's
|
|
// own modules, such as one a build step generates.
|
|
generated := PkgStrings(arguments.count)
|
|
if generated.count != arguments.count { return PkgFail("cannot allocate arguments") }
|
|
generated_count: s32 = 0
|
|
index: s32 = 1
|
|
while index < cast(s32)arguments.count {
|
|
part: string = arguments[index]
|
|
if project && index < separator && part == "--module-path" {
|
|
if index + 1 >= separator { return PkgFail("--module-path needs a folder") }
|
|
generated[generated_count] = arguments[index + 1]
|
|
generated_count += 1
|
|
index += 2
|
|
continue
|
|
}
|
|
if index >= separator ||
|
|
(part != "--project" && part != "--locked" && part != "--offline") {
|
|
filtered[count] = part
|
|
count += 1
|
|
}
|
|
index += 1
|
|
}
|
|
locked: *PkgLock = null
|
|
current: *PkgManifest = null
|
|
if project || command == "fetch" || command == "pkg" || command == "tool" ||
|
|
command == "install" {
|
|
locked = PkgNewLock()
|
|
current = PkgNewManifest()
|
|
if locked == null || current == null ||
|
|
!PkgReadLock(PkgJoin(root, "/ziran.lock"), locked) {
|
|
return PkgFail("cannot read ziran.lock; run ziran lock")
|
|
}
|
|
if !PkgCheckoutGraph(root, locked, offline, locked_mode, current) {
|
|
if pkg_failure_reported { return 2 }
|
|
return PkgFail("cannot fetch locked packages")
|
|
}
|
|
// A project runs with its pinned toolchain's own launcher, so an
|
|
// installed ziran of any age writes the import map the lock expects.
|
|
// A launcher built inside the pinned toolchain's own tree, such as
|
|
// the sanitizer build under build/sanitize, is that toolchain.
|
|
pinned: string = PkgJoin(locked.tool_path, "/build/bin/ziran")
|
|
tool_root: string = PkgJoin(PkgCanonical(locked.tool_path), "/")
|
|
own: string = PkgCanonical(launcher)
|
|
inside: bool = tool_root.count > 1 && own.count > tool_root.count &&
|
|
own[0:tool_root.count] == tool_root
|
|
if PkgGetenv("ZIRAN_PINNED_LAUNCHER") != "1" && !inside &&
|
|
own != PkgCanonical(pinned) {
|
|
if !PathExists(pinned) {
|
|
make_args: [5]string
|
|
make_args[0] = "make"
|
|
make_args[1] = "-C"
|
|
make_args[2] = locked.tool_path
|
|
make_args[3] = "all"
|
|
// The pinned launcher is build/bin/ziran whatever BUILD_DIR the
|
|
// calling make passed down, such as the sanitizer build's.
|
|
make_args[4] = "BUILD_DIR=build"
|
|
none: [0]string
|
|
if PkgRunToStderr(make_args[:], "") != 0 {
|
|
return PkgFail("cannot build pinned Ziran compiler")
|
|
}
|
|
}
|
|
forwarded := PkgStrings(arguments.count + 1)
|
|
if forwarded.count != arguments.count + 1 { return PkgFail("cannot allocate arguments") }
|
|
forwarded[0] = pinned
|
|
index = 0
|
|
while index < cast(s32)arguments.count {
|
|
forwarded[index + 1] = arguments[index]
|
|
index += 1
|
|
}
|
|
names: [1]string
|
|
values: [1]string
|
|
// "forwarded" marks the hand-off for tools without stopping a
|
|
// ziran that a tool runs from reaching the pinned launcher too;
|
|
// only "1" turns forwarding off.
|
|
names[0] = "ZIRAN_PINNED_LAUNCHER"
|
|
values[0] = "forwarded"
|
|
return PkgRun(forwarded[0:index + 1], "", names[:], values[:])
|
|
}
|
|
}
|
|
if command == "fetch" { return 0 }
|
|
if command == "compile-commands" {
|
|
return PkgCompileCommands(filtered[0:count], project, locked, launcher)
|
|
}
|
|
if command == "pkg" && count >= 1 && filtered[0] == "artifact" {
|
|
if count < 4 || filtered[2] != "--platform" || (count - 4) % 2 != 0 {
|
|
return PkgFail("usage: ziran pkg artifact PACKAGE:ARTIFACT --platform OS-ARCH-ABI [--option Alias:key=value]...")
|
|
}
|
|
overrides: [256]string
|
|
option_count: s32 = 0
|
|
for at: 4..count - 1 {
|
|
if (at - 4) % 2 == 0 {
|
|
if filtered[at] != "--option" { return PkgFail("expected --option Alias:key=value") }
|
|
} else {
|
|
overrides[option_count] = filtered[at]
|
|
option_count += 1
|
|
}
|
|
}
|
|
selection := filtered[1]
|
|
colon: s64 = 0
|
|
while colon < cast(s64)selection.count && selection[colon] != cast(u8)58 { colon += 1 }
|
|
if colon == 0 || colon >= cast(s64)selection.count - 1 {
|
|
return PkgFail("artifact selection needs PACKAGE:ARTIFACT")
|
|
}
|
|
return PkgBuildArtifactResolved(root, selection[0:colon], selection[colon + 1:],
|
|
filtered[3], locked, current, launcher, overrides[0:option_count])
|
|
}
|
|
if command == "pkg" && count >= 1 && filtered[0] == "list" {
|
|
if count > 2 { return PkgFail("usage: ziran pkg list [PACKAGE]") }
|
|
selector: string = ""
|
|
if count == 2 { selector = filtered[1] }
|
|
return PkgList(locked, current, selector)
|
|
}
|
|
if command == "pkg" && count >= 1 && filtered[0] == "paths" {
|
|
end := count
|
|
submodules := count > 1 && filtered[count - 1] == "--submodules"
|
|
if submodules { end -= 1 }
|
|
return PkgPaths(locked, filtered[1:end], submodules, offline)
|
|
}
|
|
if command == "pkg" {
|
|
if count < 2 || count > 3 || filtered[0] != "path" ||
|
|
(count == 3 && filtered[2] != "--submodules") {
|
|
return PkgFail("usage: ziran pkg path PACKAGE [--submodules] | ziran pkg paths PACKAGE... [--submodules] | ziran pkg list [PACKAGE]")
|
|
}
|
|
if filtered[1] == "ziran" {
|
|
if count == 3 { return PkgFail("Ziran has no package submodules") }
|
|
PkgPrint(locked.tool_path, false)
|
|
PkgPrint("\n", false)
|
|
return 0
|
|
}
|
|
selected: s32 = PkgSelect(locked, filtered[1])
|
|
if selected < 0 { return PkgFail("package is not locked") }
|
|
if count == 3 && !PkgPrepareSubmodules(locked.packages[selected].path,
|
|
offline) { return PkgFail("package submodules are missing") }
|
|
PkgPrint(locked.packages[selected].path, false)
|
|
PkgPrint("\n", false)
|
|
return 0
|
|
}
|
|
if command == "tool" {
|
|
if count < 2 { return PkgFail("usage: ziran tool PACKAGE COMMAND") }
|
|
empty: [0]string
|
|
return PkgRunTool(root, locked, current, filtered[0], filtered[1:count],
|
|
empty[:], empty[:])
|
|
}
|
|
if current != null && current.problem.count > 0 {
|
|
return PkgFail(current.problem)
|
|
}
|
|
if generated_count > 0 &&
|
|
!PkgGeneratedRoots(root, locked, current, generated[0:generated_count]) { return 2 }
|
|
// A project whose [package] tool is set, such as a Kryon application,
|
|
// hands its program's run, build, check, and install to that tool.
|
|
whole: bool = lifecycle && current != null && !PkgHasTarget(flags[1:])
|
|
if whole && current.tool.count > 0 && command != "install" {
|
|
passed := PkgStrings(cast(s64)count + 2 * generated_count + 1)
|
|
if passed.count != cast(s64)count + 2 * generated_count + 1 { return 2 }
|
|
passed[0] = command
|
|
passed_count: s32 = 1
|
|
// The tool runs the project's commands, so it gets the folders too.
|
|
for folder: generated[0:generated_count] {
|
|
passed[passed_count] = "--module-path"
|
|
passed[passed_count + 1] = folder
|
|
passed_count += 2
|
|
}
|
|
index = 0
|
|
while index < count {
|
|
passed[passed_count] = filtered[index]
|
|
passed_count += 1
|
|
index += 1
|
|
}
|
|
empty: [0]string
|
|
return PkgRunTool(root, locked, current, current.tool,
|
|
passed[0:passed_count], empty[:], empty[:])
|
|
}
|
|
if whole && (command == "build" || command == "run") {
|
|
if command == "build" && count > 0 { return PkgFail("usage: ziran build") }
|
|
program: string = PkgNativeBuild(root, locked, current, launcher)
|
|
if program.count == 0 { return 2 }
|
|
if command == "build" { return 0 }
|
|
run := PkgStrings(cast(s64)count + 1)
|
|
if run.count != cast(s64)count + 1 { return PkgFail("cannot allocate arguments") }
|
|
run[0] = program
|
|
run_count: s32 = 1
|
|
skipped: bool = false
|
|
index = 0
|
|
while index < count {
|
|
if filtered[index] == "--" && !skipped { skipped = true }
|
|
else {
|
|
run[run_count] = filtered[index]
|
|
run_count += 1
|
|
}
|
|
index += 1
|
|
}
|
|
empty: [0]string
|
|
return PkgRun(run[0:run_count], "", empty[:], empty[:])
|
|
}
|
|
if command == "install" {
|
|
prefix: string = PkgJoin(PkgGetenv("HOME"), "/.local")
|
|
passed := PkgStrings(cast(s64)count + 1)
|
|
if passed.count != cast(s64)count + 1 { return 2 }
|
|
passed[0] = "install"
|
|
passed_count: s32 = 1
|
|
index = 0
|
|
while index < count {
|
|
part: string = filtered[index]
|
|
if part == "--prefix" {
|
|
if index + 1 >= count { return PkgFail("--prefix needs a directory") }
|
|
prefix = filtered[index + 1]
|
|
index += 1
|
|
} else if part.count > 9 && part[0:9] == "--prefix=" {
|
|
prefix = part[9:]
|
|
} else {
|
|
passed[passed_count] = part
|
|
passed_count += 1
|
|
}
|
|
index += 1
|
|
}
|
|
if prefix.count == 0 || prefix == "/.local" {
|
|
return PkgFail("set HOME or pass --prefix")
|
|
}
|
|
if prefix[0] != cast(u8)47 { prefix = PkgJoin3(PkgCwd(), "/", prefix) }
|
|
bin: string = current.install_bin
|
|
if bin.count == 0 { bin = current.name }
|
|
if current.tool.count == 0 {
|
|
if passed_count > 1 { return PkgFail("usage: ziran install [--prefix DIR]") }
|
|
return PkgNativeInstall(root, locked, current, launcher, prefix, bin)
|
|
}
|
|
names: [2]string
|
|
values: [2]string
|
|
names[0] = "ZIRAN_INSTALL_PREFIX"
|
|
names[1] = "ZIRAN_INSTALL_BIN"
|
|
values[0] = prefix
|
|
values[1] = bin
|
|
return PkgRunTool(root, locked, current, current.tool,
|
|
passed[0:passed_count], names[:], values[:])
|
|
}
|
|
empty: [0]string
|
|
if command == "run" && PkgHasArgument(flags, "--target=c") {
|
|
compiler := "zi2c"
|
|
binary := PkgJoin3(PkgLauncherDirectory(launcher), "/", compiler)
|
|
program_root := ""
|
|
program_file := ""
|
|
map := ""
|
|
libraries := PkgGetenv("LDLIBS")
|
|
search_count: s64 = 1
|
|
if current != null { search_count += current.root_count }
|
|
if locked != null { search_count += locked.count }
|
|
search := PkgStrings(search_count)
|
|
if search.count != search_count { return 2 }
|
|
searches: s64 = 0
|
|
if project {
|
|
binary = PkgJoin3(PkgToolDirectory(launcher, locked.tool_path, compiler), "/", compiler)
|
|
if !PathExists(binary) {
|
|
make_args: [5]string = .["make", "-C", locked.tool_path, "all", "BUILD_DIR=build"]
|
|
if PkgRunToStderr(make_args[:], "") != 0 { return PkgFail("cannot build pinned Ziran compiler") }
|
|
}
|
|
program_root = PkgJoin3(root, "/", current.roots[0])
|
|
if !PkgHasSource(flags[1:]) { program_file = PkgJoin3(root, "/", current.entry) }
|
|
map = PkgWriteMap(root, locked, current)
|
|
if map.count == 0 {
|
|
if pkg_failure_reported { return 2 }
|
|
return PkgFail("cannot build project import map")
|
|
}
|
|
index = 0
|
|
while index < locked.count {
|
|
if !locked.packages[index].source {
|
|
search[searches] = locked.packages[index].path
|
|
searches += 1
|
|
}
|
|
index += 1
|
|
}
|
|
index = 0
|
|
while index < current.link_count {
|
|
libraries = PkgJoin3(libraries, " -l", current.links[index])
|
|
index += 1
|
|
}
|
|
}
|
|
standard := PkgGetenv("ZIRAN_STD")
|
|
if standard.count == 0 {
|
|
standard = PkgJoin(ParentDirectory(binary), "/../../std")
|
|
if !DirectoryExists(standard) { standard = PkgJoin(ParentDirectory(binary), "/../../../std") }
|
|
}
|
|
if DirectoryExists(standard) {
|
|
search[searches] = standard; searches += 1
|
|
}
|
|
return ScriptRun(filtered[0:count], binary, root, program_root, program_file,
|
|
search[0:searches], map, libraries, PkgCacheRoot())
|
|
}
|
|
return PkgDispatch(command, filtered[0:count], project, root, locked,
|
|
current, launcher, empty[:], empty[:])
|
|
}
|
|
|
|
// Native platform and ABI are independent of the compiler's c/go backend.
|
|
PkgArtifactTarget :: (target: string) -> bool {
|
|
if !PkgPackageName(target) || target[0] == cast(u8)45 ||
|
|
target[target.count - 1] == cast(u8)45 { return false }
|
|
parts: s32 = 0
|
|
previous: u8 = 0
|
|
for ch: target {
|
|
if ch == cast(u8)45 {
|
|
if previous == ch { return false }
|
|
parts += 1
|
|
}
|
|
previous = ch
|
|
}
|
|
return parts >= 2
|
|
}
|
|
|
|
PkgArtifactGraphKey :: (locked: *PkgLock, state: *string, reusable: *bool) {
|
|
PkgArtifactMix(state, locked.tool_commit)
|
|
for i: -1..locked.count - 1 {
|
|
path := locked.tool_path
|
|
if i >= 0 {
|
|
path = locked.packages[i].path
|
|
PkgArtifactMix(state, locked.packages[i].id)
|
|
PkgArtifactMix(state, locked.packages[i].commit)
|
|
}
|
|
PkgArtifactMix(state, PkgGitHead(path))
|
|
args: [6]string = .["git", "-C", path, "status", "--porcelain", "--untracked-files=all"]
|
|
status := PkgCapture(args[:], "")
|
|
if status.code != 0 || status.text.count > 0 { <<reusable = false }
|
|
}
|
|
}
|
|
|
|
// Resolve invocation selectors against package identities, including root aliases.
|
|
PkgArtifactOptionColon :: (entry: string) -> s64 {
|
|
at: s64 = 0
|
|
while at < cast(s64)entry.count && entry[at] != cast(u8)58 { at += 1 }
|
|
return at
|
|
}
|
|
|
|
PkgArtifactAlias :: (locked: *PkgLock, selected: s32, preferred: string) -> string {
|
|
for i: 0..locked.root_edge_count - 1 {
|
|
if locked.root_edges[i].value == locked.packages[selected].id &&
|
|
PkgFoldEqual(locked.root_edges[i].name, preferred) { return PkgJoin("", locked.root_edges[i].name) }
|
|
}
|
|
for i: 0..locked.root_edge_count - 1 {
|
|
if locked.root_edges[i].value == locked.packages[selected].id { return PkgJoin("", locked.root_edges[i].name) }
|
|
}
|
|
return PkgJoin("", locked.packages[selected].name)
|
|
}
|
|
|
|
PkgArtifactOptions :: (root: string, locked: *PkgLock, selected: s32,
|
|
alias: string, overrides: []string) -> string {
|
|
values: [256]string
|
|
count: s32 = 0
|
|
for entry: overrides {
|
|
colon := PkgArtifactOptionColon(entry)
|
|
if PkgSelect(locked, entry[0:colon]) == selected {
|
|
values[count] = entry[colon + 1:]
|
|
count += 1
|
|
}
|
|
}
|
|
return PkgResolveOptionsWithOverrides(root, alias, locked.packages[selected].path, values[0:count])
|
|
}
|
|
|
|
PkgArtifactVisit :: (root: string, locked: *PkgLock, current: *PkgManifest,
|
|
selected: s32, alias: string, artifact: string, target: string,
|
|
launcher: string, stack: []string, depth: s32, overrides: []string, validate: bool) -> string {
|
|
if selected < 0 || selected >= locked.count { PkgFail("artifact package is not locked"); return "" }
|
|
item: *PkgLocked = *locked.packages[selected]
|
|
identity := PkgJoin3(item.id, ":", artifact)
|
|
if depth >= 64 { PkgFail("artifact dependency cycle or depth limit"); return "" }
|
|
for i: 0..depth - 1 {
|
|
if stack[i] == identity { PkgFail("artifact dependency cycle"); return "" }
|
|
}
|
|
stack[depth] = identity
|
|
recipe: PkgArtifactRecipe
|
|
if item.source || !PkgArtifactRead(PkgJoin(item.path, "/ziran.toml"), artifact, *recipe) {
|
|
PkgFail("unknown or invalid artifact recipe"); return ""
|
|
}
|
|
options := PkgArtifactOptions(root, locked, selected, alias, overrides)
|
|
if options.count == 0 { return "" }
|
|
if validate {
|
|
for request: recipe.requires[0:recipe.require_count] {
|
|
colon := PkgArtifactOptionColon(request)
|
|
if colon == 0 || colon >= cast(s64)request.count - 1 { PkgFail("invalid artifact dependency selection"); return "" }
|
|
child := selected
|
|
if request[0:colon] != "self" {
|
|
child = PkgFindById(locked, PkgEdgeValue(*item.edges[0], item.edge_count, request[0:colon]))
|
|
}
|
|
if child < 0 { PkgFail("artifact package is not locked"); return "" }
|
|
if PkgArtifactVisit(root, locked, current, child, PkgArtifactAlias(locked, child, alias),
|
|
request[colon + 1:], target, launcher, stack, depth + 1, overrides, true).count == 0 { return "" }
|
|
}
|
|
return options
|
|
}
|
|
state := PkgDigest("ziran-artifact-v1")
|
|
PkgArtifactMix(*state, identity)
|
|
PkgArtifactMix(*state, target)
|
|
PkgArtifactMix(*state, PkgRead(options).text)
|
|
reusable := true
|
|
PkgArtifactGraphKey(locked, *state, *reusable)
|
|
graph_before := state
|
|
PkgArtifactMix(*state, PkgArtifactHashFile(launcher))
|
|
compiler := PkgJoin(locked.tool_path, "/build/bin/zi2c")
|
|
if PathExists(compiler) { PkgArtifactMix(*state, PkgArtifactHashFile(compiler)) }
|
|
cc := PkgGetenv("CC")
|
|
if cc.count == 0 { cc = "cc" }
|
|
cc_args: [2]string = .[cc, "--version"]
|
|
version := PkgCapture(cc_args[:], "")
|
|
if version.code != 0 { PkgFail("cannot identify artifact compiler (CC must be one executable)"); return "" }
|
|
PkgArtifactMix(*state, cc)
|
|
PkgArtifactMix(*state, version.text)
|
|
locate: [2]string = .["which", cc]
|
|
location := PkgCapture(locate[:], "")
|
|
if location.code != 0 { PkgFail("cannot locate artifact compiler"); return "" }
|
|
compiler_hash := PkgArtifactHashFile(PkgTrim(location.text))
|
|
if compiler_hash.count == 0 { PkgFail("cannot hash artifact compiler"); return "" }
|
|
PkgArtifactMix(*state, compiler_hash)
|
|
// The entire recipe declaration and relevant native build environment.
|
|
recipe_hash := PkgArtifactHashFile(PkgJoin(item.path, "/ziran.toml"))
|
|
PkgArtifactMix(*state, recipe_hash)
|
|
flags: [16]string = .["PATH", "CFLAGS", "CPPFLAGS", "CXX", "CXXFLAGS", "LDFLAGS", "LDLIBS", "AR", "LD", "RANLIB", "PKG_CONFIG", "PKG_CONFIG_PATH", "PKG_CONFIG_LIBDIR", "SYSROOT", "SDKROOT", "MACOSX_DEPLOYMENT_TARGET"]
|
|
for flag: flags { PkgArtifactMix(*state, PkgGetenv(flag)) }
|
|
input_hashes: [PKG_ARTIFACT_INPUT_LIMIT]string
|
|
for i: 0..recipe.input_count - 1 {
|
|
path := PkgJoin3(item.path, "/", recipe.inputs[i])
|
|
if !PkgArtifactInside(item.path, path) || DirectoryExists(path) {
|
|
PkgFail("artifact input must be a file inside its package"); return ""
|
|
}
|
|
hash := PkgArtifactHashFile(path)
|
|
if hash.count == 0 { PkgFail("cannot hash artifact input"); return "" }
|
|
input_hashes[i] = hash
|
|
PkgArtifactMix(*state, hash)
|
|
}
|
|
dependencies := ""
|
|
for i: 0..recipe.require_count - 1 {
|
|
request := recipe.requires[i]
|
|
colon: s64 = 0
|
|
while colon < cast(s64)request.count && request[colon] != cast(u8)58 { colon += 1 }
|
|
if colon == 0 || colon == cast(s64)request.count { PkgFail("invalid artifact dependency selection"); return "" }
|
|
dependency := request[0:colon]
|
|
child := selected
|
|
child_alias := alias
|
|
if dependency != "self" {
|
|
child = PkgFindById(locked, PkgEdgeValue(*item.edges[0], item.edge_count, dependency))
|
|
if child >= 0 { child_alias = PkgArtifactAlias(locked, child, dependency) }
|
|
}
|
|
descriptor := PkgArtifactVisit(root, locked, current, child, child_alias,
|
|
request[colon + 1:], target, launcher, stack, depth + 1, overrides, false)
|
|
if descriptor.count == 0 { return "" }
|
|
PkgArtifactMix(*state, PkgArtifactHashFile(descriptor))
|
|
PkgArtifactMix(*state, descriptor)
|
|
dependencies = PkgJoin3(dependencies, request, "\t")
|
|
dependencies = PkgJoin3(dependencies, descriptor, "\n")
|
|
}
|
|
metadata := PkgJoin3("schema\t1\npackage\t", item.id, "\nartifact\t")
|
|
metadata = PkgJoin3(metadata, artifact, "\nplatform\t")
|
|
metadata = PkgJoin3(metadata, target, "\nkey\t")
|
|
metadata = PkgJoin3(metadata, state, "\n")
|
|
transaction := PkgArtifactBegin(PkgJoin(PkgCacheRoot(), "/artifacts"), state,
|
|
metadata, *recipe, reusable)
|
|
if !transaction.valid { PkgFail("cannot stage artifact"); return "" }
|
|
if transaction.hit { return PkgJoin(transaction.directory, "/artifact.tsv") }
|
|
dependency_file := PkgJoin(transaction.stage, "/dependencies.tsv")
|
|
if !PkgWrite(dependency_file, dependencies) { PkgArtifactRemove(transaction.stage); return "" }
|
|
names: [5]string = .["ZIRAN_ARTIFACT_OUTPUT", "ZIRAN_ARTIFACT_TARGET", "ZIRAN_ARTIFACT_DEPENDENCIES", "MAKEFLAGS", "CMAKE_BUILD_PARALLEL_LEVEL"]
|
|
values: [5]string = .[transaction.stage, target, dependency_file, "-j1", "1"]
|
|
args: [6]string = .["artifact", artifact, "--platform", target, "--output", transaction.stage]
|
|
code := PkgRunSelectedToolOptions(root, locked, current, selected, alias, args[:], names[:], values[:], options)
|
|
// Refuse publication if recipe inputs changed while the provider ran.
|
|
for i: 0..recipe.input_count - 1 {
|
|
if PkgArtifactHashFile(PkgJoin3(item.path, "/", recipe.inputs[i])) != input_hashes[i] { code = 2 }
|
|
}
|
|
graph_after := PkgDigest("ziran-artifact-v1")
|
|
PkgArtifactMix(*graph_after, identity)
|
|
PkgArtifactMix(*graph_after, target)
|
|
PkgArtifactMix(*graph_after, PkgRead(options).text)
|
|
after_reusable := true
|
|
PkgArtifactGraphKey(locked, *graph_after, *after_reusable)
|
|
if graph_after != graph_before || (reusable && !after_reusable) ||
|
|
recipe_hash != PkgArtifactHashFile(PkgJoin(item.path, "/ziran.toml")) { code = 2 }
|
|
if !PkgArtifactFinish(*transaction, *recipe, code) {
|
|
PkgFail("artifact provider failed or omitted a declared output"); return ""
|
|
}
|
|
return PkgJoin(transaction.directory, "/artifact.tsv")
|
|
}
|
|
|
|
PkgBuildArtifactResolved :: (project: string, package: string, artifact: string,
|
|
target: string, locked: *PkgLock, current: *PkgManifest, launcher: string, overrides: []string) -> s32 {
|
|
if !PkgArtifactTarget(target) { return PkgFail("artifact --platform needs an explicit OS-ARCH-ABI target") }
|
|
if overrides.count > 256 { return PkgFail("too many artifact options") }
|
|
for i: 0..cast(s64)overrides.count - 1 {
|
|
entry := overrides[i]
|
|
colon := PkgArtifactOptionColon(entry)
|
|
if colon == 0 || colon >= cast(s64)entry.count - 1 || !PkgPackageName(entry[0:colon]) {
|
|
return PkgFail("--option needs Alias:key=value")
|
|
}
|
|
selected := PkgSelect(locked, entry[0:colon])
|
|
if selected < 0 { return PkgFail("--option package is not locked or is ambiguous") }
|
|
equal := colon + 1
|
|
while equal < cast(s64)entry.count && entry[equal] != cast(u8)61 { equal += 1 }
|
|
for j: 0..i - 1 {
|
|
prior := overrides[j]
|
|
split := PkgArtifactOptionColon(prior)
|
|
end := split + 1
|
|
while end < cast(s64)prior.count && prior[end] != cast(u8)61 { end += 1 }
|
|
if PkgSelect(locked, prior[0:split]) == selected && prior[split + 1:end] == entry[colon + 1:equal] {
|
|
return PkgFail("duplicate artifact option")
|
|
}
|
|
}
|
|
}
|
|
// Validate every supplied package and the complete prerequisite graph before
|
|
// compiler identification, cache lookup or any provider invocation.
|
|
for entry: overrides {
|
|
selected := PkgSelect(locked, entry[0:PkgArtifactOptionColon(entry)])
|
|
if PkgArtifactOptions(project, locked, selected, PkgArtifactAlias(locked, selected, entry[0:PkgArtifactOptionColon(entry)]), overrides).count == 0 { return 2 }
|
|
}
|
|
selected := PkgSelect(locked, package)
|
|
if selected < 0 { return PkgFail("artifact package is not locked") }
|
|
alias := PkgArtifactAlias(locked, selected, package)
|
|
stack: [64]string
|
|
if PkgArtifactVisit(project, locked, current, selected, alias, artifact, target,
|
|
launcher, stack[:], 0, overrides, true).count == 0 { return 2 }
|
|
descriptor := PkgArtifactVisit(project, locked, current, PkgSelect(locked, package),
|
|
alias, artifact, target, launcher, stack[:], 0, overrides, false)
|
|
if descriptor.count == 0 { return 2 }
|
|
PkgPrint(PkgJoin(descriptor, "\n"), false)
|
|
return 0
|
|
}
|
|
|
|
// Programmatic entry uses the same lock/override resolver as package commands.
|
|
PkgBuildArtifact :: (project: string, package: string, artifact: string,
|
|
target: string) -> s32 {
|
|
empty: [0]string
|
|
return PkgBuildArtifactWithOptions(project, package, artifact, target, empty[:])
|
|
}
|
|
|
|
PkgBuildArtifactWithOptions :: (project: string, package: string, artifact: string,
|
|
target: string, overrides: []string) -> s32 {
|
|
locked := PkgNewLock()
|
|
current := PkgNewManifest()
|
|
if !PkgReadLock(PkgJoin(project, "/ziran.lock"), locked) ||
|
|
!PkgCheckoutGraph(project, locked, false, false, current) { return PkgFail("cannot resolve artifact package graph") }
|
|
return PkgBuildArtifactResolved(project, package, artifact, target, locked,
|
|
current, PkgJoin(locked.tool_path, "/build/bin/ziran"), overrides)
|
|
}
|