@@ -15,6 +15,37 @@ import kapsis/runtime
1515import kapsis/ interactive/ prompts
1616import ../ utils
1717
18+
19+ proc detectNimToolchainPath (): string =
20+ # Detect the Nim toolchain path by parsing the output of `nim dump`
21+ let d = execCmdEx (" nim dump" )
22+ if d.exitCode != 0 : return
23+
24+ # parse from bottom; nim prints final lib paths near the end
25+ let lines = d.output.splitLines ()
26+ for i in countdown (lines.high, 0 ):
27+ var p = lines[i].strip ()
28+ if p.len == 0 :
29+ continue
30+
31+ # normalize quoted lines
32+ p = p.strip (chars = {'"' , '\' ' })
33+
34+ # candidate A: line is ".../lib"
35+ if dirExists (p) and p.endsWith (" /lib" ) or p.endsWith (" \\ lib" ):
36+ let root = p.parentDir
37+ if fileExists (root / " lib" / " nimbase.h" ):
38+ return root
39+
40+ if dirExists (p):
41+ let idx1 = p.rfind (" /lib/" )
42+ let idx2 = p.rfind (" \\ lib\\ " )
43+ let idx = max (idx1, idx2)
44+ if idx >= 0 :
45+ let root = p[0 ..< idx].strip ()
46+ if root.len > 0 and fileExists (root / " lib" / " nimbase.h" ):
47+ return root
48+
1849proc getNodeGypConfig (getNimPath: string , release: bool = false ): JsonNode =
1950 return %* {
2051 " target_name" : " main" ,
@@ -98,13 +129,14 @@ proc buildCommand*(v: Values) =
98129 QuitFailure .quit
99130 elif v.has (" --verbose" ):
100131 display (nimCmd.output)
101- var getNimPath = execCmdEx (" choosenim show path" )
102- if getNimPath.exitCode != 0 :
103- displayError (" Can't find Nim path" )
132+ let getNimPath = detectNimToolchainPath ()
133+ if getNimPath.len == 0 :
134+ displayError (" Can't find Nim toolchain path (from `nim`/`nim dump`)" )
135+ QuitFailure .quit
104136 QuitFailure .quit
105137 discard execProcess (" ln" , args = [
106138 " -s" ,
107- strip ( getNimPath.output) / " lib" / " nimbase.h" ,
139+ getNimPath / " lib" / " nimbase.h" ,
108140 cachePathDirectory
109141 ], options= {poStdErrToStdOut, poUsePath})
110142
@@ -143,7 +175,7 @@ proc buildCommand*(v: Values) =
143175 # When using `node-gyp`, we need to generate a `binding.gyp` file with
144176 # the correct configuration
145177 displayInfo (" Building with node-gyp" )
146- var gyp = %* {" targets" : [getNodeGypConfig (getNimPath.output.strip , v.has (" -r" ))]}
178+ var gyp = %* {" targets" : [getNodeGypConfig (getNimPath, v.has (" -r" ))]}
147179 let jsonConfigPath = cachePathDirectory / entryFile.replace (" .nim" , " .json" )
148180
149181 var jarr = newJArray ()
0 commit comments