alien.libraries.finder: Roll the *.os vocabs back into alien.libraries.finder for fun.

modern-harvey3-triple
Doug Coleman 2018-08-07 16:34:25 -04:00
parent f09eade430
commit 1355c94f33
9 changed files with 215 additions and 275 deletions

View File

@ -20,4 +20,218 @@ HOOK: find-library* os ( name -- path/f )
dup [ find-library* ] map-find drop
[ ] [ ?first "library_not_found" or ] ?if ;
"alien.libraries.finder." os name>> append require
<LINUX
USING: alien.libraries.finder arrays assocs
combinators.short-circuit io io.encodings.utf8 io.files
io.files.info io.launcher kernel sequences sets splitting system
unicode ;
<PRIVATE
CONSTANT: mach-map {
{ ppc.64 { "libc6" "64bit" } }
{ x86.32 { "libc6" "x32" } }
{ x86.64 { "libc6" "x86-64" } }
}
: parse-ldconfig-lines ( string -- triple )
[
"=>" split1 [ [ blank? ] trim ] bi@
[
" " split1 [ "()" in? ] trim "," split
[ [ blank? ] trim ] map
[ ": Linux" swap subseq? ] reject
] dip 3array
] map ;
: load-ldconfig-cache ( -- seq )
"/sbin/ldconfig -p" utf8 [ lines ] with-process-reader
rest parse-ldconfig-lines ;
: ldconfig-arch ( -- str )
mach-map cpu of { "libc6" } or ;
: name-matches? ( lib triple -- ? )
first swap ?head [ ?first char: . = ] [ drop f ] if ;
: arch-matches? ( lib triple -- ? )
[ drop ldconfig-arch ] [ second swap subset? ] bi* ;
: ldconfig-matches? ( lib triple -- ? )
{ [ name-matches? ] [ arch-matches? ] } 2&& ;
PRIVATE>
M: linux find-library*
"lib" prepend load-ldconfig-cache
[ ldconfig-matches? ] with find nip ?first ;
LINUX>
<MACOS
USING: accessors alien.libraries.finder arrays assocs
combinators.short-circuit environment io.files io.files.info
io.pathnames kernel locals make namespaces sequences splitting
system ;
<PRIVATE
TUPLE: framework-info location name shortname version suffix ;
: make-framework-info ( filename -- info/f )
[ framework-info new ] dip
"/" split dup [ ".framework" tail? ] find drop [
cut [
[ "/" join ] bi@ [ >>location ] [ >>name ] bi*
] keep [
rest dup ?first "Versions" = [
rest dup empty? [
unclip swap [ >>version ] dip
] unless
] when ?first "_" split1 [ >>shortname ] [ >>suffix ] bi*
] unless-empty
] [ drop ] if* dup shortname>> empty? [ drop f ] when ;
CONSTANT: default-framework-fallback {
"~/Library/Frameworks"
"/Library/Frameworks"
"/Network/Library/Frameworks"
"/System/Library/Frameworks"
}
CONSTANT: default-library-fallback {
"~/lib"
"/usr/local/lib"
"/lib"
"/usr/lib"
}
SYMBOL: dyld-environment
: dyld-env ( name -- seq )
dyld-environment get [ at ] [ os-env ] if* ;
: dyld-paths ( name -- seq )
dyld-env [ ":" split ] [ f ] if* ;
: paths% ( name seq -- )
[ prepend-path , ] with each ;
: dyld-override-search ( name -- seq )
[
dup make-framework-info [
name>> "DYLD_FRAMEWORK_PATH" dyld-paths paths%
] when*
file-name "DYLD_LIBRARY_PATH" dyld-paths paths%
] { } make ;
SYMBOL: dyld-executable-path
: dyld-executable-path-search ( name -- seq )
"@executable_path/" ?head dyld-executable-path get and [
dyld-executable-path get prepend-path
] [
drop f
] if ;
:: dyld-default-search ( name -- seq )
name make-framework-info :> framework
name file-name :> basename
"DYLD_FALLBACK_FRAMEWORK_PATH" dyld-paths :> fallback-framework-path
"DYLD_FALLBACK_LIBRARY_PATH" dyld-paths :> fallback-library-path
[
name ,
framework [
name>> fallback-framework-path paths%
] when*
basename fallback-library-path paths%
framework fallback-framework-path empty? and [
framework name>> default-framework-fallback paths%
] when
fallback-library-path empty? [
basename default-library-fallback paths%
] when
] { } make ;
: dyld-image-suffix-search ( seq -- str )
"DYLD_IMAGE_SUFFIX" dyld-env [
swap [
[
[
".dylib" ?tail [ prepend ] dip
[ ".dylib" append ] when ,
] [
,
] bi
] with each
] { } make
] when* ;
: dyld-search-paths ( name -- paths )
[ dyld-override-search ]
[ dyld-executable-path-search ]
[ dyld-default-search ] tri 3append
dyld-image-suffix-search ;
PRIVATE>
: dyld-find ( name -- path/f )
dyld-search-paths
[ { [ exists? ] [ file-info regular-file? ] } 1&& ] find
[ nip ] when* ;
: framework-find ( name -- path )
dup dyld-find [ nip ] [
".framework" over subseq-start [
dupd head
] [
[ ".framework" append ] keep
] if* file-name append-path dyld-find
] if* ;
M: macosx find-library*
[ "lib" ".dylib" surround ]
[ ".dylib" append ]
[ ".framework/" over 3append ] tri 3array
[ dyld-find ] map-find drop ;
MACOS>
<WINDOWS
USING: alien.libraries.finder arrays combinators.short-circuit
environment io.backend io.files io.files.info io.pathnames kernel
sequences splitting system system-info.windows ;
<PRIVATE
: search-paths ( -- seq )
"resource:" normalize-path
system-directory
windows-directory 3array
"PATH" os-env [ ";" split ] [ f ] if* append ;
: candidate-paths ( name -- seq )
search-paths over ".dll" tail? [
[ prepend-path ] with map
] [
[
[ prepend-path ]
[ [ ".dll" append ] [ prepend-path ] bi* ] 2bi
2array
] with map concat
] if ;
PRIVATE>
M: windows find-library*
candidate-paths [
{ [ exists? ] [ file-info regular-file? ] } 1&&
] find nip ;
WINDOWS>

