From 4b5f3ffd6b4f5bd7fb2b0c520ebedf17aa3fb3f1 Mon Sep 17 00:00:00 2001 From: alex-eg Date: Wed, 14 Oct 2026 23:21:49 +0300 Subject: [PATCH] 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. --- .gitignore | 1 + Makefile | 23 ++++++------- tools/module-deps.scm | 80 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 92 insertions(+), 12 deletions(-) create mode 100644 tools/module-deps.scm diff --git a/.gitignore b/.gitignore index 24fcac1..26bac94 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,7 @@ *.o *.import.scm *.link +modules.mk sexc sex-tests sextest diff --git a/Makefile b/Makefile index 69ba66b..f5982b4 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,5 @@ CHICKEN_C ?= csc +CSI ?= csi CSC_FLAGS += -K prefix -static # What and why: # -emit-all-import-libraries: Emit import-libraries for all defined modules. @@ -31,14 +32,15 @@ SEX_LIB = $(wildcard lib/*.sex) 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 include eggs.mk +# MODULES, and a line for each naming the modules it imports, read off +# the import forms +include modules.mk +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 @@ -46,6 +48,10 @@ sexc: $(OBJ) main.scm #------------------------------------------------------------------ +modules.mk: main.scm $(MODULES:%=%.scm) tools/module-deps.scm + $(CSI) -s tools/module-deps.scm main.scm > $@.tmp + mv $@.tmp $@ + # A module is one file that declares its own exports, made a module by # -module. It is linked with the modules among its prerequisites. comma := , @@ -56,13 +62,6 @@ LINKS = $(subst $(space),$(comma),$(basename $(filter %.o,$^))) %.o: %.scm $(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) $< -module $* -o $@ -unit $* $(if $(LINKS),-link $(LINKS)) -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 sex-fmt-c.o: sex-fmt-c.scm $(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) $< -o $@ -unit sex-fmt-c @@ -132,7 +131,7 @@ uninstall: -rmdir $(DESTDIR)$(sexlibdir) clean: - rm -f $(OBJ) main.o + rm -f $(OBJ) main.o modules.mk rm -f *.import.scm rm -f *.link rm -f sexc sex-tests sextest diff --git a/tools/module-deps.scm b/tools/module-deps.scm new file mode 100644 index 0000000..019d54c --- /dev/null +++ b/tools/module-deps.scm @@ -0,0 +1,80 @@ +;;; 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 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))))))) + +(define (local-imports file) + (let loop ((names (imported (call-with-input-file file read-list))) + (acc '())) + (cond ((null? names) (sort acc symbolstring 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