Only the eggs were told apart from the modules, so a scheme.scm became the scheme every unit imports, a dependency of every object and of itself, and the build went on to link it.
161 lines
6.2 KiB
Scheme
161 lines
6.2 KiB
Scheme
;;; The compiler's modules and the programs built from them, as make
|
|
;;; reads them: what each one imports, read off its import forms. A
|
|
;;; module is a NAME.scm in the current directory; a program is named
|
|
;;; with its source. The files either includes with include-relative
|
|
;;; count as its own.
|
|
;;;
|
|
;;; csi -s tools/module-deps.scm sexc=main.scm > modules.mk
|
|
;;;
|
|
;;; prints
|
|
;;;
|
|
;;; MODULES = fmt-c-writer infer reader ...
|
|
;;; SOURCES = fmt-c-writer.scm infer.scm ... main.scm
|
|
;;; infer.o: types.o utils.o
|
|
;;; ...
|
|
;;; main.o: sexc.o
|
|
;;; sexc: main.o sexc.o
|
|
;;; fmt-c-writer.scm:
|
|
;;; ...
|
|
;;;
|
|
;;; and a source deleted since is then one make has nothing to do for,
|
|
;;; rather than one it has no rule to make
|
|
|
|
(import scheme
|
|
(chicken base)
|
|
(chicken condition)
|
|
(chicken eval)
|
|
(chicken file)
|
|
(chicken io)
|
|
(chicken pathname)
|
|
(chicken process-context)
|
|
(chicken sort)
|
|
(chicken string))
|
|
|
|
;;; As csc reads them, given -K prefix
|
|
(keyword-style #:prefix)
|
|
|
|
;;; (only sex-macros cat) -> sex-macros
|
|
(define (spec-name spec)
|
|
(if (and (pair? spec) (memq (car spec) '(only except prefix rename)))
|
|
(spec-name (cadr spec))
|
|
spec))
|
|
|
|
;;; dependencies.txt names the eggs, so a stray test.scm is not the
|
|
;;; module the tests import
|
|
(define eggs (call-with-input-file "dependencies.txt" read-list))
|
|
|
|
;;; A scheme.scm is not the module every unit imports. Asked from /,
|
|
;;; since here a NAME.import.scm built from NAME.scm would answer
|
|
(define (chicken-module? name)
|
|
(let ((here (current-directory)))
|
|
(dynamic-wind
|
|
(lambda () (change-directory "/"))
|
|
(lambda () (handle-exceptions exn #f (module-environment name) #t))
|
|
(lambda () (change-directory here)))))
|
|
|
|
(define (module-file name)
|
|
(and (symbol? name)
|
|
(not (memq name eggs))
|
|
(let ((file (string-append (symbol->string name) ".scm")))
|
|
(and (file-exists? file)
|
|
(not (chicken-module? name))
|
|
file))))
|
|
|
|
;;; FILE's forms, or the end of the scan, naming the file that has none
|
|
(define (forms-of file)
|
|
(handle-exceptions exn
|
|
(begin
|
|
(display (string-append "module-deps: " file ": "
|
|
((condition-property-accessor 'exn 'message "") exn)
|
|
"\n")
|
|
(current-error-port))
|
|
(exit 1))
|
|
(call-with-input-file file read-list)))
|
|
|
|
;;; The names FILE imports, and the files it includes; the vendored
|
|
;;; sex-fmt-c imports inside its (module name exports ...) form
|
|
(define (scan file)
|
|
(let loop ((forms (forms-of file)) (names '()) (files '()))
|
|
(if (null? forms)
|
|
(values names files)
|
|
(let ((form (car forms)))
|
|
(cond
|
|
((not (pair? form)) (loop (cdr forms) names files))
|
|
((and (eq? (car form) 'module) (list? form) (>= (length form) 3))
|
|
(loop (append (cdddr form) (cdr forms)) names files))
|
|
((eq? (car form) 'import)
|
|
(loop (cdr forms) (append (map spec-name (cdr form)) names) files))
|
|
((eq? (car form) 'include-relative)
|
|
(let ((included (make-pathname (pathname-directory file) (cadr form))))
|
|
(let-values (((more-names more-files) (scan included)))
|
|
(loop (cdr forms)
|
|
(append more-names names)
|
|
(append (cons included more-files) files)))))
|
|
(else (loop (cdr forms) names files)))))))
|
|
|
|
(define (symbol<? a b)
|
|
(string<? (symbol->string a) (symbol->string b)))
|
|
|
|
;;; The modules among NAMES, once each
|
|
(define (local-modules names)
|
|
(let loop ((names names) (acc '()))
|
|
(cond ((null? names) (sort acc symbol<?))
|
|
((or (memq (car names) acc) (not (module-file (car names))))
|
|
(loop (cdr names) acc))
|
|
(else (loop (cdr names) (cons (car names) acc))))))
|
|
|
|
;;; Each module reached from ROOTS, as (name imported-modules . included-files)
|
|
(define (module-graph roots)
|
|
(let loop ((pending roots) (graph '()))
|
|
(cond ((null? pending) (sort graph (lambda (a b) (symbol<? (car a) (car b)))))
|
|
((assq (car pending) graph) (loop (cdr pending) graph))
|
|
(else
|
|
(let-values (((names files) (scan (module-file (car pending)))))
|
|
(let ((deps (local-modules names)))
|
|
(loop (append (cdr pending) deps)
|
|
(cons (cons (car pending) (cons deps files)) graph))))))))
|
|
|
|
(define (objects names)
|
|
(map (lambda (name) (string-append (symbol->string name) ".o")) names))
|
|
|
|
(define (rule target prerequisites)
|
|
(unless (null? prerequisites)
|
|
(print target ": " (string-intersperse prerequisites " "))))
|
|
|
|
;;; sexc=main.scm, read off main.scm and the files it includes
|
|
(define-record-type program
|
|
(make-program binary object modules sources)
|
|
program?
|
|
(binary program-binary)
|
|
(object program-object)
|
|
(modules program-modules)
|
|
(sources program-sources))
|
|
|
|
(define (read-program spec)
|
|
(let* ((parts (string-split spec "="))
|
|
(source (cadr parts)))
|
|
(let-values (((names files) (scan source)))
|
|
(make-program (car parts) (pathname-replace-extension source "o")
|
|
(local-modules names) (cons source files)))))
|
|
|
|
;;; Only core here: modules.mk is made before `make deps' installs eggs
|
|
(let* ((programs (map read-program (command-line-arguments)))
|
|
(graph (module-graph
|
|
(local-modules (apply append (map program-modules programs))))))
|
|
(define sources
|
|
(append (apply append (map (lambda (node) (cons (module-file (car node)) (cddr node)))
|
|
graph))
|
|
(apply append (map program-sources programs))))
|
|
(print "MODULES = " (string-intersperse (map (o symbol->string car) graph) " "))
|
|
(print "SOURCES = " (string-intersperse sources " "))
|
|
(for-each (lambda (node)
|
|
(rule (car (objects (list (car node))))
|
|
(append (cddr node) (objects (cadr node)))))
|
|
graph)
|
|
(for-each (lambda (p)
|
|
(let ((modules (objects (program-modules p))))
|
|
(rule (program-object p) (append (cdr (program-sources p)) modules))
|
|
(rule (program-binary p) (cons (program-object p) modules))))
|
|
programs)
|
|
(for-each (lambda (source) (print source ":")) sources))
|