compiler/backend/extccomp

    Dark Mode
Search:
  Source   Edit

Module providing functions for calling the different external C compilers Uses some hard-wired facts about each C compiler, plus options read from a lineinfos file, to provide generalized procedures to compile nim files.

Types

TInfoCC = object
  name*: string              ## the short name of the compiler
  objExt*: string            ## the compiler's object file extension
  optSpeed*: string          ## the options for optimization for speed
  optSize*: string           ## the options for optimization for size
  compilerExe*: string       ## the compiler's executable
  compileTmpl*: string       ## the compile command template
  buildGui*: string          ## command to build a GUI application
  buildDll*: string          ## command to build a shared library
  buildLib*: string          ## command to build a static library
  linkerExe*: string         ## the linker's executable (if not matching compiler's)
  linkTmpl*: string          ## command to link files to produce an exe
  includeCmd*: string        ## command to add an include dir
  linkDirCmd*: string        ## command to add a lib dir
  linkLibCmd*: string        ## command to link an external library
  debug*: string             ## flags for debug build
  pic*: string               ## command for position independent code
                             ## used on some platforms
  asmStmtFrmt*: string       ## format of ASM statement
  structStmtFmt*: string     ## Format for struct statement
  produceAsm*: string        ## Format how to produce assembler listings
  props*: TInfoCCProps       ## properties of the C compiler
  
  Source   Edit
TInfoCCProp = enum
  hasSwitchRange,           ## CC allows ranges in switch statements (GNU C)
  hasComputedGoto,          ## CC has computed goto (GNU C extension)
  hasAssume,                ## CC has `__assume` (Visual C extension)
  hasGcGuard,               ## CC supports GC_GUARD to keep stack roots
  hasGnuAsm,                ## CC's asm uses the absurd GNU assembler syntax
  hasDeclspec,              ## CC has `__declspec(X)`
  hasAttribute               ## CC has `__attribute__((X))`
properties of the C compiler:   Source   Edit

Consts

