File: Helpers.swift

package info (click to toggle)
swiftlang 6.2.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,856,264 kB
  • sloc: cpp: 9,995,718; ansic: 2,234,019; asm: 1,092,167; python: 313,940; objc: 82,726; f90: 80,126; lisp: 38,373; pascal: 25,580; sh: 20,378; ml: 5,058; perl: 4,751; makefile: 4,725; awk: 3,535; javascript: 3,018; xml: 918; fortran: 664; cs: 573; ruby: 396
file content (191 lines) | stat: -rw-r--r-- 6,729 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
 This source file is part of the Swift.org open source project

 Copyright (c) 2014 - 2025 Apple Inc. and the Swift project authors
 Licensed under Apache License v2.0 with Runtime Library Exception

 See http://swift.org/LICENSE.txt for license information
 See http://swift.org/CONTRIBUTORS.txt for Swift project authors
 */

import Foundation
import Testing
import _InternalTestSupport
import enum TSCUtility.Git
import Basics

package typealias ShReturnType = (stdout: String, stderr: String, returnCode: AsyncProcessResult.ExitStatus)

public let sdkRoot: AbsolutePath? = {
    if let environmentPath = ProcessInfo.processInfo.environment["SDK_ROOT"] {
        return try! AbsolutePath(validating: environmentPath)
    }

    #if os(macOS)
    let result = try! AsyncProcess.popen(arguments: ["xcrun", "--sdk", "macosx", "--show-sdk-path"])
    let sdkRoot = try! AbsolutePath(validating: result.utf8Output().spm_chomp())
    return sdkRoot
    #else
    return nil
    #endif
}()

public let toolchainPath: AbsolutePath = {
    if let environmentPath = ProcessInfo.processInfo.environment["TOOLCHAIN_PATH"] {
        return try! AbsolutePath(validating: environmentPath)
    }

    #if os(macOS)
    let swiftcPath = try! AbsolutePath(
        validating: sh("xcrun", "--find", "swift").stdout.spm_chomp()
    )
    #elseif os(Windows)
    let swiftcPath = try! AbsolutePath(validating: sh("where.exe", "swift.exe").stdout.spm_chomp())
    #else
    let swiftcPath = try! AbsolutePath(validating: sh("which", "swift").stdout.spm_chomp())
    #endif
    let toolchainPath = swiftcPath.parentDirectory.parentDirectory.parentDirectory
    return toolchainPath
}()

public let clang: AbsolutePath = {
    if let environmentPath = ProcessInfo.processInfo.environment["CLANG_PATH"] {
        return try! AbsolutePath(validating: environmentPath)
    }

    let clangPath = toolchainPath.appending(components: "usr", "bin", "clang")
    return clangPath
}()

public let xcodebuild: AbsolutePath = {
    #if os(macOS)
    let xcodebuildPath = try! AbsolutePath(
        validating: sh("xcrun", "--find", "xcodebuild").stdout.spm_chomp()
    )
    return xcodebuildPath
    #else
    fatalError("should not be used on other platforms than macOS")
    #endif
}()

@discardableResult
package func sh(
    _ arguments: CustomStringConvertible...,
    env: [String: String] = [:],
    file: StaticString = #file,
    line: UInt = #line
) throws -> ShReturnType {
    let result = try _sh(arguments, env: env, file: file, line: line)
    let stdout = try result.utf8Output()
    let stderr = try result.utf8stderrOutput()

    if result.exitStatus != .terminated(code: 0) {
        Issue
            .record(
                Comment(
                    "Command failed with exit code: \(result.exitStatus) - \(result.integrationTests_debugDescription)"
                )
            )
    }

    return (stdout, stderr, result.exitStatus)
}

@discardableResult
package func _sh(
    _ arguments: [CustomStringConvertible],
    env: [String: String] = [:],
    file: StaticString = #file,
    line: UInt = #line
) throws -> AsyncProcessResult {
    var environment = Environment()

    if let sdkRoot {
        environment["SDKROOT"] = sdkRoot.pathString
    }

    for (varName, value) in env {
        environment[EnvironmentKey(varName)] = value
    }

    let result = try AsyncProcess.popen(
        arguments: arguments.map(\.description), environment: environment
    )
    return result
}

public func binaryTargetsFixture(_ closure: (AbsolutePath) async throws -> Void) async throws {
    try await fixture(name: "BinaryTargets") { fixturePath in
        let inputsPath = fixturePath.appending(component: "Inputs")
        let packagePath = fixturePath.appending(component: "TestBinary")

        // Generating StaticLibrary.xcframework.
        try withTemporaryDirectory { tmpDir in
            let subpath = inputsPath.appending(component: "StaticLibrary")
            let sourcePath = subpath.appending(component: "StaticLibrary.m")
            let headersPath = subpath.appending(component: "include")
            let libraryPath = tmpDir.appending(component: "libStaticLibrary.a")
            try sh(
                clang, "-c", sourcePath, "-I", headersPath, "-fobjc-arc", "-fmodules", "-o",
                libraryPath
            )
            let xcframeworkPath = packagePath.appending(component: "StaticLibrary.xcframework")
            try sh(
                xcodebuild, "-create-xcframework", "-library", libraryPath, "-headers", headersPath,
                "-output", xcframeworkPath
            )
        }

        // Generating DynamicLibrary.xcframework.
        try withTemporaryDirectory { tmpDir in
            let subpath = inputsPath.appending(component: "DynamicLibrary")
            let sourcePath = subpath.appending(component: "DynamicLibrary.m")
            let headersPath = subpath.appending(component: "include")
            let libraryPath = tmpDir.appending(component: "libDynamicLibrary.dylib")
            try sh(
                clang, sourcePath, "-I", headersPath, "-fobjc-arc", "-fmodules", "-dynamiclib",
                "-o", libraryPath
            )
            let xcframeworkPath = packagePath.appending(component: "DynamicLibrary.xcframework")
            try sh(
                xcodebuild, "-create-xcframework", "-library", libraryPath, "-headers", headersPath,
                "-output", xcframeworkPath
            )
        }

        // Generating SwiftFramework.xcframework.
        try withTemporaryDirectory { tmpDir in
            let subpath = inputsPath.appending(component: "SwiftFramework")
            let projectPath = subpath.appending(component: "SwiftFramework.xcodeproj")
            try sh(
                xcodebuild, "-project", projectPath, "-scheme", "SwiftFramework",
                "-derivedDataPath", tmpDir, "COMPILER_INDEX_STORE_ENABLE=NO"
            )
            let frameworkPath = try AbsolutePath(
                validating: "Build/Products/Debug/SwiftFramework.framework",
                relativeTo: tmpDir
            )
            let xcframeworkPath = packagePath.appending(component: "SwiftFramework.xcframework")
            try sh(
                xcodebuild, "-create-xcframework", "-framework", frameworkPath, "-output",
                xcframeworkPath
            )
        }

        try await closure(packagePath)
    }
}

extension AsyncProcessResult {
    var integrationTests_debugDescription: String {
        """
        command: \(arguments.map(\.description).joined(separator: " "))

        stdout:
        \((try? utf8Output()) ?? "")

        stderr:
        \((try? utf8stderrOutput()) ?? "")
        """
    }
}