build the programs the way the modules are built
Each program had its own recipe: a hand-written link list, a cd into tests/ so its includes would not find the modules' namesakes, and a renamed output so csc would not overwrite the module sexc.o. Read off their imports like the modules', and compiled to an object named after their source, they share one rule; include-relative ends the cd.
This commit is contained in:
@@ -1,21 +1,29 @@
|
||||
;;; The compiler's modules and which of them each one imports, as make
|
||||
;;; reads them: a module is a NAME.scm in the current directory that
|
||||
;;; the program reaches through import forms.
|
||||
;;; 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, whose include-relative files count as its own.
|
||||
;;;
|
||||
;;; csi -s tools/module-deps.scm main.scm > modules.mk
|
||||
;;; csi -s tools/module-deps.scm sexc=main.scm > modules.mk
|
||||
;;;
|
||||
;;; prints
|
||||
;;;
|
||||
;;; MODULES = fmt-c-writer infer reader ...
|
||||
;;; PROGRAM_NAMES = sexc
|
||||
;;; PROGRAM_OBJ = main.o
|
||||
;;; SOURCES = fmt-c-writer.scm infer.scm ... main.scm
|
||||
;;; infer.o: types.o utils.o
|
||||
;;; ...
|
||||
;;; main.o: sexc.o
|
||||
;;; sexc: main.o sexc.o
|
||||
|
||||
(import scheme
|
||||
(chicken base)
|
||||
(chicken file)
|
||||
(chicken io)
|
||||
(chicken pathname)
|
||||
(chicken process-context)
|
||||
(chicken sort))
|
||||
(chicken sort)
|
||||
(chicken string))
|
||||
|
||||
;;; As csc reads them, given -K prefix
|
||||
(keyword-style #:prefix)
|
||||
@@ -26,55 +34,97 @@
|
||||
(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))
|
||||
|
||||
(define (module-file name)
|
||||
(and (symbol? name)
|
||||
(not (memq name eggs))
|
||||
(let ((file (string-append (symbol->string name) ".scm")))
|
||||
(and (file-exists? file) file))))
|
||||
|
||||
;;; The import forms at toplevel, and inside a (module NAME EXPORTS ...)
|
||||
(define (imported forms)
|
||||
(let walk ((forms forms) (acc '()))
|
||||
;;; The names FILE imports, and the files it includes
|
||||
(define (scan file)
|
||||
(let loop ((forms (call-with-input-file file read-list)) (names '()) (files '()))
|
||||
(if (null? forms)
|
||||
acc
|
||||
(values names files)
|
||||
(let ((form (car forms)))
|
||||
(walk (cdr forms)
|
||||
(cond ((not (pair? form)) acc)
|
||||
((eq? (car form) 'import)
|
||||
(append (map spec-name (cdr form)) acc))
|
||||
((eq? (car form) 'module)
|
||||
(walk (cdddr form) acc))
|
||||
(else acc)))))))
|
||||
(cond
|
||||
((not (pair? form)) (loop (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 (local-imports file)
|
||||
(let loop ((names (imported (call-with-input-file file read-list)))
|
||||
(acc '()))
|
||||
(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))))))
|
||||
|
||||
(define (symbol<? a b)
|
||||
(string<? (symbol->string a) (symbol->string b)))
|
||||
(define (imported-modules file)
|
||||
(let-values (((names files) (scan file)))
|
||||
(local-modules names)))
|
||||
|
||||
;;; Each module reached from FILE, with the modules it imports
|
||||
(define (module-graph file)
|
||||
(let loop ((pending (local-imports file)) (graph '()))
|
||||
;;; Each module reached from ROOTS, with the modules it imports
|
||||
(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 ((deps (local-imports (module-file (car pending)))))
|
||||
(let ((deps (imported-modules (module-file (car pending)))))
|
||||
(loop (append (cdr pending) deps)
|
||||
(cons (cons (car pending) deps) graph)))))))
|
||||
|
||||
(let ((graph (module-graph (car (command-line-arguments)))))
|
||||
(display "MODULES =")
|
||||
(for-each (lambda (node) (display " ") (display (car node))) graph)
|
||||
(newline)
|
||||
(for-each (lambda (node)
|
||||
(unless (null? (cdr node))
|
||||
(display (car node))
|
||||
(display ".o:")
|
||||
(for-each (lambda (dep) (display " ") (display dep) (display ".o"))
|
||||
(cdr node))
|
||||
(newline)))
|
||||
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 name object modules sources)
|
||||
program?
|
||||
(name program-name)
|
||||
(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))))))
|
||||
(print "MODULES = " (string-intersperse (map (o symbol->string car) graph) " "))
|
||||
(print "PROGRAM_NAMES = " (string-intersperse (map program-name programs) " "))
|
||||
(print "PROGRAM_OBJ = " (string-intersperse (map program-object programs) " "))
|
||||
(print "SOURCES = "
|
||||
(string-intersperse (append (map (o module-file car) graph)
|
||||
(apply append (map program-sources programs)))
|
||||
" "))
|
||||
(for-each (lambda (node) (rule (car (objects (list (car node)))) (objects (cdr node))))
|
||||
graph)
|
||||
(for-each (lambda (p)
|
||||
(let ((modules (objects (program-modules p))))
|
||||
(rule (program-object p) (append (cdr (program-sources p)) modules))
|
||||
(rule (program-name p) (cons (program-object p) modules))))
|
||||
programs))
|
||||
|
||||
Reference in New Issue
Block a user