Files
ziran/cmd/package_entry.zi
T

62 lines
2.0 KiB
Ziran

#import "package"
#import "package_common"
#import "std/byte_text_linux"
#import "std/c_string"
using Vectors :: #import "std/vec"
CompilerVersion :: #import "version"
#scope_file
libc :: #system_library "libc";
ReadLink :: (path: *u8, output: *u8, count: usize) -> isize #foreign libc "readlink";
#if #defined(__APPLE__) {
ExecutablePathRaw :: (output: *u8, size: *u32) -> s32 #foreign libc "_NSGetExecutablePath";
RealPathRaw :: (path: *u8, resolved: *u8) -> *u8 #foreign libc "realpath";
}
#scope_export
#program_export
main :: (argc: s32, argv: **u8) -> s32 {
defer PkgReleaseStringArena()
if argc == 2 {
argument := TextFromCString(argv[1])
if argument == "version" || argument == "--version" {
print("ziran %\n", CompilerVersion.Version)
return 0
}
}
arguments: Vec(string)
index: s32 = 1
while index < argc {
if !VecPush(arguments, TextFromCString(argv[index])) { return 2 }
index += 1
}
// PATH may supply just "ziran" as argv[0]. Sibling compiler tools are
// resolved beside this executable, as in the previous native launcher.
launcher: [4096]u8
length: s64 = -1
#if #defined(__APPLE__) {
unresolved: [4096]u8
capacity: u32 = 4096
if ExecutablePathRaw(*unresolved[0], *capacity) == 0 &&
RealPathRaw(*unresolved[0], *launcher[0]) != null {
length = cast(s64)TextFromCString(*launcher[0]).count
}
} else {
path: [32]u8
CopyCString("/proc/self/exe", path[:])
length = cast(s64)ReadLink(*path[0], *launcher[0], 4095)
}
if length <= 0 || length >= 4095 {
original := TextFromCString(argv[0])
length = cast(s64)original.count
if length > 4095 { length = 4095 }
cursor: s64 = 0
while cursor < length { launcher[cursor] = original[cursor]; cursor += 1 }
}
launcher[length] = 0
code := PackageMain(TextFromBytes(launcher[0:length]),
VecSlice(arguments, 0, arguments.count))
VecFree(arguments)
return code
}