]> gitweb.factorcode.org Git - factor.git/blob - basis/tools/deploy/shaker/shaker.factor
Fix comments to be ! not #!.
[factor.git] / basis / tools / deploy / shaker / shaker.factor
1 ! Copyright (C) 2007, 2010 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: arrays alien.libraries accessors io.backend
4 io.encodings.utf8 io.files io.streams.c init fry namespaces math
5 make assocs kernel parser parser.notes lexer strings.parser
6 vocabs sequences sequences.deep sequences.private words memory
7 kernel.private continuations io vocabs.loader system strings
8 sets vectors quotations byte-arrays sorting compiler.units
9 definitions generic generic.standard generic.single
10 tools.deploy.config combinators combinators.private classes
11 vocabs.loader.private classes.builtin slots.private grouping
12 command-line io.pathnames memoize namespaces.private
13 hashtables locals ;
14 QUALIFIED: bootstrap.stage2
15 QUALIFIED: classes.private
16 QUALIFIED: compiler.crossref
17 QUALIFIED: compiler.errors
18 QUALIFIED: continuations
19 QUALIFIED: definitions
20 QUALIFIED: init
21 QUALIFIED: layouts
22 QUALIFIED: source-files
23 QUALIFIED: source-files.errors
24 QUALIFIED: vocabs
25 QUALIFIED: vocabs.loader
26 IN: tools.deploy.shaker
27
28 ! This file is some hairy shit.
29
30 : add-command-line-hook ( -- )
31     [
32         (command-line) unclip
33         executable set-global
34         command-line set-global
35     ] "command-line" startup-hooks get set-at ;
36
37 : strip-startup-hooks ( -- )
38     "Stripping startup hooks" show
39     {
40         "alien.strings"
41         "cpu.x86"
42         "environment"
43         "libc"
44     }
45     [ startup-hooks get delete-at ] each
46     deploy-threads? get [
47         "threads" startup-hooks get delete-at
48     ] unless
49     strip-io? [
50         "io.backend" startup-hooks get delete-at
51     ] when
52     strip-dictionary? [
53         {
54             "vocabs"
55             "vocabs.cache"
56             "source-files.errors"
57         } [ startup-hooks get delete-at ] each
58     ] when ;
59
60 : strip-debugger ( -- )
61     strip-debugger? "debugger" lookup-vocab and [
62         "Stripping debugger" show
63         "vocab:tools/deploy/shaker/strip-debugger.factor"
64         run-file
65     ] when ;
66
67 : strip-ui-error-hook ( -- )
68     strip-debugger? deploy-ui? get and "ui" lookup-vocab and [
69         "Installing generic UI error hook" show
70         "vocab:tools/deploy/shaker/strip-ui-error-hook.factor"
71         run-file
72     ] when ;
73
74 : strip-libc ( -- )
75     "libc" lookup-vocab [
76         "Stripping manual memory management debug code" show
77         "vocab:tools/deploy/shaker/strip-libc.factor"
78         run-file
79     ] when ;
80
81 : strip-destructors ( -- )
82     "Stripping destructor debug code" show
83     "vocab:tools/deploy/shaker/strip-destructors.factor"
84     run-file ;
85
86 : strip-call ( -- )
87     "Stripping stack effect checking from call( and execute(" show
88     "vocab:tools/deploy/shaker/strip-call.factor" run-file ;
89
90 : strip-cocoa ( -- )
91     "cocoa" lookup-vocab [
92         "Stripping unused Cocoa methods" show
93         "vocab:tools/deploy/shaker/strip-cocoa.factor"
94         run-file
95     ] when ;
96
97 : strip-gobject ( -- )
98     "gobject-introspection.types" lookup-vocab [
99         "Stripping GObject type info" show
100         "vocab:tools/deploy/shaker/strip-gobject.factor"
101         run-file
102     ] when ;
103
104 : strip-gtk-icon ( -- )
105     "ui.backend.gtk" lookup-vocab [
106         "Stripping GTK icon loading code" show
107         "vocab:tools/deploy/shaker/strip-gtk-icon.factor"
108         run-file
109     ] when ;
110
111 : strip-specialized-arrays ( -- )
112     strip-dictionary? "specialized-arrays" lookup-vocab and [
113         "Stripping specialized arrays" show
114         "vocab:tools/deploy/shaker/strip-specialized-arrays.factor"
115         run-file
116     ] when ;
117
118 : strip-word-names ( words -- )
119     "Stripping word names" show
120     [ f >>name f >>vocabulary drop ] each ;
121
122 : strip-word-defs ( words -- )
123     "Stripping symbolic word definitions" show
124     [ "no-def-strip" word-prop ] reject
125     [ [ ] >>def drop ] each ;
126
127 : strip-word-props ( stripped-props words -- )
128     "Stripping word properties" show
129     swap '[
130         [
131             [ drop _ member? ] assoc-reject sift-values
132             >alist f like
133         ] change-props drop
134     ] each ;
135
136 : stripped-word-props ( -- seq )
137     [
138         strip-dictionary? [
139             {
140                 "alias"
141                 "boa-check"
142                 "coercer"
143                 "combination"
144                 "generic-call-sites"
145                 "effect-dependencies"
146                 "definition-dependencies"
147                 "conditional-dependencies"
148                 "dependency-checks"
149                 "constant"
150                 "constraints"
151                 "custom-inlining"
152                 "decision-tree"
153                 "declared-effect"
154                 "default"
155                 "default-method"
156                 "default-output-classes"
157                 "derived-from"
158                 "ebnf-parser"
159                 "engines"
160                 "forgotten"
161                 "identities"
162                 "inline"
163                 "inlined-block"
164                 "input-classes"
165                 "instances"
166                 "interval"
167                 "intrinsic"
168                 "lambda"
169                 "loc"
170                 "local-reader"
171                 "local-reader?"
172                 "local-writer"
173                 "local-writer?"
174                 "local?"
175                 "low-order"
176                 "macro"
177                 "members"
178                 "memo-quot"
179                 "methods"
180                 "method-class"
181                 "method-generic"
182                 "modular-arithmetic"
183                 "no-compile"
184                 "owner-generic"
185                 "outputs"
186                 "participants"
187                 "predicate"
188                 "predicate-definition"
189                 "predicating"
190                 "reader"
191                 "reading"
192                 "recursive"
193                 "register"
194                 "register-size"
195                 "shuffle"
196                 "slots"
197                 "special"
198                 "specializer"
199                 "struct-slots"
200                 ! UI needs this
201                 ! "superclass"
202                 "transform-n"
203                 "transform-quot"
204                 "type"
205                 "typed-def"
206                 "typed-word"
207                 "writer"
208                 "writing"
209             } %
210         ] when
211
212         strip-prettyprint? [
213             {
214                 "delimiter"
215                 "flushable"
216                 "foldable"
217                 "inline"
218                 "lambda"
219                 "macro"
220                 "memo-quot"
221                 "parsing"
222                 "word-style"
223             } %
224         ] when
225
226         deploy-c-types? get [
227             { "c-type" "struct-slots" "struct-align" } %
228         ] unless
229     ] { } make ;
230
231 : strip-words ( props -- )
232     [ word? ] instances
233     deploy-word-props? get [ 2dup strip-word-props ] unless
234     deploy-word-defs? get [ dup strip-word-defs ] unless
235     strip-word-names? [ dup strip-word-names strip-stack-traces ] when
236     2drop ;
237
238 : strip-memoized ( -- )
239     "Clearing memoized word caches" show
240     [ memoized? ] instances [ reset-memoized ] each ;
241
242 : compiler-classes ( -- seq )
243     { "compiler" "stack-checker" }
244     [ loaded-child-vocab-names [ vocab-words ] map concat [ class? ] filter ]
245     map concat unique ;
246
247 : prune-decision-tree ( tree classes -- )
248     [ tuple class>type ] 2dip '[
249         dup array? [
250             [
251                 dup array? [
252                     [
253                         2 group
254                         [ drop _ key? ] assoc-reject
255                         concat
256                     ] map
257                 ] when
258             ] map
259         ] when
260     ] change-nth ;
261
262 : strip-compiler-classes ( -- )
263     strip-dictionary? [
264         "Stripping compiler classes" show
265         [ single-generic? ] instances
266         compiler-classes '[ "decision-tree" word-prop _ prune-decision-tree ] each
267     ] when ;
268
269 : recursive-subst ( seq old new -- )
270     '[
271         _ _
272         {
273             ! old becomes new
274             { [ 2over eq? ] [ 2nip ] }
275             ! recurse into arrays
276             { [ pick array? ] [ [ dup ] 2dip recursive-subst ] }
277             ! otherwise do nothing
278             [ 2drop ]
279         } cond
280     ] map! drop ;
281
282 : strip-default-method ( generic new-default -- )
283     [
284         [ "decision-tree" word-prop ]
285         [ "default-method" word-prop ] bi
286     ] dip
287     recursive-subst ;
288
289 : new-default-method ( -- gensym )
290     [ [ "No method" throw ] ( -- * ) define-temp ] with-compilation-unit ;
291
292 : strip-default-methods ( -- )
293     ! In a development image, each generic has its own default method.
294     ! This gives better error messages for runtime type errors, but
295     ! takes up space. For deployment we merge them all together.
296     strip-debugger? [
297         "Stripping default methods" show
298         [ single-generic? ] instances
299         new-default-method '[ _ strip-default-method ] each
300     ] when ;
301
302 : vocab-tree-globals ( except names -- words )
303     [ loaded-child-vocab-names [ vocab-words ] map concat ] map concat
304     swap [ first2 lookup-word ] map sift diff ;
305
306 : stripped-globals ( -- seq )
307     [
308         "inspector-hook" "inspector" lookup-word ,
309         {
310             source-files:source-files
311             continuations:error
312             continuations:error-continuation
313             continuations:error-thread
314             continuations:restarts
315         } %
316
317         "disposables" "destructors" lookup-word ,
318
319         "functor-words" "functors.backend" lookup-word ,
320
321         { } {
322             "stack-checker"
323             "listener"
324             "bootstrap"
325         } vocab-tree-globals %
326
327         ! Don't want to strip globals from test programs
328         { } { "tools" } vocab-tree-globals
329         { } { "tools.deploy.test" } vocab-tree-globals diff %
330
331         deploy-unicode? get [
332             { } { "unicode" } vocab-tree-globals %
333         ] unless
334
335         strip-dictionary? [
336             "libraries" "alien" lookup-word ,
337
338             { { "yield-hook" "compiler.utilities" } }
339             { "cpu" "compiler" } vocab-tree-globals %
340
341             {
342                 gensym
343                 name>char-hook
344                 classes.private:next-method-quot-cache
345                 classes.private:class-and-cache
346                 classes.private:class-not-cache
347                 classes.private:class-or-cache
348                 classes.private:class<=-cache
349                 classes.private:classes-intersect-cache
350                 classes.private:implementors-map
351                 classes.private:update-map
352                 main-vocab-hook
353                 compiler.crossref:compiled-crossref
354                 compiler.crossref:generic-call-site-crossref
355                 compiler-impl
356                 compiler.errors:compiler-errors
357                 print-use-hook
358                 root-cache
359                 require-when-vocabs
360                 require-when-table
361                 source-files.errors:error-types
362                 source-files.errors:error-observers
363                 vocabs:dictionary
364                 vocabs:require-hook
365                 vocabs:vocab-observers
366                 vocabs.loader:add-vocab-root-hook
367                 word
368                 parser-quiet?
369             } %
370
371             { } { "layouts" } vocab-tree-globals %
372
373             { } { "math.partial-dispatch" } vocab-tree-globals %
374
375             { } { "math.vectors.simd" } vocab-tree-globals %
376
377             { } { "peg" } vocab-tree-globals %
378         ] when
379
380         strip-prettyprint? [
381             { } { "prettyprint.config" } vocab-tree-globals %
382         ] when
383
384         strip-debugger? [
385             \ compiler.errors:compiler-errors ,
386         ] when
387     ] { } make ;
388
389 : cleared-globals ( -- seq )
390     [
391
392         {
393             init:startup-hooks
394             input-stream
395             output-stream
396             error-stream
397             vm-path
398             image-path
399             current-directory
400         } %
401
402         "io-thread" "io.thread" lookup-word ,
403
404         deploy-threads? [
405             "initial-thread" "threads" lookup-word ,
406         ] unless
407
408         strip-io? [ io-backend , ] when
409
410         { } {
411             "timers"
412             "io.launcher"
413             "random"
414         } vocab-tree-globals %
415
416         "windows-messages" "windows.messages" lookup-word [ , ] when*
417     ] { } make ;
418
419 : strip-global? ( name stripped-globals -- ? )
420     '[ _ member? ] [ tuple? ] bi or ;
421
422 : clear-global? ( name cleared-globals -- ? )
423     '[ _ member? ] [ string? ] bi or ;
424
425 : strip-globals ( -- )
426     strip-globals? [| |
427         "Stripping globals" show
428         stripped-globals :> to-strip
429         cleared-globals :> to-clear
430         global boxes>>
431         [ drop to-strip strip-global? ] assoc-reject!
432         [
433             [
434                 swap to-clear clear-global?
435                 [ f swap value<< ] [ drop ] if
436             ] assoc-each
437         ] [ rehash ] bi
438     ] when ;
439
440 : strip-c-io ( -- )
441     ! On all platforms, if deploy-io is 1, we strip out C streams.
442     ! On Unix, if deploy-io is 3, we strip out C streams as well.
443     ! On Windows, even if deploy-io is 3, C streams are still used
444     ! for the console, so don't strip it there.
445     strip-io?
446     native-io? os windows? not and
447     or [
448         "Stripping C I/O" show
449         "vocab:tools/deploy/shaker/strip-c-io.factor" run-file
450     ] when ;
451
452 : compress ( pred post-process string -- )
453     "Compressing " prepend show
454     [ instances dup H{ } clone [ [ ] cache ] curry map ] dip call
455     become ; inline
456
457 : compress-object? ( obj -- ? )
458     {
459         { [ dup array? ] [ empty? ] }
460         { [ dup byte-array? ] [ drop t ] }
461         { [ dup string? ] [ drop t ] }
462         { [ dup wrapper? ] [ drop t ] }
463         [ drop f ]
464     } cond ;
465
466 : compress-objects ( -- )
467     [ compress-object? ] [ ] "objects" compress ;
468
469 : remain-compiled ( old new -- old new )
470     ! Quotations which were formerly compiled must remain
471     ! compiled.
472     2dup [
473         2dup [ quotation-compiled? ] [ quotation-compiled? not ] bi* and
474         [ nip jit-compile ] [ 2drop ] if
475     ] 2each ;
476
477 : compress-quotations ( -- )
478     [ quotation? ] [ remain-compiled ] "quotations" compress
479     [ quotation? ] instances [ f >>cached-effect f >>cache-counter drop ] each ;
480
481 SYMBOL: deploy-vocab
482
483 : [:c] ( -- word ) ":c" "debugger" lookup-word ;
484
485 : [print-error] ( -- word ) "print-error" "debugger" lookup-word ;
486
487 : deploy-startup-quot ( word -- )
488     [
489         [ boot ] %
490         startup-hooks get values concat %
491         strip-debugger? [ , ] [
492             ! Don't reference 'try' directly since we don't want
493             ! to pull in the debugger and prettyprinter into every
494             ! deployed app
495             [:c]
496             [print-error]
497             '[
498                 [ _ execute( obj -- ) ] [
499                     _ execute( obj -- ) nl
500                     _ execute( obj -- )
501                 ] recover
502             ] %
503         ] if
504         strip-io? [ [ flush ] % ] unless
505         [ 0 exit ] %
506     ] [ ] make
507     set-startup-quot ;
508
509 : startup-stripper ( -- )
510     t parser-quiet? set-global
511     f output-stream set-global
512     [ V{ "resource:" } clone vocab-roots set-global ]
513     "vocabs.loader" startup-hooks get-global set-at ;
514
515 : next-method* ( method -- quot )
516     [ "method-class" word-prop ]
517     [ "method-generic" word-prop ] bi
518     next-method ;
519
520 : calls-next-method? ( method -- ? )
521     def>> flatten \ (call-next-method) swap member-eq? ;
522
523 : compute-next-methods ( -- )
524     [ standard-generic? ] instances [
525         "methods" word-prop values [ calls-next-method? ] filter
526         [ dup next-method* "next-method" set-word-prop ] each
527     ] each
528     "vocab:tools/deploy/shaker/next-methods.factor" run-file ;
529
530 : (clear-megamorphic-cache) ( i array -- )
531     ! Can't do any dispatch while clearing caches since that
532     ! might leave them in an inconsistent state.
533     2dup 1 slot < [
534         2dup [ f ] 2dip set-array-nth
535         [ 1 + ] dip (clear-megamorphic-cache)
536     ] [ 2drop ] if ;
537
538 : clear-megamorphic-cache ( array -- )
539     [ 0 ] dip (clear-megamorphic-cache) ;
540
541 : find-megamorphic-caches ( -- seq )
542     "Finding megamorphic caches" show
543     [ standard-generic? ] instances [ def>> third ] map ;
544
545 : clear-megamorphic-caches ( cache -- )
546     "Clearing megamorphic caches" show
547     [ clear-megamorphic-cache ] each ;
548
549 : write-vocab-manifest ( vocab-manifest-out -- )
550     "Writing vocabulary manifest to " write dup print flush
551     loaded-vocab-names "VOCABS:" prefix
552     deploy-libraries get [ lookup-library path>> ] map members
553     "LIBRARIES:" prefix append
554     swap utf8 set-file-lines ;
555
556 : prepare-deploy-libraries ( -- )
557     "Preparing deployed libraries" show
558     deploy-libraries get [
559         libraries get [
560             [ path>> >deployed-library-path ] [ abi>> ] bi make-library
561         ] change-at
562     ] each
563
564     [
565         "deploy-libraries" "alien.libraries" lookup-word forget
566         "deploy-library" "alien.libraries" lookup-word forget
567         ">deployed-library-path" "alien.libraries.private" lookup-word forget
568     ] with-compilation-unit ;
569
570 : strip ( vocab-manifest-out -- )
571     [ write-vocab-manifest ] when*
572     startup-stripper
573     prepare-deploy-libraries
574     strip-libc
575     strip-destructors
576     strip-call
577     strip-cocoa
578     strip-gobject
579     strip-gtk-icon
580     strip-debugger
581     strip-ui-error-hook
582     strip-specialized-arrays
583     compute-next-methods
584     strip-startup-hooks
585     add-command-line-hook
586     strip-c-io
587     strip-default-methods
588     strip-compiler-classes
589     ! we can't use the Factor debugger or Factor I/O anymore
590     f ERROR-HANDLER-QUOT set-special-object
591     deploy-vocab get vocab-main deploy-startup-quot
592     find-megamorphic-caches
593     stripped-word-props
594     strip-globals
595     compress-objects
596     compress-quotations
597     strip-words
598     strip-memoized
599     clear-megamorphic-caches ;
600
601 : die-with ( error original-error -- * )
602     ! We don't want DCE to drop the error before the die call!
603     [ die 1 exit ] ( a -- * ) call-effect-unsafe ;
604
605 : die-with2 ( error original-error -- * )
606     ! We don't want DCE to drop the error before the die call!
607     [ die 1 exit ] ( a b -- * ) call-effect-unsafe ;
608
609 : deploy-error-handler ( quot -- )
610     [
611         strip-debugger?
612         [ original-error get die-with2 ]
613         ! Don't reference these words literally, if we're stripping the
614         ! debugger out we don't want to load the prettyprinter at all
615         [ [:c] execute( -- ) nl [print-error] execute( error -- ) flush ] if
616         1 exit
617     ] recover ; inline
618
619 : (deploy) ( final-image vocab-manifest-out vocab config -- )
620     ! Does the actual work of a deployment in the slave
621     ! stage2 image
622     [
623         [
624             strip-debugger? [
625                 "debugger" require
626                 "tools.errors" require
627                 "inspector" require
628                 deploy-ui? get [
629                     "ui.debugger" require
630                 ] when
631             ] unless
632             [ deploy-vocab set ] [ require ] [
633                 vocab-main [
634                     "Vocabulary has no MAIN: word." print flush 1 exit
635                 ] unless
636             ] tri
637             strip
638             "Saving final image" show
639             save-image-and-exit
640         ] deploy-error-handler
641     ] with-variables ;
642
643 : do-deploy ( -- )
644     "output-image" get
645     "vocab-manifest-out" get
646     "deploy-vocab" get
647     "Deploying " write dup write "..." print
648     "deploy-config" get parse-file first
649     (deploy) ;
650
651 MAIN: do-deploy