1 ! Copyright (C) 2007, 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors io.backend io.streams.c init fry
4 namespaces make assocs kernel parser lexer strings.parser vocabs
5 sequences words words.private memory kernel.private
6 continuations io vocabs.loader system strings sets
7 vectors quotations byte-arrays sorting compiler.units
8 definitions generic generic.standard tools.deploy.config ;
9 QUALIFIED: bootstrap.stage2
11 QUALIFIED: command-line
12 QUALIFIED: compiler.errors
13 QUALIFIED: continuations
14 QUALIFIED: definitions
17 QUALIFIED: source-files
19 IN: tools.deploy.shaker
21 ! This file is some hairy shit.
23 : strip-init-hooks ( -- )
24 "Stripping startup hooks" show
25 { "cpu.x86" "command-line" "libc" "system" "environment" }
26 [ init-hooks get delete-at ] each
28 "threads" init-hooks get delete-at
31 "io.thread" init-hooks get delete-at
34 "io.files" init-hooks get delete-at
35 "io.backend" init-hooks get delete-at
38 "compiler.units" init-hooks get delete-at
39 "tools.vocabs" init-hooks get delete-at
42 : strip-debugger ( -- )
43 strip-debugger? "debugger" vocab and [
44 "Stripping debugger" show
45 "vocab:tools/deploy/shaker/strip-debugger.factor"
51 "Stripping manual memory management debug code" show
52 "vocab:tools/deploy/shaker/strip-libc.factor"
57 "Stripping stack effect checking from call( and execute(" show
58 "vocab:tools/deploy/shaker/strip-call.factor" run-file ;
62 "Stripping unused Cocoa methods" show
63 "vocab:tools/deploy/shaker/strip-cocoa.factor"
67 : strip-word-names ( words -- )
68 "Stripping word names" show
69 [ f >>name f >>vocabulary drop ] each ;
71 : strip-word-defs ( words -- )
72 "Stripping symbolic word definitions" show
73 [ "no-def-strip" word-prop not ] filter
74 [ [ ] >>def drop ] each ;
76 : sift-assoc ( assoc -- assoc' ) [ nip ] assoc-filter ;
78 : strip-word-props ( stripped-props words -- )
79 "Stripping word properties" show
83 [ drop _ member? not ] assoc-filter sift-assoc
89 [ [ _ [ ] cache ] map ] change-props drop
93 : stripped-word-props ( -- seq )
103 "compiled-generic-uses"
110 "default-output-classes"
145 "predicate-definition"
164 "tuple-dispatch-generic"
188 : strip-words ( props -- )
190 deploy-word-props? get [ 2dup strip-word-props ] unless
191 deploy-word-defs? get [ dup strip-word-defs ] unless
192 strip-word-names? [ dup strip-word-names ] when
195 : strip-default-methods ( -- )
197 "Stripping default methods" show
199 [ generic? ] instances
200 [ "No method" throw ] (( -- * )) define-temp
201 dup t "default" set-word-prop
203 [ _ "default-method" set-word-prop ] [ make-generic ] bi
205 ] with-compilation-unit
208 : strip-vocab-globals ( except names -- words )
209 [ child-vocabs [ words ] map concat ] map concat
210 swap [ first2 lookup ] map sift diff ;
212 : stripped-globals ( -- seq )
214 "inspector-hook" "inspector" lookup ,
218 continuations:error-continuation
219 continuations:error-thread
220 continuations:restarts
222 source-files:source-files
228 "io-thread" "io.thread" lookup ,
230 "mallocs" "libc.private" lookup ,
233 "initial-thread" "threads" lookup ,
236 strip-io? [ io-backend , ] when
246 } strip-vocab-globals %
249 "libraries" "alien" lookup ,
251 { { "yield-hook" "compiler.utilities" } }
252 { "cpu" "compiler" } strip-vocab-globals %
257 classes:next-method-quot-cache
258 classes:class-and-cache
259 classes:class-not-cache
260 classes:class-or-cache
261 classes:class<=-cache
262 classes:classes-intersect-cache
263 classes:implementors-map
265 command-line:main-vocab-hook
267 compiled-generic-crossref
281 vocabs:load-vocab-hook
286 { } { "math.partial-dispatch" } strip-vocab-globals %
288 { } { "peg" } strip-vocab-globals %
292 { } { "prettyprint.config" } strip-vocab-globals %
297 compiler.errors:compiler-errors
298 continuations:thread-error-hook
302 deploy-c-types? get [
303 "c-types" "alien.c-types" lookup ,
307 "ui-error-hook" "ui.gadgets.worlds" lookup ,
310 "windows-messages" "windows.messages" lookup [ , ] when*
313 : strip-globals ( stripped-globals -- )
315 "Stripping globals" show
317 '[ drop _ member? not ] assoc-filter
318 [ drop string? not ] assoc-filter ! strip CLI args
324 deploy-io get 2 = os windows? or [
327 "io.streams.c" forget-vocab
328 ] with-compilation-unit
331 : compress ( pred post-process string -- )
332 "Compressing " prepend show
333 [ instances dup H{ } clone [ [ ] cache ] curry map ] dip call
336 : compress-byte-arrays ( -- )
337 [ byte-array? ] [ ] "byte arrays" compress ;
339 : remain-compiled ( old new -- old new )
340 #! Quotations which were formerly compiled must remain
343 2dup [ compiled>> ] [ compiled>> not ] bi* and
344 [ nip jit-compile ] [ 2drop ] if
347 : compress-quotations ( -- )
348 [ quotation? ] [ remain-compiled ] "quotations" compress
349 [ quotation? ] instances [ f >>cached-effect f >>cache-counter drop ] each ;
351 : compress-strings ( -- )
352 [ string? ] [ ] "strings" compress ;
354 : compress-wrappers ( -- )
355 [ wrapper? ] [ ] "wrappers" compress ;
357 : finish-deploy ( final-image -- )
359 [ { } set-datastack ] dip
363 "Saving final image" show
364 [ save-image-and-exit ] call-clear ;
368 : [:c] ( -- word ) ":c" "debugger" lookup ;
370 : [print-error] ( -- word ) "print-error" "debugger" lookup ;
372 : deploy-boot-quot ( word -- )
375 init-hooks get values concat %
376 strip-debugger? [ , ] [
377 ! Don't reference try directly
387 strip-io? [ [ flush ] % ] unless
392 : init-stripper ( -- )
394 f output-stream set-global ;
396 : compute-next-methods ( -- )
397 [ standard-generic? ] instances [
398 "methods" word-prop [
400 dup next-method-quot "next-method-quot" set-word-prop
403 "vocab:tools/deploy/shaker/next-methods.factor" run-file ;
407 strip-default-methods
415 f 5 setenv ! we can't use the Factor debugger or Factor I/O anymore
416 deploy-vocab get vocab-main deploy-boot-quot
418 stripped-globals strip-globals
425 : deploy-error-handler ( quot -- )
428 [ error-continuation get call>> callstack>array die ]
429 ! Don't reference these words literally, if we're stripping the
430 ! debugger out we don't want to load the prettyprinter at all
431 [ [:c] execute nl [print-error] execute flush ] if
435 : (deploy) ( final-image vocab config -- )
436 #! Does the actual work of a deployment in the slave
445 deploy-vocab get require
446 deploy-vocab get vocab-main [
447 "Vocabulary has no MAIN: word." print flush 1 exit
451 ] deploy-error-handler
457 "Deploying " write dup write "..." print
458 "deploy-config" get parse-file first