scriptconfig.nim 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. #
  2. #
  3. # The Nim Compiler
  4. # (c) Copyright 2015 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. ## Implements the new configuration system for Nim. Uses Nim as a scripting
  10. ## language.
  11. import
  12. ast, modules, idents, passes, condsyms,
  13. options, sem, llstream, vm, vmdef, commands,
  14. os, times, osproc, wordrecg, strtabs, modulegraphs,
  15. pathutils
  16. # we support 'cmpIgnoreStyle' natively for efficiency:
  17. from strutils import cmpIgnoreStyle, contains
  18. proc listDirs(a: VmArgs, filter: set[PathComponent]) =
  19. let dir = getString(a, 0)
  20. var result: seq[string] = @[]
  21. for kind, path in walkDir(dir):
  22. if kind in filter: result.add path
  23. setResult(a, result)
  24. proc setupVM*(module: PSym; cache: IdentCache; scriptName: string;
  25. graph: ModuleGraph): PEvalContext =
  26. # For Nimble we need to export 'setupVM'.
  27. result = newCtx(module, cache, graph)
  28. result.mode = emRepl
  29. registerAdditionalOps(result)
  30. let conf = graph.config
  31. # captured vars:
  32. var errorMsg: string
  33. var vthisDir = scriptName.splitFile.dir
  34. template cbconf(name, body) {.dirty.} =
  35. result.registerCallback "stdlib.system." & astToStr(name),
  36. proc (a: VmArgs) =
  37. body
  38. template cbexc(name, exc, body) {.dirty.} =
  39. result.registerCallback "stdlib.system." & astToStr(name),
  40. proc (a: VmArgs) =
  41. errorMsg = ""
  42. try:
  43. body
  44. except exc:
  45. errorMsg = getCurrentExceptionMsg()
  46. template cbos(name, body) {.dirty.} =
  47. cbexc(name, OSError, body)
  48. # Idea: Treat link to file as a file, but ignore link to directory to prevent
  49. # endless recursions out of the box.
  50. cbos listFilesImpl:
  51. listDirs(a, {pcFile, pcLinkToFile})
  52. cbos listDirsImpl:
  53. listDirs(a, {pcDir})
  54. cbos removeDir:
  55. if defined(nimsuggest) or graph.config.cmd == cmdCheck:
  56. discard
  57. else:
  58. os.removeDir(getString(a, 0), getBool(a, 1))
  59. cbos removeFile:
  60. if defined(nimsuggest) or graph.config.cmd == cmdCheck:
  61. discard
  62. else:
  63. os.removeFile getString(a, 0)
  64. cbos createDir:
  65. os.createDir getString(a, 0)
  66. result.registerCallback "stdlib.system.getError",
  67. proc (a: VmArgs) = setResult(a, errorMsg)
  68. cbos setCurrentDir:
  69. os.setCurrentDir getString(a, 0)
  70. cbos getCurrentDir:
  71. setResult(a, os.getCurrentDir())
  72. cbos moveFile:
  73. if defined(nimsuggest) or graph.config.cmd == cmdCheck:
  74. discard
  75. else:
  76. os.moveFile(getString(a, 0), getString(a, 1))
  77. cbos moveDir:
  78. if defined(nimsuggest) or graph.config.cmd == cmdCheck:
  79. discard
  80. else:
  81. os.moveDir(getString(a, 0), getString(a, 1))
  82. cbos copyFile:
  83. if defined(nimsuggest) or graph.config.cmd == cmdCheck:
  84. discard
  85. else:
  86. os.copyFile(getString(a, 0), getString(a, 1))
  87. cbos copyDir:
  88. if defined(nimsuggest) or graph.config.cmd == cmdCheck:
  89. discard
  90. else:
  91. os.copyDir(getString(a, 0), getString(a, 1))
  92. cbos getLastModificationTime:
  93. setResult(a, getLastModificationTime(getString(a, 0)).toUnix)
  94. cbos findExe:
  95. setResult(a, os.findExe(getString(a, 0)))
  96. cbos rawExec:
  97. if defined(nimsuggest) or graph.config.cmd == cmdCheck:
  98. discard
  99. else:
  100. setResult(a, osproc.execCmd getString(a, 0))
  101. cbconf getEnv:
  102. setResult(a, os.getEnv(a.getString 0, a.getString 1))
  103. cbconf existsEnv:
  104. setResult(a, os.existsEnv(a.getString 0))
  105. cbconf putEnv:
  106. os.putEnv(a.getString 0, a.getString 1)
  107. cbconf delEnv:
  108. os.delEnv(a.getString 0)
  109. cbconf dirExists:
  110. setResult(a, os.dirExists(a.getString 0))
  111. cbconf fileExists:
  112. setResult(a, os.fileExists(a.getString 0))
  113. cbconf projectName:
  114. setResult(a, conf.projectName)
  115. cbconf projectDir:
  116. setResult(a, conf.projectPath.string)
  117. cbconf projectPath:
  118. setResult(a, conf.projectFull.string)
  119. cbconf thisDir:
  120. setResult(a, vthisDir)
  121. cbconf put:
  122. options.setConfigVar(conf, getString(a, 0), getString(a, 1))
  123. cbconf get:
  124. setResult(a, options.getConfigVar(conf, a.getString 0))
  125. cbconf exists:
  126. setResult(a, options.existsConfigVar(conf, a.getString 0))
  127. cbconf nimcacheDir:
  128. setResult(a, options.getNimcacheDir(conf).string)
  129. cbconf paramStr:
  130. setResult(a, os.paramStr(int a.getInt 0))
  131. cbconf paramCount:
  132. setResult(a, os.paramCount())
  133. cbconf cmpIgnoreStyle:
  134. setResult(a, strutils.cmpIgnoreStyle(a.getString 0, a.getString 1))
  135. cbconf cmpIgnoreCase:
  136. setResult(a, strutils.cmpIgnoreCase(a.getString 0, a.getString 1))
  137. cbconf setCommand:
  138. conf.command = a.getString 0
  139. let arg = a.getString 1
  140. incl(conf.globalOptions, optWasNimscript)
  141. if arg.len > 0:
  142. conf.projectName = arg
  143. let path =
  144. if conf.projectName.isAbsolute: AbsoluteFile(conf.projectName)
  145. else: conf.projectPath / RelativeFile(conf.projectName)
  146. try:
  147. conf.projectFull = canonicalizePath(conf, path)
  148. except OSError:
  149. conf.projectFull = path
  150. cbconf getCommand:
  151. setResult(a, conf.command)
  152. cbconf switch:
  153. processSwitch(a.getString 0, a.getString 1, passPP, module.info, conf)
  154. cbconf hintImpl:
  155. processSpecificNote(a.getString 0, wHint, passPP, module.info,
  156. a.getString 1, conf)
  157. cbconf warningImpl:
  158. processSpecificNote(a.getString 0, wWarning, passPP, module.info,
  159. a.getString 1, conf)
  160. cbconf patchFile:
  161. let key = a.getString(0) & "_" & a.getString(1)
  162. var val = a.getString(2).addFileExt(NimExt)
  163. if {'$', '~'} in val:
  164. val = pathSubs(conf, val, vthisDir)
  165. elif not isAbsolute(val):
  166. val = vthisDir / val
  167. conf.moduleOverrides[key] = val
  168. cbconf selfExe:
  169. setResult(a, os.getAppFilename())
  170. cbconf cppDefine:
  171. options.cppDefine(conf, a.getString(0))
  172. cbexc stdinReadLine, EOFError:
  173. if defined(nimsuggest) or graph.config.cmd == cmdCheck:
  174. discard
  175. else:
  176. setResult(a, "")
  177. setResult(a, stdin.readLine())
  178. cbexc stdinReadAll, EOFError:
  179. if defined(nimsuggest) or graph.config.cmd == cmdCheck:
  180. discard
  181. else:
  182. setResult(a, "")
  183. setResult(a, stdin.readAll())
  184. proc runNimScript*(cache: IdentCache; scriptName: AbsoluteFile;
  185. freshDefines=true; conf: ConfigRef) =
  186. let oldSymbolFiles = conf.symbolFiles
  187. conf.symbolFiles = disabledSf
  188. let graph = newModuleGraph(cache, conf)
  189. connectCallbacks(graph)
  190. if freshDefines: initDefines(conf.symbols)
  191. defineSymbol(conf.symbols, "nimscript")
  192. defineSymbol(conf.symbols, "nimconfig")
  193. registerPass(graph, semPass)
  194. registerPass(graph, evalPass)
  195. conf.searchPaths.add(conf.libpath)
  196. let oldGlobalOptions = conf.globalOptions
  197. let oldSelectedGC = conf.selectedGC
  198. undefSymbol(conf.symbols, "nimv2")
  199. conf.globalOptions.excl {optTinyRtti, optOwnedRefs, optSeqDestructors}
  200. conf.selectedGC = gcUnselected
  201. var m = graph.makeModule(scriptName)
  202. incl(m.flags, sfMainModule)
  203. graph.vm = setupVM(m, cache, scriptName.string, graph)
  204. graph.compileSystemModule()
  205. discard graph.processModule(m, llStreamOpen(scriptName, fmRead))
  206. # watch out, "newruntime" can be set within NimScript itself and then we need
  207. # to remember this:
  208. if optOwnedRefs in oldGlobalOptions:
  209. conf.globalOptions.incl {optTinyRtti, optOwnedRefs, optSeqDestructors}
  210. defineSymbol(conf.symbols, "nimv2")
  211. if conf.selectedGC == gcUnselected:
  212. conf.selectedGC = oldSelectedGC
  213. # ensure we load 'system.nim' again for the real non-config stuff!
  214. resetSystemArtifacts(graph)
  215. # do not remove the defined symbols
  216. #initDefines()
  217. undefSymbol(conf.symbols, "nimscript")
  218. undefSymbol(conf.symbols, "nimconfig")
  219. conf.symbolFiles = oldSymbolFiles