diff --git a/CHANGELOG.md b/CHANGELOG.md index 5c9a750..29be81c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,9 @@ #### Added - Running offline without specified version [#164](https://github.com/yonaskolb/Mint/issues/164) [#166](https://github.com/yonaskolb/Mint/pull/166) @vknabel +#### Fixed +- Support building for architectures other than x86_64 on macOS (Apple Silicon) [#185](https://github.com/yonaskolb/Mint/pull/185) + ## 0.14.2 #### Changed diff --git a/Sources/MintKit/Mint.swift b/Sources/MintKit/Mint.swift index 6711929..1e37c5e 100644 --- a/Sources/MintKit/Mint.swift +++ b/Sources/MintKit/Mint.swift @@ -329,9 +329,12 @@ public class Mint { var buildCommand = "swift build -c release" #if os(macOS) - let osVersion = ProcessInfo.processInfo.operatingSystemVersion - let target = "x86_64-apple-macosx\(osVersion.majorVersion).\(osVersion.minorVersion)" - buildCommand += " -Xswiftc -target -Xswiftc \(target)" + let processInfo = ProcessInfo.processInfo + if let machineHardwareName = processInfo.machineHardwareName { + let osVersion = ProcessInfo.processInfo.operatingSystemVersion + let target = "\(machineHardwareName)-apple-macosx\(osVersion.majorVersion).\(osVersion.minorVersion)" + buildCommand += " -Xswiftc -target -Xswiftc \(target)" + } #endif try runPackageCommand(name: "Building package", diff --git a/Sources/MintKit/ProcessInfoExtensions.swift b/Sources/MintKit/ProcessInfoExtensions.swift new file mode 100644 index 0000000..d762fd1 --- /dev/null +++ b/Sources/MintKit/ProcessInfoExtensions.swift @@ -0,0 +1,17 @@ +import Foundation + +extension ProcessInfo { + /// Returns a `String` representing the machine hardware name or nil if there was an error invoking `uname(_:)` or decoding the response. + /// + /// Return value is the equivalent to running `$ uname -m` in shell. + var machineHardwareName: String? { + var sysinfo = utsname() + let result = uname(&sysinfo) + guard result == EXIT_SUCCESS else { return nil } + + let data = Data(bytes: &sysinfo.machine, count: Int(_SYS_NAMELEN)) + + guard let identifier = String(bytes: data, encoding: .ascii) else { return nil } + return identifier.trimmingCharacters(in: .controlCharacters) + } +} diff --git a/Tests/MintTests/ProcessInfoExtensionTests.swift b/Tests/MintTests/ProcessInfoExtensionTests.swift new file mode 100644 index 0000000..5b4c56a --- /dev/null +++ b/Tests/MintTests/ProcessInfoExtensionTests.swift @@ -0,0 +1,16 @@ +@testable import MintKit +import XCTest + +final class ProcessInfoExtensionTests: XCTestCase { + #if arch(x86_64) + func testMachineHardwareName_Intel() { + XCTAssertEqual(ProcessInfo.processInfo.machineHardwareName, "x86_64") + } + #endif + + #if arch(arm64) + func testMachineHardwareName_AppleSilicone() { + XCTAssertEqual(ProcessInfo.processInfo.machineHardwareName, "arm64") + } + #endif +}