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 ( -- )
38 command-line set-global
39 ] "command-line" startup-hooks get set-at ;
41 : strip-startup-hooks ( -- )
42 "Stripping startup hooks" show
49 [ startup-hooks get delete-at ] each
51 "threads" startup-hooks get delete-at
54 "io.backend" startup-hooks get delete-at
61 } [ startup-hooks get delete-at ] each
64 : strip-debugger ( -- )
65 strip-debugger? "debugger" lookup-vocab and [
66 "Stripping debugger" show
67 "vocab:tools/deploy/shaker/strip-debugger.factor"
71 : strip-ui-error-hook ( -- )
72 strip-debugger? deploy-ui? get and "ui" lookup-vocab and [
73 "Installing generic UI error hook" show
74 "vocab:tools/deploy/shaker/strip-ui-error-hook.factor"
80 "Stripping manual memory management debug code" show
81 "vocab:tools/deploy/shaker/strip-libc.factor"
85 : strip-destructors ( -- )
86 "Stripping destructor debug code" show
87 "vocab:tools/deploy/shaker/strip-destructors.factor"
91 "Stripping stack effect checking from call( and execute(" show
92 "vocab:tools/deploy/shaker/strip-call.factor" run-file ;
95 "cocoa" lookup-vocab [
96 "Stripping unused Cocoa methods" show
97 "vocab:tools/deploy/shaker/strip-cocoa.factor"
101 : strip-gobject ( -- )
102 "gobject-introspection.types" lookup-vocab [
103 "Stripping GObject type info" show
104 "vocab:tools/deploy/shaker/strip-gobject.factor"
108 : strip-gtk-icon ( -- )
109 "ui.backend.gtk" lookup-vocab [
110 "Stripping GTK icon loading code" show
111 "vocab:tools/deploy/shaker/strip-gtk-icon.factor"
115 : strip-specialized-arrays ( -- )
116 strip-dictionary? "specialized-arrays" lookup-vocab and [
117 "Stripping specialized arrays" show
118 "vocab:tools/deploy/shaker/strip-specialized-arrays.factor"
122 : strip-word-names ( words -- )
123 "Stripping word names" show
124 [ f >>name f >>vocabulary drop ] each ;
126 : strip-word-defs ( words -- )
127 "Stripping symbolic word definitions" show
128 [ "no-def-strip" word-prop ] reject
129 [ [ ] >>def drop ] each ;
131 : strip-word-props ( stripped-props words -- )
132 "Stripping word properties" show
135 [ drop _ member? ] assoc-reject sift-values
140 : stripped-word-props ( -- seq )
149 "effect-dependencies"
150 "definition-dependencies"
151 "conditional-dependencies"
160 "default-output-classes"
192 "predicate-definition"
230 deploy-c-types? get [
231 { "c-type" "struct-slots" "struct-align" } %
235 : strip-words ( props -- )
237 deploy-word-props? get [ 2dup strip-word-props ] unless
238 deploy-word-defs? get [ dup strip-word-defs ] unless
239 strip-word-names? [ dup strip-word-names strip-stack-traces ] when
242 : strip-memoized ( -- )
243 "Clearing memoized word caches" show
244 [ memoized? ] instances [ reset-memoized ] each ;
246 : compiler-classes ( -- seq )
247 { "compiler" "stack-checker" }
248 [ loaded-child-vocab-names [ vocab-words ] map concat [ class? ] filter ]
251 : prune-decision-tree ( tree classes -- )
252 [ tuple class>type ] 2dip '[
258 [ drop _ key? ] assoc-reject
266 : strip-compiler-classes ( -- )
268 "Stripping compiler classes" show
269 [ single-generic? ] instances
270 compiler-classes '[ "decision-tree" word-prop _ prune-decision-tree ] each
273 : recursive-subst ( seq old new -- )
278 { [ 2over eq? ] [ 2nip ] }
279 ! recurse into arrays
280 { [ pick array? ] [ [ dup ] 2dip recursive-subst ] }
281 ! otherwise do nothing
286 : strip-default-method ( generic new-default -- )
288 [ "decision-tree" word-prop ]
289 [ "default-method" word-prop ] bi
293 : new-default-method ( -- gensym )
294 [ [ "No method" throw ] ( -- * ) define-temp ] with-compilation-unit ;
296 : strip-default-methods ( -- )
297 ! In a development image, each generic has its own default method.
298 ! This gives better error messages for runtime type errors, but
299 ! takes up space. For deployment we merge them all together.
301 "Stripping default methods" show
302 [ single-generic? ] instances
303 new-default-method '[ _ strip-default-method ] each
306 : vocab-tree-globals ( except names -- words )
307 [ loaded-child-vocab-names [ vocab-words ] map concat ] map concat
308 swap [ first2 lookup-word ] map sift diff ;
310 : stripped-globals ( -- seq )
312 "inspector-hook" "inspector" lookup-word ,
314 source-files:source-files
316 continuations:error-continuation
317 continuations:error-thread
318 continuations:restarts
321 "disposables" "destructors" lookup-word ,
323 "functor-words" "functors.backend" lookup-word ,
329 } vocab-tree-globals %
331 ! Don't want to strip globals from test programs
332 { } { "tools" } vocab-tree-globals
333 { } { "tools.deploy.test" } vocab-tree-globals diff %
335 deploy-unicode? get [
336 { } { "unicode" } vocab-tree-globals %
340 "libraries" "alien" lookup-word ,
342 { { "yield-hook" "compiler.utilities" } }
343 { "cpu" "compiler" } vocab-tree-globals %
348 classes.private:next-method-quot-cache
349 classes.private:class-and-cache
350 classes.private:class-not-cache
351 classes.private:class-or-cache
352 classes.private:class<=-cache
353 classes.private:classes-intersect-cache
354 classes.private:implementors-map
355 classes.private:update-map
357 compiler.crossref:compiled-crossref
358 compiler.crossref:generic-call-site-crossref
360 compiler.errors:compiler-errors
365 source-files.errors:error-types
366 source-files.errors:error-observers
369 vocabs:vocab-observers
370 vocabs.loader:add-vocab-root-hook
375 { } { "layouts" } vocab-tree-globals %
377 { } { "math.partial-dispatch" } vocab-tree-globals %
379 { } { "math.vectors.simd" } vocab-tree-globals %
381 { } { "peg" } vocab-tree-globals %
385 { } { "prettyprint.config" } vocab-tree-globals %
389 \ compiler.errors:compiler-errors ,
393 : cleared-globals ( -- seq )
406 "io-thread" "io.thread" lookup-word ,
409 "initial-thread" "threads" lookup-word ,
412 strip-io? [ io-backend , ] when
418 } vocab-tree-globals %
420 "windows-messages" "windows.messages" lookup-word [ , ] when*
423 : strip-global? ( name stripped-globals -- ? )
424 '[ _ member? ] [ tuple? ] bi or ;
426 : clear-global? ( name cleared-globals -- ? )
427 '[ _ member? ] [ string? ] bi or ;
429 : strip-globals ( -- )
431 "Stripping globals" show
432 stripped-globals :> to-strip
433 cleared-globals :> to-clear
435 [ drop to-strip strip-global? ] assoc-reject!
438 swap to-clear clear-global?
439 [ f swap value<< ] [ drop ] if
445 ! On all platforms, if deploy-io is 1, we strip out C streams.
446 ! On Unix, if deploy-io is 3, we strip out C streams as well.
447 ! On Windows, even if deploy-io is 3, C streams are still used
448 ! for the console, so don't strip it there.
450 native-io? os windows? not and
452 "Stripping C I/O" show
453 "vocab:tools/deploy/shaker/strip-c-io.factor" run-file
456 : compress ( pred post-process string -- )
457 "Compressing " prepend show
458 [ instances dup H{ } clone [ [ ] cache ] curry map ] dip call
461 : compress-object? ( obj -- ? )
463 { [ dup array? ] [ empty? ] }
464 { [ dup byte-array? ] [ drop t ] }
465 { [ dup string? ] [ drop t ] }
466 { [ dup wrapper? ] [ drop t ] }
470 : compress-objects ( -- )
471 [ compress-object? ] [ ] "objects" compress ;
473 : remain-compiled ( old new -- old new )
474 ! Quotations which were formerly compiled must remain
477 2dup [ quot-compiled? ] [ quot-compiled? not ] bi* and
478 [ nip jit-compile ] [ 2drop ] if
481 : compress-quotations ( -- )
482 [ quotation? ] [ remain-compiled ] "quotations" compress
483 [ quotation? ] instances [ f >>cached-effect f >>cache-counter drop ] each ;
487 : [:c] ( -- word ) ":c" "debugger" lookup-word ;
489 : [print-error] ( -- word ) "print-error" "debugger" lookup-word ;
491 : deploy-startup-quot ( word -- )
494 startup-hooks get values concat %
495 strip-debugger? [ , ] [
496 ! Don't reference 'try' directly since we don't want
497 ! to pull in the debugger and prettyprinter into every
502 [ _ execute( obj -- ) ] [
503 _ execute( obj -- ) nl
508 strip-io? [ [ flush ] % ] unless
513 : startup-stripper ( -- )
514 t parser-quiet? set-global
515 f output-stream set-global
516 [ V{ "resource:" } clone vocab-roots set-global ]
517 "vocabs.loader" startup-hooks get-global set-at ;
519 : next-method* ( method -- quot )
520 [ "method-class" word-prop ]
521 [ "method-generic" word-prop ] bi
524 : calls-next-method? ( method -- ? )
525 def>> flatten \ (call-next-method) swap member-eq? ;
527 : compute-next-methods ( -- )
528 [ standard-generic? ] instances [
529 "methods" word-prop values [ calls-next-method? ] filter
530 [ dup next-method* "next-method" set-word-prop ] each
532 "vocab:tools/deploy/shaker/next-methods.factor" run-file ;
534 : (clear-megamorphic-cache) ( i array -- )
535 ! Can't do any dispatch while clearing caches since that
536 ! might leave them in an inconsistent state.
538 2dup [ f ] 2dip set-array-nth
539 [ 1 + ] dip (clear-megamorphic-cache)
542 : clear-megamorphic-cache ( array -- )
543 [ 0 ] dip (clear-megamorphic-cache) ;
545 : find-megamorphic-caches ( -- seq )
546 "Finding megamorphic caches" show
547 [ standard-generic? ] instances [ def>> third ] map ;
549 : clear-megamorphic-caches ( cache -- )
550 "Clearing megamorphic caches" show
551 [ clear-megamorphic-cache ] each ;
553 : write-vocab-manifest ( vocab-manifest-out -- )
554 "Writing vocabulary manifest to " write dup print flush
555 loaded-vocab-names "VOCABS:" prefix
556 deploy-libraries get [ lookup-library path>> ] map members
557 "LIBRARIES:" prefix append
558 swap utf8 set-file-lines ;
560 : prepare-deploy-libraries ( -- )
561 "Preparing deployed libraries" show
562 deploy-libraries get [
564 [ path>> >deployed-library-path ] [ abi>> ] bi make-library
569 "deploy-libraries" "alien.libraries" lookup-word forget
570 "deploy-library" "alien.libraries" lookup-word forget
571 ">deployed-library-path" "alien.libraries.private" lookup-word forget
572 ] with-compilation-unit ;
574 : strip ( vocab-manifest-out -- )
575 [ write-vocab-manifest ] when*
577 prepare-deploy-libraries
586 strip-specialized-arrays
589 add-command-line-hook
591 strip-default-methods
592 strip-compiler-classes
593 ! we can't use the Factor debugger or Factor I/O anymore
594 f ERROR-HANDLER-QUOT set-special-object
595 deploy-vocab get vocab-main deploy-startup-quot
596 find-megamorphic-caches
603 clear-megamorphic-caches ;
605 : die-with ( error original-error -- * )
606 #! We don't want DCE to drop the error before the die call!
607 [ die 1 exit ] ( a -- * ) call-effect-unsafe ;
609 : die-with2 ( error original-error -- * )
610 #! We don't want DCE to drop the error before the die call!
611 [ die 1 exit ] ( a b -- * ) call-effect-unsafe ;
613 : deploy-error-handler ( quot -- )
616 [ original-error get die-with2 ]
617 ! Don't reference these words literally, if we're stripping the
618 ! debugger out we don't want to load the prettyprinter at all
619 [ [:c] execute( -- ) nl [print-error] execute( error -- ) flush ] if
623 : (deploy) ( final-image vocab-manifest-out vocab config -- )
624 #! Does the actual work of a deployment in the slave
630 "tools.errors" require
633 "ui.debugger" require
636 [ deploy-vocab set ] [ require ] [
638 "Vocabulary has no MAIN: word." print flush 1 exit
642 "Saving final image" show
644 ] deploy-error-handler
649 "vocab-manifest-out" get
651 "Deploying " write dup write "..." print
652 "deploy-config" get parse-file first