1 ! Copyright (C) 2007, 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: arrays accessors io.backend io.streams.c init fry
4 namespaces math make assocs kernel parser parser.notes lexer
5 strings.parser vocabs sequences sequences.deep sequences.private
6 words memory kernel.private continuations io vocabs.loader
7 system strings sets vectors quotations byte-arrays sorting
8 compiler.units definitions generic generic.standard
9 generic.single tools.deploy.config combinators classes
10 classes.builtin slots.private grouping command-line ;
11 QUALIFIED: bootstrap.stage2
12 QUALIFIED: compiler.errors
13 QUALIFIED: continuations
14 QUALIFIED: definitions
17 QUALIFIED: source-files
18 QUALIFIED: source-files.errors
20 IN: tools.deploy.shaker
22 ! This file is some hairy shit.
24 : add-command-line-hook ( -- )
25 [ (command-line) command-line set-global ] "command-line"
26 startup-hooks get set-at ;
28 : strip-startup-hooks ( -- )
29 "Stripping startup hooks" show
37 [ startup-hooks get delete-at ] each
39 "threads" startup-hooks get delete-at
42 "io.thread" startup-hooks get delete-at
45 "io.files" startup-hooks get delete-at
46 "io.backend" startup-hooks get delete-at
47 "io.thread" startup-hooks get delete-at
55 } [ startup-hooks get delete-at ] each
58 : strip-debugger ( -- )
59 strip-debugger? "debugger" vocab and [
60 "Stripping debugger" show
61 "vocab:tools/deploy/shaker/strip-debugger.factor"
67 "Stripping manual memory management debug code" show
68 "vocab:tools/deploy/shaker/strip-libc.factor"
72 : strip-destructors ( -- )
73 "Stripping destructor debug code" show
74 "vocab:tools/deploy/shaker/strip-destructors.factor"
78 "Stripping stack effect checking from call( and execute(" show
79 "vocab:tools/deploy/shaker/strip-call.factor" run-file ;
83 "Stripping unused Cocoa methods" show
84 "vocab:tools/deploy/shaker/strip-cocoa.factor"
88 : strip-specialized-arrays ( -- )
89 strip-dictionary? "specialized-arrays" vocab and [
90 "Stripping specialized arrays" show
91 "vocab:tools/deploy/shaker/strip-specialized-arrays.factor"
95 : strip-word-names ( words -- )
96 "Stripping word names" show
97 [ f >>name f >>vocabulary drop ] each ;
99 : strip-word-defs ( words -- )
100 "Stripping symbolic word definitions" show
101 [ "no-def-strip" word-prop not ] filter
102 [ [ ] >>def drop ] each ;
104 : sift-assoc ( assoc -- assoc' ) [ nip ] assoc-filter ;
106 : strip-word-props ( stripped-props words -- )
107 "Stripping word properties" show
111 [ drop _ member? not ] assoc-filter sift-assoc
117 [ [ _ [ ] cache ] map ] change-props drop
121 : stripped-word-props ( -- seq )
129 "compiled-generic-uses"
138 "default-output-classes"
171 "predicate-definition"
211 deploy-c-types? get [
212 { "c-type" "struct-slots" "struct-align" } %
216 : strip-words ( props -- )
218 deploy-word-props? get [ 2dup strip-word-props ] unless
219 deploy-word-defs? get [ dup strip-word-defs ] unless
220 strip-word-names? [ dup strip-word-names strip-stack-traces ] when
223 : compiler-classes ( -- seq )
224 { "compiler" "stack-checker" }
225 [ child-vocabs [ words ] map concat [ class? ] filter ]
228 : prune-decision-tree ( tree classes -- )
229 [ tuple class>type ] 2dip '[
235 [ drop _ key? not ] assoc-filter
243 : strip-compiler-classes ( -- )
245 "Stripping compiler classes" show
246 [ single-generic? ] instances
247 compiler-classes '[ "decision-tree" word-prop _ prune-decision-tree ] each
250 : recursive-subst ( seq old new -- )
255 { [ 3dup drop eq? ] [ 2nip ] }
256 ! recurse into arrays
257 { [ pick array? ] [ [ dup ] 2dip recursive-subst ] }
258 ! otherwise do nothing
263 : strip-default-method ( generic new-default -- )
265 [ "decision-tree" word-prop ]
266 [ "default-method" word-prop ] bi
270 : new-default-method ( -- gensym )
271 [ [ "No method" throw ] (( -- * )) define-temp ] with-compilation-unit ;
273 : strip-default-methods ( -- )
274 ! In a development image, each generic has its own default method.
275 ! This gives better error messages for runtime type errors, but
276 ! takes up space. For deployment we merge them all together.
278 "Stripping default methods" show
279 [ single-generic? ] instances
280 new-default-method '[ _ strip-default-method ] each
283 : strip-vocab-globals ( except names -- words )
284 [ child-vocabs [ words ] map concat ] map concat
285 swap [ first2 lookup ] map sift diff ;
287 : stripped-globals ( -- seq )
289 "inspector-hook" "inspector" lookup ,
293 continuations:error-continuation
294 continuations:error-thread
295 continuations:restarts
297 source-files:source-files
303 "io-thread" "io.thread" lookup ,
305 "disposables" "destructors" lookup ,
307 "functor-words" "functors.backend" lookup ,
310 "initial-thread" "threads" lookup ,
313 strip-io? [ io-backend , ] when
323 } strip-vocab-globals %
326 "libraries" "alien" lookup ,
328 { { "yield-hook" "compiler.utilities" } }
329 { "cpu" "compiler" } strip-vocab-globals %
334 next-method-quot-cache
339 classes-intersect-cache
344 compiled-generic-crossref
346 compiler.errors:compiler-errors
350 source-files.errors:error-types
351 source-files.errors:error-observers
353 vocabs:load-vocab-hook
354 vocabs:vocab-observers
359 { } { "layouts" } strip-vocab-globals %
361 { } { "math.partial-dispatch" } strip-vocab-globals %
363 { } { "math.vectors.simd" } strip-vocab-globals %
365 { } { "peg" } strip-vocab-globals %
369 { } { "prettyprint.config" } strip-vocab-globals %
374 compiler.errors:compiler-errors
375 continuations:thread-error-hook
379 "ui-error-hook" "ui.gadgets.worlds" lookup ,
383 deploy-c-types? get [
384 "c-types" "alien.c-types" lookup ,
387 "windows-messages" "windows.messages" lookup [ , ] when*
390 : strip-globals ( stripped-globals -- )
392 "Stripping globals" show
394 '[ drop _ member? not ] assoc-filter
395 [ drop string? not ] assoc-filter ! strip CLI args
402 deploy-io get 3 = os windows? not and
406 "io.streams.c" forget-vocab
407 "io-thread-running?" "io.thread" lookup [
410 ] with-compilation-unit
413 : compress ( pred post-process string -- )
414 "Compressing " prepend show
415 [ instances dup H{ } clone [ [ ] cache ] curry map ] dip call
418 : compress-object? ( obj -- ? )
420 { [ dup array? ] [ empty? ] }
421 { [ dup byte-array? ] [ drop t ] }
422 { [ dup string? ] [ drop t ] }
423 { [ dup wrapper? ] [ drop t ] }
427 : compress-objects ( -- )
428 [ compress-object? ] [ ] "objects" compress ;
430 : remain-compiled ( old new -- old new )
431 ! Quotations which were formerly compiled must remain
434 2dup [ quot-compiled? ] [ quot-compiled? not ] bi* and
435 [ nip jit-compile ] [ 2drop ] if
438 : compress-quotations ( -- )
439 [ quotation? ] [ remain-compiled ] "quotations" compress
440 [ quotation? ] instances [ f >>cached-effect f >>cache-counter drop ] each ;
444 : [:c] ( -- word ) ":c" "debugger" lookup ;
446 : [print-error] ( -- word ) "print-error" "debugger" lookup ;
448 : deploy-boot-quot ( word -- )
451 startup-hooks get values concat %
452 strip-debugger? [ , ] [
453 ! Don't reference 'try' directly since we don't want
454 ! to pull in the debugger and prettyprinter into every
459 [ _ execute( obj -- ) ] [
460 _ execute( obj -- ) nl
465 strip-io? [ [ flush ] % ] unless
470 : startup-stripper ( -- )
472 f output-stream set-global ;
474 : next-method* ( method -- quot )
475 [ "method-class" word-prop ]
476 [ "method-generic" word-prop ] bi
479 : calls-next-method? ( method -- ? )
480 def>> flatten \ (call-next-method) swap memq? ;
482 : compute-next-methods ( -- )
483 [ standard-generic? ] instances [
484 "methods" word-prop values [ calls-next-method? ] filter
485 [ dup next-method* "next-method" set-word-prop ] each
487 "vocab:tools/deploy/shaker/next-methods.factor" run-file ;
489 : (clear-megamorphic-cache) ( i array -- )
490 ! Can't do any dispatch while clearing caches since that
491 ! might leave them in an inconsistent state.
493 2dup [ f ] 2dip set-array-nth
494 [ 1 + ] dip (clear-megamorphic-cache)
497 : clear-megamorphic-cache ( array -- )
498 [ 0 ] dip (clear-megamorphic-cache) ;
500 : find-megamorphic-caches ( -- seq )
501 "Finding megamorphic caches" show
502 [ standard-generic? ] instances [ def>> third ] map ;
504 : clear-megamorphic-caches ( cache -- )
505 "Clearing megamorphic caches" show
506 [ clear-megamorphic-cache ] each ;
515 strip-specialized-arrays
518 add-command-line-hook
520 strip-default-methods
521 strip-compiler-classes
522 f 5 setenv ! we can't use the Factor debugger or Factor I/O anymore
523 deploy-vocab get vocab-main deploy-boot-quot
524 find-megamorphic-caches
526 stripped-globals strip-globals
530 clear-megamorphic-caches ;
532 : deploy-error-handler ( quot -- )
535 [ error-continuation get call>> callstack>array die 1 exit ]
536 ! Don't reference these words literally, if we're stripping the
537 ! debugger out we don't want to load the prettyprinter at all
538 [ [:c] execute( -- ) nl [print-error] execute( error -- ) flush ] if
542 : (deploy) ( final-image vocab config -- )
543 #! Does the actual work of a deployment in the slave
551 "ui.debugger" require
555 deploy-vocab get require
556 deploy-vocab get vocab-main [
557 "Vocabulary has no MAIN: word." print flush 1 exit
560 "Saving final image" show
562 ] deploy-error-handler
568 "Deploying " write dup write "..." print
569 "deploy-config" get parse-file first