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

os: add kernel_version #8381

Merged
merged 1 commit into from Aug 19, 2020

Conversation

SeekingMeaning
Copy link
Contributor

@SeekingMeaning SeekingMeaning commented Aug 17, 2020

  • Have you followed the guidelines in our Contributing document?
  • Have you checked to ensure there aren't other open Pull Requests for the same change?
  • Have you added an explanation of what your changes do and why you'd like us to include them?
  • Have you written new tests for your changes? Here's an example.
  • Have you successfully run brew style with your changes locally?
  • Have you successfully run brew tests with your changes locally?

This allows for e.g. `uname -r`.split(".").first in homebrew/core/gcc to be replaced with OS.kernel_version.major

Uses:

  • `uname -r`.split(".").firstOS.kernel_version.major
  • `uname -r`.to_iOS.kernel_version.major
  • `uname -r`.chompOS.kernel_version
  • shell_output("uname -r").stripOS.kernel_version
  • Utils.safe_popen_read("uname", "-r").chompOS.kernel_version

@@ -13,6 +13,10 @@ def self.linux?
RbConfig::CONFIG["host_os"].include? "linux"
end

def self.uname_version
Copy link
Member

Choose a reason for hiding this comment

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

Maybe call this kernel_version?

@MikeMcQuaid
Copy link
Member

This allows for e.g. `uname -r`.split(".").first in homebrew/core/gcc to be replaced with OS.uname_version.major

Any new utility methods that are used by formulae should be used by at least 3 (ideally a lot more than that) before we add them. I'm not sure I see the advantage here; it's not even actually shorter in this case?

@SeekingMeaning
Copy link
Contributor Author

Any new utility methods that are used by formulae should be used by at least 3 (ideally a lot more than that) before we add them.

