File: cargo_builder.rb

package info (click to toggle)
ruby-rb-sys 0.9.87-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 192 kB
  • sloc: ruby: 1,186; makefile: 4
file content (350 lines) | stat: -rw-r--r-- 10,178 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
require "rubygems/ext"
require "rubygems/ext/builder"
require_relative "cargo_builder/link_flag_converter"

module RbSys
  # A class to build a Ruby gem Cargo. Extracted from `rubygems` gem, with some modifications.
  # @api private
  class CargoBuilder < Gem::Ext::Builder
    WELL_KNOWN_WRAPPERS = %w[sccache cachepot].freeze

    attr_accessor :spec, :runner, :env, :features, :target, :extra_rustc_args, :dry_run, :ext_dir, :extra_rustflags,
      :extra_cargo_args
    attr_writer :profile

    def initialize(spec)
      require "rubygems/command"
      require_relative "cargo_builder/link_flag_converter"

      @spec = spec
      @runner = self.class.method(:run)
      @profile = ENV.fetch("RB_SYS_CARGO_PROFILE", :release).to_sym
      @env = {}
      @features = []
      @target = ENV["CARGO_BUILD_TARGET"] || ENV["RUST_TARGET"]
      @extra_rustc_args = []
      @extra_cargo_args = []
      @dry_run = true
      @ext_dir = ""
      @extra_rustflags = []
    end

    def profile
      return :release if rubygems_invoked?

      @profile
    end

    def build(_extension, dest_path, results, args = [], lib_dir = nil, cargo_dir = Dir.pwd)
      require "fileutils"
      require "shellwords"

      build_crate(dest_path, results, args, cargo_dir)
      validate_cargo_build!(dest_path)
      rename_cdylib_for_ruby_compatibility(dest_path)
      finalize_directory(dest_path, lib_dir, cargo_dir)
      results
    end

    def build_crate(dest_path, results, args, cargo_dir)
      env = build_env
      cmd = cargo_command(dest_path, args)
      runner.call cmd, results, "cargo", cargo_dir, env

      results
    end

    def build_env
      build_env = rb_config_env
      build_env["RUBY_STATIC"] = "true" if ruby_static? && ENV.key?("RUBY_STATIC")
      build_env.merge(env)
    end

    def manifest_dir
      ext_dir
    end

    def cargo_command(dest_path, args = [])
      cmd = []
      cmd += ["cargo", "rustc"]
      cmd += ["--target", target] if target
      cmd += ["--target-dir", dest_path]
      cmd += ["--features", features.join(",")] unless features.empty?
      cmd += ["--lib"]
      cmd += ["--profile", profile.to_s]
      cmd += Gem::Command.build_args
      cmd += args
      cmd += ["--"]
      cmd += [*rustc_args(dest_path)]
      cmd += extra_rustc_args
      cmd
    end

    def cargo_dylib_path(dest_path)
      prefix = so_ext == "dll" ? "" : "lib"
      path_parts = [dest_path]
      path_parts << target if target
      path_parts += [profile_target_directory, "#{prefix}#{cargo_crate_name}.#{so_ext}"]
      File.join(*path_parts)
    end

    # We have to basically reimplement RbConfig::CONFIG['SOEXT'] here to support
    # Ruby < 2.5
    #
    # @see https://github.com/ruby/ruby/blob/c87c027f18c005460746a74c07cd80ee355b16e4/configure.ac#L3185
    def so_ext
      return RbConfig::CONFIG["SOEXT"] if RbConfig::CONFIG.key?("SOEXT")

      if win_target?
        "dll"
      elsif darwin_target?
        "dylib"
      else
        "so"
      end
    end

    private

    def rb_config_env
      result = {}
      RbConfig::CONFIG.each { |k, v| result["RBCONFIG_#{k}"] = v }
      result
    end

    def rustc_args(dest_dir)
      [
        *linker_args,
        *mkmf_libpath,
        *rustc_dynamic_linker_flags(dest_dir),
        *rustc_lib_flags(dest_dir),
        *platform_specific_rustc_args(dest_dir)
      ]
    end

    def platform_specific_rustc_args(dest_dir, flags = [])
      if mingw_target?
        # On mingw platforms, mkmf adds libruby to the linker flags
        flags += libruby_args(dest_dir)

        # Make sure ALSR is used on mingw
        # see https://github.com/rust-lang/rust/pull/75406/files
        flags += ["-C", "link-arg=-Wl,--dynamicbase"]
        flags += ["-C", "link-arg=-Wl,--disable-auto-image-base"]

        # If the gem is installed on a host with build tools installed, but is
        # run on one that isn't the missing libraries will cause the extension
        # to fail on start.
        flags += ["-C", "link-arg=-static-libgcc"]
      elsif darwin_target?
        # See https://github.com/oxidize-rb/rb-sys/issues/88
        dl_flag = "-Wl,-undefined,dynamic_lookup"
        flags += ["-C", "link-arg=#{dl_flag}"] unless makefile_config("DLDFLAGS")&.include?(dl_flag)
      end

      flags
    end

    # We want to use the same linker that Ruby uses, so that the linker flags from
    # mkmf work properly.
    def linker_args
      cc_flag = Shellwords.split(makefile_config("CC"))
      linker = cc_flag.shift

      if WELL_KNOWN_WRAPPERS.any? { |w| linker.include?(w) }
        linker = cc_flag.shift
      end

      link_args = cc_flag.flat_map { |a| ["-C", "link-arg=#{a}"] }

      return mswin_link_args if linker == "cl"

      ["-C", "linker=#{linker}", *link_args]
    end

    def mswin_link_args
      args = []
      args += ["-l", makefile_config("LIBRUBYARG_SHARED").chomp(".lib")]
      args += split_flags("LIBS").flat_map { |lib| ["-l", lib.chomp(".lib")] }
      args += split_flags("LOCAL_LIBS").flat_map { |lib| ["-l", lib.chomp(".lib")] }
      args
    end

    def libruby_args(dest_dir)
      libs = makefile_config(ruby_static? ? "LIBRUBYARG_STATIC" : "LIBRUBYARG_SHARED")
      raw_libs = Shellwords.split(libs)
      raw_libs.flat_map { |l| ldflag_to_link_modifier(l) }
    end

    def ruby_static?
      return true if %w[1 true].include?(ENV["RUBY_STATIC"])

      makefile_config("ENABLE_SHARED") == "no"
    end

    # Ruby expects the dylib to follow a file name convention for loading
    def rename_cdylib_for_ruby_compatibility(dest_path)
      new_path = final_extension_path(dest_path)
      FileUtils.cp(cargo_dylib_path(dest_path), new_path)
      new_path
    end

    def validate_cargo_build!(dir)
      dylib_path = cargo_dylib_path(dir)

      raise DylibNotFoundError, dir unless File.exist?(dylib_path)

      dylib_path
    end

    def final_extension_path(dest_path)
      dylib_path = cargo_dylib_path(dest_path)
      dlext_name = "#{spec.name}.#{makefile_config("DLEXT")}"
      dylib_path.gsub(File.basename(dylib_path), dlext_name)
    end

    def cargo_crate_name
      spec.metadata.fetch("cargo_crate_name", spec.name).tr("-", "_")
    end

    def rustc_dynamic_linker_flags(dest_dir)
      split_flags("DLDFLAGS")
        .map { |arg| maybe_resolve_ldflag_variable(arg, dest_dir) }
        .compact
        .flat_map { |arg| ldflag_to_link_modifier(arg) }
    end

    def rustc_lib_flags(dest_dir)
      split_flags("LIBS").flat_map { |arg| ldflag_to_link_modifier(arg) }
    end

    def split_flags(var)
      Shellwords.split(RbConfig::CONFIG.fetch(var, ""))
    end

    def ldflag_to_link_modifier(arg)
      LinkFlagConverter.convert(arg)
    end

    def msvc_target?
      makefile_config("target_os").include?("msvc")
    end

    def darwin_target?
      makefile_config("target_os").include?("darwin")
    end

    def mingw_target?
      makefile_config("target_os").include?("mingw")
    end

    def win_target?
      target_platform = RbConfig::CONFIG["target_os"]
      !!Gem::WIN_PATTERNS.find { |r| target_platform =~ r }
    end

    # Interpolate substition vars in the arg
    def maybe_resolve_ldflag_variable(input_arg, dest_dir)
      var_matches = input_arg.match(/\$\((\w+)\)/)

      return input_arg unless var_matches

      var_name = var_matches[1]

      return input_arg if var_name.nil? || var_name.chomp.empty?

      case var_name
      when "DEFFILE"
        # DEFFILE already generated by cargo
      else
        RbConfig::CONFIG[var_name]
      end
    end

    # Corresponds to $(LIBPATH) in mkmf
    def mkmf_libpath
      ["-L", "native=#{makefile_config("libdir")}"]
    end

    def makefile_config(var_name)
      val = RbConfig::MAKEFILE_CONFIG[var_name]

      return unless val

      RbConfig.expand(val.dup)
    end

    # Copied from ExtConfBuilder
    def finalize_directory(dest_path, lib_dir, extension_dir)
      require "fileutils"
      require "tempfile"

      ext_path = final_extension_path(dest_path)

      begin
        tmp_dest = Dir.mktmpdir(".gem.", extension_dir)

        # Some versions of `mktmpdir` return absolute paths, which will break make
        # if the paths contain spaces. However, on Ruby 1.9.x on Windows, relative
        # paths cause all C extension builds to fail.
        #
        # As such, we convert to a relative path unless we are using Ruby 1.9.x on
        # Windows. This means that when using Ruby 1.9.x on Windows, paths with
        # spaces do not work.
        #
        # Details: https://github.com/rubygems/rubygems/issues/977#issuecomment-171544940
        tmp_dest_relative = get_relative_path(tmp_dest.clone, extension_dir)

        if tmp_dest_relative
          full_tmp_dest = File.join(extension_dir, tmp_dest_relative)

          # TODO: remove in RubyGems 3
          if Gem.install_extension_in_lib && lib_dir
            FileUtils.mkdir_p lib_dir
            FileUtils.cp_r ext_path, lib_dir, remove_destination: true
          end

          FileUtils::Entry_.new(full_tmp_dest).traverse do |ent|
            destent = ent.class.new(dest_path, ent.rel)
            destent.exist? || FileUtils.mv(ent.path, destent.path)
          end
        end
      ensure
        FileUtils.rm_rf tmp_dest if tmp_dest
      end
    end

    def get_relative_path(path, base)
      path[0..base.length - 1] = "." if path.start_with?(base)
      path
    end

    def profile_target_directory
      case profile.to_sym
      when :release then "release"
      when :dev then "debug"
      else raise "unknown target directory for profile: #{profile}"
      end
    end

    def rubygems_invoked?
      ENV.key?("SOURCE_DATE_EPOCH")
    end

    # Error raised when no cdylib artifact was created
    class DylibNotFoundError < StandardError
      def initialize(dir)
        files = Dir.glob(File.join(dir, "**", "*")).map { |f| "- #{f}" }.join "\n"

        super <<~MSG
          Dynamic library not found for Rust extension (in #{dir})

          Make sure you set "crate-type" in Cargo.toml to "cdylib"

          Found files:
          #{files}
        MSG
      end
    end
  end
end