Files
ziran/cmd/package_map.zi
T

367 lines
14 KiB
Ziran

#import "package_common"
#import "package_manifest"
#import "package_lock"
#import "byte_text_linux"
PkgModule :: struct {
name: string
path: string
}
PkgModuleSet :: struct {
items: [4096]PkgModule
count: s32
}
PkgModuleGraph :: struct {
sets: [66]PkgModuleSet
}
PkgModuleByPath :: (set: *PkgModuleSet, path: string) -> string {
index: s32 = 0
while index < set.count {
if set.items[index].path == path { return PkgJoin("", set.items[index].name) }
index += 1
}
return ""
}
PkgHasModule :: (set: *PkgModuleSet, name: string) -> bool {
index: s32 = 0
while index < set.count {
if set.items[index].name == name { return true }
index += 1
}
return false
}
PkgScanModules :: (directory: string, manifest: *PkgManifest,
output: *PkgModuleSet) -> bool {
base: string = PkgCanonical(directory)
if base.count == 0 { return false }
root_index: s32 = 0
while root_index < manifest.root_count {
source_root: string = PkgCanonical(PkgJoin3(base, "/",
manifest.roots[root_index]))
if source_root.count == 0 ||
!(source_root == base || (source_root.count > base.count &&
source_root[0:cast(s64)base.count] == base &&
source_root[cast(s64)base.count] == cast(u8)47)) {
return false
}
args: [8]string
args[0] = "find"
args[1] = source_root
args[2] = "-maxdepth"
args[3] = "2"
args[4] = "-type"
args[5] = "f"
args[6] = "-name"
args[7] = "*.zi"
result: PkgProcessResult = PkgCapture(args[:], "")
if result.code != 0 { return false }
at: s64 = 0
while at < cast(s64)result.text.count {
end: s64 = at
while end < cast(s64)result.text.count &&
result.text[end] != cast(u8)10 { end += 1 }
path: string = result.text[at:end]
at = end + 1
if path.count <= source_root.count + 1 ||
path[0:cast(s64)source_root.count] != source_root { continue }
relative: string = path[cast(s64)source_root.count + 1:]
slash: s64 = -1
index: s64 = 0
while index < cast(s64)relative.count {
if relative[index] == cast(u8)47 {
if slash >= 0 { slash = -2; break }
slash = index
}
index += 1
}
name: string = ""
if slash == -1 && PkgEndsWith(relative, ".zi") {
name = relative[0:cast(s64)relative.count - 3]
} else if slash > 0 && relative[slash + 1:] == "module.zi" {
name = relative[0:slash]
}
if name.count == 0 { continue }
if !PkgIdentifier(name) || PkgHasModule(output, name) ||
output.count >= 4096 { return false }
output.items[output.count].name = name
output.items[output.count].path = path
output.count += 1
}
root_index += 1
}
return true
}
// Adds the modules of each generated folder (current.generated, canonical
// paths) to the root package's set. Locked packages never see them: a
// package resolves short imports in its own modules and its dependencies'
// exports only. A generated module may not reuse a root module's name.
PkgAddGeneratedModules :: (current: *PkgManifest, output: *PkgModuleSet) -> bool {
if current.generated_count == 0 { return true }
folder: *PkgManifest = PkgNewManifest()
found: *PkgModuleSet = cast(*PkgModuleSet)PkgAllocZero(cast(u64)size_of(PkgModuleSet))
if folder == null || found == null { return false }
folder.roots[0] = "."
folder.root_count = 1
for directory: current.generated[0:current.generated_count] {
found.count = 0
if !PkgScanModules(directory, folder, found) {
unused PkgFail(PkgJoin("cannot read the modules of --module-path folder ", directory))
return false
}
index: s32 = 0
while index < found.count {
name: string = found.items[index].name
if PkgHasModule(output, name) {
unused PkgFail(PkgJoin3(PkgJoin3("generated module ", name, " in "),
directory, " has the name of another project module"))
return false
}
if output.count >= 4096 { return false }
output.items[output.count].name = name
output.items[output.count].path = found.items[index].path
output.count += 1
index += 1
}
}
return true
}
PkgAppendModuleSet :: (builder: *PkgBuilder, id: string, directory: string,
set: *PkgModuleSet) -> void {
PkgAppend(builder, PkgJoin3("P\t", id, "\t"))
PkgAppend(builder, directory)
PkgAppend(builder, "\n")
index: s32 = 0
while index < set.count {
PkgAppend(builder, PkgJoin3("M\t", id, "\t"))
PkgAppend(builder, set.items[index].name)
PkgAppend(builder, "\t")
PkgAppend(builder, set.items[index].path)
PkgAppend(builder, "\n")
index += 1
}
}
PkgFindExport :: (manifest: *PkgManifest, name: string) -> bool {
index: s32 = 0
while index < manifest.export_count {
if manifest.exports[index].name == name { return true }
index += 1
}
return false
}
PkgShortEdge :: struct {
name: string
target: string
internal: string
qualified: string
count: s32
}
// Makes each direct dependency's exports visible to one package. Every
// export is importable as ALIAS/Module. The short name Module also works
// unless the package has its own module of that name (which wins) or
// another direct dependency exports it too (then importing the short name
// reports both qualified spellings).
PkgAddExports :: (builder: *PkgBuilder, locked: *PkgLock,
graph: *PkgModuleGraph, owner: s32, current: *PkgManifest,
root: string) -> bool {
owner_manifest: *PkgManifest = current
owner_id: string = "root"
edge_count: s32 = locked.root_edge_count
if owner >= 0 {
owner_manifest = *locked.packages[owner].manifest
owner_id = locked.packages[owner].id
edge_count = locked.packages[owner].edge_count
}
// A package's public exports also have its own manifest-qualified
// spelling. Consumer aliases belong to incoming edges, not this scope.
owner_root := root
if owner >= 0 { owner_root = locked.packages[owner].path }
// An existing dependency alias keeps its entire qualified namespace.
own_prefix_available := true
dependency: s32 = 0
while dependency < owner_manifest.dependency_count {
if owner_manifest.dependencies[dependency].alias == owner_manifest.name {
own_prefix_available = false
}
dependency += 1
}
own_export: s32 = 0
while own_prefix_available && own_export < owner_manifest.export_count {
exported := owner_manifest.exports[own_export]
own_export += 1
source := PkgCanonical(PkgJoin3(owner_root, "/", exported.value))
internal := PkgModuleByPath(*graph.sets[owner + 1], source)
if internal.count == 0 { return false }
qualified := PkgJoin3(owner_manifest.name, "/", exported.name)
PkgAppend(builder, PkgJoin3("D\t", owner_id, "\t"))
PkgAppend(builder, PkgJoin3(qualified, "\t", owner_id))
PkgAppend(builder, PkgJoin3("\t", internal, "\n"))
}
// On the heap: a package can export thousands of modules.
shorts: *PkgShortEdge = cast(*PkgShortEdge)PkgAllocZero(4096 *
cast(u64)size_of(PkgShortEdge))
if shorts == null { return false }
short_count: s32 = 0
index: s32 = 0
while index < owner_manifest.dependency_count {
dep: *PkgDependency = *owner_manifest.dependencies[index]
target_id: string = ""
if owner < 0 {
target_id = PkgEdgeValue(*locked.root_edges[0], edge_count, dep.alias)
} else {
target_id = PkgEdgeValue(*locked.packages[owner].edges[0],
edge_count, dep.alias)
}
target: s32 = PkgFindById(locked, target_id)
if target < 0 { return false }
target_manifest: *PkgManifest = *locked.packages[target].manifest
alias_index: s32 = 0
while alias_index < dep.alias_count {
if !PkgFindExport(target_manifest, dep.aliases[alias_index].name) {
PkgFail(PkgJoin3("module_aliases names ", dep.aliases[alias_index].name,
", which the dependency does not export"))
return false
}
alias_index += 1
}
export_index: s32 = 0
while export_index < target_manifest.export_count {
exported: PkgNamedValue = target_manifest.exports[export_index]
export_index += 1
source: string = PkgCanonical(PkgJoin3(
locked.packages[target].path, "/", exported.value))
internal: string = PkgModuleByPath(*graph.sets[target + 1], source)
if internal.count == 0 { return false }
qualified: string = PkgJoin3(dep.alias, "/", exported.name)
PkgAppend(builder, PkgJoin3("D\t", owner_id, "\t"))
PkgAppend(builder, PkgJoin3(qualified, "\t", target_id))
PkgAppend(builder, PkgJoin3("\t", internal, "\n"))
name: string = PkgAliasName(dep, exported.name)
if PkgHasModule(*graph.sets[owner + 1], name) { continue }
found: s32 = 0
while found < short_count && shorts[found].name != name { found += 1 }
if found < short_count {
shorts[found].qualified = PkgJoin3(shorts[found].qualified,
", ", qualified)
shorts[found].count += 1
continue
}
if short_count >= 4096 { return false }
shorts[short_count].name = name
shorts[short_count].target = target_id
shorts[short_count].internal = internal
shorts[short_count].qualified = qualified
shorts[short_count].count = 1
short_count += 1
}
index += 1
}
index = 0
while index < short_count {
edge: *PkgShortEdge = *shorts[index]
index += 1
if edge.count > 1 {
PkgAppend(builder, PkgJoin3("A\t", owner_id, "\t"))
PkgAppend(builder, PkgJoin3(edge.name, "\t", edge.qualified))
PkgAppend(builder, "\n")
continue
}
PkgAppend(builder, PkgJoin3("D\t", owner_id, "\t"))
PkgAppend(builder, PkgJoin3(edge.name, "\t", edge.target))
PkgAppend(builder, PkgJoin3("\t", edge.internal, "\n"))
}
return true
}
PkgWriteMap :: (root: string, locked: *PkgLock, current: *PkgManifest) -> string {
graph: *PkgModuleGraph = cast(*PkgModuleGraph)(PkgAllocZero(cast(u64)size_of(PkgModuleGraph)))
memory: *u8 = PkgAlloc(33554432)
if graph == null || memory == null { return "" }
builder: PkgBuilder
builder.bytes = BytesFromPointer(memory, 33554432)
builder.valid = true
if !PkgScanModules(root, current, *graph.sets[0]) { return "" }
if !PkgAddGeneratedModules(current, *graph.sets[0]) { return "" }
PkgAppendModuleSet(*builder, "root", root, *graph.sets[0])
// Files in a generated folder belong to the root package wherever the
// folder is, so their own imports resolve as the project's.
for folder: current.generated[0:current.generated_count] {
PkgAppend(*builder, PkgJoin3("P\troot\t", folder, "\n"))
}
index: s32 = 0
while index < locked.count {
item: *PkgLocked = *locked.packages[index]
if !PkgScanModules(item.path, *item.manifest, *graph.sets[index + 1]) {
return ""
}
PkgAppendModuleSet(*builder, item.id, item.path, *graph.sets[index + 1])
index += 1
}
standard: *PkgManifest = PkgNewManifest()
if standard == null { return "" }
standard.roots[0] = "std"
standard.root_count = 1
if !PkgScanModules(locked.tool_path, standard, *graph.sets[locked.count + 1]) {
return ""
}
PkgAppendModuleSet(*builder, "std", locked.tool_path,
*graph.sets[locked.count + 1])
if !PkgAddExports(*builder, locked, graph, -1, current, root) { return "" }
index = 0
while index < locked.count {
if !PkgAddExports(*builder, locked, graph, index, current, root) {
return ""
}
index += 1
}
index = 0
while index < current.bridge_count {
name: string = current.bridges[index]
if !PkgHasModule(*graph.sets[0], name) { return "" }
direct: s32 = 0
while direct < locked.root_edge_count {
target: s32 = PkgFindById(locked,
locked.root_edges[direct].value)
if target < 0 { return "" }
// A dependency resolves short imports in its own package first,
// so one that owns a module of this name keeps using its own.
if locked.packages[target].source ||
PkgHasModule(*graph.sets[target + 1], name) {
direct += 1
continue
}
PkgAppend(*builder, PkgJoin3("D\t", locked.packages[target].id,
"\t"))
PkgAppend(*builder, name)
PkgAppend(*builder, "\troot\t")
PkgAppend(*builder, name)
PkgAppend(*builder, "\n")
direct += 1
}
index += 1
}
if !builder.valid { return "" }
directory: string = PkgJoin(root, "/build/.ziran")
if !PkgMkdirs(directory) { return "" }
content := TextFromBytes(builder.bytes[0:builder.at])
digest := PkgDigest(content)
if digest == "" { return "" }
// Pin this invocation to its exact graph. A different concurrent graph
// must not replace the pathname passed to the compiler child.
snapshot := PkgJoin3(directory, "/module-map.", PkgJoin(digest, ".tsv"))
if !PkgWriteAtomic(snapshot, content) { return "" }
// Keep the latest complete map available for local inspection. Compiler
// invocations always receive their content-specific snapshot above.
if !PkgWriteAtomic(PkgJoin(directory, "/module-map.tsv"), content) { return "" }
return snapshot
}