cmdlinehelper.nim 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #
  2. #
  3. # The Nim Compiler
  4. # (c) Copyright 2018 Nim contributors
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. ## Helpers for binaries that use compiler passes, eg: nim, nimsuggest, nimfix
  10. import
  11. options, idents, nimconf, extccomp, commands, msgs,
  12. lineinfos, modulegraphs, condsyms, os, pathutils, parseopt
  13. from strutils import normalize
  14. proc prependCurDir*(f: AbsoluteFile): AbsoluteFile =
  15. when defined(unix):
  16. if os.isAbsolute(f.string): result = f
  17. else: result = AbsoluteFile("./" & f.string)
  18. else:
  19. result = f
  20. proc addCmdPrefix*(result: var string, kind: CmdLineKind) =
  21. # consider moving this to std/parseopt
  22. case kind
  23. of cmdLongOption: result.add "--"
  24. of cmdShortOption: result.add "-"
  25. of cmdArgument, cmdEnd: discard
  26. type
  27. NimProg* = ref object
  28. suggestMode*: bool
  29. supportsStdinFile*: bool
  30. processCmdLine*: proc(pass: TCmdLinePass, cmd: string; config: ConfigRef)
  31. mainCommand*: proc(graph: ModuleGraph)
  32. proc initDefinesProg*(self: NimProg, conf: ConfigRef, name: string) =
  33. condsyms.initDefines(conf.symbols)
  34. defineSymbol conf.symbols, name
  35. proc processCmdLineAndProjectPath*(self: NimProg, conf: ConfigRef) =
  36. self.processCmdLine(passCmd1, "", conf)
  37. if self.supportsStdinFile and conf.projectName == "-":
  38. handleStdinInput(conf)
  39. elif conf.projectName != "":
  40. try:
  41. conf.projectFull = canonicalizePath(conf, AbsoluteFile conf.projectName)
  42. except OSError:
  43. conf.projectFull = AbsoluteFile conf.projectName
  44. let p = splitFile(conf.projectFull)
  45. let dir = if p.dir.isEmpty: AbsoluteDir getCurrentDir() else: p.dir
  46. conf.projectPath = AbsoluteDir canonicalizePath(conf, AbsoluteFile dir)
  47. conf.projectName = p.name
  48. else:
  49. conf.projectPath = AbsoluteDir canonicalizePath(conf, AbsoluteFile getCurrentDir())
  50. proc loadConfigsAndRunMainCommand*(self: NimProg, cache: IdentCache; conf: ConfigRef): bool =
  51. if self.suggestMode:
  52. conf.command = "nimsuggest"
  53. loadConfigs(DefaultConfig, cache, conf) # load all config files
  54. block:
  55. let scriptFile = conf.projectFull.changeFileExt("nims")
  56. if not self.suggestMode:
  57. # 'nim foo.nims' means to just run the NimScript file and do nothing more:
  58. if fileExists(scriptFile) and scriptFile == conf.projectFull:
  59. if conf.command == "":
  60. conf.command = "e"
  61. return false
  62. elif conf.command.normalize == "e":
  63. return false
  64. # now process command line arguments again, because some options in the
  65. # command line can overwrite the config file's settings
  66. extccomp.initVars(conf)
  67. self.processCmdLine(passCmd2, "", conf)
  68. if conf.command == "":
  69. rawMessage(conf, errGenerated, "command missing")
  70. let graph = newModuleGraph(cache, conf)
  71. graph.suggestMode = self.suggestMode
  72. self.mainCommand(graph)
  73. return true