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:
2026-10-15 15:18:17 +03:00
parent 0024ea4b8d
commit bdc45e38e4
3 changed files with 121 additions and 80 deletions

View File

@@ -36,47 +36,38 @@ all: sexc
include eggs.mk include eggs.mk
# MODULES, and a line for each naming the modules it imports, read off # Each program, and the source it is built from
# the import forms 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 include modules.mk
OBJ = $(MODULES:%=%.o) OBJ = $(MODULES:%=%.o)
sexc: $(OBJ) main.scm modules.mk: $(SOURCES) tools/module-deps.scm dependencies.txt
$(CHICKEN_C) $(CSC_FLAGS) main.scm -o sexc-tmp -link sexc $(CSI) -s tools/module-deps.scm $(PROGRAMS) > $@.tmp
# otherwise csc hangs, probably because it tries to compile to sexc.o first mv $@.tmp $@
mv sexc-tmp sexc
#------------------------------------------------------------------ #------------------------------------------------------------------
modules.mk: main.scm $(MODULES:%=%.scm) tools/module-deps.scm # Everything is linked with the modules among its prerequisites
$(CSI) -s tools/module-deps.scm main.scm > $@.tmp LINKS = $(addprefix -link ,$(basename $(filter-out $<,$(filter %.o,$^))))
mv $@.tmp $@
# 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. It is linked with the modules among its prerequisites. # -module, except the vendored sex-fmt-c, a (module ...) form of its own
comma := , MODULE = -module $*
empty := sex-fmt-c.o: MODULE =
space := $(empty) $(empty)
LINKS = $(subst $(space),$(comma),$(basename $(filter %.o,$^)))
%.o: %.scm %.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 # A program's object is named after its source: csc names its own after
sex-fmt-c.o: sex-fmt-c.scm # -o, and sexc.o is the module's
$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) $< -o $@ -unit sex-fmt-c $(PROGRAM_OBJ): %.o: %.scm
$(CHICKEN_C) $(CSC_FLAGS) -c $< -o $@
# Both link the modules built here, whose import libraries they find $(PROGRAM_NAMES):
# with the root on the repository path. The tests are built in their $(CHICKEN_C) $(CSC_FLAGS) $< -o $@ $(LINKS)
# 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
# A program directly in tests/sex-programs is a test; lib/ holds what # A program directly in tests/sex-programs is a test; lib/ holds what
# they import # they import
@@ -104,9 +95,9 @@ uninstall:
-rmdir $(DESTDIR)$(sexlibdir) -rmdir $(DESTDIR)$(sexlibdir)
clean: clean:
rm -f $(OBJ) main.o modules.mk rm -f $(OBJ) $(PROGRAM_OBJ) modules.mk
rm -f *.import.scm rm -f *.import.scm
rm -f *.link rm -f *.link $(PROGRAM_OBJ:.o=.link)
rm -f sexc sex-tests sextest rm -f sexc sex-tests sextest
.PHONY: all check run-tests install install-strip installdirs uninstall clean .PHONY: all check run-tests install install-strip installdirs uninstall clean

View File

@@ -1,18 +1,18 @@
(import (import
test) test)
(include "basic.scm") (include-relative "basic.scm")
(include "semen.scm") (include-relative "semen.scm")
(include "reader.scm") (include-relative "reader.scm")
(include "fmt-c-writer.scm") (include-relative "fmt-c-writer.scm")
(include "utils.scm") (include-relative "utils.scm")
(include "line-directives.scm") (include-relative "line-directives.scm")
(include "codegen.scm") (include-relative "codegen.scm")
(include "encapsulation.scm") (include-relative "encapsulation.scm")
(include "args.scm") (include-relative "args.scm")
(include "module-path.scm") (include-relative "module-path.scm")
(include "types.scm") (include-relative "types.scm")
(include "infer.scm") (include-relative "infer.scm")
;;; Should be the last in the test suite ;;; Should be the last in the test suite
(test-exit) (test-exit)

View File

