read the module graph off the import forms

Each module's prerequisite line restated its import form, so adding a
module or an import meant editing the Makefile to match, and a missed
line only showed when an object came out of order.
This commit is contained in:
2026-10-14 23:21:49 +03:00
parent a504fc1410
commit 397766822d
3 changed files with 119 additions and 12 deletions

2
.gitignore vendored
View File

@@ -5,6 +5,8 @@
*.o *.o
*.import.scm *.import.scm
*.link *.link
/modules.mk
/modules.stamp
/sexc /sexc
/sex-tests /sex-tests
/sextest /sextest

View File

@@ -1,4 +1,6 @@
CHICKEN_C ?= csc CHICKEN_C ?= csc
# beside csc, whatever it is called: chicken-csc comes with chicken-csi
CSI ?= $(patsubst %csc,%csi,$(CHICKEN_C))
CSC_FLAGS += -K prefix -static CSC_FLAGS += -K prefix -static
# What and why: # What and why:
# -emit-all-import-libraries: Emit import-libraries for all defined modules. # -emit-all-import-libraries: Emit import-libraries for all defined modules.
@@ -31,14 +33,18 @@ SEX_LIB = $(wildcard lib/*.sex)
MODULE_FLAGS = -emit-all-import-libraries -module-registration -c MODULE_FLAGS = -emit-all-import-libraries -module-registration -c
# Order matters, since module check correctness on compilation
MODULES = utils types infer sex-macros reader sex-modules semen sex-fmt-c fmt-c-writer sexc
OBJ = $(MODULES:%=%.o)
all: sexc all: sexc
include eggs.mk 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
ifneq ($(MAKECMDGOALS),clean)
include modules.mk
endif
OBJ = $(MODULES:%=%.o)
sexc: $(OBJ) main.scm sexc: $(OBJ) main.scm
$(CHICKEN_C) $(CSC_FLAGS) main.scm -o sexc-tmp -link sexc $(CHICKEN_C) $(CSC_FLAGS) main.scm -o sexc-tmp -link sexc
# otherwise csc hangs, probably because it tries to compile to sexc.o first # otherwise csc hangs, probably because it tries to compile to sexc.o first
@@ -46,19 +52,26 @@ sexc: $(OBJ) main.scm
#------------------------------------------------------------------ #------------------------------------------------------------------
# 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
# names; but modules.mk is rewritten only when that changed, since make
# starts over each time it is, and a source dated in the future would
# 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 \
|| { 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 # A module is one file that declares its own exports, made a module by
# -module # -module
%.o: %.scm %.o: %.scm
$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) $< -module $* -o $@ -unit $* $(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) $< -module $* -o $@ -unit $*
infer.o: types.o utils.o
reader.o: utils.o
sex-modules.o: reader.o types.o utils.o
semen.o: infer.o sex-macros.o sex-modules.o types.o utils.o
fmt-c-writer.o: sex-fmt-c.o types.o utils.o
sexc.o: fmt-c-writer.o infer.o reader.o semen.o sex-macros.o sex-modules.o types.o utils.o
# Vendored, and a (module ...) form of its own # Vendored, and a (module ...) form of its own
sex-fmt-c.o: sex-fmt-c.scm sex-fmt-c.o: sex-fmt-c.scm
$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) $< -o $@ -unit sex-fmt-c $(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) $< -o $@ -unit sex-fmt-c
@@ -128,7 +141,7 @@ uninstall:
-rmdir $(DESTDIR)$(sexlibdir) -rmdir $(DESTDIR)$(sexlibdir)
clean: clean:
rm -f $(OBJ) main.o rm -f *.o modules.mk modules.stamp
rm -f *.import.scm rm -f *.import.scm
rm -f *.link rm -f *.link
rm -f sexc sex-tests sextest rm -f sexc sex-tests sextest

92
tools/module-deps.scm Normal file
View File

@@ -0,0 +1,92 @@
;;; 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.
;;;
;;; csi -s tools/module-deps.scm main.scm > modules.mk
;;;
;;; prints
;;;
;;; MODULES = fmt-c-writer infer reader ...
;;; infer.o: types.o utils.o
;;; ...
(import scheme
(chicken base)
(chicken condition)
(chicken file)
(chicken io)
(chicken process-context)
(chicken sort))
;;; 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))
(define (module-file name)
(and (symbol? name)
(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
(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)))
(define (local-imports file)
(let loop ((names (imported (forms-of file)))
(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 '()))
(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)))))
(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))