From 89adc3ed9800a81c54b2fcec804d49f13c381986 Mon Sep 17 00:00:00 2001 From: Kyle Date: Sun, 22 Sep 2024 15:00:08 +0800 Subject: [PATCH 1/4] Bump to Swift 6 compiler for Linux and WASI --- .github/workflows/ubuntu.yml | 3 +- .github/workflows/wasm.yml | 10 +-- Package@swift-5.10.swift | 170 ----------------------------------- Scripts/build.sh | 1 - Scripts/og_swiftinterface.sh | 1 - 5 files changed, 5 insertions(+), 180 deletions(-) delete mode 100644 Package@swift-5.10.swift diff --git a/.github/workflows/ubuntu.yml b/.github/workflows/ubuntu.yml index 5d3780a8..27ddcacb 100644 --- a/.github/workflows/ubuntu.yml +++ b/.github/workflows/ubuntu.yml @@ -12,11 +12,10 @@ jobs: strategy: fail-fast: false matrix: - swift_version: ["5.10"] + swift_version: ["6.0"] runs-on: ubuntu-22.04 env: OPENGRAPH_WERROR: 1 - OPENGRAPH_SWIFT_TESTING: 1 OPENGRAPH_ATTRIBUTEGRAPH: 0 OPENGRAPH_COMPATIBILITY_TEST: 0 container: swift:${{ matrix.swift_version }}-jammy diff --git a/.github/workflows/wasm.yml b/.github/workflows/wasm.yml index 3914d14b..e8c7990d 100644 --- a/.github/workflows/wasm.yml +++ b/.github/workflows/wasm.yml @@ -12,12 +12,11 @@ jobs: strategy: fail-fast: false matrix: - swift_version: ["5.10.0-RELEASE"] + swift_version: ["6.0-SNAPSHOT-2024-09-18-a"] os: [ubuntu-22.04] runs-on: ${{ matrix.os }} env: OPENGRAPH_WERROR: 1 - OPENGRAPH_SWIFT_TESTING: 0 OPENGRAPH_ATTRIBUTEGRAPH: 0 OPENGRAPH_COMPATIBILITY_TEST: 0 steps: @@ -28,7 +27,6 @@ jobs: - name: build run: | swift build --triple wasm32-unknown-wasi - # Blocked by upstream support for WASM. See https://github.com/apple/swift-testing/issues/228 - # - name: test - # run: | - # swift test --triple wasm32-unknown-wasi + - name: test + run: | + swift test --triple wasm32-unknown-wasi \ No newline at end of file diff --git a/Package@swift-5.10.swift b/Package@swift-5.10.swift deleted file mode 100644 index e712d850..00000000 --- a/Package@swift-5.10.swift +++ /dev/null @@ -1,170 +0,0 @@ -// swift-tools-version: 5.10 -// The swift-tools-version declares the minimum version of Swift required to build this package. - -import Foundation -import PackageDescription - -func envEnable(_ key: String, default defaultValue: Bool = false) -> Bool { - guard let value = Context.environment[key] else { - return defaultValue - } - if value == "1" { - return true - } else if value == "0" { - return false - } else { - return defaultValue - } -} - -let isXcodeEnv = Context.environment["__CFBundleIdentifier"] == "com.apple.dt.Xcode" -let development = envEnable("OPENGRAPH_DEVELOPMENT", default: false) - -var sharedSwiftSettings: [SwiftSetting] = [] - -let warningsAsErrorsCondition = envEnable("OPENGRAPH_WERROR", default: isXcodeEnv && development) -if warningsAsErrorsCondition { - sharedSwiftSettings.append(.unsafeFlags(["-warnings-as-errors"])) -} - -let openGraphShimsTarget = Target.target( - name: "OpenGraphShims", - swiftSettings: sharedSwiftSettings -) - -let openGraphShimsTestTarget = Target.testTarget( - name: "OpenGraphShimsTests", - dependencies: [ - "OpenGraphShims", - ], - exclude: ["README.md"], - swiftSettings: sharedSwiftSettings -) - -let openGraphTestTarget = Target.testTarget( - name: "OpenGraphTests", - dependencies: [ - "OpenGraph", - ], - exclude: ["README.md"], - swiftSettings: sharedSwiftSettings -) -let openGraphCompatibilityTestTarget = Target.testTarget( - name: "OpenGraphCompatibilityTests", - exclude: ["README.md"], - swiftSettings: sharedSwiftSettings -) - -let swiftBinPath = Context.environment["_"] ?? "" -let swiftBinURL = URL(fileURLWithPath: swiftBinPath) -let SDKPath = swiftBinURL.deletingLastPathComponent().deletingLastPathComponent().deletingLastPathComponent().path -let includePath = SDKPath.appending("/usr/lib/swift_static") - -let package = Package( - name: "OpenGraph", - platforms: [ - .iOS(.v13), - .macOS(.v10_15), - .macCatalyst(.v13), - .tvOS(.v13), - .watchOS(.v6), - .visionOS(.v1), - ], - products: [ - .library(name: "OpenGraphShims", targets: ["OpenGraphShims"]), - .library(name: "OpenGraph", targets: ["OpenGraph"]), - ], - targets: [ - // FIXME: Merge into one target - // OpenGraph is a C++ & Swift mix target. - // The SwiftPM support for such usage is still in progress. - .target( - name: "_OpenGraph", - cSettings: [ - .unsafeFlags(["-I", includePath], .when(platforms: .nonDarwinPlatforms)), - .define("__COREFOUNDATION_FORSWIFTFOUNDATIONONLY__", to: "1", .when(platforms: .nonDarwinPlatforms)), - ], - cxxSettings: [ - .unsafeFlags(["-I", includePath], .when(platforms: .nonDarwinPlatforms)), - .define("__COREFOUNDATION_FORSWIFTFOUNDATIONONLY__", to: "1", .when(platforms: .nonDarwinPlatforms)), - ] - ), - .target( - name: "OpenGraph", - dependencies: ["_OpenGraph"], - swiftSettings: sharedSwiftSettings - ), - .plugin( - name: "UpdateModule", - capability: .command( - intent: .custom(verb: "update-module", description: "Update AG xcframework"), - permissions: [.writeToPackageDirectory(reason: "Update AG xcframework")] - ) - ), - openGraphShimsTarget, - ], - cxxLanguageStandard: .cxx17 -) - -#if os(macOS) -// FIXME: Enable it by default will cause non-iOS/macOS Apple OS build fail currently. -// Add the corresponding tbd file and framework to fix it. -let attributeGraphCondition = envEnable("OPENGRAPH_ATTRIBUTEGRAPH", default: true) -#else -let attributeGraphCondition = envEnable("OPENGRAPH_ATTRIBUTEGRAPH") -#endif -if attributeGraphCondition { - let attributeGraphProduct = Product.library(name: "AttributeGraph", targets: ["AttributeGraph"]) - let attributeGraphTarget = Target.binaryTarget(name: "AttributeGraph", path: "AG/AttributeGraph.xcframework") - package.products.append(attributeGraphProduct) - package.targets.append(attributeGraphTarget) - - var swiftSettings: [SwiftSetting] = (openGraphShimsTarget.swiftSettings ?? []) - swiftSettings.append(.define("OPENGRAPH_ATTRIBUTEGRAPH")) - openGraphShimsTarget.swiftSettings = swiftSettings - openGraphShimsTarget.dependencies.append("AttributeGraph") -} else { - openGraphShimsTarget.dependencies.append("OpenGraph") -} - -// Remove this when swift-testing is 1.0.0 -let swiftTestingCondition = envEnable("OPENGRAPH_SWIFT_TESTING", default: true) -if swiftTestingCondition { - var dependencies = package.dependencies - dependencies.append(contentsOf: [ - .package(url: "https://github.com/apple/swift-testing", exact: "0.6.0"), - .package(url: "https://github.com/apple/swift-numerics", from: "1.0.2"), - ]) - package.dependencies = dependencies - - func addTestDependency(_ target: Target) { - var dependencies = target.dependencies - dependencies.append(contentsOf: [ - .product(name: "Testing", package: "swift-testing"), - .product(name: "RealModule", package: "swift-numerics"), - ]) - target.dependencies = dependencies - } - addTestDependency(openGraphTestTarget) - package.targets.append(openGraphTestTarget) - addTestDependency(openGraphCompatibilityTestTarget) - package.targets.append(openGraphCompatibilityTestTarget) - addTestDependency(openGraphShimsTestTarget) - package.targets.append(openGraphShimsTestTarget) -} - -let compatibilityTestCondition = envEnable("OPENGRAPH_COMPATIBILITY_TEST") -if compatibilityTestCondition && attributeGraphCondition { - openGraphCompatibilityTestTarget.dependencies.append("AttributeGraph") - var swiftSettings: [SwiftSetting] = (openGraphCompatibilityTestTarget.swiftSettings ?? []) - swiftSettings.append(.define("OPENGRAPH_COMPATIBILITY_TEST")) - openGraphCompatibilityTestTarget.swiftSettings = swiftSettings -} else { - openGraphCompatibilityTestTarget.dependencies.append("OpenGraph") -} - -extension [Platform] { - static var nonDarwinPlatforms: [Platform] { - [.linux, .android, .wasi, .openbsd, .windows] - } -} diff --git a/Scripts/build.sh b/Scripts/build.sh index 1dcc13d7..a12a29d5 100755 --- a/Scripts/build.sh +++ b/Scripts/build.sh @@ -9,5 +9,4 @@ OPENGRAPH_ROOT="$(dirname $(dirname $(filepath $0)))" cd $OPENGRAPH_ROOT -export OPENGRAPH_SWIFT_TESTING=0 swift build diff --git a/Scripts/og_swiftinterface.sh b/Scripts/og_swiftinterface.sh index 49345f1d..12a3817e 100755 --- a/Scripts/og_swiftinterface.sh +++ b/Scripts/og_swiftinterface.sh @@ -9,5 +9,4 @@ OG_ROOT="$(dirname $(dirname $(filepath $0)))" cd $OG_ROOT -export OPENGRAPH_SWIFT_TESTING=0 swift build -Xswiftc -emit-module-interface -Xswiftc -enable-library-evolution From 9ac8d208e12cde6f3ce2b8f77d9d6a5672de1272 Mon Sep 17 00:00:00 2001 From: Kyle Date: Sun, 22 Sep 2024 16:07:15 +0800 Subject: [PATCH 2/4] Fix env issue --- Package.swift | 4 ++-- .../Attribute/Attribute/PointerOffsetTests.swift | 2 -- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/Package.swift b/Package.swift index d1dda28a..62531737 100644 --- a/Package.swift +++ b/Package.swift @@ -60,10 +60,10 @@ let openGraphCompatibilityTestTarget = Target.testTarget( swiftSettings: sharedSwiftSettings ) -let swiftBinPath = Context.environment["_"] ?? "" +let swiftBinPath = Context.environment["_"] ?? "/usr/bin/swift" let swiftBinURL = URL(fileURLWithPath: swiftBinPath) let SDKPath = swiftBinURL.deletingLastPathComponent().deletingLastPathComponent().deletingLastPathComponent().path -let includePath = SDKPath.appending("/usr/lib/swift_static") +let includePath = SDKPath.appending("/usr/lib/swift") let package = Package( name: "OpenGraph", diff --git a/Tests/OpenGraphCompatibilityTests/Attribute/Attribute/PointerOffsetTests.swift b/Tests/OpenGraphCompatibilityTests/Attribute/Attribute/PointerOffsetTests.swift index 00ee6249..03de6eb4 100644 --- a/Tests/OpenGraphCompatibilityTests/Attribute/Attribute/PointerOffsetTests.swift +++ b/Tests/OpenGraphCompatibilityTests/Attribute/Attribute/PointerOffsetTests.swift @@ -42,7 +42,6 @@ struct PointerOffsetTests { #expect(invalidPointer == UnsafeMutablePointer(bitPattern: stride)) } - #if compiler(>=6.0) @Test(.bug("https://github.com/OpenSwiftUIProject/OpenGraph/issues/70", id: 70, "Verify fix of #70")) func ofAndOffset() { struct Empty { @@ -79,7 +78,6 @@ struct PointerOffsetTests { .of(&invalid.third) } } - #endif @Test("Extension API between UnsafePointer/UnsafeMutablePointer and PointerOffset") func unsafePointerAndUnsafeMutablePointerExtension() { From 7897b2b972e3f4f81e6690a7a568562303a9287d Mon Sep 17 00:00:00 2001 From: Kyle Date: Sun, 22 Sep 2024 16:14:22 +0800 Subject: [PATCH 3/4] Fix #73 --- .../UpdateModule/UpdateModuleCommand.swift | 13 +--------- .../Attribute/PointerOffsetTests.swift | 26 ------------------- 2 files changed, 1 insertion(+), 38 deletions(-) diff --git a/Plugins/UpdateModule/UpdateModuleCommand.swift b/Plugins/UpdateModule/UpdateModuleCommand.swift index 49a2704e..75f78d98 100644 --- a/Plugins/UpdateModule/UpdateModuleCommand.swift +++ b/Plugins/UpdateModule/UpdateModuleCommand.swift @@ -11,18 +11,7 @@ import Foundation struct UpdateModuleCommand: CommandPlugin { func performCommand(context: PackagePlugin.PluginContext, arguments: [String]) async throws { let process = Process() - #if os(macOS) - let path: String = try context.tool(named: "zsh").url.path() - let url: URL? = if #available(macOS 14, *) { - URL(filePath: path) - } else { - URL(string: "file://\(path)") - } - #else - let path = try context.tool(named: "zsh").path.string - let url = URL(string: "file://\(path)") - #endif - process.executableURL = url + process.executableURL = try context.tool(named: "zsh").url process.arguments = ["AG/update.sh"] try process.run() process.waitUntilExit() diff --git a/Tests/OpenGraphCompatibilityTests/Attribute/Attribute/PointerOffsetTests.swift b/Tests/OpenGraphCompatibilityTests/Attribute/Attribute/PointerOffsetTests.swift index 03de6eb4..10d5aaaa 100644 --- a/Tests/OpenGraphCompatibilityTests/Attribute/Attribute/PointerOffsetTests.swift +++ b/Tests/OpenGraphCompatibilityTests/Attribute/Attribute/PointerOffsetTests.swift @@ -145,7 +145,6 @@ struct PointerOffsetTests { #expect((pointer + secondOffset).pointee == 4) #expect((pointer + thirdOffset).pointee == 5) } - #if !(!canImport(Darwin) && !DEBUG) withUnsafePointer(to: triple) { pointer in #expect((pointer + firstOffset).pointee == 3) #expect((pointer + secondOffset).pointee == 4) @@ -154,31 +153,6 @@ struct PointerOffsetTests { #expect(triple.first == 3) #expect(triple.second == 4) #expect(triple.third == 5) - #endif - } - } - - #if !canImport(Darwin) && !DEBUG - @Test("Undefined Behavior Issue for PointerOffset", .bug("#73", relationship: .uncoveredBug)) - func unsafePointerAndUnsafeMutablePointerExtensionUB() { - var tuple = Tuple(first: 1, second: 2) - typealias Base = Tuple - let firstOffset = PointerOffset(byteOffset: 0) - withUnsafeMutablePointer(to: &tuple) { pointer in - #expect(pointer[offset: firstOffset] == 1) - pointer[offset: firstOffset] = 3 - } - let unexpectedValue = tuple.first - // The value will be unexpected due to UB - #expect(unexpectedValue == 1) - // The value will be expected after and within a withKnownIssue block - withKnownIssue { - let expectedValue = tuple.first - #expect(expectedValue == 3) - Issue.record("To make withKnownIssue pass") } - let expectedValue = tuple.first - #expect(expectedValue == 3) } - #endif } From 5d43a4ef2ef21b646353d1ed229524a224edaa9f Mon Sep 17 00:00:00 2001 From: Kyle Date: Sun, 22 Sep 2024 16:44:28 +0800 Subject: [PATCH 4/4] Fix WASM CI issue --- .github/workflows/wasm.yml | 12 +- Package@swift-5.10.swift | 170 ++++++++++++++++++ .../UpdateModule/UpdateModuleCommand.swift | 2 + 3 files changed, 180 insertions(+), 4 deletions(-) create mode 100644 Package@swift-5.10.swift diff --git a/.github/workflows/wasm.yml b/.github/workflows/wasm.yml index e8c7990d..5e9153b0 100644 --- a/.github/workflows/wasm.yml +++ b/.github/workflows/wasm.yml @@ -12,8 +12,11 @@ jobs: strategy: fail-fast: false matrix: - swift_version: ["6.0-SNAPSHOT-2024-09-18-a"] + swift_version: ["5.10.0-RELEASE", "6.0-SNAPSHOT-2024-09-18-a"] os: [ubuntu-22.04] + include: + - swift_version: "6.0-SNAPSHOT-2024-09-18-a" + continue-on-error: true runs-on: ${{ matrix.os }} env: OPENGRAPH_WERROR: 1 @@ -25,8 +28,9 @@ jobs: with: swift-version: wasm-${{ matrix.swift_version }} - name: build + continue-on-error: ${{ matrix.continue-on-error }} run: | swift build --triple wasm32-unknown-wasi - - name: test - run: | - swift test --triple wasm32-unknown-wasi \ No newline at end of file + # - name: test + # run: | + # swift test --triple wasm32-unknown-wasi \ No newline at end of file diff --git a/Package@swift-5.10.swift b/Package@swift-5.10.swift new file mode 100644 index 00000000..2701a026 --- /dev/null +++ b/Package@swift-5.10.swift @@ -0,0 +1,170 @@ +// swift-tools-version: 5.10 +// The swift-tools-version declares the minimum version of Swift required to build this package. + +import Foundation +import PackageDescription + +func envEnable(_ key: String, default defaultValue: Bool = false) -> Bool { + guard let value = Context.environment[key] else { + return defaultValue + } + if value == "1" { + return true + } else if value == "0" { + return false + } else { + return defaultValue + } +} + +let isXcodeEnv = Context.environment["__CFBundleIdentifier"] == "com.apple.dt.Xcode" +let development = envEnable("OPENGRAPH_DEVELOPMENT", default: false) + +var sharedSwiftSettings: [SwiftSetting] = [] + +let warningsAsErrorsCondition = envEnable("OPENGRAPH_WERROR", default: isXcodeEnv && development) +if warningsAsErrorsCondition { + sharedSwiftSettings.append(.unsafeFlags(["-warnings-as-errors"])) +} + +let openGraphShimsTarget = Target.target( + name: "OpenGraphShims", + swiftSettings: sharedSwiftSettings +) + +let openGraphShimsTestTarget = Target.testTarget( + name: "OpenGraphShimsTests", + dependencies: [ + "OpenGraphShims", + ], + exclude: ["README.md"], + swiftSettings: sharedSwiftSettings +) + +let openGraphTestTarget = Target.testTarget( + name: "OpenGraphTests", + dependencies: [ + "OpenGraph", + ], + exclude: ["README.md"], + swiftSettings: sharedSwiftSettings +) +let openGraphCompatibilityTestTarget = Target.testTarget( + name: "OpenGraphCompatibilityTests", + exclude: ["README.md"], + swiftSettings: sharedSwiftSettings +) + +let swiftBinPath = Context.environment["_"] ?? "/usr/bin/swift" +let swiftBinURL = URL(fileURLWithPath: swiftBinPath) +let SDKPath = swiftBinURL.deletingLastPathComponent().deletingLastPathComponent().deletingLastPathComponent().path +let includePath = SDKPath.appending("/usr/lib/swift_static") + +let package = Package( + name: "OpenGraph", + platforms: [ + .iOS(.v13), + .macOS(.v10_15), + .macCatalyst(.v13), + .tvOS(.v13), + .watchOS(.v6), + .visionOS(.v1), + ], + products: [ + .library(name: "OpenGraphShims", targets: ["OpenGraphShims"]), + .library(name: "OpenGraph", targets: ["OpenGraph"]), + ], + targets: [ + // FIXME: Merge into one target + // OpenGraph is a C++ & Swift mix target. + // The SwiftPM support for such usage is still in progress. + .target( + name: "_OpenGraph", + cSettings: [ + .unsafeFlags(["-I", includePath], .when(platforms: .nonDarwinPlatforms)), + .define("__COREFOUNDATION_FORSWIFTFOUNDATIONONLY__", to: "1", .when(platforms: .nonDarwinPlatforms)), + ], + cxxSettings: [ + .unsafeFlags(["-I", includePath], .when(platforms: .nonDarwinPlatforms)), + .define("__COREFOUNDATION_FORSWIFTFOUNDATIONONLY__", to: "1", .when(platforms: .nonDarwinPlatforms)), + ] + ), + .target( + name: "OpenGraph", + dependencies: ["_OpenGraph"], + swiftSettings: sharedSwiftSettings + ), + .plugin( + name: "UpdateModule", + capability: .command( + intent: .custom(verb: "update-module", description: "Update AG xcframework"), + permissions: [.writeToPackageDirectory(reason: "Update AG xcframework")] + ) + ), + openGraphShimsTarget, + ], + cxxLanguageStandard: .cxx17 +) + +#if os(macOS) +// FIXME: Enable it by default will cause non-iOS/macOS Apple OS build fail currently. +// Add the corresponding tbd file and framework to fix it. +let attributeGraphCondition = envEnable("OPENGRAPH_ATTRIBUTEGRAPH", default: true) +#else +let attributeGraphCondition = envEnable("OPENGRAPH_ATTRIBUTEGRAPH") +#endif +if attributeGraphCondition { + let attributeGraphProduct = Product.library(name: "AttributeGraph", targets: ["AttributeGraph"]) + let attributeGraphTarget = Target.binaryTarget(name: "AttributeGraph", path: "AG/AttributeGraph.xcframework") + package.products.append(attributeGraphProduct) + package.targets.append(attributeGraphTarget) + + var swiftSettings: [SwiftSetting] = (openGraphShimsTarget.swiftSettings ?? []) + swiftSettings.append(.define("OPENGRAPH_ATTRIBUTEGRAPH")) + openGraphShimsTarget.swiftSettings = swiftSettings + openGraphShimsTarget.dependencies.append("AttributeGraph") +} else { + openGraphShimsTarget.dependencies.append("OpenGraph") +} + +// Remove this when swift-testing is 1.0.0 +let swiftTestingCondition = envEnable("OPENGRAPH_SWIFT_TESTING", default: true) +if swiftTestingCondition { + var dependencies = package.dependencies + dependencies.append(contentsOf: [ + .package(url: "https://github.com/apple/swift-testing", exact: "0.6.0"), + .package(url: "https://github.com/apple/swift-numerics", from: "1.0.2"), + ]) + package.dependencies = dependencies + + func addTestDependency(_ target: Target) { + var dependencies = target.dependencies + dependencies.append(contentsOf: [ + .product(name: "Testing", package: "swift-testing"), + .product(name: "RealModule", package: "swift-numerics"), + ]) + target.dependencies = dependencies + } + addTestDependency(openGraphTestTarget) + package.targets.append(openGraphTestTarget) + addTestDependency(openGraphCompatibilityTestTarget) + package.targets.append(openGraphCompatibilityTestTarget) + addTestDependency(openGraphShimsTestTarget) + package.targets.append(openGraphShimsTestTarget) +} + +let compatibilityTestCondition = envEnable("OPENGRAPH_COMPATIBILITY_TEST") +if compatibilityTestCondition && attributeGraphCondition { + openGraphCompatibilityTestTarget.dependencies.append("AttributeGraph") + var swiftSettings: [SwiftSetting] = (openGraphCompatibilityTestTarget.swiftSettings ?? []) + swiftSettings.append(.define("OPENGRAPH_COMPATIBILITY_TEST")) + openGraphCompatibilityTestTarget.swiftSettings = swiftSettings +} else { + openGraphCompatibilityTestTarget.dependencies.append("OpenGraph") +} + +extension [Platform] { + static var nonDarwinPlatforms: [Platform] { + [.linux, .android, .wasi, .openbsd, .windows] + } +} diff --git a/Plugins/UpdateModule/UpdateModuleCommand.swift b/Plugins/UpdateModule/UpdateModuleCommand.swift index 75f78d98..b45ffb3d 100644 --- a/Plugins/UpdateModule/UpdateModuleCommand.swift +++ b/Plugins/UpdateModule/UpdateModuleCommand.swift @@ -10,10 +10,12 @@ import Foundation @main struct UpdateModuleCommand: CommandPlugin { func performCommand(context: PackagePlugin.PluginContext, arguments: [String]) async throws { + #if compiler(>=6.0) // Skip build on WASI 5.10 let process = Process() process.executableURL = try context.tool(named: "zsh").url process.arguments = ["AG/update.sh"] try process.run() process.waitUntilExit() + #endif } }