From ebe7969553d4ec5f75190e773371386aa08c1ee2 Mon Sep 17 00:00:00 2001 From: alex-eg Date: Thu, 15 Oct 2026 15:18:17 +0300 Subject: [PATCH] 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. --- Makefile | 53 ++++++++---------- tests/run.scm | 24 ++++---- tools/module-deps.scm | 124 +++++++++++++++++++++++++++++------------- 3 files changed, 121 insertions(+), 80 deletions(-) diff --git a/Makefile b/Makefile index 3ec402e..48fb44b 100644 --- a/Makefile +++ b/Makefile @@ -36,47 +36,38 @@ all: sexc include eggs.mk -# MODULES, and a line for each naming the modules it imports, read off -# the import forms +# Each program, and the source it is built from +PROGRAMS = sexc=main.scm sextest=tools/sextest/sextest.scm sex-tests=tests/run.scm + +# MODULES, the programs' names and objects, and a line for each naming +# what 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 - mv sexc-tmp sexc +modules.mk: $(SOURCES) tools/module-deps.scm dependencies.txt + $(CSI) -s tools/module-deps.scm $(PROGRAMS) > $@.tmp + mv $@.tmp $@ #------------------------------------------------------------------ -modules.mk: main.scm $(MODULES:%=%.scm) tools/module-deps.scm - $(CSI) -s tools/module-deps.scm main.scm > $@.tmp - mv $@.tmp $@ +# Everything is linked with the modules among its prerequisites +LINKS = $(addprefix -link ,$(basename $(filter-out $<,$(filter %.o,$^)))) # 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 := , -empty := -space := $(empty) $(empty) -LINKS = $(subst $(space),$(comma),$(basename $(filter %.o,$^))) +# -module, except the vendored sex-fmt-c, a (module ...) form of its own +MODULE = -module $* +sex-fmt-c.o: MODULE = %.o: %.scm - $(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) $< -module $* -o $@ -unit $* $(if $(LINKS),-link $(LINKS)) + $(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) $< $(MODULE) -o $@ -unit $* $(LINKS) -# 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 $@ $(LINKS) # A program directly in tests/sex-programs is a test; lib/ holds what # they import @@ -104,9 +95,9 @@ uninstall: -rmdir $(DESTDIR)$(sexlibdir) clean: - rm -f $(OBJ) main.o modules.mk + rm -f $(OBJ) $(PROGRAM_OBJ) modules.mk rm -f *.import.scm - rm -f *.link + rm -f *.link $(PROGRAM_OBJ:.o=.link) rm -f sexc sex-tests sextest .PHONY: all check run-tests install install-strip installdirs uninstall clean diff --git a/tests/run.scm b/tests/run.scm index 8464382..0065c04 100644 --- a/tests/run.scm +++ b/tests/run.scm @@ -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) diff --git a/tools/module-deps.scm b/tools/module-deps.scm index 019d54c..74e21b1 100644 --- a/tools/module-deps.scm +++ b/tools/module-deps.scm @@ -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 (symbolstring a) (symbol->string b))) + +;;; The modules among NAMES, once each +(define (local-modules names) + (let loop ((names names) (acc '())) (cond ((null? names) (sort acc symbolstring 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) (symbolstring 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))