View File

@ -1,5 +0,0 @@
USING: alien.libraries.finder sequences tools.test ;
IN: alien.libraries.finder.linux.tests
{ t } [ "libm.so" "m" find-library subseq? ] unit-test
{ t } [ "libc.so" "c" find-library subseq? ] unit-test

View File

@ -1,47 +0,0 @@
! Copyright (C) 2013 Björn Lindqvist, Doug Coleman.
! See http://factorcode.org/license.txt for BSD license
USING: alien.libraries.finder arrays assocs
combinators.short-circuit io io.encodings.utf8 io.files
io.files.info io.launcher kernel sequences sets splitting system
unicode ;
IN: alien.libraries.finder.linux
<PRIVATE
CONSTANT: mach-map {
{ ppc.64 { "libc6" "64bit" } }
{ x86.32 { "libc6" "x32" } }
{ x86.64 { "libc6" "x86-64" } }
}
: parse-ldconfig-lines ( string -- triple )
[
"=>" split1 [ [ blank? ] trim ] bi@
[
" " split1 [ "()" in? ] trim "," split
[ [ blank? ] trim ] map
[ ": Linux" swap subseq? ] reject
] dip 3array
] map ;
: load-ldconfig-cache ( -- seq )
"/sbin/ldconfig -p" utf8 [ lines ] with-process-reader
rest parse-ldconfig-lines ;
: ldconfig-arch ( -- str )
mach-map cpu of { "libc6" } or ;
: name-matches? ( lib triple -- ? )
first swap ?head [ ?first char: . = ] [ drop f ] if ;
: arch-matches? ( lib triple -- ? )
[ drop ldconfig-arch ] [ second swap subset? ] bi* ;
: ldconfig-matches? ( lib triple -- ? )
{ [ name-matches? ] [ arch-matches? ] } 2&& ;
PRIVATE>
M: linux find-library*
"lib" prepend load-ldconfig-cache
[ ldconfig-matches? ] with find nip ?first ;

View File

@ -1 +0,0 @@
linux

View File

@ -1,50 +0,0 @@
USING: alien.libraries.finder
alien.libraries.finder.macosx.private sequences tools.test ;
IN: alien.libraries.finder.macosx
{
{
f
f
f
f
T{ framework-info f "Location" "Name.framework/Name" "Name" f f }
T{ framework-info f "Location" "Name.framework/Name_suffix" "Name" f "suffix" }
f
f
T{ framework-info f "Location" "Name.framework/Versions/A/Name" "Name" "A" f }
T{ framework-info f "Location" "Name.framework/Versions/A/Name_suffix" "Name" "A" "suffix" }
}
} [
{
"broken/path"
"broken/path/_suffix"
"Location/Name.framework"
"Location/Name.framework/_suffix"
"Location/Name.framework/Name"
"Location/Name.framework/Name_suffix"
"Location/Name.framework/Versions"
"Location/Name.framework/Versions/A"
"Location/Name.framework/Versions/A/Name"
"Location/Name.framework/Versions/A/Name_suffix"
} [ make-framework-info ] map
] unit-test
{
{
"/usr/lib/libSystem.dylib"
"/System/Library/Frameworks/System.framework/System"
}
} [
{
"libSystem.dylib"
"System.framework/System"
} [ dyld-find ] map
] unit-test
{ t } [ "libm.dylib" "m" find-library subseq? ] unit-test
{ t } [ "libc.dylib" "c" find-library subseq? ] unit-test
{ t } [ "libbz2.dylib" "bz2" find-library subseq? ] unit-test
{ t } [ "AGL.framework" "AGL" find-library subseq? ] unit-test

