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: classes.private
15 IN: tools.deploy.shaker
17 ! This file is some hairy shit.
19 : add-command-line-hook ( -- )
23 command-line set-global
24 ] "command-line" startup-hooks get set-at ;
26 : strip-startup-hooks ( -- )
27 "Stripping startup hooks" show
34 [ startup-hooks get delete-at ] each
36 "threads" startup-hooks get delete-at
39 "io.backend" startup-hooks get delete-at
46 } [ startup-hooks get delete-at ] each
49 : strip-debugger ( -- )
50 strip-debugger? "debugger" lookup-vocab and [
51 "Stripping debugger" show
52 "vocab:tools/deploy/shaker/strip-debugger.factor"
56 : strip-ui-error-hook ( -- )
57 strip-debugger? deploy-ui? get and "ui" lookup-vocab and [
58 "Installing generic UI error hook" show
59 "vocab:tools/deploy/shaker/strip-ui-error-hook.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 ;
80 "cocoa" lookup-vocab [
81 "Stripping unused Cocoa methods" show
82 "vocab:tools/deploy/shaker/strip-cocoa.factor"
86 : strip-gobject ( -- )
87 "gobject-introspection.types" lookup-vocab [
88 "Stripping GObject type info" show
89 "vocab:tools/deploy/shaker/strip-gobject.factor"
93 : strip-gtk-icon ( -- )
94 "ui.backend.gtk" lookup-vocab [
95 "Stripping GTK icon loading code" show
96 "vocab:tools/deploy/shaker/strip-gtk-icon.factor"
100 : strip-specialized-arrays ( -- )
101 strip-dictionary? "specialized-arrays" lookup-vocab and [
102 "Stripping specialized arrays" show
103 "vocab:tools/deploy/shaker/strip-specialized-arrays.factor"
107 : strip-word-names ( words -- )
108 "Stripping word names" show
109 [ f >>name f >>vocabulary drop ] each ;
111 : strip-word-defs ( words -- )
112 "Stripping symbolic word definitions" show
113 [ "no-def-strip" word-prop ] reject
114 [ [ ] >>def drop ] each ;
116 : strip-word-props ( stripped-props words -- )
117 "Stripping word properties" show
120 [ drop _ member? ] assoc-reject sift-values
125 : stripped-word-props ( -- seq )
134 "effect-dependencies"
135 "definition-dependencies"
136 "conditional-dependencies"
145 "default-output-classes"
177 "predicate-definition"
215 deploy-c-types? get [
216 { "c-type" "struct-slots" "struct-align" } %
220 : strip-words ( props -- )
222 deploy-word-props? get [ 2dup strip-word-props ] unless
223 deploy-word-defs? get [ dup strip-word-defs ] unless
224 strip-word-names? [ dup strip-word-names strip-stack-traces ] when
227 : strip-memoized ( -- )
228 "Clearing memoized word caches" show
229 [ memoized? ] instances [ reset-memoized ] each ;
231 : compiler-classes ( -- seq )
232 { "compiler" "stack-checker" }
233 [ loaded-child-vocab-names [ vocab-words ] map concat [ class? ] filter ]
236 : prune-decision-tree ( tree classes -- )
237 [ tuple class>type ] 2dip '[
243 [ drop _ key? ] assoc-reject
251 : strip-compiler-classes ( -- )
253 "Stripping compiler classes" show
254 [ single-generic? ] instances
255 compiler-classes '[ "decision-tree" word-prop _ prune-decision-tree ] each
258 : recursive-subst ( seq old new -- )
263 { [ 2over eq? ] [ 2nip ] }
264 ! recurse into arrays
265 { [ pick array? ] [ [ dup ] 2dip recursive-subst ] }
266 ! otherwise do nothing
271 : strip-default-method ( generic new-default -- )
273 [ "decision-tree" word-prop ]
274 [ "default-method" word-prop ] bi
278 : new-default-method ( -- gensym )
279 [ [ "No method" throw ] ( -- * ) define-temp ] with-compilation-unit ;
281 : strip-default-methods ( -- )
282 ! In a development image, each generic has its own default method.
283 ! This gives better error messages for runtime type errors, but
284 ! takes up space. For deployment we merge them all together.
286 "Stripping default methods" show
287 [ single-generic? ] instances
288 new-default-method '[ _ strip-default-method ] each
291 : vocab-tree-globals ( except names -- words )
292 [ loaded-child-vocab-names [ vocab-words ] map concat ] map concat
293 swap [ first2 lookup-word ] map sift diff ;
295 : stripped-globals ( -- seq )
297 "inspector-hook" "inspector" lookup-word ,
299 source-files:source-files
301 continuations:error-continuation
302 continuations:error-thread
303 continuations:restarts
306 "disposables" "destructors" lookup-word ,
308 "functor-words" "functors.backend" lookup-word ,
314 } vocab-tree-globals %
316 ! Don't want to strip globals from test programs
317 { } { "tools" } vocab-tree-globals
318 { } { "tools.deploy.test" } vocab-tree-globals diff %
320 deploy-unicode? get [
321 { } { "unicode" } vocab-tree-globals %
325 "libraries" "alien" lookup-word ,
327 { { "yield-hook" "compiler.utilities" } }
328 { "cpu" "compiler" } vocab-tree-globals %
333 classes.private:next-method-quot-cache
334 classes.private:class-and-cache
335 classes.private:class-not-cache
336 classes.private:class-or-cache
337 classes.private:class<=-cache
338 classes.private:classes-intersect-cache
339 classes.private:implementors-map
340 classes.private:update-map
342 compiler.crossref:compiled-crossref
343 compiler.crossref:generic-call-site-crossref
345 compiler.errors:compiler-errors
350 source-files.errors:error-types
351 source-files.errors:error-observers
354 vocabs:vocab-observers
355 vocabs.loader:add-vocab-root-hook
360 { } { "layouts" } vocab-tree-globals %
362 { } { "math.partial-dispatch" } vocab-tree-globals %
364 { } { "math.vectors.simd" } vocab-tree-globals %
366 { } { "peg" } vocab-tree-globals %
370 { } { "prettyprint.config" } vocab-tree-globals %
374 \ compiler.errors:compiler-errors ,
378 : cleared-globals ( -- seq )
391 "io-thread" "io.thread" lookup-word ,
394 "initial-thread" "threads" lookup-word ,
397 strip-io? [ io-backend , ] when
403 } vocab-tree-globals %
405 "windows-messages" "windows.messages" lookup-word [ , ] when*
408 : strip-global? ( name stripped-globals -- ? )
409 '[ _ member? ] [ tuple? ] bi or ;
411 : clear-global? ( name cleared-globals -- ? )
412 '[ _ member? ] [ string? ] bi or ;
414 : strip-globals ( -- )
416 "Stripping globals" show
417 stripped-globals :> to-strip
418 cleared-globals :> to-clear
420 [ drop to-strip strip-global? ] assoc-reject!
423 swap to-clear clear-global?
424 [ f swap value<< ] [ drop ] if
430 ! On all platforms, if deploy-io is 1, we strip out C streams.
431 ! On Unix, if deploy-io is 3, we strip out C streams as well.
432 ! On Windows, even if deploy-io is 3, C streams are still used
433 ! for the console, so don't strip it there.
435 native-io? os windows? not and
437 "Stripping C I/O" show
438 "vocab:tools/deploy/shaker/strip-c-io.factor" run-file
441 : compress ( pred post-process string -- )
442 "Compressing " prepend show
443 [ instances dup H{ } clone [ [ ] cache ] curry map ] dip call
446 : compress-object? ( obj -- ? )
448 { [ dup array? ] [ empty? ] }
449 { [ dup byte-array? ] [ drop t ] }
450 { [ dup string? ] [ drop t ] }
451 { [ dup wrapper? ] [ drop t ] }
455 : compress-objects ( -- )
456 [ compress-object? ] [ ] "objects" compress ;
458 : remain-compiled ( old new -- old new )
459 ! Quotations which were formerly compiled must remain
462 2dup [ quotation-compiled? ] [ quotation-compiled? not ] bi* and
463 [ nip jit-compile ] [ 2drop ] if
466 : compress-quotations ( -- )
467 [ quotation? ] [ remain-compiled ] "quotations" compress
468 [ quotation? ] instances [ f >>cached-effect f >>cache-counter drop ] each ;
472 : [:c] ( -- word ) ":c" "debugger" lookup-word ;
474 : [print-error] ( -- word ) "print-error" "debugger" lookup-word ;
476 : deploy-startup-quot ( word -- )
479 startup-hooks get values concat %
480 strip-debugger? [ , ] [
481 ! Don't reference 'try' directly since we don't want
482 ! to pull in the debugger and prettyprinter into every
487 [ _ execute( obj -- ) ] [
488 _ execute( obj -- ) nl
493 strip-io? [ [ flush ] % ] unless
498 : startup-stripper ( -- )
499 t parser-quiet? set-global
500 f output-stream set-global
501 [ V{ "resource:" } clone vocab-roots set-global ]
502 "vocabs.loader" startup-hooks get-global set-at ;
504 : next-method* ( method -- quot )
505 [ "method-class" word-prop ]
506 [ "method-generic" word-prop ] bi
509 : calls-next-method? ( method -- ? )
510 def>> flatten \ (call-next-method) swap member-eq? ;
512 : compute-next-methods ( -- )
513 [ standard-generic? ] instances [
514 "methods" word-prop values [ calls-next-method? ] filter
515 [ dup next-method* "next-method" set-word-prop ] each
517 "vocab:tools/deploy/shaker/next-methods.factor" run-file ;
519 : (clear-megamorphic-cache) ( i array -- )
520 ! Can't do any dispatch while clearing caches since that
521 ! might leave them in an inconsistent state.
523 2dup [ f ] 2dip set-array-nth
524 [ 1 + ] dip (clear-megamorphic-cache)
527 : clear-megamorphic-cache ( array -- )
528 [ 0 ] dip (clear-megamorphic-cache) ;
530 : find-megamorphic-caches ( -- seq )
531 "Finding megamorphic caches" show
532 [ standard-generic? ] instances [ def>> third ] map ;
534 : clear-megamorphic-caches ( cache -- )
535 "Clearing megamorphic caches" show
536 [ clear-megamorphic-cache ] each ;
538 : write-vocab-manifest ( vocab-manifest-out -- )
539 "Writing vocabulary manifest to " write dup print flush
540 loaded-vocab-names "VOCABS:" prefix
541 deploy-libraries get [ lookup-library path>> ] map members
542 "LIBRARIES:" prefix append
543 swap utf8 set-file-lines ;
545 : prepare-deploy-libraries ( -- )
546 "Preparing deployed libraries" show
547 deploy-libraries get [
549 [ path>> >deployed-library-path ] [ abi>> ] bi make-library
554 "deploy-libraries" "alien.libraries" lookup-word forget
555 "deploy-library" "alien.libraries" lookup-word forget
556 ">deployed-library-path" "alien.libraries.private" lookup-word forget
557 ] with-compilation-unit ;
559 : strip ( vocab-manifest-out -- )
560 [ write-vocab-manifest ] when*
562 prepare-deploy-libraries
571 strip-specialized-arrays
574 add-command-line-hook
576 strip-default-methods
577 strip-compiler-classes
578 ! we can't use the Factor debugger or Factor I/O anymore
579 f ERROR-HANDLER-QUOT set-special-object
580 deploy-vocab get vocab-main deploy-startup-quot
581 find-megamorphic-caches
588 clear-megamorphic-caches ;
590 : die-with ( error original-error -- * )
591 ! We don't want DCE to drop the error before the die call!
592 [ die 1 exit ] ( a -- * ) call-effect-unsafe ;
594 : die-with2 ( error original-error -- * )
595 ! We don't want DCE to drop the error before the die call!
596 [ die 1 exit ] ( a b -- * ) call-effect-unsafe ;
598 : deploy-error-handler ( quot -- )
601 [ original-error get die-with2 ]
602 ! Don't reference these words literally, if we're stripping the
603 ! debugger out we don't want to load the prettyprinter at all
604 [ [:c] execute( -- ) nl [print-error] execute( error -- ) flush ] if
608 : (deploy) ( final-image vocab-manifest-out vocab config -- )
609 ! Does the actual work of a deployment in the slave
615 "tools.errors" require
618 "ui.debugger" require
621 [ deploy-vocab set ] [ require ] [
623 "Vocabulary has no MAIN: word." print flush 1 exit
627 "Saving final image" show
629 ] deploy-error-handler
634 "vocab-manifest-out" get
636 "Deploying " write dup write "..." print
637 "deploy-config" get parse-file first