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 compiler.crossref compiler.errors source-files.errors
11 tools.deploy.config combinators combinators.private classes
12 vocabs.loader.private classes.builtin slots.private grouping
13 command-line io.pathnames memoize namespaces.private
14 hashtables locals source-files ;
15 QUALIFIED: classes.private
16 IN: tools.deploy.shaker
18 ! This file is some hairy shit.
20 : add-command-line-hook ( -- )
24 command-line set-global
25 ] "command-line" startup-hooks get set-at ;
27 : strip-startup-hooks ( -- )
28 "Stripping startup hooks" show
35 [ startup-hooks get delete-at ] each
37 "threads" startup-hooks get delete-at
40 "io.backend" startup-hooks get delete-at
47 } [ startup-hooks get delete-at ] each
50 : strip-debugger ( -- )
51 strip-debugger? "debugger" lookup-vocab and [
52 "Stripping debugger" show
53 "vocab:tools/deploy/shaker/strip-debugger.factor"
57 : strip-ui-error-hook ( -- )
58 strip-debugger? deploy-ui? get and "ui" lookup-vocab and [
59 "Installing generic UI error hook" show
60 "vocab:tools/deploy/shaker/strip-ui-error-hook.factor"
66 "Stripping manual memory management debug code" show
67 "vocab:tools/deploy/shaker/strip-libc.factor"
71 : strip-destructors ( -- )
72 "Stripping destructor debug code" show
73 "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 ;
81 "cocoa" lookup-vocab [
82 "Stripping unused Cocoa methods" show
83 "vocab:tools/deploy/shaker/strip-cocoa.factor"
87 : strip-gobject ( -- )
88 "gobject-introspection.types" lookup-vocab [
89 "Stripping GObject type info" show
90 "vocab:tools/deploy/shaker/strip-gobject.factor"
94 : strip-gtk-icon ( -- )
95 "ui.backend.gtk" lookup-vocab [
96 "Stripping GTK icon loading code" show
97 "vocab:tools/deploy/shaker/strip-gtk-icon.factor"
101 : strip-specialized-arrays ( -- )
102 strip-dictionary? "specialized-arrays" lookup-vocab and [
103 "Stripping specialized arrays" show
104 "vocab:tools/deploy/shaker/strip-specialized-arrays.factor"
108 : strip-word-names ( words -- )
109 "Stripping word names" show
110 [ f >>name f >>vocabulary drop ] each ;
112 : strip-word-defs ( words -- )
113 "Stripping symbolic word definitions" show
114 [ "no-def-strip" word-prop ] reject
115 [ [ ] >>def drop ] each ;
117 : strip-word-props ( stripped-props words -- )
118 "Stripping word properties" show
121 [ drop _ member? ] assoc-reject sift-values
126 : stripped-word-props ( -- seq )
135 "effect-dependencies"
136 "definition-dependencies"
137 "conditional-dependencies"
146 "default-output-classes"
178 "predicate-definition"
216 deploy-c-types? get [
217 { "c-type" "struct-slots" "struct-align" } %
221 : strip-words ( props -- )
223 deploy-word-props? get [ 2dup strip-word-props ] unless
224 deploy-word-defs? get [ dup strip-word-defs ] unless
225 strip-word-names? [ dup strip-word-names strip-stack-traces ] when
228 : strip-memoized ( -- )
229 "Clearing memoized word caches" show
230 [ memoized? ] instances [ reset-memoized ] each ;
232 : compiler-classes ( -- seq )
233 { "compiler" "stack-checker" }
234 [ loaded-child-vocab-names [ vocab-words ] map concat [ class? ] filter ]
237 : prune-decision-tree ( tree classes -- )
238 [ tuple class>type ] 2dip '[
244 [ drop _ key? ] assoc-reject
252 : strip-compiler-classes ( -- )
254 "Stripping compiler classes" show
255 [ single-generic? ] instances
256 compiler-classes '[ "decision-tree" word-prop _ prune-decision-tree ] each
259 : recursive-subst ( seq old new -- )
264 { [ 2over eq? ] [ 2nip ] }
265 ! recurse into arrays
266 { [ pick array? ] [ [ dup ] 2dip recursive-subst ] }
267 ! otherwise do nothing
272 : strip-default-method ( generic new-default -- )
274 [ "decision-tree" word-prop ]
275 [ "default-method" word-prop ] bi
279 : new-default-method ( -- gensym )
280 [ [ "No method" throw ] ( -- * ) define-temp ] with-compilation-unit ;
282 : strip-default-methods ( -- )
283 ! In a development image, each generic has its own default method.
284 ! This gives better error messages for runtime type errors, but
285 ! takes up space. For deployment we merge them all together.
287 "Stripping default methods" show
288 [ single-generic? ] instances
289 new-default-method '[ _ strip-default-method ] each
292 : vocab-tree-globals ( except names -- words )
293 [ loaded-child-vocab-names [ vocab-words ] map concat ] map concat
294 swap [ first2 lookup-word ] map sift diff ;
296 : stripped-globals ( -- seq )
298 "inspector-hook" "inspector" lookup-word ,
300 source-files:source-files
302 continuations:error-continuation
303 continuations:error-thread
304 continuations:restarts
307 "disposables" "destructors" lookup-word ,
309 "functor-words" "functors.backend" lookup-word ,
315 } vocab-tree-globals %
317 ! Don't want to strip globals from test programs
318 { } { "tools" } vocab-tree-globals
319 { } { "tools.deploy.test" } vocab-tree-globals diff %
321 deploy-unicode? get [
322 { } { "unicode" } vocab-tree-globals %
326 "libraries" "alien" lookup-word ,
328 { { "yield-hook" "compiler.utilities" } }
329 { "cpu" "compiler" } vocab-tree-globals %
334 classes.private:next-method-quot-cache
335 classes.private:class-and-cache
336 classes.private:class-not-cache
337 classes.private:class-or-cache
338 classes.private:class<=-cache
339 classes.private:classes-intersect-cache
340 classes.private:implementors-map
341 classes.private:update-map
343 compiler.crossref:compiled-crossref
344 compiler.crossref:generic-call-site-crossref
346 compiler.errors:compiler-errors
351 source-files.errors:error-types
352 source-files.errors:error-observers
355 vocabs:vocab-observers
356 vocabs.loader:add-vocab-root-hook
361 { } { "layouts" } vocab-tree-globals %
363 { } { "math.partial-dispatch" } vocab-tree-globals %
365 { } { "math.vectors.simd" } vocab-tree-globals %
367 { } { "peg" } vocab-tree-globals %
371 { } { "prettyprint.config" } vocab-tree-globals %
375 \ compiler.errors:compiler-errors ,
379 : cleared-globals ( -- seq )
392 "io-thread" "io.thread" lookup-word ,
395 "initial-thread" "threads" lookup-word ,
398 strip-io? [ io-backend , ] when
404 } vocab-tree-globals %
406 "windows-messages" "windows.messages" lookup-word [ , ] when*
409 : strip-global? ( name stripped-globals -- ? )
410 '[ _ member? ] [ tuple? ] bi or ;
412 : clear-global? ( name cleared-globals -- ? )
413 '[ _ member? ] [ string? ] bi or ;
415 : strip-globals ( -- )
417 "Stripping globals" show
418 stripped-globals :> to-strip
419 cleared-globals :> to-clear
421 [ drop to-strip strip-global? ] assoc-reject!
424 swap to-clear clear-global?
425 [ f swap value<< ] [ drop ] if
431 ! On all platforms, if deploy-io is 1, we strip out C streams.
432 ! On Unix, if deploy-io is 3, we strip out C streams as well.
433 ! On Windows, even if deploy-io is 3, C streams are still used
434 ! for the console, so don't strip it there.
436 native-io? os windows? not and
438 "Stripping C I/O" show
439 "vocab:tools/deploy/shaker/strip-c-io.factor" run-file
442 : compress ( pred post-process string -- )
443 "Compressing " prepend show
444 [ instances dup H{ } clone [ [ ] cache ] curry map ] dip call
447 : compress-object? ( obj -- ? )
449 { [ dup array? ] [ empty? ] }
450 { [ dup byte-array? ] [ drop t ] }
451 { [ dup string? ] [ drop t ] }
452 { [ dup wrapper? ] [ drop t ] }
456 : compress-objects ( -- )
457 [ compress-object? ] [ ] "objects" compress ;
459 : remain-compiled ( old new -- old new )
460 ! Quotations which were formerly compiled must remain
463 2dup [ quotation-compiled? ] [ quotation-compiled? not ] bi* and
464 [ nip jit-compile ] [ 2drop ] if
467 : compress-quotations ( -- )
468 [ quotation? ] [ remain-compiled ] "quotations" compress
469 [ quotation? ] instances [ f >>cached-effect f >>cache-counter drop ] each ;
473 : [:c] ( -- word ) ":c" "debugger" lookup-word ;
475 : [print-error] ( -- word ) "print-error" "debugger" lookup-word ;
477 : deploy-startup-quot ( word -- )
480 startup-hooks get values concat %
481 strip-debugger? [ , ] [
482 ! Don't reference 'try' directly since we don't want
483 ! to pull in the debugger and prettyprinter into every
488 [ _ execute( obj -- ) ] [
489 _ execute( obj -- ) nl
494 strip-io? [ [ flush ] % ] unless
499 : startup-stripper ( -- )
500 t parser-quiet? set-global
501 f output-stream set-global
502 [ V{ "resource:" } clone vocab-roots set-global ]
503 "vocabs.loader" startup-hooks get-global set-at ;
505 : next-method* ( method -- quot )
506 [ "method-class" word-prop ]
507 [ "method-generic" word-prop ] bi
510 : calls-next-method? ( method -- ? )
511 def>> flatten \ (call-next-method) swap member-eq? ;
513 : compute-next-methods ( -- )
514 [ standard-generic? ] instances [
515 "methods" word-prop values [ calls-next-method? ] filter
516 [ dup next-method* "next-method" set-word-prop ] each
518 "vocab:tools/deploy/shaker/next-methods.factor" run-file ;
520 : (clear-megamorphic-cache) ( i array -- )
521 ! Can't do any dispatch while clearing caches since that
522 ! might leave them in an inconsistent state.
524 2dup [ f ] 2dip set-array-nth
525 [ 1 + ] dip (clear-megamorphic-cache)
528 : clear-megamorphic-cache ( array -- )
529 [ 0 ] dip (clear-megamorphic-cache) ;
531 : find-megamorphic-caches ( -- seq )
532 "Finding megamorphic caches" show
533 [ standard-generic? ] instances [ def>> third ] map ;
535 : clear-megamorphic-caches ( cache -- )
536 "Clearing megamorphic caches" show
537 [ clear-megamorphic-cache ] each ;
539 : write-vocab-manifest ( vocab-manifest-out -- )
540 "Writing vocabulary manifest to " write dup print flush
541 loaded-vocab-names "VOCABS:" prefix
542 deploy-libraries get [ lookup-library path>> ] map members
543 "LIBRARIES:" prefix append
544 swap utf8 set-file-lines ;
546 : prepare-deploy-libraries ( -- )
547 "Preparing deployed libraries" show
548 deploy-libraries get [
550 [ path>> >deployed-library-path ] [ abi>> ] bi make-library
555 "deploy-libraries" "alien.libraries" lookup-word forget
556 "deploy-library" "alien.libraries" lookup-word forget
557 ">deployed-library-path" "alien.libraries.private" lookup-word forget
558 ] with-compilation-unit ;
560 : strip ( vocab-manifest-out -- )
561 [ write-vocab-manifest ] when*
563 prepare-deploy-libraries
572 strip-specialized-arrays
575 add-command-line-hook
577 strip-default-methods
578 strip-compiler-classes
579 ! we can't use the Factor debugger or Factor I/O anymore
580 f ERROR-HANDLER-QUOT set-special-object
581 deploy-vocab get vocab-main deploy-startup-quot
582 find-megamorphic-caches
589 clear-megamorphic-caches ;
591 : die-with ( error original-error -- * )
592 ! We don't want DCE to drop the error before the die call!
593 [ die 1 exit ] ( a -- * ) call-effect-unsafe ;
595 : die-with2 ( error original-error -- * )
596 ! We don't want DCE to drop the error before the die call!
597 [ die 1 exit ] ( a b -- * ) call-effect-unsafe ;
599 : deploy-error-handler ( quot -- )
602 [ original-error get die-with2 ]
603 ! Don't reference these words literally, if we're stripping the
604 ! debugger out we don't want to load the prettyprinter at all
605 [ [:c] execute( -- ) nl [print-error] execute( error -- ) flush ] if
609 : (deploy) ( final-image vocab-manifest-out vocab config -- )
610 ! Does the actual work of a deployment in the slave
616 "tools.errors" require
619 "ui.debugger" require
622 [ deploy-vocab namespaces:set ] [ require ] [
624 "Vocabulary has no MAIN: word." print flush 1 exit
628 "Saving final image" show
630 ] deploy-error-handler
635 "vocab-manifest-out" get
637 "Deploying " write dup write "..." print
638 "deploy-config" get parse-file first