]> git.donarmstrong.com Git - lilypond.git/blobdiff - scm/lily.scm
Merge commit 'origin' into includes
[lilypond.git] / scm / lily.scm
index d63890af0aca443419ab691ce6cc9029997ac2d3..086d80f09f4d2b3e86e8438d4311a7d352002dc6 100644 (file)
 ;;;;
 ;;;;  source file of the GNU LilyPond music typesetter
 ;;;; 
-;;;; (c) 1998--2006 Jan Nieuwenhuizen <janneke@gnu.org>
+;;;; (c) 1998--2008 Jan Nieuwenhuizen <janneke@gnu.org>
 ;;;; Han-Wen Nienhuys <hanwen@xs4all.nl>
 
+;; Internationalisation: (_i "to be translated") gets an entry in the
+;; POT file (gettext ) must be invoked explicitely to do the actual
+;; "translation".
+;;(define-macro (_i x) x)
+;;(define-macro-public _i (x) x)
+;;(define-public-macro _i (x) x)
+;; Abbrv-PWR!
+(defmacro-public _i (x) x)
 
-(define (define-scheme-options)
-  (for-each (lambda (x)
-             (ly:add-option (car x) (cadr x) (caddr x)))
-         
-           `(
+(read-enable 'positions)
+(debug-enable 'debug)
 
-             ;; NAMING: either
-
-             ;; - [subject-]object-object-verb +"ing"
-             ;; - [subject-]-verb-object-object
+(define-public PLATFORM
+  (string->symbol
+   (string-downcase
+    (car (string-tokenize (utsname:sysname (uname)))))))
 
-             (anti-alias-factor 1 "render at higher resolution and scale down result\nto prevent jaggies in PNG")
-             (check-internal-types #f "check every property assignment for types")
-             (clip-systems #f "Generate cut-out snippets of a score")
-             (debug-gc #f "dump memory debugging statistics")
-             (debug-gc-assert-parsed-dead #f "for memory debugging:
-ensure that all refs to parsed objects are dead.  This is an internal option, and is switched on automatically for -ddebug-gc.") 
-             (debug-lexer #f "debug the flex lexer")
-             (debug-midi #f "generate human readable MIDI")
-             (debug-parser #f "debug the bison parser")
-             (debug-skylines #f "debug skylines")
-             (delete-intermediate-files #f
-                                        "delete unusable PostScript files")
-             (dump-profile #f "dump timing information for each file")
-             (dump-tweaks #f "dump page layout and tweaks for each score having the tweak-key layout property set.")
-             (dump-signatures #f "dump output signatures of each system")
-             
-             (eps-box-padding #f "Pad EPS bounding box left edge by this much to guarantee alignment between systems")
+(define scheme-options-definitions
+  `(
 
-             (gs-load-fonts #f
-                           "load fonts via Ghostscript.")
-             (gui #f "running from gui; redirect stderr to log file")
+    ;; NAMING: either
 
-             (include-book-title-preview #t "include book-titles in preview images.")
-             (include-eps-fonts #t "Include fonts in separate-system EPS files.")
-             (job-count #f "Process in parallel") 
-             (log-file #f "redirect output to log FILE.log")
+    ;; - [subject-]object-object-verb +"ing"
+    ;; - [subject-]-verb-object-object
 
-             (old-relative #f
-                           "relative for simultaneous music works
+    (anti-alias-factor 1 "render at higher resolution and scale down result\nto prevent jaggies in PNG")
+    (backend ps "which backend to use by default; Options: eps, null, ps [default], scm, svg)")
+    (check-internal-types #f "check every property assignment for types")
+    (clip-systems #f "Generate cut-out snippets of a score")
+    (datadir #f "LilyPond prefix for data files (Readonly).")
+    (debug-gc #f "dump memory debugging statistics")
+    (debug-gc-assert-parsed-dead #f "for memory debugging:
+ensure that all refs to parsed objects are dead.  This is an internal option, and is switched on automatically for -ddebug-gc.") 
+    (debug-lexer #f "debug the flex lexer")
+    (debug-page-breaking-scoring #f "dump scores for many different page breaking configurations")
+    (debug-parser #f "debug the bison parser")
+    (debug-property-callbacks #f "debug cyclic callback chains")
+    (debug-skylines #f "debug skylines")
+    (delete-intermediate-files #f
+                              "delete unusable PostScript files")
+    (dump-profile #f "dump memory and time information for each file")
+    (dump-cpu-profile #f "dump timing information (system-dependent)")
+    (dump-signatures #f "dump output signatures of each system.  Used for regression testing.")
+    
+    (eps-box-padding #f "Pad EPS bounding box left edge.  Guarantee alignment between systems in LaTeX.")
+    (gs-load-fonts #f
+                  "load fonts via Ghostscript.")
+    (gs-load-lily-fonts #f
+                       "load only lilypond fonts via Ghostscript.")
+    (gui #f "running from gui; redirect stderr to log file")
+    (help #f "show this help.") 
+    (include-book-title-preview #t "include book-titles in preview images.")
+    (include-eps-fonts #t "Include fonts in separate-system EPS files.")
+    (job-count #f "Process in parallel") 
+    (log-file #f "redirect output to log FILE.log")
+    (midi-extension ,(if (eq? PLATFORM 'windows)
+                        "mid"
+                        "midi")
+                   "set the default file extension for MIDI")
+
+    (old-relative #f
+                 "relative for simultaneous music works
 similar to chord syntax")
-             (object-keys #f
-                          "experimental mechanism for remembering tweaks")
-             (point-and-click #t "use point & click")
-             (paper-size "a4" "the default paper size")
-             (pixmap-format "png16m" "GS format to use for pixel images")
-             (protected-scheme-parsing #t "continue when finding errors in inline
+    (point-and-click #t "use point & click")
+    (paper-size "a4" "the default paper size")
+    (pixmap-format "png16m" "GS format to use for pixel images")
+    (preview #f "make a incipit image. ")
+    (print-pages #t "print pages normally. ")
+    (protected-scheme-parsing #t "continue when finding errors in inline
 scheme are caught in the parser. If off, halt 
 on errors, and print a stack trace.")
-             (profile-property-accesses #f "keep statistics of get_property() calls.")
-             
-             (resolution 101 "resolution for generating PNG bitmaps")
-             (read-file-list #f "Read files to be processed from command line arguments")
-
-             (safe #f "Run safely")
-             (strict-infinity-checking #f "If yes, crash on encountering Inf/NaN")
-
-             (ttf-verbosity 0
-                            "how much verbosity for TTF font embedding?")
-
-             (show-available-fonts #f
-                                   "List  font names available.")
-
-             (verbose ,(ly:command-line-verbose?) "value for the --verbose flag")
-             )))
-
+    (profile-property-accesses #f "keep statistics of get_property() calls.")
+    
+    (resolution 101 "resolution for generating PNG bitmaps")
+    (read-file-list #f "Read files to be processed from command line arguments")
+    (relative-includes #f "When processing an \\include command, look for the included file
+relative to the current file (instead of the root file)")
+
+    (safe #f "Run safely")
+    (strict-infinity-checking #f "If yes, crash on encountering Inf/NaN.")
+    (strip-output-dir #t "If yes, strip directories from input files.")
+    (separate-log-files #f "Output to FILE.log per file.")
+    (trace-memory-frequency #f "Record Scheme cell usage this many times per second, and dump to file.")
+    (trace-scheme-coverage #f "Record coverage of Scheme files") 
+    (show-available-fonts #f
+                         "List font names available.")
+    (verbose ,(ly:command-line-verbose?) "value for the --verbose flag")
+    ))
 
 ;; need to do this in the beginning. Other parts of the
 ;; Scheme init depend on these options.
 ;;
-(define-scheme-options)
+(for-each
+ (lambda (x)
+   (ly:add-option (car x) (cadr x) (caddr x)))
+ scheme-options-definitions)
+
+(for-each
+ (lambda (x)
+   (ly:set-option (car x) (cdr x)))
+ (eval-string (ly:command-line-options)))
 
 (debug-set! stack 0)
 
@@ -87,18 +116,30 @@ on errors, and print a stack trace.")
 
 (use-modules (ice-9 regex)
             (ice-9 safe)
+            (ice-9 format)
+            (ice-9 rdelim)
              (ice-9 optargs)
             (oop goops)
             (srfi srfi-1)
             (srfi srfi-13)
             (srfi srfi-14)
             (scm clip-region)
-
+            (scm memory-trace)
+            (scm coverage)
             )
+(define-public fancy-format format)
+(define-public (ergonomic-simple-format dest . rest)
+  "Like ice-9 format, but without the memory consumption."
+  
+  (if (string? dest)
+      (apply simple-format (cons #f (cons dest rest)))
+      (apply simple-format (cons dest rest))))
 
+(define format ergonomic-simple-format)
 
 ;; my display
-(define-public (myd k v) (display k) (display ": ") (display v) (display ", "))
+(define-public (myd k v) (display k) (display ": ") (display v) (display ", ")
+  v)
 
 (define-public (print . args)
   (apply format (cons (current-output-port) args)))
@@ -108,30 +149,21 @@ on errors, and print a stack trace.")
 ;;; debugging evaluator is slower.  This should
 ;;; have a more sensible default.
 
-(if (ly:get-option 'verbose)
+(if (or (ly:get-option 'verbose)
+       (ly:get-option 'trace-memory-frequency)
+       (ly:get-option 'trace-scheme-coverage)
+       )
     (begin
       (ly:set-option 'protected-scheme-parsing #f)
       (debug-enable 'debug)
       (debug-enable 'backtrace)
       (read-enable 'positions)))
 
-(define-public tex-backend?
-  (member (ly:output-backend) '("texstr" "tex")))
 
-(define-public parser #f)
-
-(define-public (lilypond-version)
-  (string-join
-   (map (lambda (x) (if (symbol? x)
-                       (symbol->string x)
-                       (number->string x)))
-       (ly:version))
-   "."))
-
-
-;; TeX C++ code actually hooks into TEX_STRING_HASHLIMIT 
-(define-public TEX_STRING_HASHLIMIT 10000000)
+(if (ly:get-option 'trace-scheme-coverage)
+    (coverage:enable))
 
+(define-public parser #f)
 
 
 ;; gettext wrapper for guile < 1.7.2
@@ -149,22 +181,6 @@ on errors, and print a stack trace.")
     (if (ly:get-option 'verbose)
        (ly:progress "]"))))
 
-;; Cygwin
-;; #(CYGWIN_NT-5.1 Hostname 1.5.12(0.116/4/2) 2004-11-10 08:34 i686)
-;;
-;; Debian
-;; #(Linux hostname 2.4.27-1-686 #1 Fri Sep 3 06:28:00 UTC 2004 i686)
-;;
-;; Mingw
-;; #(Windows XP HOSTNAME build 2600 5.01 Service Pack 1 i686)
-;;
-
-;; ugh, code dup.
-(define-public PLATFORM
-  (string->symbol
-   (string-downcase
-    (car (string-tokenize (vector-ref (uname) 0) char-set:letter)))))
-
 (define-public DOS
   (let ((platform (string-tokenize
                   (vector-ref (uname) 0) char-set:letter+digit)))
@@ -227,8 +243,6 @@ predicates. Print a message at LOCATION if any predicate failed."
 
 ;;(define-public (output-framework) (write "hello\n"))
 
-(define output-tex-module
-  (make-module 1021 (list (resolve-interface '(scm output-tex)))))
 (define output-ps-module
   (make-module 1021 (list (resolve-interface '(scm output-ps)))))
 
@@ -254,6 +268,14 @@ The syntax is the same as `define*-public'."
                                 safe-objects))
        ,safe-symbol)))
 
+(define-safe-public (lilypond-version)
+  (string-join
+   (map (lambda (x) (if (symbol? x)
+                       (symbol->string x)
+                       (number->string x)))
+       (ly:version))
+   "."))
+
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 ;; init pitch system
 
@@ -264,9 +286,10 @@ The syntax is the same as `define*-public'."
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 ;; other files.
 
-(for-each ly:load
-         ;; load-from-path
-         '("lily-library.scm"
+
+(define
+  init-scheme-files
+  '("lily-library.scm"
            "file-cache.scm"
            "define-event-classes.scm"
            "define-music-types.scm"
@@ -297,8 +320,11 @@ The syntax is the same as `define*-public'."
            "font.scm"
            "encoding.scm"
            
+           "flag-styles.scm"
            "fret-diagrams.scm"
-           "define-markup-commands.scm"
+           "harp-pedals.scm"
+           "predefined-fretboards.scm"
+            "define-markup-commands.scm"
            "define-grob-properties.scm"
            "define-grobs.scm"
            "define-grob-interfaces.scm"
@@ -313,6 +339,9 @@ The syntax is the same as `define*-public'."
            "safe-lily.scm"))
 
 
+(for-each ly:load init-scheme-files)
+
+
 (set! type-p-name-alist
       `(
        (,boolean-or-symbol? . "boolean or symbol")
@@ -357,25 +386,28 @@ The syntax is the same as `define*-public'."
 (define (profile-measurements)
   (let* ((t (times))
         (stats (gc-stats)))
-
+    
     (list
-     (- (+ (tms:utime t)
-          (tms:stime t))
+     (- (+ (tms:cutime t)
+          (tms:utime t))
        (ly:assoc-get 'gc-time-taken stats))
-
-     ;; difficult to put memory amount stats into here.
      
+     (ly:assoc-get 'total-cells-allocated  stats 0)
      )))
 
-(define (dump-profile name last this)
+(define (dump-profile base last this)
   (let*
-      ((outname (format "~a.profile" (basename name ".ly")))
+      ((outname (format "~a.profile" (dir-basename base ".ly")))
        (diff (map (lambda (y) (apply - y)) (zip this last))))
     
     (ly:progress "\nWriting timing to ~a..." outname)
     (format (open-file outname "w")
-           "time: ~a"
-           (car diff))))
+           "time: ~a\ncells: ~a\n"
+           (if (ly:get-option 'dump-cpu-profile)
+               (car diff)
+               0)
+           (cadr diff)
+           )))
 
 
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
@@ -383,6 +415,15 @@ The syntax is the same as `define*-public'."
 
 (define gc-dumping #f)
 (define gc-protect-stat-count 0)
+
+(define-public (dump-live-object-stats outfile)
+  (for-each
+   (lambda (x)
+     (format outfile "~a: ~a\n" (car x) (cdr x)))
+   (sort (gc-live-object-stats)
+        (lambda (x y)
+          (string<? (car x) (car y))))))
+
 (define-public (dump-gc-protects)
   (set! gc-protect-stat-count (1+ gc-protect-stat-count))
   (let* ((protects (sort
@@ -412,9 +453,10 @@ The syntax is the same as `define*-public'."
      outfile)
 
     (format outfile "\nprotected symbols: ~a\n"
-           (length (filter symbol?  (map car protects))))
-    
-            
+           (apply + (map (lambda (obj-count) (if (symbol? (car obj-count))
+                                                 (cdr obj-count)
+                                                 0))
+                            protects)))             
 
     ;; (display (ly:smob-protects))
     (newline outfile)
@@ -431,14 +473,7 @@ The syntax is the same as `define*-public'."
 
          (set! stats (gc-live-object-stats))
          (display "Dumping live object statistics.\n")
-         
-         (for-each
-          (lambda (x)
-            (format outfile "~a: ~a\n" (car x) (cdr x)))
-          (sort (gc-live-object-stats)
-                (lambda (x y)
-                  (string<? (car x) (car y)))))))
-
+         (dump-live-object-stats outfile)))
 
     (newline outfile)
     (let*
@@ -460,6 +495,36 @@ The syntax is the same as `define*-public'."
                           )))
 
     (set! gc-dumping #f)
+    (close-port outfile)
+    
+    ))
+
+
+(define (check-memory)
+  "read /proc/self to check up on memory use." 
+  (define (gulp-file name)
+    (let* ((file (open-input-file name))
+          (text (read-delimited "" file)))
+      (close file)
+      text))
+  (let*
+      ((stat (gulp-file "/proc/self/status"))
+       (lines (string-split stat #\newline))
+       (interesting (filter identity
+                           (map
+                            (lambda (l)
+                              (string-match "^VmData:[ \t]*([0-9]*) kB" l))
+                            lines)))
+       (mem (string->number (match:substring (car interesting) 1)))
+       )
+
+    
+    (display (format  "VMDATA: ~a\n" mem))
+    (display (gc-stats))
+    (if (> mem 100000)
+       (begin
+         (dump-gc-protects)
+         (raise 1)))
     
     ))
 
@@ -467,6 +532,8 @@ The syntax is the same as `define*-public'."
 
 
 (define (multi-fork count)
+  "Split this process in COUNT helpers. Returns either a list of pids,
+or the number of the process."
   (define (helper count acc)
     (if (> count 0)
       (let*
@@ -480,16 +547,28 @@ The syntax is the same as `define*-public'."
 
 (define-public (lilypond-main files)
   "Entry point for LilyPond."
-  
-  (define (no-files-handler)
-    (ly:usage)
-    (exit 2))
 
+  (eval-string (ly:command-line-code))
+
+  (if (ly:get-option 'help)
+      (begin
+       (ly:option-usage)
+       (exit 0)))
+
+  (if (ly:get-option 'show-available-fonts)
+      (begin
+       (ly:font-config-display-fonts)
+       (exit 0)
+       ))
+  
+  
   (if (ly:get-option 'gui)
       (gui-main files))
 
   (if (null? files)
-      (no-files-handler))
+      (begin
+       (ly:usage)
+       (exit 2)))
 
   (if (ly:get-option 'read-file-list)
       (set! files
@@ -501,8 +580,7 @@ The syntax is the same as `define*-public'."
            ))
   
   (if (and (number? (ly:get-option 'job-count))
-          (> (length files) (ly:get-option 'job-count)))
-      
+          (>= (length files) (ly:get-option 'job-count)))
       (let*
          ((count (ly:get-option 'job-count))
           (split-todo (split-list files count)) 
@@ -514,8 +592,9 @@ The syntax is the same as `define*-public'."
        
        (if (number? joblist)
            (begin
-             (ly:set-option 'log-file (format "~a-~a"
-                                              (ly:get-option 'log-file) joblist))
+             (ly:set-option
+              'log-file (format "~a-~a"
+                                (ly:get-option 'log-file) joblist))
              (set! files (vector-ref split-todo joblist)))
 
            (begin
@@ -523,33 +602,52 @@ The syntax is the same as `define*-public'."
              (for-each
               (lambda (pid)
                 (let* ((stat (cdr (waitpid pid))))
-
+                  
                   (if (not (= stat 0))
-                      (set! errors (cons (list-element-index joblist pid) errors)))))
+                      (set! errors
+                            (acons (list-element-index joblist pid)
+                                   stat errors)))))
               joblist)
 
              (for-each
               (lambda (x)
-                (let* ((logfile  (format "~a-~a.log"
-                                         (ly:get-option 'log-file) x))
+                (let* ((job (car x))
+                       (state (cdr x))
+                       (logfile (format "~a-~a.log"
+                                         (ly:get-option 'log-file) job))
                        (log (ly:gulp-file logfile))
                        (len (string-length log))
                        (tail (substring  log (max 0 (- len 1024)))))
 
-                  (display (format "\n\nlogfile ~a:\n\n ~a" logfile tail))))
+                  (if (status:term-sig state)
+                      (ly:message
+                       "\n\n~a\n"
+                       (format (_ "job ~a terminated with signal: ~a")
+                               job (status:term-sig state)))
+                      (ly:message
+                       (_ "logfile ~a (exit ~a):\n~a")
+                       logfile (status:exit-val state) tail))))
 
               errors)
 
              (if (pair? errors)
-                 (ly:error "Children ~a exited with errors." errors))
+                 (ly:error "Children ~a exited with errors." (map car errors)))
+
+             ;; must overwrite individual entries
+             (if (ly:get-option 'dump-profile)
+                 (dump-profile "lily-run-total" '(0 0) (profile-measurements)))
 
            (exit (if (null? errors) 0 1))))))
-             
           
   (if (string-or-symbol? (ly:get-option 'log-file))
       (ly:stderr-redirect (format "~a.log" (ly:get-option 'log-file)) "w"))
   
   (let ((failed (lilypond-all files)))
+    (if (ly:get-option 'trace-scheme-coverage)
+       (begin
+         (coverage:show-all (lambda (f) (string-contains f "lilypond"))
+                            )))
+    
     (if (pair? failed)
        (begin
          (ly:error (_ "failed files: ~S") (string-join failed))
@@ -560,36 +658,69 @@ The syntax is the same as `define*-public'."
          (exit 0)))))
 
 (define-public (lilypond-all files)
-
-
-  (if (ly:get-option 'show-available-fonts)
-      (begin
-       (ly:font-config-display-fonts)
-       (exit 0)
-       ))
-  
   (let* ((failed '())
-        (start-measurements #f)
+        (separate-logs (ly:get-option 'separate-log-files))
+        (ping-log
+         (if separate-logs
+             (open-file (if (string-or-symbol? (ly:get-option 'log-file))
+                            (format "~a.log" (ly:get-option 'log-file))
+                            "/dev/tty") "a") #f))
+        (do-measurements (ly:get-option 'dump-profile))
         (handler (lambda (key failed-file)
                    (set! failed (append (list failed-file) failed)))))
 
+    (gc)
     (for-each
      (lambda (x)
-
-       (gc)
-       (set! start-measurements (profile-measurements))
-       (lilypond-file handler x)
-       (if (ly:get-option 'dump-profile)
-          (dump-profile x start-measurements (profile-measurements)))
-       
-       
-       (ly:clear-anonymous-modules)
-       (if (ly:get-option 'debug-gc)
-          (dump-gc-protects)
-          (if (= (random 40) 1)
-              (ly:reset-all-fonts))))
+       (let*
+          ((start-measurements (if do-measurements
+                                   (profile-measurements)
+                                   #f))
+           (base (dir-basename x ".ly"))
+           (all-settings (ly:all-options)))
+
+        (if separate-logs
+            (ly:stderr-redirect (format "~a.log" base) "w"))
+        (if ping-log
+            (format ping-log "Procesing ~a\n" base))
+             
+        (if (ly:get-option 'trace-memory-frequency) 
+            (mtrace:start-trace  (ly:get-option 'trace-memory-frequency)))
+        
+        (lilypond-file handler x)
+        (if start-measurements
+            (dump-profile x start-measurements (profile-measurements)))
+
+        (if (ly:get-option 'trace-memory-frequency)
+            (begin
+              (mtrace:stop-trace)
+              (mtrace:dump-results base)))
+                
+        (for-each
+         (lambda (s)
+           (ly:set-option (car s) (cdr s)))
+         all-settings)
+
+        (ly:clear-anonymous-modules)
+        (ly:set-option 'debug-gc-assert-parsed-dead #t)
+        (gc)
+        (ly:set-option 'debug-gc-assert-parsed-dead #f)
+
+        
+        (if (ly:get-option 'debug-gc)
+            (dump-gc-protects)
+            (if (= (random 40) 1)
+                (ly:reset-all-fonts)))))
 
      files)
+
+    ;; we want the failed-files notice in the aggregrate logfile.
+    (if ping-log
+       (format ping-log "Failed files: ~a\n" failed))
+        
+    (if (ly:get-option 'dump-profile)
+       (dump-profile "lily-run-total" '(0 0) (profile-measurements)))
+
     failed))
 
 (define (lilypond-file handler file-name)
@@ -604,7 +735,7 @@ The syntax is the same as `define*-public'."
       (gui-no-files-handler))
 
   (if (not (string? (ly:get-option 'log-file)))
-      (let* ((base (basename (car files) ".ly"))
+      (let* ((base (dir-basename (car files) ".ly"))
             (log-name (string-append base ".log")))
        (if (not (ly:get-option 'gui))
            (ly:message (_ "Redirecting output to ~a...") log-name))