Skip to content

Commit

Permalink
fix #18578 (#18580)
Browse files Browse the repository at this point in the history
* fix #18578

* add tests

* tiny

* apply changes

* typo

* add removeStaticFile
  • Loading branch information
ringabout authored Jul 27, 2021
1 parent 22776c4 commit 9cb5ab0
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
15 changes: 9 additions & 6 deletions compiler/extccomp.nim
Original file line number Diff line number Diff line change
Expand Up @@ -665,9 +665,10 @@ proc addExternalFileToCompile*(conf: ConfigRef; filename: AbsoluteFile) =
addExternalFileToCompile(conf, c)

proc getLinkCmd(conf: ConfigRef; output: AbsoluteFile,
objfiles: string, isDllBuild: bool): string =
objfiles: string, isDllBuild: bool, removeStaticFile: bool): string =
if optGenStaticLib in conf.globalOptions:
removeFile output # fixes: bug #16947
if removeStaticFile:
removeFile output # fixes: bug #16947
result = CC[conf.cCompiler].buildLib % ["libfile", quoteShell(output),
"objfiles", objfiles]
else:
Expand Down Expand Up @@ -750,8 +751,9 @@ proc getLinkCmd(conf: ConfigRef; output: AbsoluteFile,
if optCDebug in conf.globalOptions and conf.cCompiler == ccVcc:
result.add " /Zi /FS /Od"

template getLinkCmd(conf: ConfigRef; output: AbsoluteFile, objfiles: string): string =
getLinkCmd(conf, output, objfiles, optGenDynLib in conf.globalOptions)
template getLinkCmd(conf: ConfigRef; output: AbsoluteFile, objfiles: string,
removeStaticFile = false): string =
getLinkCmd(conf, output, objfiles, optGenDynLib in conf.globalOptions, removeStaticFile)

template tryExceptOSErrorMessage(conf: ConfigRef; errorPrefix: string = "", body: untyped) =
try:
Expand Down Expand Up @@ -891,7 +893,7 @@ proc callCCompiler*(conf: ConfigRef) =
let objFile = conf.getObjFilePath(x)
let buildDll = idx != mainFileIdx
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
# for more info check the comment inside of getLinkCmd() where the /PDB:<filename> MSVC flag is used
if isVSCompatible(conf):
Expand All @@ -914,7 +916,8 @@ proc callCCompiler*(conf: ConfigRef) =
objfiles.add(quoteShell(objFile))
let mainOutput = if optGenScript notin conf.globalOptions: conf.prepareToWriteOutput
else: AbsoluteFile(conf.projectName)
linkCmd = getLinkCmd(conf, mainOutput, objfiles)

linkCmd = getLinkCmd(conf, mainOutput, objfiles, removeStaticFile = true)
extraCmds = getExtraCmds(conf, mainOutput)
if optCompileOnly notin conf.globalOptions:
const MaxCmdLen = when defined(windows): 8_000 else: 32_000
Expand Down
1 change: 1 addition & 0 deletions tests/compiles/mstaticlib.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
echo 1234
22 changes: 22 additions & 0 deletions tests/compiles/tstaticlib.nim
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 9cb5ab0

Please sign in to comment.