Skip to content

Conversation

xymus
Copy link
Contributor

@xymus xymus commented May 28, 2025

Address two issues preventing a C source file to import the generated compatibility header:

  1. Don't mark the module map generated for the compatibility header as requires objc. This triggers an error when importing it from a C source file. The compatibility header is already printed in a way where the Objective-C code is protected behind a language check. C clients can safely import it even if they may not see any content. Plus we're adding C content to the compatibility header with the official support for @cdecl that is independent of Objective-C.

  2. Emit the generated compatibility header and module map in a new include directory under the temporary build directory of each Swift library. This directory is then used as a header search path for the dependent clang targets.

    This replaces the previous strategy that relied only the generated module map to resolve imports. It was incompatible with C source files and non-modular textual includes. The use of a header search path preserves the support for module imports via the same module map and supports textual includes of the compatibility header.

Fixes #7257.

Follow up to #8695.

xymus added 2 commits May 27, 2025 15:28
The module map generated for the compatibility header was marked as
`requires objc`. This triggers an error when importing it from a C source
file.

This declaration is superfluous, the compatibility header is already
printed in a way where the Objective-C code is protected behind a
language check. C clients can safely import the current compatibility
header even if it may not see any content.

Let's lift this restriction. It isn't currently necessary and we're
adding C content to the compatibility header with the official support
for `@cdecl` that is independent of Objective-C.
…tory

Emit the generated compatibility header and module map in a new
`include` directory under the temporary build directory of Swift
libraries. This directory can then be used as a header search path for
the dependent clang targets.

This replaces the previous header search strategy that used only the
generated module map. This was incompatible with C source files and
classic non-module textual includes. The use of a header search path
should support both module imports via the module map as well as
includes of the compatibility header.
@xymus
Copy link
Contributor Author

xymus commented May 28, 2025

@swift-ci Please test

@xymus
Copy link
Contributor Author

xymus commented May 28, 2025

@swift-ci Please test macOS

@xymus
Copy link
Contributor Author

xymus commented Jun 4, 2025

@swift-ci Please test

@xymus
Copy link
Contributor Author

xymus commented Jun 5, 2025

@swift-ci Please test Windows

@xymus
Copy link
Contributor Author

xymus commented Jun 5, 2025

@swift-ci Please test

Also lift the macOS restriction now that swiftpm emits the compatibility
header on other platforms.
@xymus xymus force-pushed the compat-header-include branch from e788fa1 to c2d9f40 Compare June 5, 2025 19:50
@xymus
Copy link
Contributor Author

xymus commented Jun 5, 2025

@swift-ci Please test

@xymus
Copy link
Contributor Author

xymus commented Jun 6, 2025

@swift-ci Please test Windows

@xymus
Copy link
Contributor Author

xymus commented Jun 24, 2025

Is someone available to review this change?

@xymus
Copy link
Contributor Author

xymus commented Jun 24, 2025

@jakepetroules do you think we need a similar change in swift-build? It appears to print the requires objc as well, however I don’t think it uses the same header search strategy relying only on the module map.

@jakepetroules
Copy link
Contributor

@jakepetroules do you think we need a similar change in swift-build? It appears to print the requires objc as well, however I don’t think it uses the same header search strategy relying only on the module map.

I think so, yeah, at least for the requires objc bit.

@dschaefer2 dschaefer2 merged commit 58eb4ce into swiftlang:main Jul 3, 2025
6 checks passed
marcprux pushed a commit to swift-everywhere/swift-package-manager that referenced this pull request Jul 7, 2025
…t libraries (swiftlang#8736)

Address two issues preventing a C source file to import the generated
compatibility header:

1. Don't mark the module map generated for the compatibility header as
`requires objc`. This triggers an error when importing it from a C
source file. The compatibility header is already printed in a way where
the Objective-C code is protected behind a language check. C clients can
safely import it even if they may not see any content. Plus we're adding
C content to the compatibility header with the official support for
`@cdecl` that is independent of Objective-C.

2. Emit the generated compatibility header and module map in a new
`include` directory under the temporary build directory of each Swift
library. This directory is then used as a header search path for the
dependent clang targets.

This replaces the previous strategy that relied only the generated
module map to resolve imports. It was incompatible with C source files
and non-modular textual includes. The use of a header search path
preserves the support for module imports via the same module map and
supports textual includes of the compatibility header.

Fixes swiftlang#7257.

Follow up to swiftlang#8695.
dschaefer2 pushed a commit to dschaefer2/swift-package-manager that referenced this pull request Jul 8, 2025
…t libraries (swiftlang#8736)

Address two issues preventing a C source file to import the generated
compatibility header:

1. Don't mark the module map generated for the compatibility header as
`requires objc`. This triggers an error when importing it from a C
source file. The compatibility header is already printed in a way where
the Objective-C code is protected behind a language check. C clients can
safely import it even if they may not see any content. Plus we're adding
C content to the compatibility header with the official support for
`@cdecl` that is independent of Objective-C.

2. Emit the generated compatibility header and module map in a new
`include` directory under the temporary build directory of each Swift
library. This directory is then used as a header search path for the
dependent clang targets.

This replaces the previous strategy that relied only the generated
module map to resolve imports. It was incompatible with C source files
and non-modular textual includes. The use of a header search path
preserves the support for module imports via the same module map and
supports textual includes of the compatibility header.

Fixes swiftlang#7257.

Follow up to swiftlang#8695.
dschaefer2 added a commit that referenced this pull request Jul 8, 2025
…ted for Swift libraries #8736 (#8906)

Cherry-pick #8736 to 6.2

Address two issues preventing a C source file to import the generated
compatibility header:

Don't mark the module map generated for the compatibility header as
requires objc. This triggers an error when importing it from a C source
file. The compatibility header is already printed in a way where the
Objective-C code is protected behind a language check. C clients can
safely import it even if they may not see any content. Plus we're adding
C content to the compatibility header with the official support for
@cdecl that is independent of Objective-C.

Emit the generated compatibility header and module map in a new include
directory under the temporary build directory of each Swift library.
This directory is then used as a header search path for the dependent
clang targets.

This replaces the previous strategy that relied only the generated
module map to resolve imports. It was incompatible with C source files
and non-modular textual includes. The use of a header search path
preserves the support for module imports via the same module map and
supports textual includes of the compatibility header.

Co-authored-by: Alexis Laferrière <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Failed to generate a static or dynamic Swift library called by C code

3 participants