Skip to content
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

Swift doesn’t recognize Clang’s header on Windows #60534

Open
stevapple opened this issue Aug 12, 2022 · 9 comments · May be fixed by swiftlang/swift-installer-scripts#198
Open

Swift doesn’t recognize Clang’s header on Windows #60534

stevapple opened this issue Aug 12, 2022 · 9 comments · May be fixed by swiftlang/swift-installer-scripts#198
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior.

Comments

@stevapple
Copy link
Contributor

Describe the bug
Clang provides some C headers as polyfills (or shims) and the Swift toolchain vendors it in /usr/lib/clang/{clang_version}/include. However, the Swift compiler doesn’t recognize these headers, so a module produced by the bundled Clang may not be consumed by Swift.

Steps To Reproduce
Steps to reproduce the behavior:

  1. Clone https://github.com/apple/swift-atomics;
  2. Change directory to swift-atomics;
  3. Run swift build.

Expected behavior
Since the _AtomicsShims target is successfully compiled, it should be consumable for Atomics.

Screenshots
A9ECDABB-E29B-4822-9347-A4909F6FF54D

Environment

  • OS: Windows Server 2019 Datacenter (1809)
  • Swift version: 5.6.2
  • Visual Studio: Community 2019, 16.11.18

Additional context
Although this can be worked around from the SwiftPM side, I believe resolving it with swiftc is a better choice.

@stevapple stevapple added the bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. label Aug 12, 2022
@finagolfin
Copy link
Contributor

I see this problem all the time if the Swift symlink to the clang headers isn't set up right. Is there a symlink usr\lib\swift\clang that points at the clang headers you noted? If not, it will not find them.

@stevapple
Copy link
Contributor Author

@buttaface I believe that’s not the case on Windows. And it works after I add the symlink manually.

cc @compnerd if we can teach the installer to set up the symlink?

@stevapple stevapple changed the title Swift doesn’t recognize Clang’s header Swift doesn’t recognize Clang’s header on Windows Aug 13, 2022
@compnerd
Copy link
Member

This is a SwiftPM side issue, not a swiftc issue. The fact that this builds correctly with CMake is proof of that.

Building it locally, the problematic part of the repo for me is: https://github.com/apple/swift-atomics/blob/main/Sources/_AtomicsShims/include/_AtomicsShims.h#L63-L65. Changing the order of inclusion, I'm not a fan of - the MSVC headers should be preferred for Windows.

@stevapple
Copy link
Contributor Author

MSVC stdatomics.h doesn't support C so far. It’s even missing in VS 2019.

The Clang header will fall back using #include_next if system header is available, but the MSVC one cannot be used at this point.

@stevapple
Copy link
Contributor Author

stevapple commented Aug 17, 2022

This is a SwiftPM side issue, not a swiftc issue.

I was talking about the toolchain, not SwiftPM or compiler. /usr/lib/swift/clang symlink is set up on both Linux and macOS, I don’t think Windows should make a difference here.

@compnerd
Copy link
Member

No, the clang header will not fallback to MSVC: https://github.com/apple/llvm-project/blob/next/clang/lib/Headers/stdatomic.h#L20-L22.

I would say we should work out why it builds with CMake and not SPM and make SPM behave similar here.

@finagolfin
Copy link
Contributor

I'm surprised it works with CMake: I can't build anything that calls libc with the Swift compiler if that clang symlink is not there on linux or Android. @stevapple, does compiling the equivalent hello world to this simple file for Windows, ie import CRT or whatever, work if you remove that clang symlink?

import Glibc
print("Hey world")

On linux with this file, I get this error after removing the clang symlink that the toolchain comes with:

> ./swift-5.6.2-RELEASE-ubuntu20.04/usr/bin/swiftc hello.swift
<module-includes>:1:10: note: in file included from <module-includes>:1:
#include "SwiftGlibc.h"
         ^
/home/foo/swift-5.6.2-RELEASE-ubuntu20.04/usr/lib/swift/linux/x86_64/SwiftGlibc.h:45:10: note: in file included from /home/foo/swift-5.6.2-RELEASE-ubuntu20.04/usr/lib/swift/linux/x86_64/SwiftGlibc.h:45:
#include <limits.h>
         ^
/usr/include/limits.h:124:16: error: 'limits.h' file not found
# include_next <limits.h>
               ^
<unknown>:0: error: could not build C module 'SwiftGlibc'
<module-includes>:1:10: note: in file included from <module-includes>:1:
#include "LibcOverlayShims.h"
         ^
/home/foo/swift-5.6.2-RELEASE-ubuntu20.04/usr/lib/swift/shims/LibcOverlayShims.h:31:10: note: in file included from /home/foo/swift-5.6.2-RELEASE-ubuntu20.04/usr/lib/swift/shims/LibcOverlayShims.h:31:
#include <semaphore.h>
         ^
/usr/include/semaphore.h:22:10: note: in file included from /usr/include/semaphore.h:22:
#include <sys/types.h>
         ^

@stevapple
Copy link
Contributor Author

I would say we should work out why it builds with CMake and not SPM and make SPM behave similar here.

I don’t think it can build with CMake based on an VS 2019 installation. There’s no stdatomics.h provided by MSVC.

SwiftPM can build it successfully with VS 2022 toolset — as long as the symlink is set up. I believe this is the correct behavior, identical to other platforms.

@stevapple
Copy link
Contributor Author

For anyone running Swift 5.7 or older, set TOOLCHAIN_ROOT=C:\Library\Developer\Toolchains\unknown-Asserts-development.xctoolchain and run mklink /D %TOOLCHAIN_ROOT%\usr\lib\swift\clang ..\clang\<clang_version> with an elevated (Administrator) command prompt to work around the problem. Hopefully we can solve this by Swift 5.7.1.

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.
Projects
None yet
3 participants