About 17 formulae use uname -r (including versioned gcc's)
See https://github.com/Homebrew/homebrew-core/search?q=%22uname+-r%22

it's not even actually shorter in this case?

Hehe, you might have a point 😆 It's longer but I think it's more clear

@Bo98
Copy link
Member

Bo98 commented Aug 17, 2020

I'm not convinced the --build flag is needed in the case of GCC. That's usually indicative of a bad config.guess.

@SeekingMeaning SeekingMeaning changed the title os: add uname_version os: add kernel_version Aug 17, 2020
@SeekingMeaning
Copy link
Contributor Author

Slightly off-topic, latest GNU config.guess supports arm64 Darwin now: https://git.savannah.gnu.org/gitweb/?p=config.git;a=commitdiff;h=2593751ef276497e312d7c4ce7fd049614c7bf80

@MikeMcQuaid
Copy link
Member

Uses:

  • `uname -r`.split(".").firstOS.kernel_version.major
  • `uname -r`.to_iOS.kernel_version.major
  • `uname -r`.chompOS.kernel_version
  • shell_output("uname -r").stripOS.kernel_version
  • Utils.safe_popen_read("uname", "-r").chompOS.kernel_version

You've changed my mind: I like these.

@@ -13,6 +13,10 @@ def self.linux?
RbConfig::CONFIG["host_os"].include? "linux"
end

def self.kernel_version
@kernel_version ||= Version.new(`uname -r`.strip)
Copy link
Member

Choose a reason for hiding this comment

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

  • What if uname -r fails? (should probably raise)
  • Utils.popen_read might be a nicer fit here.

Copy link
Member

Choose a reason for hiding this comment

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

Also: could this get a unit test.

@SeekingMeaning
Copy link
Contributor Author

SeekingMeaning commented Aug 18, 2020

  • What if uname -r fails? (should probably raise)

I'm not certain, but after checking in brew irb it seems that uname would print an error message (if it has a message to print) and then `uname -r` would return an empty string, so pretty much the same as Utils.popen_read but without silencing error messages

@kernel_version ||= if output = Utils.popen_read("uname", "-r").chomp.presence
Version.new output
else
Version::NULL
Copy link
Member

Choose a reason for hiding this comment

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

Doesn't Version.create return Version::NULL?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That would be Version.parse but that might be excessive in this case since uname -r already returns a valid version string

@@ -13,6 +13,14 @@ def self.linux?
RbConfig::CONFIG["host_os"].include? "linux"
end

def self.kernel_version
@kernel_version ||= if output = Utils.popen_read("uname", "-r").chomp.presence
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
@kernel_version ||= if output = Utils.popen_read("uname", "-r").chomp.presence
@kernel_version ||= if output = Utils.safe_popen_read("uname", "-r").chomp.presence

I don't think this should be able to fail, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Whoops, I forgot about that. I was just preserving the functionality of OS::Linux::Kernel.version since that's where this came from

We probably won't need the if since safe_popen_read raises an error anyways

return @version if @version

version = Utils.popen_read("uname", "-r").chomp
return Version::NULL unless version

@version = Version.new version
OS.kernel_version
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'm not sure if we want to preserve this or not

Copy link
Member

Choose a reason for hiding this comment

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

I think so unless you can verify it's unused in e.g. linuxbrew-core in which case: 🔥 it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Looks like this is only used in brew in extend/os/linux/diagnostic (including below_minimum_version?):

% cd $(brew --repo)
% grep -r 'Linux::Kernel' *
Library/Homebrew/test/os/linux/diagnostic_spec.rb:    allow(OS::Linux::Kernel).to receive(:below_minimum_version?).and_return(true)
Library/Homebrew/extend/os/linux/diagnostic.rb:        return unless OS::Linux::Kernel.below_minimum_version?
Library/Homebrew/extend/os/linux/diagnostic.rb:          Your Linux kernel #{OS::Linux::Kernel.version} is too old.
Library/Homebrew/extend/os/linux/diagnostic.rb:          We only support kernel #{OS::Linux::Kernel.minimum_version} or later.
% cd $(brew --repo homebrew/core)
% git remote get-url origin
https://github.com/Homebrew/linuxbrew-core
% grep 'Linux::Kernel' Formula/*
% grep 'Linux::' Formula/*
Formula/clang-format.rb:    if Formula["glibc"].any_version_installed? || OS::Linux::Glibc.system_version < Formula["glibc"].version
Formula/glibc.rb:    Glibc.version >= OS::Linux::Glibc.system_version
Formula/glibc.rb:      Your system's glibc version is #{OS::Linux::Glibc.system_version}, and Homebrew's glibc version is #{Glibc.version}.
Formula/llvm@7.rb:    if Formula["glibc"].any_version_installed? || OS::Linux::Glibc.system_version < Formula["glibc"].version
Formula/llvm@8.rb:    if Formula["glibc"].any_version_installed? || OS::Linux::Glibc.system_version < Formula["glibc"].version
Formula/llvm@9.rb:    if Formula["glibc"].any_version_installed? || OS::Linux::Glibc.system_version < Formula["glibc"].version
Formula/llvm.rb:    if Formula["glibc"].any_version_installed? || OS::Linux::Glibc.system_version < Formula["glibc"].version

Copy link
Member

Choose a reason for hiding this comment

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

Cool! I think you can 🔥 then.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So uh, what does 🔥 mean in this context? 😅

Copy link
Member

Choose a reason for hiding this comment

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

Sorry: burn it down (i.e. delete it)

@@ -13,6 +13,10 @@ def self.linux?
RbConfig::CONFIG["host_os"].include? "linux"
end

def self.kernel_version
@kernel_version ||= Version.new(Utils.safe_popen_read("uname", "-r").chomp)
Copy link
Member

Choose a reason for hiding this comment

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

Perfect!

@SeekingMeaning SeekingMeaning merged commit f5b8cf4 into Homebrew:master Aug 19, 2020
@SeekingMeaning SeekingMeaning deleted the os/uname_version branch August 19, 2020 19:40
@BrewTestBot BrewTestBot added the outdated PR was locked due to age label Dec 16, 2020
@Homebrew Homebrew locked as resolved and limited conversation to collaborators Dec 16, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated PR was locked due to age
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants