Swiftpack.co - ITzTravelInTime/SwiftCPUDetect as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
ITzTravelInTime/SwiftCPUDetect
Swift Library to detect the current CPU Architecture for the current machine/process (inculing detecting if the current process is running using Rosetta) and additional system info.
.package(url: "https://github.com/ITzTravelInTime/SwiftCPUDetect.git", from: "1.3.0")

SwiftCPUDetect

Swift Library to detect the current CPU Architecture and if the current process is running under Rosetta.

Features

  • Get if the current process is ruiing using Rosetta (aka as emulated) using the AppExecutionMode enum
  • Get the Cpu architecture used by the current process, the computer or the ones supported by the current executable by using the CpuArchitecture enum
  • Get CPU and system info using the HWInfo class

Usage

Usage should be pretty simple, just take a look at the source code, here is also a very usefoul example usage:

Also check out the DEMO playground included inside the project.


import SwiftCPUDetect

//Disabled debug prints from the library
SwiftCPUDetect.GeneralPrinter.enabled = false

var str = " "

//those prints gets various info about the cpu
#if os(macOS)

print("Brand string for CPU is \"\(HWInfo.CPU.brandString() ?? "[Not detected]")\"")
print("Stepping for CPU is \"\(HWInfo.CPU.stepping() ?? 255)\"")
print("This cpu has \"\(HWInfo.CPU.coresPerPackage() ?? 255)\" cores for each package")

for info in HWInfo.CPU.featuresList() ?? []{
    str += " \(info),"
}

print("Features for CPU: \(str.dropLast())")

print("This system has \"\(HWInfo.CPU.packagesCount() ?? 255)\" cpu packages")

#endif

print("This cpu has \"\(HWInfo.CPU.coresCount() ?? 255)\" cores")
print("This cpu has \"\(HWInfo.CPU.threadsCount() ?? 255)\" threads")
print("This cpu is \"\(HWInfo.CPU.is64Bit() ? "64" : "32" )\" bits")

//Prints the ammount of RAM in bytes
print("This computer has \(HWInfo.ramAmmount() ?? 0) Bytes of RAM")

//Prints the current execution mode
print("Is my app running with Rosetta? \((AppExecutionMode.current() == .emulated) ? "Yes" : "No")")

//Prints the architecture of the current process
print("My app is running using the \(CpuArchitecture.current()?.rawValue ?? "[Can't detect architecture]") architecture")

//Prints the architecture of the current computer
print("My computer is running using the \(CpuArchitecture.machineCurrent()?.rawValue ?? "[Can't detect architecture]") architecture")

//Prints the architectures supported by the current executable
str = " "

for arch in CpuArchitecture.currentExecutableArchitectures(){
    str += " " + arch.rawValue + ","
}

print("My app supports those architectures: " + str.dropLast())


What apps/programs is this Library intended for?

This library should be used by Swift apps/programs, that needs to know system information like the current cpu architecture, if the current app/program is running using Rosetta or just needs some basic system info.

This code should work across multiple platforms based on the XNU kernel and that provvides the necessary function calls using the Foundation module, but it has been tested to work only on macOS/OS X and iOS.

About the project:

This code was created as part of my TINU project and it has been separated and made into it's own library to make the main project's source less complex and more focused on it's aim.

Also having this as it's own library allows for code to be updated separately and so various versions of the main TINU app will be able to be compiled all with the latest version of this library.

Credits:

Contacts

Copyright

Copyright (C) 2021 Pietro Caruso

This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

GitHub

link
Stars: 3
Last commit: 1 week ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Submit a free job ad (while I'm testing this). The analytics numbers for this website are here.

Release Notes

1.3.0
1 week ago
  • CpuArchitecture.actualCurrent() is now deprecated and has been replaced by CpuArchitecture.machineCurrent(), code compatibility has been maintained, but a warning message will be generated. It's strongly recommended to update your code to the new syntax since the next major updates will see the old syntax removed.
  • Improved some function descriptions.

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics