]> gitweb.factorcode.org Git - factor.git/blobdiff - basis/alien/libraries/finder/windows/windows.factor
alien.c-types: not necessary to import `short` differently anymore
[factor.git] / basis / alien / libraries / finder / windows / windows.factor
index cef5b54a05e13eca421d70c2902f5356e7648770..8272bd7737abd9d9883cf33733d12230280451a6 100644 (file)
@@ -1,22 +1,47 @@
-! Copyright (C) 2013 Björn Lindqvist
+! Copyright (C) 2013 Björn Lindqvist, John Benediktsson
 ! See http://factorcode.org/license.txt for BSD license
 
-USING: alien.libraries alien.libraries.finder arrays combinators
-kernel sequences system ;
-
+USING: alien.c-types alien.data alien.libraries.finder
+alien.strings arrays combinators.short-circuit environment
+io.backend io.files io.files.info io.pathnames kernel sequences
+specialized-arrays splitting system system-info.windows
+windows.kernel32 ;
+SPECIALIZED-ARRAY: ushort
 IN: alien.libraries.finder.windows
 
 <PRIVATE
 
-: candidate-paths ( name -- paths )
-    {
-        [ ".dll" append ]
-        [ "lib" ".dll" surround ]
-        [ "dlls/" ".dll" surround ]
-        [ "dlls/lib" ".dll" surround ]
-    } cleave 4array ;
+: 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 ;
+
+: find-library-paths ( name -- path/f )
+    candidate-paths [
+        { [ file-exists? ] [ file-info regular-file? ] } 1&&
+    ] find nip ;
+
+: find-library-file ( name -- path/f )
+    f DONT_RESOLVE_DLL_REFERENCES LoadLibraryEx [
+        [
+            32768 ushort (c-array) [ 32768 GetModuleFileName drop ] keep
+            alien>native-string
+        ] [ FreeLibrary drop ] bi
+    ] [ f ] if* ;
 
 PRIVATE>
 
-M: windows find-library
-    candidate-paths [ dlopen dll-valid? ] map-find nip ;
+M: windows find-library*
+    { [ find-library-paths ] [ find-library-file ] } 1|| ;