Skip to content

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

@stevapple

Description

@stevapple
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.

Activity

added
bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.
on Aug 12, 2022
finagolfin

finagolfin commented on Aug 13, 2022

@finagolfin
Member

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

stevapple commented on Aug 13, 2022

@stevapple
ContributorAuthor

@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?

changed the title [-]Swift doesn’t recognize Clang’s header[/-] [+]Swift doesn’t recognize Clang’s header on Windows[/+] on Aug 13, 2022
compnerd

compnerd commented on Aug 17, 2022

@compnerd
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

stevapple commented on Aug 17, 2022

@stevapple
ContributorAuthor

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

stevapple commented on Aug 17, 2022

@stevapple
ContributorAuthor

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

compnerd commented on Aug 17, 2022

@compnerd
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

finagolfin commented on Aug 18, 2022

@finagolfin
Member

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

stevapple commented on Aug 19, 2022

@stevapple
ContributorAuthor

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

stevapple commented on Sep 22, 2022

@stevapple
ContributorAuthor

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.

finagolfin

finagolfin commented on Jan 16, 2025

@finagolfin
Member

Still a problem with Swift 6? If not, please close.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Participants

      @compnerd@stevapple@finagolfin

      Issue actions

        Swift doesn’t recognize Clang’s header on Windows · Issue #60534 · swiftlang/swift