* fix #18578

* add tests

* tiny

* apply changes

* typo

* add removeStaticFile
This commit is contained in:
flywind 2021-07-27 13:21:39 +08:00 • committed by GitHub
commit 9cb5ab0108
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 6 deletions

View file

@ -665,8 +665,9 @@ proc addExternalFileToCompile*(conf: ConfigRef; filename: AbsoluteFile) =
addExternalFileToCompile(conf, c) addExternalFileToCompile(conf, c)
proc getLinkCmd(conf: ConfigRef; output: AbsoluteFile, proc getLinkCmd(conf: ConfigRef; output: AbsoluteFile,
objfiles: string, isDllBuild: bool): string = objfiles: string, isDllBuild: bool, removeStaticFile: bool): string =
if optGenStaticLib in conf.globalOptions: if optGenStaticLib in conf.globalOptions:
if removeStaticFile:
removeFile output # fixes: bug #16947 removeFile output # fixes: bug #16947
result = CC[conf.cCompiler].buildLib % ["libfile", quoteShell(output), result = CC[conf.cCompiler].buildLib % ["libfile", quoteShell(output),
"objfiles", objfiles] "objfiles", objfiles]
@ -750,8 +751,9 @@ proc getLinkCmd(conf: ConfigRef; output: AbsoluteFile,
if optCDebug in conf.globalOptions and conf.cCompiler == ccVcc: if optCDebug in conf.globalOptions and conf.cCompiler == ccVcc:
result.add " /Zi /FS /Od" result.add " /Zi /FS /Od"
template getLinkCmd(conf: ConfigRef; output: AbsoluteFile, objfiles: string): string = template getLinkCmd(conf: ConfigRef; output: AbsoluteFile, objfiles: string,
getLinkCmd(conf, output, objfiles, optGenDynLib in conf.globalOptions) removeStaticFile = false): string =
getLinkCmd(conf, output, objfiles, optGenDynLib in conf.globalOptions, removeStaticFile)
template tryExceptOSErrorMessage(conf: ConfigRef; errorPrefix: string = "", body: untyped) = template tryExceptOSErrorMessage(conf: ConfigRef; errorPrefix: string = "", body: untyped) =
try: try:
@ -891,7 +893,7 @@ proc callCCompiler*(conf: ConfigRef) =
let objFile = conf.getObjFilePath(x) let objFile = conf.getObjFilePath(x)
let buildDll = idx != mainFileIdx let buildDll = idx != mainFileIdx
let linkTarget = conf.hcrLinkTargetName(objFile, not buildDll) let linkTarget = conf.hcrLinkTargetName(objFile, not buildDll)
cmds.add(getLinkCmd(conf, linkTarget, objfiles & " " & quoteShell(objFile), buildDll)) cmds.add(getLinkCmd(conf, linkTarget, objfiles & " " & quoteShell(objFile), buildDll, removeStaticFile = true))
# try to remove all .pdb files for the current binary so they don't accumulate endlessly in the nimcache # try to remove all .pdb files for the current binary so they don't accumulate endlessly in the nimcache
# for more info check the comment inside of getLinkCmd() where the /PDB:<filename> MSVC flag is used # for more info check the comment inside of getLinkCmd() where the /PDB:<filename> MSVC flag is used
if isVSCompatible(conf): if isVSCompatible(conf):
@ -914,7 +916,8 @@ proc callCCompiler*(conf: ConfigRef) =
objfiles.add(quoteShell(objFile)) objfiles.add(quoteShell(objFile))
let mainOutput = if optGenScript notin conf.globalOptions: conf.prepareToWriteOutput let mainOutput = if optGenScript notin conf.globalOptions: conf.prepareToWriteOutput
else: AbsoluteFile(conf.projectName) else: AbsoluteFile(conf.projectName)
linkCmd = getLinkCmd(conf, mainOutput, objfiles)
linkCmd = getLinkCmd(conf, mainOutput, objfiles, removeStaticFile = true)
extraCmds = getExtraCmds(conf, mainOutput) extraCmds = getExtraCmds(conf, mainOutput)
if optCompileOnly notin conf.globalOptions: if optCompileOnly notin conf.globalOptions:
const MaxCmdLen = when defined(windows): 8_000 else: 32_000 const MaxCmdLen = when defined(windows): 8_000 else: 32_000

View file

@ -0,0 +1 @@
echo 1234

View file

@ -0,0 +1,22 @@
import std/[os, osproc, strformat]
const dir = "tests/compiles"
const fileName = dir / "mstaticlib.nim"
const nim = getCurrentCompilerExe()
block: # bug #18578
const libName = dir / "tstaticlib1.a"
let (_, status) = execCmdEx(fmt"{nim} c -o:{libName} --app:staticlib {fileName}")
doAssert status == 0
doAssert fileExists(libName)
removeFile(libName)
block: # bug #16947
const libName = dir / "tstaticlib2.a"
writeFile(libName, "echo 124")
doAssert fileExists(libName)
let (_, status) = execCmdEx(fmt"{nim} c -o:{libName} --app:staticlib {fileName}")
doAssert status == 0
doAssert fileExists(libName)
removeFile(libName)