Module: Datadog::RubyCoreSource

Defined in:
lib/datadog/ruby_core_source.rb,
lib/datadog/ruby_core_source/version.rb

Constant Summary collapse

REVISION_MAP =
{
    # Add pre-release version here since they do not have patchlevel to refer to.
    # Revision can be found at `revision.h` of ruby sources.
    # Format of this hash:
    # <RUBY_REVISION> => '<sources directory name>', e.g. `61243 => 'ruby-2.5.0-rc1'`
}
VERSION =
'3.4.1'

Class Method Summary collapse

Class Method Details

.create_makefile_with_core(hdrs, name) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/datadog/ruby_core_source.rb', line 13

def self.create_makefile_with_core(hdrs, name)
  # First, see if the gem already has the needed headers
  if hdrs.call
    create_makefile(name)
    return true
  end

  ruby_dir = if RUBY_PATCHLEVEL < 0
    extract_beta_ruby_version
  else
    "ruby-#{RUBY_VERSION}-p#{RUBY_PATCHLEVEL}"
  end

  # Check if core headers were already downloaded; if so, use them
  if RbConfig::CONFIG["rubyhdrdir"]
    dest_dir = RbConfig::CONFIG["rubyhdrdir"] + "/" + ruby_dir
    with_cppflags("-I" + dest_dir) {
      if hdrs.call
        create_makefile(name)
        return true
      end
    }
  end

  # Look for sources that ship with gem
  dest_dir = deduce_packaged_source_dir(ruby_dir)
  $stderr.puts "Using datadog-ruby_core_source headers from #{dest_dir}"
  no_source_abort(ruby_dir) unless File.directory?(dest_dir)

  with_cppflags("-I" + dest_dir) {
    if hdrs.call
      create_makefile(name)
      return true
    end
  }
  return false
end

.deduce_packaged_source_dir(ruby_dir) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/datadog/ruby_core_source.rb', line 51

def self.deduce_packaged_source_dir(ruby_dir)
  prefix = File.dirname(__FILE__) + '/ruby_core_source/'
  expected_directory = prefix + ruby_dir
  if (RUBY_REVISION.is_a? String or RUBY_REVISION > 0) and File.directory?(expected_directory)
    expected_directory
  else
    # Fallback to an older version.
    ruby_version = Gem::Version.new(RUBY_VERSION)
    path, = Dir.glob(prefix + 'ruby-*').
      select { |d| File.directory?(d) }.
      map { |d| [d, ruby_source_dir_version(d)] }.
      sort { |(_, v1), (_, v2)| -(v1 <=> v2) }.
      find { |(_, v)| v <= ruby_version }

    version = File.basename(path)
    fallback_source_warning(ruby_dir, version)
    path
  end
end

.extract_beta_ruby_versionObject



94
95
96
97
98
99
100
101
102
103
104
# File 'lib/datadog/ruby_core_source.rb', line 94

def self.extract_beta_ruby_version
  # Example Ruby description:
  # ruby 3.4.0preview2 (2024-10-07 master 32c733f57b) +PRISM [x86_64-linux]
  beta_suffix = RUBY_DESCRIPTION.split[1].sub(RUBY_VERSION, "")

  if beta_suffix.start_with?("preview", "rc")
    "ruby-#{RUBY_VERSION}-#{beta_suffix}"
  else # Don't touch it
    "ruby-#{RUBY_VERSION}"
  end
end

.fallback_source_warning(ruby_version, fallback_version) ⇒ Object



76
77
78
79
80
81
82
83
# File 'lib/datadog/ruby_core_source.rb', line 76

def self.fallback_source_warning(ruby_version, fallback_version)
  warn <<-STR
**************************************************************************
No source for #{ruby_version} (revision #{RUBY_REVISION}) provided with
datadog-ruby_core_source gem. Falling back to #{fallback_version}.
**************************************************************************
STR
end

.no_source_abort(ruby_version) ⇒ Object



85
86
87
88
89
90
91
92
# File 'lib/datadog/ruby_core_source.rb', line 85

def self.no_source_abort(ruby_version)
  abort <<-STR
Makefile creation failed
**************************************************************************
No source for #{ruby_version} provided with datadog-ruby_core_source gem.
**************************************************************************
STR
end

.ruby_source_dir_version(dir) ⇒ Object



71
72
73
74
# File 'lib/datadog/ruby_core_source.rb', line 71

def self.ruby_source_dir_version(dir)
  match = /ruby-([0-9\.]+)-((p|rc|preview)[0-9]+)\z/.match(dir)
  Gem::Version.new("#{match[1]}.#{match[2]}")
end