Skip to content

How to consume Swift modules when Cxx interop is enabled #15

@tony-go

Description

@tony-go

Hey there 👋🏼

First, thanks for this repository, he helped me learn a lot about Swift and CMake and Swift/Cxx interop.

I was experimenting with Swift/Cxx interoperability, especially with consuming a Swift class from C++.

It worked nicely until I needed to import a Swift module from one file to another.

Here is my project structure:

├── CMakeLists.txt
├── app
│   ├── App.cc  
│   ├── App.swift                 // HERE, I NEED to import module from window.swift
│   ├── CMakeLists.txt
│   └── include
│       ├── application.h
│       └── module.modulemap
├── main.cc
└── window
    ├── CMakeLists.txt
    ├── include
    │   ├── module.modulemap
    │   └── window.h
    ├── window.cc
    └── window.swift
import Ui

But it does not work as expected:

[4/8] Generating '/Users/tonygo/oss/native-research/build/app/include/app-swift.h'
FAILED: app/include/app-swift.h /Users/tonygo/oss/native-research/build/app/include/app-swift.h
cd /Users/tonygo/oss/native-research/build/app && /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swiftc -frontend -typecheck -I/Users/tonygo/oss/native-research/app/include /Users/tonygo/oss/native-research/app/App.swift -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.2.sdk -module-name Native -cxx-interoperability-mode=default -emit-clang-header-path /Users/tonygo/oss/native-research/build/app/include/app-swift.h
/Users/tonygo/oss/native-research/app/App.swift:3:8: error: no such module 'Ui'
import Ui
       ^

But when I looked at the build output, I saw Ui module files generated:

├── build
│   └── window
│       ├── CMakeFiles
│       │   └── window.dir
│       │       ├── output-file-map.json
│       │       ├── window.cc.o
│       │       ├── window.swift.o
│       │       ├── window.swift.o.d
│       │       ├── window.swift.swiftdoc
│       │       ├── window.swift.swiftmodule
│       │       ├── window.swift.swiftsourceinfo
│       │       └── window.swiftdeps
│       ├── Ui.abi.json
│       ├── Ui.swiftdoc
│       ├── Ui.swiftmodule
│       ├── Ui.swiftsourceinfo
│       ├── cmake_install.cmake
│       ├── include
│       │   └── window-swift.h
│       └── libwindow.a

I also tried with:

import window

Like it is done here: https://github.com/apple/swift-cmake-examples/tree/main/2_executable_library

I checked that app is linked to window target.

I wonder if it is not just a CMake knowledge that I miss or something else. I run out of ideas 😅.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions