]> git.donarmstrong.com Git - lilypond.git/blob - scm/lily.scm
Add a new program option: -dwarning-as-error
[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, '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 "Exit if an undefined stencil expression is
157 found.")
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 ;;  output
311
312 ;; (define-public (output-framework) (write "hello\n"))
313
314 (define output-ps-module
315   (make-module 1021 (list (resolve-interface '(scm output-ps)))))
316
317 (define-public (ps-output-expression expr port)
318   (display (eval expr output-ps-module) port))
319
320 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
321 ;; Safe definitions utility
322
323 (define safe-objects
324   (list))
325
326 (define-macro (define-safe-public arglist . body)
327   "Define a variable, export it, and mark it as safe, i.e. usable in
328 LilyPond safe mode.  The syntax is the same as `define*-public'."
329   (define (get-symbol arg)
330     (if (pair? arg)
331         (get-symbol (car arg))
332         arg))
333
334   (let ((safe-symbol (get-symbol arglist)))
335     `(begin
336        (define*-public ,arglist
337          ,@body)
338        (set! safe-objects (cons (cons ',safe-symbol ,safe-symbol)
339                                 safe-objects))
340        ,safe-symbol)))
341
342 (define-safe-public (lilypond-version)
343   (string-join
344    (map (lambda (x) (if (symbol? x)
345                         (symbol->string x)
346                         (number->string x)))
347         (ly:version))
348    "."))
349
350 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
351 ;; init pitch system
352
353 (ly:set-default-scale (ly:make-scale #(0 1 2 5/2 7/2 9/2 11/2)))
354
355 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
356 ;; other files.
357
358 (define init-scheme-files
359   '("lily-library.scm"
360     "file-cache.scm"
361     "define-event-classes.scm"
362     "define-music-types.scm"
363     "output-lib.scm"
364     "c++.scm"
365     "chord-ignatzek-names.scm"
366     "chord-entry.scm"
367     "chord-generic-names.scm"
368     "stencil.scm"
369     "markup.scm"
370     "music-functions.scm"
371     "part-combiner.scm"
372     "autochange.scm"
373     "define-music-properties.scm"
374     "auto-beam.scm"
375     "chord-name.scm"
376
377     "parser-ly-from-scheme.scm"
378     "ly-syntax-constructors.scm"
379
380     "define-context-properties.scm"
381     "translation-functions.scm"
382     "script.scm"
383     "midi.scm"
384     "layout-beam.scm"
385     "parser-clef.scm"
386     "layout-slur.scm"
387     "font.scm"
388     "encoding.scm"
389
390     "flag-styles.scm"
391     "fret-diagrams.scm"
392     "harp-pedals.scm"
393     "predefined-fretboards.scm"
394     "define-markup-commands.scm"
395     "define-grob-properties.scm"
396     "define-grobs.scm"
397     "define-grob-interfaces.scm"
398     "define-stencil-commands.scm"
399     "titling.scm"
400
401     "paper.scm"
402     "backend-library.scm"
403     "x11-color.scm"
404
405     ;; must be after everything has been defined
406     "safe-lily.scm"))
407
408 (for-each ly:load init-scheme-files)
409
410 (set! type-p-name-alist
411       `((,boolean? . "boolean")
412         (,boolean-or-symbol? . "boolean or symbol")
413         (,char? . "char")
414         (,grob-list? . "list of grobs")
415         (,hash-table? . "hash table")
416         (,input-port? . "input port")
417         (,integer? . "integer")
418         (,list? . "list")
419         (,ly:context? . "context")
420         (,ly:dimension? . "dimension, in staff space")
421         (,ly:dir? . "direction")
422         (,ly:duration? . "duration")
423         (,ly:font-metric? . "font metric")
424         (,ly:grob? . "layout object")
425         (,ly:grob-array? . "array of grobs")
426         (,ly:input-location? . "input location")
427         (,ly:moment? . "moment")
428         (,ly:music? . "music")
429         (,ly:music-list? . "list of music objects")
430         (,ly:music-output? . "music output")
431         (,ly:pitch? . "pitch")
432         (,ly:translator? . "translator")
433         (,ly:simple-closure? . "simple closure")
434         (,ly:skyline-pair? . "pair of skylines")
435         (,ly:stencil? . "stencil")
436         (,markup-list? . "list of markups")
437         (,markup? . "markup")
438         (,number-or-grob? . "number or grob")
439         (,number-or-string? . "number or string")
440         (,number-pair? . "pair of numbers")
441         (,number? . "number")
442         (,output-port? . "output port")
443         (,pair? . "pair")
444         (,procedure? . "procedure")
445         (,rhythmic-location? . "rhythmic location")
446         (,scheme? . "any type")
447         (,string? . "string")
448         (,symbol? . "symbol")
449         (,vector? . "vector")))
450
451 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
452 ;; timing
453
454 (define (profile-measurements)
455   (let* ((t (times))
456          (stats (gc-stats)))
457     (list (- (+ (tms:cutime t)
458                 (tms:utime t))
459              (ly:assoc-get 'gc-time-taken stats))
460           (ly:assoc-get 'total-cells-allocated  stats 0))))
461
462 (define (dump-profile base last this)
463   (let* ((outname (format "~a.profile" (dir-basename base ".ly")))
464          (diff (map (lambda (y) (apply - y)) (zip this last))))
465     (ly:progress "\nWriting timing to ~a..." outname)
466     (format (open-file outname "w")
467             "time: ~a\ncells: ~a\n"
468             (if (ly:get-option 'dump-cpu-profile)
469                 (car diff)
470                 0)
471             (cadr diff))))
472
473 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
474 ;; debug memory leaks
475
476 (define gc-dumping
477   #f)
478
479 (define gc-protect-stat-count
480   0)
481
482 (define-public (dump-live-object-stats outfile)
483   (for-each (lambda (x)
484               (format outfile "~a: ~a\n" (car x) (cdr x)))
485             (sort (gc-live-object-stats)
486                   (lambda (x y)
487                     (string<? (car x) (car y))))))
488
489 (define-public (dump-gc-protects)
490   (set! gc-protect-stat-count (1+ gc-protect-stat-count))
491   (let* ((protects (sort (hash-table->alist (ly:protects))
492                          (lambda (a b)
493                            (< (object-address (car a))
494                               (object-address (car b))))))
495          (out-file-name (string-append
496                          "gcstat-" (number->string gc-protect-stat-count)
497                          ".scm"))
498          (outfile (open-file out-file-name "w")))
499     (set! gc-dumping #t)
500     (display (format "Dumping GC statistics ~a...\n" out-file-name))
501     (display (map (lambda (y)
502                     (let ((x (car y))
503                           (c (cdr y)))
504                       (display
505                        (format "~a (~a) = ~a\n" (object-address x) c x)
506                        outfile)))
507                   (filter
508                    (lambda (x)
509                      (not (symbol? (car x))))
510                    protects))
511              outfile)
512     (format outfile "\nprotected symbols: ~a\n"
513             (apply + (map (lambda (obj-count)
514                             (if (symbol? (car obj-count))
515                                 (cdr obj-count)
516                                 0))
517                           protects)))
518
519     ;; (display (ly:smob-protects))
520     (newline outfile)
521     (if (defined? 'gc-live-object-stats)
522         (let* ((stats #f))
523           (display "Live object statistics: GC'ing\n")
524           (ly:reset-all-fonts)
525           (gc)
526           (gc)
527           (display "Asserting dead objects\n")
528           (ly:set-option 'debug-gc-assert-parsed-dead #t)
529           (gc)
530           (ly:set-option 'debug-gc-assert-parsed-dead #f)
531           (set! stats (gc-live-object-stats))
532           (display "Dumping live object statistics.\n")
533           (dump-live-object-stats outfile)))
534     (newline outfile)
535     (let* ((stats (gc-stats)))
536       (for-each (lambda (sym)
537                   (display
538                    (format "~a ~a ~a\n"
539                            gc-protect-stat-count
540                            sym
541                            (let ((sym-stat (assoc sym stats)))
542                              (if sym-stat
543                                  (cdr sym-stat)
544                                  "?")))
545                    outfile))
546                 '(protected-objects bytes-malloced cell-heap-size)))
547     (set! gc-dumping #f)
548     (close-port outfile)))
549
550 (define (check-memory)
551   "Read `/proc/self' to check up on memory use."
552   (define (gulp-file name)
553     (let* ((file (open-input-file name))
554            (text (read-delimited "" file)))
555       (close file)
556       text))
557
558   (let* ((stat (gulp-file "/proc/self/status"))
559          (lines (string-split stat #\newline))
560          (interesting (filter identity
561                               (map
562                                (lambda (l)
563                                  (string-match "^VmData:[ \t]*([0-9]*) kB" l))
564                                lines)))
565          (mem (string->number (match:substring (car interesting) 1))))
566     (display (format  "VMDATA: ~a\n" mem))
567     (display (gc-stats))
568     (if (> mem 100000)
569         (begin (dump-gc-protects)
570                (raise 1)))))
571
572 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
573
574 (define (multi-fork count)
575   "Split this process into COUNT helpers.  Returns either a list of
576 PIDs or the number of the process."
577   (define (helper count acc)
578     (if (> count 0)
579         (let* ((pid (primitive-fork)))
580           (if (= pid 0)
581               (1- count)
582               (helper (1- count) (cons pid acc))))
583         acc))
584
585   (helper count '()))
586
587 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
588
589 (define-public (lilypond-main files)
590   "Entry point for LilyPond."
591   (eval-string (ly:command-line-code))
592   (if (ly:get-option 'help)
593       (begin (ly:option-usage)
594              (exit 0)))
595   (if (ly:get-option 'show-available-fonts)
596       (begin (ly:font-config-display-fonts)
597              (exit 0)))
598   (if (ly:get-option 'gui)
599       (gui-main files))
600   (if (null? files)
601       (begin (ly:usage)
602              (exit 2)))
603   (if (ly:get-option 'read-file-list)
604       (set! files
605             (filter (lambda (s)
606                       (> (string-length s) 0))
607                     (apply append
608                            (map (lambda (f)
609                                   (string-split (ly:gulp-file f) #\nl))
610                                 files)))))
611   (if (and (number? (ly:get-option 'job-count))
612            (>= (length files) (ly:get-option 'job-count)))
613       (let* ((count (ly:get-option 'job-count))
614              (split-todo (split-list files count))
615              (joblist (multi-fork count))
616              (errors '()))
617         (if (not (string-or-symbol? (ly:get-option 'log-file)))
618             (ly:set-option 'log-file "lilypond-multi-run"))
619         (if (number? joblist)
620             (begin (ly:set-option
621                     'log-file (format "~a-~a"
622                                       (ly:get-option 'log-file) joblist))
623                    (set! files (vector-ref split-todo joblist)))
624             (begin (ly:progress "\nForking into jobs:  ~a\n" joblist)
625                    (for-each
626                     (lambda (pid)
627                       (let* ((stat (cdr (waitpid pid))))
628                         (if (not (= stat 0))
629                             (set! errors
630                                   (acons (list-element-index joblist pid)
631                                          stat errors)))))
632                     joblist)
633                    (for-each
634                     (lambda (x)
635                       (let* ((job (car x))
636                              (state (cdr x))
637                              (logfile (format "~a-~a.log"
638                                               (ly:get-option 'log-file) job))
639                              (log (ly:gulp-file logfile))
640                              (len (string-length log))
641                              (tail (substring  log (max 0 (- len 1024)))))
642                         (if (status:term-sig state)
643                             (ly:message
644                              "\n\n~a\n"
645                              (format (_ "job ~a terminated with signal: ~a")
646                                      job (status:term-sig state)))
647                             (ly:message
648                              (_ "logfile ~a (exit ~a):\n~a")
649                              logfile (status:exit-val state) tail))))
650                     errors)
651                    (if (pair? errors)
652                        (ly:error "Children ~a exited with errors."
653                                  (map car errors)))
654                    ;; must overwrite individual entries
655                    (if (ly:get-option 'dump-profile)
656                        (dump-profile "lily-run-total"
657                                      '(0 0) (profile-measurements)))
658                    (exit (if (null? errors)
659                              0
660                              1))))))
661   (if (string-or-symbol? (ly:get-option 'log-file))
662       (ly:stderr-redirect (format "~a.log" (ly:get-option 'log-file)) "w"))
663   (let ((failed (lilypond-all files)))
664     (if (ly:get-option 'trace-scheme-coverage)
665         (begin
666           (coverage:show-all (lambda (f)
667                                (string-contains f "lilypond")))))
668     (if (pair? failed)
669         (begin (ly:error (_ "failed files: ~S") (string-join failed))
670                (exit 1))
671         (begin
672           ;; HACK: be sure to exit with single newline
673           (ly:message "")
674           (exit 0)))))
675
676 (define-public (lilypond-all files)
677   (let* ((failed '())
678          (separate-logs (ly:get-option 'separate-log-files))
679          (ping-log
680           (if separate-logs
681               (open-file (if (string-or-symbol? (ly:get-option 'log-file))
682                              (format "~a.log" (ly:get-option 'log-file))
683                              "/dev/tty") "a") #f))
684          (do-measurements (ly:get-option 'dump-profile))
685          (handler (lambda (key failed-file)
686                     (set! failed (append (list failed-file) failed)))))
687     (gc)
688     (for-each
689      (lambda (x)
690        (let* ((start-measurements (if do-measurements
691                                       (profile-measurements)
692                                       #f))
693               (base (dir-basename x ".ly"))
694               (all-settings (ly:all-options)))
695          (if separate-logs
696              (ly:stderr-redirect (format "~a.log" base) "w"))
697          (if ping-log
698              (format ping-log "Processing ~a\n" base))
699          (if (ly:get-option 'trace-memory-frequency)
700              (mtrace:start-trace  (ly:get-option 'trace-memory-frequency)))
701          (lilypond-file handler x)
702          (if start-measurements
703              (dump-profile x start-measurements (profile-measurements)))
704          (if (ly:get-option 'trace-memory-frequency)
705              (begin (mtrace:stop-trace)
706                     (mtrace:dump-results base)))
707          (for-each (lambda (s)
708                      (ly:set-option (car s) (cdr s)))
709                    all-settings)
710          (ly:clear-anonymous-modules)
711          (ly:set-option 'debug-gc-assert-parsed-dead #t)
712          (gc)
713          (ly:set-option 'debug-gc-assert-parsed-dead #f)
714          (if (ly:get-option 'debug-gc)
715              (dump-gc-protects)
716              (if (= (random 40) 1)
717                  (ly:reset-all-fonts)))))
718      files)
719
720     ;; we want the failed-files notice in the aggregrate logfile.
721     (if ping-log
722         (format ping-log "Failed files: ~a\n" failed))
723     (if (ly:get-option 'dump-profile)
724         (dump-profile "lily-run-total" '(0 0) (profile-measurements)))
725     failed))
726
727 (define (lilypond-file handler file-name)
728   (catch 'ly-file-failed
729          (lambda () (ly:parse-file file-name))
730          (lambda (x . args) (handler x file-name))))
731
732 (use-modules (scm editor))
733
734 (define-public (gui-main files)
735   (if (null? files)
736       (gui-no-files-handler))
737   (if (not (string? (ly:get-option 'log-file)))
738       (let* ((base (dir-basename (car files) ".ly"))
739              (log-name (string-append base ".log")))
740         (if (not (ly:get-option 'gui))
741             (ly:message (_ "Redirecting output to ~a...") log-name))
742         (ly:stderr-redirect log-name "w")
743         (ly:message "# -*-compilation-*-"))
744       (let ((failed (lilypond-all files)))
745         (if (pair? failed)
746             (begin
747               ;; ugh
748               (ly:stderr-redirect "foo" "r")
749               (system (get-editor-command log-name 0 0 0))
750               (ly:error (_ "failed files: ~S") (string-join failed))
751               ;; not reached?
752               (exit 1))
753             (exit 0)))))
754
755 (define (gui-no-files-handler)
756   (let* ((ly (string-append (ly:effective-prefix) "/ly/"))
757          ;; FIXME: soft-code, localize
758          (welcome-ly (string-append ly "Welcome_to_LilyPond.ly"))
759          (cmd (get-editor-command welcome-ly 0 0 0)))
760     (ly:message (_ "Invoking `~a'...\n") cmd)
761     (system cmd)
762     (exit 1)))