Merge pull request #4 from jyapayne/configurable_compiler

Add configurable compiler to global section
This commit is contained in:
genotrance 2018-07-09 15:40:33 -05:00 committed by GitHub
commit 84894bfa0c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 78 additions and 12 deletions

View file

@ -65,6 +65,17 @@ Nimgen only supports the ```gcc``` preprocessor at this time. Support for detect
__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]_
```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
```cpp_compiler``` = string to specify a CPP compiler executable. [default: g++]
```c_compiler``` = string to specify a C compiler executable. [default: gcc]
_[n.include]_
List of all directories, one per line, to include in the search path. This is used by:-

View file

@ -1,5 +1,11 @@
import nre, os, ospaths, osproc, parsecfg, pegs, ropes, sequtils, streams, strutils, tables
const
cCompilerEnv = "CC"
cppCompilerEnv = "CPP"
defaultCCompiler = "gcc"
defaultCppCompiler = "g++"
var
gDoneRecursive: seq[string] = @[]
gDoneInline: seq[string] = @[]
@ -8,6 +14,8 @@ var
gConfig: Config
gFilter = ""
gQuotes = true
gCppCompiler = getEnv(cppCompilerEnv, defaultCCompiler)
gCCompiler = getEnv(cCompilerEnv, defaultCppCompiler)
gOutput = ""
gIncludes: seq[string] = @[]
gExcludes: seq[string] = @[]
@ -33,6 +41,33 @@ Options:
# ###
# 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 =
result = ""
var
@ -379,7 +414,7 @@ proc getDefines(file: string, inline=false): string =
proc runPreprocess(file, ppflags, flags: string, inline: bool): string =
var
pproc = if flags.contains("cpp"): "g++" else: "gcc"
pproc = if flags.contains("cpp"): gCppCompiler else: gCCompiler
cmd = "$# -E $# $#" % [pproc, ppflags, file]
for inc in gIncludes:
@ -612,7 +647,9 @@ proc runFile(file: string, cfgin: OrderedTableRef) =
if action == "create":
createDir(file.splitPath().head)
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 srch != "":
prepend(sfile, cfg[act], cfg[srch])
@ -702,6 +739,18 @@ proc runCfg(cfg: string) =
quit(1)
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"):
gFilter = gConfig["n.global"]["filter"]
if gConfig["n.global"].hasKey("quotes"):
@ -710,30 +759,31 @@ proc runCfg(cfg: string) =
if gConfig.hasKey("n.include"):
for inc in gConfig["n.include"].keys():
gIncludes.add(inc)
gIncludes.add(inc.addEnv())
if gConfig.hasKey("n.exclude"):
for excl in gConfig["n.exclude"].keys():
gExcludes.add(excl)
gExcludes.add(excl.addEnv())
if gConfig.hasKey("n.prepare"):
for prep in gConfig["n.prepare"].keys():
let (key, val) = getKey(prep)
if val == true:
let prepVal = gConfig["n.prepare"][prep]
if key == "download":
downloadUrl(gConfig["n.prepare"][prep])
downloadUrl(prepVal)
elif key == "extract":
extractZip(gConfig["n.prepare"][prep])
extractZip(prepVal)
elif key == "git":
gitRemotePull(gConfig["n.prepare"][prep])
gitRemotePull(prepVal)
elif key == "gitremote":
gitRemotePull(gConfig["n.prepare"][prep], false)
gitRemotePull(prepVal, false)
elif key == "gitsparse":
gitSparseCheckout(gConfig["n.prepare"][prep])
gitSparseCheckout(prepVal)
elif key == "execute":
discard execProc(gConfig["n.prepare"][prep])
discard execProc(prepVal)
elif key == "copy":
doCopy(gConfig["n.prepare"][prep])
doCopy(prepVal)
if gConfig.hasKey("n.wildcard"):
var wildcard = ""
@ -743,7 +793,8 @@ proc runCfg(cfg: string) =
if key == "wildcard":
wildcard = gConfig["n.wildcard"][wild]
else:
gWildcards.setSectionKey(wildcard, wild, gConfig["n.wildcard"][wild])
gWildcards.setSectionKey(wildcard, wild,
gConfig["n.wildcard"][wild])
for file in gConfig.keys():
if file in @["n.global", "n.include", "n.exclude", "n.prepare", "n.wildcard"]: