2025-04-29 15:04:43 +00:00
|
|
|
(define-library (zilch lang ninja build)
|
|
|
|
|
(import
|
|
|
|
|
(scheme base) (scheme lazy)
|
|
|
|
|
(zilch file) (zilch magic) (scheme char)
|
|
|
|
|
(zilch nix drv) (zilch nix path)
|
|
|
|
|
(zilch nixpkgs) (zilch zexpr) (zilch vfs)
|
2025-05-01 13:20:05 +00:00
|
|
|
(chicken format)
|
2025-04-29 15:04:43 +00:00
|
|
|
(srfi 128) (srfi 146) (srfi 152)
|
2025-05-11 22:21:07 +00:00
|
|
|
(zilch lang ninja) (zilch lang ninja config))
|
2025-04-29 15:04:43 +00:00
|
|
|
|
2025-05-11 22:21:07 +00:00
|
|
|
(export process-ninja-file
|
|
|
|
|
built-edge-edge built-edge-out-drv
|
|
|
|
|
built-edge-lib-placeholder built-edge-phony-inputs)
|
2025-04-29 15:04:43 +00:00
|
|
|
|
|
|
|
|
(begin
|
|
|
|
|
(define coreutils (cdr (assoc "out" (nixpkgs "coreutils"))))
|
2025-05-11 22:21:07 +00:00
|
|
|
(define patchelf (cdr (assoc "out" (nixpkgs "patchelf"))))
|
|
|
|
|
(define llvm-bintools (cdr (assoc "out" (nixpkgs-eval "llvmPackages_latest.bintools-unwrapped"))))
|
|
|
|
|
|
|
|
|
|
(define-record-type <built-edge>
|
|
|
|
|
(make-built-edge edge out-drv lib-placeholder phony-inputs)
|
|
|
|
|
built-edge?
|
|
|
|
|
(edge built-edge-edge)
|
|
|
|
|
(out-drv built-edge-out-drv)
|
|
|
|
|
(lib-placeholder built-edge-lib-placeholder)
|
|
|
|
|
(phony-inputs built-edge-phony-inputs))
|
|
|
|
|
|
|
|
|
|
(define-record-type <build-env>
|
|
|
|
|
(make-build-env config vfs build-dir)
|
|
|
|
|
build-env?
|
|
|
|
|
(config build-env-config)
|
|
|
|
|
(vfs build-env-vfs)
|
|
|
|
|
(build-dir build-env-build-dir))
|
2025-04-29 15:04:43 +00:00
|
|
|
|
|
|
|
|
;; normalize a POSIX-y path. Ninja doesn't have an internal concept of path normalisation,
|
|
|
|
|
;; so this is necessary for proper file-finding behavior.
|
|
|
|
|
(define (normalize-path path)
|
|
|
|
|
(define parts (string-split path "/"))
|
|
|
|
|
(define part-stack '())
|
|
|
|
|
(for-each
|
|
|
|
|
(lambda (part)
|
|
|
|
|
(cond
|
|
|
|
|
((string=? part "") (when (null? part-stack) (set! part-stack '(""))))
|
2025-05-11 22:21:07 +00:00
|
|
|
((string=? part "..") (if (or (null? part-stack) (string=? (car part-stack) "") (string=? (car part-stack) "..")) (set! part-stack (cons ".." part-stack)) (set! part-stack (cdr part-stack))))
|
2025-04-29 15:04:43 +00:00
|
|
|
((string=? part "."))
|
|
|
|
|
(else (set! part-stack (cons part part-stack)))))
|
|
|
|
|
parts)
|
|
|
|
|
(string-join (reverse part-stack) "/"))
|
|
|
|
|
|
|
|
|
|
(define (is-valid-store-path-char c)
|
|
|
|
|
(or
|
|
|
|
|
(and (char>=? c #\0) (char<=? c #\9))
|
|
|
|
|
(and (char>=? c #\a) (char<=? c #\z))
|
|
|
|
|
(and (char>=? c #\A) (char<=? c #\Z))
|
|
|
|
|
(member c '(#\+ #\- #\. #\_ #\? #\=))))
|
|
|
|
|
|
|
|
|
|
;; Helper to render nicer derivation names.
|
|
|
|
|
(define (make-valid-store-path-string str)
|
2025-05-01 13:20:05 +00:00
|
|
|
(if (string=? "" str)
|
|
|
|
|
"zilch-ninja"
|
|
|
|
|
(string-map (lambda (c) (if (is-valid-store-path-char c) c #\-)) (if (> (string-length str) 211) (string-copy str 0 211) str))))
|
2025-04-29 15:04:43 +00:00
|
|
|
|
2025-05-11 22:21:07 +00:00
|
|
|
;; Returns a derivation that runs the command for this edge,
|
|
|
|
|
;; inside a Nix derivation with the correct inputs.
|
|
|
|
|
(define (derivation-for-edge env edges current-edge)
|
2025-04-29 15:04:43 +00:00
|
|
|
(define resolved (build-edge-resolved current-edge))
|
|
|
|
|
(when (build-rule-rspfile resolved) (error "rspfile not yet supported" current-edge))
|
2025-05-01 13:20:05 +00:00
|
|
|
(define copy-input-files "")
|
2025-05-11 22:21:07 +00:00
|
|
|
(define is-meson-phony #f)
|
|
|
|
|
|
|
|
|
|
; Appends a single input to the input of this edge's build command.
|
2025-04-29 15:04:43 +00:00
|
|
|
(define (append-file path)
|
2025-05-11 22:21:07 +00:00
|
|
|
; Normalize paths pointing into the build environment.
|
|
|
|
|
(cond
|
|
|
|
|
((string-prefix? "/build/bdir/src/" path)
|
|
|
|
|
(set! path (string-append "../" (string-copy path 12))))
|
|
|
|
|
((string-prefix? "/build/bdir/build/" path)
|
|
|
|
|
(set! path (string-copy path 18))))
|
|
|
|
|
(define input (mapping-ref edges path (lambda () (mapping-ref/default edges (normalize-path path) #f))))
|
|
|
|
|
|
|
|
|
|
(define input-file (and input (car input)))
|
|
|
|
|
(define input-edge (and input (force (cdr input))))
|
2025-04-29 15:04:43 +00:00
|
|
|
|
2025-05-09 13:09:49 +00:00
|
|
|
(cond
|
|
|
|
|
; if input-file is 'base, this is part of the base vfs; we don't filter that right now.
|
|
|
|
|
((eq? input-file 'base) #f)
|
|
|
|
|
|
|
|
|
|
; Phony rule; pass through the inputs literally.
|
2025-05-11 22:21:07 +00:00
|
|
|
((eq? input-file 'phony) (for-each append-file (built-edge-phony-inputs input-edge)))
|
2025-05-09 13:09:49 +00:00
|
|
|
|
|
|
|
|
; This file is produced by another build edge. Add it to our input vfs.
|
|
|
|
|
(input-file
|
2025-05-01 13:20:05 +00:00
|
|
|
(let ((prev-copy-input-files copy-input-files))
|
2025-05-11 22:21:07 +00:00
|
|
|
(set! copy-input-files #~,(string-append #$prev-copy-input-files "\n" "$COREUTILS/mkdir -p bdir/" (build-env-build-dir env) "/$($COREUTILS/dirname " path "); $COREUTILS/cp -rf " #$(force input-file) " bdir/" (build-env-build-dir env) "/" path))))
|
2025-05-09 13:09:49 +00:00
|
|
|
|
|
|
|
|
(else
|
2025-05-01 13:20:05 +00:00
|
|
|
(unless (string-prefix? "/nix/store" path)
|
2025-05-11 22:21:07 +00:00
|
|
|
(error "Path doesn't exist as build edge" (list path (build-edge-outputs current-edge))))))
|
2025-04-29 15:04:43 +00:00
|
|
|
|
2025-05-01 13:20:05 +00:00
|
|
|
; Workaround for Meson not adding the .so as build dependency when linking, instead using a .symbols file.
|
2025-05-11 22:21:07 +00:00
|
|
|
; To replicate Meson's behavior, we use a .so stub generated when a .so.symbols path is seen; this serves
|
|
|
|
|
; the same purpose.
|
|
|
|
|
(when (and input-edge (built-edge-lib-placeholder input-edge))
|
|
|
|
|
(let* ((pair (force (built-edge-lib-placeholder input-edge)))
|
|
|
|
|
(so-path (car pair))
|
|
|
|
|
(so-file (cdr pair)))
|
|
|
|
|
(let ((prev-copy-input-files copy-input-files))
|
|
|
|
|
(set! copy-input-files #~,(string-append #$prev-copy-input-files "\n" "$COREUTILS/mkdir -p bdir/" (build-env-build-dir env) "/$($COREUTILS/dirname " so-path "); $COREUTILS/cp -rf " #$so-file " bdir/" (build-env-build-dir env) "/" so-path)))))
|
|
|
|
|
|
|
|
|
|
; Make sure we have all .so stubs transitively.
|
|
|
|
|
(when
|
|
|
|
|
(and input-edge (built-edge-lib-placeholder input-edge))
|
|
|
|
|
; Depend on all .so.symbols files from the input's edge's dependencies too.
|
|
|
|
|
(let*
|
|
|
|
|
((so-path (car (build-edge-inputs (built-edge-edge input-edge))))
|
|
|
|
|
(recursive-drv (mapping-ref edges so-path (lambda () (mapping-ref edges (normalize-path so-path)))))
|
|
|
|
|
(so-edge (built-edge-edge (force (cdr recursive-drv)))))
|
|
|
|
|
(for-each
|
|
|
|
|
(lambda (input)
|
|
|
|
|
(when (string-suffix? ".so.symbols" input)
|
|
|
|
|
(append-file input)))
|
|
|
|
|
(append (build-edge-inputs so-edge) (build-edge-implicit-dependencies so-edge) (build-edge-order-only-dependencies so-edge)))))
|
|
|
|
|
|
|
|
|
|
(when (string=? path "PHONY")
|
|
|
|
|
(set! is-meson-phony #t)))
|
2025-05-09 13:09:49 +00:00
|
|
|
|
2025-04-29 15:04:43 +00:00
|
|
|
; Add the inputs, implicit dependencies, _and_ order-only dependencies to our vfs.
|
|
|
|
|
(for-each append-file (build-edge-inputs current-edge))
|
|
|
|
|
(for-each append-file (build-edge-implicit-dependencies current-edge))
|
|
|
|
|
(for-each append-file (build-edge-order-only-dependencies current-edge))
|
|
|
|
|
|
2025-05-01 13:20:05 +00:00
|
|
|
; Create parent directories for each output (TODO: implicit outputs?)
|
|
|
|
|
(for-each
|
|
|
|
|
(lambda (path)
|
|
|
|
|
(define prev-copy-input-files copy-input-files)
|
2025-05-11 22:21:07 +00:00
|
|
|
(set! copy-input-files #~,(string-append #$prev-copy-input-files "\n" "$COREUTILS/mkdir -p bdir/" (build-env-build-dir env) "/$($COREUTILS/dirname " path ")")))
|
2025-05-01 13:20:05 +00:00
|
|
|
(build-edge-outputs current-edge))
|
|
|
|
|
|
2025-04-29 15:04:43 +00:00
|
|
|
; Create the VFS.
|
|
|
|
|
(define command-to-run (build-rule-command resolved))
|
|
|
|
|
|
|
|
|
|
; For each output and implicit output, copy them to the derivation's output.
|
|
|
|
|
(define copy-output-files "")
|
|
|
|
|
(define out-placeholder (make-placeholder "out"))
|
|
|
|
|
(define (append-copy-command outpath)
|
2025-05-11 22:21:07 +00:00
|
|
|
(if is-meson-phony
|
|
|
|
|
(set! copy-output-files (string-append copy-output-files "\n" "$COREUTILS/mkdir -p " out-placeholder "/$($COREUTILS/dirname " outpath "); $COREUTILS/cp -rf " outpath " " out-placeholder "/" outpath " || true"))
|
|
|
|
|
(set! copy-output-files (string-append copy-output-files "\n" "$COREUTILS/mkdir -p " out-placeholder "/$($COREUTILS/dirname " outpath "); $COREUTILS/cp -rf " outpath " " out-placeholder "/" outpath))))
|
2025-04-29 15:04:43 +00:00
|
|
|
(for-each append-copy-command (build-edge-outputs current-edge))
|
|
|
|
|
(for-each append-copy-command (build-edge-implicit-outputs current-edge))
|
|
|
|
|
|
2025-05-11 22:21:07 +00:00
|
|
|
(define patch-commands (list))
|
|
|
|
|
(for-each
|
|
|
|
|
(lambda (patch)
|
|
|
|
|
(define result (patch current-edge))
|
|
|
|
|
(when result
|
|
|
|
|
(set! patch-commands (cons result patch-commands))))
|
2025-05-11 22:21:07 +00:00
|
|
|
(ninja-build-config-patches (build-env-config env)))
|
2025-05-11 22:21:07 +00:00
|
|
|
(set! patch-commands (reverse patch-commands))
|
|
|
|
|
(define processed-patch-commands
|
|
|
|
|
#~,(string-join
|
|
|
|
|
(map
|
|
|
|
|
(lambda (item)
|
|
|
|
|
(if (string? item) item
|
|
|
|
|
(string-join item "\n")))
|
|
|
|
|
#$patch-commands)
|
|
|
|
|
"\n"))
|
|
|
|
|
|
2025-04-29 15:04:43 +00:00
|
|
|
; Run the build rule inside the build environment's vfs. This requires copying the entire VFS over, sadly.
|
|
|
|
|
(define command
|
|
|
|
|
#~,(string-append
|
2025-05-11 22:21:07 +00:00
|
|
|
; Copy over the VFS, as we have to make changes to it.
|
2025-05-11 22:21:07 +00:00
|
|
|
#$coreutils "/bin/cp -rf --no-preserve=ownership " #$(build-env-vfs env) " bdir\n"
|
2025-05-01 13:20:05 +00:00
|
|
|
#$coreutils "/bin/chmod ugo+rw -R bdir\n"
|
2025-05-11 22:21:07 +00:00
|
|
|
|
|
|
|
|
; Copy over the other input files.
|
2025-05-01 13:20:05 +00:00
|
|
|
"(COREUTILS=" #$coreutils "/bin; " #$copy-input-files ")\n"
|
2025-05-11 22:21:07 +00:00
|
|
|
#$coreutils "/bin/chmod ugo+rw -R bdir\n"
|
|
|
|
|
|
|
|
|
|
; Run any patches we have received.
|
2025-05-11 22:21:07 +00:00
|
|
|
"(cd bdir; _ZILCH_ROOT=" #$(build-env-vfs env) "; PATH=\"" #$coreutils "/bin:$PATH\"; " #$processed-patch-commands ")\n"
|
2025-05-11 22:21:07 +00:00
|
|
|
|
|
|
|
|
; Enter the build dir and then run the command for this build.
|
2025-05-11 22:21:07 +00:00
|
|
|
"(cd bdir/" (build-env-build-dir env) "; " command-to-run ") || exit 1\n"
|
2025-05-11 22:21:07 +00:00
|
|
|
|
|
|
|
|
; Create the output directory, and copyy over the output files.
|
2025-05-11 22:21:07 +00:00
|
|
|
"(COREUTILS=" #$coreutils"/bin; cd bdir/" (build-env-build-dir env) "; $COREUTILS/mkdir " out-placeholder "\n" #$copy-output-files ")"))
|
2025-04-29 15:04:43 +00:00
|
|
|
|
|
|
|
|
; Create the derivation. The rest of the code, that builds up the full list of edges, will extract each output from it.
|
|
|
|
|
(define outpath
|
|
|
|
|
(cdar
|
|
|
|
|
(store-path-for-ca-drv*
|
|
|
|
|
(make-valid-store-path-string (build-rule-description resolved))
|
|
|
|
|
"x86_64-linux"
|
2025-05-11 22:21:07 +00:00
|
|
|
'("/bin/sh" "-c" "exec /bin/sh $ZILCH_CMDPath")
|
|
|
|
|
`(("ZILCH_CMD" . ,command) ("passAsFile" . "ZILCH_CMD") . ,(ninja-build-config-environment (build-env-config env)))
|
2025-04-29 15:04:43 +00:00
|
|
|
'("out"))))
|
|
|
|
|
outpath)
|
2025-05-11 22:21:07 +00:00
|
|
|
(define (ifs-for-shsym env edges current-edge)
|
|
|
|
|
(define implib (mapping-ref (build-edge-variables current-edge) "IMPLIB" (lambda () (error "SHSYM rule does not have IMPLIB variable"))))
|
|
|
|
|
(define input-path (car (build-edge-inputs current-edge)))
|
|
|
|
|
|
|
|
|
|
(define input-edge (mapping-ref edges input-path (lambda () (mapping-ref edges (normalize-path input-path) (lambda () (error "SHSYM input path does not exist"))))))
|
|
|
|
|
|
|
|
|
|
(define command
|
|
|
|
|
#~,(string-append #$llvm-bintools "/bin/llvm-ifs --output-elf=\"$out\" \"$input\"\n"
|
|
|
|
|
#$patchelf "/bin/patchelf --set-rpath \"$(" #$patchelf "/bin/patchelf --print-rpath \"$input\")\" \"$out\""))
|
|
|
|
|
|
|
|
|
|
(define processed
|
|
|
|
|
(cdar
|
|
|
|
|
(store-path-for-ca-drv*
|
|
|
|
|
(make-valid-store-path-string (string-append "SHSYM shim for " implib))
|
|
|
|
|
"x86_64-linux"
|
|
|
|
|
(list "/bin/sh" "-c" command)
|
|
|
|
|
#~(("input" . #$(force (car input-edge))))
|
|
|
|
|
'("out"))))
|
|
|
|
|
(cons implib processed))
|
|
|
|
|
|
|
|
|
|
(define (build-edge env edges current-edge)
|
|
|
|
|
(if (build-edge-resolved current-edge)
|
|
|
|
|
(let*
|
|
|
|
|
((out-drv (delay (derivation-for-edge env edges current-edge)))
|
|
|
|
|
(is-shsym (string=? (build-edge-rule current-edge) "SHSYM"))
|
|
|
|
|
(shsym-ifs (and is-shsym (delay (ifs-for-shsym env edges current-edge)))))
|
|
|
|
|
(make-built-edge current-edge out-drv shsym-ifs '()))
|
|
|
|
|
(make-built-edge current-edge (delay (phony-edge edges current-edge)) #f (build-edge-inputs current-edge))))
|
2025-04-29 15:04:43 +00:00
|
|
|
|
2025-05-11 22:21:07 +00:00
|
|
|
(define (phony-edge edges current-edge)
|
|
|
|
|
(define copies (list))
|
|
|
|
|
(define (process-input path)
|
|
|
|
|
(define input-file (mapping-ref edges path (lambda () (mapping-ref/default edges (normalize-path path) #f))))
|
2025-05-11 22:21:07 +00:00
|
|
|
(if (eq? (car input-file) 'phony)
|
|
|
|
|
(for-each process-input (built-edge-phony-inputs (force (cdr input-file))))
|
|
|
|
|
(set! copies (cons #~,(string-append "mkdir -p \"$out/$(dirname " path ")\"; cp -rf " #$(force (car input-file)) " $out/" path) copies))))
|
2025-05-11 22:21:07 +00:00
|
|
|
(for-each process-input (build-edge-inputs current-edge))
|
|
|
|
|
|
|
|
|
|
(define command
|
|
|
|
|
#~,(string-join #$copies "\n"))
|
|
|
|
|
|
|
|
|
|
(cdar
|
|
|
|
|
(store-path-for-ca-drv*
|
|
|
|
|
"zilch-ninja-phony-edge"
|
|
|
|
|
"x86_64-linux"
|
2025-05-11 22:21:07 +00:00
|
|
|
'("/bin/sh" "-c" "exec /bin/sh $ZILCH_CMDPath")
|
|
|
|
|
#~(("ZILCH_CMD" . #$command) ("passAsFile" . "ZILCH_CMD") ("PATH" . ,(string-append #$coreutils "/bin")))
|
2025-05-11 22:21:07 +00:00
|
|
|
'("out"))))
|
|
|
|
|
|
|
|
|
|
|
2025-04-29 15:04:43 +00:00
|
|
|
;; process a ninja file and corresponding vfs, and return two values:
|
|
|
|
|
;; - `edge-ref`, a lambda that lets one fetch any build edge;
|
|
|
|
|
;; - `defaults`, a list containing the default build edges.
|
|
|
|
|
;;
|
|
|
|
|
;; If the `environment` is a <derivation> or a <store-path>, it is considered
|
|
|
|
|
;; to be a nixpkgs-style derivation, the same way `nix-shell` works.
|
2025-05-11 22:21:07 +00:00
|
|
|
(define (process-ninja-file file conf relative-to)
|
2025-05-09 13:09:49 +00:00
|
|
|
(unless (or (string=? relative-to "") (string-suffix? "/" relative-to)) (set! relative-to (string-append relative-to "/")))
|
2025-04-29 15:04:43 +00:00
|
|
|
|
|
|
|
|
(define edges (mapping (make-default-comparator)))
|
|
|
|
|
|
|
|
|
|
; record edges for each path in the base vfs.
|
|
|
|
|
(mapping-for-each
|
|
|
|
|
(lambda (kv path)
|
|
|
|
|
(define path (if (string=? (car kv) "") (cdr kv) (string-append (car kv) "/" (cdr kv))))
|
2025-05-01 13:20:05 +00:00
|
|
|
(cond
|
|
|
|
|
((string=? relative-to "") #f)
|
|
|
|
|
((string-prefix? relative-to path) (set! path (string-copy path (string-length relative-to))))
|
|
|
|
|
(else (set! path (string-append "../" path))))
|
2025-05-11 22:21:07 +00:00
|
|
|
(set! edges (mapping-set! edges path (cons 'base #f))))
|
2025-05-11 22:21:07 +00:00
|
|
|
(vfs-contents (ninja-build-config-root-dir conf)))
|
2025-05-11 22:21:07 +00:00
|
|
|
|
2025-05-11 22:21:07 +00:00
|
|
|
(define vfs-store-path (vfs-to-store (ninja-build-config-root-dir conf)))
|
2025-05-11 22:21:07 +00:00
|
|
|
(define env (make-build-env conf vfs-store-path relative-to))
|
2025-04-29 15:04:43 +00:00
|
|
|
(for-each
|
|
|
|
|
(lambda (edge)
|
2025-05-11 22:21:07 +00:00
|
|
|
(define built-edge (delay (build-edge env edges edge)))
|
2025-05-11 22:21:07 +00:00
|
|
|
(define all-outputs (append (build-edge-outputs edge) (build-edge-implicit-outputs edge)))
|
2025-05-09 13:09:49 +00:00
|
|
|
(if (build-edge-resolved edge)
|
2025-05-11 22:21:07 +00:00
|
|
|
(for-each (lambda (v) (set! edges (mapping-set! edges v (cons (delay #~,(string-append #$(force (built-edge-out-drv (force built-edge))) "/" v)) built-edge))))
|
2025-05-11 22:21:07 +00:00
|
|
|
all-outputs)
|
2025-05-09 13:09:49 +00:00
|
|
|
|
|
|
|
|
; This edge is phony; mark the edge as having its outputs.
|
2025-05-11 22:21:07 +00:00
|
|
|
(for-each (lambda (v) (set! edges (mapping-set! edges v (cons 'phony built-edge))))
|
2025-05-11 22:21:07 +00:00
|
|
|
all-outputs)))
|
2025-04-29 15:04:43 +00:00
|
|
|
(build-file-build-edges file))
|
|
|
|
|
|
2025-05-11 22:21:07 +00:00
|
|
|
(define edge-ref
|
|
|
|
|
(lambda (path)
|
2025-05-11 22:21:07 +00:00
|
|
|
(define edge (mapping-ref/default edges path #f))
|
|
|
|
|
(cons (if (promise? (car edge)) (force (car edge)) (car edge)) (force (cdr edge)))))
|
2025-04-29 15:04:43 +00:00
|
|
|
(define defaults (build-file-default-targets file))
|
|
|
|
|
(values edge-ref defaults))))
|