cmdlinehelper.nim 3.1 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, e.g.: nim, nimsuggest
  10. import
  11. options, idents, nimconf, extccomp, commands, msgs,
  12. lineinfos, modulegraphs, condsyms, pathutils
  13. import std/[os, parseopt]
  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. proc initDefinesProg*(self: NimProg, conf: ConfigRef, name: string) =
  32. condsyms.initDefines(conf.symbols)
  33. defineSymbol conf.symbols, name
  34. proc processCmdLineAndProjectPath*(self: NimProg, conf: ConfigRef) =
  35. self.processCmdLine(passCmd1, "", conf)
  36. if conf.projectIsCmd and conf.projectName in ["-", ""]:
  37. handleCmdInput(conf)
  38. elif self.supportsStdinFile and conf.projectName == "-":
  39. handleStdinInput(conf)
  40. elif conf.projectName != "":
  41. setFromProjectName(conf, conf.projectName)
  42. else:
  43. conf.projectPath = AbsoluteDir canonicalizePath(conf, AbsoluteFile getCurrentDir())
  44. proc loadConfigsAndProcessCmdLine*(self: NimProg, cache: IdentCache; conf: ConfigRef;
  45. graph: ModuleGraph): bool =
  46. if self.suggestMode:
  47. conf.setCmd cmdIdeTools
  48. if conf.cmd == cmdNimscript:
  49. incl(conf.globalOptions, optWasNimscript)
  50. loadConfigs(DefaultConfig, cache, conf, graph.idgen) # load all config files
  51. # restores `conf.notes` after loading config files
  52. # because it has overwrites the notes when compiling the system module which
  53. # is a foreign module compared to the project
  54. if conf.cmd in cmdBackends:
  55. conf.notes = conf.mainPackageNotes
  56. if not self.suggestMode:
  57. let scriptFile = conf.projectFull.changeFileExt("nims")
  58. # 'nim foo.nims' means to just run the NimScript file and do nothing more:
  59. if fileExists(scriptFile) and scriptFile == conf.projectFull:
  60. if conf.cmd == cmdNone: conf.setCmd cmdNimscript
  61. if conf.cmd == cmdNimscript: return false
  62. # now process command line arguments again, because some options in the
  63. # command line can overwrite the config file's settings
  64. if conf.backend != backendJs: # bug #19059
  65. extccomp.initVars(conf)
  66. self.processCmdLine(passCmd2, "", conf)
  67. if conf.cmd == cmdNone:
  68. rawMessage(conf, errGenerated, "command missing")
  69. graph.suggestMode = self.suggestMode
  70. return true
  71. proc loadConfigsAndRunMainCommand*(self: NimProg, cache: IdentCache; conf: ConfigRef; graph: ModuleGraph): bool =
  72. ## Alias for loadConfigsAndProcessCmdLine, here for backwards compatibility
  73. loadConfigsAndProcessCmdLine(self, cache, conf, graph)