(zilch lang ninja): support include

Change-Id: I6a6a69643a4a1d2b96386851a26d13237737f645
This commit is contained in:
puck 2025-05-11 22:21:07 +00:00
parent a6757baf0e
commit 48692fc50d
2 changed files with 24 additions and 13 deletions

View file

@ -217,7 +217,7 @@
(try-resolve (build-rule-restat rule) #f) (try-resolve (build-rule-restat rule) #f)
(try-resolve (build-rule-rspfile rule) #f) (try-resolve (build-rule-rspfile rule) #f)
(try-resolve (build-rule-rspfile-content rule) #f))) (try-resolve (build-rule-rspfile-content rule) #f)))
(define (bytevector-prefix? bv in-bv index) (define (bytevector-prefix? bv in-bv index)
(define bv-len (bytevector-length bv)) (define bv-len (bytevector-length bv))
(define (inner-loop j) (define (inner-loop j)
@ -229,9 +229,9 @@
(if (> (+ index bv-len) (bytevector-length in-bv)) (if (> (+ index bv-len) (bytevector-length in-bv))
#f #f
(inner-loop 0))) (inner-loop 0)))
;; Reads a full Ninja file, returning a <build-file> record. ;; Reads a full Ninja file, returning a <build-file> record.
(define (read-ninja-file strval) (define (read-ninja-file-inner strval into read-other-file)
(define i 0) (define i 0)
(define (eat-whitespace) (define (eat-whitespace)
(cond (cond
@ -295,10 +295,11 @@
(define (read-token) (define (read-token)
(define start-i i) (define start-i i)
(define post-space (or (bytestring-index strval (lambda (ch) (not (= ch #x20))) i) i)) (define post-space-index (bytestring-index strval (lambda (ch) (not (= ch #x20))) i))
(define post-space (or post-space-index i))
(define token (define token
(cond (cond
((>= i (bytevector-length strval)) ((>= post-space (bytevector-length strval))
'eof) 'eof)
((bytevector-prefix? #u8(#x23) strval post-space) ((bytevector-prefix? #u8(#x23) strval post-space)
(let ((end-of-comment (or (bytestring-index strval (lambda (ch) (= ch #x0A)) post-space) (bytevector-length strval)))) (let ((end-of-comment (or (bytestring-index strval (lambda (ch) (= ch #x0A)) post-space) (bytevector-length strval))))
@ -339,7 +340,9 @@
((string=? token "include") 'include) ((string=? token "include") 'include)
((string=? token "subninja") 'subninja) ((string=? token "subninja") 'subninja)
(else token)))) (else token))))
(else #f))) ((and (not post-space-index) (= (bytevector-u8-ref strval i) #x20))
'eof)
(else (fprintf (current-error-port) "found token ~S here\n" (number->string (bytevector-u8-ref strval i) 16)) #f)))
(unless (member token '(newline eof #f)) (eat-whitespace)) (unless (member token '(newline eof #f)) (eat-whitespace))
token) token)
(define (expect-token expected) (define (expect-token expected)
@ -445,17 +448,22 @@
(let ((edge (read-build-edge file))) (set-build-file-build-edges! file (cons edge (build-file-build-edges file)))) (let ((edge (read-build-edge file))) (set-build-file-build-edges! file (cons edge (build-file-build-edges file))))
(read-toplevel file)) (read-toplevel file))
((default) ((default)
(do () ((expect-token 'newline)) (set-build-file-default-targets! file (cons (resolve-evalstring (read-eval-string-text #t) file #f #f #f) (build-file-default-targets file))))) (do () ((expect-token 'newline)) (set-build-file-default-targets! file (cons (resolve-evalstring (read-eval-string-text #t) file #f #f #f) (build-file-default-targets file))))
((subninja include) (error "todo: includes")) (read-toplevel file))
((include) (read-ninja-file-inner (read-other-file (resolve-evalstring (read-eval-string-text #t) file #f #f #f)) file read-other-file)
(read-toplevel file))
((subninja) (error "todo: subninja"))
((pool) (read-pool) (read-toplevel file)) ((pool) (read-pool) (read-toplevel file))
((newline) (read-toplevel file)) ((newline) (read-toplevel file))
(else (else
(unless (string? token) (error "unexpected" (list token i))) (unless (string? token) (error "unexpected" (list token i (bytevector-length strval))))
(unless (expect-token 'equals) (error "expected =, found" (read-token))) (unless (expect-token 'equals) (error "expected =, found" (read-token)))
(let* ((value (read-eval-string-text #f)) (resolved (resolve-evalstring value file #f #f #f))) (let* ((value (read-eval-string-text #f)) (resolved (resolve-evalstring value file #f #f #f)))
(set-build-file-global-variables! file (mapping-set! (build-file-global-variables file) token resolved))) (set-build-file-global-variables! file (mapping-set! (build-file-global-variables file) token resolved)))
(read-toplevel file))) (read-toplevel file)))
file) file)
(define out-file (read-toplevel (make-build-file (mapping (make-default-comparator)) '() (mapping (make-default-comparator)) '() (mapping (make-default-comparator))))) (read-toplevel into))
(define (read-ninja-file strval read-other-file)
(define out-file (read-ninja-file-inner strval (make-build-file (mapping (make-default-comparator)) '() (mapping (make-default-comparator)) '() (mapping (make-default-comparator))) read-other-file))
(for-each (lambda (f) (unless (string=? (build-edge-rule f) "phony") (set-build-edge-resolved! f (build-rule-resolve (mapping-ref (build-file-rules out-file) (build-edge-rule f)) f out-file)))) (build-file-build-edges out-file)) (for-each (lambda (f) (unless (string=? (build-edge-rule f) "phony") (set-build-edge-resolved! f (build-rule-resolve (mapping-ref (build-file-rules out-file) (build-edge-rule f)) f out-file)))) (build-file-build-edges out-file))
out-file))) out-file)))

View file

@ -101,10 +101,13 @@
(set-ninja-build-config-root-dir! conf configured-vfs) (set-ninja-build-config-root-dir! conf configured-vfs)
(define (read-file-at-path path)
(set! path (string-append "build/" path))
(define last-slash (string-contains-right path "/"))
(call-with-port (store-path-open (vfs-file-ref configured-vfs (string-copy path 0 last-slash) (string-copy path (+ 1 last-slash))))
(lambda (p) (read-bytevector (* 20 1024 1024) p))))
(define ninja-file (define ninja-file
(read-ninja-file (read-ninja-file (read-file-at-path "build.ninja") read-file-at-path))
(call-with-port (store-path-open (vfs-file-ref configured-vfs "build" "build.ninja"))
(lambda (p) (read-bytevector (* 20 1024 1024) p)))))
; Process the build.ninja file. ; Process the build.ninja file.
(define-values (edge-ref defaults) (process-ninja-file ninja-file conf "build")) (define-values (edge-ref defaults) (process-ninja-file ninja-file conf "build"))