write a make rule for the modules a unit reads

An import pastes another file's forms into the output, so a Makefile
had to make every object depend on every .sex. --depfile names the
modules read, including those read only for an import's interface,
with an empty rule for each, as cc -MD -MP does.

Fixes #40.
This commit is contained in:
2026-10-09 17:44:36 +03:00
parent b998d298dd
commit dc15aaadab
5 changed files with 69 additions and 4 deletions

View File

@@ -2,5 +2,6 @@
(get-modules-public-forms
load-persistent-module-paths
check-public-interface
module-dependencies
read-public-interface)
"sex-modules.scm")

View File

@@ -78,13 +78,24 @@
(file-readable? module-path)
module-path))))
;;; Every module file read for this unit, first read first: what its
;;; output depends on, besides the unit itself
(define +module-dependencies+ (list))
(define (module-dependencies) (reverse +module-dependencies+))
(define (read-module path)
(unless (member path +module-dependencies+)
(set! +module-dependencies+ (cons path +module-dependencies+)))
(read-from-file path))
(define (read-public-interface module-path)
;; pub fns are reduced to prototypes, other pub forms are just pasted.
;; Declaration order is kept: a module that declares a type and then a
;; pub fn taking it has to paste them that way round, or the prototype
;; names a type C has not seen. The declarations of the types its
;; importers see only behind a pointer go first.
(let ((raw-forms (read-from-file module-path)))
(let ((raw-forms (read-module module-path)))
(append (check-public-interface raw-forms)
(interface-forms raw-forms))))
@@ -275,7 +286,7 @@
(let ((path (locate-module (symbol->string (car modules)))))
(if (or (not path) (member path seen))
(read (cdr modules) seen acc)
(let ((forms (interface-forms (read-from-file path))))
(let ((forms (interface-forms (read-module path))))
(read (append (cdr modules) (imported-modules forms #f))
(cons path seen)
(append acc (declared-types forms)))))))))

View File

@@ -64,6 +64,11 @@
(required #f)
(value #t)
(single-char #\o))
(depfile
,(fmt #f "Write a make rule for the output to FILE: it depends on" nl
(pad padding) "the source and on every module read for it. Needs -o")
(required #f)
(value #t))
(line-directives
,(fmt #f "How much #line information to emit: statement (default)," nl
(pad padding) "toplevel, or none. `statement' is what makes a debugger" nl
@@ -150,6 +155,28 @@
(map pp sex-forms)
(emit-c sex-forms)))))
;;; app.o: app.sex /m/greet.sex, and an empty rule per module, so that
;;; make takes a module deleted since as out of date rather than as a
;;; file it has no rule to make
(define (dependency-rule target source modules)
(let ((spell (lambda (path) (string-translate* path '((" " . "\\ "))))))
(apply string-append
(spell target) ":"
(append (map (lambda (path) (string-append " " (spell path)))
(if source (cons source modules) modules))
(list "\n")
(map (lambda (path) (string-append "\n" (spell path) ":\n"))
modules)))))
(define (write-dependencies depfile output input)
(when (or (eq? output 'default) (equal? output "-"))
(error "--depfile needs -o, to name what depends on the modules"))
(with-output-to-file depfile
(lambda ()
(display (dependency-rule output
(and (not (eq? input 'stdin)) input)
(module-dependencies))))))
(define (compile-to-file sex-forms output args cc-args)
"Hand the generated C to the C compiler. Returns the compiler's exit
status, which is ours to pass on."
@@ -255,6 +282,9 @@ status, which is ours to pass on."
(sex-line-directives 'none))
(let ((sex-forms (semantic-process-forms (read-raw-forms input) input)))
(let ((depfile (get-arg args 'depfile #f)))
(when depfile
(write-dependencies depfile output input)))
(if (or (get-arg args 'macro-expand #f)
(get-arg args 'emit-c #f))
;; Emit processed and macro-expanded sex code, or emit C code

View File

@@ -65,3 +65,19 @@
(test "any other output is left alone"
'("-o" "out" "-")
(spell-stdout-output '("-o" "out" "-"))))
;;; --depfile: an import pastes another file's forms into the output, so
;;; the output depends on that file as a C object does on a header
(test-group "dependency rule"
(test "the target, the source and the modules, then a rule per module"
"app.o: app.sex /m/greet.sex /m/ledger.sex\n\n/m/greet.sex:\n\n/m/ledger.sex:\n"
(dependency-rule "app.o" "app.sex" '("/m/greet.sex" "/m/ledger.sex")))
(test "no modules, no rules of their own"
"app.o: app.sex\n"
(dependency-rule "app.o" "app.sex" '()))
(test "a space is escaped, the way make reads it"
"a\\ b.o: a\\ b.sex\n"
(dependency-rule "a b.o" "a b.sex" '()))
(test "stdin is no file to depend on"
"app.o: /m/greet.sex\n\n/m/greet.sex:\n"
(dependency-rule "app.o" #f '("/m/greet.sex"))))

View File

@@ -20,6 +20,10 @@
# to a prototype and to an extern both have to look past. And a type the
# app cannot see -- private to greet, or from a module greet imports
# without `pub' -- crosses behind a pointer, declared and no more.
#
# The app's dependency rule names both modules: greet, which it imports,
# and ledger, which only greet does but which decides what the app is
# told of it.
SEXC ?= ../../sexc
@@ -28,7 +32,10 @@ EXPECTED = hello, world\nhello, sex\n2 greetings\ntext times \ntext times \nmood
check:
@$(SEXC) ledger.sex -c -o ledger.o
@$(SEXC) greet.sex -c -o greet.o
@$(SEXC) greet-app.sex -o greet-app -- greet.o ledger.o
@$(SEXC) greet-app.sex -o greet-app --depfile=greet-app.d -- greet.o ledger.o
@if ! head -1 greet-app.d | grep -q '^greet-app: greet-app.sex .*/greet.sex .*/ledger.sex$$'; then \
echo "modules FAILED, the dependency rule is:"; cat greet-app.d; $(MAKE) clean; exit 1; \
fi
@if [ "`./greet-app`" = "`printf '$(EXPECTED)\n'`" ]; then \
echo "modules ok"; \
else \
@@ -37,6 +44,6 @@ check:
@$(MAKE) --no-print-directory clean
clean:
@rm -f greet.o ledger.o greet-app
@rm -f greet.o ledger.o greet-app greet-app.d
.PHONY: check clean