1 ! Copyright (C) 2007, 2010 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: arrays accessors io.backend io.encodings.utf8 io.files
4 io.streams.c init fry namespaces math make assocs kernel parser
5 parser.notes 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 io.pathnames ;
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
470 V{ "resource:" } clone vocab-roots set-global ;
472 : next-method* ( method -- quot )
473 [ "method-class" word-prop ]
474 [ "method-generic" word-prop ] bi
477 : calls-next-method? ( method -- ? )
478 def>> flatten \ (call-next-method) swap member-eq? ;
480 : compute-next-methods ( -- )
481 [ standard-generic? ] instances [
482 "methods" word-prop values [ calls-next-method? ] filter
483 [ dup next-method* "next-method" set-word-prop ] each
485 "vocab:tools/deploy/shaker/next-methods.factor" run-file ;
487 : (clear-megamorphic-cache) ( i array -- )
488 ! Can't do any dispatch while clearing caches since that
489 ! might leave them in an inconsistent state.
491 2dup [ f ] 2dip set-array-nth
492 [ 1 + ] dip (clear-megamorphic-cache)
495 : clear-megamorphic-cache ( array -- )
496 [ 0 ] dip (clear-megamorphic-cache) ;
498 : find-megamorphic-caches ( -- seq )
499 "Finding megamorphic caches" show
500 [ standard-generic? ] instances [ def>> third ] map ;
502 : clear-megamorphic-caches ( cache -- )
503 "Clearing megamorphic caches" show
504 [ clear-megamorphic-cache ] each ;
506 : write-vocab-manifest ( vocab-manifest-out -- )
507 "Writing vocabulary manifest to " write dup print flush
508 vocabs swap utf8 set-file-lines ;
510 : strip ( vocab-manifest-out -- )
511 [ write-vocab-manifest ] when*
518 strip-specialized-arrays
521 add-command-line-hook
523 strip-default-methods
524 strip-compiler-classes
525 f 5 set-special-object ! we can't use the Factor debugger or Factor I/O anymore
526 deploy-vocab get vocab-main deploy-startup-quot
527 find-megamorphic-caches
529 stripped-globals strip-globals
533 clear-megamorphic-caches ;
535 : deploy-error-handler ( quot -- )
538 [ error-continuation get call>> callstack>array die 1 exit ]
539 ! Don't reference these words literally, if we're stripping the
540 ! debugger out we don't want to load the prettyprinter at all
541 [ [:c] execute( -- ) nl [print-error] execute( error -- ) flush ] if
545 : (deploy) ( final-image vocab-manifest-out vocab config -- )
546 #! Does the actual work of a deployment in the slave
552 "tools.errors" require
555 "ui.debugger" require
558 [ deploy-vocab set ] [ require ] [
560 "Vocabulary has no MAIN: word." print flush 1 exit
564 "Saving final image" show
566 ] deploy-error-handler
571 "vocab-manifest-out" get
573 "Deploying " write dup write "..." print
574 "deploy-config" get parse-file first