alien.libraries.finder: linux code path for find-library
/sbin/ldconfig -p is used to figure out what the correct lib name is. It is the same method as used in python ctypes.db4
							parent
							
								
									21bd4cc6ea
								
							
						
					
					
						commit
						0236212987
					
				| 
						 | 
					@ -2,16 +2,23 @@ USING:
 | 
				
			||||||
    alien.libraries
 | 
					    alien.libraries
 | 
				
			||||||
    arrays
 | 
					    arrays
 | 
				
			||||||
    assocs
 | 
					    assocs
 | 
				
			||||||
 | 
					    combinators
 | 
				
			||||||
    formatting
 | 
					    formatting
 | 
				
			||||||
    io.pathnames
 | 
					    io io.encodings.utf8 io.launcher io.pathnames
 | 
				
			||||||
    kernel
 | 
					    kernel
 | 
				
			||||||
    sequences
 | 
					    sequences
 | 
				
			||||||
 | 
					    splitting
 | 
				
			||||||
    system ;
 | 
					    system ;
 | 
				
			||||||
IN: alien.libraries.finder
 | 
					IN: alien.libraries.finder
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					! Util
 | 
				
			||||||
 | 
					: vsprintf1 ( obj fmt -- str )
 | 
				
			||||||
 | 
					    [ 1array ] dip vsprintf ;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
CONSTANT: name-formats {
 | 
					CONSTANT: name-formats {
 | 
				
			||||||
    { windows { "lib%s.dll" "%s.dll" } }
 | 
					    { windows { "lib%s.dll" "%s.dll" } }
 | 
				
			||||||
    { unix { "lib%s.so.0" } }
 | 
					    { linux { "lib%s.so" } }
 | 
				
			||||||
 | 
					    { unix { "lib%s.so" } }
 | 
				
			||||||
    { macosx { "lib%s.0.dylib" } }
 | 
					    { macosx { "lib%s.0.dylib" } }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -24,15 +31,36 @@ CONSTANT: search-paths {
 | 
				
			||||||
    { macosx { "" } }
 | 
					    { macosx { "" } }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
: vsprintf1 ( obj fmt -- str )
 | 
					 | 
				
			||||||
    [ 1array ] dip vsprintf ;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
: path-formats ( -- path-formats )
 | 
					: path-formats ( -- path-formats )
 | 
				
			||||||
    search-paths name-formats [ os of ] bi@
 | 
					    search-paths name-formats [ os of ] bi@
 | 
				
			||||||
    [ append-path ] cartesian-map concat ;
 | 
					    [ append-path ] cartesian-map concat ;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					! Find lib using ldconfig
 | 
				
			||||||
 | 
					CONSTANT: mach-map {
 | 
				
			||||||
 | 
					    { ppc.64 "libc6,64bit" }
 | 
				
			||||||
 | 
					    { x86.32 "libc6,x86-32" }
 | 
				
			||||||
 | 
					    { x86.64 "libc6,x86-64" }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					: ldconfig-cache ( -- seq )
 | 
				
			||||||
 | 
					    "/sbin/ldconfig -p" utf8 [ lines ] with-process-reader rest
 | 
				
			||||||
 | 
					    [ "=>" "" replace "\t " split harvest ] map ;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					: ldconfig-filter ( -- str )
 | 
				
			||||||
 | 
					    mach-map cpu of dup "libc6" ? "(" ")" surround ;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					: ldconfig-matches? ( lib this-lib this-arch -- ? )
 | 
				
			||||||
 | 
					    [ start 0 = ] [ ldconfig-filter = ] bi* and ;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					: ldconfig-find-soname ( lib -- seq )
 | 
				
			||||||
 | 
					    name-formats os of first vsprintf1
 | 
				
			||||||
 | 
					    ldconfig-cache [ first2 ldconfig-matches? ] with filter [ first ] map ;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
: candidate-paths ( name -- paths )
 | 
					: candidate-paths ( name -- paths )
 | 
				
			||||||
    path-formats [ vsprintf1 ] with map ;
 | 
					    {
 | 
				
			||||||
 | 
					        { [ os windows? ] [ path-formats [ vsprintf1 ] with map ] }
 | 
				
			||||||
 | 
					        { [ os linux? ] [ ldconfig-find-soname ] }
 | 
				
			||||||
 | 
					    } cond ;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
: find-library ( name -- path/f )
 | 
					: find-library ( name -- path/f )
 | 
				
			||||||
    candidate-paths [ dlopen dll-valid? ] map-find nip ;
 | 
					    candidate-paths [ dlopen dll-valid? ] map-find nip ;
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue