Swift 5.9 release on Ubuntu 22.04 fails to build std module

I have a simple CMake project that has a single-header module that imports my C++ library header I want to interface with from Swift.

cmake_minimum_required(VERSION 3.25)

project(lagom-swift LANGUAGES Swift)

add_library(lagom INTERFACE)
target_include_directories(lagom INTERFACE src/include)
target_compile_definitions(lagom INTERFACE "AK_DONT_REPLACE_STD")
target_compile_options(lagom INTERFACE "SHELL:$<$<COMPILE_LANGUAGE:Swift>:-Xcc -std=c++20>")
target_compile_features(lagom INTERFACE cxx_std_20)

add_executable(WebView
    src/WebViewUI.swift
)
target_compile_options(WebView PRIVATE -cxx-interoperability-mode=default)
target_link_libraries(WebView PRIVATE lagom)

This is a minimized example, because what I actually want is to find_package() my library, but that's beside the point here :)

in src/include, I have two files:

module.modulemap

module lagom [system] {
    requires cplusplus

    module webview {
        header "LagomWebView.h"
        export *
    }
}

and LagomWebView.h

#ifndef LAGOM_WEBVIEW_H
#define LAGOM_WEBVIEW_H

#include <new>

#endif

And a simplified WebViewUI.swift that doesn't do much but import the module:

import Foundation

import lagom.webview

@main
final class WebViewUI: ApplicationDelegate {
    func application(_ application: Application,
                    didFinishLaunchingWithOptions: [Application.LaunchOptionsKey:Any]?) -> Bool {
        return true
    }
}

Compiling on Ubuntu 22.04.3 LTS fails with a cyclic dependency b/w std and _Builtin_intrinsics. This seems to have something to do with the PSTL implementation for libc++-12.

given:
cmake -B Build -GNinja -DCMAKE_Swift_COMPILER=/home/andrew/swift/swift-5.9-RELEASE-ubuntu22.04/usr/bin/swiftc, and compiling, the compiler errors out like so:

Compiler error trace
[1/1] Linking Swift executable WebView
FAILED: WebView CMakeFiles/WebView.dir/src/WebViewUI.swift.o 
: && /home/andrew/swift/swift-5.9-RELEASE-ubuntu22.04/usr/bin/swiftc -j 32 -num-threads 32 -emit-executable -o WebView -emit-dependencies -DAK_DONT_REPLACE_STD -cxx-interoperability-mode=default -Xcc -std=c++20 -output-file-map CMakeFiles/WebView.dir//output-file-map.json -I /home/andrew/lagom-swift/src/include /home/andrew/lagom-swift/src/WebViewUI.swift    && :
/home/andrew/lagom-swift/src/include/LagomWebView.h:4:10: note: while building module 'std' imported from /home/andrew/lagom-swift/src/include/LagomWebView.h:4:
#include <new>
         ^
/usr/include/tbb/../oneapi/tbb/detail/_machine.h:42:10: note: while building module '_Builtin_intrinsics' imported from /usr/include/tbb/../oneapi/tbb/detail/_machine.h:42:
#include <immintrin.h> // _mm_pause
         ^
<module-includes>:2:10: note: in file included from <module-includes>:2:
#include "immintrin.h"
         ^
/home/andrew/swift/swift-5.9-RELEASE-ubuntu22.04/usr/lib/swift/clang/include/immintrin.h:26:10: note: in file included from /home/andrew/swift/swift-5.9-RELEASE-ubuntu22.04/usr/lib/swift/clang/include/immintrin.h:26:
#include <xmmintrin.h>
         ^
/home/andrew/swift/swift-5.9-RELEASE-ubuntu22.04/usr/lib/swift/clang/include/xmmintrin.h:31:10: note: in file included from /home/andrew/swift/swift-5.9-RELEASE-ubuntu22.04/usr/lib/swift/clang/include/xmmintrin.h:31:
#include <mm_malloc.h>
         ^
/home/andrew/swift/swift-5.9-RELEASE-ubuntu22.04/usr/lib/swift/clang/include/mm_malloc.h:13:10: note: in file included from /home/andrew/swift/swift-5.9-RELEASE-ubuntu22.04/usr/lib/swift/clang/include/mm_malloc.h:13:
#include <stdlib.h>
         ^
/usr/lib/gcc/x86_64-linux-gnu/12/../../../../include/c++/12/stdlib.h:36:11: error: cyclic dependency in module 'std': std -> _Builtin_intrinsics -> std
# include <cstdlib>
          ^
/home/andrew/lagom-swift/src/include/LagomWebView.h:4:10: note: while building module 'std' imported from /home/andrew/lagom-swift/src/include/LagomWebView.h:4:
#include <new>
         ^
<module-includes>:1:10: note: in file included from <module-includes>:1:
#include "libstdcxx.h"
         ^
/home/andrew/swift/swift-5.9-RELEASE-ubuntu22.04/usr/lib/swift/linux/x86_64/libstdcxx.h:6:10: note: in file included from /home/andrew/swift/swift-5.9-RELEASE-ubuntu22.04/usr/lib/swift/linux/x86_64/libstdcxx.h:6:
#include "execution"
         ^
/usr/lib/gcc/x86_64-linux-gnu/12/../../../../include/c++/12/execution:32:11: note: in file included from /usr/lib/gcc/x86_64-linux-gnu/12/../../../../include/c++/12/execution:32:
# include <pstl/glue_execution_defs.h>
          ^
/usr/lib/gcc/x86_64-linux-gnu/12/../../../../include/c++/12/pstl/glue_execution_defs.h:50:10: note: in file included from /usr/lib/gcc/x86_64-linux-gnu/12/../../../../include/c++/12/pstl/glue_execution_defs.h:50:
#include "algorithm_impl.h"
         ^
/usr/include/c++/12/pstl/algorithm_impl.h:22:10: note: in file included from /usr/include/c++/12/pstl/algorithm_impl.h:22:
#include "parallel_backend.h"
         ^
/usr/include/c++/12/pstl/parallel_backend.h:20:14: note: in file included from /usr/include/c++/12/pstl/parallel_backend.h:20:
#    include "parallel_backend_tbb.h"
             ^
/usr/include/c++/12/pstl/parallel_backend_tbb.h:19:10: note: in file included from /usr/include/c++/12/pstl/parallel_backend_tbb.h:19:
#include <tbb/blocked_range.h>
         ^
/usr/include/tbb/blocked_range.h:17:10: note: in file included from /usr/include/tbb/blocked_range.h:17:
#include "../oneapi/tbb/blocked_range.h"
         ^
/usr/include/tbb/../oneapi/tbb/blocked_range.h:22:10: note: in file included from /usr/include/tbb/../oneapi/tbb/blocked_range.h:22:
#include "detail/_range_common.h"
         ^
/usr/include/tbb/../oneapi/tbb/detail/_range_common.h:21:10: note: in file included from /usr/include/tbb/../oneapi/tbb/detail/_range_common.h:21:
#include "_utils.h"
         ^
/usr/include/tbb/../oneapi/tbb/detail/_utils.h:26:10: note: in file included from /usr/include/tbb/../oneapi/tbb/detail/_utils.h:26:
#include "_machine.h"
         ^
/usr/include/tbb/../oneapi/tbb/detail/_machine.h:42:10: error: could not build module '_Builtin_intrinsics'
#include <immintrin.h> // _mm_pause
         ^
1 Like

This fails inside the swift:latest docker container as well, but for a different reason:

