Skip to content

[SR-14728] SwiftPM/Driver on Windows fails to link C-based targets #4415

Open
@kabiroberai

Description

@kabiroberai
Previous ID SR-14728
Radar rdar://problem/79292673
Original Reporter @kabiroberai
Type Bug

Attachment: Download

Environment

OS: Windows 10 Pro (20H2)

Toolchain: compnerd.org Swift version 5.5-dev (LLVM 76b16fe31111aa0, Swift 5eb25f1662051fd)

Additional Detail from JIRA
Votes 0
Component/s Package Manager
Labels Bug, Android, Driver, Linux, Windows
Assignee None
Priority Medium

md5: a5cefba8ea42e9bae4dfc9028530df2a

Issue Description:

When using SPM on Windows to build a `.dynamic` library which only contains C targets, the linker fails with the following error:

lld-link: error: undefined symbol: __declspec(dllimport) swift_addNewDSOImage
>>> referenced by C:\Library\Developer\Platforms\Windows.platform\Developer\SDKs\Windows.sdk\usr\lib\swift\windows\x86_64\swiftrt.obj:(void 
__cdecl swift_image_constructor(void))
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Running `swift build -Xswiftc -v` reveals what seems to be the issue:

"C:\\Library\\Developer\\Toolchains\\unknown-Asserts-development.xctoolchain\\usr\\bin\\lld-link" "-out:C:\\Users\\kabir\\Desktop\\Code\\WinBug\\.build\\x86_64-unknown-windows-msvc\\debug\\WinBug.dll" -dll <...> "C:\\Library\\Developer\\Platforms\\Windows.platform\\Developer\\SDKs\\Windows.sdk\\usr\\lib\\swift\\windows\\x86_64\\swiftrt.obj" "C:\\Users\\kabir\\Desktop\\Code\\WinBug\\.build\\x86_64-unknown-windows-msvc\\debug\\WinBug.build\\WinBug.c.o"

i.e. the Swift driver appears to be linking swiftrt.obj but not swiftCore.lib (since swift-autolink-extract sees no need for swiftCore.lib because it's a pure C target). Using the driver by itself (instead of through swift build) also produces this issue.

I'm not too familiar with the Swift codebase but an initial search seems to indicate that the issue is caused due to swiftrt.obj being unconditionally linked at lib/Driver/WindowsToolChains.cpp#L134. Running `swift build -Xswiftc -lswiftCore` causes it to link successfully – though I suspect that not linking swiftrt.obj in C-only targets might be the preferred solution.

I've attached a minimal project demonstrating the issue; running `swift build` succeeds on both macOS and Linux, but fails on Windows.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions