| 1 | (* Host: what the compiler asks of the system it runs on, beyond the |
| 2 | library. The host is the system running raz; the target, the one its |
| 3 | program will run on. This file is macOS's, for the compiler running |
| 4 | on a Mac (a cross compiler built for one is staged with it, as |
| 5 | src/cross.sh does); Host.linux.raz says what each procedure is for |
| 6 | and what a host must give, and this one gives the same five. |
| 7 | |
| 8 | macOS differs from Linux in four things the compiler asks: |
| 9 | - there is no /proc: the compiler's own path is _NSGetExecutablePath, |
| 10 | which may name it through a link, so realpath follows them; |
| 11 | - there is one C library, libSystem, so cLibrary says glibc, which |
| 12 | here means "the C library the system has", and there is no musl; |
| 13 | - there is no memfd_create: a program raz run starts is written to a |
| 14 | file, in $TMPDIR (/tmp if it is not set), named by the compiler's |
| 15 | process number, and the file stays after the program replaces the |
| 16 | compiler, which macOS clears out of $TMPDIR in time; |
| 17 | - a file's code signature belongs to the file: a program written |
| 18 | in place over one that has run is killed. prepareRun's name is new |
| 19 | each time, and writeExecutable removes a file that is there first. |
| 20 | |
| 21 | Depends on: Sys and Files for the system's services, Scan for memory |
| 22 | that lasts until exit, Diag and Fmt for paths. |
| 23 | Leaves out: compiling and the executable format, which are Parse and |
| 24 | Exe's. *) |
| 25 | module Host; |
| 26 | import Sys, Files, Fmt, Diag, Scan; |
| 27 | import SYSTEM; |
| 28 | |
| 29 | type |
| 30 | CLib* = (none, glibc, musl); (* the C library to link against *) |
| 31 | |
| 32 | const |
| 33 | Mode755 = 1EDH; (* rwxr-xr-x *) |
| 34 | PathMax = 4096; (* more than macOS's 1024, which realpath needs *) |
| 35 | NoRoom: ERROR = "no writable directory for the program"; |
| 36 | |
| 37 | extern proc _NSGetExecutablePath(buf: ^U8; size: ^U32): I32; |
| 38 | extern proc realpath(path, resolved: ^U8): ^U8; |
| 39 | |
| 40 | (* The directory of the library, ending in what the name of raz leaves, |
| 41 | "lib": the one beside raz as the system says it, links followed, or, |
| 42 | if it won't say, beside argument 0 as it was typed *) |
| 43 | proc libraryDir*: []const U8; |
| 44 | var raw, real: ^U8; size: U32; path: []const U8; n, i: INT; |
| 45 | begin |
| 46 | raw := Scan.grab(PathMax); |
| 47 | real := Scan.grab(PathMax); |
| 48 | size := U32(PathMax); |
| 49 | path := Files.arg(0); |
| 50 | if (_NSGetExecutablePath(raw, SYSTEM.ADR(size)) = 0) and (realpath(raw, real) # nil) then |
| 51 | n := 0; |
| 52 | while real[n] # 0 do INC(n); end; |
| 53 | path := real[0..n]; |
| 54 | end; |
| 55 | i := LEN(path); |
| 56 | while (i > 0) and (path[i - 1] # '/') do DEC(i); end; |
| 57 | if i = 0 then return "lib"; end; |
| 58 | return Diag.say("{}lib", [Fmt.str(path[0..i])]); |
| 59 | end; |
| 60 | |
| 61 | (* The C library a program is linked against: the Mac's one, libSystem *) |
| 62 | proc cLibrary*: CLib; |
| 63 | begin |
| 64 | return glibc; |
| 65 | end; |
| 66 | |
| 67 | (* The file at path made to hold data, an executable: any file there |
| 68 | removed first (see above), then written and given mode 755. False if |
| 69 | it failed. *) |
| 70 | proc writeExecutable*(path, data: []const U8): BOOL; |
| 71 | begin |
| 72 | _ := Sys.remove(path); |
| 73 | return Files.write(path, data) and (Sys.chmod(path, Mode755) >= 0); |
| 74 | end; |
| 75 | |
| 76 | (* The path raz run writes its program to: a file in $TMPDIR, if that |
| 77 | is a directory the compiler may write to *) |
| 78 | proc prepareRun*: ([]const U8, ERROR); |
| 79 | var dir: []const U8; |
| 80 | begin |
| 81 | dir := Sys.env("TMPDIR"); |
| 82 | if LEN(dir) = 0 then dir := "/tmp"; end; |
| 83 | if dir[LEN(dir) - 1] = '/' then dir := dir[0..LEN(dir) - 1]; end; |
| 84 | if Sys.access(dir, {Sys.writing}) # 0 then return "", NoRoom; end; |
| 85 | return Diag.say("{}/raz-run-{}", [Fmt.str(dir), Fmt.int(Sys.pid())]), nil; |
| 86 | end; |
| 87 | |
| 88 | (* The program at path runs in the compiler's place, with the compiler's |
| 89 | arguments from number first on as its own, first as its argument 0, |
| 90 | and the environment. It returns only if that failed: minus the errno. *) |
| 91 | proc execute*(path: []const U8; first: INT): INT; |
| 92 | var args, env: ^[]const U8; n, m, i: INT; more: BOOL; |
| 93 | begin |
| 94 | n := Files.argCount() - first; |
| 95 | args := ^[]const U8(Scan.grab((n + 1) * SIZE([]const U8))); |
| 96 | for i := 0 to n - 1 do args[i] := Files.arg(first + i); end; |
| 97 | m := 0; |
| 98 | _, more := Sys.envAt(0); |
| 99 | while more do INC(m); _, more := Sys.envAt(m); end; |
| 100 | env := ^[]const U8(Scan.grab((m + 1) * SIZE([]const U8))); |
| 101 | for i := 0 to m - 1 do env[i], _ := Sys.envAt(i); end; |
| 102 | return Sys.execve(path, args[0..n], env[0..m]); |
| 103 | end; |
| 104 | |
| 105 | end Host. |