File: compute-theme-d-program-deps.scm.in

package info (click to toggle)
theme-d 7.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 21,240 kB
  • sloc: lisp: 9,688; sh: 5,345; makefile: 716; ansic: 477
file content (180 lines) | stat: -rwxr-xr-x 6,238 bytes parent folder | download
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
#!@theme_d_guile@ \
-e main -s
!#

;; A script to compute Theme-D program dependencies.

;; Copyright (C) 2025 Tommi Höynälänmaa

;; Distributed under GNU LGPL.

(use-modules (srfi srfi-1)
             (rnrs lists)
             (rnrs io ports)
             (th-scheme-utilities stdutils)
             (th-scheme-utilities singleton)
             (th-scheme-utilities hrecord)
             (th-scheme-utilities parse-command-line))


(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)
  (apply append
    (map (lambda (sx) (process-toplevel-expression sx l-s-lib)) l-sx)))


(define (add-unit sgt-l l-s-unit)
  (let ((l-old (singleton-get-element sgt-l)))
    (if (not (member l-s-unit l-old))
      (singleton-set-element! sgt-l (cons l-s-unit l-old)))))


(define (get-source-code-suffix unit-type)
  (case unit-type
    ((proper-program) ".thp")
    ((script) ".ths")
    ((interface) ".thi")
    ((body) ".thb")
    (else (raise 'invalid-unit-type))))


(define (get-unit-filename l-s-unit s-unit-type)
  (string-append (symbol->string (last l-s-unit))
                 (get-source-code-suffix s-unit-type)))


(define (process-unit l-s-unit l-s-lib s-unit-type sgt-result sgt-interfaces sgt-bodies)
  (if (and (eq? s-unit-type 'interface)
           (member l-s-unit (singleton-get-element sgt-interfaces)))
    #f
    (if (and (eq? s-unit-type 'body)
             (member l-s-unit (singleton-get-element sgt-bodies)))
      #f
      (begin
       (if (eq? s-unit-type 'interface)
         (begin
          (add-unit sgt-result l-s-unit)
          (add-unit sgt-interfaces l-s-unit)))
       (if (eq? s-unit-type 'body)
         (begin
          (add-unit sgt-result l-s-unit)
          (add-unit sgt-bodies l-s-unit)))
       (let* ((str-unit-filename (get-unit-filename l-s-unit s-unit-type))
              (ip (open-input-file str-unit-filename))
              (l-sx-contents (read-file ip)))
         (close-input-port ip)
         (let ((l-submodules
                (process-all-expressions (cddar l-sx-contents) l-s-lib)))
           (for-each
            (lambda (l-s-module)
              (process-unit l-s-module l-s-lib 'interface
                            sgt-result sgt-interfaces sgt-bodies)
              (process-unit l-s-module l-s-lib 'body
                            sgt-result sgt-interfaces sgt-bodies))
            l-submodules)))))))


(define (print-output-deps str-name s-unit-type l-s-unit l-sx-deps module?)
  (display (string-append str-name ".go :"))
  (if (eq? s-unit-type 'script)
    (begin
     (display " \\\n  ")
     (display (string-append (symbol->string (last l-s-unit)) ".tcs")))
    (begin
     (display " \\\n  ")
     (display (string-append (symbol->string (last l-s-unit)) ".tcp"))))
  (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  ")
        (if module?
          (display (string-append "__impl_" (symbol->string (last l-s-dep)) ".go"))
          (display (string-append (symbol->string (last l-s-dep)) ".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 (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)
  (let* ((str-source "")
         (str-lib "")
         (module? #f))
    (let* ((argd1
            (make-hrecord <argument-descriptor> "module" #f
                          (lambda ()
                            (set! module? #t))))
           (handle-proper-args
            (lambda (l-proper-args)
              (if (eqv? (length l-proper-args) 2)
                (begin
                 (set! str-source (car l-proper-args))
                 (set! str-lib (cadr l-proper-args)))
                (begin
                 (display "Missing arguments.\n" (standard-error-port))
                 (exit 1)))))
           (l-args-without-cmd (cdr args)))
      (parse-command-line l-args-without-cmd (list argd1) handle-proper-args)
      (let* ((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-proper-program define-script)))
          (let* ((sgt-result (make-singleton '()))
                 (sgt-interfaces (make-singleton '()))
                 (sgt-bodies (make-singleton '()))
                 (l-s-unit (cadar l-sx))
                 (s-unit-type
                  (if (eq? (caar l-sx) 'define-proper-program)
                    'proper-program
                    'script)))
            (if (and (list? l-s-unit)
                     (not-null? l-s-unit)
                     (for-all symbol? l-s-unit))
              (begin
               (process-unit l-s-unit l-s-lib s-unit-type
                             sgt-result sgt-interfaces sgt-bodies)
               (print-output-deps str-name s-unit-type l-s-unit
                                  (singleton-get-element sgt-result)
                                  module?)
               (exit 0))
              (begin
               (display "Invalid unit name.\n"
                        (standard-error-port))
               (exit 1))))
          (begin
           (display "Unrecognized translation unit syntax.\n"
                    (standard-error-port))
           (exit 1)))))))