move more global variables into ConfigRef

This commit is contained in:
Andreas Rumpf
2018-05-11 15:28:26 +02:00
parent a8a544a8f7
commit 3c4ceea427
11 changed files with 109 additions and 101 deletions

View File

@@ -287,7 +287,7 @@ proc processPath(conf: ConfigRef; path: string, info: TLineInfo,
elif notRelativeToProj:
getCurrentDir() / path
else:
options.gProjectPath / path
conf.projectPath / path
try:
result = pathSubs(conf, p, info.toFullPath().splitFile().dir)
except ValueError:
@@ -366,22 +366,22 @@ proc processSwitch*(switch, arg: string, pass: TCmdLinePass, info: TLineInfo;
expectArg(conf, switch, arg, pass, info)
let path = processPath(conf, arg, info)
options.searchPaths.keepItIf(cmpPaths(it, path) != 0)
options.lazyPaths.keepItIf(cmpPaths(it, path) != 0)
conf.searchPaths.keepItIf(cmpPaths(it, path) != 0)
conf.lazyPaths.keepItIf(cmpPaths(it, path) != 0)
if (len(path) > 0) and (path[len(path) - 1] == DirSep):
let strippedPath = path[0 .. (len(path) - 2)]
options.searchPaths.keepItIf(cmpPaths(it, strippedPath) != 0)
options.lazyPaths.keepItIf(cmpPaths(it, strippedPath) != 0)
conf.searchPaths.keepItIf(cmpPaths(it, strippedPath) != 0)
conf.lazyPaths.keepItIf(cmpPaths(it, strippedPath) != 0)
of "nimcache":
expectArg(conf, switch, arg, pass, info)
options.nimcacheDir = processPath(conf, arg, info, true)
conf.nimcacheDir = processPath(conf, arg, info, true)
of "out", "o":
expectArg(conf, switch, arg, pass, info)
options.outFile = arg
conf.outFile = arg
of "docseesrcurl":
expectArg(conf, switch, arg, pass, info)
options.docSeeSrcUrl = arg
conf.docSeeSrcUrl = arg
of "mainmodule", "m":
discard "allow for backwards compatibility, but don't do anything"
of "define", "d":
@@ -579,10 +579,10 @@ proc processSwitch*(switch, arg: string, pass: TCmdLinePass, info: TLineInfo;
processOnOffSwitchG(conf, {optGenIndex}, arg, pass, info)
of "import":
expectArg(conf, switch, arg, pass, info)
if pass in {passCmd2, passPP}: implicitImports.add arg
if pass in {passCmd2, passPP}: conf.implicitImports.add arg
of "include":
expectArg(conf, switch, arg, pass, info)
if pass in {passCmd2, passPP}: implicitIncludes.add arg
if pass in {passCmd2, passPP}: conf.implicitIncludes.add arg
of "listcmd":
expectNoArg(conf, switch, arg, pass, info)
incl(gGlobalOptions, optListCmd)
@@ -655,7 +655,7 @@ proc processSwitch*(switch, arg: string, pass: TCmdLinePass, info: TLineInfo;
of "colors": processOnOffSwitchG(conf, {optUseColors}, arg, pass, info)
of "lib":
expectArg(conf, switch, arg, pass, info)
libpath = processPath(conf, arg, info, notRelativeToProj=true)
conf.libpath = processPath(conf, arg, info, notRelativeToProj=true)
of "putenv":
expectArg(conf, switch, arg, pass, info)
splitSwitch(conf, arg, key, val, pass, info)
@@ -751,17 +751,17 @@ proc processArgument*(pass: TCmdLinePass; p: OptParser;
if argsCount == 0:
# nim filename.nims is the same as "nim e filename.nims":
if p.key.endswith(".nims"):
options.command = "e"
options.gProjectName = unixToNativePath(p.key)
config.command = "e"
config.projectName = unixToNativePath(p.key)
config.arguments = cmdLineRest(p)
result = true
elif pass != passCmd2:
options.command = p.key
config.command = p.key
else:
if pass == passCmd1: options.commandArgs.add p.key
if pass == passCmd1: config.commandArgs.add p.key
if argsCount == 1:
# support UNIX style filenames everywhere for portable build scripts:
options.gProjectName = unixToNativePath(p.key)
config.projectName = unixToNativePath(p.key)
config.arguments = cmdLineRest(p)
result = true
inc argsCount

View File

@@ -474,7 +474,7 @@ proc generateScript(conf: ConfigRef; projectFile: string, script: Rope) =
let (dir, name, ext) = splitFile(projectFile)
writeRope(script, getNimcacheDir(conf) / addFileExt("compile_" & name,
platform.OS[targetOS].scriptExt))
copyFile(libpath / "nimbase.h", getNimcacheDir(conf) / "nimbase.h")
copyFile(conf.libpath / "nimbase.h", getNimcacheDir(conf) / "nimbase.h")
proc getOptSpeed(conf: ConfigRef; c: TSystemCC): string =
result = getConfigVar(conf, c, ".options.speed")
@@ -563,7 +563,7 @@ proc getCompileCFileCmd*(conf: ConfigRef; cfile: Cfile): string =
var includeCmd, compilePattern: string
if not noAbsolutePaths(conf):
# compute include paths:
includeCmd = CC[c].includeCmd & quoteShell(libpath)
includeCmd = CC[c].includeCmd & quoteShell(conf.libpath)
for includeDir in items(cIncludes):
includeCmd.add(join([CC[c].includeCmd, includeDir.quoteShell]))
@@ -592,14 +592,14 @@ proc getCompileCFileCmd*(conf: ConfigRef; cfile: Cfile): string =
result = quoteShell(compilePattern % [
"file", cf, "objfile", objfile, "options", options,
"include", includeCmd, "nim", getPrefixDir(conf),
"nim", getPrefixDir(conf), "lib", libpath])
"nim", getPrefixDir(conf), "lib", conf.libpath])
add(result, ' ')
addf(result, CC[c].compileTmpl, [
"file", cf, "objfile", objfile,
"options", options, "include", includeCmd,
"nim", quoteShell(getPrefixDir(conf)),
"nim", quoteShell(getPrefixDir(conf)),
"lib", quoteShell(libpath)])
"lib", quoteShell(conf.libpath)])
proc footprint(conf: ConfigRef; cfile: Cfile): SecureHash =
result = secureHash(
@@ -655,12 +655,12 @@ proc compileCFile(conf: ConfigRef; list: CFileList, script: var Rope, cmds: var
proc getLinkCmd(conf: ConfigRef; projectfile, objfiles: string): string =
if optGenStaticLib in gGlobalOptions:
var libname: string
if options.outFile.len > 0:
libname = options.outFile.expandTilde
if conf.outFile.len > 0:
libname = conf.outFile.expandTilde
if not libname.isAbsolute():
libname = getCurrentDir() / libname
else:
libname = (libNameTmpl() % splitFile(gProjectName).name)
libname = (libNameTmpl() % splitFile(conf.projectName).name)
result = CC[cCompiler].buildLib % ["libfile", libname,
"objfiles", objfiles]
else:
@@ -679,8 +679,8 @@ proc getLinkCmd(conf: ConfigRef; projectfile, objfiles: string): string =
else:
exefile = splitFile(projectfile).name & platform.OS[targetOS].exeExt
builddll = ""
if options.outFile.len > 0:
exefile = options.outFile.expandTilde
if conf.outFile.len > 0:
exefile = conf.outFile.expandTilde
if not exefile.isAbsolute():
exefile = getCurrentDir() / exefile
if not noAbsolutePaths(conf):
@@ -697,13 +697,13 @@ proc getLinkCmd(conf: ConfigRef; projectfile, objfiles: string): string =
linkTmpl = CC[cCompiler].linkTmpl
result = quoteShell(result % ["builddll", builddll,
"buildgui", buildgui, "options", linkOptions, "objfiles", objfiles,
"exefile", exefile, "nim", getPrefixDir(conf), "lib", libpath])
"exefile", exefile, "nim", getPrefixDir(conf), "lib", conf.libpath])
result.add ' '
addf(result, linkTmpl, ["builddll", builddll,
"buildgui", buildgui, "options", linkOptions,
"objfiles", objfiles, "exefile", exefile,
"nim", quoteShell(getPrefixDir(conf)),
"lib", quoteShell(libpath)])
"lib", quoteShell(conf.libpath)])
template tryExceptOSErrorMessage(conf: ConfigRef; errorPrefix: string = "", body: untyped): typed =
try:
@@ -903,7 +903,7 @@ proc writeMapping*(conf: ConfigRef; symbolMapping: Rope) =
getConfigVar(conf, cCompiler, ".options.linker")))
add(code, "\n[Environment]\nlibpath=")
add(code, strutils.escape(libpath))
add(code, strutils.escape(conf.libpath))
addf(code, "\n[Symbols]$n$1", [symbolMapping])
writeRope(code, joinPath(gProjectPath, "mapping.txt"))
writeRope(code, joinPath(conf.projectPath, "mapping.txt"))

