1 ! Copyright (C) 2007, 2010 Slava Pestov.
2 ! See https://factorcode.org/license.txt for BSD license.
3 USING: accessors alien.libraries arrays assocs byte-arrays
4 classes classes.builtin classes.private combinators
5 combinators.private command-line compiler.crossref
6 compiler.errors compiler.units continuations definitions generic
7 generic.single generic.standard grouping hashtables init io
8 io.backend io.encodings.utf8 io.files io.pathnames io.streams.c
9 kernel kernel.private make math memoize memory namespaces parser
10 parser.notes quotations sequences sequences.deep
11 sequences.private sets slots.private source-files
12 source-files.errors strings strings.parser system
13 tools.deploy.config vocabs vocabs.loader vocabs.loader.private
15 QUALIFIED: classes.private
16 IN: tools.deploy.shaker
18 ! This file is some hairy shit.
20 : add-command-line-hook ( -- )
23 command-line set-global
24 ] "command-line" startup-hooks get set-at ;
26 : set-stop-after-last-window? ( -- )
27 get-namestack [ "stop-after-last-window?" swap key? ] any? [
28 "ui-stop-after-last-window?" "ui.backend" lookup-word [
29 "stop-after-last-window?" get swap set-global
33 : strip-startup-hooks ( -- )
34 "Stripping startup hooks" show
40 [ startup-hooks get delete-at ] each
42 "threads" startup-hooks get delete-at
45 "io.backend" startup-hooks get delete-at
53 } [ startup-hooks get delete-at ] each
56 : strip-debugger ( -- )
57 strip-debugger? "debugger" lookup-vocab and [
58 "Stripping debugger" show
59 "vocab:tools/deploy/shaker/strip-debugger.factor"
63 : strip-ui-error-hook ( -- )
64 strip-debugger? deploy-ui? get and "ui" lookup-vocab and [
65 "Installing generic UI error hook" show
66 "vocab:tools/deploy/shaker/strip-ui-error-hook.factor"
72 "Stripping manual memory management debug code" show
73 "vocab:tools/deploy/shaker/strip-libc.factor"
77 : strip-destructors ( -- )
78 "Stripping destructor debug code" show
79 "vocab:tools/deploy/shaker/strip-destructors.factor"
83 "Stripping stack effect checking from call( and execute(" show
84 "vocab:tools/deploy/shaker/strip-call.factor" run-file ;
87 "cocoa" lookup-vocab [
88 "Stripping unused Cocoa methods" show
89 "vocab:tools/deploy/shaker/strip-cocoa.factor"
93 : strip-gobject ( -- )
94 "gobject-introspection.types" lookup-vocab [
95 "Stripping GObject type info" show
96 "vocab:tools/deploy/shaker/strip-gobject.factor"
100 : strip-gtk-icon ( -- )
101 "ui.backend.gtk" lookup-vocab [
102 "Stripping GTK icon loading code" show
103 "vocab:tools/deploy/shaker/strip-gtk-icon.factor"
107 : strip-specialized-arrays ( -- )
108 strip-dictionary? "specialized-arrays" lookup-vocab and [
109 "Stripping specialized arrays" show
110 "vocab:tools/deploy/shaker/strip-specialized-arrays.factor"
114 : strip-word-names ( words -- )
115 "Stripping word names" show
116 [ f >>name f >>vocabulary drop ] each ;
118 : strip-word-defs ( words -- )
119 "Stripping symbolic word definitions" show
120 [ [ ] >>def drop ] each ;
122 : strip-word-props ( stripped-props words -- )
123 "Stripping word properties" show
126 [ drop _ member? ] assoc-reject sift-values
131 : stripped-word-props ( -- seq )
146 "default-output-classes"
189 "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 ( -- set )
247 { "compiler" "stack-checker" } [
248 loaded-child-vocab-names
249 [ vocab-words ] map concat
251 ] map concat fast-set ;
253 : prune-decision-tree ( tree classes -- )
254 [ tuple class>type ] 2dip '[
260 [ drop _ in? ] assoc-reject
268 : strip-compiler-classes ( -- )
270 "Stripping compiler classes" show
271 [ single-generic? ] instances
272 compiler-classes '[ "decision-tree" word-prop _ prune-decision-tree ] each
275 : recursive-subst ( seq old new -- )
280 { [ 2over eq? ] [ 2nip ] }
281 ! recurse into arrays
282 { [ pick array? ] [ [ dup ] 2dip recursive-subst ] }
283 ! otherwise do nothing
288 : strip-default-method ( generic new-default -- )
290 [ "decision-tree" word-prop ]
291 [ "default-method" word-prop ] bi
295 : new-default-method ( -- gensym )
296 [ [ "No method" throw ] ( -- * ) define-temp ] with-compilation-unit ;
298 : strip-default-methods ( -- )
299 ! In a development image, each generic has its own default method.
300 ! This gives better error messages for runtime type errors, but
301 ! takes up space. For deployment we merge them all together.
303 "Stripping default methods" show
304 [ single-generic? ] instances
305 new-default-method '[ _ strip-default-method ] each
308 : vocab-tree-globals ( except names -- words )
309 [ loaded-child-vocab-names [ vocab-words ] map concat ] map concat
310 swap [ first2 lookup-word ] map sift diff ;
312 : stripped-globals ( -- seq )
314 "inspector-hook" "inspector" lookup-word ,
316 source-files:source-files
318 continuations:error-continuation
319 continuations:error-thread
320 continuations:restarts
323 "disposables" "destructors" lookup-word ,
325 "functor-words" "functors.backend" lookup-word ,
331 } vocab-tree-globals %
333 ! Don't want to strip globals from test programs
334 { } { "tools" } vocab-tree-globals
335 { } { "tools.deploy.test" } vocab-tree-globals diff %
337 deploy-unicode? get [
338 { } { "unicode" } vocab-tree-globals %
342 "libraries" "alien" lookup-word ,
344 { { "yield-hook" "compiler.utilities" } }
345 { "cpu" "compiler" } vocab-tree-globals %
350 classes.private:next-method-quot-cache
351 classes.private:class-and-cache
352 classes.private:class-not-cache
353 classes.private:class-or-cache
354 classes.private:class<=-cache
355 classes.private:classes-intersect-cache
356 classes.private:implementors-map
357 classes.private:update-map
359 compiler.crossref:compiled-crossref
360 compiler.crossref:generic-call-site-crossref
362 compiler.errors:compiler-errors
367 source-files.errors:error-types
368 source-files.errors:error-observers
371 vocabs:vocab-observers
372 vocabs.loader:add-vocab-root-hook
373 vocabs.parser:manifest
378 { } { "layouts" } vocab-tree-globals %
380 { } { "math.partial-dispatch" } vocab-tree-globals %
382 { } { "math.vectors.simd" } vocab-tree-globals %
384 { } { "peg" } vocab-tree-globals %
388 { } { "prettyprint.config" } vocab-tree-globals %
392 \ compiler.errors:compiler-errors ,
396 : cleared-globals ( -- seq )
409 "io-thread" "io.thread" lookup-word ,
412 "initial-thread" "threads" lookup-word ,
415 strip-io? [ io-backend , ] when
421 } vocab-tree-globals %
423 "windows-messages" "windows.messages" lookup-word [ , ] when*
426 : strip-global? ( name stripped-globals -- ? )
427 '[ _ member? ] [ tuple? ] bi or ;
429 : clear-global? ( name cleared-globals -- ? )
430 '[ _ member? ] [ string? ] bi or ;
432 : strip-globals ( -- )
434 "Stripping globals" show
435 stripped-globals :> to-strip
436 cleared-globals :> to-clear
438 [ drop to-strip strip-global? ] assoc-reject!
441 swap to-clear clear-global?
442 [ f swap value<< ] [ drop ] if
448 ! On all platforms, if deploy-io is 1, we strip out C streams.
449 ! On Unix, if deploy-io is 3, we strip out C streams as well.
450 ! On Windows, even if deploy-io is 3, C streams are still used
451 ! for the console, so don't strip it there.
453 native-io? os windows? not and
455 "Stripping C I/O" show
456 "vocab:tools/deploy/shaker/strip-c-io.factor" run-file
459 : compress ( pred post-process string -- )
460 "Compressing " prepend show
461 [ instances dup H{ } clone [ [ ] cache ] curry map ] dip call
464 : compress-object? ( obj -- ? )
466 { [ dup array? ] [ empty? ] }
467 { [ dup byte-array? ] [ drop t ] }
468 { [ dup string? ] [ drop t ] }
469 { [ dup wrapper? ] [ drop t ] }
473 : compress-objects ( -- )
474 [ compress-object? ] [ ] "objects" compress ;
476 : remain-compiled ( old new -- old new )
477 ! Quotations which were formerly compiled must remain
480 2dup [ quotation-compiled? ] [ quotation-compiled? not ] bi* and
481 [ nip jit-compile ] [ 2drop ] if
484 : compress-quotations ( -- )
485 [ quotation? ] [ remain-compiled ] "quotations" compress
486 [ quotation? ] instances [ f >>cached-effect f >>cache-counter drop ] each ;
490 : [:c] ( -- word ) ":c" "debugger" lookup-word ;
492 : [print-error] ( -- word ) "print-error" "debugger" lookup-word ;
494 : deploy-startup-quot ( word -- )
497 startup-hooks get values concat %
498 strip-debugger? [ , ] [
499 ! Don't reference 'try' directly since we don't want
500 ! to pull in the debugger and prettyprinter into every
505 [ _ execute( obj -- ) ] [
506 _ execute( obj -- ) nl
511 strip-io? [ [ flush ] % ] unless
516 : startup-stripper ( -- )
517 t parser-quiet? set-global
518 f output-stream set-global
519 [ V{ "resource:" } clone vocab-roots set-global ]
520 "vocabs.loader" startup-hooks get-global set-at ;
522 : next-method* ( method -- quot )
523 [ "method-class" word-prop ]
524 [ "method-generic" word-prop ] bi
527 : calls-next-method? ( method -- ? )
528 def>> flatten \ (call-next-method) swap member-eq? ;
530 : compute-next-methods ( -- )
531 [ standard-generic? ] instances [
532 "methods" word-prop values [ calls-next-method? ] filter
533 [ dup next-method* "next-method" set-word-prop ] each
535 "vocab:tools/deploy/shaker/next-methods.factor" run-file ;
537 : (clear-megamorphic-cache) ( i array -- )
538 ! Can't do any dispatch while clearing caches since that
539 ! might leave them in an inconsistent state.
541 2dup [ f ] 2dip set-array-nth
542 [ 1 + ] dip (clear-megamorphic-cache)
545 : clear-megamorphic-cache ( array -- )
546 [ 0 ] dip (clear-megamorphic-cache) ;
548 : find-megamorphic-caches ( -- seq )
549 "Finding megamorphic caches" show
550 [ standard-generic? ] instances [ def>> third ] map ;
552 : clear-megamorphic-caches ( cache -- )
553 "Clearing megamorphic caches" show
554 [ clear-megamorphic-cache ] each ;
556 : write-vocab-manifest ( vocab-manifest-out -- )
557 "Writing vocabulary manifest to " write dup print flush
558 loaded-vocab-names "VOCABS:" prefix
559 deploy-libraries get [ lookup-library path>> ] map members
560 "LIBRARIES:" prefix append
561 swap utf8 set-file-lines ;
563 : prepare-deploy-libraries ( -- )
564 "Preparing deployed libraries" show
565 deploy-libraries get [
567 [ path>> >deployed-library-path ] [ abi>> ] bi make-library
572 "deploy-libraries" "alien.libraries" lookup-word forget
573 "deploy-library" "alien.libraries" lookup-word forget
574 ">deployed-library-path" "alien.libraries.private" lookup-word forget
575 ] with-compilation-unit ;
577 : strip ( vocab-manifest-out -- )
578 [ write-vocab-manifest ] when*
580 prepare-deploy-libraries
589 strip-specialized-arrays
592 add-command-line-hook
594 strip-default-methods
595 strip-compiler-classes
596 ! we can't use the Factor debugger or Factor I/O anymore
597 f ERROR-HANDLER-QUOT set-special-object
598 deploy-vocab get vocab-main deploy-startup-quot
599 find-megamorphic-caches
606 clear-megamorphic-caches ;
608 : die-with ( error original-error -- * )
609 ! We don't want DCE to drop the error before the die call!
610 [ die 1 exit ] ( a -- * ) call-effect-unsafe ;
612 : die-with2 ( error original-error -- * )
613 ! We don't want DCE to drop the error before the die call!
614 [ die 1 exit ] ( a b -- * ) call-effect-unsafe ;
616 : deploy-error-handler ( quot -- )
619 [ original-error get die-with2 ]
620 ! Don't reference these words literally, if we're stripping the
621 ! debugger out we don't want to load the prettyprinter at all
622 [ [:c] execute( -- ) nl [print-error] execute( error -- ) flush ] if
626 : (deploy) ( final-image vocab-manifest-out vocab config -- )
627 ! Does the actual work of a deployment in the slave
633 "tools.errors" require
636 "ui.debugger" require
639 [ deploy-vocab namespaces:set ] [ require ] [
641 "Vocabulary has no MAIN: word." print flush 1 exit
644 set-stop-after-last-window?
646 "Saving final image" show
648 ] deploy-error-handler
653 "vocab-manifest-out" get
655 "Deploying " write dup write "..." print
656 "deploy-config" get parse-file first