]> gitweb.factorcode.org Git - factor.git/blob - basis/tools/deploy/shaker/shaker.factor
tools.deploy.windows: embed icon.ico from deployed vocab dir into deployed exe
[factor.git] / basis / tools / deploy / shaker / shaker.factor
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
17 QUALIFIED: init
18 QUALIFIED: layouts
19 QUALIFIED: source-files
20 QUALIFIED: source-files.errors
21 QUALIFIED: vocabs
22 IN: tools.deploy.shaker
23
24 ! This file is some hairy shit.
25
26 : add-command-line-hook ( -- )
27     [ (command-line) command-line set-global ] "command-line"
28     startup-hooks get set-at ;
29
30 : strip-startup-hooks ( -- )
31     "Stripping startup hooks" show
32     {
33         "alien.strings"
34         "cpu.x86"
35         "environment"
36         "libc"
37     }
38     [ startup-hooks get delete-at ] each
39     deploy-threads? get [
40         "threads" startup-hooks get delete-at
41     ] unless
42     native-io? [
43         "io.thread" startup-hooks get delete-at
44     ] unless
45     strip-io? [
46         "io.backend" startup-hooks get delete-at
47         "io.thread" startup-hooks get delete-at
48     ] when
49     strip-dictionary? [
50         {
51             ! "compiler.units"
52             "vocabs"
53             "vocabs.cache"
54             "source-files.errors"
55         } [ startup-hooks get delete-at ] each
56     ] when ;
57
58 : strip-debugger ( -- )
59     strip-debugger? "debugger" vocab and [
60         "Stripping debugger" show
61         "vocab:tools/deploy/shaker/strip-debugger.factor"
62         run-file
63     ] when ;
64
65 : strip-libc ( -- )
66     "libc" vocab [
67         "Stripping manual memory management debug code" show
68         "vocab:tools/deploy/shaker/strip-libc.factor"
69         run-file
70     ] when ;
71
72 : strip-destructors ( -- )
73     "Stripping destructor debug code" show
74     "vocab:tools/deploy/shaker/strip-destructors.factor"
75     run-file ;
76
77 : strip-call ( -- )
78     "Stripping stack effect checking from call( and execute(" show
79     "vocab:tools/deploy/shaker/strip-call.factor" run-file ;
80
81 : strip-cocoa ( -- )
82     "cocoa" vocab [
83         "Stripping unused Cocoa methods" show
84         "vocab:tools/deploy/shaker/strip-cocoa.factor"
85         run-file
86     ] when ;
87
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"
92         run-file
93     ] when ;
94
95 : strip-word-names ( words -- )
96     "Stripping word names" show
97     [ f >>name f >>vocabulary drop ] each ;
98
99 : strip-word-defs ( words -- )
100     "Stripping symbolic word definitions" show
101     [ "no-def-strip" word-prop not ] filter
102     [ [ ] >>def drop ] each ;
103
104 : sift-assoc ( assoc -- assoc' ) [ nip ] assoc-filter ;
105
106 : strip-word-props ( stripped-props words -- )
107     "Stripping word properties" show
108     swap '[
109         [
110             [ drop _ member? not ] assoc-filter sift-assoc
111             >alist f like
112         ] change-props drop
113     ] each ;
114
115 : stripped-word-props ( -- seq )
116     [
117         strip-dictionary? [
118             {
119                 "alias"
120                 "boa-check"
121                 "coercer"
122                 "combination"
123                 "generic-call-sites"
124                 "effect-dependencies"
125                 "definition-dependencies"
126                 "conditional-dependencies"
127                 "dependency-checks"
128                 "constant"
129                 "constraints"
130                 "custom-inlining"
131                 "decision-tree"
132                 "declared-effect"
133                 "default"
134                 "default-method"
135                 "default-output-classes"
136                 "derived-from"
137                 "ebnf-parser"
138                 "engines"
139                 "forgotten"
140                 "identities"
141                 "inline"
142                 "inlined-block"
143                 "input-classes"
144                 "instances"
145                 "interval"
146                 "intrinsic"
147                 "lambda"
148                 "loc"
149                 "local-reader"
150                 "local-reader?"
151                 "local-writer"
152                 "local-writer?"
153                 "local?"
154                 "low-order"
155                 "macro"
156                 "members"
157                 "memo-quot"
158                 "methods"
159                 "method-class"
160                 "method-generic"
161                 "modular-arithmetic"
162                 "no-compile"
163                 "owner-generic"
164                 "outputs"
165                 "participants"
166                 "predicate"
167                 "predicate-definition"
168                 "predicating"
169                 "primitive"
170                 "reader"
171                 "reading"
172                 "recursive"
173                 "register"
174                 "register-size"
175                 "shuffle"
176                 "slots"
177                 "special"
178                 "specializer"
179                 "struct-slots"
180                 ! UI needs this
181                 ! "superclass"
182                 "transform-n"
183                 "transform-quot"
184                 "type"
185                 "typed-def"
186                 "typed-word"
187                 "writer"
188                 "writing"
189             } %
190         ] when
191         
192         strip-prettyprint? [
193             {
194                 "delimiter"
195                 "flushable"
196                 "foldable"
197                 "inline"
198                 "lambda"
199                 "macro"
200                 "memo-quot"
201                 "parsing"
202                 "word-style"
203             } %
204         ] when
205         
206         deploy-c-types? get [
207             { "c-type" "struct-slots" "struct-align" } %
208         ] unless
209     ] { } make ;
210
211 : strip-words ( props -- )
212     [ word? ] instances
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
216     2drop ;
217
218 : compiler-classes ( -- seq )
219     { "compiler" "stack-checker" }
220     [ child-vocabs [ words ] map concat [ class? ] filter ]
221     map concat unique ;
222
223 : prune-decision-tree ( tree classes -- )
224     [ tuple class>type ] 2dip '[
225         dup array? [
226             [
227                 dup array? [
228                     [
229                         2 group
230                         [ drop _ key? not ] assoc-filter
231                         concat
232                     ] map
233                 ] when
234             ] map
235         ] when
236     ] change-nth ;
237
238 : strip-compiler-classes ( -- )
239     strip-dictionary? [
240         "Stripping compiler classes" show
241         [ single-generic? ] instances
242         compiler-classes '[ "decision-tree" word-prop _ prune-decision-tree ] each
243     ] when ;
244
245 : recursive-subst ( seq old new -- )
246     '[
247         _ _
248         {
249             ! old becomes new
250             { [ 3dup drop eq? ] [ 2nip ] }
251             ! recurse into arrays
252             { [ pick array? ] [ [ dup ] 2dip recursive-subst ] }
253             ! otherwise do nothing
254             [ 2drop ]
255         } cond
256     ] map! drop ;
257
258 : strip-default-method ( generic new-default -- )
259     [
260         [ "decision-tree" word-prop ]
261         [ "default-method" word-prop ] bi
262     ] dip
263     recursive-subst ;
264
265 : new-default-method ( -- gensym )
266     [ [ "No method" throw ] (( -- * )) define-temp ] with-compilation-unit ;
267
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.
272     strip-debugger? [
273         "Stripping default methods" show
274         [ single-generic? ] instances
275         new-default-method '[ _ strip-default-method ] each
276     ] when ;
277
278 : strip-vocab-globals ( except names -- words )
279     [ child-vocabs [ words ] map concat ] map concat
280     swap [ first2 lookup ] map sift diff ;
281
282 : stripped-globals ( -- seq )
283     [
284         "inspector-hook" "inspector" lookup ,
285
286         {
287             continuations:error
288             continuations:error-continuation
289             continuations:error-thread
290             continuations:restarts
291             init:startup-hooks
292             source-files:source-files
293             input-stream
294             output-stream
295             error-stream
296         } %
297
298         "io-thread" "io.thread" lookup ,
299
300         "disposables" "destructors" lookup ,
301
302         "functor-words" "functors.backend" lookup ,
303         
304         deploy-threads? [
305             "initial-thread" "threads" lookup ,
306         ] unless
307
308         strip-io? [ io-backend , ] when
309
310         { } {
311             "alarms"
312             "tools"
313             "io.launcher"
314             "random"
315             "stack-checker"
316             "bootstrap"
317             "listener"
318         } strip-vocab-globals %
319
320         strip-dictionary? [
321             "libraries" "alien" lookup ,
322
323             { { "yield-hook" "compiler.utilities" } }
324             { "cpu" "compiler" } strip-vocab-globals %
325
326             {
327                 gensym
328                 name>char-hook
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
337                 main-vocab-hook
338                 compiler.crossref:compiled-crossref
339                 compiler.crossref:generic-call-site-crossref
340                 compiler-impl
341                 compiler.errors:compiler-errors
342                 lexer-factory
343                 print-use-hook
344                 root-cache
345                 source-files.errors:error-types
346                 source-files.errors:error-observers
347                 vocabs:dictionary
348                 vocabs:load-vocab-hook
349                 vocabs:vocab-observers
350                 word
351                 parser-notes
352             } %
353
354             { } { "layouts" } strip-vocab-globals %
355
356             { } { "math.partial-dispatch" } strip-vocab-globals %
357
358             { } { "math.vectors.simd" } strip-vocab-globals %
359
360             { } { "peg" } strip-vocab-globals %
361         ] when
362
363         strip-prettyprint? [
364             { } { "prettyprint.config" } strip-vocab-globals %
365         ] when
366
367         strip-debugger? [
368             {
369                 compiler.errors:compiler-errors
370                 continuations:thread-error-hook
371             } %
372             
373             deploy-ui? get [
374                 "ui-error-hook" "ui.gadgets.worlds" lookup ,
375             ] when
376         ] when
377
378         deploy-c-types? get [
379             "c-types" "alien.c-types" lookup ,
380         ] unless
381
382         "windows-messages" "windows.messages" lookup [ , ] when*
383     ] { } make ;
384
385 : strip-globals ( stripped-globals -- )
386     strip-globals? [
387         "Stripping globals" show
388         global swap
389         '[ drop _ member? not ] assoc-filter
390         [ drop string? not ] assoc-filter ! strip CLI args
391         sift-assoc
392         21 set-special-object
393     ] [ drop ] if ;
394
395 : strip-c-io ( -- )
396     strip-io?
397     deploy-io get 3 = os windows? not and
398     or [
399         [
400             c-io-backend forget
401             "io.streams.c" forget-vocab
402             "io-thread-running?" "io.thread" lookup [
403                 global delete-at
404             ] when*
405         ] with-compilation-unit
406     ] when ;
407
408 : compress ( pred post-process string -- )
409     "Compressing " prepend show
410     [ instances dup H{ } clone [ [ ] cache ] curry map ] dip call
411     become ; inline
412
413 : compress-object? ( obj -- ? )
414     {
415         { [ dup array? ] [ empty? ] }
416         { [ dup byte-array? ] [ drop t ] }
417         { [ dup string? ] [ drop t ] }
418         { [ dup wrapper? ] [ drop t ] }
419         [ drop f ]
420     } cond ;
421
422 : compress-objects ( -- )
423     [ compress-object? ] [ ] "objects" compress ;
424
425 : remain-compiled ( old new -- old new )
426     ! Quotations which were formerly compiled must remain
427     ! compiled.
428     2dup [
429         2dup [ quot-compiled? ] [ quot-compiled? not ] bi* and
430         [ nip jit-compile ] [ 2drop ] if
431     ] 2each ;
432
433 : compress-quotations ( -- )
434     [ quotation? ] [ remain-compiled ] "quotations" compress
435     [ quotation? ] instances [ f >>cached-effect f >>cache-counter drop ] each ;
436
437 SYMBOL: deploy-vocab
438
439 : [:c] ( -- word ) ":c" "debugger" lookup ;
440
441 : [print-error] ( -- word ) "print-error" "debugger" lookup ;
442
443 : deploy-startup-quot ( word -- )
444     [
445         [ boot ] %
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
450             ! deployed app
451             [:c]
452             [print-error]
453             '[
454                 [ _ execute( obj -- ) ] [
455                     _ execute( obj -- ) nl
456                     _ execute( obj -- )
457                 ] recover
458             ] %
459         ] if
460         strip-io? [ [ flush ] % ] unless
461         [ 0 exit ] %
462     ] [ ] make
463     set-startup-quot ;
464
465 : startup-stripper ( -- )
466     t "quiet" set-global
467     f output-stream set-global
468     V{ "resource:" } clone vocab-roots set-global ;
469
470 : next-method* ( method -- quot )
471     [ "method-class" word-prop ]
472     [ "method-generic" word-prop ] bi
473     next-method ;
474
475 : calls-next-method? ( method -- ? )
476     def>> flatten \ (call-next-method) swap member-eq? ;
477
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
482     ] each
483     "vocab:tools/deploy/shaker/next-methods.factor" run-file ;
484
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.
488     2dup 1 slot < [
489         2dup [ f ] 2dip set-array-nth
490         [ 1 + ] dip (clear-megamorphic-cache)
491     ] [ 2drop ] if ;
492
493 : clear-megamorphic-cache ( array -- )
494     [ 0 ] dip (clear-megamorphic-cache) ;
495
496 : find-megamorphic-caches ( -- seq )
497     "Finding megamorphic caches" show
498     [ standard-generic? ] instances [ def>> third ] map ;
499
500 : clear-megamorphic-caches ( cache -- )
501     "Clearing megamorphic caches" show
502     [ clear-megamorphic-cache ] each ;
503
504 : write-vocab-manifest ( vocab-manifest-out -- )
505     "Writing vocabulary manifest to " write dup print flush
506     vocabs swap utf8 set-file-lines ;
507
508 : strip ( vocab-manifest-out -- )
509     [ write-vocab-manifest ] when*
510     startup-stripper
511     strip-libc
512     strip-destructors
513     strip-call
514     strip-cocoa
515     strip-debugger
516     strip-specialized-arrays
517     compute-next-methods
518     strip-startup-hooks
519     add-command-line-hook
520     strip-c-io
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
526     stripped-word-props
527     stripped-globals strip-globals
528     compress-objects
529     compress-quotations
530     strip-words
531     clear-megamorphic-caches ;
532
533 : deploy-error-handler ( quot -- )
534     [
535         strip-debugger?
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
540         1 exit
541     ] recover ; inline
542
543 : (deploy) ( final-image vocab-manifest-out vocab config -- )
544     #! Does the actual work of a deployment in the slave
545     #! stage2 image
546     [
547         [
548             strip-debugger? [
549                 "debugger" require
550                 "tools.errors" require
551                 "inspector" require
552                 deploy-ui? get [
553                     "ui.debugger" require
554                 ] when
555             ] unless
556             [ deploy-vocab set ] [ require ] [
557                 vocab-main [
558                     "Vocabulary has no MAIN: word." print flush 1 exit
559                 ] unless
560             ] tri
561             strip
562             "Saving final image" show
563             save-image-and-exit
564         ] deploy-error-handler
565     ] bind ;
566
567 : do-deploy ( -- )
568     "output-image" get
569     "vocab-manifest-out" get
570     "deploy-vocab" get
571     "Deploying " write dup write "..." print
572     "deploy-config" get parse-file first
573     (deploy) ;
574
575 MAIN: do-deploy