Files
ziran/cmd/package_script.zi
T

234 lines
10 KiB
Ziran

#import "package_common"
#import "file_linux"
#import "byte_text_linux"
#import "text"
using Vectors :: #import "vec"
#scope_export
ParentDirectory :: (path: string) -> string {
end := path.count
while end > 0 && path[end - 1] != #char "/" { end -= 1 }
if end == 0 { return "." }
if end == 1 { return "/" }
return path[0:end - 1]
}
#scope_file
Hash :: (value: string) -> string {
return PkgDigest(value)
}
Parent :: (path: string) -> string {
return ParentDirectory(path)
}
ModuleName :: (path: string) -> string {
start := path.count
while start > 0 && path[start - 1] != #char "/" { start -= 1 }
suffix: s64 = 3
if Ends(path, ".zir") { suffix = 4 }
return path[start:path.count - suffix]
}
Ends :: (value: string, suffix: string) -> bool {
return value.count >= suffix.count && value[value.count - suffix.count:] == suffix
}
FingerprintFile :: (path: string, state: *string) -> bool {
// A compiler can outgrow ordinary project files, most of all instrumented.
content := PkgReadLimited(path, 1073741824)
if !content.valid { return false }
<<state = Hash(PkgJoin3(<<state, Hash(path), Hash(content.text)))
// #load may reach outside every module search root. Until the frontend
// publishes its loaded-file dependency list, compile these each time.
if Ends(path, ".zi") && ContainsFoldASCII(content.text, "#load") { return false }
return (<<state).count > 0
}
// Conservative content-based invalidation also detects newly added imports
// that can shadow a standard or package module. Canonical paths prevent
// symlink cycles; sorted entries make the key independent of readdir order.
FingerprintTree :: (path: string, state: *string, seen: *Vec(string), depth: s32, ignored: string) -> bool {
if depth > 64 { return false }
canonical := PkgCanonical(path)
if canonical.count == 0 { return false }
if canonical == ignored { return true }
<<state = Hash(PkgJoin3(<<state, Hash(path), Hash(canonical)))
if (<<state).count == 0 { return false }
for at: 0..seen.count - 1 { if (<<seen)[at] == canonical { return true } }
if !VecPush(<<seen, canonical) { return false }
cursor := OpenDirectory(path)
if !cursor.valid { return false }
names: Vec(string)
while true {
entry := NextDirectoryEntry(*cursor)
if !entry.valid { break }
name := TextFromBytes(entry.name[0:entry.name_count])
if name == "." || name == ".." || name == ".git" || name == "__pycache__" { continue }
if !VecPush(names, PkgJoin("", name)) { CloseDirectory(*cursor); return false }
}
CloseDirectory(*cursor)
for index: 1..names.count - 1 {
at := index
while at > 0 && PkgLexLess(names[at], names[at - 1]) {
earlier := names[at - 1]
names[at - 1] = names[at]
names[at] = earlier
at -= 1
}
}
valid := true
for name: VecSlice(names, 0, names.count) {
child := PkgJoin3(path, "/", name)
if DirectoryExists(child) {
// Build outputs, private work and backups hold no sources; a
// module path inside one is fingerprinted as its own root.
if name[0] == #char "." || name == "build" || name == "work" || name == "backup" ||
name == "node_modules" { continue }
if !FingerprintTree(child, state, seen, depth + 1, ignored) { valid = false; break }
} else if Ends(name, ".zi") || Ends(name, ".zir") || Ends(name, ".h") ||
name == "ziran.toml" || name == "ziran.lock" || name == "ziran.local.toml" {
if !FingerprintFile(child, state) { valid = false; break }
}
}
VecFree(names)
return valid
}
CacheKey :: (binary: string, source: string, root: string, entry: string,
options: []string, search: []string, map: string, libraries: string,
ignored: string, valid: *bool) -> string {
state := Hash(PkgJoin3(Hash(source), Hash(root), Hash(entry)))
state = Hash(PkgJoin3(state, Hash(PkgGetenv("ZIRAN_STD")), Hash(libraries)))
for option: options { state = Hash(PkgJoin(state, Hash(option))) }
seen: Vec(string)
<<valid = FingerprintFile(binary, *state) && FingerprintFile(source, *state)
if map.count > 0 { <<valid = <<valid && FingerprintFile(map, *state) }
for path: search {
if path.count > 0 && !FingerprintTree(path, *state, *seen, 0, ignored) { <<valid = false }
}
VecFree(seen)
return state
}
#scope_export
// Flags end at the first script or `--`. Values of compiler options are
// skipped, so a root named something.zi is not mistaken for the script.
ScriptArgumentEnd :: (arguments: []string) -> s32 {
index: s32 = 1
while index < arguments.count {
part := arguments[index]
if part == "--" || Ends(part, ".zi") || Ends(part, ".zir") { return index + 1 }
if part == "--root" || part == "--entry" ||
part == "--module-path" || part == "--bind" || part == "--bind-host" { index += 1 }
index += 1
}
return cast(s32)arguments.count
}
// Run one script as a native program built into the cache.
// The caller supplies the pinned project's import map and search roots.
ScriptRun :: (arguments: []string, binary: string, project_root: string,
program_root: string, program_file: string, roots: []string,
map: string, libraries: string, cache_root: string) -> s32 {
source := program_file
root := program_root
entry := ""
cache := true
options: Vec(string)
search: Vec(string)
for path: roots { VecPush(search, path) }
separator: s64 = arguments.count
index: s64 = 0
while index < arguments.count {
part := arguments[index]
if part == "--" { separator = index + 1; break }
if part == "--target=c" { index += 1; continue }
if part == "--no-cache" { cache = false; index += 1; continue }
if part == "--root" || part == "--entry" ||
part == "--module-path" || part == "--bind" || part == "--bind-host" {
if index + 1 >= arguments.count { return PkgFail(PkgJoin(part, " needs a value")) }
value := arguments[index + 1]
if part == "--root" { root = value }
else if part == "--entry" { entry = value }
else {
VecPush(options, part); VecPush(options, value)
if part == "--module-path" {
at: s64 = 0
while at < value.count && value[at] != #char "=" { at += 1 }
if at < value.count { value = value[at + 1:] }
VecPush(search, value)
}
}
index += 2
} else if part == "--diagnostics=text" || part == "--diagnostics=json" {
VecPush(options, part); index += 1
} else if Ends(part, ".zi") || Ends(part, ".zir") {
if source.count > 0 && source != program_file { return PkgFail("run takes one script") }
source = part
separator = index + 1
if separator < arguments.count && arguments[separator] == "--" { separator += 1 }
break
} else { return PkgFail(PkgJoin("unknown script run argument: ", part)) }
}
if source.count == 0 { return PkgFail("run --target=c needs a script or project entry") }
source = PkgCanonical(source)
if source.count == 0 { return PkgFail("cannot find the script") }
if root.count == 0 { root = Parent(source) }
root = PkgCanonical(root)
if root.count == 0 { return PkgFail("cannot find the source root") }
// A native tool beside the project's module root, such as tools/x.zi,
// is built from its own folder and still imports the project's modules.
if !(source.count > root.count && source[0:root.count] == root && source[root.count] == #char "/") {
VecPush(options, "--module-path"); VecPush(options, root)
root = PkgCanonical(Parent(source))
}
if entry.count == 0 { entry = PkgJoin(ModuleName(source), ":main") }
module := entry
for at: 0..entry.count - 1 {
if entry[at] == #char ":" { module = entry[0:at]; break }
}
VecPush(search, root)
if project_root.count > 0 { VecPush(search, project_root) }
directory := PkgJoin(cache_root, "/native")
if !PkgMkdirs(cache_root) || !CreatePrivateDirectory(directory) { return PkgFail("cannot create the script cache") }
ignored := PkgCanonical(directory)
valid: bool
state := CacheKey(binary, source, root, entry, VecSlice(options, 0, options.count),
VecSlice(search, 0, search.count), map, libraries, ignored, *valid)
directory = PkgJoin3(directory, "/", state)
if !CreatePrivateDirectory(directory) { return PkgFail("cannot create the script output directory") }
ready := PkgJoin(directory, "/.ready")
stamp := PkgRead(ready)
program := PkgJoin3(directory, "/", module)
if !cache || !valid || !stamp.valid || stamp.text != state || !PathExists(program) {
RemoveFile(ready)
build: Vec(string)
VecPush(build, binary); VecPush(build, "--exe")
VecPush(build, "--root"); VecPush(build, root)
VecPush(build, "--entry"); VecPush(build, entry)
VecPush(build, "-o"); VecPush(build, directory)
for option: VecSlice(options, 0, options.count) { VecPush(build, option) }
VecPush(build, source)
names: [2]string = .["ZIRAN_PACKAGE_MAP", "LDLIBS"]
values: [2]string = .[map, libraries]
result := PkgRun(VecSlice(build, 0, build.count), "", names[:], values[:])
VecFree(build)
if result != 0 { return result }
stable: bool
after := CacheKey(binary, source, root, entry, VecSlice(options, 0, options.count),
VecSlice(search, 0, search.count), map, libraries, ignored, *stable)
// Never publish output under a key calculated from different inputs.
if !stable || after != state { valid = false }
if cache && valid { PkgWrite(ready, state) }
}
run: Vec(string)
VecPush(run, program)
for at: separator..arguments.count - 1 { VecPush(run, arguments[at]) }
names: [2]string = .["ZIRAN_SCRIPT_FILE", "ZIRAN_SCRIPT_ROOT"]
values: [2]string = .[source, root]
result := PkgRun(VecSlice(run, 0, run.count), "", names[:], values[:])
VecFree(run); VecFree(options); VecFree(search)
return result
}