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

[PROF-9257] Fix incorrect platform detection for x86_64-linux-gnu/aarch64-linux-gnu #338

Merged
merged 3 commits into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
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
52 changes: 34 additions & 18 deletions ruby/lib/libdatadog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,7 @@ def self.available_binaries
end

def self.pkgconfig_folder(pkgconfig_file_name = "datadog_profiling_with_rpath.pc")
current_platform = Gem::Platform.local.to_s

if RbConfig::CONFIG["arch"].include?("-musl") && !current_platform.include?("-musl")
# Fix/workaround for https://github.com/DataDog/dd-trace-rb/issues/2222
#
# Old versions of rubygems (for instance 3.0.3) don't properly detect alternative libc implementations on Linux;
# in particular for our case, they don't detect musl. (For reference, Rubies older than 2.7 may have shipped with
# an affected version of rubygems).
# In such cases, we fall back to use RbConfig::CONFIG['arch'] instead.
#
# Why not use RbConfig::CONFIG['arch'] always? Because Gem::Platform.local.to_s does some normalization we want
# in other situations -- for instance, it turns `x86_64-linux-gnu` to `x86_64-linux`. So for now we only add this
# workaround in a specific situation where we actually know it is wrong.
#
# See also https://github.com/rubygems/rubygems/pull/2922 and https://github.com/rubygems/rubygems/pull/4082

current_platform = RbConfig::CONFIG["arch"]
end
current_platform = self.current_platform

return unless available_binaries.include?(current_platform)

Expand All @@ -40,4 +23,37 @@ def self.pkgconfig_folder(pkgconfig_file_name = "datadog_profiling_with_rpath.pc
private_class_method def self.vendor_directory
ENV["LIBDATADOG_VENDOR_OVERRIDE"] || "#{__dir__}/../vendor/libdatadog-#{Libdatadog::LIB_VERSION}/"
end

def self.current_platform
platform = Gem::Platform.local.to_s

if platform.end_with?("-gnu")
# In some cases on Linux with glibc the platform includes a -gnu suffix. We normalize it to not have the suffix.
#
# Note: This should be platform = platform.delete_suffix("-gnu") but it doesn't work on legacy Rubies; once
# dd-trace-rb 2.0 is out we can simplify this.
#
platform = platform[0..-5]
end

if RbConfig::CONFIG["arch"].include?("-musl") && !platform.include?("-musl")
# Fix/workaround for https://github.com/datadog/dd-trace-rb/issues/2222
#
# Old versions of rubygems (for instance 3.0.3) don't properly detect alternative libc implementations on Linux;
# in particular for our case, they don't detect musl. (For reference, Rubies older than 2.7 may have shipped with
# an affected version of rubygems).
# In such cases, we fall back to use RbConfig::CONFIG['arch'] instead.
#
# Why not use RbConfig::CONFIG['arch'] always? Because Gem::Platform.local.to_s does some normalization that seemed
# useful in the past, although part of it got removed in https://github.com/rubygems/rubygems/pull/5852.
# For now we only add this workaround in a specific situation where we actually know it is wrong, but in the
# future it may be worth re-evaluating if we should move away from `Gem::Platform` altogether.
#
# See also https://github.com/rubygems/rubygems/pull/2922 and https://github.com/rubygems/rubygems/pull/4082

RbConfig::CONFIG["arch"]
else
platform
end
end
end
2 changes: 1 addition & 1 deletion ruby/lib/libdatadog/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Libdatadog
# Current libdatadog version
LIB_VERSION = "6.0.0"

GEM_MAJOR_VERSION = "1"
GEM_MAJOR_VERSION = "2"
GEM_MINOR_VERSION = "0"
GEM_PRERELEASE_VERSION = "" # remember to include dot prefix, if needed!
private_constant :GEM_MAJOR_VERSION, :GEM_MINOR_VERSION, :GEM_PRERELEASE_VERSION
Expand Down
13 changes: 13 additions & 0 deletions ruby/spec/libdatadog_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ def create_dummy_pkgconfig_file(pkgconfig_folder)
# Fix for https://github.com/DataDog/dd-trace-rb/issues/2222

before do
allow(RbConfig::CONFIG).to receive(:[]).and_call_original
allow(RbConfig::CONFIG).to receive(:[]).with("arch").and_return("x86_64-linux-musl")
allow(Gem::Platform).to receive(:local).and_return("x86_64-linux")

Expand All @@ -101,6 +102,18 @@ def create_dummy_pkgconfig_file(pkgconfig_folder)
expect(Libdatadog.pkgconfig_folder).to eq "#{temporary_directory}/x86_64-linux-musl/some/folder/containing/the/pkgconfig/file"
end
end

context "when platform ends with -gnu" do
let(:pkgconfig_folder) { "#{temporary_directory}/aarch64-linux/some/folder/containing/the/pkgconfig/file" }

before do
allow(Gem::Platform).to receive(:local).and_return(Gem::Platform.new("aarch64-linux-gnu"))
end

it "chops off the -gnu suffix and returns the folder containing the pkgconfig file for the non-gnu variant" do
expect(Libdatadog.pkgconfig_folder).to eq pkgconfig_folder
end
end
end

context "but not for the current platform" do
Expand Down
Loading