View File

@@ -152,7 +152,7 @@ proc getModuleName*(conf: ConfigRef; n: PNode): string =
let x = addFileExt(a, "nim")
if fileExists(x): return x
for candidate in stdlibDirs:
attempt(options.libpath / candidate / modname)
attempt(conf.libpath / candidate / modname)
# hacky way to implement 'x / y /../ z':
result = getModuleName(conf, n1)

View File

@@ -400,7 +400,7 @@ proc quit(conf: ConfigRef; msg: TMsgKind) =
else:
styledMsgWriteln(fgRed, "No stack traceback available\n" &
"To create a stacktrace, rerun compilation with ./koch temp " &
options.command & " <file>")
conf.command & " <file>")
quit 1
proc handleError(conf: ConfigRef; msg: TMsgKind, eh: TErrorHandling, s: string) =

View File

@@ -13,8 +13,8 @@ import parseutils, strutils, strtabs, os, options, msgs, sequtils,
configuration
proc addPath*(conf: ConfigRef; path: string, info: TLineInfo) =
if not options.searchPaths.contains(path):
options.searchPaths.insert(path, 0)
if not conf.searchPaths.contains(path):
conf.searchPaths.insert(path, 0)
type
Version* = distinct string
@@ -112,9 +112,9 @@ proc addNimblePath(conf: ConfigRef; p: string, info: TLineInfo) =
if not path.isAbsolute():
path = p / path
if not contains(options.searchPaths, path):
if not contains(conf.searchPaths, path):
message(conf, info, hintPath, path)
options.lazyPaths.insert(path, 0)
conf.lazyPaths.insert(path, 0)
proc addPathRec(conf: ConfigRef; dir: string, info: TLineInfo) =
var packages = newStringTable(modeStyleInsensitive)

