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

def create_rust_makefile(target, &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 = CargoBuilder.new(spec)

  yield builder if blk

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

  full_cargo_command = cargo_command(srcdir, builder)

  # rubocop:disable Style/GlobalVars
  make_install = +<<~MAKE
    CARGO ?= cargo
    CARGO_BUILD_TARGET ?= #{builder.target}
    SOEXT ?= #{builder.so_ext}

    # Determine the prefix Cargo uses for the lib.
    ifneq ($(SOEXT),dll)
      SOEXT_PREFIX ?= lib
    endif

    RB_SYS_CARGO_PROFILE ?= #{builder.profile}
    RB_SYS_CARGO_FEATURES ?= #{builder.features.join(",")}

    # Set dirname for the profile, since the profiles do not directly map to target dir (i.e. dev -> debug)
    ifeq ($(RB_SYS_CARGO_PROFILE),dev)
      RB_SYS_CARGO_PROFILE_DIR ?= debug
    else
      RB_SYS_CARGO_PROFILE_DIR ?= $(RB_SYS_CARGO_PROFILE)
    endif

    # Set the build profile (dev, release, etc.) Compat with Rust 1.51.
    ifeq ($(RB_SYS_CARGO_PROFILE),release)
      RB_SYS_CARGO_PROFILE_FLAG = --release
    else
      RB_SYS_CARGO_PROFILE_FLAG = --profile $(RB_SYS_CARGO_PROFILE)
    endif

    # Account for sub-directories when using `--target` argument with Cargo
    ifneq ($(CARGO_BUILD_TARGET),)
      RB_SYS_CARGO_BUILD_TARGET_DIR ?= target/$(CARGO_BUILD_TARGET)
    else
      RB_SYS_CARGO_BUILD_TARGET_DIR ?= target
    endif

    target_prefix = #{target_prefix}
    TARGET_NAME = #{target[/\A\w+/]}
    TARGET_ENTRY = #{RbConfig::CONFIG["EXPORT_PREFIX"]}Init_$(TARGET_NAME)
    CLEANLIBS = $(RUSTLIB) $(DLLIB) $(DEFFILE)
    RUBYARCHDIR   = $(sitearchdir)$(target_prefix)
    TARGET = #{target}
    DLLIB = $(TARGET).#{RbConfig::CONFIG["DLEXT"]}
    TARGET_DIR = #{Dir.pwd}/$(RB_SYS_CARGO_BUILD_TARGET_DIR)/$(RB_SYS_CARGO_PROFILE_DIR)
    RUSTLIB = $(TARGET_DIR)/$(SOEXT_PREFIX)$(TARGET_NAME).$(SOEXT)

    DISTCLEANDIRS = $(TARGET_DIR)
    DEFFILE = $(TARGET_DIR)/$(TARGET)-$(arch).def
    #{base_makefile(srcdir)}

    ifneq ($(RB_SYS_VERBOSE),)
      Q = $(0=@)
    endif

    #{env_vars(builder)}

    FORCE: ;

    $(TARGET_DIR):
    \t$(ECHO) creating target directory \\($(@)\\)
    \t$(Q) $(MAKEDIRS) $(TARGET_DIR)

    $(DEFFILE): $(TARGET_DIR)
    \t$(ECHO) generating $(@)
    \t$(Q) ($(COPY) $(srcdir)/$(TARGET).def $@ 2> /dev/null) || (echo EXPORTS && echo $(TARGET_ENTRY)) > $@

    $(DLLIB): $(DEFFILE) FORCE
    \t$(ECHO) generating $(@) \\("$(RB_SYS_CARGO_PROFILE)"\\)
    \t$(Q) #{full_cargo_command}
    \t$(Q) $(COPY) "$(RUSTLIB)" $@

    install: $(DLLIB) Makefile
    \t$(ECHO) installing $(DLLIB)
    \t$(Q) $(MAKEDIRS) $(RUBYARCHDIR)
    \t$(Q) $(INSTALL_PROG) $(DLLIB) $(RUBYARCHDIR)

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

  gsub_cargo_command!(make_install, builder: builder)

  File.write("Makefile", make_install)
end