CC: array[succ(low(TSystemCC)) .. high(TSystemCC), TInfoCC] = [(name: "gcc",
    objExt: "o", optSpeed: " -O3 -fno-ident", optSize: " -Os -fno-ident",
    compilerExe: "gcc", compileTmpl: "-c $options $include -o $objfile $file",
    buildGui: " -mwindows", buildDll: " -shared",
    buildLib: "ar rcs $libfile $objfiles", linkerExe: "",
    linkTmpl: "$buildgui $builddll -o $exefile $objfiles $options",
    includeCmd: " -I", linkDirCmd: " -L", linkLibCmd: " -l$1", debug: "",
    pic: "-fPIC", asmStmtFrmt: "asm($1);$n", structStmtFmt: "$1 $3 $2 ",
    produceAsm: "-Wa,-acdl=$asmfile -g -fverbose-asm -masm=intel", props: {
    hasSwitchRange..hasComputedGoto, hasGcGuard..hasGnuAsm, hasAttribute}), (
    name: "switch_gcc", objExt: "o", optSpeed: " -O3 ", optSize: " -Os ",
    compilerExe: "aarch64-none-elf-gcc", compileTmpl: "-w -MMD -MP -MF $dfile -c $options $include -o $objfile $file",
    buildGui: " -mwindows", buildDll: " -shared",
    buildLib: "aarch64-none-elf-gcc-ar rcs $libfile $objfiles",
    linkerExe: "aarch64-none-elf-gcc", linkTmpl: "$buildgui $builddll -Wl,-Map,$mapfile -o $exefile $objfiles $options",
    includeCmd: " -I", linkDirCmd: " -L", linkLibCmd: " -l$1", debug: "",
    pic: "-fPIE", asmStmtFrmt: "asm($1);$n", structStmtFmt: "$1 $3 $2 ",
    produceAsm: "-Wa,-acdl=$asmfile -g -fverbose-asm -masm=intel", props: {
    hasSwitchRange..hasComputedGoto, hasGcGuard..hasGnuAsm, hasAttribute}), (
    name: "llvm_gcc", objExt: "o", optSpeed: " -O3 -fno-ident",
    optSize: " -Os -fno-ident", compilerExe: "llvm-gcc",
    compileTmpl: "-c $options $include -o $objfile $file",
    buildGui: " -mwindows", buildDll: " -shared",
    buildLib: "llvm-ar rcs $libfile $objfiles", linkerExe: "",
    linkTmpl: "$buildgui $builddll -o $exefile $objfiles $options",
    includeCmd: " -I", linkDirCmd: " -L", linkLibCmd: " -l$1", debug: "",
    pic: "-fPIC", asmStmtFrmt: "asm($1);$n", structStmtFmt: "$1 $3 $2 ",
    produceAsm: "-Wa,-acdl=$asmfile -g -fverbose-asm -masm=intel", props: {
    hasSwitchRange..hasComputedGoto, hasGcGuard..hasGnuAsm, hasAttribute}), (
    name: "clang", objExt: "o", optSpeed: " -O3 -fno-ident",
    optSize: " -Os -fno-ident", compilerExe: "clang",
    compileTmpl: "-c $options $include -o $objfile $file",
    buildGui: " -mwindows", buildDll: " -shared",
    buildLib: "llvm-ar rcs $libfile $objfiles", linkerExe: "",
    linkTmpl: "$buildgui $builddll -o $exefile $objfiles $options",
    includeCmd: " -I", linkDirCmd: " -L", linkLibCmd: " -l$1", debug: "",
    pic: "-fPIC", asmStmtFrmt: "asm($1);$n", structStmtFmt: "$1 $3 $2 ",
    produceAsm: "-Wa,-acdl=$asmfile -g -fverbose-asm -masm=intel", props: {
    hasSwitchRange..hasComputedGoto, hasGcGuard..hasGnuAsm, hasAttribute}), (
    name: "bcc", objExt: "obj", optSpeed: " -O3 -6 ", optSize: " -O1 -6 ",
    compilerExe: "bcc32c", compileTmpl: "-c $options $include -o$objfile $file",
    buildGui: " -tW", buildDll: " -tWD", buildLib: "", linkerExe: "bcc32",
    linkTmpl: "$options $buildgui $builddll -e$exefile $objfiles",
    includeCmd: " -I", linkDirCmd: "", linkLibCmd: "", debug: "", pic: "",
    asmStmtFrmt: "__asm{$n$1$n}$n", structStmtFmt: "$1 $2", produceAsm: "",
    props: {hasSwitchRange..hasComputedGoto, hasGcGuard, hasAttribute}), (
    name: "vcc", objExt: "obj", optSpeed: " /Ogityb2 ", optSize: " /O1 ",
    compilerExe: "cl",
    compileTmpl: "/c$vccplatform $options $include /nologo /Fo$objfile $file",
    buildGui: " /SUBSYSTEM:WINDOWS user32.lib ", buildDll: " /LD",
    buildLib: "lib /OUT:$libfile $objfiles", linkerExe: "cl", linkTmpl: "$builddll$vccplatform /Fe$exefile $objfiles $buildgui /nologo $options",
    includeCmd: " /I", linkDirCmd: " /LIBPATH:", linkLibCmd: " $1.lib",
    debug: " /RTC1 /Z7 ", pic: "", asmStmtFrmt: "__asm{$n$1$n}$n",
    structStmtFmt: "$3$n$1 $2", produceAsm: "/Fa$asmfile",
    props: {hasAssume, hasDeclspec}), (name: "tcc", objExt: "o", optSpeed: "",
                                       optSize: "", compilerExe: "tcc", compileTmpl: "-c $options $include -o $objfile $file",
                                       buildGui: "-Wl,-subsystem=gui",
                                       buildDll: " -shared", buildLib: "",
                                       linkerExe: "tcc", linkTmpl: "-o $exefile $options $buildgui $builddll $objfiles",
                                       includeCmd: " -I", linkDirCmd: "",
                                       linkLibCmd: "", debug: " -g ", pic: "",
                                       asmStmtFrmt: "asm($1);$n",
                                       structStmtFmt: "$1 $2", produceAsm: "-Wa,-acdl=$asmfile -g -fverbose-asm -masm=intel", props: {
    hasSwitchRange..hasComputedGoto, hasGnuAsm}), (name: "env", objExt: "o",
    optSpeed: " -O3 ", optSize: " -O1 ", compilerExe: "",
    compileTmpl: "-c $ccenvflags $options $include -o $objfile $file",
    buildGui: "", buildDll: " -shared ", buildLib: "", linkerExe: "",
    linkTmpl: "-o $exefile $buildgui $builddll $objfiles $options",
    includeCmd: " -I", linkDirCmd: "", linkLibCmd: "", debug: "", pic: "",
    asmStmtFrmt: "__asm{$n$1$n}$n", structStmtFmt: "$1 $2", produceAsm: "",
    props: {hasGnuAsm}), (name: "icl", objExt: "obj", optSpeed: " /Ogityb2 ",
                          optSize: " /O1 ", compilerExe: "icl", compileTmpl: "/c$vccplatform $options $include /nologo /Fo$objfile $file",
                          buildGui: " /SUBSYSTEM:WINDOWS user32.lib ",
                          buildDll: " /LD",
                          buildLib: "lib /OUT:$libfile $objfiles",
                          linkerExe: "icl", linkTmpl: "$builddll$vccplatform /Fe$exefile $objfiles $buildgui /nologo $options",
                          includeCmd: " /I", linkDirCmd: " /LIBPATH:",
                          linkLibCmd: " $1.lib", debug: " /RTC1 /Z7 ", pic: "",
                          asmStmtFrmt: "__asm{$n$1$n}$n",
                          structStmtFmt: "$3$n$1 $2", produceAsm: "/Fa$asmfile",
                          props: {hasAssume, hasDeclspec}), (name: "icc",
    objExt: "o", optSpeed: " -O3 -fno-ident", optSize: " -Os -fno-ident",
    compilerExe: "icc", compileTmpl: "-c $options $include -o $objfile $file",
    buildGui: " -mwindows", buildDll: " -shared",
    buildLib: "ar rcs $libfile $objfiles", linkerExe: "icc",
    linkTmpl: "$buildgui $builddll -o $exefile $objfiles $options",
    includeCmd: " -I", linkDirCmd: " -L", linkLibCmd: " -l$1", debug: "",
    pic: "-fPIC", asmStmtFrmt: "asm($1);$n", structStmtFmt: "$1 $3 $2 ",
    produceAsm: "-Wa,-acdl=$asmfile -g -fverbose-asm -masm=intel", props: {
    hasSwitchRange..hasComputedGoto, hasGcGuard..hasGnuAsm, hasAttribute}), (
    name: "clang_cl", objExt: "obj", optSpeed: " /Ogityb2 ", optSize: " /O1 ",
    compilerExe: "clang-cl",
    compileTmpl: "/c$vccplatform $options $include /nologo /Fo$objfile $file",
    buildGui: " /SUBSYSTEM:WINDOWS user32.lib ", buildDll: " /LD",
    buildLib: "lib /OUT:$libfile $objfiles", linkerExe: "clang-cl", linkTmpl: "-fuse-ld=lld $builddll$vccplatform /Fe$exefile $objfiles $buildgui /nologo $options",
    includeCmd: " /I", linkDirCmd: " /LIBPATH:", linkLibCmd: " $1.lib",
    debug: " /RTC1 /Z7 ", pic: "", asmStmtFrmt: "__asm{$n$1$n}$n",
    structStmtFmt: "$3$n$1 $2", produceAsm: "/Fa$asmfile",
    props: {hasAssume, hasDeclspec})]
  Source   Edit
