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
14 QUALIFIED: bootstrap.stage2
15 QUALIFIED: classes.private
16 QUALIFIED: compiler.crossref
17 QUALIFIED: compiler.errors
18 QUALIFIED: continuations
19 QUALIFIED: definitions
22 QUALIFIED: source-files
23 QUALIFIED: source-files.errors
25 QUALIFIED: vocabs.loader
26 IN: tools.deploy.shaker
28 ! This file is some hairy shit.
30 : add-command-line-hook ( -- )
34 command-line set-global
35 ] "command-line" startup-hooks get set-at ;
37 : strip-startup-hooks ( -- )
38 "Stripping startup hooks" show
45 [ startup-hooks get delete-at ] each
47 "threads" startup-hooks get delete-at
50 "io.backend" startup-hooks get delete-at
57 } [ startup-hooks get delete-at ] each
60 : strip-debugger ( -- )
61 strip-debugger? "debugger" lookup-vocab and [
62 "Stripping debugger" show
63 "vocab:tools/deploy/shaker/strip-debugger.factor"
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"
76 "Stripping manual memory management debug code" show
77 "vocab:tools/deploy/shaker/strip-libc.factor"
81 : strip-destructors ( -- )
82 "Stripping destructor debug code" show
83 "vocab:tools/deploy/shaker/strip-destructors.factor"
87 "Stripping stack effect checking from call( and execute(" show
88 "vocab:tools/deploy/shaker/strip-call.factor" run-file ;
91 "cocoa" lookup-vocab [
92 "Stripping unused Cocoa methods" show
93 "vocab:tools/deploy/shaker/strip-cocoa.factor"
97 : strip-gobject ( -- )
98 "gobject-introspection.types" lookup-vocab [
99 "Stripping GObject type info" show
100 "vocab:tools/deploy/shaker/strip-gobject.factor"
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"
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"
118 : strip-word-names ( words -- )
119 "Stripping word names" show
120 [ f >>name f >>vocabulary drop ] each ;
122 : strip-word-defs ( words -- )
123 "Stripping symbolic word definitions" show
124 [ "no-def-strip" word-prop ] reject
125 [ [ ] >>def drop ] each ;
127 : strip-word-props ( stripped-props words -- )
128 "Stripping word properties" show
131 [ drop _ member? ] assoc-reject sift-values
136 : stripped-word-props ( -- seq )
145 "effect-dependencies"
146 "definition-dependencies"
147 "conditional-dependencies"
156 "default-output-classes"
188 "predicate-definition"
226 deploy-c-types? get [
227 { "c-type" "struct-slots" "struct-align" } %
231 : strip-words ( props -- )
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
238 : strip-memoized ( -- )
239 "Clearing memoized word caches" show
240 [ memoized? ] instances [ reset-memoized ] each ;
242 : compiler-classes ( -- seq )
243 { "compiler" "stack-checker" }
244 [ loaded-child-vocab-names [ vocab-words ] map concat [ class? ] filter ]
247 : prune-decision-tree ( tree classes -- )
248 [ tuple class>type ] 2dip '[
254 [ drop _ key? ] assoc-reject
262 : strip-compiler-classes ( -- )
264 "Stripping compiler classes" show
265 [ single-generic? ] instances
266 compiler-classes '[ "decision-tree" word-prop _ prune-decision-tree ] each
269 : recursive-subst ( seq old new -- )
274 { [ 2over eq? ] [ 2nip ] }
275 ! recurse into arrays
276 { [ pick array? ] [ [ dup ] 2dip recursive-subst ] }
277 ! otherwise do nothing
282 : strip-default-method ( generic new-default -- )
284 [ "decision-tree" word-prop ]
285 [ "default-method" word-prop ] bi
289 : new-default-method ( -- gensym )
290 [ [ "No method" throw ] ( -- * ) define-temp ] with-compilation-unit ;
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.
297 "Stripping default methods" show
298 [ single-generic? ] instances
299 new-default-method '[ _ strip-default-method ] each
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 ;
306 : stripped-globals ( -- seq )
308 "inspector-hook" "inspector" lookup-word ,
310 source-files:source-files
312 continuations:error-continuation
313 continuations:error-thread
314 continuations:restarts
317 "disposables" "destructors" lookup-word ,
319 "functor-words" "functors.backend" lookup-word ,
325 } vocab-tree-globals %
327 ! Don't want to strip globals from test programs
328 { } { "tools" } vocab-tree-globals
329 { } { "tools.deploy.test" } vocab-tree-globals diff %
331 deploy-unicode? get [
332 { } { "unicode" } vocab-tree-globals %
336 "libraries" "alien" lookup-word ,
338 { { "yield-hook" "compiler.utilities" } }
339 { "cpu" "compiler" } vocab-tree-globals %
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
353 compiler.crossref:compiled-crossref
354 compiler.crossref:generic-call-site-crossref
356 compiler.errors:compiler-errors
361 source-files.errors:error-types
362 source-files.errors:error-observers
365 vocabs:vocab-observers
366 vocabs.loader:add-vocab-root-hook
371 { } { "layouts" } vocab-tree-globals %
373 { } { "math.partial-dispatch" } vocab-tree-globals %
375 { } { "math.vectors.simd" } vocab-tree-globals %
377 { } { "peg" } vocab-tree-globals %
381 { } { "prettyprint.config" } vocab-tree-globals %
385 \ compiler.errors:compiler-errors ,
389 : cleared-globals ( -- seq )
402 "io-thread" "io.thread" lookup-word ,
405 "initial-thread" "threads" lookup-word ,
408 strip-io? [ io-backend , ] when
414 } vocab-tree-globals %
416 "windows-messages" "windows.messages" lookup-word [ , ] when*
419 : strip-global? ( name stripped-globals -- ? )
420 '[ _ member? ] [ tuple? ] bi or ;
422 : clear-global? ( name cleared-globals -- ? )
423 '[ _ member? ] [ string? ] bi or ;
425 : strip-globals ( -- )
427 "Stripping globals" show
428 stripped-globals :> to-strip
429 cleared-globals :> to-clear
431 [ drop to-strip strip-global? ] assoc-reject!
434 swap to-clear clear-global?
435 [ f swap value<< ] [ drop ] if
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.
446 native-io? os windows? not and
448 "Stripping C I/O" show
449 "vocab:tools/deploy/shaker/strip-c-io.factor" run-file
452 : compress ( pred post-process string -- )
453 "Compressing " prepend show
454 [ instances dup H{ } clone [ [ ] cache ] curry map ] dip call
457 : compress-object? ( obj -- ? )
459 { [ dup array? ] [ empty? ] }
460 { [ dup byte-array? ] [ drop t ] }
461 { [ dup string? ] [ drop t ] }
462 { [ dup wrapper? ] [ drop t ] }
466 : compress-objects ( -- )
467 [ compress-object? ] [ ] "objects" compress ;
469 : remain-compiled ( old new -- old new )
470 ! Quotations which were formerly compiled must remain
473 2dup [ quotation-compiled? ] [ quotation-compiled? not ] bi* and
474 [ nip jit-compile ] [ 2drop ] if
477 : compress-quotations ( -- )
478 [ quotation? ] [ remain-compiled ] "quotations" compress
479 [ quotation? ] instances [ f >>cached-effect f >>cache-counter drop ] each ;
483 : [:c] ( -- word ) ":c" "debugger" lookup-word ;
485 : [print-error] ( -- word ) "print-error" "debugger" lookup-word ;
487 : deploy-startup-quot ( word -- )
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
498 [ _ execute( obj -- ) ] [
499 _ execute( obj -- ) nl
504 strip-io? [ [ flush ] % ] unless
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 ;
515 : next-method* ( method -- quot )
516 [ "method-class" word-prop ]
517 [ "method-generic" word-prop ] bi
520 : calls-next-method? ( method -- ? )
521 def>> flatten \ (call-next-method) swap member-eq? ;
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
528 "vocab:tools/deploy/shaker/next-methods.factor" run-file ;
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.
534 2dup [ f ] 2dip set-array-nth
535 [ 1 + ] dip (clear-megamorphic-cache)
538 : clear-megamorphic-cache ( array -- )
539 [ 0 ] dip (clear-megamorphic-cache) ;
541 : find-megamorphic-caches ( -- seq )
542 "Finding megamorphic caches" show
543 [ standard-generic? ] instances [ def>> third ] map ;
545 : clear-megamorphic-caches ( cache -- )
546 "Clearing megamorphic caches" show
547 [ clear-megamorphic-cache ] each ;
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 ;
556 : prepare-deploy-libraries ( -- )
557 "Preparing deployed libraries" show
558 deploy-libraries get [
560 [ path>> >deployed-library-path ] [ abi>> ] bi make-library
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 ;
570 : strip ( vocab-manifest-out -- )
571 [ write-vocab-manifest ] when*
573 prepare-deploy-libraries
582 strip-specialized-arrays
585 add-command-line-hook
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
599 clear-megamorphic-caches ;
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 ;
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 ;
609 : deploy-error-handler ( quot -- )
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
619 : (deploy) ( final-image vocab-manifest-out vocab config -- )
620 #! Does the actual work of a deployment in the slave
626 "tools.errors" require
629 "ui.debugger" require
632 [ deploy-vocab set ] [ require ] [
634 "Vocabulary has no MAIN: word." print flush 1 exit
638 "Saving final image" show
640 ] deploy-error-handler
645 "vocab-manifest-out" get
647 "Deploying " write dup write "..." print
648 "deploy-config" get parse-file first