View File

@ -1,135 +0,0 @@
! Copyright (C) 2013 John Benediktsson
! See http://factorcode.org/license.txt for BSD license
USING: accessors alien.libraries.finder arrays assocs
combinators.short-circuit environment io.files io.files.info
io.pathnames kernel locals make namespaces sequences splitting
system ;
IN: alien.libraries.finder.macosx
<PRIVATE
TUPLE: framework-info location name shortname version suffix ;
: make-framework-info ( filename -- info/f )
[ framework-info new ] dip
"/" split dup [ ".framework" tail? ] find drop [
cut [
[ "/" join ] bi@ [ >>location ] [ >>name ] bi*
] keep [
rest dup ?first "Versions" = [
rest dup empty? [
unclip swap [ >>version ] dip
] unless
] when ?first "_" split1 [ >>shortname ] [ >>suffix ] bi*
] unless-empty
] [ drop ] if* dup shortname>> empty? [ drop f ] when ;
CONSTANT: default-framework-fallback {
"~/Library/Frameworks"
"/Library/Frameworks"
"/Network/Library/Frameworks"
"/System/Library/Frameworks"
}
CONSTANT: default-library-fallback {
"~/lib"
"/usr/local/lib"
"/lib"
"/usr/lib"
}
SYMBOL: dyld-environment
: dyld-env ( name -- seq )
dyld-environment get [ at ] [ os-env ] if* ;
: dyld-paths ( name -- seq )
dyld-env [ ":" split ] [ f ] if* ;
: paths% ( name seq -- )
[ prepend-path , ] with each ;
: dyld-override-search ( name -- seq )
[
dup make-framework-info [
name>> "DYLD_FRAMEWORK_PATH" dyld-paths paths%
] when*
file-name "DYLD_LIBRARY_PATH" dyld-paths paths%
] { } make ;
SYMBOL: dyld-executable-path
: dyld-executable-path-search ( name -- seq )
"@executable_path/" ?head dyld-executable-path get and [
dyld-executable-path get prepend-path
] [
drop f
] if ;
:: dyld-default-search ( name -- seq )
name make-framework-info :> framework
name file-name :> basename
"DYLD_FALLBACK_FRAMEWORK_PATH" dyld-paths :> fallback-framework-path
"DYLD_FALLBACK_LIBRARY_PATH" dyld-paths :> fallback-library-path
[
name ,
framework [
name>> fallback-framework-path paths%
] when*
basename fallback-library-path paths%
framework fallback-framework-path empty? and [
framework name>> default-framework-fallback paths%
] when
fallback-library-path empty? [
basename default-library-fallback paths%
] when
] { } make ;
: dyld-image-suffix-search ( seq -- str )
"DYLD_IMAGE_SUFFIX" dyld-env [
swap [
[
[
".dylib" ?tail [ prepend ] dip
[ ".dylib" append ] when ,
] [
,
] bi
] with each
] { } make
] when* ;
: dyld-search-paths ( name -- paths )
[ dyld-override-search ]
[ dyld-executable-path-search ]
[ dyld-default-search ] tri 3append
dyld-image-suffix-search ;
PRIVATE>
: dyld-find ( name -- path/f )
dyld-search-paths
[ { [ exists? ] [ file-info regular-file? ] } 1&& ] find
[ nip ] when* ;
: framework-find ( name -- path )
dup dyld-find [ nip ] [
".framework" over subseq-start [
dupd head
] [
[ ".framework" append ] keep
] if* file-name append-path dyld-find
] if* ;
M: macosx find-library*
[ "lib" ".dylib" surround ]
[ ".dylib" append ]
[ ".framework/" over 3append ] tri 3array
[ dyld-find ] map-find drop ;

View File

@ -1 +0,0 @@
macosx

View File

@ -1 +0,0 @@
windows

View File

@ -1,34 +0,0 @@
! Copyright (C) 2013 Björn Lindqvist, John Benediktsson
! See http://factorcode.org/license.txt for BSD license
USING: alien.libraries.finder arrays combinators.short-circuit
environment io.backend io.files io.files.info io.pathnames kernel
sequences splitting system system-info.windows ;
IN: alien.libraries.finder.windows
<PRIVATE
: search-paths ( -- seq )
"resource:" normalize-path
system-directory
windows-directory 3array
"PATH" os-env [ ";" split ] [ f ] if* append ;
: candidate-paths ( name -- seq )
search-paths over ".dll" tail? [
[ prepend-path ] with map
] [
[
[ prepend-path ]
[ [ ".dll" append ] [ prepend-path ] bi* ] 2bi
2array
] with map concat
] if ;
PRIVATE>
M: windows find-library*
candidate-paths [
{ [ exists? ] [ file-info regular-file? ] } 1&&
] find nip ;