Compare commits
7 Commits
6f7b28cfa5
...
67749c8db0
| Author | SHA1 | Date | |
|---|---|---|---|
| 67749c8db0 | |||
| faa9036dcc | |||
| c33bfd704d | |||
| 397766822d | |||
| a504fc1410 | |||
| c65721a070 | |||
| dcb65c5504 |
9
.gitignore
vendored
9
.gitignore
vendored
@@ -5,10 +5,11 @@
|
||||
*.o
|
||||
*.import.scm
|
||||
*.link
|
||||
modules.mk
|
||||
sexc
|
||||
sex-tests
|
||||
sextest
|
||||
/modules.mk
|
||||
/modules.stamp
|
||||
/sexc
|
||||
/sex-tests
|
||||
/sextest
|
||||
|
||||
# Scrapped design docs, kept for reference
|
||||
/attic
|
||||
|
||||
54
Makefile
54
Makefile
@@ -1,5 +1,6 @@
|
||||
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
|
||||
# What and why:
|
||||
# -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
|
||||
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
|
||||
# what it imports, read off the import forms
|
||||
# MODULES, and a line for each module and program naming what it
|
||||
# imports, read off the import forms. Not for clean, which has to work
|
||||
# when they cannot be read
|
||||
ifneq ($(MAKECMDGOALS),clean)
|
||||
include modules.mk
|
||||
OBJ = $(MODULES:%=%.o)
|
||||
endif
|
||||
|
||||
modules.mk: $(SOURCES) tools/module-deps.scm dependencies.txt
|
||||
$(CSI) -s tools/module-deps.scm $(PROGRAMS) > $@.tmp
|
||||
mv $@.tmp $@
|
||||
# The scan is redone whenever a source may have changed what it
|
||||
# imports, or a NAME.scm may have come to be the module an import
|
||||
# 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
|
||||
# -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 $* $(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
|
||||
# -o, and sexc.o is the module's
|
||||
@@ -67,7 +77,7 @@ $(PROGRAM_OBJ): %.o: %.scm
|
||||
$(CHICKEN_C) $(CSC_FLAGS) -c $< -o $@
|
||||
|
||||
$(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
|
||||
# they import
|
||||
@@ -76,8 +86,12 @@ SEX_TEST_PROGRAMS = $(wildcard tests/sex-programs/*.sex)
|
||||
check run-tests: sexc sex-tests sextest
|
||||
./sex-tests
|
||||
./sextest --sexc=./sexc $(SEX_TEST_PROGRAMS)
|
||||
cd tests/modules && SEXC=$(CURDIR)/sexc $(CSI) -s check.scm
|
||||
cd tests/exit-code && SEXC=$(CURDIR)/sexc $(CSI) -s check.scm
|
||||
@$(MAKE) --no-print-directory check-modules
|
||||
@$(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_PROGRAM) sexc $(DESTDIR)$(bindir)/sexc
|
||||
@@ -95,9 +109,9 @@ uninstall:
|
||||
-rmdir $(DESTDIR)$(sexlibdir)
|
||||
|
||||
clean:
|
||||
rm -f $(OBJ) $(PROGRAM_OBJ) modules.mk
|
||||
rm -f *.o $(PROGRAM_OBJ) modules.mk modules.stamp
|
||||
rm -f *.import.scm
|
||||
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
|
||||
|
||||
@@ -10,20 +10,22 @@
|
||||
;;; Everything is built inside a scratch directory, so nothing is left
|
||||
;;; here to clean up.
|
||||
;;;
|
||||
;;; csi -s check.scm
|
||||
;;; make check-exit-code
|
||||
|
||||
(import scheme
|
||||
(chicken base)
|
||||
(chicken file)
|
||||
(chicken io)
|
||||
(chicken pathname)
|
||||
(chicken process)
|
||||
(chicken process-context)
|
||||
(chicken string)
|
||||
test)
|
||||
|
||||
;;; A path stays good past the change of directory below; a bare name
|
||||
;;; is looked up on PATH
|
||||
(define sexc
|
||||
(let ((path (or (get-environment-variable "SEXC") "../../sexc")))
|
||||
(if (absolute-pathname? path)
|
||||
(if (or (absolute-pathname? path) (not (substring-index "/" path)))
|
||||
path
|
||||
(make-pathname (current-directory) path))))
|
||||
|
||||
@@ -33,12 +35,13 @@
|
||||
|
||||
;;; Whether sexc exits 0 when run with ARGS, saying nothing of it
|
||||
(define (sexc-succeeds? . args)
|
||||
(let ((proc (process* sexc args)))
|
||||
(read-string #f (process-output-port proc))
|
||||
(read-string #f (process-error-port proc))
|
||||
(process-wait proc)
|
||||
(and (process-returned-normally? proc)
|
||||
(zero? (process-exit-status proc)))))
|
||||
(zero? (system (string-append (string-intersperse (map qs (cons sexc args)))
|
||||
" >/dev/null 2>&1"))))
|
||||
|
||||
;;; Every check below expects a failure, which a sexc that cannot run
|
||||
;;; gives too
|
||||
(test "sexc compiles a program" #t
|
||||
(sexc-succeeds? "hello.sex" "-o" out))
|
||||
|
||||
(test "a failed emission leaves no temporary .c" '()
|
||||
(begin
|
||||
|
||||
@@ -28,13 +28,14 @@
|
||||
;;; and ledger, which only greet does but which decides what the app is
|
||||
;;; told of it.
|
||||
;;;
|
||||
;;; csi -s check.scm
|
||||
;;; make check-modules
|
||||
|
||||
(import scheme
|
||||
(chicken base)
|
||||
(chicken file)
|
||||
(chicken io)
|
||||
(chicken irregex)
|
||||
(chicken pathname)
|
||||
(chicken process)
|
||||
(chicken process-context)
|
||||
srfi-1
|
||||
@@ -42,6 +43,11 @@
|
||||
|
||||
(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
|
||||
(define (output-of program . args)
|
||||
(let* ((proc (process program args))
|
||||
@@ -56,14 +62,15 @@
|
||||
(take (or (output-of sexc "--public-interface" "greet.sex") '()) 3))
|
||||
|
||||
(test-assert "the app links with both modules"
|
||||
(and (output-of sexc "ledger.sex" "-c" "-o" "ledger.o")
|
||||
(output-of sexc "greet.sex" "-c" "-o" "greet.o")
|
||||
(output-of sexc "greet-app.sex" "-o" "greet-app" "--depfile=greet-app.d"
|
||||
"--" "greet.o" "ledger.o")))
|
||||
(and (output-of sexc "ledger.sex" "-c" "-o" (built "ledger.o"))
|
||||
(output-of sexc "greet.sex" "-c" "-o" (built "greet.o"))
|
||||
(output-of sexc "greet-app.sex" "-o" (built "greet-app")
|
||||
(string-append "--depfile=" (built "greet-app.d"))
|
||||
"--" (built "greet.o") (built "ledger.o"))))
|
||||
|
||||
(test-assert "the dependency rule names both modules"
|
||||
(irregex-match "greet-app: greet-app\\.sex .*/greet\\.sex .*/ledger\\.sex"
|
||||
(call-with-input-file "greet-app.d" read-line)))
|
||||
(irregex-match ".*/greet-app: greet-app\\.sex .*/greet\\.sex .*/ledger\\.sex"
|
||||
(call-with-input-file (built "greet-app.d") read-line)))
|
||||
|
||||
(test "the app runs through both"
|
||||
'("hello, world"
|
||||
@@ -79,8 +86,8 @@
|
||||
"board 42"
|
||||
"ledger 2"
|
||||
"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)
|
||||
|
||||
@@ -1,23 +1,28 @@
|
||||
;;; 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.
|
||||
;;; 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
|
||||
;;;
|
||||
;;; 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
|
||||
;;; 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
|
||||
(chicken base)
|
||||
(chicken condition)
|
||||
(chicken file)
|
||||
(chicken io)
|
||||
(chicken pathname)
|
||||
@@ -44,14 +49,28 @@
|
||||
(let ((file (string-append (symbol->string name) ".scm")))
|
||||
(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)
|
||||
(let loop ((forms (call-with-input-file file read-list)) (names '()) (files '()))
|
||||
(let loop ((forms (forms-of file)) (names '()) (files '()))
|
||||
(if (null? forms)
|
||||
(values names files)
|
||||
(let ((form (car forms)))
|
||||
(cond
|
||||
((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)
|
||||
(loop (cdr forms) (append (map spec-name (cdr form)) names) files))
|
||||
((eq? (car form) 'include-relative)
|
||||
@@ -73,19 +92,16 @@
|
||||
(loop (cdr names) acc))
|
||||
(else (loop (cdr names) (cons (car names) acc))))))
|
||||
|
||||
(define (imported-modules file)
|
||||
(let-values (((names files) (scan file)))
|
||||
(local-modules names)))
|
||||
|
||||
;;; Each module reached from ROOTS, with the modules it imports
|
||||
;;; Each module reached from ROOTS, as (name imported-modules . included-files)
|
||||
(define (module-graph roots)
|
||||
(let loop ((pending roots) (graph '()))
|
||||
(cond ((null? pending) (sort graph (lambda (a b) (symbol<? (car a) (car b)))))
|
||||
((assq (car pending) graph) (loop (cdr pending) graph))
|
||||
(else
|
||||
(let ((deps (imported-modules (module-file (car pending)))))
|
||||
(loop (append (cdr pending) deps)
|
||||
(cons (cons (car pending) deps) graph)))))))
|
||||
(let-values (((names files) (scan (module-file (car pending)))))
|
||||
(let ((deps (local-modules names)))
|
||||
(loop (append (cdr pending) deps)
|
||||
(cons (cons (car pending) (cons deps files)) graph))))))))
|
||||
|
||||
(define (objects 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
|
||||
(define-record-type program
|
||||
(make-program name object modules sources)
|
||||
(make-program binary object modules sources)
|
||||
program?
|
||||
(name program-name)
|
||||
(binary program-binary)
|
||||
(object program-object)
|
||||
(modules program-modules)
|
||||
(sources program-sources))
|
||||
@@ -114,17 +130,19 @@
|
||||
(let* ((programs (map read-program (command-line-arguments)))
|
||||
(graph (module-graph
|
||||
(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 "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))))
|
||||
(print "SOURCES = " (string-intersperse sources " "))
|
||||
(for-each (lambda (node)
|
||||
(rule (car (objects (list (car node))))
|
||||
(append (cddr node) (objects (cadr 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))
|
||||
(rule (program-binary p) (cons (program-object p) modules))))
|
||||
programs)
|
||||
(for-each (lambda (source) (print source ":")) sources))
|
||||
|
||||
Reference in New Issue
Block a user