1 ! Copyright (C) 2007, 2010 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: arrays accessors io.backend io.pathnames io.streams.c
4 init fry namespaces math make assocs kernel parser parser.notes
5 lexer strings.parser vocabs sequences sequences.deep
6 sequences.private words memory kernel.private continuations io
7 vocabs.loader system strings sets vectors quotations byte-arrays
8 sorting compiler.units definitions generic generic.standard
9 generic.single tools.deploy.config combinators classes
10 classes.builtin slots.private grouping command-line ;
11 QUALIFIED: bootstrap.stage2
12 QUALIFIED: classes.private
13 QUALIFIED: compiler.crossref
14 QUALIFIED: compiler.errors
15 QUALIFIED: continuations
16 QUALIFIED: definitions
19 QUALIFIED: source-files
20 QUALIFIED: source-files.errors
22 IN: tools.deploy.shaker
24 ! This file is some hairy shit.
26 : add-command-line-hook ( -- )
27 [ (command-line) command-line set-global ] "command-line"
28 startup-hooks get set-at ;
30 : strip-startup-hooks ( -- )
31 "Stripping startup hooks" show
38 [ startup-hooks get delete-at ] each
40 "threads" startup-hooks get delete-at
43 "io.thread" startup-hooks get delete-at
46 "io.backend" startup-hooks get delete-at
47 "io.thread" startup-hooks get delete-at
54 } [ startup-hooks get delete-at ] each
57 : strip-debugger ( -- )
58 strip-debugger? "debugger" vocab and [
59 "Stripping debugger" show
60 "vocab:tools/deploy/shaker/strip-debugger.factor"
66 "Stripping manual memory management debug code" show
67 "vocab:tools/deploy/shaker/strip-libc.factor"
71 : strip-destructors ( -- )
72 "Stripping destructor debug code" show
73 "vocab:tools/deploy/shaker/strip-destructors.factor"
77 "Stripping stack effect checking from call( and execute(" show
78 "vocab:tools/deploy/shaker/strip-call.factor" run-file ;
82 "Stripping unused Cocoa methods" show
83 "vocab:tools/deploy/shaker/strip-cocoa.factor"
87 : strip-specialized-arrays ( -- )
88 strip-dictionary? "specialized-arrays" vocab and [
89 "Stripping specialized arrays" show
90 "vocab:tools/deploy/shaker/strip-specialized-arrays.factor"
94 : strip-word-names ( words -- )
95 "Stripping word names" show
96 [ f >>name f >>vocabulary drop ] each ;
98 : strip-word-defs ( words -- )
99 "Stripping symbolic word definitions" show
100 [ "no-def-strip" word-prop not ] filter
101 [ [ ] >>def drop ] each ;
103 : sift-assoc ( assoc -- assoc' ) [ nip ] assoc-filter ;
105 : strip-word-props ( stripped-props words -- )
106 "Stripping word properties" show
109 [ drop _ member? not ] assoc-filter sift-assoc
114 : stripped-word-props ( -- seq )
123 "effect-dependencies"
124 "definition-dependencies"
125 "conditional-dependencies"
134 "default-output-classes"
166 "predicate-definition"
205 deploy-c-types? get [
206 { "c-type" "struct-slots" "struct-align" } %
210 : strip-words ( props -- )
212 deploy-word-props? get [ 2dup strip-word-props ] unless
213 deploy-word-defs? get [ dup strip-word-defs ] unless
214 strip-word-names? [ dup strip-word-names strip-stack-traces ] when
217 : compiler-classes ( -- seq )
218 { "compiler" "stack-checker" }
219 [ child-vocabs [ words ] map concat [ class? ] filter ]
222 : prune-decision-tree ( tree classes -- )
223 [ tuple class>type ] 2dip '[
229 [ drop _ key? not ] assoc-filter
237 : strip-compiler-classes ( -- )
239 "Stripping compiler classes" show
240 [ single-generic? ] instances
241 compiler-classes '[ "decision-tree" word-prop _ prune-decision-tree ] each
244 : recursive-subst ( seq old new -- )
249 { [ 3dup drop eq? ] [ 2nip ] }
250 ! recurse into arrays
251 { [ pick array? ] [ [ dup ] 2dip recursive-subst ] }
252 ! otherwise do nothing
257 : strip-default-method ( generic new-default -- )
259 [ "decision-tree" word-prop ]
260 [ "default-method" word-prop ] bi
264 : new-default-method ( -- gensym )
265 [ [ "No method" throw ] (( -- * )) define-temp ] with-compilation-unit ;
267 : strip-default-methods ( -- )
268 ! In a development image, each generic has its own default method.
269 ! This gives better error messages for runtime type errors, but
270 ! takes up space. For deployment we merge them all together.
272 "Stripping default methods" show
273 [ single-generic? ] instances
274 new-default-method '[ _ strip-default-method ] each
277 : strip-vocab-globals ( except names -- words )
278 [ child-vocabs [ words ] map concat ] map concat
279 swap [ first2 lookup ] map sift diff ;
281 : stripped-globals ( -- seq )
283 "inspector-hook" "inspector" lookup ,
287 continuations:error-continuation
288 continuations:error-thread
289 continuations:restarts
291 source-files:source-files
300 "io-thread" "io.thread" lookup ,
302 "disposables" "destructors" lookup ,
304 "functor-words" "functors.backend" lookup ,
307 "initial-thread" "threads" lookup ,
310 strip-io? [ io-backend , ] when
320 } strip-vocab-globals %
323 "libraries" "alien" lookup ,
325 { { "yield-hook" "compiler.utilities" } }
326 { "cpu" "compiler" } strip-vocab-globals %
331 classes.private:next-method-quot-cache
332 classes.private:class-and-cache
333 classes.private:class-not-cache
334 classes.private:class-or-cache
335 classes.private:class<=-cache
336 classes.private:classes-intersect-cache
337 classes.private:implementors-map
338 classes.private:update-map
340 compiler.crossref:compiled-crossref
341 compiler.crossref:generic-call-site-crossref
343 compiler.errors:compiler-errors
347 source-files.errors:error-types
348 source-files.errors:error-observers
350 vocabs:load-vocab-hook
351 vocabs:vocab-observers
356 { } { "layouts" } strip-vocab-globals %
358 { } { "math.partial-dispatch" } strip-vocab-globals %
360 { } { "math.vectors.simd" } strip-vocab-globals %
362 { } { "peg" } strip-vocab-globals %
366 { } { "prettyprint.config" } strip-vocab-globals %
371 compiler.errors:compiler-errors
372 continuations:thread-error-hook
376 "ui-error-hook" "ui.gadgets.worlds" lookup ,
380 deploy-c-types? get [
381 "c-types" "alien.c-types" lookup ,
384 "windows-messages" "windows.messages" lookup [ , ] when*
387 : strip-globals ( stripped-globals -- )
389 "Stripping globals" show
391 '[ drop _ member? not ] assoc-filter
392 [ drop string? not ] assoc-filter ! strip CLI args
394 21 set-special-object
399 deploy-io get 3 = os windows? not and
403 "io.streams.c" forget-vocab
404 "io-thread-running?" "io.thread" lookup [
407 ] with-compilation-unit
410 : compress ( pred post-process string -- )
411 "Compressing " prepend show
412 [ instances dup H{ } clone [ [ ] cache ] curry map ] dip call
415 : compress-object? ( obj -- ? )
417 { [ dup array? ] [ empty? ] }
418 { [ dup byte-array? ] [ drop t ] }
419 { [ dup string? ] [ drop t ] }
420 { [ dup wrapper? ] [ drop t ] }
424 : compress-objects ( -- )
425 [ compress-object? ] [ ] "objects" compress ;
427 : remain-compiled ( old new -- old new )
428 ! Quotations which were formerly compiled must remain
431 2dup [ quot-compiled? ] [ quot-compiled? not ] bi* and
432 [ nip jit-compile ] [ 2drop ] if
435 : compress-quotations ( -- )
436 [ quotation? ] [ remain-compiled ] "quotations" compress
437 [ quotation? ] instances [ f >>cached-effect f >>cache-counter drop ] each ;
441 : [:c] ( -- word ) ":c" "debugger" lookup ;
443 : [print-error] ( -- word ) "print-error" "debugger" lookup ;
445 : deploy-startup-quot ( word -- )
448 startup-hooks get values concat %
449 strip-debugger? [ , ] [
450 ! Don't reference 'try' directly since we don't want
451 ! to pull in the debugger and prettyprinter into every
456 [ _ execute( obj -- ) ] [
457 _ execute( obj -- ) nl
462 strip-io? [ [ flush ] % ] unless
467 : startup-stripper ( -- )
469 f output-stream set-global ;
471 : next-method* ( method -- quot )
472 [ "method-class" word-prop ]
473 [ "method-generic" word-prop ] bi
476 : calls-next-method? ( method -- ? )
477 def>> flatten \ (call-next-method) swap member-eq? ;
479 : compute-next-methods ( -- )
480 [ standard-generic? ] instances [
481 "methods" word-prop values [ calls-next-method? ] filter
482 [ dup next-method* "next-method" set-word-prop ] each
484 "vocab:tools/deploy/shaker/next-methods.factor" run-file ;
486 : (clear-megamorphic-cache) ( i array -- )
487 ! Can't do any dispatch while clearing caches since that
488 ! might leave them in an inconsistent state.
490 2dup [ f ] 2dip set-array-nth
491 [ 1 + ] dip (clear-megamorphic-cache)
494 : clear-megamorphic-cache ( array -- )
495 [ 0 ] dip (clear-megamorphic-cache) ;
497 : find-megamorphic-caches ( -- seq )
498 "Finding megamorphic caches" show
499 [ standard-generic? ] instances [ def>> third ] map ;
501 : clear-megamorphic-caches ( cache -- )
502 "Clearing megamorphic caches" show
503 [ clear-megamorphic-cache ] each ;
512 strip-specialized-arrays
515 add-command-line-hook
517 strip-default-methods
518 strip-compiler-classes
519 f 5 set-special-object ! we can't use the Factor debugger or Factor I/O anymore
520 deploy-vocab get vocab-main deploy-startup-quot
521 find-megamorphic-caches
523 stripped-globals strip-globals
527 clear-megamorphic-caches ;
529 : deploy-error-handler ( quot -- )
532 [ error-continuation get call>> callstack>array die 1 exit ]
533 ! Don't reference these words literally, if we're stripping the
534 ! debugger out we don't want to load the prettyprinter at all
535 [ [:c] execute( -- ) nl [print-error] execute( error -- ) flush ] if
539 : (deploy) ( final-image vocab config -- )
540 #! Does the actual work of a deployment in the slave
546 "tools.errors" require
549 "ui.debugger" require
553 deploy-vocab get require
554 deploy-vocab get vocab-main [
555 "Vocabulary has no MAIN: word." print flush 1 exit
558 "Saving final image" show
560 ] deploy-error-handler
566 "Deploying " write dup write "..." print
567 "deploy-config" get parse-file first