Skip to content

Commit

Permalink
Merge pull request #2770 from rakudo/use-nqp-uname
Browse files Browse the repository at this point in the history
Refactor $*KERNEL internal use of uname
  • Loading branch information
lizmat committed Mar 17, 2019
2 parents fc4aca0 + ec40933 commit ffe8409
Showing 1 changed file with 56 additions and 49 deletions.
105 changes: 56 additions & 49 deletions src/core/Kernel.pm6
Expand Up @@ -8,11 +8,53 @@ class Kernel does Systemic {
has Str $!hardware;
has Str $!arch;
has Int $!bits;
has Bool $!has_uname;

method !uname($opt) {
$!has_uname //= ("/bin/uname", "/usr/bin/uname").first({ .IO.e && .IO.x }).so;
$!has_uname ?? qqx/uname $opt/.chomp !! 'unknown';
#?if moar
has $!uname;
method !uname {
$!uname ?? $!uname !! ($!uname := nqp::uname())
}
#?endif

method !uname-s {
#?if moar
nqp::atpos_s(self!uname, nqp::const::UNAME_SYSNAME)
#?endif
#?if !moar
try shell('uname -s', :out, :!err).out.slurp(:close).chomp;
#?endif
}

method !uname-r {
#?if moar
nqp::atpos_s(self!uname, nqp::const::UNAME_RELEASE)
#?endif
#?if !moar
try shell('uname -r', :out, :!err).out.slurp(:close).chomp;
#?endif
}

method !uname-v {
#?if moar
nqp::atpos_s(self!uname, nqp::const::UNAME_VERSION)
#?endif
#?if !moar
try shell('uname -v', :out, :!err).out.slurp(:close).chomp;
#?endif
}

method !uname-m {
#?if moar
nqp::atpos_s(self!uname, nqp::const::UNAME_MACHINE)
#?endif
#?if !moar
try shell('uname -m', :out, :!err).out.slurp(:close).chomp;
#?endif
}

method !uname-p {
# TODO: find a way to get this without shelling out
try shell("uname -p", :out, :!err).out.slurp(:close).chomp;
}

submethod BUILD(:$!auth = "unknown" --> Nil) { }
Expand All @@ -27,71 +69,36 @@ class Kernel does Systemic {
'browser';
}
default {
lc self!uname('-s');
lc self!uname-s();
}
}
}
}

method version {
$!version //= Version.new( do {
given $*DISTRO.name {
when 'freebsd' {
self!uname('-r'); # -K -U not introduced until 10.0
}
when 'macosx' {
my $unamev = self!uname('-v');
$unamev ~~ m/^Darwin \s+ Kernel \s+ Version \s+ (<[\d\.]>+)/
?? ~$0
!! $unamev.chomp;
}
default {
given $.name {
when 'linux' {
# somewhat counter-intuitively the '-r' is what
# most people think of the kernel version
self!uname('-r');
}
default {
self!uname('-v');
}
}
}
}
} );
# it doesn't make sense to return a Version object here, but its currently enforced by roast
# TODO: remove Version checks from roast? and check ecosystem for fallout.
$!version //= Version.new(self!uname-v());
}

method release {
$!release //= do {
given $*DISTRO.name {
when any <openbsd netbsd dragonfly> { # needs adapting
self!uname('-r');
}
default {
self!uname('-v');
}
}
}
# somewhat counter-intuitively the UNAME_RELEASE is what
# most people think of the kernel version
$!release //= self!uname-r();
}

method hardware {
$!hardware //= do {
given $*DISTRO.name {
default {
self!uname('-m');
}
}
}
$!hardware //= self!uname-m();
}

method arch {
$!arch //= do {
given $*DISTRO.name {
when 'raspbian' {
self!uname('-m');
self!uname-m();
}
default {
self!uname('-p');
self!uname-p();
}
}
}
Expand Down

0 comments on commit ffe8409

Please sign in to comment.