@@ -1,21 +1,29 @@
;;; The compiler's modules and which of them each one imports, as make ;;; The compiler's modules and the programs built from them, as make
;;; reads them: a module is a NAME.scm in the current directory that ;;; reads them: what each one imports, read off its import forms. A
;;; the program reaches through import forms. ;;; 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 ;;; prints
;;; ;;;
;;; MODULES = fmt-c-writer infer reader ... ;;; 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 ;;; infer.o: types.o utils.o
;;; ... ;;; ...
;;; main.o: sexc.o
;;; sexc: main.o sexc.o
(import scheme (import scheme
(chicken base) (chicken base)
(chicken file) (chicken file)
(chicken io) (chicken io)
(chicken pathname)
(chicken process-context) (chicken process-context)
(chicken sort)) (chicken sort)
(chicken string))
;;; As csc reads them, given -K prefix ;;; As csc reads them, given -K prefix
(keyword-style #:prefix) (keyword-style #:prefix)
@@ -26,55 +34,97 @@
(spec-name (cadr spec)) (spec-name (cadr spec))
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) (define (module-file name)
(and (symbol? name) (and (symbol? name)
(not (memq name eggs))
(let ((file (string-append (symbol->string name) ".scm"))) (let ((file (string-append (symbol->string name) ".scm")))
(and (file-exists? file) file)))) (and (file-exists? file) file))))
;;; The import forms at toplevel, and inside a (module NAME EXPORTS ...) ;;; The names FILE imports, and the files it includes
(define (imported forms) (define (scan file)
(let walk ((forms forms) (acc '())) (let loop ((forms (call-with-input-file file read-list)) (names '()) (files '()))
(if (null? forms) (if (null? forms)
acc (values names files)
(let ((form (car forms))) (let ((form (car forms)))
(walk (cdr forms) (cond
(cond ((not (pair? form)) acc) ((not (pair? form)) (loop (cdr forms) names files))
((eq? (car form) 'import) ((eq? (car form) 'import)
(append (map spec-name (cdr form)) acc)) (loop (cdr forms) (append (map spec-name (cdr form)) names) files))
((eq? (car form) 'module) ((eq? (car form) 'include-relative)
(walk (cdddr form) acc)) (let ((included (make-pathname (pathname-directory file) (cadr form))))
(else acc))))))) (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) (define (symbol<? a b)
(let loop ((names (imported (call-with-input-file file read-list))) (string<? (symbol->string a) (symbol->string b)))
(acc '()))
;;; The modules among NAMES, once each
(define (local-modules names)
(let loop ((names names) (acc '()))
(cond ((null? names) (sort acc symbol<?)) (cond ((null? names) (sort acc symbol<?))
((or (memq (car names) acc) (not (module-file (car names)))) ((or (memq (car names) acc) (not (module-file (car names))))
(loop (cdr names) acc)) (loop (cdr names) acc))
(else (loop (cdr names) (cons (car names) acc)))))) (else (loop (cdr names) (cons (car names) acc))))))
(define (symbol<? a b) (define (imported-modules file)
(string<? (symbol->string a) (symbol->string b))) (let-values (((names files) (scan file)))
(local-modules names)))
;;; Each module reached from FILE, with the modules it imports ;;; Each module reached from ROOTS, with the modules it imports
(define (module-graph file) (define (module-graph roots)
(let loop ((pending (local-imports file)) (graph '())) (let loop ((pending roots) (graph '()))
(cond ((null? pending) (sort graph (lambda (a b) (symbol<? (car a) (car b))))) (cond ((null? pending) (sort graph (lambda (a b) (symbol<? (car a) (car b)))))
((assq (car pending) graph) (loop (cdr pending) graph)) ((assq (car pending) graph) (loop (cdr pending) graph))
(else (else
(let ((deps (local-imports (module-file (car pending))))) (let ((deps (imported-modules (module-file (car pending)))))
(loop (append (cdr pending) deps) (loop (append (cdr pending) deps)
(cons (cons (car pending) deps) graph))))))) (cons (cons (car pending) deps) graph)))))))
(let ((graph (module-graph (car (command-line-arguments))))) (define (objects names)
(display "MODULES =") (map (lambda (name) (string-append (symbol->string name) ".o")) names))
(for-each (lambda (node) (display " ") (display (car node))) graph)
(newline) (define (rule target prerequisites)
(for-each (lambda (node) (unless (null? prerequisites)
(unless (null? (cdr node)) (print target ": " (string-intersperse prerequisites " "))))
(display (car node))
(display ".o:") ;;; sexc=main.scm, read off main.scm and the files it includes
(for-each (lambda (dep) (display " ") (display dep) (display ".o")) (define-record-type program
(cdr node)) (make-program name object modules sources)
(newline))) program?
graph)) (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))