1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
|
#!@theme_d_guile@ \
-e main -s
!#
;; A script to compute Theme-D module dependencies.
;; Copyright (C) 2025 Tommi Höynälänmaa
;; Distributed under GNU LGPL.
(use-modules (rnrs lists)
(rnrs io ports)
(th-scheme-utilities stdutils)
(th-scheme-utilities singleton))
(import (only (rnrs base) assert))
(import (srfi srfi-1))
(define (process-module? sx l-s-lib)
(or
(and (symbol? sx) (null? l-s-lib))
(and (list? sx)
(not-null? sx)
(equal? (drop-right sx 1) l-s-lib))))
(define (process-toplevel-expression sx l-s-lib)
(if (and (list? sx) (not-null? sx)
(memq (car sx) '(import import-and-reexport use)))
(filter (lambda (sx-module)
(and (list? sx-module)
(not-null? sx-module)
(equal? (drop-right sx-module 1) l-s-lib)))
(cdr sx))
'()))
(define (process-all-expressions l-sx l-s-lib)
(map (lambda (mn) (if (symbol? mn) (list mn) mn))
(apply append
(map (lambda (sx) (process-toplevel-expression sx l-s-lib)) l-sx))))
(define (is-module-name? x)
(or (symbol? x)
(and (list? x)
(not-null? x)
(if (for-all symbol? x) #t #f))))
(define (get-name mn)
(assert (is-module-name? mn))
(cond
((symbol? mn) mn)
((and (list? mn) (not-null? mn)) (last mn))
(else (raise 'internal-error))))
(define (get-imports mn s-unit-type l-s-lib)
(assert (is-module-name? mn))
(assert (and (list? l-s-lib) (for-all symbol? l-s-lib)))
(let* ((s-name
(if (symbol? mn) mn (last mn)))
(str-ext
(case s-unit-type
((interface) "thi")
((body) "thb")
((proper-program) "thp")
((script) "ths")
(else (raise 'internal-error-with-extensions))))
(str-filename
(string-append (symbol->string s-name) "." str-ext))
(ip (open-input-file str-filename))
(l-sx (read-file ip)))
(close-input-port ip)
(if (and (list? l-sx)
(eqv? (length l-sx) 1)
(list? (car l-sx))
(>= (length (car l-sx)) 2)
(memq (caar l-sx)
'(define-interface
define-body
define-proper-program
define-script)))
(process-all-expressions (cddar l-sx) l-s-lib)
(begin
(display "Unrecognized translation unit syntax.\n"
(standard-error-port))
(exit 1)))))
(define (collect-module-names sgt-module-names mn-cur s-unit-type l-s-lib)
(assert (singleton? sgt-module-names))
(assert (is-module-name? mn-cur))
(assert (symbol? s-unit-type))
(assert (and (list? l-s-lib) (for-all symbol? l-s-lib)))
(let ((l-mn (singleton-get-element sgt-module-names))
(load
(lambda (mn)
(collect-module-names
sgt-module-names mn 'body l-s-lib))))
(if (and (not (member mn-cur l-mn))
(process-module? mn-cur l-s-lib))
(begin
(if (eq? s-unit-type 'body)
(begin
(singleton-set-element! sgt-module-names (cons mn-cur l-mn))
(for-each
load
(get-imports mn-cur 'interface l-s-lib))))
(for-each
load
(get-imports mn-cur s-unit-type l-s-lib))))))
(define (collect-toplevel-module-names mn-target l-sx s-unit-type l-s-lib)
(if (not (is-module-name? mn-target))
(begin
(display "Syntax error in the source module.\n"
(standard-error-port))
(exit 1)))
(if (eq? s-unit-type 'interface)
(process-all-expressions (cddar l-sx) l-s-lib)
(let* ((l-imports (process-all-expressions (cddar l-sx) l-s-lib))
(sgt-module-names (make-singleton '())))
(if (eq? s-unit-type 'body)
(collect-module-names sgt-module-names mn-target 'interface l-s-lib))
(for-each
(lambda (mn) (collect-module-names sgt-module-names mn 'body l-s-lib))
l-imports)
(delete-duplicates
(append l-imports (singleton-get-element sgt-module-names))))))
(define (get-intf-name str-name)
(string-append "__intf_" str-name))
(define (get-impl-name str-name)
(string-append "__impl_" str-name))
(define (print-output-deps str-name str-ext l-sx-deps)
(cond
((string=? str-ext "tci")
(display (string-append (get-intf-name str-name) ".go :")))
((string=? str-ext "tcb")
(display (string-append (get-impl-name str-name) ".go :")))
(else
(display (string-append str-name ".go :"))))
;; A module body depends on its interface.
(if (string=? str-ext "tcb")
(begin
(display " \\\n ")
(display (string-append str-name ".tcb"))
(display " \\\n ")
(display (string-append (get-intf-name str-name) ".go"))))
(for-each
(lambda (l-s-dep)
(if (is-module-name? l-s-dep)
(let ((str-name (symbol->string (get-name l-s-dep))))
(display " \\\n ")
(display
(string-append
(get-intf-name str-name)
".go"))
(display " \\\n ")
(display
(string-append
str-name
".tcb")))
(begin
(display "warning: invalid module " (standard-error-port))
(display l-s-dep (standard-error-port))
(newline (standard-error-port)))))
l-sx-deps)
(newline)
(newline))
(define (process-toplevel-expression2 sx l-s-lib)
(if (and (list? sx) (not-null? sx)
(equal? (car sx) 'prelink-body))
(filter (lambda (sx-module)
(and (list? sx-module)
(not-null? sx-module)
(equal? (drop-right sx-module 1) l-s-lib)))
(cdr sx))
'()))
(define (process-all-expressions2 l-sx l-s-lib)
(apply append
(map (lambda (sx) (process-toplevel-expression2 sx l-s-lib)) l-sx)))
(define (print-output-deps2 str-name l-sx-deps)
(display (string-append (get-impl-name str-name) ".go :"))
(for-each
(lambda (l-s-dep)
(if (and (list? l-s-dep) (not-null? l-s-dep) (for-all symbol? l-s-dep))
(begin
(display " \\\n ")
(display
(string-append
(get-impl-name (symbol->string (last l-s-dep)))
".go")))
(begin
(display "warning: invalid module " (standard-error-port))
(display l-s-dep (standard-error-port))
(newline (standard-error-port)))))
l-sx-deps)
(newline)
(newline))
(define (get-filename-without-ext filename)
(let ((index (string-index filename #\.)))
(if (eq? index #f)
filename
(string-take filename index))))
(define (parse-library str-lib)
(map string->symbol (split-string str-lib #\space)))
(define (main args)
(if (eqv? (length args) 3)
(let* ((str-source (cadr args))
(str-lib (caddr args))
(l-s-lib (parse-library str-lib))
(str-name (get-filename-without-ext str-source))
(ip (open-input-file str-source))
(l-sx (read-file ip)))
(close-input-port ip)
(if (and (list? l-sx)
(eqv? (length l-sx) 1)
(list? (car l-sx))
(>= (length (car l-sx)) 2)
(memq (caar l-sx)
'(define-interface
define-body
define-proper-program
define-script)))
(begin
(let* ((str-ext
(case (caar l-sx)
((define-interface) "tci")
((define-body) "tcb")
((define-proper-program) "tcp")
((define-script) "tcs")
(else
(display "Internal error.\n" (standard-error-port))
(exit 1))))
(s-unit-type
(case (caar l-sx)
((define-interface) 'interface)
((define-body) 'body)
((define-proper-program) 'proper-program)
((define-script) 'script)
(else
(display "Internal error.\n" (standard-error-port))
(exit 1))))
(mn-target (cadar l-sx))
(l-deps
(collect-toplevel-module-names
mn-target l-sx s-unit-type l-s-lib)))
(print-output-deps str-name str-ext l-deps)
(if (string=? str-ext "tcb")
(let ((l-deps2 (process-all-expressions2 (cddar l-sx) l-s-lib)))
(if (not (null? l-deps2))
(print-output-deps2 str-name l-deps2))))
(exit 0)))
(begin
(display "Unrecognized translation unit syntax.\n"
(standard-error-port))
(exit 1))))
(begin
(display "Invalid number of arguments.\n" (standard-error-port))
(exit 1))))
|