Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support building for architectures other than x86_64 on macOS (Apple Silicon) #185

Merged
merged 5 commits into from
Oct 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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? {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if I am being too strict here? Could this be non-optional and either just force unwrap or return an empty string in the (unlikely) event of a failure?

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
}
Comment on lines +4 to +16
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if there is a better way to test this.. While we only need for machineHardwareName for macOS, it should also work for Linux as well.

I was going to use XCTSkipIf(true, "...") but that's Xcode 11.4+ whereas we need to support Xcode 10.2+.