1 ;;;; This file is part of LilyPond, the GNU music typesetter.
3 ;;;; Copyright (C) 1998--2015 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 ;;; Boolean thunk - are we integrating Guile V2.0 or higher with LilyPond?
31 (define-public (guile-v2)
32 (string>? (version) "1.9.10"))
34 (read-enable 'positions)
38 (debug-enable 'backtrace)
39 (debug-set! show-file-name #t)))
41 (define-public PLATFORM
44 (car (string-tokenize (utsname:sysname (uname)) char-set:letter)))))
46 ;; We don't use (srfi srfi-39) (parameter objects) here because that
47 ;; does not give us a name/handle to the underlying fluids themselves.
49 (define %parser (make-fluid))
50 (define %location (make-fluid))
51 ;; No public setters: should not get overwritten in action
52 (define-public (*parser*) (fluid-ref %parser))
53 (define-public (*location*) (fluid-ref %location))
54 ;; but properly scoped location should be fine
55 (defmacro-public with-location (loc . body)
56 `(with-fluids ((,%location ,loc)) ,@body))
58 ;; It would be nice to convert occurences of parser/location to
59 ;; (*parser*)/(*location*) using the syncase module but it is utterly
60 ;; broken in GUILE 1 and would require changing a lot of unrelated
61 ;; innocuous constructs which just happen to fall apart with
62 ;; inscrutable error messages.
65 ;; Session-handling variables and procedures.
67 ;; A "session" corresponds to one .ly file processed on a LilyPond
68 ;; command line. Every session gets to see a reasonably fresh state
69 ;; of LilyPond and should work independently from previous files.
71 ;; Session management relies on cooperation, namely the user not
72 ;; trying to change variables and data structures internal to
73 ;; LilyPond. It is not proof against in-place modification of data
74 ;; structures (as they are just reinitialized with the original
75 ;; identities), and it is not proof against tampering with internals.
77 ;; As a consequence, session management is not sufficient for
78 ;; separating multiple independent .ly files in "-dsafe" mode: you
79 ;; should give each its own LilyPond process when reliable separation
82 ;; For standard tasks and programming practices, multiple sessions in
83 ;; the same LilyPond job should work reasonably independently and
84 ;; without "bleed-over" while still loading and compiling the
85 ;; relevant .scm and .ly files only once.
88 (define lilypond-declarations '())
89 (define lilypond-exports '())
90 (define after-session-hook (make-hook))
92 (define-public (call-after-session thunk)
93 (if (ly:undead? lilypond-declarations)
94 (ly:error (_ "call-after-session used after session start")))
95 (add-hook! after-session-hook thunk #t))
97 (define (make-session-variable name value)
98 (if (ly:undead? lilypond-declarations)
99 (ly:error (_ "define-session used after session start")))
100 (let ((var (module-make-local-var! (current-module) name)))
101 (if (variable-bound? var)
102 (ly:error (_ "symbol ~S redefined") name))
103 (variable-set! var value)
106 (defmacro define-session (name value)
107 "This defines a variable @var{name} with the starting value
108 @var{value} that is reinitialized at the start of each session.
109 A@tie{}session basically corresponds to one LilyPond file on the
110 command line. The value is recorded at the start of the first session
111 after loading all initialization files and before loading the user
112 file and is reinstated for all of the following sessions. This
113 happens just by replacing the value, not by copying structures, so you
114 should not destructively modify them. For example, lists defined in
115 this manner should be changed within a session only be adding material
116 to their front or replacing them altogether, not by modifying parts of
117 them. It is an error to call @code{define-session} after the first
118 session has started."
119 (define (add-session-variable name value)
120 (set! lilypond-declarations
121 (cons (make-session-variable name value) lilypond-declarations)))
122 `(,add-session-variable ',name ,value))
124 (defmacro define-session-public (name value)
125 "Like @code{define-session}, but also exports @var{name} into parser modules."
126 (define (add-session-variable name value)
127 (set! lilypond-exports
128 (acons name (make-session-variable name value) lilypond-exports)))
130 ;; this is a bit icky: we place the variable right into every
131 ;; parser module so that both set! and define will affect the
132 ;; original variable in the (lily) module. However, we _also_
133 ;; export it normally from (lily) for the sake of other modules
134 ;; not sharing the name space of the parser.
135 (,add-session-variable ',name ,value)
138 (define (session-terminate)
139 (if (ly:undead? lilypond-declarations)
142 (lambda (p) (variable-set! (cadr p) (cddr p)))
143 (ly:get-undead lilypond-declarations))
144 (run-hook after-session-hook))))
146 (define lilypond-interfaces #f)
148 (define-public (session-initialize thunk)
149 "Initialize this session. The first session in a LilyPond run is
150 initialized by calling @var{thunk}, then recording the values of all
151 variables in the current module as well as those defined with
152 @code{define-session}. Subsequent calls of @code{session-initialize}
153 ignore @var{thunk} and instead just reinitialize all recorded
154 variables to their value after the initial call of @var{thunk}."
156 ;; We need to save the variables of the current module along with
157 ;; their values: functions defined in the module might refer to the
160 ;; The entries in lilypond-declarations consist of a cons* consisting
161 ;; of symbol, variable, and value. Variables defined with
162 ;; define-session have the symbol set to #f.
164 (if (ly:undead? lilypond-declarations)
166 (module-use-interfaces! (current-module) (reverse lilypond-interfaces))
171 (variable-set! var val)
173 (module-add! (current-module) (car p) var))))
174 (ly:get-undead lilypond-declarations)))
176 ;; import all public session variables natively into parser
177 ;; module. That makes them behave identically under define/set!
178 (for-each (lambda (v)
179 (module-add! (current-module) (car v) (cdr v)))
181 ;; Initialize first session
183 ;; lilypond-exports is no longer needed since we will grab its
184 ;; values from (current-module).
185 (set! lilypond-exports #f)
186 (set! lilypond-interfaces
187 (filter (lambda (m) (eq? 'interface (module-kind m)))
188 (module-uses (current-module))))
189 (let ((decl (map! (lambda (v)
190 (cons* #f v (variable-ref v)))
191 lilypond-declarations)))
194 (let ((val (variable-ref v)))
195 (if (not (ly:lily-parser? val))
201 (set! lilypond-declarations (ly:make-undead decl))))))
203 (define scheme-options-definitions
207 ;; - [subject-]object-object-verb +"ing"
208 ;; - [subject-]-verb-object-object
210 ;; Avoid overlong lines in `lilypond -dhelp'! Strings should not
211 ;; be longer than 48 characters per line.
214 "Render at higher resolution (using given factor)
215 and scale down result to prevent jaggies in
219 "Create .tex, .texi, .count files in the
223 "Select backend. Possible values: 'eps, 'null,
224 'ps, 'scm, 'socket, 'svg.")
225 (check-internal-types
227 "Check every property assignment for types.")
230 "Generate cut-out snippets of a score.")
233 "LilyPond prefix for data files (read-only).")
236 "Dump memory debugging statistics.")
237 (debug-gc-assert-parsed-dead
239 "For memory debugging: Ensure that all
240 references to parsed objects are dead. This is
241 an internal option, and is switched on
242 automatically for `-ddebug-gc'.")
245 "Debug the flex lexer.")
246 (debug-page-breaking-scoring
248 "Dump scores for many different page breaking
252 "Debug the bison parser.")
253 (debug-property-callbacks
255 "Debug cyclic callback chains.")
259 (delete-intermediate-files
261 "Delete unusable, intermediate PostScript files.")
264 "Dump memory and time information for each file.")
267 "Dump timing information (system-dependent).")
270 "Dump output signatures of each system. Used for
271 regression testing.")
274 "Embed the source files inside the generated PDF document.")
277 "Pad left edge of the output EPS bounding box by
278 given amount (in mm).")
281 "Load fonts via Ghostscript.")
284 "Load only LilyPond fonts via Ghostscript.")
285 (gs-never-embed-fonts
287 "Make Ghostscript embed only TrueType fonts and no other font format.")
290 "Run LilyPond from a GUI and redirect stderr to
295 (include-book-title-preview
297 "Include book titles in preview images.")
300 "Include fonts in separate-system EPS files.")
303 "Include file for global settings, included before the score is processed.")
306 "Process in parallel, using the given number of
310 "If string FOO is given as argument, redirect
311 output to log file `FOO.log'.")
314 "Maximum depth for the markup tree. If a markup has more levels,
315 assume it will not terminate on its own, print a warning and return a
316 null markup instead.")
317 (midi-extension ,(if (eq? PLATFORM 'windows)
320 "Set the default file extension for MIDI output
321 file to given string.")
322 (music-strings-to-paths
324 "Convert text strings to paths when glyphs belong
328 "Add point & click links to PDF and SVG output.")
331 "Set default paper size.")
334 "Set GhostScript's output format for pixel images.")
337 "Create preview images also.")
340 "Print pages in the normal way.")
341 (protected-scheme-parsing
343 "Continue when errors in inline scheme are caught
344 in the parser. If #f, halt on errors and print
346 (profile-property-accesses
348 "Keep statistics of get_property() calls.")
351 "Set resolution for generating PNG pixmaps to
352 given value (in dpi).")
355 "Specify name of a file which contains a list of
356 input files to be processed.")
359 "When processing an \\include command, look for
360 the included file relative to the current file\
361 \n(instead of the root file)")
364 "Run in safer mode.")
367 "For input files `FILE1.ly', `FILE2.ly', ...
368 output log data to files `FILE1.log',
370 (show-available-fonts
372 "List available font names.")
373 (strict-infinity-checking
375 "Force a crash on encountering Inf and NaN
376 floating point exceptions.")
379 "Don't use directories from input files while
380 constructing output file names.")
383 "Set the PostScript strokeadjust operator explicitly.
384 This employs different drawing primitives, resulting in
385 large PDF file size increases but often markedly better
389 "Use woff font files in SVG backend.")
390 (trace-memory-frequency
392 "Record Scheme cell usage this many times per
393 second. Dump results to `FILE.stacks' and
395 (trace-scheme-coverage
397 "Record coverage of Scheme files in `FILE.cov'.")
398 (verbose ,(ly:verbose-output?)
399 "Verbose output, i.e. loglevel at least DEBUG (read-only).")
402 "Change all warning and programming_error
403 messages into errors.")
406 ;; Need to do this in the beginning. Other parts of the Scheme
407 ;; initialization depend on these options.
409 (for-each (lambda (x)
410 (ly:add-option (car x) (cadr x) (caddr x)))
411 scheme-options-definitions)
413 (for-each (lambda (x)
414 (ly:set-option (car x) (cdr x)))
415 (eval-string (ly:command-line-options)))
419 (if (defined? 'set-debug-cell-accesses!)
420 (set-debug-cell-accesses! #f))
422 ;;(set-debug-cell-accesses! 1000)
424 (use-modules (ice-9 regex)
436 (scm safe-utility-defs))
438 (define-public _ gettext)
439 ;;; There are new modules defined in Guile V2.0 which we need to use.
441 ;; Modules and scheme files loaded by lily.scm use currying
442 ;; in Guile V2 this needs a module which is not present in Guile V1.8
447 (ly:debug (_ "Using (ice-9 curried-definitions) module\n"))
448 (use-modules (ice-9 curried-definitions)))
450 (ly:debug (_ "Guile 1.8\n"))))
452 ;; TODO add in modules for V1.8.7 deprecated in V2.0 and integrated
453 ;; into Guile base code, like (ice-9 syncase).
456 (define-public fancy-format
459 (define-public (ergonomic-simple-format dest . rest)
460 "Like ice-9's @code{format}, but without the memory consumption."
462 (apply simple-format #f dest rest)
463 (apply simple-format dest rest)))
466 ergonomic-simple-format)
469 (define-public (myd k v)
476 (define-public (print . args)
477 (apply format (current-output-port) args))
480 ;;; General settings.
482 ;;; Debugging evaluator is slower. This should have a more sensible
486 (if (or (ly:get-option 'verbose)
487 (ly:get-option 'trace-memory-frequency)
488 (ly:get-option 'trace-scheme-coverage))
490 (ly:set-option 'protected-scheme-parsing #f)
491 (debug-enable 'backtrace)
492 (read-enable 'positions)))
494 (if (ly:get-option 'trace-scheme-coverage)
497 (define music-string-to-path-backends
500 (if (memq (ly:get-option 'backend) music-string-to-path-backends)
501 (ly:set-option 'music-strings-to-paths #t))
503 (define-public (ly:load x)
504 (let* ((file-name (%search-load-path x)))
505 (ly:debug "[~A" file-name)
507 (ly:error (_ "cannot find: ~A") x))
508 (primitive-load-path file-name) ;; to support Guile V2 autocompile
509 ;; TODO: Any chance to use ly:debug here? Need to extend it to prevent
510 ;; a newline in this case
511 (if (ly:get-option 'verbose)
512 (ly:progress "]\n"))))
515 (let ((platform (string-tokenize
516 (vector-ref (uname) 0) char-set:letter+digit)))
517 (if (null? (cdr platform)) #f
518 (member (string-downcase (cadr platform)) '("95" "98" "me")))))
521 (if (string-index x #\\)
523 (string-regexp-substitute
525 (string-regexp-substitute "\\\\" "/" x))))
527 (define-public (ly-getcwd)
528 (if (eq? PLATFORM 'windows)
532 (define-public (is-absolute? file-name)
533 (let ((file-name-length (string-length file-name)))
534 (if (= file-name-length 0)
536 (or (eq? (string-ref file-name 0) #\/)
537 (and (eq? PLATFORM 'windows)
538 (> file-name-length 2)
539 (eq? (string-ref file-name 1) #\:)
540 (or (eq? (string-ref file-name 2) #\\)
541 (eq? (string-ref file-name 2) #\/)))))))
543 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
544 ;;; If necessary, emulate Guile V2 module_export_all! for Guile V1.8.n
547 (define (module-export-all! mod)
548 (define (fresh-interface!)
549 (let ((iface (make-module)))
550 (set-module-name! iface (module-name mod))
551 ;; for guile 2: (set-module-version! iface (module-version mod))
552 (set-module-kind! iface 'interface)
553 (set-module-public-interface! mod iface)
555 (let ((iface (or (module-public-interface mod)
556 (fresh-interface!))))
557 (set-module-obarray! iface (module-obarray mod))))))
560 (define-safe-public (lilypond-version)
562 (map (lambda (x) (if (symbol? x)
568 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
571 (ly:set-default-scale (ly:make-scale #(0 1 2 5/2 7/2 9/2 11/2)))
573 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
577 ;; List of Scheme files to be loaded into the (lily) module.
579 ;; - Library definitions, need to be at the head of the list
580 (define init-scheme-files-lib
583 ;; - Files containing definitions used later by other files later in load
584 (define init-scheme-files-used
585 '("markup-macros.scm"
586 "parser-ly-from-scheme.scm"))
587 ;; - Main body of files to be loaded
588 (define init-scheme-files-body
590 "define-event-classes.scm"
591 "define-music-callbacks.scm"
592 "define-music-types.scm"
593 "define-note-names.scm"
598 "define-markup-commands.scm"
600 "modal-transforms.scm"
601 "chord-generic-names.scm"
602 "chord-ignatzek-names.scm"
603 "music-functions.scm"
606 "define-music-properties.scm"
608 "time-signature-settings.scm"
613 "define-context-properties.scm"
614 "translation-functions.scm"
628 "define-woodwind-diagrams.scm"
629 "display-woodwind-diagrams.scm"
630 "predefined-fretboards.scm"
631 "define-grob-properties.scm"
633 "define-grob-interfaces.scm"
634 "define-stencil-commands.scm"
635 "scheme-engravers.scm"
640 "backend-library.scm"
642 ;; - Files to be loaded last
643 (define init-scheme-files-tail
644 ;; - must be after everything has been defined
647 ;; Now construct the load list
649 (define init-scheme-files
650 (append init-scheme-files-lib
651 init-scheme-files-used
652 init-scheme-files-body
653 init-scheme-files-tail))
655 (for-each ly:load init-scheme-files)
657 (define-public r5rs-primary-predicates
658 `((,boolean? . "boolean")
659 (,char? . "character")
660 (,number? . "number")
663 (,procedure? . "procedure")
664 (,string? . "string")
665 (,symbol? . "symbol")
666 (,vector? . "vector")))
668 (define-public r5rs-secondary-predicates
669 `((,char-alphabetic? . "alphabetic character")
670 (,char-lower-case? . "lower-case character")
671 (,char-numeric? . "numeric character")
672 (,char-upper-case? . "upper-case character")
673 (,char-whitespace? . "whitespace character")
675 (,complex? . "complex number")
676 (,even? . "even number")
677 (,exact? . "exact number")
678 (,inexact? . "inexact number")
679 (,integer? . "integer")
680 (,negative? . "negative number")
681 (,odd? . "odd number")
682 (,positive? . "positive number")
683 (,rational? . "rational number")
684 (,real? . "real number")
690 (,input-port? . "input port")
691 (,output-port? . "output port")
693 ;; would this ever be used?
694 (,eof-object? . "end-of-file object")
697 (define-public guile-predicates
698 `((,hash-table? . "hash table")
701 (define-public lilypond-scheme-predicates
702 `((,boolean-or-symbol? . "boolean or symbol")
704 (,cheap-list? . "list")
705 (,fraction? . "fraction, as pair")
706 (,grob-list? . "list of grobs")
707 (,index? . "non-negative integer")
708 (,key? . "index or symbol")
709 (,key-list? . "list of indexes or symbols")
710 (,key-list-or-music? . "key list or music")
711 (,key-list-or-symbol? . "key list or symbol")
712 (,markup? . "markup")
713 (,markup-command-list? . "markup command list")
714 (,markup-list? . "markup list")
715 (,moment-pair? . "pair of moment objects")
716 (,number-list? . "number list")
717 (,number-or-grob? . "number or grob")
718 (,number-or-markup? . "number or markup")
719 (,number-or-pair? . "number or pair")
720 (,number-or-string? . "number or string")
721 (,number-pair? . "pair of numbers")
722 (,number-pair-list? . "list of number pairs")
723 (,rational-or-procedure? . "an exact rational or procedure")
724 (,rhythmic-location? . "rhythmic location")
725 (,scheme? . "any type")
726 (,string-or-pair? . "string or pair")
727 (,string-or-music? . "string or music")
728 (,string-or-symbol? . "string or symbol")
729 (,symbol-list? . "symbol list")
730 (,symbol-list-or-music? . "symbol list or music")
731 (,symbol-list-or-symbol? . "symbol list or symbol")
735 (define-public lilypond-exported-predicates
736 `((,ly:book? . "book")
738 (,ly:context? . "context")
739 (,ly:context-def? . "context definition")
740 (,ly:context-mod? . "context modification")
741 (,ly:dimension? . "dimension, in staff space")
742 (,ly:dir? . "direction")
743 (,ly:dispatcher? . "dispatcher")
744 (,ly:duration? . "duration")
745 (,ly:event? . "post event")
746 (,ly:font-metric? . "font metric")
747 (,ly:grob? . "graphical (layout) object")
748 (,ly:grob-array? . "array of grobs")
749 (,ly:grob-properties? . "grob properties")
750 (,ly:input-location? . "input location")
752 (,ly:iterator? . "iterator")
753 (,ly:lily-lexer? . "lily-lexer")
754 (,ly:lily-parser? . "lily-parser")
755 (,ly:listener? . "listener")
756 (,ly:moment? . "moment")
757 (,ly:music? . "music")
758 (,ly:music-function? . "music function")
759 (,ly:music-list? . "list of music objects")
760 (,ly:music-output? . "music output")
761 (,ly:otf-font? . "OpenType font")
762 (,ly:output-def? . "output definition")
763 (,ly:page-marker? . "page marker")
764 (,ly:pango-font? . "pango font")
765 (,ly:paper-book? . "paper book")
766 (,ly:paper-system? . "paper-system Prob")
767 (,ly:pitch? . "pitch")
768 (,ly:prob? . "property object")
769 (,ly:score? . "score")
770 (,ly:skyline? . "skyline")
771 (,ly:skyline-pair? . "pair of skylines")
772 (,ly:source-file? . "source file")
773 (,ly:spanner? . "spanner")
774 (,ly:spring? . "spring")
775 (,ly:stencil? . "stencil")
776 (,ly:stream-event? . "stream event")
777 (,ly:translator? . "translator")
778 (,ly:translator-group? . "translator group")
779 (,ly:undead? . "undead container")
780 (,ly:unpure-pure-container? . "unpure/pure container")
784 (set! type-p-name-alist
785 (append r5rs-primary-predicates
786 r5rs-secondary-predicates
788 lilypond-scheme-predicates
789 lilypond-exported-predicates))
792 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
795 (define (profile-measurements)
798 (list (- (+ (tms:cutime t)
800 (assoc-get 'gc-time-taken stats))
801 (assoc-get 'total-cells-allocated stats 0))))
803 (define (dump-profile base last this)
804 (let* ((outname (format #f "~a.profile" (dir-basename base ".ly")))
805 (diff (map - this last)))
806 (ly:progress "\nWriting timing to ~a...\n" outname)
807 (format (open-file outname "w")
808 "time: ~a\ncells: ~a\n"
809 (if (ly:get-option 'dump-cpu-profile)
814 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
815 ;; debug memory leaks
820 (define gc-protect-stat-count
823 ;; Undead objects that should be ignored after the first time round
825 (make-weak-key-hash-table 0))
827 (define-public (dump-live-object-stats outfile)
828 (for-each (lambda (x)
829 (format outfile "~a: ~a\n" (car x) (cdr x)))
830 (sort (gc-live-object-stats)
832 (string<? (car x) (car y))))))
834 (define-public (dump-gc-protects)
835 (set! gc-protect-stat-count (1+ gc-protect-stat-count))
836 (let* ((protects (sort (hash-table->alist (ly:protects))
838 (< (object-address (car a))
839 (object-address (car b))))))
840 (out-file-name (string-append
841 "gcstat-" (number->string gc-protect-stat-count)
843 (outfile (open-file out-file-name "w")))
845 (ly:progress "Dumping GC statistics ~a...\n" out-file-name)
846 (for-each (lambda (y)
849 (format outfile "~a (~a) = ~a\n" (object-address x) c x)))
852 (not (symbol? (car x))))
854 (format outfile "\nprotected symbols: ~a\n"
855 (apply + (map (lambda (obj-count)
856 (if (symbol? (car obj-count))
861 ;; (display (ly:smob-protects))
863 (if (defined? 'gc-live-object-stats)
865 (ly:progress "Live object statistics: GC'ing\n")
869 (ly:progress "Asserting dead objects\n")
870 (ly:set-option 'debug-gc-assert-parsed-dead #t)
872 (ly:set-option 'debug-gc-assert-parsed-dead #f)
875 (if (not (hashq-ref gc-zombies x))
877 (ly:programming-error "Parsed object should be dead: ~a" x)
878 (hashq-set! gc-zombies x #t))))
879 (ly:parsed-undead-list!))
880 (set! stats (gc-live-object-stats))
881 (ly:progress "Dumping live object statistics.\n")
882 (dump-live-object-stats outfile)))
884 (let* ((stats (gc-stats)))
885 (for-each (lambda (sym)
886 (format outfile "~a ~a ~a\n"
887 gc-protect-stat-count
889 (assoc-get sym stats "?")))
890 '(protected-objects bytes-malloced cell-heap-size)))
892 (close-port outfile)))
894 (define (check-memory)
895 "Read `/proc/self' to check up on memory use."
896 (define (gulp-file name)
897 (let* ((file (open-input-file name))
898 (text (read-delimited "" file)))
902 (let* ((stat (gulp-file "/proc/self/status"))
903 (lines (string-split stat #\newline))
904 (interesting (filter-map
906 (string-match "^VmData:[ \t]*([0-9]*) kB" l))
908 (mem (string->number (match:substring (car interesting) 1))))
909 (format #t "VMDATA: ~a\n" mem)
913 (begin (dump-gc-protects)
916 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
918 (define (multi-fork count)
919 "Split this process into COUNT helpers. Returns either a list of
920 PIDs or the number of the process."
921 (define (helper count acc)
923 (let* ((pid (primitive-fork)))
926 (helper (1- count) (cons pid acc))))
931 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
933 (define* (ly:exit status #:optional (silently #f))
934 "Exit function for lilypond"
937 ((0) (ly:basic-progress (_ "Success: compilation successfully completed")))
938 ((1) (ly:warning (_ "Compilation completed with warnings or errors")))
939 (else (ly:message ""))))
942 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
944 (define-public (lilypond-main files)
945 "Entry point for LilyPond."
946 (eval-string (ly:command-line-code))
947 (if (ly:get-option 'help)
948 (begin (ly:option-usage)
950 (if (ly:get-option 'show-available-fonts)
951 (begin (ly:font-config-display-fonts)
953 (if (ly:get-option 'gui)
958 (if (ly:get-option 'read-file-list)
963 (string-split (string-delete (ly:gulp-file f) #\cr) #\nl))
965 (if (and (number? (ly:get-option 'job-count))
966 (>= (length files) (ly:get-option 'job-count)))
967 (let* ((count (ly:get-option 'job-count))
968 (split-todo (split-list files count))
969 (joblist (multi-fork count))
971 (if (not (string-or-symbol? (ly:get-option 'log-file)))
972 (ly:set-option 'log-file "lilypond-multi-run"))
973 (if (number? joblist)
974 (begin (ly:set-option
975 'log-file (format #f "~a-~a"
976 (ly:get-option 'log-file) joblist))
977 (set! files (vector-ref split-todo joblist)))
978 (begin (ly:progress "\nForking into jobs: ~a\n" joblist)
981 (let* ((stat (cdr (waitpid pid))))
984 (acons (list-element-index joblist pid)
991 (logfile (format #f "~a-~a.log"
992 (ly:get-option 'log-file) job))
993 (log (ly:gulp-file logfile))
994 (len (string-length log))
995 (tail (substring log (max 0 (- len 1024)))))
996 (if (status:term-sig state)
999 (format #f (_ "job ~a terminated with signal: ~a")
1000 job (status:term-sig state)))
1002 (_ "logfile ~a (exit ~a):\n~a")
1003 logfile (status:exit-val state) tail))))
1006 (ly:error "Children ~a exited with errors."
1008 ;; must overwrite individual entries
1009 (if (ly:get-option 'dump-profile)
1010 (dump-profile "lily-run-total"
1011 '(0 0) (profile-measurements)))
1016 (if (string-or-symbol? (ly:get-option 'log-file))
1017 (ly:stderr-redirect (format #f "~a.log" (ly:get-option 'log-file)) "w"))
1018 (let ((failed (lilypond-all files)))
1019 (if (ly:get-option 'trace-scheme-coverage)
1021 (coverage:show-all (lambda (f)
1022 (string-contains f "lilypond")))))
1024 (begin (ly:error (_ "failed files: ~S") (string-join failed))
1030 (define-public (lilypond-all files)
1032 (separate-logs (ly:get-option 'separate-log-files))
1035 (if (string-or-symbol? (ly:get-option 'log-file))
1036 (open-file (format #f "~a.log" (ly:get-option 'log-file))
1038 (fdes->outport 2))))
1039 (do-measurements (ly:get-option 'dump-profile))
1040 (handler (lambda (key failed-file)
1041 (set! failed (append (list failed-file) failed)))))
1045 (let* ((start-measurements (if do-measurements
1046 (profile-measurements)
1048 (base (dir-basename x ".ly"))
1049 (all-settings (ly:all-options)))
1051 (ly:stderr-redirect (format #f "~a.log" base) "w"))
1053 (format ping-log "Processing ~a\n" base))
1054 (if (ly:get-option 'trace-memory-frequency)
1055 (mtrace:start-trace (ly:get-option 'trace-memory-frequency)))
1056 (lilypond-file handler x)
1057 (ly:check-expected-warnings)
1059 (if start-measurements
1060 (dump-profile x start-measurements (profile-measurements)))
1061 (if (ly:get-option 'trace-memory-frequency)
1062 (begin (mtrace:stop-trace)
1063 (mtrace:dump-results base)))
1064 (for-each (lambda (s)
1065 (ly:set-option (car s) (cdr s)))
1067 (ly:set-option 'debug-gc-assert-parsed-dead #t)
1069 (ly:set-option 'debug-gc-assert-parsed-dead #f)
1072 (if (not (hashq-ref gc-zombies x))
1074 (ly:programming-error "Parsed object should be dead: ~a" x)
1075 (hashq-set! gc-zombies x #t))))
1076 (ly:parsed-undead-list!))
1077 (if (ly:get-option 'debug-gc)
1079 (ly:reset-all-fonts))
1083 ;; Ensure a notice re failed files is written to aggregate logfile.
1085 (format ping-log "Failed files: ~a\n" failed))
1086 (if (ly:get-option 'dump-profile)
1087 (dump-profile "lily-run-total" '(0 0) (profile-measurements)))
1090 (define (lilypond-file handler file-name)
1091 (catch 'ly-file-failed
1092 (lambda () (ly:parse-file file-name))
1093 (lambda (x . args) (handler x file-name))))
1095 (use-modules (scm editor))
1097 (define-public (gui-main files)
1099 (gui-no-files-handler))
1100 (if (not (string? (ly:get-option 'log-file)))
1101 (let* ((base (dir-basename (car files) ".ly"))
1102 (log-name (string-append base ".log")))
1103 (if (not (ly:get-option 'gui))
1104 (ly:message (_ "Redirecting output to ~a...") log-name))
1105 (ly:stderr-redirect log-name "w")
1106 (ly:message "# -*-compilation-*-"))
1107 (let ((failed (lilypond-all files)))
1111 (ly:stderr-redirect "foo" "r")
1112 (system (get-editor-command log-name 0 0 0))
1113 (ly:error (_ "failed files: ~S") (string-join failed))
1118 (define (gui-no-files-handler)
1119 (let* ((ly (string-append (ly:effective-prefix) "/ly/"))
1120 ;; FIXME: soft-code, localize
1121 (welcome-ly (string-append ly "Welcome_to_LilyPond.ly"))
1122 (cmd (get-editor-command welcome-ly 0 0 0)))
1123 (ly:message (_ "Invoking `~a'...\n") cmd)