diff options
Diffstat (limited to 'compiler/cmdlinehelper.nim')
-rw-r--r-- | compiler/cmdlinehelper.nim | 113 |
1 files changed, 46 insertions, 67 deletions
diff --git a/compiler/cmdlinehelper.nim b/compiler/cmdlinehelper.nim index a50d47ad2..e51248639 100644 --- a/compiler/cmdlinehelper.nim +++ b/compiler/cmdlinehelper.nim @@ -7,20 +7,33 @@ # distribution, for details about the copyright. # -## Helpers for binaries that use compiler passes, eg: nim, nimsuggest, nimfix +## Helpers for binaries that use compiler passes, e.g.: nim, nimsuggest import - options, idents, nimconf, scriptconfig, extccomp, commands, msgs, - lineinfos, modulegraphs, condsyms, os, pathutils + options, idents, nimconf, extccomp, commands, msgs, + lineinfos, modulegraphs, condsyms, pathutils -from strutils import normalize +import std/[os, parseopt] + +proc prependCurDir*(f: AbsoluteFile): AbsoluteFile = + when defined(unix): + if os.isAbsolute(f.string): result = f + else: result = AbsoluteFile("./" & f.string) + else: + result = f + +proc addCmdPrefix*(result: var string, kind: CmdLineKind) = + # consider moving this to std/parseopt + case kind + of cmdLongOption: result.add "--" + of cmdShortOption: result.add "-" + of cmdArgument, cmdEnd: discard type NimProg* = ref object suggestMode*: bool supportsStdinFile*: bool processCmdLine*: proc(pass: TCmdLinePass, cmd: string; config: ConfigRef) - mainCommand*: proc(graph: ModuleGraph) proc initDefinesProg*(self: NimProg, conf: ConfigRef, name: string) = condsyms.initDefines(conf.symbols) @@ -28,79 +41,45 @@ proc initDefinesProg*(self: NimProg, conf: ConfigRef, name: string) = proc processCmdLineAndProjectPath*(self: NimProg, conf: ConfigRef) = self.processCmdLine(passCmd1, "", conf) - if self.supportsStdinFile and conf.projectName == "-": - conf.projectName = "stdinfile" - conf.projectFull = AbsoluteFile "stdinfile" - conf.projectPath = AbsoluteDir getCurrentDir() - conf.projectIsStdin = true + if conf.projectIsCmd and conf.projectName in ["-", ""]: + handleCmdInput(conf) + elif self.supportsStdinFile and conf.projectName == "-": + handleStdinInput(conf) elif conf.projectName != "": - try: - conf.projectFull = canonicalizePath(conf, AbsoluteFile conf.projectName) - except OSError: - conf.projectFull = AbsoluteFile conf.projectName - let p = splitFile(conf.projectFull) - let dir = if p.dir.isEmpty: AbsoluteDir getCurrentDir() else: p.dir - conf.projectPath = AbsoluteDir canonicalizePath(conf, AbsoluteFile dir) - conf.projectName = p.name + setFromProjectName(conf, conf.projectName) else: conf.projectPath = AbsoluteDir canonicalizePath(conf, AbsoluteFile getCurrentDir()) -proc loadConfigsAndRunMainCommand*(self: NimProg, cache: IdentCache; conf: ConfigRef): bool = - loadConfigs(DefaultConfig, cache, conf) # load all config files +proc loadConfigsAndProcessCmdLine*(self: NimProg, cache: IdentCache; conf: ConfigRef; + graph: ModuleGraph): bool = if self.suggestMode: - conf.command = "nimsuggest" - - template runNimScriptIfExists(path: AbsoluteFile) = - let p = path # eval once - if fileExists(p): - runNimScript(cache, p, freshDefines = false, conf) - - # Caution: make sure this stays in sync with `loadConfigs` - if optSkipSystemConfigFile notin conf.globalOptions: - runNimScriptIfExists(getSystemConfigPath(conf, DefaultConfigNims)) - - if optSkipUserConfigFile notin conf.globalOptions: - runNimScriptIfExists(getUserConfigPath(DefaultConfigNims)) + conf.setCmd cmdIdeTools + if conf.cmd == cmdNimscript: + incl(conf.globalOptions, optWasNimscript) + loadConfigs(DefaultConfig, cache, conf, graph.idgen) # load all config files + # restores `conf.notes` after loading config files + # because it has overwrites the notes when compiling the system module which + # is a foreign module compared to the project + if conf.cmd in cmdBackends: + conf.notes = conf.mainPackageNotes - if optSkipParentConfigFiles notin conf.globalOptions: - for dir in parentDirs(conf.projectPath.string, fromRoot = true, inclusive = false): - runNimScriptIfExists(AbsoluteDir(dir) / DefaultConfigNims) - - if optSkipProjConfigFile notin conf.globalOptions: - runNimScriptIfExists(conf.projectPath / DefaultConfigNims) - block: + if not self.suggestMode: let scriptFile = conf.projectFull.changeFileExt("nims") - if not self.suggestMode: - runNimScriptIfExists(scriptFile) - # 'nim foo.nims' means to just run the NimScript file and do nothing more: - if fileExists(scriptFile) and scriptFile == conf.projectFull: - if conf.command == "": - conf.command = "e" - return false - elif conf.command.normalize == "e": - return false - else: - if scriptFile != conf.projectFull: - runNimScriptIfExists(scriptFile) - else: - # 'nimsuggest foo.nims' means to just auto-complete the NimScript file - discard - + # 'nim foo.nims' means to just run the NimScript file and do nothing more: + if fileExists(scriptFile) and scriptFile == conf.projectFull: + if conf.cmd == cmdNone: conf.setCmd cmdNimscript + if conf.cmd == cmdNimscript: return false # now process command line arguments again, because some options in the # command line can overwrite the config file's settings - extccomp.initVars(conf) - # XXX This is hacky. We need to find a better way. - case conf.command - of "cpp", "compiletocpp": - conf.cmd = cmdCompileToCpp - else: - discard - + if conf.backend != backendJs: # bug #19059 + extccomp.initVars(conf) self.processCmdLine(passCmd2, "", conf) - if conf.command == "": + if conf.cmd == cmdNone: rawMessage(conf, errGenerated, "command missing") - let graph = newModuleGraph(cache, conf) graph.suggestMode = self.suggestMode - self.mainCommand(graph) return true + +proc loadConfigsAndRunMainCommand*(self: NimProg, cache: IdentCache; conf: ConfigRef; graph: ModuleGraph): bool = + ## Alias for loadConfigsAndProcessCmdLine, here for backwards compatibility + loadConfigsAndProcessCmdLine(self, cache, conf, graph) |