flatload.scm 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. ; Copyright (c) 1993-2008 by Richard Kelsey and Jonathan Rees. See file COPYING.
  2. ; flatloaded -> load
  3. (define *noisy?* #f)
  4. (define (flatload struct . env-option)
  5. (let ((env (if (null? env-option)
  6. (interaction-environment)
  7. (car env-option)))
  8. (l '())
  9. (set-package-loaded?! set-package-loaded?!))
  10. (walk-packages (list struct)
  11. (lambda (p)
  12. (not (package-loaded? p)))
  13. (lambda (file p)
  14. (let* ((fn (package-file-name p))
  15. (file (namestring file
  16. (if fn
  17. (file-name-directory fn)
  18. #f)
  19. *load-file-type*)))
  20. (if *noisy?*
  21. (begin (display #\space) (display file)))
  22. (set! l (cons (lambda () (apply fload file env-option))
  23. l))))
  24. (lambda (forms p)
  25. (set! l (cons (lambda ()
  26. (for-each (lambda (form)
  27. (eval form env))
  28. forms))
  29. l)))
  30. (lambda (p)
  31. (set! l (cons (lambda ()
  32. (set-package-loaded?! p #t))
  33. l))))
  34. (for-each (lambda (thunk) (thunk)) (reverse l))
  35. (newline)))
  36. (define (fload filename . rest)
  37. (let ((save filename))
  38. (dynamic-wind (lambda () (set! *source-file-name* filename))
  39. (lambda ()
  40. (apply load filename rest))
  41. (lambda () (set! *source-file-name* save)))))
  42. (define (walk-packages structs process? file-action forms-action after-action)
  43. (let ((seen '()))
  44. (letrec ((recur
  45. (lambda (s)
  46. (let ((p (structure-package s)))
  47. (if (not (memq p seen))
  48. (begin
  49. (set! seen (cons p seen))
  50. (if (process? p)
  51. (begin
  52. (if *noisy?*
  53. (begin (newline)
  54. (display "[")
  55. (write (structure-name s))))
  56. ;; (write (structure-name s)) (display " ")
  57. (for-each recur (package-opens p))
  58. (for-each (lambda (name+struct)
  59. (recur (cdr name+struct)))
  60. (package-accesses p))
  61. (for-each (lambda (clause)
  62. (case (car clause)
  63. ((files)
  64. (for-each (lambda (f)
  65. (file-action f p))
  66. (cdr clause)))
  67. ((begin)
  68. (forms-action (cdr clause) p))))
  69. (package-clauses p))
  70. (after-action p)
  71. (if *noisy?* (display "]"))))))))))
  72. (for-each recur structs))
  73. (if *noisy?* (newline))
  74. seen))
  75. ; Return list of names of all files needed to build a particular structure.
  76. ; This is handy for creating dependency lists for "make".
  77. (define (all-file-names struct . base-option)
  78. (let ((l '())
  79. (b '()))
  80. (walk-packages base-option
  81. (lambda (p) #t)
  82. (lambda (filename p) #f)
  83. (lambda (forms p) #f)
  84. (lambda (p)
  85. (set! b (cons p b))))
  86. (walk-packages (list struct)
  87. (lambda (p)
  88. (not (memq p b)))
  89. (lambda (filename p)
  90. (let ((dir (file-name-directory (package-file-name p))))
  91. (set! l (cons (namestring filename dir *load-file-type*)
  92. l))))
  93. (lambda (forms p)
  94. (display "Package contains (begin ...) clause: ")
  95. (write forms)
  96. (newline))
  97. (lambda (p) #f))
  98. (reverse l)))