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 FROM: assocs => change-at ;
27 FROM: namespaces => set ;
28 FROM: sequences => change-nth ;
29 FROM: sets => members ;
30 IN: tools.deploy.shaker
32 ! This file is some hairy shit.
34 : add-command-line-hook ( -- )
35 [ (command-line) command-line set-global ] "command-line"
36 startup-hooks get set-at ;
38 : strip-startup-hooks ( -- )
39 "Stripping startup hooks" show
46 [ startup-hooks get delete-at ] each
48 "threads" startup-hooks get delete-at
51 "io.backend" startup-hooks get delete-at
58 } [ startup-hooks get delete-at ] each
61 : strip-debugger ( -- )
62 strip-debugger? "debugger" lookup-vocab and [
63 "Stripping debugger" show
64 "vocab:tools/deploy/shaker/strip-debugger.factor"
68 : strip-ui-error-hook ( -- )
69 strip-debugger? deploy-ui? get and "ui" lookup-vocab and [
70 "Installing generic UI error hook" show
71 "vocab:tools/deploy/shaker/strip-ui-error-hook.factor"
77 "Stripping manual memory management debug code" show
78 "vocab:tools/deploy/shaker/strip-libc.factor"
82 : strip-destructors ( -- )
83 "Stripping destructor debug code" show
84 "vocab:tools/deploy/shaker/strip-destructors.factor"
88 "Stripping stack effect checking from call( and execute(" show
89 "vocab:tools/deploy/shaker/strip-call.factor" run-file ;
92 "cocoa" lookup-vocab [
93 "Stripping unused Cocoa methods" show
94 "vocab:tools/deploy/shaker/strip-cocoa.factor"
98 : strip-gobject ( -- )
99 "gobject-introspection.types" lookup-vocab [
100 "Stripping GObject type info" show
101 "vocab:tools/deploy/shaker/strip-gobject.factor"
105 : strip-gtk-icon ( -- )
106 "ui.backend.gtk" lookup-vocab [
107 "Stripping GTK icon loading code" show
108 "vocab:tools/deploy/shaker/strip-gtk-icon.factor"
112 : strip-specialized-arrays ( -- )
113 strip-dictionary? "specialized-arrays" lookup-vocab and [
114 "Stripping specialized arrays" show
115 "vocab:tools/deploy/shaker/strip-specialized-arrays.factor"
119 : strip-word-names ( words -- )
120 "Stripping word names" show
121 [ f >>name f >>vocabulary drop ] each ;
123 : strip-word-defs ( words -- )
124 "Stripping symbolic word definitions" show
125 [ "no-def-strip" word-prop not ] filter
126 [ [ ] >>def drop ] each ;
128 : sift-assoc ( assoc -- assoc' ) [ nip ] assoc-filter ;
130 : strip-word-props ( stripped-props words -- )
131 "Stripping word properties" show
134 [ drop _ member? not ] assoc-filter sift-assoc
139 : stripped-word-props ( -- seq )
148 "effect-dependencies"
149 "definition-dependencies"
150 "conditional-dependencies"
159 "default-output-classes"
191 "predicate-definition"
229 deploy-c-types? get [
230 { "c-type" "struct-slots" "struct-align" } %
234 : strip-words ( props -- )
236 deploy-word-props? get [ 2dup strip-word-props ] unless
237 deploy-word-defs? get [ dup strip-word-defs ] unless
238 strip-word-names? [ dup strip-word-names strip-stack-traces ] when
241 : strip-memoized ( -- )
242 "Clearing memoized word caches" show
243 [ memoized? ] instances [ reset-memoized ] each ;
245 : compiler-classes ( -- seq )
246 { "compiler" "stack-checker" }
247 [ child-vocabs [ words ] map concat [ class? ] filter ]
250 : prune-decision-tree ( tree classes -- )
251 [ tuple class>type ] 2dip '[
257 [ drop _ key? not ] assoc-filter
265 : strip-compiler-classes ( -- )
267 "Stripping compiler classes" show
268 [ single-generic? ] instances
269 compiler-classes '[ "decision-tree" word-prop _ prune-decision-tree ] each
272 : recursive-subst ( seq old new -- )
277 { [ 3dup drop eq? ] [ 2nip ] }
278 ! recurse into arrays
279 { [ pick array? ] [ [ dup ] 2dip recursive-subst ] }
280 ! otherwise do nothing
285 : strip-default-method ( generic new-default -- )
287 [ "decision-tree" word-prop ]
288 [ "default-method" word-prop ] bi
292 : new-default-method ( -- gensym )
293 [ [ "No method" throw ] ( -- * ) define-temp ] with-compilation-unit ;
295 : strip-default-methods ( -- )
296 ! In a development image, each generic has its own default method.
297 ! This gives better error messages for runtime type errors, but
298 ! takes up space. For deployment we merge them all together.
300 "Stripping default methods" show
301 [ single-generic? ] instances
302 new-default-method '[ _ strip-default-method ] each
305 : vocab-tree-globals ( except names -- words )
306 [ child-vocabs [ words ] map concat ] map concat
307 swap [ first2 lookup-word ] map sift diff ;
309 : stripped-globals ( -- seq )
311 "inspector-hook" "inspector" lookup-word ,
313 source-files:source-files
315 continuations:error-continuation
316 continuations:error-thread
317 continuations:restarts
320 "disposables" "destructors" lookup-word ,
322 "functor-words" "functors.backend" lookup-word ,
328 } vocab-tree-globals %
330 ! Don't want to strip globals from test programs
331 { } { "tools" } vocab-tree-globals
332 { } { "tools.deploy.test" } vocab-tree-globals diff %
334 deploy-unicode? get [
335 { } { "unicode" } vocab-tree-globals %
339 "libraries" "alien" lookup-word ,
341 { { "yield-hook" "compiler.utilities" } }
342 { "cpu" "compiler" } vocab-tree-globals %
347 classes.private:next-method-quot-cache
348 classes.private:class-and-cache
349 classes.private:class-not-cache
350 classes.private:class-or-cache
351 classes.private:class<=-cache
352 classes.private:classes-intersect-cache
353 classes.private:implementors-map
354 classes.private:update-map
356 compiler.crossref:compiled-crossref
357 compiler.crossref:generic-call-site-crossref
359 compiler.errors:compiler-errors
364 source-files.errors:error-types
365 source-files.errors:error-observers
368 vocabs:vocab-observers
369 vocabs.loader:add-vocab-root-hook
374 { } { "layouts" } vocab-tree-globals %
376 { } { "math.partial-dispatch" } vocab-tree-globals %
378 { } { "math.vectors.simd" } vocab-tree-globals %
380 { } { "peg" } vocab-tree-globals %
384 { } { "prettyprint.config" } vocab-tree-globals %
388 \ compiler.errors:compiler-errors ,
392 : cleared-globals ( -- seq )
405 "io-thread" "io.thread" lookup-word ,
408 "initial-thread" "threads" lookup-word ,
411 strip-io? [ io-backend , ] when
417 } vocab-tree-globals %
419 "windows-messages" "windows.messages" lookup-word [ , ] when*
422 : strip-global? ( name stripped-globals -- ? )
423 '[ _ member? ] [ tuple? ] bi or ;
425 : clear-global? ( name cleared-globals -- ? )
426 '[ _ member? ] [ string? ] bi or ;
428 : strip-globals ( -- )
430 "Stripping globals" show
431 stripped-globals :> to-strip
432 cleared-globals :> to-clear
434 [ drop to-strip strip-global? not ] assoc-filter!
437 swap to-clear clear-global?
438 [ f swap value<< ] [ drop ] if
444 ! On all platforms, if deploy-io is 1, we strip out C streams.
445 ! On Unix, if deploy-io is 3, we strip out C streams as well.
446 ! On Windows, even if deploy-io is 3, C streams are still used
447 ! for the console, so don't strip it there.
449 native-io? os windows? not and
451 "Stripping C I/O" show
452 "vocab:tools/deploy/shaker/strip-c-io.factor" run-file
455 : compress ( pred post-process string -- )
456 "Compressing " prepend show
457 [ instances dup H{ } clone [ [ ] cache ] curry map ] dip call
460 : compress-object? ( obj -- ? )
462 { [ dup array? ] [ empty? ] }
463 { [ dup byte-array? ] [ drop t ] }
464 { [ dup string? ] [ drop t ] }
465 { [ dup wrapper? ] [ drop t ] }
469 : compress-objects ( -- )
470 [ compress-object? ] [ ] "objects" compress ;
472 : remain-compiled ( old new -- old new )
473 ! Quotations which were formerly compiled must remain
476 2dup [ quot-compiled? ] [ quot-compiled? not ] bi* and
477 [ nip jit-compile ] [ 2drop ] if
480 : compress-quotations ( -- )
481 [ quotation? ] [ remain-compiled ] "quotations" compress
482 [ quotation? ] instances [ f >>cached-effect f >>cache-counter drop ] each ;
486 : [:c] ( -- word ) ":c" "debugger" lookup-word ;
488 : [print-error] ( -- word ) "print-error" "debugger" lookup-word ;
490 : deploy-startup-quot ( word -- )
493 startup-hooks get values concat %
494 strip-debugger? [ , ] [
495 ! Don't reference 'try' directly since we don't want
496 ! to pull in the debugger and prettyprinter into every
501 [ _ execute( obj -- ) ] [
502 _ execute( obj -- ) nl
507 strip-io? [ [ flush ] % ] unless
512 : startup-stripper ( -- )
513 t parser-quiet? set-global
514 f output-stream set-global
515 [ V{ "resource:" } clone vocab-roots set-global ]
516 "vocabs.loader" startup-hooks get-global set-at ;
518 : next-method* ( method -- quot )
519 [ "method-class" word-prop ]
520 [ "method-generic" word-prop ] bi
523 : calls-next-method? ( method -- ? )
524 def>> flatten \ (call-next-method) swap member-eq? ;
526 : compute-next-methods ( -- )
527 [ standard-generic? ] instances [
528 "methods" word-prop values [ calls-next-method? ] filter
529 [ dup next-method* "next-method" set-word-prop ] each
531 "vocab:tools/deploy/shaker/next-methods.factor" run-file ;
533 : (clear-megamorphic-cache) ( i array -- )
534 ! Can't do any dispatch while clearing caches since that
535 ! might leave them in an inconsistent state.
537 2dup [ f ] 2dip set-array-nth
538 [ 1 + ] dip (clear-megamorphic-cache)
541 : clear-megamorphic-cache ( array -- )
542 [ 0 ] dip (clear-megamorphic-cache) ;
544 : find-megamorphic-caches ( -- seq )
545 "Finding megamorphic caches" show
546 [ standard-generic? ] instances [ def>> third ] map ;
548 : clear-megamorphic-caches ( cache -- )
549 "Clearing megamorphic caches" show
550 [ clear-megamorphic-cache ] each ;
552 : write-vocab-manifest ( vocab-manifest-out -- )
553 "Writing vocabulary manifest to " write dup print flush
554 vocabs "VOCABS:" prefix
555 deploy-libraries get [ library path>> ] map members "LIBRARIES:" prefix append
556 swap utf8 set-file-lines ;
558 : prepare-deploy-libraries ( -- )
559 "Preparing deployed libraries" show
560 deploy-libraries get [
562 [ path>> >deployed-library-path ] [ abi>> ] bi <library>
567 "deploy-libraries" "alien.libraries" lookup-word forget
568 "deploy-library" "alien.libraries" lookup-word forget
569 ">deployed-library-path" "alien.libraries.private" lookup-word forget
570 ] with-compilation-unit ;
572 : strip ( vocab-manifest-out -- )
573 [ write-vocab-manifest ] when*
575 prepare-deploy-libraries
584 strip-specialized-arrays
587 add-command-line-hook
589 strip-default-methods
590 strip-compiler-classes
591 ! we can't use the Factor debugger or Factor I/O anymore
592 f ERROR-HANDLER-QUOT set-special-object
593 deploy-vocab get vocab-main deploy-startup-quot
594 find-megamorphic-caches
601 clear-megamorphic-caches ;
603 : die-with ( error original-error -- * )
604 #! We don't want DCE to drop the error before the die call!
605 [ die 1 exit ] ( a -- * ) call-effect-unsafe ;
607 : die-with2 ( error original-error -- * )
608 #! We don't want DCE to drop the error before the die call!
609 [ die 1 exit ] ( a b -- * ) call-effect-unsafe ;
611 : deploy-error-handler ( quot -- )
614 [ original-error get die-with2 ]
615 ! Don't reference these words literally, if we're stripping the
616 ! debugger out we don't want to load the prettyprinter at all
617 [ [:c] execute( -- ) nl [print-error] execute( error -- ) flush ] if
621 : (deploy) ( final-image vocab-manifest-out vocab config -- )
622 #! Does the actual work of a deployment in the slave
628 "tools.errors" require
631 "ui.debugger" require
634 [ deploy-vocab set ] [ require ] [
636 "Vocabulary has no MAIN: word." print flush 1 exit
640 "Saving final image" show
642 ] deploy-error-handler
647 "vocab-manifest-out" get
649 "Deploying " write dup write "..." print
650 "deploy-config" get parse-file first