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 ;
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
55 } [ startup-hooks get delete-at ] each
58 : strip-debugger ( -- )
59 strip-debugger? "debugger" vocab and [
60 "Stripping debugger" show
61 "vocab:tools/deploy/shaker/strip-debugger.factor"
67 "Stripping manual memory management debug code" show
68 "vocab:tools/deploy/shaker/strip-libc.factor"
72 : strip-destructors ( -- )
73 "Stripping destructor debug code" show
74 "vocab:tools/deploy/shaker/strip-destructors.factor"
78 "Stripping stack effect checking from call( and execute(" show
79 "vocab:tools/deploy/shaker/strip-call.factor" run-file ;
83 "Stripping unused Cocoa methods" show
84 "vocab:tools/deploy/shaker/strip-cocoa.factor"
88 : strip-specialized-arrays ( -- )
89 strip-dictionary? "specialized-arrays" vocab and [
90 "Stripping specialized arrays" show
91 "vocab:tools/deploy/shaker/strip-specialized-arrays.factor"
95 : strip-word-names ( words -- )
96 "Stripping word names" show
97 [ f >>name f >>vocabulary drop ] each ;
99 : strip-word-defs ( words -- )
100 "Stripping symbolic word definitions" show
101 [ "no-def-strip" word-prop not ] filter
102 [ [ ] >>def drop ] each ;
104 : sift-assoc ( assoc -- assoc' ) [ nip ] assoc-filter ;
106 : strip-word-props ( stripped-props words -- )
107 "Stripping word properties" show
110 [ drop _ member? not ] assoc-filter sift-assoc
115 : stripped-word-props ( -- seq )
124 "effect-dependencies"
125 "definition-dependencies"
126 "conditional-dependencies"
135 "default-output-classes"
167 "predicate-definition"
206 deploy-c-types? get [
207 { "c-type" "struct-slots" "struct-align" } %
211 : strip-words ( props -- )
213 deploy-word-props? get [ 2dup strip-word-props ] unless
214 deploy-word-defs? get [ dup strip-word-defs ] unless
215 strip-word-names? [ dup strip-word-names strip-stack-traces ] when
218 : compiler-classes ( -- seq )
219 { "compiler" "stack-checker" }
220 [ child-vocabs [ words ] map concat [ class? ] filter ]
223 : prune-decision-tree ( tree classes -- )
224 [ tuple class>type ] 2dip '[
230 [ drop _ key? not ] assoc-filter
238 : strip-compiler-classes ( -- )
240 "Stripping compiler classes" show
241 [ single-generic? ] instances
242 compiler-classes '[ "decision-tree" word-prop _ prune-decision-tree ] each
245 : recursive-subst ( seq old new -- )
250 { [ 3dup drop eq? ] [ 2nip ] }
251 ! recurse into arrays
252 { [ pick array? ] [ [ dup ] 2dip recursive-subst ] }
253 ! otherwise do nothing
258 : strip-default-method ( generic new-default -- )
260 [ "decision-tree" word-prop ]
261 [ "default-method" word-prop ] bi
265 : new-default-method ( -- gensym )
266 [ [ "No method" throw ] (( -- * )) define-temp ] with-compilation-unit ;
268 : strip-default-methods ( -- )
269 ! In a development image, each generic has its own default method.
270 ! This gives better error messages for runtime type errors, but
271 ! takes up space. For deployment we merge them all together.
273 "Stripping default methods" show
274 [ single-generic? ] instances
275 new-default-method '[ _ strip-default-method ] each
278 : strip-vocab-globals ( except names -- words )
279 [ child-vocabs [ words ] map concat ] map concat
280 swap [ first2 lookup ] map sift diff ;
282 : stripped-globals ( -- seq )
284 "inspector-hook" "inspector" lookup ,
288 continuations:error-continuation
289 continuations:error-thread
290 continuations:restarts
292 source-files:source-files
298 "io-thread" "io.thread" lookup ,
300 "disposables" "destructors" lookup ,
302 "functor-words" "functors.backend" lookup ,
305 "initial-thread" "threads" lookup ,
308 strip-io? [ io-backend , ] when
318 } strip-vocab-globals %
321 "libraries" "alien" lookup ,
323 { { "yield-hook" "compiler.utilities" } }
324 { "cpu" "compiler" } strip-vocab-globals %
329 classes.private:next-method-quot-cache
330 classes.private:class-and-cache
331 classes.private:class-not-cache
332 classes.private:class-or-cache
333 classes.private:class<=-cache
334 classes.private:classes-intersect-cache
335 classes.private:implementors-map
336 classes.private:update-map
338 compiler.crossref:compiled-crossref
339 compiler.crossref:generic-call-site-crossref
341 compiler.errors:compiler-errors
345 source-files.errors:error-types
346 source-files.errors:error-observers
348 vocabs:load-vocab-hook
349 vocabs:vocab-observers
354 { } { "layouts" } strip-vocab-globals %
356 { } { "math.partial-dispatch" } strip-vocab-globals %
358 { } { "math.vectors.simd" } strip-vocab-globals %
360 { } { "peg" } strip-vocab-globals %
364 { } { "prettyprint.config" } strip-vocab-globals %
369 compiler.errors:compiler-errors
370 continuations:thread-error-hook
374 "ui-error-hook" "ui.gadgets.worlds" lookup ,
378 deploy-c-types? get [
379 "c-types" "alien.c-types" lookup ,
382 "windows-messages" "windows.messages" lookup [ , ] when*
385 : strip-globals ( stripped-globals -- )
387 "Stripping globals" show
389 '[ drop _ member? not ] assoc-filter
390 [ drop string? not ] assoc-filter ! strip CLI args
392 21 set-special-object
397 deploy-io get 3 = os windows? not and
401 "io.streams.c" forget-vocab
402 "io-thread-running?" "io.thread" lookup [
405 ] with-compilation-unit
408 : compress ( pred post-process string -- )
409 "Compressing " prepend show
410 [ instances dup H{ } clone [ [ ] cache ] curry map ] dip call
413 : compress-object? ( obj -- ? )
415 { [ dup array? ] [ empty? ] }
416 { [ dup byte-array? ] [ drop t ] }
417 { [ dup string? ] [ drop t ] }
418 { [ dup wrapper? ] [ drop t ] }
422 : compress-objects ( -- )
423 [ compress-object? ] [ ] "objects" compress ;
425 : remain-compiled ( old new -- old new )
426 ! Quotations which were formerly compiled must remain
429 2dup [ quot-compiled? ] [ quot-compiled? not ] bi* and
430 [ nip jit-compile ] [ 2drop ] if
433 : compress-quotations ( -- )
434 [ quotation? ] [ remain-compiled ] "quotations" compress
435 [ quotation? ] instances [ f >>cached-effect f >>cache-counter drop ] each ;
439 : [:c] ( -- word ) ":c" "debugger" lookup ;
441 : [print-error] ( -- word ) "print-error" "debugger" lookup ;
443 : deploy-startup-quot ( word -- )
446 startup-hooks get values concat %
447 strip-debugger? [ , ] [
448 ! Don't reference 'try' directly since we don't want
449 ! to pull in the debugger and prettyprinter into every
454 [ _ execute( obj -- ) ] [
455 _ execute( obj -- ) nl
460 strip-io? [ [ flush ] % ] unless
465 : startup-stripper ( -- )
467 f output-stream set-global
468 V{ "resource:" } clone vocab-roots set-global ;
470 : next-method* ( method -- quot )
471 [ "method-class" word-prop ]
472 [ "method-generic" word-prop ] bi
475 : calls-next-method? ( method -- ? )
476 def>> flatten \ (call-next-method) swap member-eq? ;
478 : compute-next-methods ( -- )
479 [ standard-generic? ] instances [
480 "methods" word-prop values [ calls-next-method? ] filter
481 [ dup next-method* "next-method" set-word-prop ] each
483 "vocab:tools/deploy/shaker/next-methods.factor" run-file ;
485 : (clear-megamorphic-cache) ( i array -- )
486 ! Can't do any dispatch while clearing caches since that
487 ! might leave them in an inconsistent state.
489 2dup [ f ] 2dip set-array-nth
490 [ 1 + ] dip (clear-megamorphic-cache)
493 : clear-megamorphic-cache ( array -- )
494 [ 0 ] dip (clear-megamorphic-cache) ;
496 : find-megamorphic-caches ( -- seq )
497 "Finding megamorphic caches" show
498 [ standard-generic? ] instances [ def>> third ] map ;
500 : clear-megamorphic-caches ( cache -- )
501 "Clearing megamorphic caches" show
502 [ clear-megamorphic-cache ] each ;
504 : write-vocab-manifest ( vocab-manifest-out -- )
505 "Writing vocabulary manifest to " write dup print flush
506 vocabs swap utf8 set-file-lines ;
508 : strip ( vocab-manifest-out -- )
509 [ write-vocab-manifest ] when*
516 strip-specialized-arrays
519 add-command-line-hook
521 strip-default-methods
522 strip-compiler-classes
523 f 5 set-special-object ! we can't use the Factor debugger or Factor I/O anymore
524 deploy-vocab get vocab-main deploy-startup-quot
525 find-megamorphic-caches
527 stripped-globals strip-globals
531 clear-megamorphic-caches ;
533 : deploy-error-handler ( quot -- )
536 [ error-continuation get call>> callstack>array die 1 exit ]
537 ! Don't reference these words literally, if we're stripping the
538 ! debugger out we don't want to load the prettyprinter at all
539 [ [:c] execute( -- ) nl [print-error] execute( error -- ) flush ] if
543 : (deploy) ( final-image vocab-manifest-out vocab config -- )
544 #! Does the actual work of a deployment in the slave
550 "tools.errors" require
553 "ui.debugger" require
556 [ deploy-vocab set ] [ require ] [
558 "Vocabulary has no MAIN: word." print flush 1 exit
562 "Saving final image" show
564 ] deploy-error-handler
569 "vocab-manifest-out" get
571 "Deploying " write dup write "..." print
572 "deploy-config" get parse-file first