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 ;
13 QUALIFIED: bootstrap.stage2
14 QUALIFIED: classes.private
15 QUALIFIED: compiler.crossref
16 QUALIFIED: compiler.errors
17 QUALIFIED: continuations
18 QUALIFIED: definitions
21 QUALIFIED: source-files
22 QUALIFIED: source-files.errors
24 QUALIFIED: vocabs.loader
25 FROM: assocs => change-at ;
26 FROM: namespaces => set ;
27 FROM: sequences => change-nth ;
28 FROM: sets => members ;
29 IN: tools.deploy.shaker
31 ! This file is some hairy shit.
33 : add-command-line-hook ( -- )
34 [ (command-line) command-line set-global ] "command-line"
35 startup-hooks get set-at ;
37 : strip-startup-hooks ( -- )
38 "Stripping startup hooks" show
45 [ startup-hooks get delete-at ] each
47 "threads" startup-hooks get delete-at
50 "io.backend" startup-hooks get delete-at
57 } [ startup-hooks get delete-at ] each
60 : strip-debugger ( -- )
61 strip-debugger? "debugger" vocab and [
62 "Stripping debugger" show
63 "vocab:tools/deploy/shaker/strip-debugger.factor"
67 : strip-ui-error-hook ( -- )
68 strip-debugger? deploy-ui? get and "ui" vocab and [
69 "Installing generic UI error hook" show
70 "vocab:tools/deploy/shaker/strip-ui-error-hook.factor"
76 "Stripping manual memory management debug code" show
77 "vocab:tools/deploy/shaker/strip-libc.factor"
81 : strip-destructors ( -- )
82 "Stripping destructor debug code" show
83 "vocab:tools/deploy/shaker/strip-destructors.factor"
87 "Stripping stack effect checking from call( and execute(" show
88 "vocab:tools/deploy/shaker/strip-call.factor" run-file ;
92 "Stripping unused Cocoa methods" show
93 "vocab:tools/deploy/shaker/strip-cocoa.factor"
97 : strip-gobject ( -- )
98 "gobject-introspection.types" vocab [
99 "Stripping GObject type info" show
100 "vocab:tools/deploy/shaker/strip-gobject.factor"
104 : strip-gtk-icon ( -- )
105 "ui.backend.gtk" vocab [
106 "Stripping GTK icon loading code" show
107 "vocab:tools/deploy/shaker/strip-gtk-icon.factor"
111 : strip-specialized-arrays ( -- )
112 strip-dictionary? "specialized-arrays" vocab and [
113 "Stripping specialized arrays" show
114 "vocab:tools/deploy/shaker/strip-specialized-arrays.factor"
118 : strip-word-names ( words -- )
119 "Stripping word names" show
120 [ f >>name f >>vocabulary drop ] each ;
122 : strip-word-defs ( words -- )
123 "Stripping symbolic word definitions" show
124 [ "no-def-strip" word-prop not ] filter
125 [ [ ] >>def drop ] each ;
127 : sift-assoc ( assoc -- assoc' ) [ nip ] assoc-filter ;
129 : strip-word-props ( stripped-props words -- )
130 "Stripping word properties" show
133 [ drop _ member? not ] assoc-filter sift-assoc
138 : stripped-word-props ( -- seq )
147 "effect-dependencies"
148 "definition-dependencies"
149 "conditional-dependencies"
158 "default-output-classes"
190 "predicate-definition"
228 deploy-c-types? get [
229 { "c-type" "struct-slots" "struct-align" } %
233 : strip-words ( props -- )
235 deploy-word-props? get [ 2dup strip-word-props ] unless
236 deploy-word-defs? get [ dup strip-word-defs ] unless
237 strip-word-names? [ dup strip-word-names strip-stack-traces ] when
240 : compiler-classes ( -- seq )
241 { "compiler" "stack-checker" }
242 [ child-vocabs [ words ] map concat [ class? ] filter ]
245 : prune-decision-tree ( tree classes -- )
246 [ tuple class>type ] 2dip '[
252 [ drop _ key? not ] assoc-filter
260 : strip-compiler-classes ( -- )
262 "Stripping compiler classes" show
263 [ single-generic? ] instances
264 compiler-classes '[ "decision-tree" word-prop _ prune-decision-tree ] each
267 : recursive-subst ( seq old new -- )
272 { [ 3dup drop eq? ] [ 2nip ] }
273 ! recurse into arrays
274 { [ pick array? ] [ [ dup ] 2dip recursive-subst ] }
275 ! otherwise do nothing
280 : strip-default-method ( generic new-default -- )
282 [ "decision-tree" word-prop ]
283 [ "default-method" word-prop ] bi
287 : new-default-method ( -- gensym )
288 [ [ "No method" throw ] ( -- * ) define-temp ] with-compilation-unit ;
290 : strip-default-methods ( -- )
291 ! In a development image, each generic has its own default method.
292 ! This gives better error messages for runtime type errors, but
293 ! takes up space. For deployment we merge them all together.
295 "Stripping default methods" show
296 [ single-generic? ] instances
297 new-default-method '[ _ strip-default-method ] each
300 : strip-vocab-globals ( except names -- words )
301 [ child-vocabs [ words ] map concat ] map concat
302 swap [ first2 lookup ] map sift diff ;
304 : stripped-globals ( -- seq )
306 "inspector-hook" "inspector" lookup ,
310 continuations:error-continuation
311 continuations:error-thread
312 continuations:restarts
314 source-files:source-files
323 "io-thread" "io.thread" lookup ,
325 "disposables" "destructors" lookup ,
327 "functor-words" "functors.backend" lookup ,
330 "initial-thread" "threads" lookup ,
333 strip-io? [ io-backend , ] when
343 } strip-vocab-globals %
346 "libraries" "alien" lookup ,
348 { { "yield-hook" "compiler.utilities" } }
349 { "cpu" "compiler" } strip-vocab-globals %
354 classes.private:next-method-quot-cache
355 classes.private:class-and-cache
356 classes.private:class-not-cache
357 classes.private:class-or-cache
358 classes.private:class<=-cache
359 classes.private:classes-intersect-cache
360 classes.private:implementors-map
361 classes.private:update-map
363 compiler.crossref:compiled-crossref
364 compiler.crossref:generic-call-site-crossref
366 compiler.errors:compiler-errors
372 source-files.errors:error-types
373 source-files.errors:error-observers
375 vocabs:load-vocab-hook
376 vocabs:vocab-observers
377 vocabs.loader:add-vocab-root-hook
382 { } { "layouts" } strip-vocab-globals %
384 { } { "math.partial-dispatch" } strip-vocab-globals %
386 { } { "math.vectors.simd" } strip-vocab-globals %
388 { } { "peg" } strip-vocab-globals %
392 { } { "prettyprint.config" } strip-vocab-globals %
396 \ compiler.errors:compiler-errors ,
399 "windows-messages" "windows.messages" lookup [ , ] when*
402 : strip-globals ( stripped-globals -- )
404 "Stripping globals" show
406 '[ drop _ member? not ] assoc-filter
407 [ drop string? not ] assoc-filter ! strip CLI args
409 21 set-special-object
413 ! On all platforms, if deploy-io is 1, we strip out C streams.
414 ! On Unix, if deploy-io is 3, we strip out C streams as well.
415 ! On Windows, even if deploy-io is 3, C streams are still used
416 ! for the console, so don't strip it there.
418 native-io? os windows? not and
420 "Stripping C I/O" show
421 "vocab:tools/deploy/shaker/strip-c-io.factor" run-file
424 : compress ( pred post-process string -- )
425 "Compressing " prepend show
426 [ instances dup H{ } clone [ [ ] cache ] curry map ] dip call
429 : compress-object? ( obj -- ? )
431 { [ dup array? ] [ empty? ] }
432 { [ dup byte-array? ] [ drop t ] }
433 { [ dup string? ] [ drop t ] }
434 { [ dup wrapper? ] [ drop t ] }
438 : compress-objects ( -- )
439 [ compress-object? ] [ ] "objects" compress ;
441 : remain-compiled ( old new -- old new )
442 ! Quotations which were formerly compiled must remain
445 2dup [ quot-compiled? ] [ quot-compiled? not ] bi* and
446 [ nip jit-compile ] [ 2drop ] if
449 : compress-quotations ( -- )
450 [ quotation? ] [ remain-compiled ] "quotations" compress
451 [ quotation? ] instances [ f >>cached-effect f >>cache-counter drop ] each ;
455 : [:c] ( -- word ) ":c" "debugger" lookup ;
457 : [print-error] ( -- word ) "print-error" "debugger" lookup ;
459 : deploy-startup-quot ( word -- )
462 startup-hooks get values concat %
463 strip-debugger? [ , ] [
464 ! Don't reference 'try' directly since we don't want
465 ! to pull in the debugger and prettyprinter into every
470 [ _ execute( obj -- ) ] [
471 _ execute( obj -- ) nl
476 strip-io? [ [ flush ] % ] unless
481 : startup-stripper ( -- )
482 t parser-quiet? set-global
483 f output-stream set-global
484 [ V{ "resource:" } clone vocab-roots set-global ]
485 "vocabs.loader" startup-hooks get-global set-at ;
487 : next-method* ( method -- quot )
488 [ "method-class" word-prop ]
489 [ "method-generic" word-prop ] bi
492 : calls-next-method? ( method -- ? )
493 def>> flatten \ (call-next-method) swap member-eq? ;
495 : compute-next-methods ( -- )
496 [ standard-generic? ] instances [
497 "methods" word-prop values [ calls-next-method? ] filter
498 [ dup next-method* "next-method" set-word-prop ] each
500 "vocab:tools/deploy/shaker/next-methods.factor" run-file ;
502 : (clear-megamorphic-cache) ( i array -- )
503 ! Can't do any dispatch while clearing caches since that
504 ! might leave them in an inconsistent state.
506 2dup [ f ] 2dip set-array-nth
507 [ 1 + ] dip (clear-megamorphic-cache)
510 : clear-megamorphic-cache ( array -- )
511 [ 0 ] dip (clear-megamorphic-cache) ;
513 : find-megamorphic-caches ( -- seq )
514 "Finding megamorphic caches" show
515 [ standard-generic? ] instances [ def>> third ] map ;
517 : clear-megamorphic-caches ( cache -- )
518 "Clearing megamorphic caches" show
519 [ clear-megamorphic-cache ] each ;
521 : write-vocab-manifest ( vocab-manifest-out -- )
522 "Writing vocabulary manifest to " write dup print flush
523 vocabs "VOCABS:" prefix
524 deploy-libraries get [ library path>> ] map members "LIBRARIES:" prefix append
525 swap utf8 set-file-lines ;
527 : prepare-deploy-libraries ( -- )
528 "Preparing deployed libraries" show
529 deploy-libraries get [
531 [ path>> >deployed-library-path ] [ abi>> ] bi <library>
536 "deploy-libraries" "alien.libraries" lookup forget
537 "deploy-library" "alien.libraries" lookup forget
538 ">deployed-library-path" "alien.libraries.private" lookup forget
539 ] with-compilation-unit ;
541 : strip ( vocab-manifest-out -- )
542 [ write-vocab-manifest ] when*
544 prepare-deploy-libraries
553 strip-specialized-arrays
556 add-command-line-hook
558 strip-default-methods
559 strip-compiler-classes
560 f 5 set-special-object ! we can't use the Factor debugger or Factor I/O anymore
561 deploy-vocab get vocab-main deploy-startup-quot
562 find-megamorphic-caches
564 stripped-globals strip-globals
568 clear-megamorphic-caches ;
570 : die-with ( error original-error -- * )
571 #! We don't want DCE to drop the error before the die call!
572 [ die 1 exit ] ( a -- * ) call-effect-unsafe ;
574 : die-with2 ( error original-error -- * )
575 #! We don't want DCE to drop the error before the die call!
576 [ die 1 exit ] ( a b -- * ) call-effect-unsafe ;
578 : deploy-error-handler ( quot -- )
581 [ original-error get die-with2 ]
582 ! Don't reference these words literally, if we're stripping the
583 ! debugger out we don't want to load the prettyprinter at all
584 [ [:c] execute( -- ) nl [print-error] execute( error -- ) flush ] if
588 : (deploy) ( final-image vocab-manifest-out vocab config -- )
589 #! Does the actual work of a deployment in the slave
595 "tools.errors" require
598 "ui.debugger" require
601 [ deploy-vocab set ] [ require ] [
603 "Vocabulary has no MAIN: word." print flush 1 exit
607 "Saving final image" show
609 ] deploy-error-handler
614 "vocab-manifest-out" get
616 "Deploying " write dup write "..." print
617 "deploy-config" get parse-file first