Compare commits

..

7 Commits

Author SHA1 Message Date
67749c8db0 build the programs the way the modules are built
All checks were successful
Sex CI / build-linux (pull_request) Successful in 5m17s
Each program had its own recipe: a hand-written link list, a cd into
tests/ to keep its includes off the modules' namesakes, and an output
renamed so as not to overwrite sexc.o. Read off their imports and built
from an object named after their source, they share one rule.
2026-10-19 21:49:43 +03:00
faa9036dcc take every program in tests/sex-programs as a test
A program left out of the hand-kept list was never run, and nothing said
so; the one module the tests import moves to lib/ instead.
2026-10-19 21:49:32 +03:00
c33bfd704d check modules and exit codes with the test egg
Written as shell inside make, a check said only that it failed and left
the reason to rerunning it by hand; now each says what it expected.
2026-10-19 21:49:24 +03:00
397766822d 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.
2026-10-19 21:48:51 +03:00
a504fc1410 build the unit tests and sextest from the root
Each subdirectory's Makefile repeated the compiler's module list just to
link one program, and its result was copied back to the root anyway.
2026-10-19 21:48:16 +03:00
c65721a070 keep the local egg repository in a file of its own
The Makefile was mostly about where the eggs live; what sexc is built
from is easier to find without it.
2026-10-19 21:48:04 +03:00
dcb65c5504 make each compiler module one file, built once
A module was a body plus a (module ...) wrapper naming its exports, and
the wrapper came in up to three copies: the root's, a test one that
exported everything, and sextest's, each recompiling the same body.

csc -module makes a file a module by itself, with export forms in its
body, so the wrappers are gone. tests/ and tools/sextest/ link what the
root built, finding it on the repository path; the tests see what a
module exports, and the names only they need are exported as such.
2026-10-19 21:48:04 +03:00
5 changed files with 109 additions and 66 deletions

9
.gitignore vendored
View File

@@ -5,10 +5,11 @@
*.o *.o
*.import.scm *.import.scm
*.link *.link
modules.mk /modules.mk
sexc /modules.stamp
sex-tests /sexc
sextest /sex-tests
/sextest
# Scrapped design docs, kept for reference # Scrapped design docs, kept for reference
/attic /attic

View File

