improve the compiler option "cppCompileToNamespace", a custom namespace can now be set

This commit is contained in:
Giovanni 2018-09-15 20:06:26 +09:00
commit f1b64e4b96
5 changed files with 22 additions and 10 deletions

View file

@ -679,8 +679,10 @@ proc generateHeaders(m: BModule) =
add(m.s[cfsHeaders], "#undef powerpc\L") add(m.s[cfsHeaders], "#undef powerpc\L")
add(m.s[cfsHeaders], "#undef unix\L") add(m.s[cfsHeaders], "#undef unix\L")
proc openNamespaceNim(): Rope = proc openNamespaceNim(namespace: string): Rope =
result.add("namespace Nim {\L") result.add("namespace ")
result.add(namespace)
result.add(" {\L")
proc closeNamespaceNim(): Rope = proc closeNamespaceNim(): Rope =
result.add("}\L") result.add("}\L")
@ -1036,7 +1038,10 @@ proc addIntTypes(result: var Rope; conf: ConfigRef) {.inline.} =
addf(result, "#define NIM_NEW_MANGLING_RULES\L" & addf(result, "#define NIM_NEW_MANGLING_RULES\L" &
"#define NIM_INTBITS $1\L", [ "#define NIM_INTBITS $1\L", [
platform.CPU[conf.target.targetCPU].intSize.rope]) platform.CPU[conf.target.targetCPU].intSize.rope])
if optUseNimNamespace in conf.globalOptions: result.add("#define USE_NIM_NAMESPACE\L") if optUseNimNamespace in conf.globalOptions:
result.add("#define USE_NIM_NAMESPACE ")
result.add(conf.cppCustomNamespace)
result.add("\L")
proc getCopyright(conf: ConfigRef; cfile: Cfile): Rope = proc getCopyright(conf: ConfigRef; cfile: Cfile): Rope =
if optCompileOnly in conf.globalOptions: if optCompileOnly in conf.globalOptions:
@ -1210,10 +1215,10 @@ proc genMainProc(m: BModule) =
[m.g.mainModInit, initStackBottomCall, rope(m.labels)]) [m.g.mainModInit, initStackBottomCall, rope(m.labels)])
if optNoMain notin m.config.globalOptions: if optNoMain notin m.config.globalOptions:
if optUseNimNamespace in m.config.globalOptions: if optUseNimNamespace in m.config.globalOptions:
m.s[cfsProcs].add closeNamespaceNim() & "using namespace Nim;\L" m.s[cfsProcs].add closeNamespaceNim() & "using namespace " & m.config.cppCustomNamespace & ";\L"
appcg(m, m.s[cfsProcs], otherMain, []) appcg(m, m.s[cfsProcs], otherMain, [])
if optUseNimNamespace in m.config.globalOptions: m.s[cfsProcs].add openNamespaceNim() if optUseNimNamespace in m.config.globalOptions: m.s[cfsProcs].add openNamespaceNim(m.config.cppCustomNamespace)
proc getSomeInitName(m: PSym, suffix: string): Rope = proc getSomeInitName(m: PSym, suffix: string): Rope =
assert m.kind == skModule assert m.kind == skModule
@ -1336,7 +1341,7 @@ proc genModule(m: BModule, cfile: Cfile): Rope =
add(result, m.s[i]) add(result, m.s[i])
add(result, genSectionEnd(i, m.config)) add(result, genSectionEnd(i, m.config))
if optUseNimNamespace in m.config.globalOptions and i == cfsHeaders: if optUseNimNamespace in m.config.globalOptions and i == cfsHeaders:
result.add openNamespaceNim() result.add openNamespaceNim(m.config.cppCustomNamespace)
add(result, m.s[cfsInitProc]) add(result, m.s[cfsInitProc])
if optUseNimNamespace in m.config.globalOptions: result.add closeNamespaceNim() if optUseNimNamespace in m.config.globalOptions: result.add closeNamespaceNim()
@ -1469,7 +1474,7 @@ proc writeHeader(m: BModule) =
add(result, genSectionStart(i, m.config)) add(result, genSectionStart(i, m.config))
add(result, m.s[i]) add(result, m.s[i])
add(result, genSectionEnd(i, m.config)) add(result, genSectionEnd(i, m.config))
if optUseNimNamespace in m.config.globalOptions and i == cfsHeaders: result.add openNamespaceNim() if optUseNimNamespace in m.config.globalOptions and i == cfsHeaders: result.add openNamespaceNim(m.config.cppCustomNamespace)
add(result, m.s[cfsInitProc]) add(result, m.s[cfsInitProc])
if optGenDynLib in m.config.globalOptions: if optGenDynLib in m.config.globalOptions:

View file

@ -735,7 +735,11 @@ proc processSwitch*(switch, arg: string, pass: TCmdLinePass, info: TLineInfo;
of "nep1": of "nep1":
processOnOffSwitchG(conf, {optCheckNep1}, arg, pass, info) processOnOffSwitchG(conf, {optCheckNep1}, arg, pass, info)
of "cppcompiletonamespace": of "cppcompiletonamespace":
expectNoArg(conf, switch, arg, pass, info) if conf != nil:
if arg != "":
conf.cppCustomNamespace = arg
else:
conf.cppCustomNamespace = "Nim"
incl conf.globalOptions, optUseNimNamespace incl conf.globalOptions, optUseNimNamespace
defineSymbol(conf.symbols, "cppCompileToNamespace") defineSymbol(conf.symbols, "cppCompileToNamespace")
else: else:

View file

@ -242,6 +242,7 @@ type
writelnHook*: proc (output: string) {.closure.} writelnHook*: proc (output: string) {.closure.}
structuredErrorHook*: proc (config: ConfigRef; info: TLineInfo; msg: string; structuredErrorHook*: proc (config: ConfigRef; info: TLineInfo; msg: string;
severity: Severity) {.closure.} severity: Severity) {.closure.}
cppCustomNamespace*: string
template depConfigFields*(fn) {.dirty.} = template depConfigFields*(fn) {.dirty.} =
fn(target) fn(target)

View file

@ -81,7 +81,9 @@ Advanced options:
--NimblePath:PATH add a path for Nimble support --NimblePath:PATH add a path for Nimble support
--noNimblePath deactivate the Nimble path --noNimblePath deactivate the Nimble path
--noCppExceptions use default exception handling with C++ backend --noCppExceptions use default exception handling with C++ backend
--cppCompileToNamespace use namespace "Nim" for the generated C++ code --cppCompileToNamespace:namespace
use the provided namespace for the generated C++ code,
if no namespace is provided "Nim" will be used
--excludePath:PATH exclude a path from the list of search paths --excludePath:PATH exclude a path from the list of search paths
--dynlibOverride:SYMBOL marks SYMBOL so that dynlib:SYMBOL --dynlibOverride:SYMBOL marks SYMBOL so that dynlib:SYMBOL
has no effect and can be statically linked instead; has no effect and can be statically linked instead;

View file

@ -268,7 +268,7 @@ __clang__
/* wrap all Nim typedefs into namespace Nim */ /* wrap all Nim typedefs into namespace Nim */
#ifdef USE_NIM_NAMESPACE #ifdef USE_NIM_NAMESPACE
namespace Nim { namespace USE_NIM_NAMESPACE {
#endif #endif
/* bool types (C++ has it): */ /* bool types (C++ has it): */