define nimVersion automatically and avoid needing -d:nimVersion140 (#18726)

* define `nimVersion` and avoid needing -d:nimVersion140

* fix changelog
This commit is contained in:
Timothee Cour 2021-10-16 23:37:52 -07:00 • committed by GitHub
commit f77dea01fd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 25 additions and 4 deletions

View file

@ -1,7 +1,6 @@
# v1.8.x - yyyy-mm-dd # v1.8.x - yyyy-mm-dd
## Changes affecting backward compatibility ## Changes affecting backward compatibility
@ -16,6 +15,8 @@
## Compiler changes ## Compiler changes
- `nim` can now compile version 1.4.0 as follows: `nim c --lib:lib --stylecheck:off compiler/nim`,
without requiring `-d:nimVersion140` which is now a noop.
## Tool changes ## Tool changes

View file

@ -656,7 +656,7 @@ type
mUnaryPlusI, mBitnotI, mUnaryPlusI, mBitnotI,
mUnaryPlusF64, mUnaryMinusF64, mUnaryPlusF64, mUnaryMinusF64,
mCharToStr, mBoolToStr, mCharToStr, mBoolToStr,
mIntToStr, mInt64ToStr, mFloatToStr, # for -d:nimVersion140 mIntToStr, mInt64ToStr, mFloatToStr, # for compiling nimStdlibVersion < 1.5.1 (not bootstrapping)
mCStrToStr, mCStrToStr,
mStrToStr, mEnumToStr, mStrToStr, mEnumToStr,
mAnd, mOr, mAnd, mOr,

View file

@ -894,7 +894,7 @@ proc genFieldCheck(p: BProc, e: PNode, obj: Rope, field: PSym) =
let discIndex = rdSetElemLoc(p.config, v, u.t) let discIndex = rdSetElemLoc(p.config, v, u.t)
if optTinyRtti in p.config.globalOptions: if optTinyRtti in p.config.globalOptions:
# not sure how to use `genEnumToStr` here # not sure how to use `genEnumToStr` here
if p.config.isDefined("nimVersion140"): if p.config.getStdlibVersion < (1,5,1):
const code = "{ #raiseFieldError($1); $2} $n" const code = "{ #raiseFieldError($1); $2} $n"
linefmt(p, cpsStmts, code, [strLit, raiseInstr(p)]) linefmt(p, cpsStmts, code, [strLit, raiseInstr(p)])
else: else:
@ -905,7 +905,7 @@ proc genFieldCheck(p: BProc, e: PNode, obj: Rope, field: PSym) =
let first = p.config.firstOrd(disc.sym.typ) let first = p.config.firstOrd(disc.sym.typ)
let firstLit = int64Literal(cast[int](first)) let firstLit = int64Literal(cast[int](first))
let discName = genTypeInfo(p.config, p.module, disc.sym.typ, e.info) let discName = genTypeInfo(p.config, p.module, disc.sym.typ, e.info)
if p.config.isDefined("nimVersion140"): if p.config.getStdlibVersion < (1,5,1):
const code = "{ #raiseFieldError($1); $2} $n" const code = "{ #raiseFieldError($1); $2} $n"
linefmt(p, cpsStmts, code, [strLit, raiseInstr(p)]) linefmt(p, cpsStmts, code, [strLit, raiseInstr(p)])
else: else:

View file

@ -165,6 +165,7 @@ const
cmdCtags, cmdBuildindex} cmdCtags, cmdBuildindex}
type type
NimVer* = tuple[major: int, minor: int, patch: int]
TStringSeq* = seq[string] TStringSeq* = seq[string]
TGCMode* = enum # the selected GC TGCMode* = enum # the selected GC
gcUnselected = "unselected" gcUnselected = "unselected"
@ -346,6 +347,7 @@ type
outDir*: AbsoluteDir outDir*: AbsoluteDir
jsonBuildFile*: AbsoluteFile jsonBuildFile*: AbsoluteFile
prefixDir*, libpath*, nimcacheDir*: AbsoluteDir prefixDir*, libpath*, nimcacheDir*: AbsoluteDir
nimStdlibVersion*: NimVer
dllOverrides, moduleOverrides*, cfileSpecificOptions*: StringTableRef dllOverrides, moduleOverrides*, cfileSpecificOptions*: StringTableRef
projectName*: string # holds a name like 'nim' projectName*: string # holds a name like 'nim'
projectPath*: AbsoluteDir # holds a path like /home/alice/projects/nim/compiler/ projectPath*: AbsoluteDir # holds a path like /home/alice/projects/nim/compiler/
@ -389,6 +391,16 @@ type
cppCustomNamespace*: string cppCustomNamespace*: string
vmProfileData*: ProfileData vmProfileData*: ProfileData
proc parseNimVersion*(a: string): NimVer =
# could be moved somewhere reusable
if a.len > 0:
let b = a.split(".")
assert b.len == 3, a
template fn(i) = result[i] = b[i].parseInt # could be optimized if needed
fn(0)
fn(1)
fn(2)
proc assignIfDefault*[T](result: var T, val: T, def = default(T)) = proc assignIfDefault*[T](result: var T, val: T, def = default(T)) =
## if `result` was already assigned to a value (that wasn't `def`), this is a noop. ## if `result` was already assigned to a value (that wasn't `def`), this is a noop.
if result == def: result = val if result == def: result = val
@ -560,6 +572,12 @@ proc newPartialConfigRef*(): ConfigRef =
proc cppDefine*(c: ConfigRef; define: string) = proc cppDefine*(c: ConfigRef; define: string) =
c.cppDefines.incl define c.cppDefines.incl define
proc getStdlibVersion*(conf: ConfigRef): NimVer =
if conf.nimStdlibVersion == (0,0,0):
let s = conf.symbols.getOrDefault("nimVersion", "")
conf.nimStdlibVersion = s.parseNimVersion
result = conf.nimStdlibVersion
proc isDefined*(conf: ConfigRef; symbol: string): bool = proc isDefined*(conf: ConfigRef; symbol: string): bool =
if conf.symbols.hasKey(symbol): if conf.symbols.hasKey(symbol):
result = true result = true

View file

@ -14,3 +14,5 @@ when defined(nimStrictMode):
# switch("hint", "ConvFromXtoItselfNotNeeded") # switch("hint", "ConvFromXtoItselfNotNeeded")
switch("hintAsError", "ConvFromXtoItselfNotNeeded") switch("hintAsError", "ConvFromXtoItselfNotNeeded")
# future work: XDeclaredButNotUsed # future work: XDeclaredButNotUsed
switch("define", "nimVersion:" & NimVersion)