aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorgenotrance <dev@genotrance.com>2018-07-09 15:40:33 -0500
committerGitHub <noreply@github.com>2018-07-09 15:40:33 -0500
commit84894bfa0c2eb82e719c73a512c07d0ca595aff1 (patch)
tree2120bdd87dcad7fabf0ef0e143c2609b8c6b4fcb
parentadaa7e997f34e3374c60c72206cd61f396f95075 (diff)
parent8e8e5fbd09de38207d13ac0e79b4716308b1d828 (diff)
downloadnimgen-84894bfa0c2eb82e719c73a512c07d0ca595aff1.tar.gz
nimgen-84894bfa0c2eb82e719c73a512c07d0ca595aff1.zip
Merge pull request #4 from jyapayne/configurable_compiler
Add configurable compiler to global section
-rw-r--r--README.md15
-rw-r--r--nimgen.nim75
2 files changed, 78 insertions, 12 deletions
diff --git a/README.md b/README.md
index 1e95d0a..40f7556 100644
--- a/README.md
+++ b/README.md
@@ -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:-
diff --git a/nimgen.nim b/nimgen.nim
index 328dd2d..c132fe1 100644
--- a/nimgen.nim
+++ b/nimgen.nim
@@ -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"]: