1 ;;;; This file is part of LilyPond, the GNU music typesetter.
3 ;;;; Copyright (C) 1998--2010 Jan Nieuwenhuizen <janneke@gnu.org>
4 ;;;; Han-Wen Nienhuys <hanwen@xs4all.nl>
6 ;;;; LilyPond is free software: you can redistribute it and/or modify
7 ;;;; it under the terms of the GNU General Public License as published by
8 ;;;; the Free Software Foundation, either version 3 of the License, or
9 ;;;; (at your option) any later version.
11 ;;;; LilyPond is distributed in the hope that it will be useful,
12 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ;;;; GNU General Public License for more details.
16 ;;;; You should have received a copy of the GNU General Public License
17 ;;;; along with LilyPond. If not, see <http://www.gnu.org/licenses/>.
19 ;; Internationalisation: (_i "to be translated") gets an entry in the
20 ;; POT file; (gettext ...) must be invoked explicitly to do the actual
23 ;; (define-macro (_i x) x)
24 ;; (define-macro-public _i (x) x)
25 ;; (define-public-macro _i (x) x)
28 (defmacro-public _i (x) x)
30 (read-enable 'positions)
33 (define-public PLATFORM
36 (car (string-tokenize (utsname:sysname (uname)))))))
38 (define scheme-options-definitions
42 ;; - [subject-]object-object-verb +"ing"
43 ;; - [subject-]-verb-object-object
45 ;; Avoid overlong lines in `lilypond -dhelp'! Strings should not
46 ;; be longer than 48 characters per line.
49 "Render at higher resolution (using given factor)
50 and scale down result to prevent jaggies in
53 "Create .tex, .texi, .count files in the
56 "Select backend. Possible values: 'eps, 'null,
57 'ps, 'scm, 'socket, 'svg.")
58 (check-internal-types #f
59 "Check every property assignment for types.")
61 "Generate cut-out snippets of a score.")
63 "LilyPond prefix for data files (read-only).")
65 "Dump memory debugging statistics.")
66 (debug-gc-assert-parsed-dead #f
67 "For memory debugging: Ensure that all
68 references to parsed objects are dead. This is
69 an internal option, and is switched on
70 automatically for `-ddebug-gc'.")
72 "Debug the flex lexer.")
73 (debug-page-breaking-scoring #f
74 "Dump scores for many different page breaking
77 "Debug the bison parser.")
78 (debug-property-callbacks #f
79 "Debug cyclic callback chains.")
82 (delete-intermediate-files #t
83 "Delete unusable, intermediate PostScript files.")
85 "Dump memory and time information for each file.")
87 "Dump timing information (system-dependent).")
89 "Dump output signatures of each system. Used for
92 "Pad left edge of the output EPS bounding box by
93 given amount (in mm).")
95 "Load fonts via Ghostscript.")
96 (gs-load-lily-fonts #f
97 "Load only LilyPond fonts via Ghostscript.")
99 "Run LilyPond from a GUI and redirect stderr to
103 (include-book-title-preview #t
104 "Include book titles in preview images.")
105 (include-eps-fonts #t
106 "Include fonts in separate-system EPS files.")
108 "Include file for global settings, included before the score is processed.")
110 "Process in parallel, using the given number of
113 "If string FOO is given as argument, redirect
114 output to log file `FOO.log'.")
115 (midi-extension ,(if (eq? PLATFORM 'windows)
118 "Set the default file extension for MIDI output
119 file to given string.")
120 (music-strings-to-paths #f
121 "Convert text strings to paths when glyphs belong
124 "Make \\relative mode for simultaneous music work
125 similar to chord syntax.")
127 "Add point & click links to PDF output.")
129 "Set default paper size.")
130 (pixmap-format "png16m"
131 "Set GhostScript's output format for pixel images.")
133 "Create preview images also.")
135 "Print pages in the normal way.")
136 (protected-scheme-parsing #t
137 "Continue when errors in inline scheme are caught
138 in the parser. If #f, halt on errors and print
140 (profile-property-accesses #f
141 "Keep statistics of get_property() calls.")
143 "Set resolution for generating PNG pixmaps to
144 given value (in dpi).")
146 "Specify name of a file which contains a list of
147 input files to be processed.")
148 (relative-includes #f
149 "When processing an \\include command, look for
150 the included file relative to the current file
151 (instead of the root file)")
153 "Run in safer mode.")
154 (separate-log-files #f
155 "For input files `FILE1.ly', `FILE2.ly', ...
156 output log data to files `FILE1.log',
158 (show-available-fonts #f
159 "List available font names.")
160 (strict-infinity-checking #f
161 "Force a crash on encountering Inf and NaN
162 floating point exceptions.")
164 "Don't use directories from input files while
165 constructing output file names.")
167 "Use woff font files in SVG backend.")
168 (trace-memory-frequency #f
169 "Record Scheme cell usage this many times per
170 second. Dump results to `FILE.stacks' and
172 (trace-scheme-coverage #f
173 "Record coverage of Scheme files in `FILE.cov'.")
174 (verbose ,(ly:command-line-verbose?)
175 "Value of the --verbose flag (read-only).")
177 "Change all warning and programming_error
178 messages into errors.")
181 ;; Need to do this in the beginning. Other parts of the Scheme
182 ;; initialization depend on these options.
184 (for-each (lambda (x)
185 (ly:add-option (car x) (cadr x) (caddr x)))
186 scheme-options-definitions)
188 (for-each (lambda (x)
189 (ly:set-option (car x) (cdr x)))
190 (eval-string (ly:command-line-options)))
194 (if (defined? 'set-debug-cell-accesses!)
195 (set-debug-cell-accesses! #f))
197 ;(set-debug-cell-accesses! 1000)
199 (use-modules (ice-9 regex)
212 (define-public fancy-format
215 (define-public (ergonomic-simple-format dest . rest)
216 "Like ice-9 format, but without the memory consumption."
218 (apply simple-format (cons #f (cons dest rest)))
219 (apply simple-format (cons dest rest))))
222 ergonomic-simple-format)
225 (define-public (myd k v)
232 (define-public (print . args)
233 (apply format (cons (current-output-port) args)))
236 ;;; General settings.
238 ;;; Debugging evaluator is slower. This should have a more sensible
241 (if (or (ly:get-option 'verbose)
242 (ly:get-option 'trace-memory-frequency)
243 (ly:get-option 'trace-scheme-coverage))
245 (ly:set-option 'protected-scheme-parsing #f)
246 (debug-enable 'debug)
247 (debug-enable 'backtrace)
248 (read-enable 'positions)))
250 (if (ly:get-option 'trace-scheme-coverage)
253 (define-public parser #f)
255 (define music-string-to-path-backends
258 (if (memq (ly:get-option 'backend) music-string-to-path-backends)
259 (ly:set-option 'music-strings-to-paths #t))
261 (define-public _ gettext)
263 (define-public (ly:load x)
264 (let* ((file-name (%search-load-path x)))
265 (if (ly:get-option 'verbose)
266 (ly:progress "[~A" file-name))
268 (ly:error (_ "cannot find: ~A") x))
269 (primitive-load file-name)
270 (if (ly:get-option 'verbose)
271 (ly:progress "]\n"))))
274 (let ((platform (string-tokenize
275 (vector-ref (uname) 0) char-set:letter+digit)))
276 (if (null? (cdr platform)) #f
277 (member (string-downcase (cadr platform)) '("95" "98" "me")))))
280 (if (string-index x #\\)
282 (string-regexp-substitute
284 (string-regexp-substitute "\\\\" "/" x))))
286 (define-public (ly-getcwd)
287 (if (eq? PLATFORM 'windows)
291 (define-public (is-absolute? file-name)
292 (let ((file-name-length (string-length file-name)))
293 (if (= file-name-length 0)
295 (or (eq? (string-ref file-name 0) #\/)
296 (and (eq? PLATFORM 'windows)
297 (> file-name-length 2)
298 (eq? (string-ref file-name 1) #\:)
299 (eq? (string-ref file-name 2) #\/))))))
301 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
303 (define (type-check-list location signature arguments)
304 "Typecheck a list of arguments against a list of type predicates.
305 Print a message at LOCATION if any predicate failed."
306 (define (recursion-helper signature arguments count)
307 (define (helper pred? arg count)
308 (if (not (pred? arg))
313 #f (_ "wrong type for argument ~a. Expecting ~a, found ~s")
314 count (type-name pred?) arg))
318 (if (null? signature)
320 (and (helper (car signature) (car arguments) count)
321 (recursion-helper (cdr signature) (cdr arguments) (1+ count)))))
322 (recursion-helper signature arguments 1))
325 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
326 ;; Safe definitions utility
331 (define-macro (define-safe-public arglist . body)
332 "Define a variable, export it, and mark it as safe, i.e. usable in
333 LilyPond safe mode. The syntax is the same as `define*-public'."
334 (define (get-symbol arg)
336 (get-symbol (car arg))
339 (let ((safe-symbol (get-symbol arglist)))
341 (define*-public ,arglist
343 (set! safe-objects (cons (cons ',safe-symbol ,safe-symbol)
347 (define-safe-public (lilypond-version)
349 (map (lambda (x) (if (symbol? x)
355 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
358 (ly:set-default-scale (ly:make-scale #(0 1 2 5/2 7/2 9/2 11/2)))
360 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
363 (define init-scheme-files
366 "define-event-classes.scm"
367 "define-music-callbacks.scm"
368 "define-music-types.scm"
371 "chord-ignatzek-names.scm"
373 "chord-generic-names.scm"
376 "music-functions.scm"
379 "define-music-properties.scm"
380 "time-signature-settings.scm"
384 "parser-ly-from-scheme.scm"
385 "ly-syntax-constructors.scm"
387 "define-context-properties.scm"
388 "translation-functions.scm"
400 "define-woodwind-diagrams.scm"
401 "display-woodwind-diagrams.scm"
402 "predefined-fretboards.scm"
403 "define-markup-commands.scm"
404 "define-grob-properties.scm"
406 "define-grob-interfaces.scm"
407 "define-stencil-commands.scm"
411 "backend-library.scm"
415 ;; must be after everything has been defined
418 (for-each ly:load init-scheme-files)
420 (define-public r5rs-primary-predicates
421 `((,boolean? . "boolean")
422 (,char? . "character")
423 (,number? . "number")
426 (,procedure? . "procedure")
427 (,string? . "string")
428 (,symbol? . "symbol")
429 (,vector? . "vector")))
431 (define-public r5rs-secondary-predicates
432 `((,char-alphabetic? . "alphabetic character")
433 (,char-lower-case? . "lower-case character")
434 (,char-numeric? . "numeric character")
435 (,char-upper-case? . "upper-case character")
436 (,char-whitespace? . "whitespace character")
438 (,complex? . "complex number")
439 (,even? . "even number")
440 (,exact? . "exact number")
441 (,inexact? . "inexact number")
442 (,integer? . "integer")
443 (,negative? . "negative number")
444 (,odd? . "odd number")
445 (,positive? . "positive number")
446 (,rational? . "rational number")
447 (,real? . "real number")
453 (,input-port? . "input port")
454 (,output-port? . "output port")
456 ;; would this ever be used?
457 (,eof-object? . "end-of-file object")
460 (define-public guile-predicates
461 `((,hash-table? . "hash table")
464 (define-public lilypond-scheme-predicates
465 `((,boolean-or-symbol? . "boolean or symbol")
467 (,cheap-list? . "list")
468 (,grob-list? . "list of grobs")
469 ;; this is built on cheap-list
470 (,list-or-symbol? . "list or symbol")
471 (,markup? . "markup")
472 (,markup-command-list? . "markup command list")
473 (,markup-list? . "markup list")
474 (,moment-pair? . "pair of moment objects")
475 (,number-or-grob? . "number or grob")
476 (,number-or-string? . "number or string")
477 (,number-pair? . "pair of numbers")
478 (,rhythmic-location? . "rhythmic location")
479 (,scheme? . "any type")
480 (,string-or-pair? . "string or pair")
481 (,string-or-symbol? . "string or symbol")
484 (define-public lilypond-exported-predicates
486 (,ly:context? . "context")
487 (,ly:dimension? . "dimension, in staff space")
488 (,ly:dir? . "direction")
489 (,ly:dispatcher? . "dispatcher")
490 (,ly:duration? . "duration")
491 (,ly:font-metric? . "font metric")
492 (,ly:grob? . "graphical (layout) object")
493 (,ly:grob-array? . "array of grobs")
494 (,ly:input-location? . "input location")
496 (,ly:iterator? . "iterator")
497 (,ly:lily-lexer? . "lily-lexer")
498 (,ly:lily-parser? . "lily-parser")
499 (,ly:listener? . "listener")
500 (,ly:moment? . "moment")
501 (,ly:music? . "music")
502 (,ly:music-function? . "music function")
503 (,ly:music-list? . "list of music objects")
504 (,ly:music-output? . "music output")
505 (,ly:otf-font? . "OpenType font")
506 (,ly:output-def? . "output definition")
507 (,ly:page-marker? . "page marker")
508 (,ly:pango-font? . "pango font")
509 (,ly:paper-book? . "paper book")
510 (,ly:paper-system? . "paper-system Prob")
511 (,ly:pitch? . "pitch")
512 (,ly:prob? . "property object")
513 (,ly:score? . "score")
514 (,ly:simple-closure? . "simple closure")
515 (,ly:skyline? . "skyline")
516 (,ly:skyline-pair? . "pair of skylines")
517 (,ly:source-file? . "source file")
518 (,ly:spanner? . "spanner")
519 (,ly:stencil? . "stencil")
520 (,ly:stream-event? . "stream event")
521 (,ly:translator? . "translator")
522 (,ly:translator-group? . "translator group")
526 (set! type-p-name-alist
527 (append r5rs-primary-predicates
528 r5rs-secondary-predicates
530 lilypond-scheme-predicates
531 lilypond-exported-predicates))
534 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
537 (define (profile-measurements)
540 (list (- (+ (tms:cutime t)
542 (assoc-get 'gc-time-taken stats))
543 (assoc-get 'total-cells-allocated stats 0))))
545 (define (dump-profile base last this)
546 (let* ((outname (format "~a.profile" (dir-basename base ".ly")))
547 (diff (map (lambda (y) (apply - y)) (zip this last))))
548 (ly:progress "\nWriting timing to ~a..." outname)
549 (format (open-file outname "w")
550 "time: ~a\ncells: ~a\n"
551 (if (ly:get-option 'dump-cpu-profile)
556 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
557 ;; debug memory leaks
562 (define gc-protect-stat-count
565 (define-public (dump-live-object-stats outfile)
566 (for-each (lambda (x)
567 (format outfile "~a: ~a\n" (car x) (cdr x)))
568 (sort (gc-live-object-stats)
570 (string<? (car x) (car y))))))
572 (define-public (dump-gc-protects)
573 (set! gc-protect-stat-count (1+ gc-protect-stat-count))
574 (let* ((protects (sort (hash-table->alist (ly:protects))
576 (< (object-address (car a))
577 (object-address (car b))))))
578 (out-file-name (string-append
579 "gcstat-" (number->string gc-protect-stat-count)
581 (outfile (open-file out-file-name "w")))
583 (display (format "Dumping GC statistics ~a...\n" out-file-name))
584 (display (map (lambda (y)
588 (format "~a (~a) = ~a\n" (object-address x) c x)
592 (not (symbol? (car x))))
595 (format outfile "\nprotected symbols: ~a\n"
596 (apply + (map (lambda (obj-count)
597 (if (symbol? (car obj-count))
602 ;; (display (ly:smob-protects))
604 (if (defined? 'gc-live-object-stats)
606 (display "Live object statistics: GC'ing\n")
610 (display "Asserting dead objects\n")
611 (ly:set-option 'debug-gc-assert-parsed-dead #t)
613 (ly:set-option 'debug-gc-assert-parsed-dead #f)
614 (set! stats (gc-live-object-stats))
615 (display "Dumping live object statistics.\n")
616 (dump-live-object-stats outfile)))
618 (let* ((stats (gc-stats)))
619 (for-each (lambda (sym)
622 gc-protect-stat-count
624 (assoc-get sym stats "?"))
627 '(protected-objects bytes-malloced cell-heap-size)))
629 (close-port outfile)))
631 (define (check-memory)
632 "Read `/proc/self' to check up on memory use."
633 (define (gulp-file name)
634 (let* ((file (open-input-file name))
635 (text (read-delimited "" file)))
639 (let* ((stat (gulp-file "/proc/self/status"))
640 (lines (string-split stat #\newline))
641 (interesting (filter identity
644 (string-match "^VmData:[ \t]*([0-9]*) kB" l))
646 (mem (string->number (match:substring (car interesting) 1))))
647 (display (format "VMDATA: ~a\n" mem))
650 (begin (dump-gc-protects)
653 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
655 (define (multi-fork count)
656 "Split this process into COUNT helpers. Returns either a list of
657 PIDs or the number of the process."
658 (define (helper count acc)
660 (let* ((pid (primitive-fork)))
663 (helper (1- count) (cons pid acc))))
668 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
670 (define* (ly:exit status #:optional (silently #f) )
671 "Exit function for lilypond"
674 ((0) (ly:success "Compilation successfully completed"))
675 ((1) (ly:warning "Compilation completed with warnings or errors"))
676 (else (ly:message "")))
680 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
682 (define-public (lilypond-main files)
683 "Entry point for LilyPond."
684 (eval-string (ly:command-line-code))
685 (if (ly:get-option 'help)
686 (begin (ly:option-usage)
688 (if (ly:get-option 'show-available-fonts)
689 (begin (ly:font-config-display-fonts)
691 (if (ly:get-option 'gui)
696 (if (ly:get-option 'read-file-list)
699 (> (string-length s) 0))
702 (string-split (ly:gulp-file f) #\nl))
704 (if (and (number? (ly:get-option 'job-count))
705 (>= (length files) (ly:get-option 'job-count)))
706 (let* ((count (ly:get-option 'job-count))
707 (split-todo (split-list files count))
708 (joblist (multi-fork count))
710 (if (not (string-or-symbol? (ly:get-option 'log-file)))
711 (ly:set-option 'log-file "lilypond-multi-run"))
712 (if (number? joblist)
713 (begin (ly:set-option
714 'log-file (format "~a-~a"
715 (ly:get-option 'log-file) joblist))
716 (set! files (vector-ref split-todo joblist)))
717 (begin (ly:progress "\nForking into jobs: ~a\n" joblist)
720 (let* ((stat (cdr (waitpid pid))))
723 (acons (list-element-index joblist pid)
730 (logfile (format "~a-~a.log"
731 (ly:get-option 'log-file) job))
732 (log (ly:gulp-file logfile))
733 (len (string-length log))
734 (tail (substring log (max 0 (- len 1024)))))
735 (if (status:term-sig state)
738 (format (_ "job ~a terminated with signal: ~a")
739 job (status:term-sig state)))
741 (_ "logfile ~a (exit ~a):\n~a")
742 logfile (status:exit-val state) tail))))
745 (ly:error "Children ~a exited with errors."
747 ;; must overwrite individual entries
748 (if (ly:get-option 'dump-profile)
749 (dump-profile "lily-run-total"
750 '(0 0) (profile-measurements)))
755 (if (string-or-symbol? (ly:get-option 'log-file))
756 (ly:stderr-redirect (format "~a.log" (ly:get-option 'log-file)) "w"))
757 (let ((failed (lilypond-all files)))
758 (if (ly:get-option 'trace-scheme-coverage)
760 (coverage:show-all (lambda (f)
761 (string-contains f "lilypond")))))
763 (begin (ly:error (_ "failed files: ~S") (string-join failed))
769 (define-public (lilypond-all files)
771 (separate-logs (ly:get-option 'separate-log-files))
774 (open-file (if (string-or-symbol? (ly:get-option 'log-file))
775 (format "~a.log" (ly:get-option 'log-file))
776 "/dev/tty") "a") #f))
777 (do-measurements (ly:get-option 'dump-profile))
778 (handler (lambda (key failed-file)
779 (set! failed (append (list failed-file) failed)))))
783 (let* ((start-measurements (if do-measurements
784 (profile-measurements)
786 (base (dir-basename x ".ly"))
787 (all-settings (ly:all-options)))
789 (ly:stderr-redirect (format "~a.log" base) "w"))
791 (format ping-log "Processing ~a\n" base))
792 (if (ly:get-option 'trace-memory-frequency)
793 (mtrace:start-trace (ly:get-option 'trace-memory-frequency)))
794 (lilypond-file handler x)
795 (if start-measurements
796 (dump-profile x start-measurements (profile-measurements)))
797 (if (ly:get-option 'trace-memory-frequency)
798 (begin (mtrace:stop-trace)
799 (mtrace:dump-results base)))
800 (for-each (lambda (s)
801 (ly:set-option (car s) (cdr s)))
803 (ly:set-option 'debug-gc-assert-parsed-dead #t)
805 (ly:set-option 'debug-gc-assert-parsed-dead #f)
806 (if (ly:get-option 'debug-gc)
808 (ly:reset-all-fonts))))
811 ;; we want the failed-files notice in the aggregrate logfile.
813 (format ping-log "Failed files: ~a\n" failed))
814 (if (ly:get-option 'dump-profile)
815 (dump-profile "lily-run-total" '(0 0) (profile-measurements)))
818 (define (lilypond-file handler file-name)
819 (catch 'ly-file-failed
820 (lambda () (ly:parse-file file-name))
821 (lambda (x . args) (handler x file-name))))
823 (use-modules (scm editor))
825 (define-public (gui-main files)
827 (gui-no-files-handler))
828 (if (not (string? (ly:get-option 'log-file)))
829 (let* ((base (dir-basename (car files) ".ly"))
830 (log-name (string-append base ".log")))
831 (if (not (ly:get-option 'gui))
832 (ly:message (_ "Redirecting output to ~a...") log-name))
833 (ly:stderr-redirect log-name "w")
834 (ly:message "# -*-compilation-*-"))
835 (let ((failed (lilypond-all files)))
839 (ly:stderr-redirect "foo" "r")
840 (system (get-editor-command log-name 0 0 0))
841 (ly:error (_ "failed files: ~S") (string-join failed))
846 (define (gui-no-files-handler)
847 (let* ((ly (string-append (ly:effective-prefix) "/ly/"))
848 ;; FIXME: soft-code, localize
849 (welcome-ly (string-append ly "Welcome_to_LilyPond.ly"))
850 (cmd (get-editor-command welcome-ly 0 0 0)))
851 (ly:message (_ "Invoking `~a'...\n") cmd)