Skip to content
This repository has been archived by the owner on Jan 3, 2024. It is now read-only.

Improve ARM architecture detection: arm64 and armv7 #106

Merged
merged 1 commit into from
Mar 1, 2021
Merged
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
13 changes: 12 additions & 1 deletion lib/system.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,17 @@ export function toFancyArch (arch) {
return arch;
}

function getArmUnameArch () {
const uname = spawnSync('uname', ['-a']);
if (uname.error) return '';
let unameOut = uname.stdout && uname.stdout.toString();
unameOut = (unameOut || '').toLowerCase();
if (unameOut.includes('aarch64')) return 'arm64';
if (unameOut.includes('arm64')) return 'arm64';
if (unameOut.includes('armv7')) return 'armv7';
return '';
}

Copy link
Contributor Author

@pdcastro pdcastro Oct 14, 2020

Choose a reason for hiding this comment

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

It was only after writing and testing the code that I realised that there is already an open PR on the same topic: #101

I'd say that this PR (#106) is a bit more robust / conservative and complete, because:

  • It falls back to the existing /proc/cpuinfo implementation if uname is not available (however unlikely). Note that spawnSync('uname', ['-a']) does not throw an Error if uname does not exist, but sets uname.error which the code detects.
  • uname -a is a bit more robust than uname -m (thinking of all different Unix kernels out there: BSD, Solaris, Linux, even perhaps macOS...), as it does not rely on a particular field or implementation of uname.
  • The detection is case insensitive.
  • It detects arm64 (e.g. Raspberry Pi 4) in addition to armv7 (e.g. Raspberry Pi 2/3). I've tested on both!

Copy link
Contributor

Choose a reason for hiding this comment

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

Close this PR so :)

Copy link
Contributor Author

@pdcastro pdcastro Oct 15, 2020

Choose a reason for hiding this comment

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

Sorry, my writing was ambiguous (I've now edited it): I meant that this PR (#106) is better than the pre-existing one (#101). But I leave it for reviewers to decide whether they agree! :-)

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh ok sorry I misunderstood the comment

Copy link
Member

Choose a reason for hiding this comment

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

@pdcastro I really appreciate your help here and the descriptive comment!

function getArmHostArch () {
const cpu = fs.readFileSync('/proc/cpuinfo', 'utf8');
if (cpu.indexOf('vfpv3') >= 0) return 'armv7';
Expand All @@ -77,7 +88,7 @@ function getArmHostArch () {

function getHostArch () {
const { arch } = process;
if (arch === 'arm') return getArmHostArch();
if (arch === 'arm') return getArmUnameArch() || getArmHostArch();
return toFancyArch(arch);
}

Expand Down