UpdateMakefiles 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #!/bin/sh
  2. # aside from this initial boilerplate, this is actually -*- scheme -*- code
  3. main='(module-ref (resolve-module '\''(scripts script-entry-point)) '\'main')'
  4. exec ${GUILE-guile} -l $0 -c "(apply $main (command-line))" "$0" "$@"
  5. !#
  6. ;;; UpdateMakefiles - to extract part of definitions.mk and copy to individual Makefiles
  7. ;;;
  8. ;;; Copyright (c) 2009 Openmoko Inc.
  9. ;;;
  10. ;;; Authors Christopher Hall <hsw@openmoko.com>
  11. ;;;
  12. ;;; This program is free software: you can redistribute it and/or modify
  13. ;;; it under the terms of the GNU General Public License as published by
  14. ;;; the Free Software Foundation, either version 3 of the License, or
  15. ;;; (at your option) any later version.
  16. ;;;
  17. ;;; This program is distributed in the hope that it will be useful,
  18. ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. ;;; GNU General Public License for more details.
  21. ;;;
  22. ;;; You should have received a copy of the GNU General Public License
  23. ;;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. (define-module (scripts script-entry-point)
  25. :use-module (ice-9 getopt-long)
  26. :use-module (ice-9 rdelim)
  27. :use-module (ice-9 regex)
  28. :use-module (srfi srfi-1)
  29. :export (script-entry-point))
  30. (define-macro (unless cond . body)
  31. `(if (not ,cond) (begin ,@body)))
  32. (define *program* "program-name-here")
  33. (define *debug* #f)
  34. (define *verbose* #f)
  35. (define (usage message)
  36. "Display a usage message"
  37. (if (string? message)
  38. (format #t "error: ~a~%" message))
  39. (format #t "usage: ~a [--verbose] [--debug] --source=file files...~%" *program*)
  40. (exit 1))
  41. (define (main . args)
  42. "Main program"
  43. (set! *program* (second args))
  44. (let* ((parsed-opts (getopt-long (drop args 1)
  45. '((list (single-char #\l) (value #t))
  46. (source (single-char #\s) (value #t))
  47. (verbose (single-char #\v))
  48. (debug (single-char #\d)))))
  49. (source (option-ref parsed-opts 'source #f))
  50. (verbose (option-ref parsed-opts 'verbose #f))
  51. (debug (option-ref parsed-opts 'debug #f))
  52. (files (option-ref parsed-opts '() #f)))
  53. (if debug
  54. (begin
  55. (display parsed-opts)
  56. (newline)))
  57. (set! *debug* debug)
  58. (set! *verbose* verbose)
  59. (unless source (usage "Missing source"))
  60. (if (null? files) (usage "Missing files"))
  61. (if debug
  62. (format #t "source = ~a~%" source))
  63. (for-each (lambda (file)
  64. (if (file-exists? file)
  65. (process-file source file)
  66. (format #t "Ignore non-existant file:~a~%" file))
  67. )
  68. files)))
  69. (define (process-file source file)
  70. (if *verbose*
  71. (format #t "Processing: ~a~%" file))
  72. (let*
  73. ((backup-name (string-append file "~"))
  74. (output-port (mkstemp! (string-append file "-XXXXXX")))
  75. (new-name (port-filename output-port)))
  76. (chmod output-port (logand #o666 (lognot (umask))))
  77. (with-input-from-file file
  78. (lambda ()
  79. (copy-to output-port "+++START_UPDATE_MAKEFILE:" #f #f)
  80. (skip-to output-port "---END_UPDATE_MAKEFILE:")
  81. (unless (eof-object? (peek-char))
  82. (begin
  83. (format output-port "# +++START_UPDATE_MAKEFILE: Start of auto included code~%")
  84. (with-input-from-file source
  85. (lambda ()
  86. (skip-to output-port "+++START_COPY:")
  87. (copy-to output-port "---END_COPY:" "#INC: " "#REMARK")))
  88. (format output-port "# ---END_UPDATE_MAKEFILE: End of auto included code~%")))
  89. (copy-remainder output-port)))
  90. (if (file-exists? backup-name)
  91. (delete-file backup-name))
  92. (rename-file file backup-name)
  93. (rename-file new-name file)))
  94. (define (skip-to port match-line)
  95. (while (and (not (eof-object? (peek-char)))
  96. (not (string-contains (read-line) match-line)))))
  97. (define (copy-to port match-line remove-prefix skip-prefix)
  98. (while (and (not (eof-object? (peek-char)))
  99. (let ((line (read-line)))
  100. (if (string-contains line match-line)
  101. #f
  102. (cond
  103. ((and remove-prefix (string-prefix? remove-prefix line))
  104. (format port "~a~%" (string-drop line (string-length remove-prefix))))
  105. ((and skip-prefix (string-prefix? skip-prefix line))
  106. #t)
  107. (#t
  108. (format port "~a~%" line))))))))
  109. (define (copy-remainder port)
  110. (while (not (eof-object? (peek-char)))
  111. (format port "~a~%" (read-line))))