hExt = ".h"
  Source   Edit

Procs

proc addCompileOption(conf: ConfigRef; option: string) {....raises: [], tags: [].}
  Source   Edit
proc addCompileOptionCmd(conf: ConfigRef; option: string) {....raises: [], tags: [].}
  Source   Edit
proc addExternalFileToCompile(conf: ConfigRef; c: var Cfile) {....raises: [OSError,
    KeyError, Exception, IOError, ERecoverableError, ValueError, EOFError], tags: [
    ReadEnvEffect, ReadIOEffect, ReadDirEffect, RootEffect, WriteDirEffect,
    WriteIOEffect].}
  Source   Edit
proc addExternalFileToCompile(conf: ConfigRef; filename: AbsoluteFile) {....raises: [
    OSError, IOError, KeyError, Exception, ERecoverableError, ValueError,
    EOFError], tags: [ReadEnvEffect, ReadIOEffect, WriteDirEffect,
                      ReadDirEffect, RootEffect, WriteIOEffect].}
  Source   Edit
proc addExternalFileToLink(conf: ConfigRef; filename: AbsoluteFile) {.
    ...raises: [], tags: [].}
Add new file to be linked with every compiled target.   Source   Edit
proc addFileToCompile(conf: ConfigRef; cf: Cfile) {....raises: [], tags: [].}
  Source   Edit
proc addLinkOption(conf: ConfigRef; option: string) {....raises: [], tags: [].}
  Source   Edit
