Skip to content

Commit

Permalink
Fix Swift 6 build on Linux (#278)
Browse files Browse the repository at this point in the history
  • Loading branch information
waahm7 committed Sep 10, 2024
1 parent 26be737 commit ec04c6a
Show file tree
Hide file tree
Showing 15 changed files with 82 additions and 92 deletions.
1 change: 1 addition & 0 deletions .swiftlint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ disabled_rules:
- syntactic_sugar
- unused_capture_list
- blanket_disable_command
- trailing_comma

opt_in_rules:
- empty_count
Expand Down
15 changes: 8 additions & 7 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import PackageDescription

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

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

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

//////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -81,10 +85,8 @@ awsCCalPlatformExcludes.append("source/unix")
/// s2n-tls
//////////////////////////////////////////////////////////////////////
#if os(Linux)
// add pq-crypto back after adding in platform and chipset detection
let s2nExcludes = ["bin", "codebuild", "coverage", "docker-images",
"docs", "lib", "pq-crypto/kyber_r3",
"pq-crypto/README.md", "pq-crypto/Makefile", "pq-crypto/s2n_pq_asm.mk",
"docs", "lib",
"libcrypto-build", "scram",
"s2n.mk", "Makefile", "stuffer/Makefile", "crypto/Makefile",
"tls/Makefile", "utils/Makefile", "error/Makefile", "tls/extensions/Makefile",
Expand All @@ -108,7 +110,7 @@ packageTargets.append(.target(
/// aws-c-io
//////////////////////////////////////////////////////////////////////
var ioDependencies: [Target.Dependency] = ["AwsCCommon", "AwsCCal"]
var awsCIoPlatformExcludes = ["docs", "CODE_OF_CONDUCT.md", "codebuild", "PKCS11.md", "THIRD-PARTY-LICENSES.txt",
var awsCIoPlatformExcludes = ["docs", "CODE_OF_CONDUCT.md", "codebuild", "PKCS11.md",
"source/pkcs11/v2.40"] + excludesFromAll
var cSettingsIO = cSettings

Expand Down Expand Up @@ -140,7 +142,6 @@ var awsCChecksumsExcludes = [
"LICENSE",
"builder.json",
"README.md",
"format-check.sh",
"cmake",
"tests"]

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0.
// Copyright Amazon.com, Inc. or its affiliates.
// All Rights Reserved. SPDX-License-Identifier: Apache-2.0.

import AwsCAuth
import AwsCIo
Expand Down Expand Up @@ -570,6 +570,13 @@ private func onGetCredentials(credentials: OpaquePointer?,
continuationCore.continuation.resume(returning: Credentials(rawValue: credentials!))
}

// We need to share this pointer to C in a task block but Swift compiler complains
// that Pointer does not conform to Sendable. Wrap the pointer in a @unchecked Sendable block
// for Swift compiler to stop complaining.
struct SendablePointer: @unchecked Sendable {
let pointer: UnsafeMutableRawPointer
}

private func getCredentialsDelegateFn(_ delegatePtr: UnsafeMutableRawPointer!,
_ callbackFn: (@convention(c) (
OpaquePointer?,
Expand All @@ -579,14 +586,15 @@ private func getCredentialsDelegateFn(_ delegatePtr: UnsafeMutableRawPointer!,
let delegate = Unmanaged<Box<CredentialsProviding>>
.fromOpaque(delegatePtr)
.takeUnretainedValue().contents
let userData = SendablePointer(pointer: userData)
Task {
do {
let credentials = try await delegate.getCredentials()
callbackFn(credentials.rawValue, AWS_OP_SUCCESS, userData)
callbackFn(credentials.rawValue, AWS_OP_SUCCESS, userData.pointer)
} catch CommonRunTimeError.crtError(let crtError) {
callbackFn(nil, crtError.code, userData)
callbackFn(nil, crtError.code, userData.pointer)
} catch {
callbackFn(nil, Int32(AWS_AUTH_CREDENTIALS_PROVIDER_DELEGATE_FAILURE.rawValue), userData)
callbackFn(nil, Int32(AWS_AUTH_CREDENTIALS_PROVIDER_DELEGATE_FAILURE.rawValue), userData.pointer)
}
}
return AWS_OP_SUCCESS
Expand Down
75 changes: 37 additions & 38 deletions Source/Elasticurl/Elasticurl.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0.

import _Concurrency
import AwsCommonRuntimeKit
import Foundation
import _Concurrency

// swiftlint:disable cyclomatic_complexity function_body_length
struct Context {
Expand All @@ -15,16 +15,15 @@ struct Context {
public var certificate: String?
public var privateKey: String?
public var connectTimeout: Int = 3000
public var headers: [String: String] = [String: String]()
public var headers: [String: String] = .init()
public var includeHeaders: Bool = false
public var outputFileName: String?
public var traceFile: String?
public var insecure: Bool = false
public var url: URL = URL(fileURLWithPath: "")
public var url: URL = .init(fileURLWithPath: "")
public var data: Data?
public var alpnList: [String] = []
public var outputStream = FileHandle.standardOutput

}

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

static func parseArguments() {

let optionString = "a:b:c:e:f:H:d:g:j:l:m:M:GPHiko:t:v:VwWh"
let options = [ElasticurlOptions.caCert.rawValue,
ElasticurlOptions.caPath.rawValue,
Expand Down Expand Up @@ -100,7 +98,6 @@ struct Elasticurl {
}

if let dataFilePath = argumentsDict["g"] as? String {

guard let url = URL(string: dataFilePath) else {
print("path to data file is incorrect or does not exist")
exit(-1)
Expand Down Expand Up @@ -161,7 +158,7 @@ struct Elasticurl {
context.alpnList.append("h2")
}

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

// make sure a url was given before we do anything else
guard let urlString = CommandLine.arguments.last,
let url = URL(string: urlString) else {
let url = URL(string: urlString)
else {
print("Invalid URL: \(CommandLine.arguments.last!)")
exit(-1)
}
Expand Down Expand Up @@ -258,8 +256,6 @@ struct Elasticurl {

let socketOptions = SocketOptions(socketType: .stream)

let semaphore = DispatchSemaphore(value: 0)

var stream: HTTPStream?
let path = context.url.path == "" ? "/" : context.url.path
let httpRequest: HTTPRequest = try HTTPRequest(method: context.verb, path: path)
Expand All @@ -276,22 +272,6 @@ struct Elasticurl {
}
httpRequest.addHeaders(headers: headers)

let onResponse: HTTPRequestOptions.OnResponse = { _, headers in
for header in headers {
print(header.name + " : " + header.value)
}
}

let onBody: HTTPRequestOptions.OnIncomingBody = { bodyChunk in
writeData(data: bodyChunk)
}

let onComplete: HTTPRequestOptions.OnStreamComplete = { result in
print(result)

semaphore.signal()
}

let httpClientOptions = HTTPClientConnectionOptions(clientBootstrap: bootstrap,
hostName: context.url.host!,
initialWindowSize: Int.max,
Expand All @@ -302,21 +282,40 @@ struct Elasticurl {
monitoringOptions: nil)

let connectionManager = try HTTPClientConnectionManager(options: httpClientOptions)
do {
let connection = try await connectionManager.acquireConnection()
let requestOptions = HTTPRequestOptions(request: httpRequest,
onResponse: onResponse,
onIncomingBody: onBody,
onStreamComplete: onComplete)
stream = try connection.makeRequest(requestOptions: requestOptions)
try stream!.activate()
let connection = try await connectionManager.acquireConnection()
try await withCheckedThrowingContinuation { continuation in
let onResponse: HTTPRequestOptions.OnResponse = { _, headers in
for header in headers {
print(header.name + " : " + header.value)
}
}

} catch {
print("connection has shut down with error: \(error.localizedDescription)" )
semaphore.signal()
let onBody: HTTPRequestOptions.OnIncomingBody = { bodyChunk in
writeData(data: bodyChunk)
}

let onComplete: HTTPRequestOptions.OnStreamComplete = { result in
switch result {
case let .success(status):
print("response status:\(status)")
continuation.resume(returning: ())
case let .failure(error):
continuation.resume(throwing: error)
}
}

do {
let requestOptions = HTTPRequestOptions(request: httpRequest,
onResponse: onResponse,
onIncomingBody: onBody,
onStreamComplete: onComplete)
stream = try connection.makeRequest(requestOptions: requestOptions)
try stream!.activate()
} catch {
continuation.resume(throwing: error)
}
}

semaphore.wait()
exit(EXIT_SUCCESS)
} catch let err {
showHelp()
Expand Down
5 changes: 4 additions & 1 deletion Test/AwsCommonRuntimeKitTests/Resources/example_profile.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
[default]
aws_access_key_id = default_access_key_id
aws_secret_access_key = default_secret_access_key
credential_process = echo '{"Version": 1, "AccessKeyId": "AccessKey123", "SecretAccessKey": "SecretAccessKey123", "SessionToken": "SessionToken123","Expiration":"2020-02-25T06:03:31Z"}'
s3 =
max_concurrent_requests = 20

[profile process]
credential_process = echo '{"Version": 1, "AccessKeyId": "AccessKey123", "SecretAccessKey": "SecretAccessKey123", "SessionToken": "SessionToken123","Expiration":"2020-02-25T06:03:31Z"}'

[profile crt_user]
aws_access_key_id = example_access_key_id
aws_secret_access_key = example_secret_access_key
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ class CredentialsProviderTests: XCBaseTestCase {
do {
let provider = try CredentialsProvider(source: .process(
fileBasedConfiguration: FileBasedConfiguration(
configFilePath: Bundle.module.path(forResource: "example_profile", ofType: "txt")!),
configFilePath: Bundle.module.path(forResource: "example_profile", ofType: "txt")!),
profileFileNameOverride: "process",
shutdownCallback: getShutdownCallback()))
let credentials = try await provider.getCredentials()
XCTAssertNotNil(credentials)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class FileBasedConfigurationTests: XCBaseTestCase {
let fileBasedConfiguration = try FileBasedConfiguration(configFilePath: profilePath, credentialsFilePath: configPath)
XCTAssertNotNil(fileBasedConfiguration)
let defaultSection = fileBasedConfiguration.getSection(name: "default", sectionType: .profile)!
XCTAssertEqual(defaultSection.propertyCount, 4)
XCTAssertEqual(defaultSection.propertyCount, 3)
let property = defaultSection.getProperty(name: "aws_access_key_id")!
XCTAssertEqual("accessKey", property.value)

Expand Down
2 changes: 1 addition & 1 deletion aws-common-runtime/aws-c-cal
41 changes: 9 additions & 32 deletions aws-common-runtime/config/aws/common/config.h
Original file line number Diff line number Diff line change
@@ -1,48 +1,25 @@
#ifdef __APPLE__
# include <TargetConditionals.h>
#endif

#ifndef AWS_COMMON_CONFIG_H
# define AWS_COMMON_CONFIG_H
/**
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/

#ifndef AWS_COMMON_CONFIG_H
#define AWS_COMMON_CONFIG_H
/*
* This header exposes compiler feature test results determined during cmake
* configure time to inline function implementations. The macros defined here
* should be considered to be an implementation detail, and can change at any
* time.
*/
# if TARGET_OS_IPHONE
# define AWS_HAVE_GCC_OVERFLOW_MATH_EXTENSIONS
# define AWS_HAVE_GCC_INLINE_ASM
/* #undef AWS_HAVE_MSVC_MULX */
# define AWS_C_CAL_OPENSSLCRYPTO_COMMON_H
# elif TARGET_OS_SIMULATOR
# define AWS_HAVE_GCC_OVERFLOW_MATH_EXTENSIONS
# define AWS_HAVE_GCC_INLINE_ASM
/* #undef AWS_HAVE_MSVC_MULX */
# define AWS_C_CAL_OPENSSLCRYPTO_COMMON_H
# elif TARGET_OS_MAC
# define AWS_HAVE_GCC_OVERFLOW_MATH_EXTENSIONS
# define AWS_HAVE_GCC_INLINE_ASM
/* #undef AWS_HAVE_MSVC_MULX */
# define AWS_C_CAL_OPENSSLCRYPTO_COMMON_H
# endif
#else
# define AWS_HAVE_GCC_OVERFLOW_MATH_EXTENSIONS
# define AWS_HAVE_GCC_INLINE_ASM
/* #undef AWS_HAVE_MSVC_MULX */
#ifdef __APPLE__
/* This is a trick to skip OpenSSL header on Apple platforms since Swift Package Manager is not smart enough to exclude
* some headers.
*/
# define AWS_C_CAL_OPENSSLCRYPTO_COMMON_H
#endif
#define AWS_UNSTABLE_TESTING_API 1

#define AWS_UNSTABLE_TESTING_API 1
#define AWS_AFFINITY_METHOD 0

#define AWS_HAVE_EXECINFO 1
/* Disable Intel VTune tracing API here since aws-crt-swift doesn't use CMake */
#define INTEL_NO_ITTNOTIFY_API

/* Don't use APIs forbidden by App Stores (e.g. non-public system APIs) */
#define AWS_APPSTORE_SAFE 1
#endif
2 changes: 1 addition & 1 deletion aws-common-runtime/s2n
Submodule s2n updated 255 files

0 comments on commit ec04c6a

Please sign in to comment.