niminst: diverse improvements; fixes #80

This commit is contained in:
Araq 2012-01-13 21:15:00 +01:00
commit 8ed3e295a3
13 changed files with 102 additions and 46 deletions

View file

@ -2,7 +2,7 @@
REM Generated by niminst REM Generated by niminst
SET CC=gcc SET CC=gcc
SET LINKER=gcc SET LINKER=gcc
SET COMP_FLAGS=-w -O3 -fno-strict-aliasing SET COMP_FLAGS= -w -O3 -fno-strict-aliasing
SET LINK_FLAGS= SET LINK_FLAGS=
REM call the compiler: REM call the compiler:

View file

@ -46,13 +46,6 @@ proc mangleName(s: PSym): PRope =
app(result, toRope(mangle(s.name.s))) app(result, toRope(mangle(s.name.s)))
app(result, "_") app(result, "_")
app(result, toRope(s.id)) app(result, toRope(s.id))
when false:
# deactivated to make mapping file smaller which is currently only used
# for the list of generated C files
if optGenMapping in gGlobalOptions:
if s.owner != nil:
appf(gMapping, "r\"$1.$2\": $3$n",
[toRope(s.owner.Name.s), toRope(s.name.s), result])
s.loc.r = result s.loc.r = result
proc getTypeName(typ: PType): PRope = proc getTypeName(typ: PType): PRope =

View file

@ -294,7 +294,7 @@ const
pcc(), pcc(),
ucc(), ucc(),
icc(), icc(),
gpp() ] gpp()]
const const
hExt* = "h" hExt* = "h"
@ -336,7 +336,7 @@ proc setCC*(ccname: string) =
defineSymbol(CC[ccompiler].name) defineSymbol(CC[ccompiler].name)
proc addOpt(dest: var string, src: string) = proc addOpt(dest: var string, src: string) =
if len(dest) == 0 or dest[len(dest) - 1 + 0] != ' ': add(dest, " ") if len(dest) == 0 or dest[len(dest)-1] != ' ': add(dest, " ")
add(dest, src) add(dest, src)
proc addLinkOption*(option: string) = proc addLinkOption*(option: string) =
@ -456,6 +456,16 @@ proc CFileSpecificOptions(cfilename: string): string =
var key = trunk & ".always" var key = trunk & ".always"
if existsConfigVar(key): addOpt(result, getConfigVar(key)) if existsConfigVar(key): addOpt(result, getConfigVar(key))
proc getCompileOptions: string =
result = CFileSpecificOptions("__dummy__")
proc getLinkOptions: string =
result = linkOptions
for linkedLib in items(cLinkedLibs):
result.add(cc[ccompiler].linkLibCmd % linkedLib.quoteIfContainsWhite)
for libDir in items(cLibs):
result.add cc[ccompiler].linkDirCmd, libDir.quoteIfContainsWhite
proc getCompileCFileCmd*(cfilename: string, isExternal = false): string = proc getCompileCFileCmd*(cfilename: string, isExternal = false): string =
var c = ccompiler var c = ccompiler
var options = CFileSpecificOptions(cfilename) var options = CFileSpecificOptions(cfilename)
@ -566,11 +576,7 @@ proc CallCCompiler*(projectfile: string) =
if not noAbsolutePaths(): if not noAbsolutePaths():
exefile = joinPath(splitFile(projectFile).dir, exefile) exefile = joinPath(splitFile(projectFile).dir, exefile)
exefile = quoteIfContainsWhite(exefile) exefile = quoteIfContainsWhite(exefile)
for linkedLib in items(cLinkedLibs): let linkOptions = getLinkOptions()
linkOptions.add(cc[c].linkLibCmd % linkedLib.quoteIfContainsWhite)
for libDir in items(cLibs):
linkOptions.add cc[c].linkDirCmd, libDir.quoteIfContainsWhite
linkCmd = quoteIfContainsWhite(linkCmd % ["builddll", builddll, linkCmd = quoteIfContainsWhite(linkCmd % ["builddll", builddll,
"buildgui", buildgui, "options", linkOptions, "objfiles", objfiles, "buildgui", buildgui, "options", linkOptions, "objfiles", objfiles,
"exefile", exefile, "nimrod", getPrefixDir(), "lib", libpath]) "exefile", exefile, "nimrod", getPrefixDir(), "lib", libpath])
@ -599,6 +605,15 @@ proc writeMapping*(gSymbolMapping: PRope) =
var code = toRope("[C_Files]\n") var code = toRope("[C_Files]\n")
app(code, genMappingFiles(toCompile)) app(code, genMappingFiles(toCompile))
app(code, genMappingFiles(externalToCompile)) app(code, genMappingFiles(externalToCompile))
appf(code, "[Symbols]$n$1", [gSymbolMapping]) app(code, "\n[C_Compiler]\nFlags=")
app(code, strutils.escape(getCompileOptions()))
app(code, "\n[Linker]\nFlags=")
app(code, strutils.escape(getLinkOptions()))
app(code, "\n[Environment]\nlibpath=")
app(code, strutils.escape(libpath))
appf(code, "\n[Symbols]$n$1", [gSymbolMapping])
WriteRope(code, joinPath(gProjectPath, "mapping.txt")) WriteRope(code, joinPath(gProjectPath, "mapping.txt"))

