Skip to content

Commit

Permalink
Support building for architectures other than x86_64 on macOS (Apple …
Browse files Browse the repository at this point in the history
…Silicon) (#185)

* Add and test new 'ProfileInfo.machineHardwareName' property

* Update 'Mint.install(package:executable:beforeOtherCommand:force:link:noInstall:)' method to construct 'swift build' command that specifies the correct target architecture in order to support building on Apple Silicon

* Remove use of XCTSkipIf to preserve support for Xcode 10.2

* Update CHANGELOG.md

* Clean up ProcessInfoExtensionTests by removing print for skipped tests
  • Loading branch information
liamnichols authored Oct 23, 2020
1 parent 7b9920f commit 06f3914
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 6 additions & 3 deletions Sources/MintKit/Mint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
17 changes: 17 additions & 0 deletions Sources/MintKit/ProcessInfoExtensions.swift
Original file line number Diff line number Diff line change
@@ -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)
}
}
16 changes: 16 additions & 0 deletions Tests/MintTests/ProcessInfoExtensionTests.swift
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 06f3914

Please sign in to comment.