]> git.donarmstrong.com Git - lilypond.git/blob - scm/lily.scm
Improve 'warning-as-error handling again
[lilypond.git] / scm / lily.scm
1 ;;;; lily.scm -- top-level Scheme stuff
2 ;;;;
3 ;;;;  source file of the GNU LilyPond music typesetter
4 ;;;;
5 ;;;; (c) 1998--2009 Jan Nieuwenhuizen <janneke@gnu.org>
6 ;;;; Han-Wen Nienhuys <hanwen@xs4all.nl>
7
8 ;; Internationalisation: (_i "to be translated") gets an entry in the
9 ;; POT file; (gettext ...) must be invoked explicitly to do the actual
10 ;; "translation".
11 ;;
12 ;; (define-macro (_i x) x)
13 ;; (define-macro-public _i (x) x)
14 ;; (define-public-macro _i (x) x)
15 ;; Abbrv-PWR!
16
17 (defmacro-public _i (x) x)
18
19 (read-enable 'positions)
20 (debug-enable 'debug)
21
22 (define-public PLATFORM
23   (string->symbol
24    (string-downcase
25     (car (string-tokenize (utsname:sysname (uname)))))))
26
27 (define scheme-options-definitions
28   `(
29     ;; NAMING: either
30
31     ;; - [subject-]object-object-verb +"ing"
32     ;; - [subject-]-verb-object-object
33
34     ;; Avoid overlong lines in `lilypond -dhelp'!  Strings should not
35     ;; be longer than 48 characters per line.
36
37     (anti-alias-factor 1
38 "Render at higher resolution (using given factor)
39 and scale down result to prevent jaggies in
40 PNG images.")
41     (backend ps
42 "Select backend.  Possible values: 'eps, 'null,
43 'ps, 'scm, 'socket, 'svg.")
44     (check-internal-types #f
45 "Check every property assignment for types.")
46     (clip-systems #f
47 "Generate cut-out snippets of a score.")
48     (datadir #f
49 "LilyPond prefix for data files (read-only).")
50     (debug-gc #f
51 "Dump memory debugging statistics.")
52     (debug-gc-assert-parsed-dead #f
53 "For memory debugging: Ensure that all
54 references to parsed objects are dead.  This is
55 an internal option, and is switched on
56 automatically for `-ddebug-gc'.")
57     (debug-lexer #f
58 "Debug the flex lexer.")
59     (debug-page-breaking-scoring #f
60 "Dump scores for many different page breaking
61 configurations.")
62     (debug-parser #f
63 "Debug the bison parser.")
64     (debug-property-callbacks #f
65 "Debug cyclic callback chains.")
66     (debug-skylines #f
67 "Debug skylines.")
68     (delete-intermediate-files #t
69 "Delete unusable, intermediate PostScript files.")
70     (dump-profile #f
71 "Dump memory and time information for each file.")
72     (dump-cpu-profile #f
73 "Dump timing information (system-dependent).")
74     (dump-signatures #f
75 "Dump output signatures of each system.  Used for
76 regression testing.")
77     (eps-box-padding #f
78 "Pad left edge of the output EPS bounding box by
79 given amount (in mm).")
80     (gs-load-fonts #f
81 "Load fonts via Ghostscript.")
82     (gs-load-lily-fonts #f
83 "Load only LilyPond fonts via Ghostscript.")
84     (gui #f
85 "Run LilyPond from a GUI and redirect stderr to
86 a log file.")
87     (help #f
88 "Show this help.")
89     (include-book-title-preview #t
90 "Include book titles in preview images.")
91     (include-eps-fonts #t
92 "Include fonts in separate-system EPS files.")
93     (job-count #f
94 "Process in parallel, using the given number of
95 jobs.")
96     (log-file #f
97 "If string FOO is given as argument, redirect
98 output to log file `FOO.log'.")
99     (midi-extension ,(if (eq? PLATFORM 'windows)
100                          "mid"
101                          "midi")
102 "Set the default file extension for MIDI output
103 file to given string.")
104     (old-relative #f
105 "Make \\relative mode for simultaneous music work
106 similar to chord syntax.")
107     (point-and-click #t
108 "Add point & click links to PDF output.")
109     (paper-size "a4"
110 "Set default paper size.")
111     (pixmap-format "png16m"
112 "Set GhostScript's output format for pixel images.")
113     (preview #f
114 "Create PNG and EPS preview images also.")
115     (print-pages #t
116 "Print pages in the normal way.")
117     (protected-scheme-parsing #t
118 "Continue when errors in inline scheme are caught
119 in the parser.  If #f, halt on errors and print
120 a stack trace.")
121     (profile-property-accesses #f
122 "Keep statistics of get_property() calls.")
123     (resolution 101
124 "Set resolution for generating PNG pixmaps to
125 given value (in dpi).")
126     (read-file-list #f
127 "Specify name of a file which contains a list of
128 input files to be processed.")
129     (relative-includes #f
130 "When processing an \\include command, look for
131 the included file relative to the current file
132 (instead of the root file)")
133     (safe #f
134 "Run in safer mode.")
135     (strict-infinity-checking #f
136 "Force a crash on encountering Inf and NaN
137 floating point exceptions.")
138     (strip-output-dir #t
139 "Don't use directories from input files while
140 constructing output file names.")
141     (separate-log-files #f
142 "For input files `FILE1.ly', `FILE2.ly', ...
143 output log data to files `FILE1.log',
144 `FILE2.log', ...")
145     (trace-memory-frequency #f
146 "Record Scheme cell usage this many times per
147 second.  Dump results to `FILE.stacks' and
148 `FILE.graph'.")
149     (trace-scheme-coverage #f
150 "Record coverage of Scheme files in `FILE.cov'.")
151     (show-available-fonts #f
152 "List available font names.")
153     (verbose ,(ly:command-line-verbose?)
154 "Value of the --verbose flag (read-only).")
155     (warning-as-error #f
156 "Change all warning and programming_error
157 messages into errors.")
158     ))
159
160 ;; Need to do this in the beginning.  Other parts of the Scheme
161 ;; initialization depend on these options.
162
163 (for-each (lambda (x)
164             (ly:add-option (car x) (cadr x) (caddr x)))
165           scheme-options-definitions)
166
167 (for-each (lambda (x)
168             (ly:set-option (car x) (cdr x)))
169           (eval-string (ly:command-line-options)))
170
171 (debug-set! stack 0)
172
173 (if (defined? 'set-debug-cell-accesses!)
174     (set-debug-cell-accesses! #f))
175
176                                         ;(set-debug-cell-accesses! 1000)
177
178 (use-modules (ice-9 regex)
179              (ice-9 safe)
180              (ice-9 format)
181              (ice-9 rdelim)
182              (ice-9 optargs)
183              (oop goops)
184              (srfi srfi-1)
185              (srfi srfi-13)
186              (srfi srfi-14)
187              (scm clip-region)
188              (scm memory-trace)
189              (scm coverage))
190
191 (define-public fancy-format
192   format)
193
194 (define-public (ergonomic-simple-format dest . rest)
195   "Like ice-9 format, but without the memory consumption."
196   (if (string? dest)
197       (apply simple-format (cons #f (cons dest rest)))
198       (apply simple-format (cons dest rest))))
199
200 (define format
201   ergonomic-simple-format)
202
203 ;; my display
204 (define-public (myd k v)
205   (display k)
206   (display ": ")
207   (display v)
208   (display ", ")
209   v)
210
211 (define-public (print . args)
212   (apply format (cons (current-output-port) args)))
213
214
215 ;;; General settings.
216 ;;;
217 ;;; Debugging evaluator is slower.  This should have a more sensible
218 ;;; default.
219
220 (if (or (ly:get-option 'verbose)
221         (ly:get-option 'trace-memory-frequency)
222         (ly:get-option 'trace-scheme-coverage))
223     (begin
224       (ly:set-option 'protected-scheme-parsing #f)
225       (debug-enable 'debug)
226       (debug-enable 'backtrace)
227       (read-enable 'positions)))
228
229 (if (ly:get-option 'trace-scheme-coverage)
230     (coverage:enable))
231
232 (define-public parser #f)
233
234
235 ;; gettext wrapper for guile < 1.7.2
236 (if (defined? 'gettext)
237     (define-public _ gettext)
238     (define-public _ ly:gettext))
239
240 (define-public (ly:load x)
241   (let* ((file-name (%search-load-path x)))
242     (if (ly:get-option 'verbose)
243         (ly:progress "[~A" file-name))
244     (if (not file-name)
245         (ly:error (_ "cannot find: ~A") x))
246     (primitive-load file-name)
247     (if (ly:get-option 'verbose)
248         (ly:progress "]\n"))))
249
250 (define-public DOS
251   (let ((platform (string-tokenize
252                    (vector-ref (uname) 0) char-set:letter+digit)))
253     (if (null? (cdr platform)) #f
254         (member (string-downcase (cadr platform)) '("95" "98" "me")))))
255
256 (case PLATFORM
257   ((windows)
258    (define native-getcwd
259      getcwd)
260
261    (define (slashify x)
262      (if (string-index x #\\)
263          x
264          (string-regexp-substitute
265           "//*" "/"
266           (string-regexp-substitute "\\\\" "/" x))))
267
268    ;; FIXME: this prints a warning.
269    (define-public (ly-getcwd)
270      (slashify (native-getcwd))))
271
272   (else
273    (define-public ly-getcwd
274      getcwd)))
275
276 (define-public (is-absolute? file-name)
277   (let ((file-name-length (string-length file-name)))
278     (if (= file-name-length 0)
279         #f
280         (or (eq? (string-ref file-name 0) #\/)
281             (and (eq? PLATFORM 'windows)
282                  (> file-name-length 2)
283                  (eq? (string-ref file-name 1) #\:)
284                  (eq? (string-ref file-name 2) #\/))))))
285
286 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
287
288 (define (type-check-list location signature arguments)
289   "Typecheck a list of arguments against a list of type predicates.
290 Print a message at LOCATION if any predicate failed."
291   (define (recursion-helper signature arguments count)
292     (define (helper pred? arg count)
293       (if (not (pred? arg))
294           (begin
295             (ly:input-message
296              location
297              (format
298               #f (_ "wrong type for argument ~a.  Expecting ~a, found ~s")
299               count (type-name pred?) arg))
300             #f)
301           #t))
302
303     (if (null? signature)
304         #t
305         (and (helper (car signature) (car arguments) count)
306              (recursion-helper (cdr signature) (cdr arguments) (1+ count)))))
307   (recursion-helper signature arguments 1))
308
309
310 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
311 ;; Safe definitions utility
312
313 (define safe-objects
314   (list))
315
316 (define-macro (define-safe-public arglist . body)
317   "Define a variable, export it, and mark it as safe, i.e. usable in
318 LilyPond safe mode.  The syntax is the same as `define*-public'."
319   (define (get-symbol arg)
320     (if (pair? arg)
321         (get-symbol (car arg))
322         arg))
323
324   (let ((safe-symbol (get-symbol arglist)))
325     `(begin
326        (define*-public ,arglist
327          ,@body)
328        (set! safe-objects (cons (cons ',safe-symbol ,safe-symbol)
329                                 safe-objects))
330        ,safe-symbol)))
331
332 (define-safe-public (lilypond-version)
333   (string-join
334    (map (lambda (x) (if (symbol? x)
335                         (symbol->string x)
336                         (number->string x)))
337         (ly:version))
338    "."))
339
340 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
341 ;; init pitch system
342
343 (ly:set-default-scale (ly:make-scale #(0 1 2 5/2 7/2 9/2 11/2)))
344
345 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
346 ;; other files.
347
348 (define init-scheme-files
349   '("lily-library.scm"
350     "file-cache.scm"
351     "define-event-classes.scm"
352     "define-music-types.scm"
353     "output-lib.scm"
354     "c++.scm"
355     "chord-ignatzek-names.scm"
356     "chord-entry.scm"
357     "chord-generic-names.scm"
358     "stencil.scm"
359     "markup.scm"
360     "music-functions.scm"
361     "part-combiner.scm"
362     "autochange.scm"
363     "define-music-properties.scm"
364     "beam-settings.scm"
365     "auto-beam.scm"
366     "chord-name.scm"
367
368     "parser-ly-from-scheme.scm"
369     "ly-syntax-constructors.scm"
370
371     "define-context-properties.scm"
372     "translation-functions.scm"
373     "script.scm"
374     "midi.scm"
375     "layout-beam.scm"
376     "parser-clef.scm"
377     "layout-slur.scm"
378     "font.scm"
379     "encoding.scm"
380
381     "flag-styles.scm"
382     "fret-diagrams.scm"
383     "harp-pedals.scm"
384     "predefined-fretboards.scm"
385     "define-markup-commands.scm"
386     "define-grob-properties.scm"
387     "define-grobs.scm"
388     "define-grob-interfaces.scm"
389     "define-stencil-commands.scm"
390     "titling.scm"
391
392     "paper.scm"
393     "backend-library.scm"
394     "x11-color.scm"
395     "tablature.scm"
396
397     ;; must be after everything has been defined
398     "safe-lily.scm"))
399
400 (for-each ly:load init-scheme-files)
401
402 (set! type-p-name-alist
403       `((,boolean? . "boolean")
404         (,boolean-or-symbol? . "boolean or symbol")
405         (,char? . "char")
406         (,grob-list? . "list of grobs")
407         (,hash-table? . "hash table")
408         (,input-port? . "input port")
409         (,integer? . "integer")
410         (,list? . "list")
411         (,ly:context? . "context")
412         (,ly:dimension? . "dimension, in staff space")
413         (,ly:dir? . "direction")
414         (,ly:duration? . "duration")
415         (,ly:font-metric? . "font metric")
416         (,ly:grob? . "layout object")
417         (,ly:grob-array? . "array of grobs")
418         (,ly:input-location? . "input location")
419         (,ly:moment? . "moment")
420         (,ly:music? . "music")
421         (,ly:music-list? . "list of music objects")
422         (,ly:music-output? . "music output")
423         (,ly:pitch? . "pitch")
424         (,ly:translator? . "translator")
425         (,ly:simple-closure? . "simple closure")
426         (,ly:skyline-pair? . "pair of skylines")
427         (,ly:stencil? . "stencil")
428         (,markup-list? . "list of markups")
429         (,markup? . "markup")
430         (,number-or-grob? . "number or grob")
431         (,number-or-string? . "number or string")
432         (,number-pair? . "pair of numbers")
433         (,number? . "number")
434         (,output-port? . "output port")
435         (,pair? . "pair")
436         (,procedure? . "procedure")
437         (,rhythmic-location? . "rhythmic location")
438         (,scheme? . "any type")
439         (,string? . "string")
440         (,symbol? . "symbol")
441         (,vector? . "vector")))
442
443 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
444 ;; timing
445
446 (define (profile-measurements)
447   (let* ((t (times))
448          (stats (gc-stats)))
449     (list (- (+ (tms:cutime t)
450                 (tms:utime t))
451              (ly:assoc-get 'gc-time-taken stats))
452           (ly:assoc-get 'total-cells-allocated  stats 0))))
453
454 (define (dump-profile base last this)
455   (let* ((outname (format "~a.profile" (dir-basename base ".ly")))
456          (diff (map (lambda (y) (apply - y)) (zip this last))))
457     (ly:progress "\nWriting timing to ~a..." outname)
458     (format (open-file outname "w")
459             "time: ~a\ncells: ~a\n"
460             (if (ly:get-option 'dump-cpu-profile)
461                 (car diff)
462                 0)
463             (cadr diff))))
464
465 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
466 ;; debug memory leaks
467
468 (define gc-dumping
469   #f)
470
471 (define gc-protect-stat-count
472   0)
473
474 (define-public (dump-live-object-stats outfile)
475   (for-each (lambda (x)
476               (format outfile "~a: ~a\n" (car x) (cdr x)))
477             (sort (gc-live-object-stats)
478                   (lambda (x y)
479                     (string<? (car x) (car y))))))
480
481 (define-public (dump-gc-protects)
482   (set! gc-protect-stat-count (1+ gc-protect-stat-count))
483   (let* ((protects (sort (hash-table->alist (ly:protects))
484                          (lambda (a b)
485                            (< (object-address (car a))
486                               (object-address (car b))))))
487          (out-file-name (string-append
488                          "gcstat-" (number->string gc-protect-stat-count)
489                          ".scm"))
490          (outfile (open-file out-file-name "w")))
491     (set! gc-dumping #t)
492     (display (format "Dumping GC statistics ~a...\n" out-file-name))
493     (display (map (lambda (y)
494                     (let ((x (car y))
495                           (c (cdr y)))
496                       (display
497                        (format "~a (~a) = ~a\n" (object-address x) c x)
498                        outfile)))
499                   (filter
500                    (lambda (x)
501                      (not (symbol? (car x))))
502                    protects))
503              outfile)
504     (format outfile "\nprotected symbols: ~a\n"
505             (apply + (map (lambda (obj-count)
506                             (if (symbol? (car obj-count))
507                                 (cdr obj-count)
508                                 0))
509                           protects)))
510
511     ;; (display (ly:smob-protects))
512     (newline outfile)
513     (if (defined? 'gc-live-object-stats)
514         (let* ((stats #f))
515           (display "Live object statistics: GC'ing\n")
516           (ly:reset-all-fonts)
517           (gc)
518           (gc)
519           (display "Asserting dead objects\n")
520           (ly:set-option 'debug-gc-assert-parsed-dead #t)
521           (gc)
522           (ly:set-option 'debug-gc-assert-parsed-dead #f)
523           (set! stats (gc-live-object-stats))
524           (display "Dumping live object statistics.\n")
525           (dump-live-object-stats outfile)))
526     (newline outfile)
527     (let* ((stats (gc-stats)))
528       (for-each (lambda (sym)
529                   (display
530                    (format "~a ~a ~a\n"
531                            gc-protect-stat-count
532                            sym
533                            (let ((sym-stat (assoc sym stats)))
534                              (if sym-stat
535                                  (cdr sym-stat)
536                                  "?")))
537                    outfile))
538                 '(protected-objects bytes-malloced cell-heap-size)))
539     (set! gc-dumping #f)
540     (close-port outfile)))
541
542 (define (check-memory)
543   "Read `/proc/self' to check up on memory use."
544   (define (gulp-file name)
545     (let* ((file (open-input-file name))
546            (text (read-delimited "" file)))
547       (close file)
548       text))
549
550   (let* ((stat (gulp-file "/proc/self/status"))
551          (lines (string-split stat #\newline))
552          (interesting (filter identity
553                               (map
554                                (lambda (l)
555                                  (string-match "^VmData:[ \t]*([0-9]*) kB" l))
556                                lines)))
557          (mem (string->number (match:substring (car interesting) 1))))
558     (display (format  "VMDATA: ~a\n" mem))
559     (display (gc-stats))
560     (if (> mem 100000)
561         (begin (dump-gc-protects)
562                (raise 1)))))
563
564 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
565
566 (define (multi-fork count)
567   "Split this process into COUNT helpers.  Returns either a list of
568 PIDs or the number of the process."
569   (define (helper count acc)
570     (if (> count 0)
571         (let* ((pid (primitive-fork)))
572           (if (= pid 0)
573               (1- count)
574               (helper (1- count) (cons pid acc))))
575         acc))
576
577   (helper count '()))
578
579 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
580
581 (define-public (lilypond-main files)
582   "Entry point for LilyPond."
583   (eval-string (ly:command-line-code))
584   (if (ly:get-option 'help)
585       (begin (ly:option-usage)
586              (exit 0)))
587   (if (ly:get-option 'show-available-fonts)
588       (begin (ly:font-config-display-fonts)
589              (exit 0)))
590   (if (ly:get-option 'gui)
591       (gui-main files))
592   (if (null? files)
593       (begin (ly:usage)
594              (exit 2)))
595   (if (ly:get-option 'read-file-list)
596       (set! files
597             (filter (lambda (s)
598                       (> (string-length s) 0))
599                     (apply append
600                            (map (lambda (f)
601                                   (string-split (ly:gulp-file f) #\nl))
602                                 files)))))
603   (if (and (number? (ly:get-option 'job-count))
604            (>= (length files) (ly:get-option 'job-count)))
605       (let* ((count (ly:get-option 'job-count))
606              (split-todo (split-list files count))
607              (joblist (multi-fork count))
608              (errors '()))
609         (if (not (string-or-symbol? (ly:get-option 'log-file)))
610             (ly:set-option 'log-file "lilypond-multi-run"))
611         (if (number? joblist)
612             (begin (ly:set-option
613                     'log-file (format "~a-~a"
614                                       (ly:get-option 'log-file) joblist))
615                    (set! files (vector-ref split-todo joblist)))
616             (begin (ly:progress "\nForking into jobs:  ~a\n" joblist)
617                    (for-each
618                     (lambda (pid)
619                       (let* ((stat (cdr (waitpid pid))))
620                         (if (not (= stat 0))
621                             (set! errors
622                                   (acons (list-element-index joblist pid)
623                                          stat errors)))))
624                     joblist)
625                    (for-each
626                     (lambda (x)
627                       (let* ((job (car x))
628                              (state (cdr x))
629                              (logfile (format "~a-~a.log"
630                                               (ly:get-option 'log-file) job))
631                              (log (ly:gulp-file logfile))
632                              (len (string-length log))
633                              (tail (substring  log (max 0 (- len 1024)))))
634                         (if (status:term-sig state)
635                             (ly:message
636                              "\n\n~a\n"
637                              (format (_ "job ~a terminated with signal: ~a")
638                                      job (status:term-sig state)))
639                             (ly:message
640                              (_ "logfile ~a (exit ~a):\n~a")
641                              logfile (status:exit-val state) tail))))
642                     errors)
643                    (if (pair? errors)
644                        (ly:error "Children ~a exited with errors."
645                                  (map car errors)))
646                    ;; must overwrite individual entries
647                    (if (ly:get-option 'dump-profile)
648                        (dump-profile "lily-run-total"
649                                      '(0 0) (profile-measurements)))
650                    (exit (if (null? errors)
651                              0
652                              1))))))
653   (if (string-or-symbol? (ly:get-option 'log-file))
654       (ly:stderr-redirect (format "~a.log" (ly:get-option 'log-file)) "w"))
655   (let ((failed (lilypond-all files)))
656     (if (ly:get-option 'trace-scheme-coverage)
657         (begin
658           (coverage:show-all (lambda (f)
659                                (string-contains f "lilypond")))))
660     (if (pair? failed)
661         (begin (ly:error (_ "failed files: ~S") (string-join failed))
662                (exit 1))
663         (begin
664           ;; HACK: be sure to exit with single newline
665           (ly:message "")
666           (exit 0)))))
667
668 (define-public (lilypond-all files)
669   (let* ((failed '())
670          (separate-logs (ly:get-option 'separate-log-files))
671          (ping-log
672           (if separate-logs
673               (open-file (if (string-or-symbol? (ly:get-option 'log-file))
674                              (format "~a.log" (ly:get-option 'log-file))
675                              "/dev/tty") "a") #f))
676          (do-measurements (ly:get-option 'dump-profile))
677          (handler (lambda (key failed-file)
678                     (set! failed (append (list failed-file) failed)))))
679     (gc)
680     (for-each
681      (lambda (x)
682        (let* ((start-measurements (if do-measurements
683                                       (profile-measurements)
684                                       #f))
685               (base (dir-basename x ".ly"))
686               (all-settings (ly:all-options)))
687          (if separate-logs
688              (ly:stderr-redirect (format "~a.log" base) "w"))
689          (if ping-log
690              (format ping-log "Processing ~a\n" base))
691          (if (ly:get-option 'trace-memory-frequency)
692              (mtrace:start-trace  (ly:get-option 'trace-memory-frequency)))
693          (lilypond-file handler x)
694          (if start-measurements
695              (dump-profile x start-measurements (profile-measurements)))
696          (if (ly:get-option 'trace-memory-frequency)
697              (begin (mtrace:stop-trace)
698                     (mtrace:dump-results base)))
699          (for-each (lambda (s)
700                      (ly:set-option (car s) (cdr s)))
701                    all-settings)
702          (ly:clear-anonymous-modules)
703          (ly:set-option 'debug-gc-assert-parsed-dead #t)
704          (gc)
705          (ly:set-option 'debug-gc-assert-parsed-dead #f)
706          (if (ly:get-option 'debug-gc)
707              (dump-gc-protects)
708              (if (= (random 40) 1)
709                  (ly:reset-all-fonts)))))
710      files)
711
712     ;; we want the failed-files notice in the aggregrate logfile.
713     (if ping-log
714         (format ping-log "Failed files: ~a\n" failed))
715     (if (ly:get-option 'dump-profile)
716         (dump-profile "lily-run-total" '(0 0) (profile-measurements)))
717     failed))
718
719 (define (lilypond-file handler file-name)
720   (catch 'ly-file-failed
721          (lambda () (ly:parse-file file-name))
722          (lambda (x . args) (handler x file-name))))
723
724 (use-modules (scm editor))
725
726 (define-public (gui-main files)
727   (if (null? files)
728       (gui-no-files-handler))
729   (if (not (string? (ly:get-option 'log-file)))
730       (let* ((base (dir-basename (car files) ".ly"))
731              (log-name (string-append base ".log")))
732         (if (not (ly:get-option 'gui))
733             (ly:message (_ "Redirecting output to ~a...") log-name))
734         (ly:stderr-redirect log-name "w")
735         (ly:message "# -*-compilation-*-"))
736       (let ((failed (lilypond-all files)))
737         (if (pair? failed)
738             (begin
739               ;; ugh
740               (ly:stderr-redirect "foo" "r")
741               (system (get-editor-command log-name 0 0 0))
742               (ly:error (_ "failed files: ~S") (string-join failed))
743               ;; not reached?
744               (exit 1))
745             (exit 0)))))
746
747 (define (gui-no-files-handler)
748   (let* ((ly (string-append (ly:effective-prefix) "/ly/"))
749          ;; FIXME: soft-code, localize
750          (welcome-ly (string-append ly "Welcome_to_LilyPond.ly"))
751          (cmd (get-editor-command welcome-ly 0 0 0)))
752     (ly:message (_ "Invoking `~a'...\n") cmd)
753     (system cmd)
754     (exit 1)))