View file

@ -62,11 +62,11 @@ proc tryExec(cmd: string): bool =
result = execShellCmd(cmd) == 0 result = execShellCmd(cmd) == 0
proc csource(args: string) = proc csource(args: string) =
exec("nimrod cc $1 -r tools/niminst --var:version=$2 csource compiler/nimrod.ini $1" % exec("nimrod cc $1 -r tools/niminst/niminst --var:version=$2 csource compiler/nimrod.ini $1" %
[args, NimrodVersion]) [args, NimrodVersion])
proc zip(args: string) = proc zip(args: string) =
exec("nimrod cc -r tools/niminst --var:version=$# zip compiler/nimrod.ini" % exec("nimrod cc -r tools/niminst/niminst --var:version=$# zip compiler/nimrod.ini" %
NimrodVersion) NimrodVersion)
proc buildTool(toolname, args: string) = proc buildTool(toolname, args: string) =
@ -75,9 +75,9 @@ proc buildTool(toolname, args: string) =
proc inno(args: string) = proc inno(args: string) =
# make sure we have generated the c2nim and niminst executables: # make sure we have generated the c2nim and niminst executables:
buildTool("tools/niminst", args) buildTool("tools/niminst/niminst", args)
buildTool("compiler/c2nim/c2nim", args) buildTool("compiler/c2nim/c2nim", args)
exec("tools" / "niminst --var:version=$# inno compiler/nimrod" % exec("tools" / "niminst" / "niminst --var:version=$# inno compiler/nimrod" %
NimrodVersion) NimrodVersion)
proc install(args: string) = proc install(args: string) =

View file

