Simplify the build: one-file modules, a module graph read off the imports #67
57
Makefile
57
Makefile
@@ -37,20 +37,17 @@ all: sexc
|
||||
|
||||
include eggs.mk
|
||||
|
||||
# MODULES, and a line for each naming the modules it imports, read off
|
||||
# the import forms. Not for clean, which has to work when they cannot
|
||||
# be read
|
||||
# Each program, and the source it is built from
|
||||
PROGRAMS = sexc=main.scm sextest=tools/sextest/sextest.scm sex-tests=tests/run.scm
|
||||
PROGRAM_NAMES = $(foreach p,$(PROGRAMS),$(firstword $(subst =, ,$(p))))
|
||||
PROGRAM_OBJ = $(foreach p,$(PROGRAMS),$(patsubst %.scm,%.o,$(lastword $(subst =, ,$(p)))))
|
||||
|
||||
# MODULES, and a line for each module and program naming what it
|
||||
# imports, read off the import forms. Not for clean, which has to work
|
||||
# when they cannot be read
|
||||
ifneq ($(MAKECMDGOALS),clean)
|
||||
include modules.mk
|
||||
endif
|
||||
OBJ = $(MODULES:%=%.o)
|
||||
|
||||
sexc: $(OBJ) main.scm
|
||||
$(CHICKEN_C) $(CSC_FLAGS) main.scm -o sexc-tmp -link sexc
|
||||
# otherwise csc hangs, probably because it tries to compile to sexc.o first
|
||||
mv sexc-tmp sexc
|
||||
|
||||
#------------------------------------------------------------------
|
||||
|
||||
# The scan is redone whenever a source may have changed what it
|
||||
# imports, or a NAME.scm may have come to be the module an import
|
||||
@@ -59,34 +56,28 @@ sexc: $(OBJ) main.scm
|
||||
# have it start over for good
|
||||
modules.mk: modules.stamp ;
|
||||
|
||||
modules.stamp: $(filter-out %.import.scm,$(wildcard *.scm)) tools/module-deps.scm
|
||||
$(CSI) -s tools/module-deps.scm main.scm > modules.mk.tmp \
|
||||
modules.stamp: $(SOURCES) $(filter-out %.import.scm,$(wildcard *.scm)) \
|
||||
tools/module-deps.scm dependencies.txt Makefile
|
||||
$(CSI) -s tools/module-deps.scm $(PROGRAMS) > modules.mk.tmp \
|
||||
|
alex-eg marked this conversation as resolved
|
||||
|| { rm -f modules.mk.tmp; exit 1; }
|
||||
if cmp -s modules.mk.tmp modules.mk; then rm modules.mk.tmp; \
|
||||
else mv modules.mk.tmp modules.mk; fi
|
||||
touch $@
|
||||
|
||||
#------------------------------------------------------------------
|
||||
|
||||
# A module is one file that declares its own exports, made a module by
|
||||
# -module
|
||||
|
||||
# -module, except the vendored sex-fmt-c, a (module ...) form of its own
|
||||
%.o: %.scm
|
||||
$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) $< -module $* -o $@ -unit $*
|
||||
$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) $< $(if $(filter sex-fmt-c,$*),,-module $*) -o $@ -unit $*
|
||||
|
||||
# Vendored, and a (module ...) form of its own
|
||||
sex-fmt-c.o: sex-fmt-c.scm
|
||||
$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) $< -o $@ -unit sex-fmt-c
|
||||
# A program's object is named after its source: csc names its own after
|
||||
# -o, and sexc.o is the module's
|
||||
$(PROGRAM_OBJ): %.o: %.scm
|
||||
$(CHICKEN_C) $(CSC_FLAGS) -c $< -o $@
|
||||
|
||||
# Both link the modules built here, whose import libraries they find
|
||||
# with the root on the repository path. The tests are built in their
|
||||
# own directory, where their includes would otherwise find the modules'
|
||||
# namesakes
|
||||
WITH_ROOT_REPO = CHICKEN_REPOSITORY_PATH=$(CURDIR):$(CHICKEN_REPOSITORY_PATH)
|
||||
|
||||
sex-tests: $(wildcard tests/*.scm) $(OBJ)
|
||||
cd tests && $(WITH_ROOT_REPO) $(CHICKEN_C) $(CSC_FLAGS) run.scm -o ../$@ -link sexc
|
||||
|
||||
sextest: tools/sextest/sextest.scm reader.o utils.o
|
||||
$(CHICKEN_C) $(CSC_FLAGS) $< -o $@ -link reader,utils
|
||||
$(PROGRAM_NAMES):
|
||||
$(CHICKEN_C) $(CSC_FLAGS) $< -o $@
|
||||
|
||||
# A program directly in tests/sex-programs is a test; lib/ holds what
|
||||
# they import
|
||||
@@ -118,9 +109,9 @@ uninstall:
|
||||
-rmdir $(DESTDIR)$(sexlibdir)
|
||||
|
||||
clean:
|
||||
rm -f *.o modules.mk modules.stamp
|
||||
rm -f *.o $(PROGRAM_OBJ) modules.mk modules.stamp
|
||||
rm -f *.import.scm
|
||||
rm -f *.link
|
||||
rm -f sexc sex-tests sextest
|
||||
rm -f *.link $(PROGRAM_OBJ:.o=.link)
|
||||
rm -f $(PROGRAM_NAMES)
|
||||
|
||||
.PHONY: all check run-tests check-modules check-exit-code install install-strip installdirs uninstall clean
|
||||
|
||||
@@ -1,18 +1,18 @@
|
||||
(import
|
||||
test)
|
||||
|
||||
(include "basic.scm")
|
||||
(include "semen.scm")
|
||||
(include "reader.scm")
|
||||
(include "fmt-c-writer.scm")
|
||||
(include "utils.scm")
|
||||
(include "line-directives.scm")
|
||||
(include "codegen.scm")
|
||||
(include "encapsulation.scm")
|
||||
(include "args.scm")
|
||||
(include "module-path.scm")
|
||||
(include "types.scm")
|
||||
(include "infer.scm")
|
||||
(include-relative "basic.scm")
|
||||
(include-relative "semen.scm")
|
||||
(include-relative "reader.scm")
|
||||
(include-relative "fmt-c-writer.scm")
|
||||
(include-relative "utils.scm")
|
||||
(include-relative "line-directives.scm")
|
||||
(include-relative "codegen.scm")
|
||||
(include-relative "encapsulation.scm")
|
||||
(include-relative "args.scm")
|
||||
(include-relative "module-path.scm")
|
||||
(include-relative "types.scm")
|
||||
(include-relative "infer.scm")
|
||||
|
||||
;;; Should be the last in the test suite
|
||||
(test-exit)
|
||||
|
||||
@@ -1,22 +1,34 @@
|
||||
;;; 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. The files either includes with include-relative
|
||||
;;; 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 ...
|
||||
;;; 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 file)
|
||||
(chicken io)
|
||||
(chicken pathname)
|
||||
(chicken process-context)
|
||||
(chicken sort))
|
||||
(chicken sort)
|
||||
(chicken string))
|
||||
|
||||
;;; As csc reads them, given -K prefix
|
||||
(keyword-style #:prefix)
|
||||
@@ -27,25 +39,16 @@
|
||||
(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 '()))
|
||||
(if (null? forms)
|
||||
acc
|
||||
(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)))))))
|
||||
|
||||
;;; FILE's forms, or the end of the scan, naming the file that has none
|
||||
(define (forms-of file)
|
||||
(handle-exceptions exn
|
||||
@@ -57,36 +60,89 @@
|
||||
(exit 1))
|
||||
(call-with-input-file file read-list)))
|
||||
|
||||
(define (local-imports file)
|
||||
(let loop ((names (imported (forms-of file)))
|
||||
(acc '()))
|
||||
;;; 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))))))
|
||||
|
||||
(define (symbol<? a b)
|
||||
(string<? (symbol->string a) (symbol->string b)))
|
||||
|
||||
;;; 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, 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 ((deps (local-imports (module-file (car pending)))))
|
||||
(let-values (((names files) (scan (module-file (car pending)))))
|
||||
(let ((deps (local-modules names)))
|
||||
(loop (append (cdr pending) deps)
|
||||
(cons (cons (car pending) deps) graph)))))))
|
||||
(cons (cons (car pending) (cons deps files)) 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)))
|
||||
(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))
|
||||
|
||||
Reference in New Issue
Block a user
That is another list of files, that is easy to forget about. I don't know how to fix it, whether to make a variable somewhere on top of the makefile, or make it computable somehow. The fact is - we will forget about it someday.
That we will, but probably CI will save us. Or manual testing.