added a new feature: --cc:env so that you can use any C compiler as long as it works like GCC

This commit is contained in:
Andreas Rumpf 2020-05-06 17:14:38 +02:00
commit c64db68f0b
3 changed files with 31 additions and 13 deletions

View file

@ -240,15 +240,15 @@ compiler tcc:
props: {hasSwitchRange, hasComputedGoto, hasGnuAsm}) props: {hasSwitchRange, hasComputedGoto, hasGnuAsm})
# Your C Compiler # Your C Compiler
compiler ucc: compiler envcc:
result = ( result = (
name: "ucc", name: "env",
objExt: "o", objExt: "o",
optSpeed: " -O3 ", optSpeed: " -O3 ",
optSize: " -O1 ", optSize: " -O1 ",
compilerExe: "cc", compilerExe: "",
cppCompiler: "", cppCompiler: "",
compileTmpl: "-c $options $include -o $objfile $file", compileTmpl: "-c $ccenvflags $options $include -o $objfile $file",
buildGui: "", buildGui: "",
buildDll: " -shared ", buildDll: " -shared ",
buildLib: "", # XXX: not supported yet buildLib: "", # XXX: not supported yet
@ -263,7 +263,7 @@ compiler ucc:
structStmtFmt: "$1 $2", structStmtFmt: "$1 $2",
produceAsm: "", produceAsm: "",
cppXsupport: "", cppXsupport: "",
props: {}) props: {hasGnuAsm})
const const
CC*: array[succ(low(TSystemCC))..high(TSystemCC), TInfoCC] = [ CC*: array[succ(low(TSystemCC))..high(TSystemCC), TInfoCC] = [
@ -274,7 +274,7 @@ const
bcc(), bcc(),
vcc(), vcc(),
tcc(), tcc(),
ucc(), envcc(),
icl(), icl(),
icc(), icc(),
clangcl()] clangcl()]
@ -483,11 +483,23 @@ proc needsExeExt(conf: ConfigRef): bool {.inline.} =
proc useCpp(conf: ConfigRef; cfile: AbsoluteFile): bool = proc useCpp(conf: ConfigRef; cfile: AbsoluteFile): bool =
conf.cmd == cmdCompileToCpp and not cfile.string.endsWith(".c") conf.cmd == cmdCompileToCpp and not cfile.string.endsWith(".c")
proc envFlags(conf: ConfigRef): string =
result = if conf.cmd == cmdCompileToCpp:
getEnv("CXXFLAGS")
else:
getEnv("CFLAGS")
proc getCompilerExe(conf: ConfigRef; compiler: TSystemCC; cfile: AbsoluteFile): string = proc getCompilerExe(conf: ConfigRef; compiler: TSystemCC; cfile: AbsoluteFile): string =
result = if useCpp(conf, cfile): if compiler == ccEnv:
CC[compiler].cppCompiler result = if useCpp(conf, cfile):
else: getEnv("CXX")
CC[compiler].compilerExe else:
getEnv("CC")
else:
result = if useCpp(conf, cfile):
CC[compiler].cppCompiler
else:
CC[compiler].compilerExe
if result.len == 0: if result.len == 0:
rawMessage(conf, errGenerated, rawMessage(conf, errGenerated,
"Compiler '$1' doesn't support the requested target" % "Compiler '$1' doesn't support the requested target" %
@ -581,7 +593,8 @@ proc getCompileCFileCmd*(conf: ConfigRef; cfile: Cfile,
"dfile", dfile, "dfile", dfile,
"file", cfsh, "objfile", quoteShell(objfile), "options", options, "file", cfsh, "objfile", quoteShell(objfile), "options", options,
"include", includeCmd, "nim", getPrefixDir(conf).string, "include", includeCmd, "nim", getPrefixDir(conf).string,
"lib", conf.libpath.string]) "lib", conf.libpath.string,
"ccenvflags", envFlags(conf)])
if optProduceAsm in conf.globalOptions: if optProduceAsm in conf.globalOptions:
if CC[conf.cCompiler].produceAsm.len > 0: if CC[conf.cCompiler].produceAsm.len > 0:
@ -601,7 +614,8 @@ proc getCompileCFileCmd*(conf: ConfigRef; cfile: Cfile,
"options", options, "include", includeCmd, "options", options, "include", includeCmd,
"nim", quoteShell(getPrefixDir(conf)), "nim", quoteShell(getPrefixDir(conf)),
"lib", quoteShell(conf.libpath), "lib", quoteShell(conf.libpath),
"vccplatform", vccplatform(conf)]) "vccplatform", vccplatform(conf),
"ccenvflags", envFlags(conf)])
proc footprint(conf: ConfigRef; cfile: Cfile): SecureHash = proc footprint(conf: ConfigRef; cfile: Cfile): SecureHash =
result = secureHash( result = secureHash(

View file

@ -165,7 +165,7 @@ type
TSystemCC* = enum TSystemCC* = enum
ccNone, ccGcc, ccNintendoSwitch, ccLLVM_Gcc, ccCLang, ccBcc, ccVcc, ccNone, ccGcc, ccNintendoSwitch, ccLLVM_Gcc, ccCLang, ccBcc, ccVcc,
ccTcc, ccUcc, ccIcl, ccIcc, ccClangCl ccTcc, ccEnv, ccIcl, ccIcc, ccClangCl
ExceptionSystem* = enum ExceptionSystem* = enum
excNone, # no exception system selected yet excNone, # no exception system selected yet

View file

@ -240,6 +240,10 @@ add the ``-f`` flag to force all files to be recompiled.
The default compiler is defined at the top of ``config\nim.cfg``. The default compiler is defined at the top of ``config\nim.cfg``.
Changing this setting affects the compiler used by ``koch`` to (re)build Nim. Changing this setting affects the compiler used by ``koch`` to (re)build Nim.
To use the ``CC`` environment variable, use ``nim c --cc:env myfile.nim``. To use the
``CXX`` environment variable, use ``nim cpp --cc:env myfile.nim``. ``--cc:env`` is available
since Nim version 1.4.
Cross compilation Cross compilation
================= =================