AtlatestRepositorysigil-ui

sigil-ui / tree / scripts / macossetup.sgl

1#!/usr/bin/env sigil
2;;; SDK-free compile and ABI checks; run from Linux, never opens a display.
3(import (sigil shell prelude) (only (sigil list) any))
4(requires (tools taskset nice prlimit df du ps))
5(define root (realpath (path (script-dir) "../..")))
6(define font (realpath (arg 1)))
7(define window (realpath (arg 2)))
8(define zig (realpath (arg 3)))
9(define headers (realpath (arg 4)))
10(define resource-root (realpath (arg 5)))
11(define cpu-list (arg 6))
12(define desired-nice (string->number (arg 7)))
13(define maximum-memory (string->number (arg 8)))
14(define minimum-free (string->number (arg 9)))
15(define maximum-scratch (string->number (arg 10)))
16(define maximum-seconds (string->number (arg 11)))
17(unless (and desired-nice maximum-memory minimum-free maximum-scratch maximum-seconds
18 (<= 0 desired-nice 19) (> maximum-memory 0) (> minimum-free 0)
19 (> maximum-scratch 0) (> maximum-seconds 0))
20 (error "Expected explicit positive resource limits"))
21(define build (path root "build/macos-qualification"))
22(unless (and (equal? (out ,zig version) "0.16.0")
23 (file? (path headers "sigil/sigil.h"))
24 (file? (path font "native/coretext.c")))
25 (error "Expected Zig 0.16.0, matching Sigil headers, and the font candidate"))
26(each name '("tmp" "cache" "global-cache" "logs")
27 (ensure-directory (path build name)))
28(setenv! "TMPDIR" (path build "tmp"))
29(setenv! "ZIG_LOCAL_CACHE_DIR" (path build "cache"))
30(setenv! "ZIG_GLOBAL_CACHE_DIR" (path build "global-cache"))
31(define priority (string->number (trim (out ps -p ,(process-id) -o "ni="))))
32(unless priority (error "Cannot inspect caller priority"))
33(define (scratch-directories)
34 (define candidates '())
35 (each directory (list (path resource-root "build") (path root "build") (path font "build")
36 (path font "test/coretext-host/build") (path root "test/macos-drawing/build")
37 (path font "test/freetype-host/build")
38 (path window "build") (path window "test/appkit/conformance/build"))
39 (when (directory? directory)
40 (define canonical (realpath directory))
41 (unless (member canonical candidates) (set! candidates (cons canonical candidates)))))
42 (filter (lambda (directory)
43 (not (any (lambda (other)
44 (and (not (equal? directory other))
45 (starts-with? directory (str other "/")))) candidates)))
46 candidates))
47(define (room! group)
48 (let ((disk (regex-split "\\s+" (trim (cadr (lines df -Pk ,root)))))
49 (scratch 0)
50 (rss 0))
51 (each directory (scratch-directories)
52 (define usage ($? du -sk ,directory))
53 ;; Zig removes temporary objects concurrently. Accept only ENOENT in
54 ;; this owned directory, with a valid summary; other failures stop work.
55 (unless (or (ok? usage)
56 (and (= (exit-status usage) 1)
57 (every (lambda (line)
58 (or (string=? line "")
59 (and (contains? line (str "cannot access '" directory "/"))
60 (ends-with? line "': No such file or directory"))))
61 (split (stderr-of usage) "\n"))))
62 (error "Cannot measure owned scratch" (format-outcome usage)))
63 (define fields (regex-split "\\s+" (trim (stdout-of usage))))
64 (unless (and (pair? fields) (string->number (car fields)))
65 (error "Missing scratch summary" directory))
66 (set! scratch (+ scratch (string->number (car fields)))))
67 (when (< (string->number (list-ref disk 3)) (quotient minimum-free 1024))
68 (error "Free disk crossed the supplied floor"))
69 (when (>= (* scratch 1024) maximum-scratch)
70 (error "Scratch crossed the supplied limit"))
71 (when group
72 (each row (lines ps -eo "pgid=,rss=")
73 (let ((fields (regex-split "\\s+" (trim row))))
74 (when (and (= (length fields) 2)
75 (= (string->number (car fields)) group))
76 (set! rss (+ rss (string->number (cadr fields))))))))
77 (when (>= rss (quotient maximum-memory 1024))
78 (error "Owned process group crossed the supplied RSS limit"))))
79(define (guarded! name executable arguments (opts: (directory root)))
80 (room! #f)
81 (let ((child #f) (started (current-milliseconds))
82 (log-file (path build "logs" (str name ".log"))))
83 (dynamic-wind
84 (lambda () #f)
85 (lambda ()
86 (set! child
87 (apply process-spawn (tool-path 'taskset)
88 (append
89 (list "-c" cpu-list (tool-path 'nice) "-n" (str (max 0 (- desired-nice priority)))
90 (tool-path 'prlimit) (str "--as=" maximum-memory) "--" executable)
91 arguments
92 (list new-process-group: #t die-with-parent: #t
93 cwd: directory in: "/dev/null" out: log-file err: #t))))
94 (let poll ()
95 (when (process-alive? child)
96 (room! (process-pgid child))
97 (when (> (- (current-milliseconds) started) (* maximum-seconds 1000))
98 (error "Process exceeded the supplied deadline" name))
99 (sleep 0.5)
100 (poll)))
101 (unless (= 0 (process-wait child))
102 (error "Compilation failed" name (read-text log-file)))
103 (room! #f)
104 (println "PASS: ~a" name))
105 (lambda ()
106 (when child
107 (when (process-alive? child) (process-signal! child 'kill))
108 (process-wait child))))))
109(define (compile! name arguments)
110 (guarded! name zig (cons "cc" arguments)))