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: alien.libraries.private => >deployed-library-path ;
26 FROM: namespaces => set ;
27 FROM: sets => members ;
28 IN: tools.deploy.shaker
30 ! This file is some hairy shit.
32 : add-command-line-hook ( -- )
33 [ (command-line) command-line set-global ] "command-line"
34 startup-hooks get set-at ;
36 : strip-startup-hooks ( -- )
37 "Stripping startup hooks" show
44 [ startup-hooks get delete-at ] each
46 "threads" startup-hooks get delete-at
49 "io.backend" startup-hooks get delete-at
56 } [ startup-hooks get delete-at ] each
59 : strip-debugger ( -- )
60 strip-debugger? "debugger" vocab and [
61 "Stripping debugger" show
62 "vocab:tools/deploy/shaker/strip-debugger.factor"
66 : strip-ui-error-hook ( -- )
67 strip-debugger? deploy-ui? get and "ui" vocab and [
68 "Installing generic UI error hook" show
69 "vocab:tools/deploy/shaker/strip-ui-error-hook.factor"
75 "Stripping manual memory management debug code" show
76 "vocab:tools/deploy/shaker/strip-libc.factor"
80 : strip-destructors ( -- )
81 "Stripping destructor debug code" show
82 "vocab:tools/deploy/shaker/strip-destructors.factor"
86 "Stripping stack effect checking from call( and execute(" show
87 "vocab:tools/deploy/shaker/strip-call.factor" run-file ;
91 "Stripping unused Cocoa methods" show
92 "vocab:tools/deploy/shaker/strip-cocoa.factor"
96 : strip-gobject ( -- )
97 "gobject-introspection.types" vocab [
98 "Stripping GObject type info" show
99 "vocab:tools/deploy/shaker/strip-gobject.factor"
103 : strip-specialized-arrays ( -- )
104 strip-dictionary? "specialized-arrays" vocab and [
105 "Stripping specialized arrays" show
106 "vocab:tools/deploy/shaker/strip-specialized-arrays.factor"
110 : strip-word-names ( words -- )
111 "Stripping word names" show
112 [ f >>name f >>vocabulary drop ] each ;
114 : strip-word-defs ( words -- )
115 "Stripping symbolic word definitions" show
116 [ "no-def-strip" word-prop not ] filter
117 [ [ ] >>def drop ] each ;
119 : sift-assoc ( assoc -- assoc' ) [ nip ] assoc-filter ;
121 : strip-word-props ( stripped-props words -- )
122 "Stripping word properties" show
125 [ drop _ member? not ] assoc-filter sift-assoc
130 : stripped-word-props ( -- seq )
139 "effect-dependencies"
140 "definition-dependencies"
141 "conditional-dependencies"
150 "default-output-classes"
182 "predicate-definition"
220 deploy-c-types? get [
221 { "c-type" "struct-slots" "struct-align" } %
225 : strip-words ( props -- )
227 deploy-word-props? get [ 2dup strip-word-props ] unless
228 deploy-word-defs? get [ dup strip-word-defs ] unless
229 strip-word-names? [ dup strip-word-names strip-stack-traces ] when
232 : compiler-classes ( -- seq )
233 { "compiler" "stack-checker" }
234 [ child-vocabs [ words ] map concat [ class? ] filter ]
237 : prune-decision-tree ( tree classes -- )
238 [ tuple class>type ] 2dip '[
244 [ drop _ key? not ] assoc-filter
252 : strip-compiler-classes ( -- )
254 "Stripping compiler classes" show
255 [ single-generic? ] instances
256 compiler-classes '[ "decision-tree" word-prop _ prune-decision-tree ] each
259 : recursive-subst ( seq old new -- )
264 { [ 3dup drop eq? ] [ 2nip ] }
265 ! recurse into arrays
266 { [ pick array? ] [ [ dup ] 2dip recursive-subst ] }
267 ! otherwise do nothing
272 : strip-default-method ( generic new-default -- )
274 [ "decision-tree" word-prop ]
275 [ "default-method" word-prop ] bi
279 : new-default-method ( -- gensym )
280 [ [ "No method" throw ] (( -- * )) define-temp ] with-compilation-unit ;
282 : strip-default-methods ( -- )
283 ! In a development image, each generic has its own default method.
284 ! This gives better error messages for runtime type errors, but
285 ! takes up space. For deployment we merge them all together.
287 "Stripping default methods" show
288 [ single-generic? ] instances
289 new-default-method '[ _ strip-default-method ] each
292 : strip-vocab-globals ( except names -- words )
293 [ child-vocabs [ words ] map concat ] map concat
294 swap [ first2 lookup ] map sift diff ;
296 : stripped-globals ( -- seq )
298 "inspector-hook" "inspector" lookup ,
302 continuations:error-continuation
303 continuations:error-thread
304 continuations:restarts
306 source-files:source-files
315 "io-thread" "io.thread" lookup ,
317 "disposables" "destructors" lookup ,
319 "functor-words" "functors.backend" lookup ,
322 "initial-thread" "threads" lookup ,
325 strip-io? [ io-backend , ] when
335 } strip-vocab-globals %
338 "libraries" "alien" lookup ,
340 { { "yield-hook" "compiler.utilities" } }
341 { "cpu" "compiler" } strip-vocab-globals %
346 classes.private:next-method-quot-cache
347 classes.private:class-and-cache
348 classes.private:class-not-cache
349 classes.private:class-or-cache
350 classes.private:class<=-cache
351 classes.private:classes-intersect-cache
352 classes.private:implementors-map
353 classes.private:update-map
355 compiler.crossref:compiled-crossref
356 compiler.crossref:generic-call-site-crossref
358 compiler.errors:compiler-errors
364 source-files.errors:error-types
365 source-files.errors:error-observers
367 vocabs:load-vocab-hook
368 vocabs:vocab-observers
369 vocabs.loader:add-vocab-root-hook
374 { } { "layouts" } strip-vocab-globals %
376 { } { "math.partial-dispatch" } strip-vocab-globals %
378 { } { "math.vectors.simd" } strip-vocab-globals %
380 { } { "peg" } strip-vocab-globals %
384 { } { "prettyprint.config" } strip-vocab-globals %
389 compiler.errors:compiler-errors
390 continuations:thread-error-hook
394 "windows-messages" "windows.messages" lookup [ , ] when*
397 : strip-globals ( stripped-globals -- )
399 "Stripping globals" show
401 '[ drop _ member? not ] assoc-filter
402 [ drop string? not ] assoc-filter ! strip CLI args
404 21 set-special-object
408 ! On all platforms, if deploy-io is 1, we strip out C streams.
409 ! On Unix, if deploy-io is 3, we strip out C streams as well.
410 ! On Windows, even if deploy-io is 3, C streams are still used
411 ! for the console, so don't strip it there.
413 deploy-io get 3 = os windows? not and
415 "Stripping C I/O" show
416 "vocab:tools/deploy/shaker/strip-c-io.factor" run-file
419 : compress ( pred post-process string -- )
420 "Compressing " prepend show
421 [ instances dup H{ } clone [ [ ] cache ] curry map ] dip call
424 : compress-object? ( obj -- ? )
426 { [ dup array? ] [ empty? ] }
427 { [ dup byte-array? ] [ drop t ] }
428 { [ dup string? ] [ drop t ] }
429 { [ dup wrapper? ] [ drop t ] }
433 : compress-objects ( -- )
434 [ compress-object? ] [ ] "objects" compress ;
436 : remain-compiled ( old new -- old new )
437 ! Quotations which were formerly compiled must remain
440 2dup [ quot-compiled? ] [ quot-compiled? not ] bi* and
441 [ nip jit-compile ] [ 2drop ] if
444 : compress-quotations ( -- )
445 [ quotation? ] [ remain-compiled ] "quotations" compress
446 [ quotation? ] instances [ f >>cached-effect f >>cache-counter drop ] each ;
450 : [:c] ( -- word ) ":c" "debugger" lookup ;
452 : [print-error] ( -- word ) "print-error" "debugger" lookup ;
454 : deploy-startup-quot ( word -- )
457 startup-hooks get values concat %
458 strip-debugger? [ , ] [
459 ! Don't reference 'try' directly since we don't want
460 ! to pull in the debugger and prettyprinter into every
465 [ _ execute( obj -- ) ] [
466 _ execute( obj -- ) nl
471 strip-io? [ [ flush ] % ] unless
476 : startup-stripper ( -- )
478 f output-stream set-global
479 [ V{ "resource:" } clone vocab-roots set-global ]
480 "vocabs.loader" startup-hooks get-global set-at ;
482 : next-method* ( method -- quot )
483 [ "method-class" word-prop ]
484 [ "method-generic" word-prop ] bi
487 : calls-next-method? ( method -- ? )
488 def>> flatten \ (call-next-method) swap member-eq? ;
490 : compute-next-methods ( -- )
491 [ standard-generic? ] instances [
492 "methods" word-prop values [ calls-next-method? ] filter
493 [ dup next-method* "next-method" set-word-prop ] each
495 "vocab:tools/deploy/shaker/next-methods.factor" run-file ;
497 : (clear-megamorphic-cache) ( i array -- )
498 ! Can't do any dispatch while clearing caches since that
499 ! might leave them in an inconsistent state.
501 2dup [ f ] 2dip set-array-nth
502 [ 1 + ] dip (clear-megamorphic-cache)
505 : clear-megamorphic-cache ( array -- )
506 [ 0 ] dip (clear-megamorphic-cache) ;
508 : find-megamorphic-caches ( -- seq )
509 "Finding megamorphic caches" show
510 [ standard-generic? ] instances [ def>> third ] map ;
512 : clear-megamorphic-caches ( cache -- )
513 "Clearing megamorphic caches" show
514 [ clear-megamorphic-cache ] each ;
516 : write-vocab-manifest ( vocab-manifest-out -- )
517 "Writing vocabulary manifest to " write dup print flush
518 vocabs "VOCABS:" prefix
519 deploy-libraries get [ libraries get at path>> ] map members "LIBRARIES:" prefix append
520 swap utf8 set-file-lines ;
522 : prepare-deploy-libraries ( -- )
523 "Preparing deployed libraries" show
524 deploy-libraries get [
526 [ path>> >deployed-library-path ] [ abi>> ] bi <library>
531 "deploy-libraries" "alien.libraries" lookup forget
532 "deploy-library" "alien.libraries" lookup forget
533 ">deployed-library-path" "alien.libraries.private" lookup forget
534 ] with-compilation-unit ;
536 : strip ( vocab-manifest-out -- )
537 [ write-vocab-manifest ] when*
539 prepare-deploy-libraries
547 strip-specialized-arrays
550 add-command-line-hook
552 strip-default-methods
553 strip-compiler-classes
554 f 5 set-special-object ! we can't use the Factor debugger or Factor I/O anymore
555 deploy-vocab get vocab-main deploy-startup-quot
556 find-megamorphic-caches
558 stripped-globals strip-globals
562 clear-megamorphic-caches ;
564 : die-with ( error original-error -- * )
565 #! We don't want DCE to drop the error before the die call!
566 [ die 1 exit ] (( a -- * )) call-effect-unsafe ;
568 : die-with2 ( error original-error -- * )
569 #! We don't want DCE to drop the error before the die call!
570 [ die 1 exit ] (( a b -- * )) call-effect-unsafe ;
572 : deploy-error-handler ( quot -- )
575 [ original-error get die-with2 ]
576 ! Don't reference these words literally, if we're stripping the
577 ! debugger out we don't want to load the prettyprinter at all
578 [ [:c] execute( -- ) nl [print-error] execute( error -- ) flush ] if
582 : (deploy) ( final-image vocab-manifest-out vocab config -- )
583 #! Does the actual work of a deployment in the slave
589 "tools.errors" require
592 "ui.debugger" require
595 [ deploy-vocab set ] [ require ] [
597 "Vocabulary has no MAIN: word." print flush 1 exit
601 "Saving final image" show
603 ] deploy-error-handler
608 "vocab-manifest-out" get
610 "Deploying " write dup write "..." print
611 "deploy-config" get parse-file first