factor/basis/tools/deploy/shaker/shaker.factor

655 lines
19 KiB
Factor
Raw Normal View History

! Copyright (C) 2007, 2010 Slava Pestov.
2007-10-06 13:37:47 -04:00
! See http://factorcode.org/license.txt for BSD license.
USING: arrays alien.libraries accessors io.backend
io.encodings.utf8 io.files io.streams.c init fry namespaces math
make assocs kernel parser parser.notes lexer strings.parser
vocabs sequences sequences.deep sequences.private words memory
kernel.private continuations io vocabs.loader system strings
sets vectors quotations byte-arrays sorting compiler.units
definitions generic generic.standard generic.single
tools.deploy.config combinators combinators.private classes
vocabs.loader.private classes.builtin slots.private grouping
command-line io.pathnames memoize namespaces.private
hashtables locals ;
QUALIFIED: bootstrap.stage2
2010-01-31 12:02:09 -05:00
QUALIFIED: classes.private
QUALIFIED: compiler.crossref
2008-11-16 14:46:30 -05:00
QUALIFIED: compiler.errors
QUALIFIED: continuations
QUALIFIED: definitions
QUALIFIED: init
QUALIFIED: layouts
QUALIFIED: source-files
QUALIFIED: source-files.errors
QUALIFIED: vocabs
QUALIFIED: vocabs.loader
FROM: assocs => change-at ;
FROM: namespaces => set ;
FROM: sequences => change-nth ;
2007-10-06 13:37:47 -04:00
IN: tools.deploy.shaker
2008-07-02 16:57:38 -04:00
! This file is some hairy shit.
: add-command-line-hook ( -- )
[
(command-line) unclip
executable set-global
command-line set-global
] "command-line" startup-hooks get set-at ;
: strip-startup-hooks ( -- )
2007-10-06 13:37:47 -04:00
"Stripping startup hooks" show
{
"alien.strings"
"cpu.x86"
"environment"
"libc"
}
[ startup-hooks get delete-at ] each
deploy-threads? get [
"threads" startup-hooks get delete-at
] unless
strip-io? [
"io.backend" startup-hooks get delete-at
] when
strip-dictionary? [
{
"vocabs"
"vocabs.cache"
"source-files.errors"
} [ startup-hooks get delete-at ] each
] when ;
2007-10-06 13:37:47 -04:00
: strip-debugger ( -- )
2011-10-24 20:00:09 -04:00
strip-debugger? "debugger" lookup-vocab and [
2007-10-06 13:37:47 -04:00
"Stripping debugger" show
2009-02-15 20:53:21 -05:00
"vocab:tools/deploy/shaker/strip-debugger.factor"
2007-10-06 13:37:47 -04:00
run-file
] when ;
: strip-ui-error-hook ( -- )
2011-10-24 20:00:09 -04:00
strip-debugger? deploy-ui? get and "ui" lookup-vocab and [
"Installing generic UI error hook" show
"vocab:tools/deploy/shaker/strip-ui-error-hook.factor"
run-file
] when ;
: strip-libc ( -- )
2011-10-24 20:00:09 -04:00
"libc" lookup-vocab [
"Stripping manual memory management debug code" show
2009-02-15 20:53:21 -05:00
"vocab:tools/deploy/shaker/strip-libc.factor"
run-file
2007-10-06 13:37:47 -04:00
] when ;
: strip-destructors ( -- )
"Stripping destructor debug code" show
"vocab:tools/deploy/shaker/strip-destructors.factor"
run-file ;
: strip-call ( -- )
"Stripping stack effect checking from call( and execute(" show
"vocab:tools/deploy/shaker/strip-call.factor" run-file ;
2007-10-06 13:37:47 -04:00
: strip-cocoa ( -- )
2011-10-24 20:00:09 -04:00
"cocoa" lookup-vocab [
2007-10-06 13:37:47 -04:00
"Stripping unused Cocoa methods" show
2009-02-15 20:53:21 -05:00
"vocab:tools/deploy/shaker/strip-cocoa.factor"
2007-10-06 13:37:47 -04:00
run-file
] when ;
: strip-gobject ( -- )
2011-10-24 20:00:09 -04:00
"gobject-introspection.types" lookup-vocab [
"Stripping GObject type info" show
"vocab:tools/deploy/shaker/strip-gobject.factor"
run-file
] when ;
: strip-gtk-icon ( -- )
2011-10-24 20:00:09 -04:00
"ui.backend.gtk" lookup-vocab [
"Stripping GTK icon loading code" show
"vocab:tools/deploy/shaker/strip-gtk-icon.factor"
run-file
] when ;
: strip-specialized-arrays ( -- )
2011-10-24 20:00:09 -04:00
strip-dictionary? "specialized-arrays" lookup-vocab and [
"Stripping specialized arrays" show
"vocab:tools/deploy/shaker/strip-specialized-arrays.factor"
run-file
] when ;
2007-10-06 13:37:47 -04:00
: strip-word-names ( words -- )
"Stripping word names" show
2008-06-30 17:06:14 -04:00
[ f >>name f >>vocabulary drop ] each ;
2007-10-06 13:37:47 -04:00
: strip-word-defs ( words -- )
2008-01-07 16:14:21 -05:00
"Stripping symbolic word definitions" show
[ "no-def-strip" word-prop ] reject
2008-06-30 17:06:14 -04:00
[ [ ] >>def drop ] each ;
2007-10-06 13:37:47 -04:00
2008-06-25 21:47:07 -04:00
: strip-word-props ( stripped-props words -- )
2007-10-06 13:37:47 -04:00
"Stripping word properties" show
swap '[
[
[ drop _ member? ] assoc-reject sift-values
>alist f like
] change-props drop
] each ;
2007-10-06 13:37:47 -04:00
2008-06-25 21:47:07 -04:00
: stripped-word-props ( -- seq )
2007-10-06 13:37:47 -04:00
[
2008-06-25 21:47:07 -04:00
strip-dictionary? [
{
2008-08-05 05:38:01 -04:00
"alias"
"boa-check"
2008-06-25 21:47:07 -04:00
"coercer"
"combination"
"generic-call-sites"
"effect-dependencies"
"definition-dependencies"
"conditional-dependencies"
"dependency-checks"
"constant"
2008-06-25 21:47:07 -04:00
"constraints"
"custom-inlining"
"decision-tree"
2008-06-25 21:47:07 -04:00
"declared-effect"
2008-07-05 04:07:10 -04:00
"default"
"default-method"
2008-06-25 21:47:07 -04:00
"default-output-classes"
2008-07-05 04:07:10 -04:00
"derived-from"
"ebnf-parser"
2008-08-31 20:17:04 -04:00
"engines"
"forgotten"
"identities"
2008-08-31 20:17:04 -04:00
"inline"
"inlined-block"
2008-07-12 02:08:30 -04:00
"input-classes"
2009-03-17 19:53:44 -04:00
"instances"
2008-06-25 21:47:07 -04:00
"interval"
"intrinsic"
2008-08-31 20:17:04 -04:00
"lambda"
2008-06-25 21:47:07 -04:00
"loc"
2008-08-31 20:17:04 -04:00
"local-reader"
"local-reader?"
"local-writer"
"local-writer?"
"local?"
"low-order"
2008-08-31 20:17:04 -04:00
"macro"
"members"
2008-08-31 20:17:04 -04:00
"memo-quot"
"methods"
2008-07-05 04:07:10 -04:00
"method-class"
"method-generic"
"modular-arithmetic"
2008-07-05 04:07:10 -04:00
"no-compile"
"owner-generic"
2008-08-31 20:17:04 -04:00
"outputs"
2008-06-25 21:47:07 -04:00
"participants"
"predicate"
"predicate-definition"
"predicating"
2008-08-31 20:17:04 -04:00
"reader"
"reading"
"recursive"
2008-08-05 05:38:01 -04:00
"register"
"register-size"
2008-08-31 20:17:04 -04:00
"shuffle"
"slots"
"special"
2008-06-25 21:47:07 -04:00
"specializer"
"struct-slots"
! UI needs this
! "superclass"
2008-08-31 20:17:04 -04:00
"transform-n"
"transform-quot"
2008-06-25 21:47:07 -04:00
"type"
"typed-def"
"typed-word"
2008-08-31 20:17:04 -04:00
"writer"
"writing"
2008-06-25 21:47:07 -04:00
} %
] when
2008-06-25 21:47:07 -04:00
strip-prettyprint? [
{
"delimiter"
"flushable"
"foldable"
"inline"
"lambda"
"macro"
"memo-quot"
"parsing"
"word-style"
} %
2007-10-06 13:37:47 -04:00
] when
2009-09-24 07:24:43 -04:00
deploy-c-types? get [
2009-09-24 14:00:26 -04:00
{ "c-type" "struct-slots" "struct-align" } %
2009-09-24 07:24:43 -04:00
] unless
2007-10-06 13:37:47 -04:00
] { } make ;
: strip-words ( props -- )
[ word? ] instances
deploy-word-props? get [ 2dup strip-word-props ] unless
deploy-word-defs? get [ dup strip-word-defs ] unless
strip-word-names? [ dup strip-word-names strip-stack-traces ] when
2drop ;
2007-10-06 13:37:47 -04:00
: strip-memoized ( -- )
"Clearing memoized word caches" show
[ memoized? ] instances [ reset-memoized ] each ;
: compiler-classes ( -- seq )
{ "compiler" "stack-checker" }
[ loaded-child-vocab-names [ vocab-words ] map concat [ class? ] filter ]
map concat unique ;
: prune-decision-tree ( tree classes -- )
[ tuple class>type ] 2dip '[
dup array? [
[
dup array? [
[
2 group
[ drop _ key? ] assoc-reject
concat
] map
] when
] map
] when
] change-nth ;
: strip-compiler-classes ( -- )
strip-dictionary? [
"Stripping compiler classes" show
[ single-generic? ] instances
compiler-classes '[ "decision-tree" word-prop _ prune-decision-tree ] each
] when ;
: recursive-subst ( seq old new -- )
'[
_ _
{
! old becomes new
2012-10-22 23:45:15 -04:00
{ [ 2over eq? ] [ 2nip ] }
! recurse into arrays
{ [ pick array? ] [ [ dup ] 2dip recursive-subst ] }
! otherwise do nothing
[ 2drop ]
} cond
] map! drop ;
: strip-default-method ( generic new-default -- )
[
[ "decision-tree" word-prop ]
[ "default-method" word-prop ] bi
] dip
recursive-subst ;
: new-default-method ( -- gensym )
[ [ "No method" throw ] ( -- * ) define-temp ] with-compilation-unit ;
: strip-default-methods ( -- )
! In a development image, each generic has its own default method.
! This gives better error messages for runtime type errors, but
! takes up space. For deployment we merge them all together.
strip-debugger? [
"Stripping default methods" show
[ single-generic? ] instances
new-default-method '[ _ strip-default-method ] each
] when ;
: vocab-tree-globals ( except names -- words )
[ loaded-child-vocab-names [ vocab-words ] map concat ] map concat
2011-11-06 18:57:24 -05:00
swap [ first2 lookup-word ] map sift diff ;
: stripped-globals ( -- seq )
[
2011-11-06 18:57:24 -05:00
"inspector-hook" "inspector" lookup-word ,
{
source-files:source-files
continuations:error
continuations:error-continuation
continuations:error-thread
continuations:restarts
} %
2011-11-06 18:57:24 -05:00
"disposables" "destructors" lookup-word ,
2011-11-06 18:57:24 -05:00
"functor-words" "functors.backend" lookup-word ,
2008-06-25 21:47:07 -04:00
{ } {
"stack-checker"
"listener"
"bootstrap"
} vocab-tree-globals %
! Don't want to strip globals from test programs
{ } { "tools" } vocab-tree-globals
{ } { "tools.deploy.test" } vocab-tree-globals diff %
deploy-unicode? get [
{ } { "unicode" } vocab-tree-globals %
] unless
strip-dictionary? [
2011-11-06 18:57:24 -05:00
"libraries" "alien" lookup-word ,
2008-08-05 05:38:01 -04:00
2009-03-23 23:36:51 -04:00
{ { "yield-hook" "compiler.utilities" } }
{ "cpu" "compiler" } vocab-tree-globals %
{
2008-04-04 11:11:31 -04:00
gensym
name>char-hook
2010-01-31 12:02:09 -05:00
classes.private:next-method-quot-cache
classes.private:class-and-cache
classes.private:class-not-cache
classes.private:class-or-cache
classes.private:class<=-cache
classes.private:classes-intersect-cache
classes.private:implementors-map
classes.private:update-map
main-vocab-hook
compiler.crossref:compiled-crossref
compiler.crossref:generic-call-site-crossref
compiler-impl
compiler.errors:compiler-errors
2008-11-20 22:54:07 -05:00
print-use-hook
2008-03-24 21:15:42 -04:00
root-cache
require-when-vocabs
require-when-table
source-files.errors:error-types
source-files.errors:error-observers
2008-03-24 21:15:42 -04:00
vocabs:dictionary
vocabs:require-hook
vocabs:vocab-observers
vocabs.loader:add-vocab-root-hook
word
2011-09-07 03:34:23 -04:00
parser-quiet?
} %
{ } { "layouts" } vocab-tree-globals %
{ } { "math.partial-dispatch" } vocab-tree-globals %
{ } { "math.vectors.simd" } vocab-tree-globals %
2009-09-24 07:24:43 -04:00
{ } { "peg" } vocab-tree-globals %
] when
strip-prettyprint? [
{ } { "prettyprint.config" } vocab-tree-globals %
] when
strip-debugger? [
\ compiler.errors:compiler-errors ,
] when
] { } make ;
: cleared-globals ( -- seq )
[
{
init:startup-hooks
input-stream
output-stream
error-stream
vm-path
image-path
current-directory
} %
"io-thread" "io.thread" lookup-word ,
deploy-threads? [
"initial-thread" "threads" lookup-word ,
] unless
strip-io? [ io-backend , ] when
{ } {
"timers"
"io.launcher"
"random"
} vocab-tree-globals %
2011-11-06 18:57:24 -05:00
"windows-messages" "windows.messages" lookup-word [ , ] when*
] { } make ;
: strip-global? ( name stripped-globals -- ? )
'[ _ member? ] [ tuple? ] bi or ;
: clear-global? ( name cleared-globals -- ? )
'[ _ member? ] [ string? ] bi or ;
: strip-globals ( -- )
strip-globals? [| |
"Stripping globals" show
stripped-globals :> to-strip
cleared-globals :> to-clear
global boxes>>
[ drop to-strip strip-global? ] assoc-reject!
[
[
swap to-clear clear-global?
[ f swap value<< ] [ drop ] if
] assoc-each
] [ rehash ] bi
] when ;
2007-10-06 13:37:47 -04:00
2008-10-02 04:38:36 -04:00
: strip-c-io ( -- )
! On all platforms, if deploy-io is 1, we strip out C streams.
! On Unix, if deploy-io is 3, we strip out C streams as well.
! On Windows, even if deploy-io is 3, C streams are still used
! for the console, so don't strip it there.
strip-io?
2011-10-15 22:19:44 -04:00
native-io? os windows? not and
or [
"Stripping C I/O" show
"vocab:tools/deploy/shaker/strip-c-io.factor" run-file
] when ;
2008-10-02 04:38:36 -04:00
: compress ( pred post-process string -- )
2008-07-05 04:07:10 -04:00
"Compressing " prepend show
[ instances dup H{ } clone [ [ ] cache ] curry map ] dip call
2008-07-05 04:07:10 -04:00
become ; inline
: compress-object? ( obj -- ? )
{
{ [ dup array? ] [ empty? ] }
{ [ dup byte-array? ] [ drop t ] }
{ [ dup string? ] [ drop t ] }
{ [ dup wrapper? ] [ drop t ] }
[ drop f ]
} cond ;
: compress-objects ( -- )
[ compress-object? ] [ ] "objects" compress ;
: remain-compiled ( old new -- old new )
! Quotations which were formerly compiled must remain
! compiled.
2dup [
2dup [ quotation-compiled? ] [ quotation-compiled? not ] bi* and
[ nip jit-compile ] [ 2drop ] if
] 2each ;
2008-07-05 04:07:10 -04:00
: compress-quotations ( -- )
2009-03-17 19:53:44 -04:00
[ quotation? ] [ remain-compiled ] "quotations" compress
[ quotation? ] instances [ f >>cached-effect f >>cache-counter drop ] each ;
2008-07-05 04:07:10 -04:00
2007-10-06 13:37:47 -04:00
SYMBOL: deploy-vocab
2011-11-06 18:57:24 -05:00
: [:c] ( -- word ) ":c" "debugger" lookup-word ;
2011-11-06 18:57:24 -05:00
: [print-error] ( -- word ) "print-error" "debugger" lookup-word ;
2009-11-20 01:55:16 -05:00
: deploy-startup-quot ( word -- )
2007-10-06 13:37:47 -04:00
[
[ boot ] %
startup-hooks get values concat %
strip-debugger? [ , ] [
! Don't reference 'try' directly since we don't want
! to pull in the debugger and prettyprinter into every
! deployed app
[:c]
[print-error]
'[
2009-04-17 15:44:08 -04:00
[ _ execute( obj -- ) ] [
_ execute( obj -- ) nl
_ execute( obj -- )
] recover
] %
] if
strip-io? [ [ flush ] % ] unless
[ 0 exit ] %
2008-12-08 15:58:00 -05:00
] [ ] make
2009-11-20 01:55:16 -05:00
set-startup-quot ;
2007-10-06 13:37:47 -04:00
: startup-stripper ( -- )
t parser-quiet? set-global
f output-stream set-global
[ V{ "resource:" } clone vocab-roots set-global ]
"vocabs.loader" startup-hooks get-global set-at ;
2008-10-02 07:41:17 -04:00
: next-method* ( method -- quot )
[ "method-class" word-prop ]
[ "method-generic" word-prop ] bi
next-method ;
: calls-next-method? ( method -- ? )
def>> flatten \ (call-next-method) swap member-eq? ;
: compute-next-methods ( -- )
[ standard-generic? ] instances [
"methods" word-prop values [ calls-next-method? ] filter
[ dup next-method* "next-method" set-word-prop ] each
] each
2009-02-15 20:53:21 -05:00
"vocab:tools/deploy/shaker/next-methods.factor" run-file ;
: (clear-megamorphic-cache) ( i array -- )
! Can't do any dispatch while clearing caches since that
! might leave them in an inconsistent state.
2dup 1 slot < [
2dup [ f ] 2dip set-array-nth
[ 1 + ] dip (clear-megamorphic-cache)
] [ 2drop ] if ;
: clear-megamorphic-cache ( array -- )
[ 0 ] dip (clear-megamorphic-cache) ;
: find-megamorphic-caches ( -- seq )
"Finding megamorphic caches" show
[ standard-generic? ] instances [ def>> third ] map ;
: clear-megamorphic-caches ( cache -- )
"Clearing megamorphic caches" show
[ clear-megamorphic-cache ] each ;
: write-vocab-manifest ( vocab-manifest-out -- )
"Writing vocabulary manifest to " write dup print flush
loaded-vocab-names "VOCABS:" prefix
deploy-libraries get [ lookup-library path>> ] map members
"LIBRARIES:" prefix append
swap utf8 set-file-lines ;
: prepare-deploy-libraries ( -- )
2010-02-16 17:23:51 -05:00
"Preparing deployed libraries" show
deploy-libraries get [
libraries get [
[ path>> >deployed-library-path ] [ abi>> ] bi make-library
] change-at
] each
[
2011-11-06 18:57:24 -05:00
"deploy-libraries" "alien.libraries" lookup-word forget
"deploy-library" "alien.libraries" lookup-word forget
">deployed-library-path" "alien.libraries.private" lookup-word forget
] with-compilation-unit ;
: strip ( vocab-manifest-out -- )
[ write-vocab-manifest ] when*
startup-stripper
prepare-deploy-libraries
2008-01-07 16:14:21 -05:00
strip-libc
strip-destructors
strip-call
2007-10-06 13:37:47 -04:00
strip-cocoa
strip-gobject
strip-gtk-icon
2007-10-06 13:37:47 -04:00
strip-debugger
strip-ui-error-hook
strip-specialized-arrays
compute-next-methods
strip-startup-hooks
add-command-line-hook
2008-10-02 07:41:17 -04:00
strip-c-io
strip-default-methods
strip-compiler-classes
! we can't use the Factor debugger or Factor I/O anymore
f ERROR-HANDLER-QUOT set-special-object
2009-11-20 01:55:16 -05:00
deploy-vocab get vocab-main deploy-startup-quot
find-megamorphic-caches
stripped-word-props
strip-globals
compress-objects
2008-07-05 04:07:10 -04:00
compress-quotations
strip-words
strip-memoized
clear-megamorphic-caches ;
2007-10-06 13:37:47 -04:00
: die-with ( error original-error -- * )
#! We don't want DCE to drop the error before the die call!
[ die 1 exit ] ( a -- * ) call-effect-unsafe ;
: die-with2 ( error original-error -- * )
#! We don't want DCE to drop the error before the die call!
[ die 1 exit ] ( a b -- * ) call-effect-unsafe ;
: deploy-error-handler ( quot -- )
[
strip-debugger?
[ original-error get die-with2 ]
! Don't reference these words literally, if we're stripping the
! debugger out we don't want to load the prettyprinter at all
[ [:c] execute( -- ) nl [print-error] execute( error -- ) flush ] if
1 exit
] recover ; inline
: (deploy) ( final-image vocab-manifest-out vocab config -- )
2007-10-06 13:37:47 -04:00
#! Does the actual work of a deployment in the slave
#! stage2 image
[
[
strip-debugger? [
"debugger" require
"tools.errors" require
"inspector" require
deploy-ui? get [
"ui.debugger" require
] when
] unless
[ deploy-vocab set ] [ require ] [
vocab-main [
"Vocabulary has no MAIN: word." print flush 1 exit
] unless
] tri
2007-10-06 13:37:47 -04:00
strip
"Saving final image" show
save-image-and-exit
] deploy-error-handler
] with-variables ;
2007-10-06 13:37:47 -04:00
: do-deploy ( -- )
"output-image" get
"vocab-manifest-out" get
2007-10-06 13:37:47 -04:00
"deploy-vocab" get
"Deploying " write dup write "..." print
2008-12-08 15:58:00 -05:00
"deploy-config" get parse-file first
2007-10-06 13:37:47 -04:00
(deploy) ;
MAIN: do-deploy