cxx FFI bindings to the Swift C++ API
Building the cxx-swift
crate is a somewhat involved process since it requires having built the Swift toolchain, which in turn requires building (or bootstrapping from) LLVM and Clang.
The quickest and easiest way to be able to develop and build the cxx-swift
crate is to use the devcontainer definition in the .devcontainer
directory.
The devcontainer provides a self-contained development environment with the necessary libraries for building the FFI bindings for the Swift toolchain already compiled, along with LLVM, Clang, and the Rust toolchain.
The devcontainer uses a multi-platform image with variants available for both arm64
and amd64
, which will be chosen automatically for your platform; the arm64
variant will be chosen for Apple Silicon Macs, so there is no need to run the amd64
variant under Docker with Rosetta.
In order to use the devcontainer, if you open the repo with Visual Studio Code and have the Dev Containers extension installed (you will also need Docker installed), the editor should prompt you to open the workspace in the devcontainer. If you do not see the prompt for this, you can instead manually open the command palette and input and select the command Dev Containers: Rebuild and Reopen in Container
.
Alternatively, you should be able to open the devcontainer with other compatible tools, including the cli tool.
NOTE: The devcontainer uses a docker image that is around 2.5GB compressed, which decompresses locally to around 15GB. You can expect the devcontainer initialization process to take a few minutes the very first time you launch it (during which the editor may look like it has stalled, but will eventually finish). In any case, it should still be significantly faster than having to download and compile the entire LLVM, Clang, and Swift toolchains. After the first initialization, subsequent launches of the devcontainer should be almost instantaneous.
- Start from a common development directory, e.g.,
~/Development
brew install llvm sccache
mkdir swift-project
cd swift-project
git clone --depth 1 --single-branch --branch swift-5.8.1-RELEASE https://github.com/apple/swift.git swift
cd swift
./utils/update-checkout --clone --skip-history --skip-tags --tag swift-5.8.1-RELEASE
./utils/build-script --host-cc /opt/homebrew/opt/llvm@16/bin/clang --host-cxx /opt/homebrew/opt/llvm@16/bin/clang++ --bootstrapping hosttools --build-toolchain-only true --build-swift-private-stdlib false --skip-build-compiler-rt --skip-build-clang-tools-extra --skip-early-swift-driver --skip-early-swiftsyntax --skip-build-benchmarks --enable-experimental-differentiable-programming false --enable-experimental-concurrency false --enable-experimental-distributed false --enable-experimental-string-processing false --sccache --min-size-release --clean --darwin-deployment-version-os "$(otool -l /opt/homebrew/opt/llvm@16/bin/clang++ | awk '/minos/ { print $2 }')" --host-libtool /opt/homebrew/opt/llvm@16/bin/llvm-libtool-darwin --host-lipo /opt/homebrew/opt/llvm@16/bin/llvm-lipo
cd ~/Development
git clone https://github.com/silvanshade/cxx-swift
cd cxx-swift
CC=/opt/homebrew/opt/llvm@16/bin/clang CXX=/opt/homebrew/opt/llvm@16/bin/clang++ SWIFT_PROJECT_PATH="${HOME}/Development/swift-project" cargo build
NOTE: the SWIFT_PROJECT_PATH
environment variable must be set and must be an absolute path pointing to the Swift toolchain built in step (8) when running cargo
commands since the cxx-swift
build scripts rely on this variable to locate the necessary LLVM / Clang / Swift toolchain headers and static libraries.
- Start from a common development directory, e.g.,
~/Development
brew install llvm sccache
mkdir swift-project
cd swift-project
git clone --depth 1 --single-branch --branch swift-5.8.1-RELEASE https://github.com/apple/swift.git swift
cd swift
./utils/update-checkout --clone --skip-history --skip-tags --tag swift-5.8.1-RELEASE
./utils/build-script --host-cc clang-16 --host-cxx clang++-16 --bootstrapping hosttools --build-toolchain-only true --build-swift-private-stdlib false --skip-build-compiler-rt --skip-build-clang-tools-extra --skip-early-swift-driver --skip-early-swiftsyntax --skip-build-benchmarks --enable-experimental-differentiable-programming false --enable-experimental-concurrency false --enable-experimental-distributed false --enable-experimental-string-processing false --sccache --min-size-release --clean
cd ~/Development
git clone https://github.com/silvanshade/cxx-swift
cd cxx-swift
CC=clang-15 CXX=clang++-16 SWIFT_PROJECT_PATH="${HOME}/Development/swift-project" cargo build
NOTE: the SWIFT_PROJECT_PATH
environment variable must be set and must be an absolute path pointing to the Swift toolchain built in step (8) when running cargo
commands since the cxx-swift
build scripts rely on this variable to locate the necessary LLVM / Clang / Swift toolchain headers and static libraries.