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 namespaces
4 math make assocs kernel parser parser.notes lexer strings.parser
5 vocabs sequences sequences.private words memory kernel.private
6 continuations io vocabs.loader system strings sets vectors quotations
7 byte-arrays sorting compiler.units definitions generic
8 generic.standard generic.single tools.deploy.config combinators
9 classes classes.builtin slots.private grouping ;
10 QUALIFIED: bootstrap.stage2
11 QUALIFIED: command-line
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 : strip-init-hooks ( -- )
25 "Stripping startup hooks" show
34 [ init-hooks get delete-at ] each
36 "threads" init-hooks get delete-at
39 "io.thread" init-hooks get delete-at
42 "io.files" init-hooks get delete-at
43 "io.backend" init-hooks get delete-at
44 "io.thread" init-hooks get delete-at
52 } [ init-hooks get delete-at ] each
55 : strip-debugger ( -- )
56 strip-debugger? "debugger" vocab and [
57 "Stripping debugger" show
58 "vocab:tools/deploy/shaker/strip-debugger.factor"
64 "Stripping manual memory management debug code" show
65 "vocab:tools/deploy/shaker/strip-libc.factor"
69 : strip-destructors ( -- )
71 "Stripping destructor debug code" show
72 "vocab:tools/deploy/shaker/strip-destructors.factor"
77 "Stripping stack effect checking from call( and execute(" show
78 "vocab:tools/deploy/shaker/strip-call.factor" run-file ;
82 "Stripping unused Cocoa methods" show
83 "vocab:tools/deploy/shaker/strip-cocoa.factor"
87 : strip-word-names ( words -- )
88 "Stripping word names" show
89 [ f >>name f >>vocabulary drop ] each ;
91 : strip-word-defs ( words -- )
92 "Stripping symbolic word definitions" show
93 [ "no-def-strip" word-prop not ] filter
94 [ [ ] >>def drop ] each ;
96 : sift-assoc ( assoc -- assoc' ) [ nip ] assoc-filter ;
98 : strip-word-props ( stripped-props words -- )
99 "Stripping word properties" show
103 [ drop _ member? not ] assoc-filter sift-assoc
109 [ [ _ [ ] cache ] map ] change-props drop
113 : stripped-word-props ( -- seq )
121 "compiled-generic-uses"
129 "default-output-classes"
161 "predicate-definition"
198 : strip-words ( props -- )
200 deploy-word-props? get [ 2dup strip-word-props ] unless
201 deploy-word-defs? get [ dup strip-word-defs ] unless
202 strip-word-names? [ dup strip-word-names ] when
205 : compiler-classes ( -- seq )
206 { "compiler" "stack-checker" }
207 [ child-vocabs [ words ] map concat [ class? ] filter ]
210 : prune-decision-tree ( tree classes -- )
211 [ tuple class>type ] 2dip '[
217 [ drop _ key? not ] assoc-filter
225 : strip-compiler-classes ( -- )
227 "Stripping compiler classes" show
228 [ single-generic? ] instances
229 compiler-classes '[ "decision-tree" word-prop _ prune-decision-tree ] each
232 : recursive-subst ( seq old new -- )
237 { [ 3dup drop eq? ] [ 2nip ] }
238 ! recurse into arrays
239 { [ pick array? ] [ [ dup ] 2dip recursive-subst ] }
240 ! otherwise do nothing
245 : strip-default-method ( generic new-default -- )
247 [ "decision-tree" word-prop ]
248 [ "default-method" word-prop ] bi
252 : new-default-method ( -- gensym )
253 [ [ "No method" throw ] (( -- * )) define-temp ] with-compilation-unit ;
255 : strip-default-methods ( -- )
256 ! In a development image, each generic has its own default method.
257 ! This gives better error messages for runtime type errors, but
258 ! takes up space. For deployment we merge them all together.
260 "Stripping default methods" show
261 [ single-generic? ] instances
262 new-default-method '[ _ strip-default-method ] each
265 : strip-vocab-globals ( except names -- words )
266 [ child-vocabs [ words ] map concat ] map concat
267 swap [ first2 lookup ] map sift diff ;
269 : stripped-globals ( -- seq )
271 "inspector-hook" "inspector" lookup ,
275 continuations:error-continuation
276 continuations:error-thread
277 continuations:restarts
279 source-files:source-files
285 "io-thread" "io.thread" lookup ,
287 "mallocs" "libc.private" lookup ,
289 "disposables" "destructors" lookup ,
292 "initial-thread" "threads" lookup ,
295 strip-io? [ io-backend , ] when
305 } strip-vocab-globals %
308 "libraries" "alien" lookup ,
310 { { "yield-hook" "compiler.utilities" } }
311 { "cpu" "compiler" } strip-vocab-globals %
316 next-method-quot-cache
321 classes-intersect-cache
324 command-line:main-vocab-hook
326 compiled-generic-crossref
328 compiler.errors:compiler-errors
332 source-files.errors:error-types
333 source-files.errors:error-observers
335 vocabs:load-vocab-hook
336 vocabs:vocab-observers
341 { } { "layouts" } strip-vocab-globals %
343 { } { "math.partial-dispatch" } strip-vocab-globals %
345 { } { "math.vectors.specialization" } strip-vocab-globals %
347 { } { "peg" } strip-vocab-globals %
351 { } { "prettyprint.config" } strip-vocab-globals %
356 compiler.errors:compiler-errors
357 continuations:thread-error-hook
361 "ui-error-hook" "ui.gadgets.worlds" lookup ,
365 deploy-c-types? get [
366 "c-types" "alien.c-types" lookup ,
369 "windows-messages" "windows.messages" lookup [ , ] when*
372 : strip-globals ( stripped-globals -- )
374 "Stripping globals" show
376 '[ drop _ member? not ] assoc-filter
377 [ drop string? not ] assoc-filter ! strip CLI args
384 deploy-io get 3 = os windows? not and
388 "io.streams.c" forget-vocab
389 "io-thread-running?" "io.thread" lookup [
392 ] with-compilation-unit
395 : compress ( pred post-process string -- )
396 "Compressing " prepend show
397 [ instances dup H{ } clone [ [ ] cache ] curry map ] dip call
400 : compress-object? ( obj -- ? )
402 { [ dup array? ] [ empty? ] }
403 { [ dup byte-array? ] [ drop t ] }
404 { [ dup string? ] [ drop t ] }
405 { [ dup wrapper? ] [ drop t ] }
409 : compress-objects ( -- )
410 [ compress-object? ] [ ] "objects" compress ;
412 : remain-compiled ( old new -- old new )
413 ! Quotations which were formerly compiled must remain
416 2dup [ quot-compiled? ] [ quot-compiled? not ] bi* and
417 [ nip jit-compile ] [ 2drop ] if
420 : compress-quotations ( -- )
421 [ quotation? ] [ remain-compiled ] "quotations" compress
422 [ quotation? ] instances [ f >>cached-effect f >>cache-counter drop ] each ;
426 : [:c] ( -- word ) ":c" "debugger" lookup ;
428 : [print-error] ( -- word ) "print-error" "debugger" lookup ;
430 : deploy-boot-quot ( word -- )
433 init-hooks get values concat %
434 strip-debugger? [ , ] [
435 ! Don't reference 'try' directly since we don't want
436 ! to pull in the debugger and prettyprinter into every
441 [ _ execute( obj -- ) ] [
442 _ execute( obj -- ) nl
447 strip-io? [ [ flush ] % ] unless
452 : init-stripper ( -- )
454 f output-stream set-global ;
456 : next-method* ( method -- quot )
457 [ "method-class" word-prop ]
458 [ "method-generic" word-prop ] bi
461 : compute-next-methods ( -- )
462 [ standard-generic? ] instances [
463 "methods" word-prop [
464 nip dup next-method* "next-method" set-word-prop
467 "vocab:tools/deploy/shaker/next-methods.factor" run-file ;
469 : (clear-megamorphic-cache) ( i array -- )
470 ! Can't do any dispatch while clearing caches since that
471 ! might leave them in an inconsistent state.
473 2dup [ f ] 2dip set-array-nth
474 [ 1 + ] dip (clear-megamorphic-cache)
477 : clear-megamorphic-cache ( array -- )
478 [ 0 ] dip (clear-megamorphic-cache) ;
480 : find-megamorphic-caches ( -- seq )
481 "Finding megamorphic caches" show
482 [ standard-generic? ] instances [ def>> third ] map ;
484 : clear-megamorphic-caches ( cache -- )
485 "Clearing megamorphic caches" show
486 [ clear-megamorphic-cache ] each ;
498 strip-default-methods
499 strip-compiler-classes
500 f 5 setenv ! we can't use the Factor debugger or Factor I/O anymore
501 deploy-vocab get vocab-main deploy-boot-quot
502 find-megamorphic-caches
504 stripped-globals strip-globals
508 clear-megamorphic-caches ;
510 : deploy-error-handler ( quot -- )
513 [ error-continuation get call>> callstack>array die 1 exit ]
514 ! Don't reference these words literally, if we're stripping the
515 ! debugger out we don't want to load the prettyprinter at all
516 [ [:c] execute( -- ) nl [print-error] execute( error -- ) flush ] if
520 : (deploy) ( final-image vocab config -- )
521 #! Does the actual work of a deployment in the slave
529 "ui.debugger" require
533 deploy-vocab get require
534 deploy-vocab get vocab-main [
535 "Vocabulary has no MAIN: word." print flush 1 exit
538 "Saving final image" show
540 ] deploy-error-handler
546 "Deploying " write dup write "..." print
547 "deploy-config" get parse-file first