View File

@@ -239,7 +239,7 @@ proc loadConfigs*(cfg: string; cache: IdentCache; config: ConfigRef = nil) =
if optSkipUserConfigFile notin gGlobalOptions:
readConfigFile(getUserConfigPath(cfg), cache, config)
let pd = if gProjectPath.len > 0: gProjectPath else: getCurrentDir()
let pd = if config.projectPath.len > 0: config.projectPath else: getCurrentDir()
if optSkipParentConfigFiles notin gGlobalOptions:
for dir in parentDirs(pd, fromRoot=true, inclusive=false):
readConfigFile(dir / cfg, cache, config)
@@ -247,13 +247,13 @@ proc loadConfigs*(cfg: string; cache: IdentCache; config: ConfigRef = nil) =
if optSkipProjConfigFile notin gGlobalOptions:
readConfigFile(pd / cfg, cache, config)
if gProjectName.len != 0:
if config.projectName.len != 0:
# new project wide config file:
var projectConfig = changeFileExt(gProjectFull, "nimcfg")
var projectConfig = changeFileExt(config.projectFull, "nimcfg")
if not fileExists(projectConfig):
projectConfig = changeFileExt(gProjectFull, "nim.cfg")
projectConfig = changeFileExt(config.projectFull, "nim.cfg")
readConfigFile(projectConfig, cache, config)
proc loadConfigs*(cfg: string; config: ConfigRef = nil) =
proc loadConfigs*(cfg: string; config: ConfigRef) =
# for backwards compatibility only.
loadConfigs(cfg, newIdentCache(), config)

View File