compiler errors
root@697de004db56:/app# cmake -B Build -GNinja
-- The Swift compiler identification is Apple 5.9
-- Check for working Swift compiler: /usr/bin/swiftc
-- Check for working Swift compiler: /usr/bin/swiftc - works
-- Configuring done (0.5s)
-- Generating done (0.0s)
-- Build files have been written to: /app/Build
root@697de004db56:/app# ninja -C Build
ninja: Entering directory `Build'
[1/1] Linking Swift executable WebView
FAILED: WebView CMakeFiles/WebView.dir/src/WebViewUI.swift.o 
: && /usr/bin/swiftc -j 32 -num-threads 32 -emit-executable -o WebView -emit-dependencies -DAK_DONT_REPLACE_STD -cxx-interoperability-mode=default -Xcc -std=c++20 -output-file-map CMakeFiles/WebView.dir//output-file-map.json -I /app/src/include /app/src/WebViewUI.swift    && :
<module-includes>:2:10: note: in file included from <module-includes>:2:
#include "CoreFoundation.h"
         ^
/usr/lib/swift/CoreFoundation/CoreFoundation.h:103:10: note: in file included from /usr/lib/swift/CoreFoundation/CoreFoundation.h:103:
#include <CoreFoundation/ForSwiftFoundationOnly.h>
         ^
/usr/lib/swift/CoreFoundation/ForSwiftFoundationOnly.h:600:18: error: variable has incomplete type 'struct statx'
    struct statx statx_buffer = {0};
                 ^
/usr/lib/swift/CoreFoundation/ForSwiftFoundationOnly.h:590:85: note: forward declaration of 'statx'
_statx(int dfd, const char *filename, unsigned int flags, unsigned int mask, struct statx *buffer) {
                                                                                    ^
<module-includes>:2:10: note: in file included from <module-includes>:2:
#include "CoreFoundation.h"
         ^
/usr/lib/swift/CoreFoundation/CoreFoundation.h:103:10: note: in file included from /usr/lib/swift/CoreFoundation/CoreFoundation.h:103:
#include <CoreFoundation/ForSwiftFoundationOnly.h>
         ^
/usr/lib/swift/CoreFoundation/ForSwiftFoundationOnly.h:603:87: error: use of undeclared identifier 'STATX_ALL'
    int ret = _statx(AT_FDCWD, filename, AT_SYMLINK_NOFOLLOW | AT_STATX_SYNC_AS_STAT, STATX_ALL, &statx_buffer);
                                                                                      ^
<module-includes>:2:10: note: in file included from <module-includes>:2:
#include "CoreFoundation.h"
         ^
/usr/lib/swift/CoreFoundation/CoreFoundation.h:103:10: note: in file included from /usr/lib/swift/CoreFoundation/CoreFoundation.h:103:
#include <CoreFoundation/ForSwiftFoundationOnly.h>
         ^
/usr/lib/swift/CoreFoundation/ForSwiftFoundationOnly.h:621:37: error: use of undeclared identifier 'STATX_BTIME'
        if (statx_buffer.stx_mask & STATX_BTIME) {
                                    ^
<unknown>:0: error: could not build C module 'CoreFoundation'
error: fatalError
ninja: build stopped: subcommand failed.

This is with

$ docker image list swift
REPOSITORY   TAG       IMAGE ID       CREATED        SIZE
swift        latest    d660abe6e287   19 hours ago   2.55GB

and after installing cmake 3.27 from Kitware's cmake apt repo, and ninja-build.

I think I figured this one out actually. For inside the container anyway :)

installing cmake from the kit ware repos installs gcc-11 as a dependency at the moment. That breaks things. Removing it and making sure to install libstdc++-10-dev lets the project get to the (probably obvious) swift errors in my example project.

Locally on my machine with gcc-12 and such installed, the error from my first post persists.

This looks similar to issues I've seen in trying to use the C++ interop with libc++ on Android. Does uninstalling LLVM's libc++ and only using the GNU libstdc++ fix the issue? I believe the linux CI is only ever built with libstdc++, so libc++ doesn't work with Swift's C++ interop on non-Darwin platforms right now.