@@ -1,5 +1,6 @@
CHICKEN_C ?= csc CHICKEN_C ?= csc
CSI ?= csi # 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.
@@ -38,28 +39,37 @@ include eggs.mk
# Each program, and the source it is built from # Each program, and the source it is built from
PROGRAMS = sexc=main.scm sextest=tools/sextest/sextest.scm sex-tests=tests/run.scm 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, the programs' names and objects, and a line for each naming # MODULES, and a line for each module and program naming what it
# what it imports, read off the import forms # imports, read off the import forms. Not for clean, which has to work
# when they cannot be read
ifneq ($(MAKECMDGOALS),clean)
include modules.mk include modules.mk
OBJ = $(MODULES:%=%.o) endif
modules.mk: $(SOURCES) tools/module-deps.scm dependencies.txt # The scan is redone whenever a source may have changed what it
$(CSI) -s tools/module-deps.scm $(PROGRAMS) > $@.tmp # imports, or a NAME.scm may have come to be the module an import
mv $@.tmp $@ # 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: $(SOURCES) $(filter-out %.import.scm,$(wildcard *.scm)) \
tools/module-deps.scm dependencies.txt Makefile
$(CSI) -s tools/module-deps.scm $(PROGRAMS) > 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 $@
#------------------------------------------------------------------ #------------------------------------------------------------------
# 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 # A module is one file that declares its own exports, made a module by
# -module, except the vendored sex-fmt-c, a (module ...) form of its own # -module, except the vendored sex-fmt-c, a (module ...) form of its own
MODULE = -module $*
sex-fmt-c.o: MODULE =
%.o: %.scm %.o: %.scm
$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) $< $(MODULE) -o $@ -unit $* $(LINKS) $(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) $< $(if $(filter sex-fmt-c,$*),,-module $*) -o $@ -unit $*
# A program's object is named after its source: csc names its own after # A program's object is named after its source: csc names its own after
# -o, and sexc.o is the module's # -o, and sexc.o is the module's
@@ -67,7 +77,7 @@ $(PROGRAM_OBJ): %.o: %.scm
$(CHICKEN_C) $(CSC_FLAGS) -c $< -o $@ $(CHICKEN_C) $(CSC_FLAGS) -c $< -o $@
$(PROGRAM_NAMES): $(PROGRAM_NAMES):
$(CHICKEN_C) $(CSC_FLAGS) $< -o $@ $(LINKS) $(CHICKEN_C) $(CSC_FLAGS) $< -o $@
# 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
@@ -76,8 +86,12 @@ SEX_TEST_PROGRAMS = $(wildcard tests/sex-programs/*.sex)
check run-tests: sexc sex-tests sextest check run-tests: sexc sex-tests sextest
./sex-tests ./sex-tests
./sextest --sexc=./sexc $(SEX_TEST_PROGRAMS) ./sextest --sexc=./sexc $(SEX_TEST_PROGRAMS)
cd tests/modules && SEXC=$(CURDIR)/sexc $(CSI) -s check.scm @$(MAKE) --no-print-directory check-modules
cd tests/exit-code && SEXC=$(CURDIR)/sexc $(CSI) -s check.scm @$(MAKE) --no-print-directory check-exit-code
# The end-to-end checks, each on its own: run with the eggs on the path
check-modules check-exit-code: check-%: sexc
cd tests/$* && SEXC='$(CURDIR)/sexc' $(CSI) -s check.scm
install: all installdirs install: all installdirs
$(INSTALL_PROGRAM) sexc $(DESTDIR)$(bindir)/sexc $(INSTALL_PROGRAM) sexc $(DESTDIR)$(bindir)/sexc
@@ -95,9 +109,9 @@ uninstall:
-rmdir $(DESTDIR)$(sexlibdir) -rmdir $(DESTDIR)$(sexlibdir)
clean: clean:
rm -f $(OBJ) $(PROGRAM_OBJ) modules.mk rm -f *.o $(PROGRAM_OBJ) modules.mk modules.stamp
rm -f *.import.scm rm -f *.import.scm
rm -f *.link $(PROGRAM_OBJ:.o=.link) rm -f *.link $(PROGRAM_OBJ:.o=.link)
rm -f sexc sex-tests sextest rm -f $(PROGRAM_NAMES)
.PHONY: all check run-tests install install-strip installdirs uninstall clean .PHONY: all check run-tests check-modules check-exit-code install install-strip installdirs uninstall clean

View File

@@ -10,20 +10,22 @@
;;; Everything is built inside a scratch directory, so nothing is left ;;; Everything is built inside a scratch directory, so nothing is left
;;; here to clean up. ;;; here to clean up.
;;; ;;;
;;; csi -s check.scm ;;; make check-exit-code
(import scheme (import scheme
(chicken base) (chicken base)
(chicken file) (chicken file)
(chicken io)
(chicken pathname) (chicken pathname)
(chicken process) (chicken process)
(chicken process-context) (chicken process-context)
(chicken string)
test) test)
;;; A path stays good past the change of directory below; a bare name
;;; is looked up on PATH
(define sexc (define sexc
(let ((path (or (get-environment-variable "SEXC") "../../sexc"))) (let ((path (or (get-environment-variable "SEXC") "../../sexc")))
(if (absolute-pathname? path) (if (or (absolute-pathname? path) (not (substring-index "/" path)))
path path
(make-pathname (current-directory) path)))) (make-pathname (current-directory) path))))
@@ -33,12 +35,13 @@
;;; Whether sexc exits 0 when run with ARGS, saying nothing of it ;;; Whether sexc exits 0 when run with ARGS, saying nothing of it
(define (sexc-succeeds? . args) (define (sexc-succeeds? . args)
(let ((proc (process* sexc args))) (zero? (system (string-append (string-intersperse (map qs (cons sexc args)))
(read-string #f (process-output-port proc)) " >/dev/null 2>&1"))))
(read-string #f (process-error-port proc))
(process-wait proc) ;;; Every check below expects a failure, which a sexc that cannot run
(and (process-returned-normally? proc) ;;; gives too
(zero? (process-exit-status proc))))) (test "sexc compiles a program" #t
(sexc-succeeds? "hello.sex" "-o" out))
(test "a failed emission leaves no temporary .c" '() (test "a failed emission leaves no temporary .c" '()
(begin (begin

View File

@@ -28,13 +28,14 @@
;;; and ledger, which only greet does but which decides what the app is ;;; and ledger, which only greet does but which decides what the app is
;;; told of it. ;;; told of it.
;;; ;;;
;;; csi -s check.scm ;;; make check-modules
(import scheme (import scheme
(chicken base) (chicken base)
(chicken file) (chicken file)
(chicken io) (chicken io)
(chicken irregex) (chicken irregex)
(chicken pathname)
(chicken process) (chicken process)
(chicken process-context) (chicken process-context)
srfi-1 srfi-1
@@ -42,6 +43,11 @@
(define sexc (or (get-environment-variable "SEXC") "../../sexc")) (define sexc (or (get-environment-variable "SEXC") "../../sexc"))
;;; Where the objects and the app go: not beside the sources, where what
;;; an earlier run left would pass for what this one did not build
(define scratch (create-temporary-directory))
(define (built name) (make-pathname scratch name))
;;; The lines PROGRAM prints when run with ARGS, or #f when it fails ;;; The lines PROGRAM prints when run with ARGS, or #f when it fails
(define (output-of program . args) (define (output-of program . args)
(let* ((proc (process program args)) (let* ((proc (process program args))
@@ -56,14 +62,15 @@
(take (or (output-of sexc "--public-interface" "greet.sex") '()) 3)) (take (or (output-of sexc "--public-interface" "greet.sex") '()) 3))
(test-assert "the app links with both modules" (test-assert "the app links with both modules"
(and (output-of sexc "ledger.sex" "-c" "-o" "ledger.o") (and (output-of sexc "ledger.sex" "-c" "-o" (built "ledger.o"))
(output-of sexc "greet.sex" "-c" "-o" "greet.o") (output-of sexc "greet.sex" "-c" "-o" (built "greet.o"))
(output-of sexc "greet-app.sex" "-o" "greet-app" "--depfile=greet-app.d" (output-of sexc "greet-app.sex" "-o" (built "greet-app")
"--" "greet.o" "ledger.o"))) (string-append "--depfile=" (built "greet-app.d"))
"--" (built "greet.o") (built "ledger.o"))))
(test-assert "the dependency rule names both modules" (test-assert "the dependency rule names both modules"
(irregex-match "greet-app: greet-app\\.sex .*/greet\\.sex .*/ledger\\.sex" (irregex-match ".*/greet-app: greet-app\\.sex .*/greet\\.sex .*/ledger\\.sex"
(call-with-input-file "greet-app.d" read-line))) (call-with-input-file (built "greet-app.d") read-line)))
(test "the app runs through both" (test "the app runs through both"
'("hello, world" '("hello, world"
@@ -79,8 +86,8 @@
"board 42" "board 42"
"ledger 2" "ledger 2"
"colour 123") "colour 123")
(output-of "./greet-app")) (output-of (built "greet-app")))
(for-each delete-file* '("greet.o" "ledger.o" "greet-app" "greet-app.d")) (delete-directory scratch #t)
(test-exit) (test-exit)

View File

@@ -1,23 +1,28 @@
;;; The compiler's modules and the programs built from them, as make ;;; The compiler's modules and the programs built from them, as make
;;; reads them: what each one imports, read off its import forms. A ;;; 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 ;;; module is a NAME.scm in the current directory; a program is named
;;; with its source, whose include-relative files count as its own. ;;; with its source. The files either includes with include-relative
;;; count as its own.
;;; ;;;
;;; csi -s tools/module-deps.scm sexc=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 ;;; 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 ;;; main.o: sexc.o
;;; sexc: 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 (import scheme
(chicken base) (chicken base)
(chicken condition)
(chicken file) (chicken file)
(chicken io) (chicken io)
(chicken pathname) (chicken pathname)
@@ -44,14 +49,28 @@
(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 names FILE imports, and the files it includes ;;; 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)))
;;; The names FILE imports, and the files it includes; the vendored
;;; sex-fmt-c imports inside its (module name exports ...) form
(define (scan file) (define (scan file)
(let loop ((forms (call-with-input-file file read-list)) (names '()) (files '())) (let loop ((forms (forms-of file)) (names '()) (files '()))
(if (null? forms) (if (null? forms)
(values names files) (values names files)
(let ((form (car forms))) (let ((form (car forms)))
(cond (cond
((not (pair? form)) (loop (cdr forms) names files)) ((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) ((eq? (car form) 'import)
(loop (cdr forms) (append (map spec-name (cdr form)) names) files)) (loop (cdr forms) (append (map spec-name (cdr form)) names) files))
((eq? (car form) 'include-relative) ((eq? (car form) 'include-relative)
@@ -73,19 +92,16 @@
(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 (imported-modules file) ;;; Each module reached from ROOTS, as (name imported-modules . included-files)
(let-values (((names files) (scan file)))
(local-modules names)))
;;; Each module reached from ROOTS, with the modules it imports
(define (module-graph roots) (define (module-graph roots)
(let loop ((pending roots) (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 (imported-modules (module-file (car pending))))) (let-values (((names files) (scan (module-file (car pending)))))
(let ((deps (local-modules names)))
(loop (append (cdr pending) deps) (loop (append (cdr pending) deps)
(cons (cons (car pending) deps) graph))))))) (cons (cons (car pending) (cons deps files)) graph))))))))
(define (objects names) (define (objects names)
(map (lambda (name) (string-append (symbol->string name) ".o")) names)) (map (lambda (name) (string-append (symbol->string name) ".o")) names))
@@ -96,9 +112,9 @@
;;; sexc=main.scm, read off main.scm and the files it includes ;;; sexc=main.scm, read off main.scm and the files it includes
(define-record-type program (define-record-type program
(make-program name object modules sources) (make-program binary object modules sources)
program? program?
(name program-name) (binary program-binary)
(object program-object) (object program-object)
(modules program-modules) (modules program-modules)
(sources program-sources)) (sources program-sources))
@@ -114,17 +130,19 @@
(let* ((programs (map read-program (command-line-arguments))) (let* ((programs (map read-program (command-line-arguments)))
(graph (module-graph (graph (module-graph
(local-modules (apply append (map program-modules programs)))))) (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 "MODULES = " (string-intersperse (map (o symbol->string car) graph) " "))
(print "PROGRAM_NAMES = " (string-intersperse (map program-name programs) " ")) (print "SOURCES = " (string-intersperse sources " "))
(print "PROGRAM_OBJ = " (string-intersperse (map program-object programs) " ")) (for-each (lambda (node)
(print "SOURCES = " (rule (car (objects (list (car node))))
(string-intersperse (append (map (o module-file car) graph) (append (cddr node) (objects (cadr node)))))
(apply append (map program-sources programs)))
" "))
(for-each (lambda (node) (rule (car (objects (list (car node)))) (objects (cdr node))))
graph) graph)
(for-each (lambda (p) (for-each (lambda (p)
(let ((modules (objects (program-modules p)))) (let ((modules (objects (program-modules p))))
(rule (program-object p) (append (cdr (program-sources p)) modules)) (rule (program-object p) (append (cdr (program-sources p)) modules))
(rule (program-name p) (cons (program-object p) modules)))) (rule (program-binary p) (cons (program-object p) modules))))
programs)) programs)
(for-each (lambda (source) (print source ":")) sources))