@@ -29,7 +29,7 @@ proc overwriteFiles*(conf: ConfigRef) =
let doStrip = options.getConfigVar(conf, "pretty.strip").normalize == "on"
for i in 0 .. high(gSourceFiles):
if gSourceFiles[i].dirty and not gSourceFiles[i].isNimfixFile and
(not gOnlyMainfile or gSourceFiles[i].fileIdx == gProjectMainIdx.FileIndex):
(not gOnlyMainfile or gSourceFiles[i].fileIdx == conf.projectMainIdx.FileIndex):
let newFile = if gOverWrite: gSourceFiles[i].fullpath
else: gSourceFiles[i].fullpath.changeFileExt(".pretty.nim")
try:

View File

@@ -135,6 +135,22 @@ type
## symbols are always guaranteed to be style
## insensitive. Otherwise hell would break lose.
packageCache*: StringTableRef
searchPaths*: seq[string]
lazyPaths*: seq[string]
outFile*, prefixDir*, libpath*, nimcacheDir*: string
dllOverrides, moduleOverrides*: StringTableRef
projectName*: string # holds a name like 'nim'
projectPath*: string # holds a path like /home/alice/projects/nim/compiler/
projectFull*: string # projectPath/projectName
projectIsStdin*: bool # whether we're compiling from stdin
projectMainIdx*: int32 # the canonical path id of the main module
command*: string # the main command (e.g. cc, check, scan, etc)
commandArgs*: seq[string] # any arguments after the main command
keepComments*: bool # whether the parser needs to keep comments
implicitImports*: seq[string] # modules that are to be implicitly imported
implicitIncludes*: seq[string] # modules that are to be implicitly included
docSeeSrcUrl*: string # if empty, no seeSrc will be generated. \
# The string uses the formatting variables `path` and `line`.
const oldExperimentalFeatures* = {implicitDeref, dotOperators, callOperator, parallel}
@@ -151,7 +167,24 @@ proc newConfigRef*(): ConfigRef =
notes: NotesVerbosity[1], mainPackageNotes: NotesVerbosity[1],
configVars: newStringTable(modeStyleInsensitive),
symbols: newStringTable(modeStyleInsensitive),
packageCache: newPackageCache())
packageCache: newPackageCache(),
searchPaths: @[],
lazyPaths: @[],
outFile: "", prefixDir: "", libpath: "", nimcacheDir: "",
dllOverrides: newStringTable(modeCaseInsensitive),
moduleOverrides: newStringTable(modeStyleInsensitive),
projectName: "", # holds a name like 'nim'
projectPath: "", # holds a path like /home/alice/projects/nim/compiler/
projectFull: "", # projectPath/projectName
projectIsStdin: false, # whether we're compiling from stdin
projectMainIdx: 0'i32, # the canonical path id of the main module
command: "", # the main command (e.g. cc, check, scan, etc)
commandArgs: @[], # any arguments after the main command
keepComments: true, # whether the parser needs to keep comments
implicitImports: @[], # modules that are to be implicitly imported
implicitIncludes: @[], # modules that are to be implicitly included
docSeeSrcUrl: ""
)
proc newPartialConfigRef*(): ConfigRef =
## create a new ConfigRef that is only good enough for error reporting.
@@ -215,11 +248,6 @@ var
gExitcode*: int8
gCmd*: TCommands = cmdNone # the command
gSelectedGC* = gcRefc # the selected GC
searchPaths*: seq[string] = @[]
lazyPaths*: seq[string] = @[]
outFile*: string = ""
docSeeSrcUrl*: string = "" # if empty, no seeSrc will be generated. \
# The string uses the formatting variables `path` and `line`.
#headerFile*: string = ""
gVerbosity* = 1 # how verbose the compiler is
gNumberOfProcessors*: int # number of processors
@@ -262,24 +290,6 @@ const
DocConfig* = "nimdoc.cfg"
DocTexConfig* = "nimdoc.tex.cfg"
# additional configuration variables:
var
gDllOverrides = newStringTable(modeCaseInsensitive)
gModuleOverrides* = newStringTable(modeStyleInsensitive)
gPrefixDir* = "" # Overrides the default prefix dir in getPrefixDir proc.
libpath* = ""
gProjectName* = "" # holds a name like 'nim'
gProjectPath* = "" # holds a path like /home/alice/projects/nim/compiler/
gProjectFull* = "" # projectPath/projectName
gProjectIsStdin* = false # whether we're compiling from stdin
gProjectMainIdx*: int32 # the canonical path id of the main module
nimcacheDir* = ""
command* = "" # the main command (e.g. cc, check, scan, etc)
commandArgs*: seq[string] = @[] # any arguments after the main command
gKeepComments*: bool = true # whether the parser needs to keep comments
implicitImports*: seq[string] = @[] # modules that are to be implicitly imported
implicitIncludes*: seq[string] = @[] # modules that are to be implicitly included
const oKeepVariableNames* = true
template compilingLib*(conf: ConfigRef): bool =
@@ -289,10 +299,10 @@ proc mainCommandArg*(conf: ConfigRef): string =
## This is intended for commands like check or parse
## which will work on the main project file unless
## explicitly given a specific file argument
if commandArgs.len > 0:
result = commandArgs[0]
if conf.commandArgs.len > 0:
result = conf.commandArgs[0]
else:
result = gProjectName
result = conf.projectName
proc existsConfigVar*(conf: ConfigRef; key: string): bool =
result = hasKey(conf.configVars, key)
@@ -304,37 +314,36 @@ proc setConfigVar*(conf: ConfigRef; key, val: string) =
conf.configVars[key] = val
proc getOutFile*(conf: ConfigRef; filename, ext: string): string =
if options.outFile != "": result = options.outFile
if conf.outFile != "": result = conf.outFile
else: result = changeFileExt(filename, ext)
proc getPrefixDir*(conf: ConfigRef): string =
## Gets the prefix dir, usually the parent directory where the binary resides.
##
## This is overridden by some tools (namely nimsuggest) via the ``gPrefixDir``
## This is overridden by some tools (namely nimsuggest) via the ``conf.prefixDir``
## global.
if gPrefixDir != "": result = gPrefixDir
else:
result = splitPath(getAppDir()).head
if conf.prefixDir != "": result = conf.prefixDir
else: result = splitPath(getAppDir()).head
proc setDefaultLibpath*(conf: ConfigRef) =
# set default value (can be overwritten):
if libpath == "":
if conf.libpath == "":
# choose default libpath:
var prefix = getPrefixDir(conf)
when defined(posix):
if prefix == "/usr": libpath = "/usr/lib/nim"
elif prefix == "/usr/local": libpath = "/usr/local/lib/nim"
else: libpath = joinPath(prefix, "lib")
else: libpath = joinPath(prefix, "lib")
if prefix == "/usr": conf.libpath = "/usr/lib/nim"
elif prefix == "/usr/local": conf.libpath = "/usr/local/lib/nim"
else: conf.libpath = joinPath(prefix, "lib")
else: conf.libpath = joinPath(prefix, "lib")
# Special rule to support other tools (nimble) which import the compiler
# modules and make use of them.
let realNimPath = findExe("nim")
# Find out if $nim/../../lib/system.nim exists.
let parentNimLibPath = realNimPath.parentDir.parentDir / "lib"
if not fileExists(libpath / "system.nim") and
if not fileExists(conf.libpath / "system.nim") and
fileExists(parentNimlibPath / "system.nim"):
libpath = parentNimLibPath
conf.libpath = parentNimLibPath
proc canonicalizePath*(conf: ConfigRef; path: string): string =
# on Windows, 'expandFilename' calls getFullPathName which doesn't do
@@ -350,7 +359,7 @@ proc canonicalizePath*(conf: ConfigRef; path: string): string =
proc shortenDir*(conf: ConfigRef; dir: string): string =
## returns the interesting part of a dir
var prefix = gProjectPath & DirSep
var prefix = conf.projectPath & DirSep
if startsWith(dir, prefix):
return substr(dir, len(prefix))
prefix = getPrefixDir(conf) & DirSep
@@ -366,25 +375,24 @@ proc removeTrailingDirSep*(path: string): string =
proc disableNimblePath*(conf: ConfigRef) =
gNoNimblePath = true
lazyPaths.setLen(0)
conf.lazyPaths.setLen(0)
include packagehandling
proc getNimcacheDir*(conf: ConfigRef): string =
result = if nimcacheDir.len > 0: nimcacheDir else: shortenDir(conf, gProjectPath) /
genSubDir
result = if conf.nimcacheDir.len > 0: conf.nimcacheDir
else: shortenDir(conf, conf.projectPath) / genSubDir
proc pathSubs*(conf: ConfigRef; p, config: string): string =
let home = removeTrailingDirSep(os.getHomeDir())
result = unixToNativePath(p % [
"nim", getPrefixDir(conf),
"lib", libpath,
"lib", conf.libpath,
"home", home,
"config", config,
"projectname", options.gProjectName,
"projectpath", options.gProjectPath,
"projectdir", options.gProjectPath,
"projectname", conf.projectName,
"projectpath", conf.projectPath,
"projectdir", conf.projectPath,
"nimcache", getNimcacheDir(conf)])
if "~/" in result:
result = result.replace("~/", home & '/')
@@ -410,28 +418,28 @@ proc completeGeneratedFilePath*(conf: ConfigRef; f: string, createSubDir: bool =
#echo "completeGeneratedFilePath(", f, ") = ", result
proc rawFindFile(conf: ConfigRef; f: string): string =
for it in searchPaths:
for it in conf.searchPaths:
result = joinPath(it, f)
if existsFile(result):
return canonicalizePath(conf, result)
result = ""
proc rawFindFile2(conf: ConfigRef; f: string): string =
for i, it in lazyPaths:
for i, it in conf.lazyPaths:
result = joinPath(it, f)
if existsFile(result):
# bring to front
for j in countDown(i,1):
swap(lazyPaths[j], lazyPaths[j-1])
swap(conf.lazyPaths[j], conf.lazyPaths[j-1])
return canonicalizePath(conf, result)
result = ""
template patchModule(conf: ConfigRef) {.dirty.} =
if result.len > 0 and gModuleOverrides.len > 0:
if result.len > 0 and conf.moduleOverrides.len > 0:
let key = getPackageName(conf, result) & "_" & splitFile(result).name
if gModuleOverrides.hasKey(key):
let ov = gModuleOverrides[key]
if conf.moduleOverrides.hasKey(key):
let ov = conf.moduleOverrides[key]
if ov.len > 0: result = ov
proc findFile*(conf: ConfigRef; f: string): string {.procvar.} =
@@ -492,10 +500,10 @@ proc canonDynlibName(s: string): string =
result = s.substr(start)
proc inclDynlibOverride*(conf: ConfigRef; lib: string) =
gDllOverrides[lib.canonDynlibName] = "true"
conf.dllOverrides[lib.canonDynlibName] = "true"
proc isDynlibOverride*(conf: ConfigRef; lib: string): bool =
result = gDynlibOverrideAll or gDllOverrides.hasKey(lib.canonDynlibName)
result = gDynlibOverrideAll or conf.dllOverrides.hasKey(lib.canonDynlibName)
proc binaryStrSearch*(x: openArray[string], y: string): int =
var a = 0

View File

@@ -216,8 +216,8 @@ proc processModule*(graph: ModuleGraph; module: PSym, stream: PLLStream,
# modules to include between compilation runs? we'd need to track that
# in ROD files. I think we should enable this feature only
# for the interactive mode.
processImplicits graph.config, implicitImports, nkImportStmt, a, module
processImplicits graph.config, implicitIncludes, nkIncludeStmt, a, module
processImplicits graph.config, graph.config.implicitImports, nkImportStmt, a, module
processImplicits graph.config, graph.config.implicitIncludes, nkIncludeStmt, a, module
while true:
if graph.stopCompile(): break

View File

@@ -918,7 +918,7 @@ proc handleSymbolFile*(module: PSym; cache: IdentCache; conf: ConfigRef): PRodRe
if gSymbolFiles in {disabledSf, writeOnlySf, v2Sf}:
module.id = getID()
return nil
idgen.loadMaxIds(conf, options.gProjectPath / options.gProjectName)
idgen.loadMaxIds(conf, conf.projectPath / conf.projectName)
let fileIdx = module.fileIdx
discard checkDep(fileIdx, cache, conf)
#if gMods[fileIdx.int32].reason == rrEmpty: internalError("handleSymbolFile")

View File

@@ -653,7 +653,7 @@ proc myClose(graph: ModuleGraph; c: PPassContext, n: PNode): PNode =
result = process(c, n)
var w = PRodWriter(c)
writeRod(w)
idgen.saveMaxIds(graph.config, options.gProjectPath / options.gProjectName)
idgen.saveMaxIds(graph.config, graph.config.projectPath / graph.config.projectName)
const rodwritePass* = makePass(open = myOpen, close = myClose, process = process)