@ -676,6 +676,31 @@ proc replace*(s: string, sub, by: char): string {.noSideEffect,
else: result[i] = s[i] else: result[i] = s[i]
inc(i) inc(i)
proc replaceWord*(s, sub: string, by = ""): string {.noSideEffect,
rtl, extern: "nsuReplaceStr".} =
## Replaces `sub` in `s` by the string `by`. Each occurance of `sub`
## has to be surrounded by word boundaries (comparable to ``\\w`` in
## regular expressions), otherwise it is not replaced.
const wordChars = {'a'..'z', 'A'..'Z', '0'..'9', '_', '\128'..'\255'}
var a {.noinit.}: TSkipTable
result = ""
preprocessSub(sub, a)
var i = 0
while true:
var j = findAux(s, sub, i, a)
if j < 0: break
# word boundary?
if (j == 0 or s[j-1] notin wordChars) and
(j+sub.len >= s.len or s[j+sub.len] notin wordChars):
add result, substr(s, i, j - 1)
add result, by
i = j + len(sub)
else:
add result, substr(s, i, j)
i = j + 1
# copy the rest:
add result, substr(s, i)
proc delete*(s: var string, first, last: int) {.noSideEffect, proc delete*(s: var string, first, last: int) {.noSideEffect,
rtl, extern: "nsuDelete".} = rtl, extern: "nsuDelete".} =
## Deletes in `s` the characters at position `first` .. `last`. This modifies ## Deletes in `s` the characters at position `first` .. `last`. This modifies
@ -960,6 +985,9 @@ proc findNormalized(x: string, inArray: openarray[string]): int =
# security hole... # security hole...
return -1 return -1
proc invalidFormatString() {.noinline.} =
raise newException(EInvalidValue, "invalid format string")
proc addf*(s: var string, formatstr: string, a: openarray[string]) {. proc addf*(s: var string, formatstr: string, a: openarray[string]) {.
noSideEffect, rtl, extern: "nsuAddf".} = noSideEffect, rtl, extern: "nsuAddf".} =
## The same as ``add(s, formatstr % a)``, but more efficient. ## The same as ``add(s, formatstr % a)``, but more efficient.
@ -971,6 +999,7 @@ proc addf*(s: var string, formatstr: string, a: openarray[string]) {.
case formatstr[i+1] # again we use the fact that strings case formatstr[i+1] # again we use the fact that strings
# are zero-terminated here # are zero-terminated here
of '#': of '#':
if num >% a.high: invalidFormatString()
add s, a[num] add s, a[num]
inc i, 2 inc i, 2
inc num inc num
@ -985,26 +1014,25 @@ proc addf*(s: var string, formatstr: string, a: openarray[string]) {.
while formatstr[i] in Digits: while formatstr[i] in Digits:
j = j * 10 + ord(formatstr[i]) - ord('0') j = j * 10 + ord(formatstr[i]) - ord('0')
inc(i) inc(i)
if not negative: let idx = if not negative: j-1 else: a.len-j
add s, a[j - 1] if idx >% a.high: invalidFormatString()
else: add s, a[idx]
add s, a[a.len - j]
of '{': of '{':
var j = i+1 var j = i+1
while formatstr[j] notin {'\0', '}'}: inc(j) while formatstr[j] notin {'\0', '}'}: inc(j)
var x = findNormalized(substr(formatstr, i+2, j-1), a) var x = findNormalized(substr(formatstr, i+2, j-1), a)
if x >= 0 and x < high(a): add s, a[x+1] if x >= 0 and x < high(a): add s, a[x+1]
else: raise newException(EInvalidValue, "invalid format string") else: invalidFormatString()
i = j+1 i = j+1
of 'a'..'z', 'A'..'Z', '\128'..'\255', '_': of 'a'..'z', 'A'..'Z', '\128'..'\255', '_':
var j = i+1 var j = i+1
while formatstr[j] in PatternChars: inc(j) while formatstr[j] in PatternChars: inc(j)
var x = findNormalized(substr(formatstr, i+1, j-1), a) var x = findNormalized(substr(formatstr, i+1, j-1), a)
if x >= 0 and x < high(a): add s, a[x+1] if x >= 0 and x < high(a): add s, a[x+1]
else: raise newException(EInvalidValue, "invalid format string") else: invalidFormatString()
i = j i = j
else: else:
raise newException(EInvalidValue, "invalid format string") invalidFormatString()
else: else:
add s, formatstr[i] add s, formatstr[i]
inc(i) inc(i)
@ -1076,3 +1104,7 @@ when isMainModule:
doAssert "$animal eats $food." % ["animal", "The cat", "food", "fish"] == doAssert "$animal eats $food." % ["animal", "The cat", "food", "fish"] ==
"The cat eats fish." "The cat eats fish."
doAssert "-ld a-ldz -ld".replaceWord("-ld") == " a-ldz "
doAssert "-lda-ldz -ld abc".replaceWord("-ld") == "-lda-ldz abc"

View file

@ -1,6 +1,7 @@
version 0.8.14 version 0.8.14
============== ==============
- improve niminst
- fix line info in assertions - fix line info in assertions
version 0.9.0 version 0.9.0

View file

@ -3,8 +3,8 @@
# result = "@echo off\nREM Generated by niminst\n" # result = "@echo off\nREM Generated by niminst\n"
SET CC=gcc SET CC=gcc
SET LINKER=gcc SET LINKER=gcc
SET COMP_FLAGS=-w -O3 -fno-strict-aliasing SET COMP_FLAGS=?{c.ccompiler.flags}
SET LINK_FLAGS= SET LINK_FLAGS=?{c.linker.flags}
REM call the compiler: REM call the compiler:

View file

@ -5,8 +5,8 @@
# "# To regenerate run ``niminst csource`` or ``koch csource``\n" # "# To regenerate run ``niminst csource`` or ``koch csource``\n"
CC="gcc" CC="gcc"
LINKER="gcc" LINKER="gcc"
COMP_FLAGS="-w -O3 -fno-strict-aliasing" COMP_FLAGS="?{c.ccompiler.flags}"
LINK_FLAGS="" LINK_FLAGS="?{c.linker.flags}"
# add(result, "# platform detection\n") # add(result, "# platform detection\n")
ucpu=`uname -m` ucpu=`uname -m`
uos=`uname` uos=`uname`

View file

@ -49,8 +49,9 @@ type
cat: array[TFileCategory, seq[string]] cat: array[TFileCategory, seq[string]]
binPaths, authors, oses, cpus: seq[string] binPaths, authors, oses, cpus: seq[string]
cfiles: array[1..maxOS, array[1..maxCPU, seq[string]]] cfiles: array[1..maxOS, array[1..maxCPU, seq[string]]]
ccompiler, innosetup: tuple[path, flags: string] ccompiler, linker, innosetup: tuple[path, flags: string]
name, displayName, version, description, license, infile, outdir: string name, displayName, version, description, license, infile, outdir: string
libpath: string
innoSetupFlag, installScript, uninstallScript: bool innoSetupFlag, installScript, uninstallScript: bool
vars: PStringTable vars: PStringTable
app: TAppType app: TAppType
@ -69,6 +70,7 @@ proc initConfigData(c: var TConfigData) =
c.oses = @[] c.oses = @[]
c.cpus = @[] c.cpus = @[]
c.ccompiler = ("", "") c.ccompiler = ("", "")
c.linker = ("", "")
c.innosetup = ("", "") c.innosetup = ("", "")
c.name = "" c.name = ""
c.displayName = "" c.displayName = ""
@ -78,6 +80,7 @@ proc initConfigData(c: var TConfigData) =
c.infile = "" c.infile = ""
c.outdir = "" c.outdir = ""
c.nimrodArgs = "" c.nimrodArgs = ""
c.libpath = ""
c.innoSetupFlag = false c.innoSetupFlag = false
c.installScript = false c.installScript = false
c.uninstallScript = false c.uninstallScript = false
@ -255,6 +258,7 @@ proc parseIniFile(c: var TConfigData) =
of "unixbin": filesOnly(p, k.key, v, c.cat[fcUnixBin]) of "unixbin": filesOnly(p, k.key, v, c.cat[fcUnixBin])
of "innosetup": pathFlags(p, k.key, v, c.innoSetup) of "innosetup": pathFlags(p, k.key, v, c.innoSetup)
of "ccompiler": pathFlags(p, k.key, v, c.ccompiler) of "ccompiler": pathFlags(p, k.key, v, c.ccompiler)
of "linker": pathFlags(p, k.key, v, c.linker)
else: quit(errorStr(p, "invalid section: " & section)) else: quit(errorStr(p, "invalid section: " & section))
of cfgOption: quit(errorStr(p, "syntax error")) of cfgOption: quit(errorStr(p, "syntax error"))
@ -268,26 +272,36 @@ proc parseIniFile(c: var TConfigData) =
# ------------------------- generate source based installation --------------- # ------------------------- generate source based installation ---------------
proc readCFiles(c: var TConfigData, osA, cpuA: int) = proc readCFiles(c: var TConfigData, osA, cpuA: int) =
var cfg: TCfgParser var p: TCfgParser
var cfilesSection = false
var f = splitFile(c.infile).dir / "mapping.txt" var f = splitFile(c.infile).dir / "mapping.txt"
c.cfiles[osA][cpuA] = @[] c.cfiles[osA][cpuA] = @[]
var input = newFileStream(f, fmRead) var input = newFileStream(f, fmRead)
var section = ""
if input != nil: if input != nil:
open(cfg, input, f) open(p, input, f)
while true: while true:
var k = next(cfg) var k = next(p)
case k.kind case k.kind
of cfgEof: break of cfgEof: break
of cfgSectionStart: of cfgSectionStart:
if cfilesSection: break section = normalize(k.section)
cfilesSection = cmpIgnoreStyle(k.section, "cfiles") == 0 of cfgKeyValuePair:
of cfgKeyValuePair: nil case section
of "ccompiler": pathFlags(p, k.key, k.value, c.ccompiler)
of "linker":
pathFlags(p, k.key, k.value, c.linker)
# HACK: we conditionally add ``-lm -ldl``, so remove them from the
# linker flags:
c.linker.flags = c.linker.flags.replaceWord("-lm").replaceWord(
"-ldl").strip
else:
if cmpIgnoreStyle(k.key, "libpath") == 0:
c.libpath = k.value
of cfgOption: of cfgOption:
if cfilesSection and cmpIgnoreStyle(k.key, "file") == 0: if section == "cfiles" and cmpIgnoreStyle(k.key, "file") == 0:
add(c.cfiles[osA][cpuA], k.value) add(c.cfiles[osA][cpuA], k.value)
of cfgError: quit(errorStr(cfg, k.msg)) of cfgError: quit(errorStr(p, k.msg))
close(cfg) close(p)
else: else:
quit("Cannot open: " & f) quit("Cannot open: " & f)
@ -321,7 +335,7 @@ proc removeDuplicateFiles(c: var TConfigData) =
c.cfiles[osA][cpuA][i] = orig c.cfiles[osA][cpuA][i] = orig
proc srcdist(c: var TConfigData) = proc srcdist(c: var TConfigData) =
for x in walkFiles("lib/*.h"): for x in walkFiles(c.libpath / "lib/*.h"):
CopyFile(dest="build" / extractFilename(x), source=x) CopyFile(dest="build" / extractFilename(x), source=x)
for osA in 1..c.oses.len: for osA in 1..c.oses.len:
for cpuA in 1..c.cpus.len: for cpuA in 1..c.cpus.len:
@ -380,7 +394,7 @@ when haveZipLib:
addFile(z, proj / buildShFile, buildShFile) addFile(z, proj / buildShFile, buildShFile)
addFile(z, proj / installShFile, installShFile) addFile(z, proj / installShFile, installShFile)
addFile(z, proj / deinstallShFile, deinstallShFile) addFile(z, proj / deinstallShFile, deinstallShFile)
for f in walkFiles("lib/*.h"): for f in walkFiles(c.libpath / "lib/*.h"):
addFile(z, proj / "build" / extractFilename(f), f) addFile(z, proj / "build" / extractFilename(f), f)
for osA in 1..c.oses.len: for osA in 1..c.oses.len:
for cpuA in 1..c.cpus.len: for cpuA in 1..c.cpus.len:

View file

@ -129,7 +129,8 @@ Library Additions
- Slicing as implemented by the system module now supports *splicing*. - Slicing as implemented by the system module now supports *splicing*.
- Added explicit channels for thread communication. - Added explicit channels for thread communication.
- Added ``matchers`` module for email address etc. matching. - Added ``matchers`` module for email address etc. matching.
- Added ``strutils.unindent``, ``strutils.countLines``. - Added ``strutils.unindent``, ``strutils.countLines``,
``strutils.replaceWord``.
- Added ``system.slurp`` for easy resource embedding. - Added ``system.slurp`` for easy resource embedding.
- Added ``system.running`` for threads. - Added ``system.running`` for threads.
- Added ``system.programResult``. - Added ``system.programResult``.