|
| 1 | +import Foundation |
| 2 | +import Testing |
| 3 | +@testable @_spi(Internals) import ScipioKit |
| 4 | + |
| 5 | +private let fixturePath = URL(filePath: #filePath) |
| 6 | + .deletingLastPathComponent() |
| 7 | + .appending(components: "Resources", "Fixtures") |
| 8 | + |
| 9 | +@Suite(.serialized) |
| 10 | +struct DynamicFrameworkTests { |
| 11 | + private let fileManager: FileManager = .default |
| 12 | + |
| 13 | + @Test( |
| 14 | + arguments: [FrameworkType.dynamic, .mergeable] |
| 15 | + ) |
| 16 | + func otherLDFlags(frameworkType: FrameworkType) async throws { |
| 17 | + let packageName = "DynamicFrameworkOtherLDFlagsTestPackage" |
| 18 | + |
| 19 | + let outputDir = fileManager.temporaryDirectory |
| 20 | + .appending(components: "Scipio", packageName) |
| 21 | + |
| 22 | + defer { |
| 23 | + _ = try? FileManager.default.removeItem(atPath: outputDir.path) |
| 24 | + } |
| 25 | + |
| 26 | + try await buildPackage( |
| 27 | + packageName: packageName, |
| 28 | + buildOptionsMatrix: [ |
| 29 | + "UsableFromInlinePackage": .init(frameworkType: frameworkType), |
| 30 | + ], |
| 31 | + outputDir: outputDir |
| 32 | + ) |
| 33 | + |
| 34 | + try await checkPlatformDependentLibraries( |
| 35 | + framework: .init( |
| 36 | + name: "UsableFromInlinePackage", |
| 37 | + destinations: [.iOS, .macOS] |
| 38 | + ), |
| 39 | + dependencies: [ |
| 40 | + "ClangModule": [.iOS, .macOS], |
| 41 | + "ClangModuleForIOS": [.iOS], |
| 42 | + "ClangModuleForMacOS": [.macOS], |
| 43 | + ], |
| 44 | + outputDir: outputDir |
| 45 | + ) |
| 46 | + } |
| 47 | + |
| 48 | + private func buildPackage( |
| 49 | + packageName: String, |
| 50 | + buildOptionsMatrix: [String: Runner.Options.TargetBuildOptions], |
| 51 | + outputDir: URL |
| 52 | + ) async throws { |
| 53 | + let runner = Runner( |
| 54 | + mode: .prepareDependencies, |
| 55 | + options: .init( |
| 56 | + baseBuildOptions: .init( |
| 57 | + buildConfiguration: .release, |
| 58 | + isSimulatorSupported: false, |
| 59 | + isDebugSymbolsEmbedded: false, |
| 60 | + frameworkType: .static, |
| 61 | + enableLibraryEvolution: false |
| 62 | + ), |
| 63 | + buildOptionsMatrix: buildOptionsMatrix, |
| 64 | + shouldOnlyUseVersionsFromResolvedFile: true, |
| 65 | + cachePolicies: .disabled, |
| 66 | + overwrite: true, |
| 67 | + verbose: false |
| 68 | + ) |
| 69 | + ) |
| 70 | + let packageDir = fixturePath.appending(component: packageName) |
| 71 | + |
| 72 | + try await runner.run( |
| 73 | + packageDirectory: packageDir, |
| 74 | + frameworkOutputDir: .custom(outputDir) |
| 75 | + ) |
| 76 | + } |
| 77 | + |
| 78 | + private func checkPlatformDependentLibraries( |
| 79 | + framework: Framework, |
| 80 | + dependencies: [String: Set<Destination>], |
| 81 | + outputDir: URL, |
| 82 | + sourceLocation: SourceLocation = #_sourceLocation |
| 83 | + ) async throws { |
| 84 | + let xcFrameworkPath = outputDir.appending(component: framework.xcFrameworkName) |
| 85 | + |
| 86 | + let executor = ProcessExecutor() |
| 87 | + |
| 88 | + for destination in Destination.allCases { |
| 89 | + let binaryPath = xcFrameworkPath.appending( |
| 90 | + components: destination.rawValue, "\(framework.name).framework", framework.name |
| 91 | + ) |
| 92 | + |
| 93 | + let executionResult = try await executor.execute("/usr/bin/otool", "-l", binaryPath.path(percentEncoded: false)) |
| 94 | + let loadCommands = try executionResult.unwrapOutput() |
| 95 | + |
| 96 | + for (dependencyName, destinations) in dependencies { |
| 97 | + let shouldContain = destinations.contains(destination) |
| 98 | + #expect( |
| 99 | + loadCommands.contains(dependencyName) == shouldContain, |
| 100 | + "\(dependencyName) \(shouldContain ? "must" : "must NOT") be linked to \(framework.name) for \(destination)." |
| 101 | + ) |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + private struct Framework { |
| 107 | + var name: String |
| 108 | + var destinations: Set<Destination> |
| 109 | + |
| 110 | + var xcFrameworkName: String { |
| 111 | + "\(name).xcframework" |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + private enum Destination: String, CaseIterable { |
| 116 | + case iOS = "ios-arm64" |
| 117 | + case macOS = "macos-arm64_x86_64" |
| 118 | + } |
| 119 | +} |
0 commit comments