Merge pull request #4493 from oderwat/findexe-symlinks
findExe() now by default follows symlinks.
This commit is contained in:
commit
ec0de31751
2 changed files with 27 additions and 5 deletions
|
|
@ -212,9 +212,7 @@ proc setDefaultLibpath*() =
|
||||||
|
|
||||||
# Special rule to support other tools (nimble) which import the compiler
|
# Special rule to support other tools (nimble) which import the compiler
|
||||||
# modules and make use of them.
|
# modules and make use of them.
|
||||||
let realNimPath = # Make sure we expand the symlink
|
let realNimPath = findExe("nim")
|
||||||
if symlinkExists(findExe("nim")): expandSymlink(findExe("nim"))
|
|
||||||
else: findExe("nim")
|
|
||||||
# Find out if $nim/../../lib/system.nim exists.
|
# Find out if $nim/../../lib/system.nim exists.
|
||||||
let parentNimLibPath = realNimPath.parentDir().parentDir() / "lib"
|
let parentNimLibPath = realNimPath.parentDir().parentDir() / "lib"
|
||||||
if not fileExists(libpath / "system.nim") and
|
if not fileExists(libpath / "system.nim") and
|
||||||
|
|
|
||||||
|
|
@ -556,12 +556,21 @@ when declared(getEnv) or defined(nimscript):
|
||||||
yield substr(s, first, last-1)
|
yield substr(s, first, last-1)
|
||||||
inc(last)
|
inc(last)
|
||||||
|
|
||||||
proc findExe*(exe: string): string {.
|
when not defined(windows) and declared(os):
|
||||||
|
proc checkSymlink(path: string): bool =
|
||||||
|
var rawInfo: Stat
|
||||||
|
if lstat(path, rawInfo) < 0'i32:
|
||||||
|
raiseOSError(osLastError())
|
||||||
|
S_ISLNK(rawInfo.st_mode)
|
||||||
|
|
||||||
|
proc findExe*(exe: string, followSymlinks: bool = true): string {.
|
||||||
tags: [ReadDirEffect, ReadEnvEffect, ReadIOEffect].} =
|
tags: [ReadDirEffect, ReadEnvEffect, ReadIOEffect].} =
|
||||||
## Searches for `exe` in the current working directory and then
|
## Searches for `exe` in the current working directory and then
|
||||||
## in directories listed in the ``PATH`` environment variable.
|
## in directories listed in the ``PATH`` environment variable.
|
||||||
## Returns "" if the `exe` cannot be found. On DOS-like platforms, `exe`
|
## Returns "" if the `exe` cannot be found. On DOS-like platforms, `exe`
|
||||||
## is added the `ExeExt <#ExeExt>`_ file extension if it has none.
|
## is added the `ExeExt <#ExeExt>`_ file extension if it has none.
|
||||||
|
## If the system supports symlinks it also resolves them until it
|
||||||
|
## meets the actual file. This behavior can be disabled if desired.
|
||||||
result = addFileExt(exe, ExeExt)
|
result = addFileExt(exe, ExeExt)
|
||||||
if existsFile(result): return
|
if existsFile(result): return
|
||||||
var path = string(getEnv("PATH"))
|
var path = string(getEnv("PATH"))
|
||||||
|
|
@ -572,7 +581,22 @@ when declared(getEnv) or defined(nimscript):
|
||||||
result
|
result
|
||||||
else:
|
else:
|
||||||
var x = expandTilde(candidate) / result
|
var x = expandTilde(candidate) / result
|
||||||
if existsFile(x): return x
|
if existsFile(x):
|
||||||
|
when not defined(windows) and declared(os):
|
||||||
|
while followSymlinks: # doubles as if here
|
||||||
|
if x.checkSymlink:
|
||||||
|
var r = newString(256)
|
||||||
|
var len = readlink(x, r, 256)
|
||||||
|
if len < 0:
|
||||||
|
raiseOSError(osLastError())
|
||||||
|
if len > 256:
|
||||||
|
r = newString(len+1)
|
||||||
|
len = readlink(x, r, len)
|
||||||
|
setLen(r, len)
|
||||||
|
x = r
|
||||||
|
else:
|
||||||
|
break
|
||||||
|
return x
|
||||||
result = ""
|
result = ""
|
||||||
|
|
||||||
when defined(nimscript) or (defined(nimdoc) and not declared(os)):
|
when defined(nimscript) or (defined(nimdoc) and not declared(os)):
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue