Skip to content

Commit 6c0cabb

Browse files
Fixes
- Fixed compiling - Added value storage for `AppExecutionMode.current` - Improved comments
1 parent 3b84ea5 commit 6c0cabb

File tree

3 files changed

+25
-12
lines changed

3 files changed

+25
-12
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,6 @@ Also having this as it's own library allows for code to be updated separately an
3737
# Credits:
3838

3939
ITzTravelInTime (Pietro Caruso) - Project creator
40+
Original source for the methods used to perform the detections: https://developer.apple.com/forums/thread/652667
41+
4042

Sources/SwiftCPUDetect/SwiftCPUDetect.swift

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,36 @@ public enum AppExecutionMode: Int32, Codable, Equatable, CaseIterable{
1616

1717
///Gets if the current process is running natively or emulated
1818
static func current() -> AppExecutionMode{
19-
var ret: Int32 = 0
20-
var size = ret.bitWidth / 8
21-
22-
let result = sysctlbyname("sysctl.proc_translated", &ret, &size, nil, 0)
19+
//stores the obtained value so useless re-detections are avoided since this value isn't supposed to change at execution time
20+
struct MEM{
21+
static var state: AppExecutionMode? = nil
22+
}
2323

24-
if result == -1 {
25-
if (errno == ENOENT){
26-
return AppExecutionMode.native
24+
if MEM.state == nil {
25+
var ret: Int32 = 0
26+
var size = ret.bitWidth / 8
27+
28+
let result = sysctlbyname("sysctl.proc_translated", &ret, &size, nil, 0)
29+
30+
if result == -1 {
31+
if (errno == ENOENT){
32+
MEM.state = AppExecutionMode.native
33+
}else{
34+
MEM.state = AppExecutionMode.unkown
35+
}
36+
}else{
37+
MEM.state = AppExecutionMode(rawValue: ret) ?? .unkown
2738
}
28-
return AppExecutionMode.unkown
39+
2940
}
3041

31-
return AppExecutionMode(rawValue: ret) ?? .unkown
42+
return MEM.state!
3243
}
3344
}
3445

3546
///This enum is used to make more conveniente the detection of the actual cpu architecture
3647
public enum CpuArchitecture: String, Codable, Equatable, CaseIterable{
37-
case ppc = "ppc" //belive it or not but there are swift compilers for ppc out there
48+
case ppc = "ppc" //belive it or not but there are swift compilers for ppc out there, so a bunch of PPC targets are included, if one is missing feel free to add it using a pull request.
3849
case ppcG1 = "ppc601"
3950
case ppcG2 = "ppc604"
4051
case ppcG3 = "ppc750"
@@ -60,7 +71,7 @@ public enum CpuArchitecture: String, Codable, Equatable, CaseIterable{
6071

6172
///Gets the current architecture used by the current process
6273
public static func current() -> CpuArchitecture?{
63-
//stores the obtained value so useless re-detections are avoided
74+
//stores the obtained value so useless re-detections are avoided since this value isn't supposed to change at execution time
6475
struct MEM{
6576
static var state: CpuArchitecture? = nil
6677
}

Tests/SwiftCPUDetectTests/SwiftCPUDetectTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import XCTest
2-
@testable import Swift_CPUDetect
2+
@testable import SwiftCPUDetect
33

44
final class Swift_CPUDetectTests: XCTestCase {
55
func testExample() {

0 commit comments

Comments
 (0)