Add FoundationURL trait (#118)

Add a default enabled trait "FoundationURL" that can be disabled for
users who don't want Foundation dependencies. Also enable
`InternalImportsByDefault`.
This commit is contained in:
Guoye Zhang authored and GitHub committed 2026-06-01 11:57:33 -07:00
1 parent bfafc0b613
commit f14fbc0e56
11 files changed
+140 -15

No files matched your search

+27
View File
@@ -67,3 +67,30 @@ jobs:
windows_6_3_enabled: true
windows_nightly_next_enabled: true
windows_nightly_main_enabled: true
construct-linkage-test-matrix:
name: Construct linkage matrix
runs-on: ubuntu-latest
outputs:
linkage-test-matrix: '${{ steps.generate-matrix.outputs.linkage-test-matrix }}'
steps:
- name: Checkout repository
uses: actions/checkout@v6
with:
persist-credentials: false
- id: generate-matrix
run: echo "linkage-test-matrix=$(curl -s https://raw.githubusercontent.com/apple/swift-nio/main/scripts/generate_matrix.sh | bash)" >> "$GITHUB_OUTPUT"
env:
MATRIX_LINUX_SETUP_COMMAND: apt-get update -y && apt-get install -yq jq && git config --global --add safe.directory /swift-http-types
MATRIX_LINUX_COMMAND: ./scripts/run-linkage-test.sh
MATRIX_LINUX_6_0_ENABLED: false
MATRIX_LINUX_NIGHTLY_NEXT_ENABLED: false
MATRIX_LINUX_NIGHTLY_MAIN_ENABLED: false
linkage-test:
name: Linkage test
needs: construct-linkage-test-matrix
uses: apple/swift-nio/.github/workflows/swift_test_matrix.yml@main
with:
name: "Linkage test"
matrix_string: '${{ needs.construct-linkage-test-matrix.outputs.linkage-test-matrix }}'
+5 -1
View File
@@ -8,6 +8,10 @@ let package = Package(
.library(name: "HTTPTypes", targets: ["HTTPTypes"]),
.library(name: "HTTPTypesFoundation", targets: ["HTTPTypesFoundation"]),
],
traits: [
.trait(name: "FoundationURL", description: "Enable HTTPRequest conveniences with Foundation URL"),
.default(enabledTraits: ["FoundationURL"]),
],
targets: [
.target(name: "HTTPTypes"),
.target(
@@ -33,7 +37,7 @@ let package = Package(
for target in package.targets {
var settings = target.swiftSettings ?? []
settings.append(.enableExperimentalFeature("StrictConcurrency=complete"))
settings.append(.enableUpcomingFeature("InternalImportsByDefault"))
target.swiftSettings = settings
}
+6 -2
View File
@@ -12,10 +12,12 @@
//
//===----------------------------------------------------------------------===//
#if FoundationURL
#if canImport(FoundationEssentials)
import FoundationEssentials
public import FoundationEssentials
#else // canImport(FoundationEssentials)
import Foundation
public import Foundation
#if canImport(CoreFoundation)
import CoreFoundation
#endif // canImport(CoreFoundation)
@@ -214,3 +216,5 @@ extension URL {
#endif // !canImport(FoundationEssentials) && canImport(CoreFoundation)
}
}
#endif
@@ -12,8 +12,6 @@
//
//===----------------------------------------------------------------------===//
import HTTPTypes
extension String {
var isASCII: Bool {
self.utf8.allSatisfy { $0 & 0x80 == 0 }
@@ -12,11 +12,10 @@
//
//===----------------------------------------------------------------------===//
import Foundation
import HTTPTypes
#if canImport(FoundationNetworking)
import FoundationNetworking
public import FoundationNetworking
#else
public import Foundation
#endif
#if !os(WASI)
@@ -12,11 +12,10 @@
//
//===----------------------------------------------------------------------===//
import Foundation
import HTTPTypes
public import Foundation
#if canImport(FoundationNetworking)
import FoundationNetworking
public import FoundationNetworking
#endif
#if !os(WASI)
@@ -12,11 +12,10 @@
//
//===----------------------------------------------------------------------===//
import Foundation
import HTTPTypes
public import Foundation
#if canImport(FoundationNetworking)
import FoundationNetworking
public import FoundationNetworking
#endif
#if !os(WASI)
+2
View File
@@ -0,0 +1,2 @@
.build
Package.resolved
+18
View File
@@ -0,0 +1,18 @@
// swift-tools-version: 6.1
import PackageDescription
let package = Package(
name: "linkage-test",
dependencies: [
.package(name: "swift-http-types", path: "../..", traits: [])
],
targets: [
.executableTarget(
name: "linkageTest",
dependencies: [
.product(name: "HTTPTypes", package: "swift-http-types")
]
)
]
)
@@ -0,0 +1,17 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2026 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of Swift project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import HTTPTypes
print("\(HTTPRequest.self)")
+58
View File
@@ -0,0 +1,58 @@
#!/bin/bash
##===----------------------------------------------------------------------===##
##
## This source file is part of the Swift open source project
##
## Copyright (c) 2026 Apple Inc. and the Swift project authors
## Licensed under Apache License v2.0
##
## See LICENSE.txt for license information
## See CONTRIBUTORS.txt for the list of Swift project authors
##
## SPDX-License-Identifier: Apache-2.0
##
##===----------------------------------------------------------------------===##
set -eu
# Validate that we're running on Linux
if [[ "$(uname -s)" != "Linux" ]]; then
echo "Error: This script must be run on Linux. Current OS: $(uname -s)" >&2
exit 1
fi
echo "Running on Linux - proceeding with linkage test..."
# Build the linkage test package
echo "Building linkage test package..."
swift build --package-path Tests/LinkageTest
# Check the architecture and build path
ARCH=$(uname -m)
if [[ "$ARCH" == "x86_64" ]]; then
BUILD_PATH="Tests/LinkageTest/.build/x86_64-unknown-linux-gnu/debug/linkageTest"
elif [[ "$ARCH" == "aarch64" ]]; then
BUILD_PATH="Tests/LinkageTest/.build/aarch64-unknown-linux-gnu/debug/linkageTest"
else
echo "Error: Unsupported architecture: $ARCH" >&2
exit 1
fi
# Verify the binary exists
if [[ ! -f "$BUILD_PATH" ]]; then
echo "Error: Built binary not found at $BUILD_PATH" >&2
exit 1
fi
echo "Checking linkage for binary: $BUILD_PATH"
# Run ldd and check if libFoundation* is linked
LDD_OUTPUT=$(ldd "$BUILD_PATH")
echo "LDD output:"
echo "$LDD_OUTPUT"
if echo "$LDD_OUTPUT" | grep -q "libFoundation"; then
echo "Error: Binary is linked against libFoundation* - this indicates incorrect linkage. Ensure the full Foundation is not linked on Linux when default traits are disabled." >&2
exit 1
else
echo "Success: Binary is not linked against libFoundation* - linkage test passed."
fi