Module: RbSys::Mkmf

Defined in:
lib/rb_sys/mkmf.rb

Overview

Helper class for creating Rust Makefiles

Instance Method Summary collapse

Instance Method Details

#create_rust_makefile(target, srcprefix = nil) {|builder| ... } ⇒ Object

Helper for building Rust extensions by creating a Ruby compatible makefile for Rust. By using this class, your rust extension will be 100% compatible with the rake-compiler gem, which allows for easy cross compilation.

. require 'rb_sys/mkmf'

. create_rust_makefile("my_extension") #=> Generate a Makefile in the current directory

. require 'rb_sys/mkmf'

. create_rust_makefile("my_extension") do |r| . # All of these are optional . r.env = { 'FOO' => 'bar' } . r.profile = ENV.fetch('CARGO_BUILD_PROFILE', :dev).to_sym . r.features = %w . end

Examples:

Basic

require 'mkmf'

Configure a custom build profile

require 'mkmf'

Yields:

  • (builder)


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
# File 'lib/rb_sys/mkmf.rb', line 31

def create_rust_makefile(target, srcprefix = nil, &blk)
  if target.include?("/")
    target_prefix, target = File.split(target)
    target_prefix[0, 0] = "/"
  else
    target_prefix = ""
  end

  spec = Struct.new(:name, :metadata).new(target, {})
  builder = Gem::Ext::CargoBuilder.new(spec)

  yield builder if blk

  srcprefix ||= "$(srcdir)/#{srcprefix}".chomp("/")
  RbConfig.expand(srcdir = srcprefix.dup)

  # rubocop:disable Style/GlobalVars
  make_install = <<~MAKE
    target_prefix = #{target_prefix}
    CARGO_PROFILE = release
    CLEANLIBS = $(RUSTLIB) $(DLLIB)
    DISTCLEANDIRS = target/
    RUBYARCHDIR   = $(sitearchdir)$(target_prefix)
    RUSTLIB = #{dllib_path(builder)}
    TARGET = #{target}
    DLLIB = $(TARGET).#{RbConfig::CONFIG["DLEXT"]}

    #{base_makefile(srcdir)}

    #{env_vars(builder)}

    FORCE: ;

    $(DLLIB): FORCE
    \t#{cargo_command(srcdir, builder)}
    \t$(COPY) "$(RUSTLIB)" $@

    install: $(DLLIB)
    \t$(INSTALL_PROG) $(DLLIB) $(RUBYARCHDIR)

    all: #{$extout ? "install" : "$(DLLIB)"}
  MAKE

  File.write("Makefile", make_install)
end