Files
swift-nio/scripts/check-cxx-interop-compatibility.sh
Si Beaumont c329d1e173 Fix C++ interop CI script to work with changes in SwiftPM (#3510)
### Motivation:

SwiftPM behaviour has changed in recent Swift nightlies.

In most recent released version of Swift, SwiftPM will use the parent
directory name for the source and test target directory too, even when
it contains a period:

```
% docker run -it swift:6.2 bash -c 'mkdir -p /tmp/foo.bar && cd /tmp/foo.bar && swift package init'
Creating library package: foo.bar
Creating Package.swift
Creating .gitignore
Creating Sources
Creating Sources/foo.bar/foo_bar.swift
Creating Tests/
Creating Tests/foo.barTests/
Creating Tests/foo.barTests/foo_barTests.swift
```

Note in the above how it replaces the `.` with a `_` in the source
files, but the directories retain the `.`.

In the recent nightly-main, SwiftPM has also started replacing `.` with
`_` in the target directory names:

```
% docker run -it swiftlang/swift:nightly-main bash -c 'cat /etc/motd && mkdir -p /tmp/foo.bar && cd /tmp/foo.bar && swift package init'
################################################################
#                                                              #
# Swift Nightly Docker Image                                   #
# Tag: swift-DEVELOPMENT-SNAPSHOT-2026-02-06-a                 #
#                                                              #
################################################################
Creating library package: foo.bar
Creating Package.swift
Creating .gitignore
Creating Sources
Creating Sources/foo_bar/foo_bar.swift
Creating Tests/
Creating Tests/foo_barTests/
Creating Tests/foo_barTests/foo_barTests.swift
```

Probably this
https://github.com/swiftlang/swift-package-manager/pull/9252.

This breaks our CI automation, only on nightly-main, because we run
`swift package init` in a directory created by `mktemp -d`, which, by
default, follows the pattern `tmp.XXXXXXX`.

### Modifications:

Create temporary directory for Swift package using explicit pattern
containing no periods.

### Result:

C++ interop CI should now work again on nightly-main Swift, and continue
to work on all supported Swift versions.
2026-02-11 17:13:33 +00:00

49 lines
1.6 KiB
Bash
Executable File

#!/bin/bash
##===----------------------------------------------------------------------===##
##
## This source file is part of the SwiftNIO open source project
##
## Copyright (c) 2024 Apple Inc. and the SwiftNIO project authors
## Licensed under Apache License v2.0
##
## See LICENSE.txt for license information
## See CONTRIBUTORS.txt for the list of SwiftNIO project authors
##
## SPDX-License-Identifier: Apache-2.0
##
##===----------------------------------------------------------------------===##
set -euo pipefail
log() { printf -- "** %s\n" "$*" >&2; }
error() { printf -- "** ERROR: %s\n" "$*" >&2; }
fatal() { error "$@"; exit 1; }
log "Checking for Cxx interoperability compatibility..."
source_dir=$(pwd)
working_dir=$(mktemp -d "/tmp/tmp_swift_package_XXXXXXXXXX")
project_name=$(basename "$working_dir")
source_file=Sources/$project_name/$project_name.swift
library_products=$( swift package dump-package | jq -r '.products[] | select(.type.library != null) | .name')
package_name=$(swift package dump-package | jq -r '.name')
cd "$working_dir"
swift package init
{
echo "let swiftSettings: [SwiftSetting] = [.interoperabilityMode(.Cxx)]"
echo "for target in package.targets { target.swiftSettings = (target.swiftSettings ?? []) + swiftSettings }"
} >> Package.swift
echo "package.dependencies.append(.package(path: \"$source_dir\"))" >> Package.swift
for product in $library_products; do
echo "package.targets.first!.dependencies.append(.product(name: \"$product\", package: \"$package_name\"))" >> Package.swift
echo "import $product" >> "$source_file"
done
swift build
log "✅ Passed the Cxx interoperability tests."