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 ;
11 QUALIFIED: bootstrap.stage2
12 QUALIFIED: command-line
13 QUALIFIED: compiler.errors
14 QUALIFIED: continuations
15 QUALIFIED: definitions
18 QUALIFIED: source-files
19 QUALIFIED: source-files.errors
21 IN: tools.deploy.shaker
23 ! This file is some hairy shit.
25 : strip-init-hooks ( -- )
26 "Stripping startup hooks" show
35 [ init-hooks get delete-at ] each
37 "threads" init-hooks get delete-at
40 "io.thread" init-hooks get delete-at
43 "io.files" init-hooks get delete-at
44 "io.backend" init-hooks get delete-at
45 "io.thread" init-hooks get delete-at
53 } [ init-hooks get delete-at ] each
56 : strip-debugger ( -- )
57 strip-debugger? "debugger" vocab and [
58 "Stripping debugger" show
59 "vocab:tools/deploy/shaker/strip-debugger.factor"
65 "Stripping manual memory management debug code" show
66 "vocab:tools/deploy/shaker/strip-libc.factor"
70 : strip-destructors ( -- )
71 "Stripping destructor debug code" show
72 "vocab:tools/deploy/shaker/strip-destructors.factor"
76 "Stripping stack effect checking from call( and execute(" show
77 "vocab:tools/deploy/shaker/strip-call.factor" run-file ;
81 "Stripping unused Cocoa methods" show
82 "vocab:tools/deploy/shaker/strip-cocoa.factor"
86 : strip-word-names ( words -- )
87 "Stripping word names" show
88 [ f >>name f >>vocabulary drop ] each ;
90 : strip-word-defs ( words -- )
91 "Stripping symbolic word definitions" show
92 [ "no-def-strip" word-prop not ] filter
93 [ [ ] >>def drop ] each ;
95 : sift-assoc ( assoc -- assoc' ) [ nip ] assoc-filter ;
97 : strip-word-props ( stripped-props words -- )
98 "Stripping word properties" show
102 [ drop _ member? not ] assoc-filter sift-assoc
108 [ [ _ [ ] cache ] map ] change-props drop
112 : stripped-word-props ( -- seq )
120 "compiled-generic-uses"
129 "default-output-classes"
162 "predicate-definition"
201 : strip-words ( props -- )
203 deploy-word-props? get [ 2dup strip-word-props ] unless
204 deploy-word-defs? get [ dup strip-word-defs ] unless
205 strip-word-names? [ dup strip-word-names ] when
208 : compiler-classes ( -- seq )
209 { "compiler" "stack-checker" }
210 [ child-vocabs [ words ] map concat [ class? ] filter ]
213 : prune-decision-tree ( tree classes -- )
214 [ tuple class>type ] 2dip '[
220 [ drop _ key? not ] assoc-filter
228 : strip-compiler-classes ( -- )
230 "Stripping compiler classes" show
231 [ single-generic? ] instances
232 compiler-classes '[ "decision-tree" word-prop _ prune-decision-tree ] each
235 : recursive-subst ( seq old new -- )
240 { [ 3dup drop eq? ] [ 2nip ] }
241 ! recurse into arrays
242 { [ pick array? ] [ [ dup ] 2dip recursive-subst ] }
243 ! otherwise do nothing
248 : strip-default-method ( generic new-default -- )
250 [ "decision-tree" word-prop ]
251 [ "default-method" word-prop ] bi
255 : new-default-method ( -- gensym )
256 [ [ "No method" throw ] (( -- * )) define-temp ] with-compilation-unit ;
258 : strip-default-methods ( -- )
259 ! In a development image, each generic has its own default method.
260 ! This gives better error messages for runtime type errors, but
261 ! takes up space. For deployment we merge them all together.
263 "Stripping default methods" show
264 [ single-generic? ] instances
265 new-default-method '[ _ strip-default-method ] each
268 : strip-vocab-globals ( except names -- words )
269 [ child-vocabs [ words ] map concat ] map concat
270 swap [ first2 lookup ] map sift diff ;
272 : stripped-globals ( -- seq )
274 "inspector-hook" "inspector" lookup ,
278 continuations:error-continuation
279 continuations:error-thread
280 continuations:restarts
282 source-files:source-files
288 "io-thread" "io.thread" lookup ,
290 "disposables" "destructors" lookup ,
292 "functor-words" "functors.backend" lookup ,
295 "initial-thread" "threads" lookup ,
298 strip-io? [ io-backend , ] when
308 } strip-vocab-globals %
311 "libraries" "alien" lookup ,
313 { { "yield-hook" "compiler.utilities" } }
314 { "cpu" "compiler" } strip-vocab-globals %
319 next-method-quot-cache
324 classes-intersect-cache
327 command-line:main-vocab-hook
329 compiled-generic-crossref
331 compiler.errors:compiler-errors
335 source-files.errors:error-types
336 source-files.errors:error-observers
338 vocabs:load-vocab-hook
339 vocabs:vocab-observers
344 { } { "layouts" } strip-vocab-globals %
346 { } { "math.partial-dispatch" } strip-vocab-globals %
348 { } { "peg" } strip-vocab-globals %
352 { } { "prettyprint.config" } strip-vocab-globals %
357 compiler.errors:compiler-errors
358 continuations:thread-error-hook
362 "ui-error-hook" "ui.gadgets.worlds" lookup ,
366 deploy-c-types? get [
367 "c-types" "alien.c-types" lookup ,
370 "windows-messages" "windows.messages" lookup [ , ] when*
373 : strip-globals ( stripped-globals -- )
375 "Stripping globals" show
377 '[ drop _ member? not ] assoc-filter
378 [ drop string? not ] assoc-filter ! strip CLI args
385 deploy-io get 3 = os windows? not and
389 "io.streams.c" forget-vocab
390 "io-thread-running?" "io.thread" lookup [
393 ] with-compilation-unit
396 : compress ( pred post-process string -- )
397 "Compressing " prepend show
398 [ instances dup H{ } clone [ [ ] cache ] curry map ] dip call
401 : compress-object? ( obj -- ? )
403 { [ dup array? ] [ empty? ] }
404 { [ dup byte-array? ] [ drop t ] }
405 { [ dup string? ] [ drop t ] }
406 { [ dup wrapper? ] [ drop t ] }
410 : compress-objects ( -- )
411 [ compress-object? ] [ ] "objects" compress ;
413 : remain-compiled ( old new -- old new )
414 ! Quotations which were formerly compiled must remain
417 2dup [ quot-compiled? ] [ quot-compiled? not ] bi* and
418 [ nip jit-compile ] [ 2drop ] if
421 : compress-quotations ( -- )
422 [ quotation? ] [ remain-compiled ] "quotations" compress
423 [ quotation? ] instances [ f >>cached-effect f >>cache-counter drop ] each ;
427 : [:c] ( -- word ) ":c" "debugger" lookup ;
429 : [print-error] ( -- word ) "print-error" "debugger" lookup ;
431 : deploy-boot-quot ( word -- )
434 init-hooks get values concat %
435 strip-debugger? [ , ] [
436 ! Don't reference 'try' directly since we don't want
437 ! to pull in the debugger and prettyprinter into every
442 [ _ execute( obj -- ) ] [
443 _ execute( obj -- ) nl
448 strip-io? [ [ flush ] % ] unless
453 : init-stripper ( -- )
455 f output-stream set-global ;
457 : next-method* ( method -- quot )
458 [ "method-class" word-prop ]
459 [ "method-generic" word-prop ] bi
462 : calls-next-method? ( method -- ? )
463 def>> flatten \ (call-next-method) swap memq? ;
465 : compute-next-methods ( -- )
466 [ standard-generic? ] instances [
467 "methods" word-prop values [ calls-next-method? ] filter
468 [ dup next-method* "next-method" set-word-prop ] each
470 "vocab:tools/deploy/shaker/next-methods.factor" run-file ;
472 : (clear-megamorphic-cache) ( i array -- )
473 ! Can't do any dispatch while clearing caches since that
474 ! might leave them in an inconsistent state.
476 2dup [ f ] 2dip set-array-nth
477 [ 1 + ] dip (clear-megamorphic-cache)
480 : clear-megamorphic-cache ( array -- )
481 [ 0 ] dip (clear-megamorphic-cache) ;
483 : find-megamorphic-caches ( -- seq )
484 "Finding megamorphic caches" show
485 [ standard-generic? ] instances [ def>> third ] map ;
487 : clear-megamorphic-caches ( cache -- )
488 "Clearing megamorphic caches" show
489 [ clear-megamorphic-cache ] each ;
501 strip-default-methods
502 strip-compiler-classes
503 f 5 setenv ! we can't use the Factor debugger or Factor I/O anymore
504 deploy-vocab get vocab-main deploy-boot-quot
505 find-megamorphic-caches
507 stripped-globals strip-globals
511 clear-megamorphic-caches ;
513 : deploy-error-handler ( quot -- )
516 [ error-continuation get call>> callstack>array die 1 exit ]
517 ! Don't reference these words literally, if we're stripping the
518 ! debugger out we don't want to load the prettyprinter at all
519 [ [:c] execute( -- ) nl [print-error] execute( error -- ) flush ] if
523 : (deploy) ( final-image vocab config -- )
524 #! Does the actual work of a deployment in the slave
532 "ui.debugger" require
536 deploy-vocab get require
537 deploy-vocab get vocab-main [
538 "Vocabulary has no MAIN: word." print flush 1 exit
541 "Saving final image" show
543 ] deploy-error-handler
549 "Deploying " write dup write "..." print
550 "deploy-config" get parse-file first