forked from alex-eg/sex
build the programs the way the modules are built
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.
This commit is contained in:
57
Makefile
57
Makefile
@@ -37,20 +37,17 @@ 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. Not for clean, which has to work when they cannot
|
PROGRAMS = sexc=main.scm sextest=tools/sextest/sextest.scm sex-tests=tests/run.scm
|
||||||
# be read
|
PROGRAM_NAMES = $(foreach p,$(PROGRAMS),$(firstword $(subst =, ,$(p))))
|
||||||
|
PROGRAM_OBJ = $(foreach p,$(PROGRAMS),$(patsubst %.scm,%.o,$(lastword $(subst =, ,$(p)))))
|
||||||
|
|
||||||
|
# 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)
|
ifneq ($(MAKECMDGOALS),clean)
|
||||||
include modules.mk
|
include modules.mk
|
||||||
endif
|
endif
|
||||||
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
|
|
||||||
|
|
||||||
#------------------------------------------------------------------
|
|
||||||
|
|
||||||
# The scan is redone whenever a source may have changed what it
|
# 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
|
# imports, or a NAME.scm may have come to be the module an import
|
||||||
@@ -59,34 +56,28 @@ sexc: $(OBJ) main.scm
|
|||||||
# have it start over for good
|
# have it start over for good
|
||||||
modules.mk: modules.stamp ;
|
modules.mk: modules.stamp ;
|
||||||
|
|
||||||
modules.stamp: $(filter-out %.import.scm,$(wildcard *.scm)) tools/module-deps.scm
|
modules.stamp: $(SOURCES) $(filter-out %.import.scm,$(wildcard *.scm)) \
|
||||||
$(CSI) -s tools/module-deps.scm main.scm > modules.mk.tmp \
|
tools/module-deps.scm dependencies.txt Makefile
|
||||||
|
$(CSI) -s tools/module-deps.scm $(PROGRAMS) > modules.mk.tmp \
|
||||||
|| { rm -f modules.mk.tmp; exit 1; }
|
|| { rm -f modules.mk.tmp; exit 1; }
|
||||||
if cmp -s modules.mk.tmp modules.mk; then rm modules.mk.tmp; \
|
if cmp -s modules.mk.tmp modules.mk; then rm modules.mk.tmp; \
|
||||||
else mv modules.mk.tmp modules.mk; fi
|
else mv modules.mk.tmp modules.mk; fi
|
||||||
touch $@
|
touch $@
|
||||||
|
|
||||||
|
#------------------------------------------------------------------
|
||||||
|
|
||||||
# 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
|
# -module, except the vendored sex-fmt-c, a (module ...) form of its own
|
||||||
|
|
||||||
%.o: %.scm
|
%.o: %.scm
|
||||||
$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) $< -module $* -o $@ -unit $*
|
$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) $< $(if $(filter sex-fmt-c,$*),,-module $*) -o $@ -unit $*
|
||||||
|
|
||||||
# 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 $@
|
||||||
# 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
|
||||||
@@ -118,9 +109,9 @@ uninstall:
|
|||||||
-rmdir $(DESTDIR)$(sexlibdir)
|
-rmdir $(DESTDIR)$(sexlibdir)
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -f *.o modules.mk modules.stamp
|
rm -f *.o $(PROGRAM_OBJ) modules.mk modules.stamp
|
||||||
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 $(PROGRAM_NAMES)
|
||||||
|
|
||||||
.PHONY: all check run-tests check-modules check-exit-code install install-strip installdirs uninstall clean
|
.PHONY: all check run-tests check-modules check-exit-code install install-strip installdirs uninstall clean
|
||||||
|
|||||||
@@ -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)
|
||||||
|
|||||||
@@ -1,22 +1,34 @@
|
|||||||
;;; 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. The files either includes with include-relative
|
||||||
|
;;; 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 ...
|
||||||
|
;;; 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
|
||||||
|
;;; 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 condition)
|
||||||
(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)
|
||||||
@@ -27,25 +39,16 @@
|
|||||||
(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 ...)
|
|
||||||
(define (imported forms)
|
|
||||||
(let walk ((forms forms) (acc '()))
|
|
||||||
(if (null? forms)
|
|
||||||
acc
|
|
||||||
(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)))))))
|
|
||||||
|
|
||||||
;;; FILE's forms, or the end of the scan, naming the file that has none
|
;;; FILE's forms, or the end of the scan, naming the file that has none
|
||||||
(define (forms-of file)
|
(define (forms-of file)
|
||||||
(handle-exceptions exn
|
(handle-exceptions exn
|
||||||
@@ -57,36 +60,89 @@
|
|||||||
(exit 1))
|
(exit 1))
|
||||||
(call-with-input-file file read-list)))
|
(call-with-input-file file read-list)))
|
||||||
|
|
||||||
(define (local-imports file)
|
;;; The names FILE imports, and the files it includes; the vendored
|
||||||
(let loop ((names (imported (forms-of file)))
|
;;; sex-fmt-c imports inside its (module name exports ...) form
|
||||||
(acc '()))
|
(define (scan file)
|
||||||
|
(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)
|
||||||
|
(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 (symbol<? a b)
|
||||||
|
(string<? (symbol->string a) (symbol->string b)))
|
||||||
|
|
||||||
|
;;; 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)
|
;;; Each module reached from ROOTS, as (name imported-modules . included-files)
|
||||||
(string<? (symbol->string a) (symbol->string b)))
|
(define (module-graph roots)
|
||||||
|
(let loop ((pending roots) (graph '()))
|
||||||
;;; Each module reached from FILE, with the modules it imports
|
|
||||||
(define (module-graph file)
|
|
||||||
(let loop ((pending (local-imports file)) (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-values (((names files) (scan (module-file (car pending)))))
|
||||||
(loop (append (cdr pending) deps)
|
(let ((deps (local-modules names)))
|
||||||
(cons (cons (car pending) deps) graph)))))))
|
(loop (append (cdr pending) deps)
|
||||||
|
(cons (cons (car pending) (cons deps files)) 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)
|
||||||
|
(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 binary object modules sources)
|
||||||
|
program?
|
||||||
|
(binary program-binary)
|
||||||
|
(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))))))
|
||||||
|
(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 "SOURCES = " (string-intersperse sources " "))
|
||||||
(for-each (lambda (node)
|
(for-each (lambda (node)
|
||||||
(unless (null? (cdr node))
|
(rule (car (objects (list (car node))))
|
||||||
(display (car node))
|
(append (cddr node) (objects (cadr node)))))
|
||||||
(display ".o:")
|
graph)
|
||||||
(for-each (lambda (dep) (display " ") (display dep) (display ".o"))
|
(for-each (lambda (p)
|
||||||
(cdr node))
|
(let ((modules (objects (program-modules p))))
|
||||||
(newline)))
|
(rule (program-object p) (append (cdr (program-sources p)) modules))
|
||||||
graph))
|
(rule (program-binary p) (cons (program-object p) modules))))
|
||||||
|
programs)
|
||||||
|
(for-each (lambda (source) (print source ":")) sources))
|
||||||
|
|||||||
Reference in New Issue
Block a user