set-compiler.scm 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. ;;; Copyright 2010 by Christian Jaeger <chrjae@gmail.com>
  2. ;;; This file is part of GIT System.
  3. ;;;
  4. ;;; GIT System is free software: you can redistribute it and/or modify
  5. ;;; it under the terms of the GNU Lesser General Public License as published by
  6. ;;; the Free Software Foundation, either version 3 of the License, or
  7. ;;; (at your option) any later version.
  8. ;;;
  9. ;;; GIT System is distributed in the hope that it will be useful,
  10. ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;;; GNU Lesser General Public License for more details.
  13. ;;;
  14. ;;; You should have received a copy of the GNU Lesser General Public License
  15. ;;; along with GIT System. If not, see <http://www.gnu.org/licenses/>.
  16. ;; possibly-compile and load
  17. ;; BUGS:
  18. ;; - sometimes when loading compiled object, editing file, reloading
  19. ;; into running system it will compile and load, then on subsequent
  20. ;; load give the 'cannot load multiple times' error
  21. ;; - sometimes, with (define compile-mode 'c), a file that has been
  22. ;; compiled, when edited, will not get reloaded into the running
  23. ;; system from source, neither freshly compiled. strange. Only time
  24. ;; this happened was with Serialization-Deserialization.scm
  25. (define-macro (compile-time-define-if-not-defined name expr)
  26. (with-exception-catcher
  27. (lambda (e)
  28. (eval `(define ,name ,expr)))
  29. (lambda ()
  30. (eval name) ;; if it doesn't exist, will define it
  31. '(begin))))
  32. (compile-time-define-if-not-defined objects-loaded (make-table)) ;; name to [file mtime,no,] index
  33. (define (object-load-if-changed name i)
  34. (define (load+set)
  35. (load name)
  36. (table-set! objects-loaded name i))
  37. (if (eq? compile-mode 's)
  38. (error "BUG"))
  39. (cond ((table-ref objects-loaded name #f)
  40. => (lambda (oldi)
  41. (if (> i oldi)
  42. (load+set)
  43. #f)))
  44. (else
  45. (load+set))))
  46. (define (i/load name)
  47. (let ((sourcefile (string-append name ".scm")))
  48. (load sourcefile)))
  49. (define (set-compiler:perhaps-add lis key val) ;; ignore if val is false
  50. (if val
  51. (cons key (cons val lis))
  52. lis))
  53. (define mod:compiled? (make-parameter #f))
  54. (define (c/load name #!key ld-options cc-options)
  55. ;; possibly compile and load:
  56. (let* ((compile-options (set-compiler:perhaps-add
  57. (set-compiler:perhaps-add
  58. compile-options
  59. ld-options: ld-options)
  60. cc-options: cc-options))
  61. (sourcefile (string-append name ".scm")))
  62. (case compile-mode
  63. ((s) ;; always source
  64. (load sourcefile))
  65. (else
  66. (let* ((sourceinf (file-info sourcefile))
  67. (evtl-compile+load
  68. (lambda (i)
  69. (case compile-mode
  70. ((l) ;; load binary or source, whatever is newer (left up to Gambit)
  71. (error "not yet implemented"))
  72. ((c) ;; compile
  73. (println (list "compiling: " name))
  74. (parameterize
  75. ((mod:compiled? #t))
  76. (apply compile-file sourcefile compile-options))
  77. ;; gives #f on failure; but, we want to go to the debugger
  78. ;; maybe?, or at least stop the process, so:
  79. (object-load-if-changed name i))
  80. (else
  81. (error "invalid compile-mode value:" compile-mode))))))
  82. (cond ((let lp ((i 1)
  83. (obinf #f))
  84. (let ((obp (string-append name ".o"
  85. (number->string i))))
  86. (if (file-exists? obp)
  87. (lp (+ i 1)
  88. (file-info obp))
  89. (and obinf
  90. (cons obinf (- i 1))))))
  91. =>
  92. (lambda (obinf+i)
  93. (if (> (time->seconds (file-info-last-modification-time sourceinf))
  94. (time->seconds (file-info-last-modification-time (car obinf+i))))
  95. (evtl-compile+load (cdr obinf+i))
  96. ;; assuming macros haven't changed
  97. (object-load-if-changed name (cdr obinf+i)))))
  98. (else
  99. ;; no binary
  100. (evtl-compile+load 1))))))))