(define impc:ti:search-for-dylib
(lambda (path)
(let loop ((candidate-paths
(append
(list
path
(sanitize-platform-path (string-append (sys:share-dir) "/libs/aot-cache/" path))
(sanitize-platform-path (string-append (sys:share-dir) "/libs/platform-shlibs/" path)))
(unix-or-Windows (map
(lambda (x)
(sanitize-platform-path
(string-append x "/" path)))
(append (regex:split
(sys:command-output
"echo $LD_LIBRARY_PATH")
":")
'("/usr/local/lib/" "/usr/lib/" "/opt/local/lib/" "/usr/lib/x86_64-linux-gnu")))
(list (sanitize-platform-path (string-append "C:/Windows/System32/" path)))))))
(if (null? candidate-paths)
#f
(let ((dylib (sys:open-dylib (car candidate-paths) #f)))
(if dylib
(cons dylib (car candidate-paths))
(if (file-exists? (car candidate-paths))
;; if sys:open-dylib failed bu the file is there, something's gone wrong
(begin
(print-with-colors *impc:compiler:pretty-print-error-color* 'default #t
(print "Error"))
(print ": could not open ")
(print-with-colors *impc:compiler:pretty-print-type-color* 'default #t (car candidate-paths))
(print " dynamic library\n")
(error ""))
(loop (cdr candidate-paths)))))))))