Merge pull request #4 from jyapayne/configurable_compiler
Add configurable compiler to global section
This commit is contained in:
commit
84894bfa0c
2 changed files with 78 additions and 12 deletions
15
README.md
15
README.md
|
|
@ -65,6 +65,17 @@ Nimgen only supports the ```gcc``` preprocessor at this time. Support for detect
|
||||||
|
|
||||||
__Config file__
|
__Config file__
|
||||||
|
|
||||||
|
In all sections below, environment variables are supported via Nim's string interpolation `%` symbol imported from the `strutils` module. Simply use double quotes to enclose any value and put `$` or `${}` around the environment variable name. In addition, the `output` var from the n.global section is available as ${output}. For example:
|
||||||
|
|
||||||
|
[n.global]
|
||||||
|
c_compiler="$CC"
|
||||||
|
cpp_compiler="${CPP}-arm"
|
||||||
|
output="src/path"
|
||||||
|
|
||||||
|
[n.include]
|
||||||
|
"${output}/library/include"
|
||||||
|
"${MY_INCLUDE_PATH}/include"
|
||||||
|
|
||||||
_[n.global]_
|
_[n.global]_
|
||||||
|
|
||||||
```output``` = name of the Nimble project once installed, also location to place generated .nim files
|
```output``` = name of the Nimble project once installed, also location to place generated .nim files
|
||||||
|
|
@ -73,6 +84,10 @@ _[n.global]_
|
||||||
|
|
||||||
```filter``` = string to identify and recurse into library .h files in #include statements and exclude standard headers
|
```filter``` = string to identify and recurse into library .h files in #include statements and exclude standard headers
|
||||||
|
|
||||||
|
```cpp_compiler``` = string to specify a CPP compiler executable. [default: g++]
|
||||||
|
|
||||||
|
```c_compiler``` = string to specify a C compiler executable. [default: gcc]
|
||||||
|
|
||||||
_[n.include]_
|
_[n.include]_
|
||||||
|
|
||||||
List of all directories, one per line, to include in the search path. This is used by:-
|
List of all directories, one per line, to include in the search path. This is used by:-
|
||||||
|
|
|
||||||
75
nimgen.nim
75
nimgen.nim
|
|
@ -1,5 +1,11 @@
|
||||||
import nre, os, ospaths, osproc, parsecfg, pegs, ropes, sequtils, streams, strutils, tables
|
import nre, os, ospaths, osproc, parsecfg, pegs, ropes, sequtils, streams, strutils, tables
|
||||||
|
|
||||||
|
const
|
||||||
|
cCompilerEnv = "CC"
|
||||||
|
cppCompilerEnv = "CPP"
|
||||||
|
defaultCCompiler = "gcc"
|
||||||
|
defaultCppCompiler = "g++"
|
||||||
|
|
||||||
var
|
var
|
||||||
gDoneRecursive: seq[string] = @[]
|
gDoneRecursive: seq[string] = @[]
|
||||||
gDoneInline: seq[string] = @[]
|
gDoneInline: seq[string] = @[]
|
||||||
|
|
@ -8,6 +14,8 @@ var
|
||||||
gConfig: Config
|
gConfig: Config
|
||||||
gFilter = ""
|
gFilter = ""
|
||||||
gQuotes = true
|
gQuotes = true
|
||||||
|
gCppCompiler = getEnv(cppCompilerEnv, defaultCCompiler)
|
||||||
|
gCCompiler = getEnv(cCompilerEnv, defaultCppCompiler)
|
||||||
gOutput = ""
|
gOutput = ""
|
||||||
gIncludes: seq[string] = @[]
|
gIncludes: seq[string] = @[]
|
||||||
gExcludes: seq[string] = @[]
|
gExcludes: seq[string] = @[]
|
||||||
|
|
@ -33,6 +41,33 @@ Options:
|
||||||
# ###
|
# ###
|
||||||
# Helpers
|
# Helpers
|
||||||
|
|
||||||
|
proc addEnv(str: string): string =
|
||||||
|
var newStr = str
|
||||||
|
for pair in envPairs():
|
||||||
|
try:
|
||||||
|
newStr = newStr % [pair.key, pair.value.string]
|
||||||
|
except ValueError:
|
||||||
|
# Ignore if there are no values to replace. We
|
||||||
|
# want to continue anyway
|
||||||
|
discard
|
||||||
|
|
||||||
|
try:
|
||||||
|
newStr = newStr % ["output", gOutput]
|
||||||
|
except ValueError:
|
||||||
|
# Ignore if there are no values to replace. We
|
||||||
|
# want to continue anyway
|
||||||
|
discard
|
||||||
|
|
||||||
|
# if there are still format args, print a warning
|
||||||
|
if newStr.contains("${"):
|
||||||
|
echo "WARNING: \"", newStr, "\" still contains an uninterpolated value!"
|
||||||
|
|
||||||
|
return newStr
|
||||||
|
|
||||||
|
proc `[]`(table: OrderedTableRef[string, string], key: string): string =
|
||||||
|
## Gets table values with env vars inserted
|
||||||
|
tables.`[]`(table, key).addEnv
|
||||||
|
|
||||||
proc execProc(cmd: string): string =
|
proc execProc(cmd: string): string =
|
||||||
result = ""
|
result = ""
|
||||||
var
|
var
|
||||||
|
|
@ -379,7 +414,7 @@ proc getDefines(file: string, inline=false): string =
|
||||||
|
|
||||||
proc runPreprocess(file, ppflags, flags: string, inline: bool): string =
|
proc runPreprocess(file, ppflags, flags: string, inline: bool): string =
|
||||||
var
|
var
|
||||||
pproc = if flags.contains("cpp"): "g++" else: "gcc"
|
pproc = if flags.contains("cpp"): gCppCompiler else: gCCompiler
|
||||||
cmd = "$# -E $# $#" % [pproc, ppflags, file]
|
cmd = "$# -E $# $#" % [pproc, ppflags, file]
|
||||||
|
|
||||||
for inc in gIncludes:
|
for inc in gIncludes:
|
||||||
|
|
@ -612,7 +647,9 @@ proc runFile(file: string, cfgin: OrderedTableRef) =
|
||||||
if action == "create":
|
if action == "create":
|
||||||
createDir(file.splitPath().head)
|
createDir(file.splitPath().head)
|
||||||
writeFile(file, cfg[act])
|
writeFile(file, cfg[act])
|
||||||
elif action in @["prepend", "append", "replace", "comment", "rename", "compile", "dynlib", "pragma", "pipe"] and sfile != "":
|
elif action in @["prepend", "append", "replace", "comment",
|
||||||
|
"rename", "compile", "dynlib", "pragma",
|
||||||
|
"pipe"] and sfile != "":
|
||||||
if action == "prepend":
|
if action == "prepend":
|
||||||
if srch != "":
|
if srch != "":
|
||||||
prepend(sfile, cfg[act], cfg[srch])
|
prepend(sfile, cfg[act], cfg[srch])
|
||||||
|
|
@ -702,6 +739,18 @@ proc runCfg(cfg: string) =
|
||||||
quit(1)
|
quit(1)
|
||||||
createDir(gOutput)
|
createDir(gOutput)
|
||||||
|
|
||||||
|
if gConfig["n.global"].hasKey("cpp_compiler"):
|
||||||
|
gCppCompiler = gConfig["n.global"]["cpp_compiler"]
|
||||||
|
else:
|
||||||
|
# Reset on a per project basis
|
||||||
|
gCppCompiler = getEnv(cppCompilerEnv, defaultCppCompiler)
|
||||||
|
|
||||||
|
if gConfig["n.global"].hasKey("c_compiler"):
|
||||||
|
gCCompiler = gConfig["n.global"]["c_compiler"]
|
||||||
|
else:
|
||||||
|
# Reset on a per project basis
|
||||||
|
gCCompiler = getEnv(cCompilerEnv, defaultCCompiler)
|
||||||
|
|
||||||
if gConfig["n.global"].hasKey("filter"):
|
if gConfig["n.global"].hasKey("filter"):
|
||||||
gFilter = gConfig["n.global"]["filter"]
|
gFilter = gConfig["n.global"]["filter"]
|
||||||
if gConfig["n.global"].hasKey("quotes"):
|
if gConfig["n.global"].hasKey("quotes"):
|
||||||
|
|
@ -710,30 +759,31 @@ proc runCfg(cfg: string) =
|
||||||
|
|
||||||
if gConfig.hasKey("n.include"):
|
if gConfig.hasKey("n.include"):
|
||||||
for inc in gConfig["n.include"].keys():
|
for inc in gConfig["n.include"].keys():
|
||||||
gIncludes.add(inc)
|
gIncludes.add(inc.addEnv())
|
||||||
|
|
||||||
if gConfig.hasKey("n.exclude"):
|
if gConfig.hasKey("n.exclude"):
|
||||||
for excl in gConfig["n.exclude"].keys():
|
for excl in gConfig["n.exclude"].keys():
|
||||||
gExcludes.add(excl)
|
gExcludes.add(excl.addEnv())
|
||||||
|
|
||||||
if gConfig.hasKey("n.prepare"):
|
if gConfig.hasKey("n.prepare"):
|
||||||
for prep in gConfig["n.prepare"].keys():
|
for prep in gConfig["n.prepare"].keys():
|
||||||
let (key, val) = getKey(prep)
|
let (key, val) = getKey(prep)
|
||||||
if val == true:
|
if val == true:
|
||||||
|
let prepVal = gConfig["n.prepare"][prep]
|
||||||
if key == "download":
|
if key == "download":
|
||||||
downloadUrl(gConfig["n.prepare"][prep])
|
downloadUrl(prepVal)
|
||||||
elif key == "extract":
|
elif key == "extract":
|
||||||
extractZip(gConfig["n.prepare"][prep])
|
extractZip(prepVal)
|
||||||
elif key == "git":
|
elif key == "git":
|
||||||
gitRemotePull(gConfig["n.prepare"][prep])
|
gitRemotePull(prepVal)
|
||||||
elif key == "gitremote":
|
elif key == "gitremote":
|
||||||
gitRemotePull(gConfig["n.prepare"][prep], false)
|
gitRemotePull(prepVal, false)
|
||||||
elif key == "gitsparse":
|
elif key == "gitsparse":
|
||||||
gitSparseCheckout(gConfig["n.prepare"][prep])
|
gitSparseCheckout(prepVal)
|
||||||
elif key == "execute":
|
elif key == "execute":
|
||||||
discard execProc(gConfig["n.prepare"][prep])
|
discard execProc(prepVal)
|
||||||
elif key == "copy":
|
elif key == "copy":
|
||||||
doCopy(gConfig["n.prepare"][prep])
|
doCopy(prepVal)
|
||||||
|
|
||||||
if gConfig.hasKey("n.wildcard"):
|
if gConfig.hasKey("n.wildcard"):
|
||||||
var wildcard = ""
|
var wildcard = ""
|
||||||
|
|
@ -743,7 +793,8 @@ proc runCfg(cfg: string) =
|
||||||
if key == "wildcard":
|
if key == "wildcard":
|
||||||
wildcard = gConfig["n.wildcard"][wild]
|
wildcard = gConfig["n.wildcard"][wild]
|
||||||
else:
|
else:
|
||||||
gWildcards.setSectionKey(wildcard, wild, gConfig["n.wildcard"][wild])
|
gWildcards.setSectionKey(wildcard, wild,
|
||||||
|
gConfig["n.wildcard"][wild])
|
||||||
|
|
||||||
for file in gConfig.keys():
|
for file in gConfig.keys():
|
||||||
if file in @["n.global", "n.include", "n.exclude", "n.prepare", "n.wildcard"]:
|
if file in @["n.global", "n.include", "n.exclude", "n.prepare", "n.wildcard"]:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue