extccomp.nim 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132
  1. #
  2. #
  3. # The Nim Compiler
  4. # (c) Copyright 2013 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. # Module providing functions for calling the different external C compilers
  10. # Uses some hard-wired facts about each C/C++ compiler, plus options read
  11. # from a lineinfos file, to provide generalized procedures to compile
  12. # nim files.
  13. import ropes, platform, condsyms, options, msgs, lineinfos, pathutils, modulepaths
  14. import std/[os, osproc, streams, sequtils, times, strtabs, json, jsonutils, sugar, parseutils]
  15. import std / strutils except addf
  16. when defined(nimPreviewSlimSystem):
  17. import std/[syncio, assertions]
  18. import ../dist/checksums/src/checksums/sha1
  19. type
  20. TInfoCCProp* = enum # properties of the C compiler:
  21. hasSwitchRange, # CC allows ranges in switch statements (GNU C)
  22. hasComputedGoto, # CC has computed goto (GNU C extension)
  23. hasCpp, # CC is/contains a C++ compiler
  24. hasAssume, # CC has __assume (Visual C extension)
  25. hasGcGuard, # CC supports GC_GUARD to keep stack roots
  26. hasGnuAsm, # CC's asm uses the absurd GNU assembler syntax
  27. hasDeclspec, # CC has __declspec(X)
  28. hasAttribute, # CC has __attribute__((X))
  29. hasBuiltinUnreachable # CC has __builtin_unreachable
  30. TInfoCCProps* = set[TInfoCCProp]
  31. TInfoCC* = tuple[
  32. name: string, # the short name of the compiler
  33. objExt: string, # the compiler's object file extension
  34. optSpeed: string, # the options for optimization for speed
  35. optSize: string, # the options for optimization for size
  36. compilerExe: string, # the compiler's executable
  37. cppCompiler: string, # name of the C++ compiler's executable (if supported)
  38. compileTmpl: string, # the compile command template
  39. buildGui: string, # command to build a GUI application
  40. buildDll: string, # command to build a shared library
  41. buildLib: string, # command to build a static library
  42. linkerExe: string, # the linker's executable (if not matching compiler's)
  43. linkTmpl: string, # command to link files to produce an exe
  44. includeCmd: string, # command to add an include dir
  45. linkDirCmd: string, # command to add a lib dir
  46. linkLibCmd: string, # command to link an external library
  47. debug: string, # flags for debug build
  48. pic: string, # command for position independent code
  49. # used on some platforms
  50. asmStmtFrmt: string, # format of ASM statement
  51. structStmtFmt: string, # Format for struct statement
  52. produceAsm: string, # Format how to produce assembler listings
  53. cppXsupport: string, # what to do to enable C++X support
  54. props: TInfoCCProps] # properties of the C compiler
  55. # Configuration settings for various compilers.
  56. # When adding new compilers, the cmake sources could be a good reference:
  57. # https://cmake.org/gitweb?p=cmake.git;a=tree;f=Modules/Platform;
  58. template compiler(name, settings: untyped): untyped =
  59. proc name: TInfoCC {.compileTime.} = settings
  60. const
  61. gnuAsmListing = "-Wa,-acdl=$asmfile -g -fverbose-asm -masm=intel"
  62. # GNU C and C++ Compiler
  63. compiler gcc:
  64. result = (
  65. name: "gcc",
  66. objExt: "o",
  67. optSpeed: " -O3 -fno-ident",
  68. optSize: " -Os -fno-ident",
  69. compilerExe: "gcc",
  70. cppCompiler: "g++",
  71. compileTmpl: "-c $options $include -o $objfile $file",
  72. buildGui: " -mwindows",
  73. buildDll: " -shared",
  74. buildLib: "ar rcs $libfile $objfiles",
  75. linkerExe: "",
  76. linkTmpl: "$buildgui $builddll -o $exefile $objfiles $options",
  77. includeCmd: " -I",
  78. linkDirCmd: " -L",
  79. linkLibCmd: " -l$1",
  80. debug: "",
  81. pic: "-fPIC",
  82. asmStmtFrmt: "__asm__($1);$n",
  83. structStmtFmt: "$1 $3 $2 ", # struct|union [packed] $name
  84. produceAsm: gnuAsmListing,
  85. cppXsupport: "-std=gnu++17 -funsigned-char",
  86. props: {hasSwitchRange, hasComputedGoto, hasCpp, hasGcGuard, hasGnuAsm,
  87. hasAttribute, hasBuiltinUnreachable})
  88. # GNU C and C++ Compiler
  89. compiler nintendoSwitchGCC:
  90. result = (
  91. name: "switch_gcc",
  92. objExt: "o",
  93. optSpeed: " -O3 ",
  94. optSize: " -Os ",
  95. compilerExe: "aarch64-none-elf-gcc",
  96. cppCompiler: "aarch64-none-elf-g++",
  97. compileTmpl: "-w -MMD -MP -MF $dfile -c $options $include -o $objfile $file",
  98. buildGui: " -mwindows",
  99. buildDll: " -shared",
  100. buildLib: "aarch64-none-elf-gcc-ar rcs $libfile $objfiles",
  101. linkerExe: "aarch64-none-elf-gcc",
  102. linkTmpl: "$buildgui $builddll -Wl,-Map,$mapfile -o $exefile $objfiles $options",
  103. includeCmd: " -I",
  104. linkDirCmd: " -L",
  105. linkLibCmd: " -l$1",
  106. debug: "",
  107. pic: "-fPIE",
  108. asmStmtFrmt: "asm($1);$n",
  109. structStmtFmt: "$1 $3 $2 ", # struct|union [packed] $name
  110. produceAsm: gnuAsmListing,
  111. cppXsupport: "-std=gnu++17 -funsigned-char",
  112. props: {hasSwitchRange, hasComputedGoto, hasCpp, hasGcGuard, hasGnuAsm,
  113. hasAttribute, hasBuiltinUnreachable})
  114. # LLVM Frontend for GCC/G++
  115. compiler llvmGcc:
  116. result = gcc() # Uses settings from GCC
  117. result.name = "llvm_gcc"
  118. result.compilerExe = "llvm-gcc"
  119. result.cppCompiler = "llvm-g++"
  120. when defined(macosx) or defined(openbsd):
  121. # `llvm-ar` not available
  122. result.buildLib = "ar rcs $libfile $objfiles"
  123. else:
  124. result.buildLib = "llvm-ar rcs $libfile $objfiles"
  125. # Clang (LLVM) C/C++ Compiler
  126. compiler clang:
  127. result = llvmGcc() # Uses settings from llvmGcc
  128. result.name = "clang"
  129. result.compilerExe = "clang"
  130. result.cppCompiler = "clang++"
  131. # Microsoft Visual C/C++ Compiler
  132. compiler vcc:
  133. result = (
  134. name: "vcc",
  135. objExt: "obj",
  136. optSpeed: " /Ogityb2 ",
  137. optSize: " /O1 ",
  138. compilerExe: "cl",
  139. cppCompiler: "cl",
  140. compileTmpl: "/c$vccplatform $options $include /nologo /Fo$objfile $file",
  141. buildGui: " /SUBSYSTEM:WINDOWS user32.lib ",
  142. buildDll: " /LD",
  143. buildLib: "vccexe --command:lib$vccplatform /nologo /OUT:$libfile $objfiles",
  144. linkerExe: "cl",
  145. linkTmpl: "$builddll$vccplatform /Fe$exefile $objfiles $buildgui /nologo $options",
  146. includeCmd: " /I",
  147. # HACK: we call `cl` so we have to pass `/link` for linker options,
  148. # but users may still want to pass arguments to `cl` (see #14221)
  149. # to deal with this, we add `/link` before each `/LIBPATH`,
  150. # the linker ignores extra `/link`s since it's an unrecognized argument
  151. linkDirCmd: " /link /LIBPATH:",
  152. linkLibCmd: " $1.lib",
  153. debug: " /RTC1 /Z7 ",
  154. pic: "",
  155. asmStmtFrmt: "__asm{$n$1$n}$n",
  156. structStmtFmt: "$3$n$1 $2",
  157. produceAsm: "/Fa$asmfile",
  158. cppXsupport: "",
  159. props: {hasCpp, hasAssume, hasDeclspec})
  160. # Nvidia CUDA NVCC Compiler
  161. compiler nvcc:
  162. result = gcc()
  163. result.name = "nvcc"
  164. result.compilerExe = "nvcc"
  165. result.cppCompiler = "nvcc"
  166. result.compileTmpl = "-c -x cu -Xcompiler=\"$options\" $include -o $objfile $file"
  167. result.linkTmpl = "$buildgui $builddll -o $exefile $objfiles -Xcompiler=\"$options\""
  168. # AMD HIPCC Compiler (rocm/cuda)
  169. compiler hipcc:
  170. result = clang()
  171. result.name = "hipcc"
  172. result.compilerExe = "hipcc"
  173. result.cppCompiler = "hipcc"
  174. compiler clangcl:
  175. result = vcc()
  176. result.name = "clang_cl"
  177. result.compilerExe = "clang-cl"
  178. result.cppCompiler = "clang-cl"
  179. result.linkerExe = "clang-cl"
  180. result.linkTmpl = "-fuse-ld=lld " & result.linkTmpl
  181. # Intel C/C++ Compiler
  182. compiler icl:
  183. result = vcc()
  184. result.name = "icl"
  185. result.compilerExe = "icl"
  186. result.linkerExe = "icl"
  187. # Intel compilers try to imitate the native ones (gcc and msvc)
  188. compiler icc:
  189. result = gcc()
  190. result.name = "icc"
  191. result.compilerExe = "icc"
  192. result.linkerExe = "icc"
  193. # Borland C Compiler
  194. compiler bcc:
  195. result = (
  196. name: "bcc",
  197. objExt: "obj",
  198. optSpeed: " -O3 -6 ",
  199. optSize: " -O1 -6 ",
  200. compilerExe: "bcc32c",
  201. cppCompiler: "cpp32c",
  202. compileTmpl: "-c $options $include -o$objfile $file",
  203. buildGui: " -tW",
  204. buildDll: " -tWD",
  205. buildLib: "", # XXX: not supported yet
  206. linkerExe: "bcc32",
  207. linkTmpl: "$options $buildgui $builddll -e$exefile $objfiles",
  208. includeCmd: " -I",
  209. linkDirCmd: "", # XXX: not supported yet
  210. linkLibCmd: "", # XXX: not supported yet
  211. debug: "",
  212. pic: "",
  213. asmStmtFrmt: "__asm{$n$1$n}$n",
  214. structStmtFmt: "$1 $2",
  215. produceAsm: "",
  216. cppXsupport: "",
  217. props: {hasSwitchRange, hasComputedGoto, hasCpp, hasGcGuard,
  218. hasAttribute})
  219. # Tiny C Compiler
  220. compiler tcc:
  221. result = (
  222. name: "tcc",
  223. objExt: "o",
  224. optSpeed: "",
  225. optSize: "",
  226. compilerExe: "tcc",
  227. cppCompiler: "",
  228. compileTmpl: "-c $options $include -o $objfile $file",
  229. buildGui: "-Wl,-subsystem=gui",
  230. buildDll: " -shared",
  231. buildLib: "", # XXX: not supported yet
  232. linkerExe: "tcc",
  233. linkTmpl: "-o $exefile $options $buildgui $builddll $objfiles",
  234. includeCmd: " -I",
  235. linkDirCmd: " -L",
  236. linkLibCmd: " -l$1",
  237. debug: " -g ",
  238. pic: "",
  239. asmStmtFrmt: "asm($1);$n",
  240. structStmtFmt: "$1 $2",
  241. produceAsm: gnuAsmListing,
  242. cppXsupport: "",
  243. props: {hasSwitchRange, hasComputedGoto, hasGnuAsm})
  244. # Your C Compiler
  245. compiler envcc:
  246. result = (
  247. name: "env",
  248. objExt: "o",
  249. optSpeed: " -O3 ",
  250. optSize: " -O1 ",
  251. compilerExe: "",
  252. cppCompiler: "",
  253. compileTmpl: "-c $ccenvflags $options $include -o $objfile $file",
  254. buildGui: "",
  255. buildDll: " -shared ",
  256. buildLib: "", # XXX: not supported yet
  257. linkerExe: "",
  258. linkTmpl: "-o $exefile $buildgui $builddll $objfiles $options",
  259. includeCmd: " -I",
  260. linkDirCmd: "", # XXX: not supported yet
  261. linkLibCmd: "", # XXX: not supported yet
  262. debug: "",
  263. pic: "",
  264. asmStmtFrmt: "__asm{$n$1$n}$n",
  265. structStmtFmt: "$1 $2",
  266. produceAsm: "",
  267. cppXsupport: "",
  268. props: {hasGnuAsm})
  269. const
  270. CC*: array[succ(low(TSystemCC))..high(TSystemCC), TInfoCC] = [
  271. gcc(),
  272. nintendoSwitchGCC(),
  273. llvmGcc(),
  274. clang(),
  275. bcc(),
  276. vcc(),
  277. tcc(),
  278. envcc(),
  279. icl(),
  280. icc(),
  281. clangcl(),
  282. hipcc(),
  283. nvcc()]
  284. hExt* = ".h"
  285. template writePrettyCmdsStderr(cmd) =
  286. if cmd.len > 0:
  287. flushDot(conf)
  288. stderr.writeLine(cmd)
  289. proc nameToCC*(name: string): TSystemCC =
  290. ## Returns the kind of compiler referred to by `name`, or ccNone
  291. ## if the name doesn't refer to any known compiler.
  292. for i in succ(ccNone)..high(TSystemCC):
  293. if cmpIgnoreStyle(name, CC[i].name) == 0:
  294. return i
  295. result = ccNone
  296. proc listCCnames(): string =
  297. result = ""
  298. for i in succ(ccNone)..high(TSystemCC):
  299. if i > succ(ccNone): result.add ", "
  300. result.add CC[i].name
  301. proc isVSCompatible*(conf: ConfigRef): bool =
  302. return conf.cCompiler == ccVcc or
  303. conf.cCompiler == ccClangCl or
  304. (conf.cCompiler == ccIcl and conf.target.hostOS in osDos..osWindows)
  305. proc getConfigVar(conf: ConfigRef; c: TSystemCC, suffix: string): string =
  306. # use ``cpu.os.cc`` for cross compilation, unless ``--compileOnly`` is given
  307. # for niminst support
  308. var fullSuffix = suffix
  309. case conf.backend
  310. of backendCpp, backendJs, backendObjc: fullSuffix = "." & $conf.backend & suffix
  311. of backendC: discard
  312. of backendInvalid:
  313. # during parsing of cfg files; we don't know the backend yet, no point in
  314. # guessing wrong thing
  315. return ""
  316. if (conf.target.hostOS != conf.target.targetOS or conf.target.hostCPU != conf.target.targetCPU) and
  317. optCompileOnly notin conf.globalOptions:
  318. let fullCCname = platform.CPU[conf.target.targetCPU].name & '.' &
  319. platform.OS[conf.target.targetOS].name & '.' &
  320. CC[c].name & fullSuffix
  321. result = getConfigVar(conf, fullCCname)
  322. if existsConfigVar(conf, fullCCname):
  323. result = getConfigVar(conf, fullCCname)
  324. else:
  325. # not overridden for this cross compilation setting?
  326. result = getConfigVar(conf, CC[c].name & fullSuffix)
  327. else:
  328. result = getConfigVar(conf, CC[c].name & fullSuffix)
  329. proc setCC*(conf: ConfigRef; ccname: string; info: TLineInfo) =
  330. conf.cCompiler = nameToCC(ccname)
  331. if conf.cCompiler == ccNone:
  332. localError(conf, info, "unknown C compiler: '$1'. Available options are: $2" % [ccname, listCCnames()])
  333. conf.compileOptions = getConfigVar(conf, conf.cCompiler, ".options.always")
  334. conf.linkOptions = ""
  335. conf.cCompilerPath = getConfigVar(conf, conf.cCompiler, ".path")
  336. for c in CC: undefSymbol(conf.symbols, c.name)
  337. defineSymbol(conf.symbols, CC[conf.cCompiler].name)
  338. proc addOpt(dest: var string, src: string) =
  339. if dest.len == 0 or dest[^1] != ' ': dest.add(" ")
  340. dest.add(src)
  341. proc addLinkOption*(conf: ConfigRef; option: string) =
  342. addOpt(conf.linkOptions, option)
  343. proc addCompileOption*(conf: ConfigRef; option: string) =
  344. if strutils.find(conf.compileOptions, option, 0) < 0:
  345. addOpt(conf.compileOptions, option)
  346. proc addLinkOptionCmd*(conf: ConfigRef; option: string) =
  347. addOpt(conf.linkOptionsCmd, option)
  348. proc addCompileOptionCmd*(conf: ConfigRef; option: string) =
  349. conf.compileOptionsCmd.add(option)
  350. proc initVars*(conf: ConfigRef) =
  351. # we need to define the symbol here, because ``CC`` may have never been set!
  352. for c in CC: undefSymbol(conf.symbols, c.name)
  353. defineSymbol(conf.symbols, CC[conf.cCompiler].name)
  354. addCompileOption(conf, getConfigVar(conf, conf.cCompiler, ".options.always"))
  355. #addLinkOption(getConfigVar(cCompiler, ".options.linker"))
  356. if conf.cCompilerPath.len == 0:
  357. conf.cCompilerPath = getConfigVar(conf, conf.cCompiler, ".path")
  358. proc completeCfilePath*(conf: ConfigRef; cfile: AbsoluteFile,
  359. createSubDir: bool = true): AbsoluteFile =
  360. ## Generate the absolute file path to the generated modules.
  361. result = completeGeneratedFilePath(conf, cfile, createSubDir)
  362. proc toObjFile*(conf: ConfigRef; filename: AbsoluteFile): AbsoluteFile =
  363. # Object file for compilation
  364. result = AbsoluteFile(filename.string & "." & CC[conf.cCompiler].objExt)
  365. proc addFileToCompile*(conf: ConfigRef; cf: Cfile) =
  366. conf.toCompile.add(cf)
  367. proc addLocalCompileOption*(conf: ConfigRef; option: string; nimfile: AbsoluteFile) =
  368. let key = completeCfilePath(conf, mangleModuleName(conf, nimfile).AbsoluteFile).string
  369. var value = conf.cfileSpecificOptions.getOrDefault(key)
  370. if strutils.find(value, option, 0) < 0:
  371. addOpt(value, option)
  372. conf.cfileSpecificOptions[key] = value
  373. proc resetCompilationLists*(conf: ConfigRef) =
  374. conf.toCompile.setLen 0
  375. ## XXX: we must associate these with their originating module
  376. # when the module is loaded/unloaded it adds/removes its items
  377. # That's because we still need to hash check the external files
  378. # Maybe we can do that in checkDep on the other hand?
  379. conf.externalToLink.setLen 0
  380. proc addExternalFileToLink*(conf: ConfigRef; filename: AbsoluteFile) =
  381. conf.externalToLink.insert(filename.string, 0)
  382. proc execWithEcho(conf: ConfigRef; cmd: string, msg = hintExecuting): int =
  383. rawMessage(conf, msg, if msg == hintLinking and not(optListCmd in conf.globalOptions or conf.verbosity > 1): "" else: cmd)
  384. result = execCmd(cmd)
  385. proc execExternalProgram*(conf: ConfigRef; cmd: string, msg = hintExecuting) =
  386. if execWithEcho(conf, cmd, msg) != 0:
  387. rawMessage(conf, errGenerated, "execution of an external program failed: '$1'" %
  388. cmd)
  389. proc generateScript(conf: ConfigRef; script: Rope) =
  390. let (_, name, _) = splitFile(conf.outFile.string)
  391. let filename = getNimcacheDir(conf) / RelativeFile(addFileExt("compile_" & name,
  392. platform.OS[conf.target.targetOS].scriptExt))
  393. if not writeRope(script, filename):
  394. rawMessage(conf, errGenerated, "could not write to file: " & filename.string)
  395. proc getOptSpeed(conf: ConfigRef; c: TSystemCC): string =
  396. result = getConfigVar(conf, c, ".options.speed")
  397. if result == "":
  398. result = CC[c].optSpeed # use default settings from this file
  399. proc getDebug(conf: ConfigRef; c: TSystemCC): string =
  400. result = getConfigVar(conf, c, ".options.debug")
  401. if result == "":
  402. result = CC[c].debug # use default settings from this file
  403. proc getOptSize(conf: ConfigRef; c: TSystemCC): string =
  404. result = getConfigVar(conf, c, ".options.size")
  405. if result == "":
  406. result = CC[c].optSize # use default settings from this file
  407. proc noAbsolutePaths(conf: ConfigRef): bool {.inline.} =
  408. # We used to check current OS != specified OS, but this makes no sense
  409. # really: Cross compilation from Linux to Linux for example is entirely
  410. # reasonable.
  411. # `optGenMapping` is included here for niminst.
  412. # We use absolute paths for vcc / cl, see issue #19883.
  413. let options =
  414. if conf.cCompiler == ccVcc:
  415. {optGenMapping}
  416. else:
  417. {optGenScript, optGenMapping}
  418. result = conf.globalOptions * options != {}
  419. proc cFileSpecificOptions(conf: ConfigRef; nimname, fullNimFile: string): string =
  420. result = conf.compileOptions
  421. if (conf.cCompiler == ccGcc or conf.cCompiler == ccCLang) and
  422. conf.selectedGC == gcRefc:
  423. # bug #10625
  424. addOpt(result, "-fno-omit-frame-pointer")
  425. for option in conf.compileOptionsCmd:
  426. if strutils.find(result, option, 0) < 0:
  427. addOpt(result, option)
  428. if optCDebug in conf.globalOptions:
  429. let key = nimname & ".debug"
  430. if existsConfigVar(conf, key): addOpt(result, getConfigVar(conf, key))
  431. else: addOpt(result, getDebug(conf, conf.cCompiler))
  432. if optOptimizeSpeed in conf.options:
  433. let key = nimname & ".speed"
  434. if existsConfigVar(conf, key): addOpt(result, getConfigVar(conf, key))
  435. else: addOpt(result, getOptSpeed(conf, conf.cCompiler))
  436. elif optOptimizeSize in conf.options:
  437. let key = nimname & ".size"
  438. if existsConfigVar(conf, key): addOpt(result, getConfigVar(conf, key))
  439. else: addOpt(result, getOptSize(conf, conf.cCompiler))
  440. let key = nimname & ".always"
  441. if existsConfigVar(conf, key): addOpt(result, getConfigVar(conf, key))
  442. addOpt(result, conf.cfileSpecificOptions.getOrDefault(fullNimFile))
  443. proc getCompileOptions(conf: ConfigRef): string =
  444. result = cFileSpecificOptions(conf, "__dummy__", "__dummy__")
  445. proc vccplatform(conf: ConfigRef): string =
  446. # VCC specific but preferable over the config hacks people
  447. # had to do before, see #11306
  448. if conf.cCompiler == ccVcc:
  449. let exe = getConfigVar(conf, conf.cCompiler, ".exe")
  450. if "vccexe.exe" == extractFilename(exe):
  451. result = case conf.target.targetCPU
  452. of cpuI386: " --platform:x86"
  453. of cpuArm: " --platform:arm"
  454. of cpuAmd64: " --platform:amd64"
  455. else: ""
  456. else:
  457. result = ""
  458. else:
  459. result = ""
  460. proc getLinkOptions(conf: ConfigRef): string =
  461. result = conf.linkOptions & " " & conf.linkOptionsCmd & " "
  462. for linkedLib in items(conf.cLinkedLibs):
  463. result.add(CC[conf.cCompiler].linkLibCmd % linkedLib.quoteShell)
  464. for libDir in items(conf.cLibs):
  465. result.add(join([CC[conf.cCompiler].linkDirCmd, libDir.quoteShell]))
  466. proc needsExeExt(conf: ConfigRef): bool {.inline.} =
  467. result = (optGenScript in conf.globalOptions and conf.target.targetOS == osWindows) or
  468. (conf.target.hostOS == osWindows)
  469. proc useCpp(conf: ConfigRef; cfile: AbsoluteFile): bool =
  470. # List of possible file extensions taken from gcc
  471. for ext in [".C", ".cc", ".cpp", ".CPP", ".c++", ".cp", ".cxx"]:
  472. if cfile.string.endsWith(ext): return true
  473. false
  474. proc envFlags(conf: ConfigRef): string =
  475. result = if conf.backend == backendCpp:
  476. getEnv("CXXFLAGS")
  477. else:
  478. getEnv("CFLAGS")
  479. proc getCompilerExe(conf: ConfigRef; compiler: TSystemCC; isCpp: bool): string =
  480. if compiler == ccEnv:
  481. result = if isCpp:
  482. getEnv("CXX")
  483. else:
  484. getEnv("CC")
  485. else:
  486. result = if isCpp:
  487. CC[compiler].cppCompiler
  488. else:
  489. CC[compiler].compilerExe
  490. if result.len == 0:
  491. rawMessage(conf, errGenerated,
  492. "Compiler '$1' doesn't support the requested target" %
  493. CC[compiler].name)
  494. proc ccHasSaneOverflow*(conf: ConfigRef): bool =
  495. if conf.cCompiler == ccGcc:
  496. result = false # assume an old or crappy GCC
  497. var exe = getConfigVar(conf, conf.cCompiler, ".exe")
  498. if exe.len == 0: exe = CC[conf.cCompiler].compilerExe
  499. # NOTE: should we need the full version, use -dumpfullversion
  500. let (s, exitCode) = try: execCmdEx(exe & " -dumpversion") except IOError, OSError, ValueError: ("", 1)
  501. if exitCode == 0:
  502. var major: int = 0
  503. discard parseInt(s, major)
  504. result = major >= 5
  505. else:
  506. result = conf.cCompiler == ccCLang
  507. proc getLinkerExe(conf: ConfigRef; compiler: TSystemCC): string =
  508. result = if CC[compiler].linkerExe.len > 0: CC[compiler].linkerExe
  509. else: getCompilerExe(conf, compiler, optMixedMode in conf.globalOptions or conf.backend == backendCpp)
  510. proc getCompileCFileCmd*(conf: ConfigRef; cfile: Cfile,
  511. isMainFile = false; produceOutput = false): string =
  512. let
  513. c = conf.cCompiler
  514. isCpp = useCpp(conf, cfile.cname)
  515. # We produce files like module.nim.cpp, so the absolute Nim filename is not
  516. # cfile.name but `cfile.cname.changeFileExt("")`:
  517. var options = cFileSpecificOptions(conf, cfile.nimname, cfile.cname.changeFileExt("").string)
  518. if isCpp:
  519. # needs to be prepended so that --passc:-std=c++17 can override default.
  520. # we could avoid allocation by making cFileSpecificOptions inplace
  521. options = CC[c].cppXsupport & ' ' & options
  522. # If any C++ file was compiled, we need to use C++ driver for linking as well
  523. incl conf.globalOptions, optMixedMode
  524. var exe = getConfigVar(conf, c, ".exe")
  525. if exe.len == 0: exe = getCompilerExe(conf, c, isCpp)
  526. if needsExeExt(conf): exe = addFileExt(exe, "exe")
  527. if (optGenDynLib in conf.globalOptions or (conf.hcrOn and not isMainFile)) and
  528. ospNeedsPIC in platform.OS[conf.target.targetOS].props:
  529. options.add(' ' & CC[c].pic)
  530. if cfile.customArgs != "":
  531. options.add ' '
  532. options.add cfile.customArgs
  533. var compilePattern: string
  534. # compute include paths:
  535. var includeCmd = CC[c].includeCmd & quoteShell(conf.libpath)
  536. if not noAbsolutePaths(conf):
  537. for includeDir in items(conf.cIncludes):
  538. includeCmd.add(join([CC[c].includeCmd, includeDir.quoteShell]))
  539. compilePattern = joinPath(conf.cCompilerPath, exe)
  540. else:
  541. compilePattern = exe
  542. includeCmd.add(join([CC[c].includeCmd, quoteShell(conf.projectPath.string)]))
  543. let cf = if noAbsolutePaths(conf): AbsoluteFile extractFilename(cfile.cname.string)
  544. else: cfile.cname
  545. let objfile =
  546. if cfile.obj.isEmpty:
  547. if CfileFlag.External notin cfile.flags or noAbsolutePaths(conf):
  548. toObjFile(conf, cf).string
  549. else:
  550. completeCfilePath(conf, toObjFile(conf, cf)).string
  551. elif noAbsolutePaths(conf):
  552. extractFilename(cfile.obj.string)
  553. else:
  554. cfile.obj.string
  555. # D files are required by nintendo switch libs for
  556. # compilation. They are basically a list of all includes.
  557. let dfile = objfile.changeFileExt(".d").quoteShell
  558. let cfsh = quoteShell(cf)
  559. result = quoteShell(compilePattern % [
  560. "dfile", dfile,
  561. "file", cfsh, "objfile", quoteShell(objfile), "options", options,
  562. "include", includeCmd, "nim", getPrefixDir(conf).string,
  563. "lib", conf.libpath.string,
  564. "ccenvflags", envFlags(conf)])
  565. if optProduceAsm in conf.globalOptions:
  566. if CC[conf.cCompiler].produceAsm.len > 0:
  567. let asmfile = objfile.changeFileExt(".asm").quoteShell
  568. addOpt(result, CC[conf.cCompiler].produceAsm % ["asmfile", asmfile])
  569. if produceOutput:
  570. rawMessage(conf, hintUserRaw, "Produced assembler here: " & asmfile)
  571. else:
  572. if produceOutput:
  573. rawMessage(conf, hintUserRaw, "Couldn't produce assembler listing " &
  574. "for the selected C compiler: " & CC[conf.cCompiler].name)
  575. result.add(' ')
  576. strutils.addf(result, CC[c].compileTmpl, [
  577. "dfile", dfile,
  578. "file", cfsh, "objfile", quoteShell(objfile),
  579. "options", options, "include", includeCmd,
  580. "nim", quoteShell(getPrefixDir(conf)),
  581. "lib", quoteShell(conf.libpath),
  582. "vccplatform", vccplatform(conf),
  583. "ccenvflags", envFlags(conf)])
  584. proc footprint(conf: ConfigRef; cfile: Cfile): SecureHash =
  585. result = secureHash(
  586. $secureHashFile(cfile.cname.string) &
  587. platform.OS[conf.target.targetOS].name &
  588. platform.CPU[conf.target.targetCPU].name &
  589. extccomp.CC[conf.cCompiler].name &
  590. getCompileCFileCmd(conf, cfile))
  591. proc externalFileChanged(conf: ConfigRef; cfile: Cfile): bool =
  592. if conf.backend == backendJs: return false # pre-existing behavior, but not sure it's good
  593. let hashFile = toGeneratedFile(conf, conf.mangleModuleName(cfile.cname).AbsoluteFile, "sha1")
  594. let currentHash = footprint(conf, cfile)
  595. var f: File = default(File)
  596. if open(f, hashFile.string, fmRead):
  597. let oldHash = parseSecureHash(f.readLine())
  598. close(f)
  599. result = oldHash != currentHash
  600. else:
  601. result = true
  602. if result:
  603. if open(f, hashFile.string, fmWrite):
  604. f.writeLine($currentHash)
  605. close(f)
  606. proc addExternalFileToCompile*(conf: ConfigRef; c: var Cfile) =
  607. # we want to generate the hash file unconditionally
  608. let extFileChanged = externalFileChanged(conf, c)
  609. if optForceFullMake notin conf.globalOptions and fileExists(c.obj) and
  610. not extFileChanged:
  611. c.flags.incl CfileFlag.Cached
  612. else:
  613. # make sure Nim keeps recompiling the external file on reruns
  614. # if compilation is not successful
  615. discard tryRemoveFile(c.obj.string)
  616. conf.toCompile.add(c)
  617. proc addExternalFileToCompile*(conf: ConfigRef; filename: AbsoluteFile) =
  618. var c = Cfile(nimname: splitFile(filename).name, cname: filename,
  619. obj: toObjFile(conf, completeCfilePath(conf, filename, false)),
  620. flags: {CfileFlag.External})
  621. addExternalFileToCompile(conf, c)
  622. proc getLinkCmd(conf: ConfigRef; output: AbsoluteFile,
  623. objfiles: string, isDllBuild: bool, removeStaticFile: bool): string =
  624. if optGenStaticLib in conf.globalOptions:
  625. if removeStaticFile:
  626. removeFile output # fixes: bug #16947
  627. result = CC[conf.cCompiler].buildLib % ["libfile", quoteShell(output),
  628. "objfiles", objfiles,
  629. "vccplatform", vccplatform(conf)]
  630. else:
  631. var linkerExe = getConfigVar(conf, conf.cCompiler, ".linkerexe")
  632. if linkerExe.len == 0: linkerExe = getLinkerExe(conf, conf.cCompiler)
  633. # bug #6452: We must not use ``quoteShell`` here for ``linkerExe``
  634. if needsExeExt(conf): linkerExe = addFileExt(linkerExe, "exe")
  635. if noAbsolutePaths(conf): result = linkerExe
  636. else: result = joinPath(conf.cCompilerPath, linkerExe)
  637. let buildgui = if optGenGuiApp in conf.globalOptions and conf.target.targetOS == osWindows:
  638. CC[conf.cCompiler].buildGui
  639. else:
  640. ""
  641. let builddll = if isDllBuild: CC[conf.cCompiler].buildDll else: ""
  642. let exefile = quoteShell(output)
  643. when false:
  644. if optCDebug in conf.globalOptions:
  645. writeDebugInfo(exefile.changeFileExt("ndb"))
  646. # Map files are required by Nintendo Switch compilation. They are a list
  647. # of all function calls in the library and where they come from.
  648. let mapfile = quoteShell(getNimcacheDir(conf) / RelativeFile(splitFile(output).name & ".map"))
  649. let linkOptions = getLinkOptions(conf) & " " &
  650. getConfigVar(conf, conf.cCompiler, ".options.linker")
  651. var linkTmpl = getConfigVar(conf, conf.cCompiler, ".linkTmpl")
  652. if linkTmpl.len == 0:
  653. linkTmpl = CC[conf.cCompiler].linkTmpl
  654. result = quoteShell(result % ["builddll", builddll,
  655. "mapfile", mapfile,
  656. "buildgui", buildgui, "options", linkOptions, "objfiles", objfiles,
  657. "exefile", exefile, "nim", getPrefixDir(conf).string, "lib", conf.libpath.string])
  658. result.add ' '
  659. strutils.addf(result, linkTmpl, ["builddll", builddll,
  660. "mapfile", mapfile,
  661. "buildgui", buildgui, "options", linkOptions,
  662. "objfiles", objfiles, "exefile", exefile,
  663. "nim", quoteShell(getPrefixDir(conf)),
  664. "lib", quoteShell(conf.libpath),
  665. "vccplatform", vccplatform(conf)])
  666. # On windows the debug information for binaries is emitted in a separate .pdb
  667. # file and the binaries (.dll and .exe) contain a full path to that .pdb file.
  668. # This is a problem for hot code reloading because even when we copy the .dll
  669. # and load the copy so the build process may overwrite the original .dll on
  670. # the disk (windows locks the files of running binaries) the copy still points
  671. # to the original .pdb (and a simple copy of the .pdb won't help). This is a
  672. # problem when a debugger is attached to the program we are hot-reloading.
  673. # This problem is nonexistent on Unix since there by default debug symbols
  674. # are embedded in the binaries so loading a copy of a .so will be fine. There
  675. # is the '/Z7' flag for the MSVC compiler to embed the debug info of source
  676. # files into their respective .obj files but the linker still produces a .pdb
  677. # when a final .dll or .exe is linked so the debug info isn't embedded.
  678. # There is also the issue that even when a .dll is unloaded the debugger
  679. # still keeps the .pdb for that .dll locked. This is a major problem and
  680. # because of this we cannot just alternate between 2 names for a .pdb file
  681. # when rebuilding a .dll - instead we need to accumulate differently named
  682. # .pdb files in the nimcache folder - this is the easiest and most reliable
  683. # way of being able to debug and rebuild the program at the same time. This
  684. # is accomplished using the /PDB:<filename> flag (there also exists the
  685. # /PDBALTPATH:<filename> flag). The only downside is that the .pdb files are
  686. # at least 300kb big (when linking statically to the runtime - or else 5mb+)
  687. # and will quickly accumulate. There is a hacky solution: we could try to
  688. # delete all .pdb files with a pattern and swallow exceptions.
  689. #
  690. # links about .pdb files and hot code reloading:
  691. # https://ourmachinery.com/post/dll-hot-reloading-in-theory-and-practice/
  692. # https://ourmachinery.com/post/little-machines-working-together-part-2/
  693. # https://github.com/fungos/cr
  694. # https://fungos.github.io/blog/2017/11/20/cr.h-a-simple-c-hot-reload-header-only-library/
  695. # on forcing the debugger to unlock a locked .pdb of an unloaded library:
  696. # https://blog.molecular-matters.com/2017/05/09/deleting-pdb-files-locked-by-visual-studio/
  697. # and a bit about the .pdb format in case that is ever needed:
  698. # https://github.com/crosire/blink
  699. # https://www.debuginfo.com/articles/debuginfomatch.html#pdbfiles
  700. if conf.hcrOn and isVSCompatible(conf):
  701. let t = now()
  702. let pdb = output.string & "." & format(t, "MMMM-yyyy-HH-mm-") & $t.nanosecond & ".pdb"
  703. result.add " /link /PDB:" & pdb
  704. if optCDebug in conf.globalOptions and conf.cCompiler == ccVcc:
  705. result.add " /Zi /FS /Od"
  706. template getLinkCmd(conf: ConfigRef; output: AbsoluteFile, objfiles: string,
  707. removeStaticFile = false): string =
  708. getLinkCmd(conf, output, objfiles, optGenDynLib in conf.globalOptions, removeStaticFile)
  709. template tryExceptOSErrorMessage(conf: ConfigRef; errorPrefix: string = "", body: untyped) =
  710. try:
  711. body
  712. except OSError:
  713. let ose = (ref OSError)(getCurrentException())
  714. if errorPrefix.len > 0:
  715. rawMessage(conf, errGenerated, errorPrefix & " " & ose.msg & " " & $ose.errorCode)
  716. else:
  717. rawMessage(conf, errGenerated, "execution of an external program failed: '$1'" %
  718. (ose.msg & " " & $ose.errorCode))
  719. raise
  720. proc getExtraCmds(conf: ConfigRef; output: AbsoluteFile): seq[string] =
  721. result = @[]
  722. when defined(macosx):
  723. if optCDebug in conf.globalOptions and optGenStaticLib notin conf.globalOptions:
  724. # if needed, add an option to skip or override location
  725. result.add "dsymutil " & $(output).quoteShell
  726. proc execLinkCmd(conf: ConfigRef; linkCmd: string) =
  727. tryExceptOSErrorMessage(conf, "invocation of external linker program failed."):
  728. execExternalProgram(conf, linkCmd, hintLinking)
  729. proc execCmdsInParallel(conf: ConfigRef; cmds: seq[string]; prettyCb: proc (idx: int)) =
  730. let runCb = proc (idx: int, p: Process) =
  731. let exitCode = p.peekExitCode
  732. if exitCode != 0:
  733. rawMessage(conf, errGenerated, "execution of an external compiler program '" &
  734. cmds[idx] & "' failed with exit code: " & $exitCode & "\n\n")
  735. if conf.numberOfProcessors == 0: conf.numberOfProcessors = countProcessors()
  736. var res = 0
  737. if conf.numberOfProcessors <= 1:
  738. for i in 0..high(cmds):
  739. tryExceptOSErrorMessage(conf, "invocation of external compiler program failed."):
  740. res = execWithEcho(conf, cmds[i])
  741. if res != 0:
  742. rawMessage(conf, errGenerated, "execution of an external program failed: '$1'" %
  743. cmds[i])
  744. else:
  745. tryExceptOSErrorMessage(conf, "invocation of external compiler program failed."):
  746. res = execProcesses(cmds, {poStdErrToStdOut, poUsePath, poParentStreams},
  747. conf.numberOfProcessors, prettyCb, afterRunEvent=runCb)
  748. if res != 0:
  749. if conf.numberOfProcessors <= 1:
  750. rawMessage(conf, errGenerated, "execution of an external program failed: '$1'" %
  751. cmds.join())
  752. proc linkViaResponseFile(conf: ConfigRef; cmd: string) =
  753. # Extracting the linker.exe here is a bit hacky but the best solution
  754. # given ``buildLib``'s design.
  755. var i = 0
  756. var last = 0
  757. if cmd.len > 0 and cmd[0] == '"':
  758. inc i
  759. while i < cmd.len and cmd[i] != '"': inc i
  760. last = i
  761. inc i
  762. else:
  763. while i < cmd.len and cmd[i] != ' ': inc i
  764. last = i
  765. while i < cmd.len and cmd[i] == ' ': inc i
  766. let linkerArgs = conf.projectName & "_" & "linkerArgs.txt"
  767. let args = cmd.substr(i)
  768. # GCC's response files don't support backslashes. Junk.
  769. if conf.cCompiler == ccGcc or conf.cCompiler == ccCLang:
  770. writeFile(linkerArgs, args.replace('\\', '/'))
  771. else:
  772. writeFile(linkerArgs, args)
  773. try:
  774. when defined(macosx):
  775. execLinkCmd(conf, "xargs " & cmd.substr(0, last) & " < " & linkerArgs)
  776. else:
  777. execLinkCmd(conf, cmd.substr(0, last) & " @" & linkerArgs)
  778. finally:
  779. removeFile(linkerArgs)
  780. proc linkViaShellScript(conf: ConfigRef; cmd: string) =
  781. let linkerScript = conf.projectName & "_" & "linkerScript.sh"
  782. writeFile(linkerScript, cmd)
  783. let shell = getEnv("SHELL")
  784. try:
  785. execLinkCmd(conf, shell & " " & linkerScript)
  786. finally:
  787. removeFile(linkerScript)
  788. proc getObjFilePath(conf: ConfigRef, f: Cfile): string =
  789. if noAbsolutePaths(conf): f.obj.extractFilename
  790. else: f.obj.string
  791. proc hcrLinkTargetName(conf: ConfigRef, objFile: string, isMain = false): AbsoluteFile =
  792. let basename = splitFile(objFile).name
  793. let targetName = if isMain: basename & ".exe"
  794. else: platform.OS[conf.target.targetOS].dllFrmt % basename
  795. result = conf.getNimcacheDir / RelativeFile(targetName)
  796. proc displayProgressCC(conf: ConfigRef, path, compileCmd: string): string =
  797. result = ""
  798. if conf.hasHint(hintCC):
  799. if optListCmd in conf.globalOptions or conf.verbosity > 1:
  800. result = MsgKindToStr[hintCC] % (demangleModuleName(path.splitFile.name) & ": " & compileCmd)
  801. else:
  802. result = MsgKindToStr[hintCC] % demangleModuleName(path.splitFile.name)
  803. proc preventLinkCmdMaxCmdLen(conf: ConfigRef, linkCmd: string) =
  804. # Prevent linkcmd from exceeding the maximum command line length.
  805. # Windows's command line limit is about 8K (8191 characters) so C compilers on
  806. # Windows support a feature where the command line can be passed via ``@linkcmd``
  807. # to them.
  808. const MaxCmdLen = when defined(windows): 8_000 elif defined(macosx): 260_000 else: 32_000
  809. if linkCmd.len > MaxCmdLen:
  810. when defined(macosx):
  811. linkViaShellScript(conf, linkCmd)
  812. else:
  813. linkViaResponseFile(conf, linkCmd)
  814. else:
  815. execLinkCmd(conf, linkCmd)
  816. proc callCCompiler*(conf: ConfigRef) =
  817. var
  818. linkCmd: string = ""
  819. extraCmds: seq[string]
  820. if conf.globalOptions * {optCompileOnly, optGenScript} == {optCompileOnly}:
  821. return # speed up that call if only compiling and no script shall be
  822. # generated
  823. #var c = cCompiler
  824. var script: Rope = ""
  825. var cmds: TStringSeq = default(TStringSeq)
  826. var prettyCmds: TStringSeq = default(TStringSeq)
  827. let prettyCb = proc (idx: int) = writePrettyCmdsStderr(prettyCmds[idx])
  828. for idx, it in conf.toCompile:
  829. # call the C compiler for the .c file:
  830. if CfileFlag.Cached in it.flags: continue
  831. let compileCmd = getCompileCFileCmd(conf, it, idx == conf.toCompile.len - 1, produceOutput=true)
  832. if optCompileOnly notin conf.globalOptions:
  833. cmds.add(compileCmd)
  834. prettyCmds.add displayProgressCC(conf, $it.cname, compileCmd)
  835. if optGenScript in conf.globalOptions:
  836. script.add(compileCmd)
  837. script.add("\n")
  838. if optCompileOnly notin conf.globalOptions:
  839. execCmdsInParallel(conf, cmds, prettyCb)
  840. if optNoLinking notin conf.globalOptions:
  841. # call the linker:
  842. var objfiles = ""
  843. for it in conf.externalToLink:
  844. let objFile = if noAbsolutePaths(conf): it.extractFilename else: it
  845. objfiles.add(' ')
  846. objfiles.add(quoteShell(
  847. addFileExt(objFile, CC[conf.cCompiler].objExt)))
  848. if conf.hcrOn: # lets assume that optCompileOnly isn't on
  849. cmds = @[]
  850. let mainFileIdx = conf.toCompile.len - 1
  851. for idx, x in conf.toCompile:
  852. # don't relink each of the many binaries (one for each source file) if the nim code is
  853. # cached because that would take too much time for small changes - the only downside to
  854. # this is that if an external-to-link file changes the final target wouldn't be relinked
  855. if CfileFlag.Cached in x.flags: continue
  856. # we pass each object file as if it is the project file - a .dll will be created for each such
  857. # object file in the nimcache directory, and only in the case of the main project file will
  858. # there be probably an executable (if the project is such) which will be copied out of the nimcache
  859. let objFile = conf.getObjFilePath(x)
  860. let buildDll = idx != mainFileIdx
  861. let linkTarget = conf.hcrLinkTargetName(objFile, not buildDll)
  862. cmds.add(getLinkCmd(conf, linkTarget, objfiles & " " & quoteShell(objFile), buildDll, removeStaticFile = true))
  863. # try to remove all .pdb files for the current binary so they don't accumulate endlessly in the nimcache
  864. # for more info check the comment inside of getLinkCmd() where the /PDB:<filename> MSVC flag is used
  865. if isVSCompatible(conf):
  866. for pdb in walkFiles(objFile & ".*.pdb"):
  867. discard tryRemoveFile(pdb)
  868. # execute link commands in parallel - output will be a bit different
  869. # if it fails than that from execLinkCmd() but that doesn't matter
  870. prettyCmds = map(prettyCmds, proc (curr: string): string = return curr.replace("CC", "Link"))
  871. execCmdsInParallel(conf, cmds, prettyCb)
  872. # only if not cached - copy the resulting main file from the nimcache folder to its originally intended destination
  873. if CfileFlag.Cached notin conf.toCompile[mainFileIdx].flags:
  874. let mainObjFile = getObjFilePath(conf, conf.toCompile[mainFileIdx])
  875. let src = conf.hcrLinkTargetName(mainObjFile, true)
  876. let dst = conf.prepareToWriteOutput
  877. copyFileWithPermissions(src.string, dst.string)
  878. else:
  879. for x in conf.toCompile:
  880. let objFile = if noAbsolutePaths(conf): x.obj.extractFilename else: x.obj.string
  881. objfiles.add(' ')
  882. objfiles.add(quoteShell(objFile))
  883. let mainOutput = if optGenScript notin conf.globalOptions: conf.prepareToWriteOutput
  884. else: AbsoluteFile(conf.outFile)
  885. linkCmd = getLinkCmd(conf, mainOutput, objfiles, removeStaticFile = true)
  886. extraCmds = getExtraCmds(conf, mainOutput)
  887. if optCompileOnly notin conf.globalOptions:
  888. preventLinkCmdMaxCmdLen(conf, linkCmd)
  889. for cmd in extraCmds:
  890. execExternalProgram(conf, cmd, hintExecuting)
  891. else:
  892. linkCmd = ""
  893. if optGenScript in conf.globalOptions:
  894. script.add(linkCmd)
  895. script.add("\n")
  896. generateScript(conf, script)
  897. template hashNimExe(): string = $secureHashFile(os.getAppFilename())
  898. proc jsonBuildInstructionsFile*(conf: ConfigRef): AbsoluteFile =
  899. # `outFile` is better than `projectName`, as it allows having different json
  900. # files for a given source file compiled with different options; it also
  901. # works out of the box with `hashMainCompilationParams`.
  902. result = getNimcacheDir(conf) / conf.outFile.changeFileExt("json")
  903. const cacheVersion = "D20240927T193831" # update when `BuildCache` spec changes
  904. type BuildCache = object
  905. cacheVersion: string
  906. outputFile: string
  907. outputLastModificationTime: string
  908. compile: seq[(string, string)]
  909. link: seq[string]
  910. linkcmd: string
  911. extraCmds: seq[string]
  912. configFiles: seq[string] # the hash shouldn't be needed
  913. stdinInput: bool
  914. projectIsCmd: bool
  915. cmdInput: string
  916. currentDir: string
  917. cmdline: string
  918. depfiles: seq[(string, string)]
  919. nimexe: string
  920. proc writeJsonBuildInstructions*(conf: ConfigRef; deps: StringTableRef) =
  921. var linkFiles = collect(for it in conf.externalToLink:
  922. var it = it
  923. if conf.noAbsolutePaths: it = it.extractFilename
  924. it.addFileExt(CC[conf.cCompiler].objExt))
  925. for it in conf.toCompile: linkFiles.add it.obj.string
  926. var bcache = BuildCache(
  927. cacheVersion: cacheVersion,
  928. outputFile: conf.absOutFile.string,
  929. compile: collect(for i, it in conf.toCompile:
  930. if CfileFlag.Cached notin it.flags: (it.cname.string, getCompileCFileCmd(conf, it))),
  931. link: linkFiles,
  932. linkcmd: getLinkCmd(conf, conf.absOutFile, linkFiles.quoteShellCommand),
  933. extraCmds: getExtraCmds(conf, conf.absOutFile),
  934. stdinInput: conf.projectIsStdin,
  935. projectIsCmd: conf.projectIsCmd,
  936. cmdInput: conf.cmdInput,
  937. configFiles: conf.configFiles.mapIt(it.string),
  938. currentDir: getCurrentDir())
  939. if optRun in conf.globalOptions or isDefined(conf, "nimBetterRun"):
  940. bcache.cmdline = conf.commandLine
  941. for it in conf.m.fileInfos:
  942. let path = it.fullPath.string
  943. if isAbsolute(path): # TODO: else?
  944. if path in deps:
  945. bcache.depfiles.add (path, deps[path])
  946. else: # backup for configs etc.
  947. bcache.depfiles.add (path, $secureHashFile(path))
  948. bcache.nimexe = hashNimExe()
  949. if fileExists(bcache.outputFile):
  950. bcache.outputLastModificationTime = $getLastModificationTime(bcache.outputFile)
  951. conf.jsonBuildFile = conf.jsonBuildInstructionsFile
  952. conf.jsonBuildFile.string.writeFile(bcache.toJson.pretty)
  953. proc changeDetectedViaJsonBuildInstructions*(conf: ConfigRef; jsonFile: AbsoluteFile): bool =
  954. result = false
  955. if not fileExists(jsonFile) or not fileExists(conf.absOutFile): return true
  956. var bcache: BuildCache = default(BuildCache)
  957. try: bcache.fromJson(jsonFile.string.parseFile)
  958. except IOError, OSError, ValueError:
  959. stderr.write "Warning: JSON processing failed for: $#\n" % jsonFile.string
  960. return true
  961. if bcache.currentDir != getCurrentDir() or # fixes bug #16271
  962. bcache.configFiles != conf.configFiles.mapIt(it.string) or
  963. bcache.cacheVersion != cacheVersion or bcache.outputFile != conf.absOutFile.string or
  964. bcache.cmdline != conf.commandLine or bcache.nimexe != hashNimExe() or
  965. bcache.projectIsCmd != conf.projectIsCmd or conf.cmdInput != bcache.cmdInput: return true
  966. if bcache.stdinInput or conf.projectIsStdin: return true
  967. # xxx optimize by returning false if stdin input was the same
  968. for (file, hash) in bcache.depfiles:
  969. if $secureHashFile(file) != hash: return true
  970. if bcache.outputLastModificationTime != $getLastModificationTime(bcache.outputFile):
  971. return true
  972. proc runJsonBuildInstructions*(conf: ConfigRef; jsonFile: AbsoluteFile) =
  973. var bcache: BuildCache = default(BuildCache)
  974. try: bcache.fromJson(jsonFile.string.parseFile)
  975. except ValueError, KeyError, JsonKindError:
  976. let e = getCurrentException()
  977. conf.quitOrRaise "\ncaught exception:\n$#\nstacktrace:\n$#error evaluating JSON file: $#" %
  978. [e.msg, e.getStackTrace(), jsonFile.string]
  979. let output = bcache.outputFile
  980. createDir output.parentDir
  981. let outputCurrent = $conf.absOutFile
  982. if output != outputCurrent or bcache.cacheVersion != cacheVersion:
  983. globalError(conf, gCmdLineInfo,
  984. "jsonscript command outputFile '$1' must match '$2' which was specified during --compileOnly, see \"outputFile\" entry in '$3' " %
  985. [outputCurrent, output, jsonFile.string])
  986. var cmds: TStringSeq = default(TStringSeq)
  987. var prettyCmds: TStringSeq = default(TStringSeq)
  988. let prettyCb = proc (idx: int) = writePrettyCmdsStderr(prettyCmds[idx])
  989. for (name, cmd) in bcache.compile:
  990. cmds.add cmd
  991. prettyCmds.add displayProgressCC(conf, name, cmd)
  992. execCmdsInParallel(conf, cmds, prettyCb)
  993. preventLinkCmdMaxCmdLen(conf, bcache.linkcmd)
  994. for cmd in bcache.extraCmds: execExternalProgram(conf, cmd, hintExecuting)
  995. proc genMappingFiles(conf: ConfigRef; list: CfileList): Rope =
  996. result = ""
  997. for it in list:
  998. result.addf("--file:r\"$1\"$N", [rope(it.cname.string)])
  999. proc writeMapping*(conf: ConfigRef; symbolMapping: Rope) =
  1000. if optGenMapping notin conf.globalOptions: return
  1001. var code = rope("[C_Files]\n")
  1002. code.add(genMappingFiles(conf, conf.toCompile))
  1003. code.add("\n[C_Compiler]\nFlags=")
  1004. code.add(strutils.escape(getCompileOptions(conf)))
  1005. code.add("\n[Linker]\nFlags=")
  1006. code.add(strutils.escape(getLinkOptions(conf) & " " &
  1007. getConfigVar(conf, conf.cCompiler, ".options.linker")))
  1008. code.add("\n[Environment]\nlibpath=")
  1009. code.add(strutils.escape(conf.libpath.string))
  1010. code.addf("\n[Symbols]$n$1", [symbolMapping])
  1011. let filename = conf.projectPath / RelativeFile"mapping.txt"
  1012. if not writeRope(code, filename):
  1013. rawMessage(conf, errGenerated, "could not write to file: " & filename.string)