proc addLinkOptionCmd(conf: ConfigRef; option: string) {....raises: [], tags: [].}
  Source   Edit
proc addLocalCompileOption(conf: ConfigRef; option: string;
                           nimfile: AbsoluteFile) {.
    ...raises: [OSError, IOError, KeyError, Exception], tags: [ReadEnvEffect,
    ReadIOEffect, WriteDirEffect, ReadDirEffect, RootEffect].}
  Source   Edit
proc callCCompiler(conf: ConfigRef) {....raises: [Exception, ERecoverableError,
    OSError, IOError, ValueError], tags: [ReadEnvEffect, RootEffect,
    ReadIOEffect, WriteDirEffect, ReadDirEffect, ExecIOEffect, TimeEffect,
    WriteIOEffect].}
  Source   Edit
proc ccHasSaneOverflow(conf: ConfigRef): bool {....raises: [],
    tags: [ExecIOEffect, ReadIOEffect, RootEffect].}
  Source   Edit
proc changeDetectedViaJsonBuildInstructions(conf: ConfigRef;
    jsonFile: AbsoluteFile): bool {....raises: [OSError, Exception, IOError,
    ValueError], tags: [ReadDirEffect, ReadIOEffect, WriteIOEffect].}
  Source   Edit
proc completeCfilePath(conf: ConfigRef; cfile: AbsoluteFile;
                       createSubDir: bool = true): AbsoluteFile {.
    ...raises: [OSError, IOError],
    tags: [ReadEnvEffect, ReadIOEffect, WriteDirEffect, ReadDirEffect].}
  Source   Edit
proc externalObjFile(conf: ConfigRef; cfile: AbsoluteFile): AbsoluteFile {.
    ...raises: [OSError, IOError],
    tags: [ReadEnvEffect, ReadIOEffect, WriteDirEffect, ReadDirEffect].}
Returns the path of the object file to output for the external C file cfile.   Source   Edit
proc getCompileCFileCmd(conf: ConfigRef; cfile: Cfile; isMainFile = false;
                        produceOutput = false): string {.
    ...raises: [Exception, ERecoverableError, OSError, IOError, ValueError], tags: [
    ReadEnvEffect, RootEffect, ReadIOEffect, WriteDirEffect, ReadDirEffect].}
  Source   Edit
proc initVars(conf: ConfigRef) {....raises: [], tags: [].}
  Source   Edit
proc isVSCompatible(conf: ConfigRef): bool {....raises: [], tags: [].}
  Source   Edit
proc jsonBuildInstructionsFile(conf: ConfigRef): AbsoluteFile {.
    ...raises: [OSError], tags: [ReadEnvEffect, ReadIOEffect].}
  Source   Edit
proc listCCnames(): seq[string] {....raises: [], tags: [].}
  Source   Edit
proc nameToCC(name: string): TSystemCC {....raises: [], tags: [].}
Returns the kind of compiler referred to by name, or ccNone if the name doesn't refer to any known compiler.   Source   Edit
proc resetCompilationLists(conf: ConfigRef) {....raises: [], tags: [].}
  Source   Edit
proc runJsonBuildInstructions(conf: ConfigRef; jsonFile: AbsoluteFile) {.
    ...raises: [OSError, IOError, Exception, ERecoverableError], tags: [
    ReadIOEffect, WriteIOEffect, WriteDirEffect, ReadDirEffect, RootEffect,
    ExecIOEffect, TimeEffect, ReadEnvEffect].}
  Source   Edit
proc setCC(conf: ConfigRef; ccname: string): TSystemCC {....raises: [], tags: [].}
returns the compiler that was set, or ccNone in case there was an error   Source   Edit
proc toObjFile(conf: ConfigRef; filename: AbsoluteFile): AbsoluteFile {.
    ...raises: [], tags: [].}
  Source   Edit
proc writeJsonBuildInstructions(conf: ConfigRef) {.
    ...raises: [OSError, Exception, ERecoverableError, IOError, ValueError], tags: [
    ReadDirEffect, ReadEnvEffect, RootEffect, ReadIOEffect, WriteDirEffect,
    WriteIOEffect].}
  Source   Edit
proc writeMapping(conf: ConfigRef; symbolMapping: Rope) {.
    ...raises: [ValueError, OSError, IOError, Exception, ERecoverableError],
    tags: [ReadEnvEffect, ReadIOEffect, WriteIOEffect, RootEffect].}
  Source   Edit