Skip to content

Commit ec04c6a

Browse files
authored
Fix Swift 6 build on Linux (#278)
1 parent 26be737 commit ec04c6a

15 files changed

Lines changed: 82 additions & 92 deletions

File tree

.swiftlint.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ disabled_rules:
1717
- syntactic_sugar
1818
- unused_capture_list
1919
- blanket_disable_command
20+
- trailing_comma
2021

2122
opt_in_rules:
2223
- empty_count

Package.swift

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import PackageDescription
33

44
let excludesFromAll = ["tests", "cmake", "CONTRIBUTING.md",
5-
"LICENSE", "format-check.sh", "NOTICE", "builder.json",
5+
"LICENSE", "format-check.py", "NOTICE", "builder.json",
66
"CMakeLists.txt", "README.md"]
77
var packageTargets: [Target] = []
88

@@ -15,7 +15,11 @@ var package = Package(name: "aws-crt-swift",
1515
)
1616

1717
let cSettings: [CSetting] = [
18-
.define("DEBUG_BUILD", .when(configuration: .debug))
18+
.define("DEBUG_BUILD", .when(configuration: .debug)),
19+
// Disable Intel VTune tracing API here since aws-crt-swift doesn't use CMake
20+
.define("INTEL_NO_ITTNOTIFY_API"),
21+
// Don't use APIs forbidden by App Stores (e.g. non-public system APIs)
22+
.define("AWS_APPSTORE_SAFE"),
1923
]
2024

2125
//////////////////////////////////////////////////////////////////////
@@ -81,10 +85,8 @@ awsCCalPlatformExcludes.append("source/unix")
8185
/// s2n-tls
8286
//////////////////////////////////////////////////////////////////////
8387
#if os(Linux)
84-
// add pq-crypto back after adding in platform and chipset detection
8588
let s2nExcludes = ["bin", "codebuild", "coverage", "docker-images",
86-
"docs", "lib", "pq-crypto/kyber_r3",
87-
"pq-crypto/README.md", "pq-crypto/Makefile", "pq-crypto/s2n_pq_asm.mk",
89+
"docs", "lib",
8890
"libcrypto-build", "scram",
8991
"s2n.mk", "Makefile", "stuffer/Makefile", "crypto/Makefile",
9092
"tls/Makefile", "utils/Makefile", "error/Makefile", "tls/extensions/Makefile",
@@ -108,7 +110,7 @@ packageTargets.append(.target(
108110
/// aws-c-io
109111
//////////////////////////////////////////////////////////////////////
110112
var ioDependencies: [Target.Dependency] = ["AwsCCommon", "AwsCCal"]
111-
var awsCIoPlatformExcludes = ["docs", "CODE_OF_CONDUCT.md", "codebuild", "PKCS11.md", "THIRD-PARTY-LICENSES.txt",
113+
var awsCIoPlatformExcludes = ["docs", "CODE_OF_CONDUCT.md", "codebuild", "PKCS11.md",
112114
"source/pkcs11/v2.40"] + excludesFromAll
113115
var cSettingsIO = cSettings
114116

@@ -140,7 +142,6 @@ var awsCChecksumsExcludes = [
140142
"LICENSE",
141143
"builder.json",
142144
"README.md",
143-
"format-check.sh",
144145
"cmake",
145146
"tests"]
146147

Source/AwsCommonRuntimeKit/auth/credentials/CredentialsProvider.swift

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2-
// SPDX-License-Identifier: Apache-2.0.
1+
// Copyright Amazon.com, Inc. or its affiliates.
2+
// All Rights Reserved. SPDX-License-Identifier: Apache-2.0.
33

44
import AwsCAuth
55
import AwsCIo
@@ -570,6 +570,13 @@ private func onGetCredentials(credentials: OpaquePointer?,
570570
continuationCore.continuation.resume(returning: Credentials(rawValue: credentials!))
571571
}
572572

573+
// We need to share this pointer to C in a task block but Swift compiler complains
574+
// that Pointer does not conform to Sendable. Wrap the pointer in a @unchecked Sendable block
575+
// for Swift compiler to stop complaining.
576+
struct SendablePointer: @unchecked Sendable {
577+
let pointer: UnsafeMutableRawPointer
578+
}
579+
573580
private func getCredentialsDelegateFn(_ delegatePtr: UnsafeMutableRawPointer!,
574581
_ callbackFn: (@convention(c) (
575582
OpaquePointer?,
@@ -579,14 +586,15 @@ private func getCredentialsDelegateFn(_ delegatePtr: UnsafeMutableRawPointer!,
579586
let delegate = Unmanaged<Box<CredentialsProviding>>
580587
.fromOpaque(delegatePtr)
581588
.takeUnretainedValue().contents
589+
let userData = SendablePointer(pointer: userData)
582590
Task {
583591
do {
584592
let credentials = try await delegate.getCredentials()
585-
callbackFn(credentials.rawValue, AWS_OP_SUCCESS, userData)
593+
callbackFn(credentials.rawValue, AWS_OP_SUCCESS, userData.pointer)
586594
} catch CommonRunTimeError.crtError(let crtError) {
587-
callbackFn(nil, crtError.code, userData)
595+
callbackFn(nil, crtError.code, userData.pointer)
588596
} catch {
589-
callbackFn(nil, Int32(AWS_AUTH_CREDENTIALS_PROVIDER_DELEGATE_FAILURE.rawValue), userData)
597+
callbackFn(nil, Int32(AWS_AUTH_CREDENTIALS_PROVIDER_DELEGATE_FAILURE.rawValue), userData.pointer)
590598
}
591599
}
592600
return AWS_OP_SUCCESS

Source/Elasticurl/Elasticurl.swift

Lines changed: 37 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0.
33

4+
import _Concurrency
45
import AwsCommonRuntimeKit
56
import Foundation
6-
import _Concurrency
77

88
// swiftlint:disable cyclomatic_complexity function_body_length
99
struct Context {
@@ -15,16 +15,15 @@ struct Context {
1515
public var certificate: String?
1616
public var privateKey: String?
1717
public var connectTimeout: Int = 3000
18-
public var headers: [String: String] = [String: String]()
18+
public var headers: [String: String] = .init()
1919
public var includeHeaders: Bool = false
2020
public var outputFileName: String?
2121
public var traceFile: String?
2222
public var insecure: Bool = false
23-
public var url: URL = URL(fileURLWithPath: "")
23+
public var url: URL = .init(fileURLWithPath: "")
2424
public var data: Data?
2525
public var alpnList: [String] = []
2626
public var outputStream = FileHandle.standardOutput
27-
2827
}
2928

3029
@main
@@ -33,7 +32,6 @@ struct Elasticurl {
3332
private static var context = Context()
3433

3534
static func parseArguments() {
36-
3735
let optionString = "a:b:c:e:f:H:d:g:j:l:m:M:GPHiko:t:v:VwWh"
3836
let options = [ElasticurlOptions.caCert.rawValue,
3937
ElasticurlOptions.caPath.rawValue,
@@ -100,7 +98,6 @@ struct Elasticurl {
10098
}
10199

102100
if let dataFilePath = argumentsDict["g"] as? String {
103-
104101
guard let url = URL(string: dataFilePath) else {
105102
print("path to data file is incorrect or does not exist")
106103
exit(-1)
@@ -161,7 +158,7 @@ struct Elasticurl {
161158
context.alpnList.append("h2")
162159
}
163160

164-
if argumentsDict["w"] == nil && argumentsDict["W"] == nil {
161+
if argumentsDict["w"] == nil, argumentsDict["W"] == nil {
165162
context.alpnList.append("h2")
166163
context.alpnList.append("http/1.1")
167164
}
@@ -173,7 +170,8 @@ struct Elasticurl {
173170

174171
// make sure a url was given before we do anything else
175172
guard let urlString = CommandLine.arguments.last,
176-
let url = URL(string: urlString) else {
173+
let url = URL(string: urlString)
174+
else {
177175
print("Invalid URL: \(CommandLine.arguments.last!)")
178176
exit(-1)
179177
}
@@ -258,8 +256,6 @@ struct Elasticurl {
258256

259257
let socketOptions = SocketOptions(socketType: .stream)
260258

261-
let semaphore = DispatchSemaphore(value: 0)
262-
263259
var stream: HTTPStream?
264260
let path = context.url.path == "" ? "/" : context.url.path
265261
let httpRequest: HTTPRequest = try HTTPRequest(method: context.verb, path: path)
@@ -276,22 +272,6 @@ struct Elasticurl {
276272
}
277273
httpRequest.addHeaders(headers: headers)
278274

279-
let onResponse: HTTPRequestOptions.OnResponse = { _, headers in
280-
for header in headers {
281-
print(header.name + " : " + header.value)
282-
}
283-
}
284-
285-
let onBody: HTTPRequestOptions.OnIncomingBody = { bodyChunk in
286-
writeData(data: bodyChunk)
287-
}
288-
289-
let onComplete: HTTPRequestOptions.OnStreamComplete = { result in
290-
print(result)
291-
292-
semaphore.signal()
293-
}
294-
295275
let httpClientOptions = HTTPClientConnectionOptions(clientBootstrap: bootstrap,
296276
hostName: context.url.host!,
297277
initialWindowSize: Int.max,
@@ -302,21 +282,40 @@ struct Elasticurl {
302282
monitoringOptions: nil)
303283

304284
let connectionManager = try HTTPClientConnectionManager(options: httpClientOptions)
305-
do {
306-
let connection = try await connectionManager.acquireConnection()
307-
let requestOptions = HTTPRequestOptions(request: httpRequest,
308-
onResponse: onResponse,
309-
onIncomingBody: onBody,
310-
onStreamComplete: onComplete)
311-
stream = try connection.makeRequest(requestOptions: requestOptions)
312-
try stream!.activate()
285+
let connection = try await connectionManager.acquireConnection()
286+
try await withCheckedThrowingContinuation { continuation in
287+
let onResponse: HTTPRequestOptions.OnResponse = { _, headers in
288+
for header in headers {
289+
print(header.name + " : " + header.value)
290+
}
291+
}
313292

314-
} catch {
315-
print("connection has shut down with error: \(error.localizedDescription)" )
316-
semaphore.signal()
293+
let onBody: HTTPRequestOptions.OnIncomingBody = { bodyChunk in
294+
writeData(data: bodyChunk)
295+
}
296+
297+
let onComplete: HTTPRequestOptions.OnStreamComplete = { result in
298+
switch result {
299+
case let .success(status):
300+
print("response status:\(status)")
301+
continuation.resume(returning: ())
302+
case let .failure(error):
303+
continuation.resume(throwing: error)
304+
}
305+
}
306+
307+
do {
308+
let requestOptions = HTTPRequestOptions(request: httpRequest,
309+
onResponse: onResponse,
310+
onIncomingBody: onBody,
311+
onStreamComplete: onComplete)
312+
stream = try connection.makeRequest(requestOptions: requestOptions)
313+
try stream!.activate()
314+
} catch {
315+
continuation.resume(throwing: error)
316+
}
317317
}
318318

319-
semaphore.wait()
320319
exit(EXIT_SUCCESS)
321320
} catch let err {
322321
showHelp()

Test/AwsCommonRuntimeKitTests/Resources/example_profile.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
[default]
22
aws_access_key_id = default_access_key_id
33
aws_secret_access_key = default_secret_access_key
4-
credential_process = echo '{"Version": 1, "AccessKeyId": "AccessKey123", "SecretAccessKey": "SecretAccessKey123", "SessionToken": "SessionToken123","Expiration":"2020-02-25T06:03:31Z"}'
54
s3 =
65
max_concurrent_requests = 20
6+
7+
[profile process]
8+
credential_process = echo '{"Version": 1, "AccessKeyId": "AccessKey123", "SecretAccessKey": "SecretAccessKey123", "SessionToken": "SessionToken123","Expiration":"2020-02-25T06:03:31Z"}'
9+
710
[profile crt_user]
811
aws_access_key_id = example_access_key_id
912
aws_secret_access_key = example_secret_access_key

Test/AwsCommonRuntimeKitTests/auth/CredentialsProviderTests.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,8 @@ class CredentialsProviderTests: XCBaseTestCase {
133133
do {
134134
let provider = try CredentialsProvider(source: .process(
135135
fileBasedConfiguration: FileBasedConfiguration(
136-
configFilePath: Bundle.module.path(forResource: "example_profile", ofType: "txt")!),
136+
configFilePath: Bundle.module.path(forResource: "example_profile", ofType: "txt")!),
137+
profileFileNameOverride: "process",
137138
shutdownCallback: getShutdownCallback()))
138139
let credentials = try await provider.getCredentials()
139140
XCTAssertNotNil(credentials)

Test/AwsCommonRuntimeKitTests/sdkutils/FileBasedConfigurationTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class FileBasedConfigurationTests: XCBaseTestCase {
1313
let fileBasedConfiguration = try FileBasedConfiguration(configFilePath: profilePath, credentialsFilePath: configPath)
1414
XCTAssertNotNil(fileBasedConfiguration)
1515
let defaultSection = fileBasedConfiguration.getSection(name: "default", sectionType: .profile)!
16-
XCTAssertEqual(defaultSection.propertyCount, 4)
16+
XCTAssertEqual(defaultSection.propertyCount, 3)
1717
let property = defaultSection.getProperty(name: "aws_access_key_id")!
1818
XCTAssertEqual("accessKey", property.value)
1919

0 commit comments

Comments
 (0)