Skip to content

swiftinterface compilation does not re-serialise static library builds #77756

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
hjyamauchi opened this issue Nov 21, 2024 · 5 comments · Fixed by #77861
Closed

swiftinterface compilation does not re-serialise static library builds #77756

hjyamauchi opened this issue Nov 21, 2024 · 5 comments · Fixed by #77861
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. c++ interop Feature: Interoperability with C++ compiler The Swift compiler itself Frontend Area → compiler: Frontend linker error swift 6.0 unexpected error Bug: Unexpected error

Comments

@hjyamauchi
Copy link
Contributor

hjyamauchi commented Nov 21, 2024

Description

It seems that I recently started getting link errors related to swiftCxx.lib and swiftCxxStdlib.lib such as:

Windows (x64/arm64):

lld-link: error: could not open 'swiftCxx.lib': no such file or directory
lld-link: error: could not open 'swiftCxxStdlib.lib': no such file or directory
LINK : fatal error LNK1104: cannot open file 'swiftCxx.lib'

where I presume it should probably need to load instead libswiftCxx.lib and libswiftCxxStdlib.lib which actually exist in the toolchain directory, as opposed to swiftCxx.lib and swiftCxxStdlib.lib which don't exist there.

Locally reverting #77559 (and swiftlang/swift-installer-scripts#344) seems to make this error go away.

Reproduction

On a recent toolchain build in the main branch:

Windows

C:\Users\hiroshi\Test>cat Test.swift
import CXXModule

C:\Users\hiroshi\Test>cat Inputs\header.h
#ifndef HEADER_H
#define HEADER_H

#endif // HEADER_H

C:\Users\hiroshi\Test>cat Inputs\module.modulemap
module CXXModule {
  header "header.h"
  requires cplusplus
}

C:\Users\hiroshi\Test>swiftc Test.swift -cxx-interoperability-mode=default -o Test.exe -I C:\Users\hiroshi\SwiftCxxTest\Inputs
error: link command failed with exit code 1104 (use -v to see invocation)
LINK : fatal error LNK1104: cannot open file 'swiftCxx.lib'
clang: error: linker command failed with exit code 1104 (use -v to see invocation)

Expected behavior

Successful linking, no link errors.

Environment

With a recent main-branch toolchain build on windows.

Additional information

No response

@hjyamauchi hjyamauchi added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels labels Nov 21, 2024
@hjyamauchi
Copy link
Contributor Author

CC @egorzhdan

@egorzhdan egorzhdan self-assigned this Nov 21, 2024
@egorzhdan egorzhdan added c++ interop Feature: Interoperability with C++ and removed triage needed This issue needs more specific labels labels Nov 21, 2024
@egorzhdan
Copy link
Contributor

Hmm, looks like this code might need to change:

// Do not try to link Cxx with itself.
if (mainModuleName != "Cxx") {
RegistrationCallback(LinkLibrary(Target.isOSWindows() && hasStaticCxx
? "libswiftCxx"
: "swiftCxx",
LibraryKind::Library));
}

However, the Windows CI seems to be passing now, so I wonder if there is a difference between the CI and the local build config.

@hjyamauchi
Copy link
Contributor Author

I didn't encounter this during a toolchain build, but in an internal app build. So, the Windows CI build may not trigger this particular issue. I hope that anyone other than me can reproduce with the reduced test example.

@compnerd
Copy link
Member

The code doesn't need to change, it is correct. The problem is that we are building the libraries as static libraries rather than dynamic libraries, which is why they are failing to link.

compnerd added a commit to compnerd/apple-swift that referenced this issue Nov 27, 2024
When compiling the swiftmodule from the textual swift interface, ensure
that we re-serialise the static or dynamic nature of the module. This is
required for proper code generation where the static and dynamic linking
is material to symbolic references. This also opens the possibility of
optimizations on other platforms via internalisation of the symbols.

Fixes: swiftlang#77756
@compnerd compnerd changed the title An C++ interop-related link error swiftinterface compilation does not re-serialise static library builds Nov 27, 2024
@compnerd
Copy link
Member

The issue here is that we are generating a static resilient module and distributing the swiftinterface now. When the swiftinterface was being re-serialised, we would drop the static indicator resulting in the module trying to be dynamically linked when it was static.

compnerd added a commit to compnerd/apple-swift that referenced this issue Nov 27, 2024
When compiling the swiftmodule from the textual swift interface, ensure
that we re-serialise the static or dynamic nature of the module. This is
required for proper code generation where the static and dynamic linking
is material to symbolic references. This also opens the possibility of
optimizations on other platforms via internalisation of the symbols.

Fixes: swiftlang#77756
@AnthonyLatsis AnthonyLatsis added linker error unexpected error Bug: Unexpected error compiler The Swift compiler itself Frontend Area → compiler: Frontend swift 6.0 labels Dec 4, 2024
bnbarham pushed a commit to bnbarham/swift that referenced this issue Jan 2, 2025
When compiling the swiftmodule from the textual swift interface, ensure
that we re-serialise the static or dynamic nature of the module. This is
required for proper code generation where the static and dynamic linking
is material to symbolic references. This also opens the possibility of
optimizations on other platforms via internalisation of the symbols.

Fixes: swiftlang#77756
(cherry picked from commit b660d20)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. c++ interop Feature: Interoperability with C++ compiler The Swift compiler itself Frontend Area → compiler: Frontend linker error swift 6.0 unexpected error Bug: Unexpected error
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants