(define (rc->dfs-node rc) (let ((target (list-ref rc 0)) (prereqs (list-ref rc 1)) (wants-build? (list-ref rc 2)) (build-func (list-ref rc 3))) (make-dfs target prereqs (list wants-build? build-func)))) (define (rcs->dag rcs) (map (lambda (rc) (let ((target (list-ref rc 0)) (prereqs (list-ref rc 1)) (wants-build? (list-ref rc 2)) (build-func (list-ref rc 3))) (make-dfs target prereqs (list wants-build? build-func)))) rcs)) (define (dag->rcs dag) (map (lambda (node) (let* ((ls (dfs->list node)) (target (list-ref ls 0)) (prereqs (list-ref ls 1)) (ignored (list-ref ls 2))) (if ignored (let ((wants-build? (car ignored)) (build-func (cadr ignored))) (list target prereqs wants-build? build-func)) (error "node without wants-build? and build-func")))) dag)) (define (lookup-rc rc rcs) (let ((maybe-rc (find (lambda (current) (eq? (car rc) (car current))) rcs))) (if maybe-rc maybe-rc (error "lookup-rc: rc not found.")))) (define (lookup-fname fname rcs) (let ((maybe-fname (find (lambda (current) (eq? fname (car current))) rcs))) (if maybe-fname maybe-fname (error "lookup-fname: fname not found.")))) (define (lookup-rule pred fname rules) (let ((maybe-rule (find (lambda (current) (pred fname (car current))) rules))) (if maybe-rule (cdr maybe-rule) (error "lookup-rule: fname not found in rules.")))) (define (rcs+commons->rules pred rule-candidates common-rcs) (let* ((common-rules (common-rcs->common-rules common-rcs)) (create-leaf (lambda (maybe-target) (rc->dfs-node (search-match-in-common-rules common-rules maybe-target)))) (sorted-dag (dfs (rcs->dag rule-candidates) pred #t create-leaf)) (sorted-rcs (dag->rcs sorted-dag))) (if (not (null? sorted-rcs)) (let for-all-rcs ((rc (car sorted-rcs)) (todo (cdr sorted-rcs)) (last-done '())) (let* ((target (list-ref rc 0)) (prereqs (list-ref rc 1)) (wants-build? (list-ref rc 2)) (build-func (list-ref rc 3)) (done (cons (cons target (make-rule (map (lambda (prereq) (lookup-rule pred prereq last-done)) prereqs) wants-build? build-func)) last-done))) (if (null? todo) done (for-all-rcs (car todo) (cdr todo) done)))) sorted-rcs))) (define (rules->rule-set rule-alist) (if (not (null? rule-alist)) (let ((rules (map cdr rule-alist))) (let for-each-rule ((current-rule (car rules)) (rules-to-do (cdr rules)) (rule-set (make-empty-rule-set))) (let ((next-rule-set (rule-set-add current-rule rule-set))) (if (not (null? rules-to-do)) (for-each-rule (car rules-to-do) (cdr rules-to-do) next-rule-set) next-rule-set))))))