Module: RbSys::Mkmf

Defined in:
lib/rb_sys/mkmf.rb,
lib/rb_sys/mkmf/config.rb

Overview

Helper class for creating Rust Makefiles

Defined Under Namespace

Classes: Config

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.

Examples:

Basic

require 'mkmf'
require 'rb_sys/mkmf'

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

Configure a custom build profile

require 'mkmf'
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[some_cargo_feature]
  r.extra_rustflags = %w[--cfg=foo]
  r.target_dir = "some/target/dir"
end

Yields:

  • (builder)


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

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, {})
  cargo_builder = CargoBuilder.new(spec)
  builder = Config.new(cargo_builder)

  yield builder if blk

  srcprefix = File.join("$(srcdir)", builder.ext_dir.gsub(/\A\.\/?/, "")).chomp("/")
  RbConfig.expand(srcdir = srcprefix.dup)

  full_cargo_command = cargo_command(srcdir, builder)

  global_rustflags = []
  # Re-enable if this causes issues, but it should be fine and it prevents unnecessary recompilation
  # global_rustflags << "--cfg=rb_sys_gem" unless builder.use_cargo_build
  global_rustflags << "--cfg=rb_sys_use_stable_api_compiled_fallback" if builder.use_stable_api_compiled_fallback?

  make_install = +<<~MAKE
    #{conditional_assign("RB_SYS_BUILD_DIR", File.join(Dir.pwd, ".rb-sys"))}
    #{conditional_assign("CARGO", "cargo")}
    #{conditional_assign("CARGO_BUILD_TARGET", builder.target)}
    #{conditional_assign("SOEXT", builder.so_ext)}
    #{try_load_bundled_libclang(builder)}

    # Determine the prefix Cargo uses for the lib.
    #{if_neq_stmt("$(SOEXT)", "dll")}
    #{conditional_assign("SOEXT_PREFIX", "lib", indent: 1)}
    #{endif_stmt}

    #{set_cargo_profile(builder)}
    #{conditional_assign("RB_SYS_CARGO_FEATURES", builder.features.join(","))}
    #{conditional_assign("RB_SYS_GLOBAL_RUSTFLAGS", global_rustflags.join(" "))}
    #{conditional_assign("RB_SYS_EXTRA_RUSTFLAGS", builder.extra_rustflags.join(" "))}
    #{conditional_assign("RB_SYS_EXTRA_CARGO_ARGS", builder.extra_cargo_args.join(" "))}
    #{conditional_assign("RB_SYS_CARGO_MANIFEST_DIR", builder.manifest_dir)}

    # Set dirname for the profile, since the profiles do not directly map to target dir (i.e. dev -> debug)
    #{if_eq_stmt("$(RB_SYS_CARGO_PROFILE)", "dev")}
    #{conditional_assign("RB_SYS_CARGO_PROFILE_DIR", "debug", indent: 1)}
    #{else_stmt}
    #{conditional_assign("RB_SYS_CARGO_PROFILE_DIR", "$(RB_SYS_CARGO_PROFILE)", indent: 1)}
    #{endif_stmt}

    # Set the build profile (dev, release, etc.).
    #{assign_stmt("RB_SYS_CARGO_PROFILE_FLAG", "--profile $(RB_SYS_CARGO_PROFILE)", indent: 1)}

    # Account for sub-directories when using `--target` argument with Cargo
    #{conditional_assign("RB_SYS_CARGO_TARGET_DIR", "target")}
    #{if_neq_stmt("$(CARGO_BUILD_TARGET)", "")}
    #{assign_stmt("RB_SYS_FULL_TARGET_DIR", "$(RB_SYS_CARGO_TARGET_DIR)/$(CARGO_BUILD_TARGET)", indent: 1)}
    #{else_stmt}
    #{assign_stmt("RB_SYS_FULL_TARGET_DIR", "$(RB_SYS_CARGO_TARGET_DIR)", indent: 1)}
    #{endif_stmt}

    target_prefix = #{target_prefix}
    TARGET_NAME = #{target[/\A\w+/]}
    TARGET_ENTRY = #{RbConfig::CONFIG["EXPORT_PREFIX"]}Init_$(TARGET_NAME)
    RUBYARCHDIR = $(sitearchdir)$(target_prefix)
    TARGET = #{target}
    DLLIB = $(TARGET).#{RbConfig::CONFIG["DLEXT"]}
    RUSTLIBDIR = $(RB_SYS_FULL_TARGET_DIR)/$(RB_SYS_CARGO_PROFILE_DIR)
    RUSTLIB = $(RUSTLIBDIR)/$(SOEXT_PREFIX)$(TARGET_NAME).$(SOEXT)
    TIMESTAMP_DIR = .
    POSTLINK = #{RbConfig::CONFIG["POSTLINK"] || "$(ECHO) skipping postlink"}

    CLEANOBJS = $(RUSTLIBDIR) $(RB_SYS_BUILD_DIR)
    CLEANLIBS = $(DLLIB) $(RUSTLIB)
    RUBYGEMS_CLEAN_DIRS = $(CLEANOBJS) $(CLEANFILES) #{builder.rubygems_clean_dirs.join(" ")}

    #{base_makefile(srcdir)}

    .PHONY: gemclean

    #{if_neq_stmt("$(RB_SYS_VERBOSE)", "")}
    #{assign_stmt("Q", "$(0=@)", indent: 1)}
    #{endif_stmt}

    #{env_vars(builder)}
    #{export_env("RUSTFLAGS", "$(RB_SYS_GLOBAL_RUSTFLAGS) $(RB_SYS_EXTRA_RUSTFLAGS) $(RUSTFLAGS)")}

    FORCE: ;

    #{optional_rust_toolchain(builder)}

    #{timestamp_file("sitearchdir")}:
    \t$(Q) $(MAKEDIRS) $(@D) $(RUBYARCHDIR)
    \t$(Q) $(TOUCH) $@

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

    $(DLLIB): $(RUSTLIB)
    \t$(Q) $(COPY) "$(RUSTLIB)" $@
    \t$(Q) $(POSTLINK)

    install-so: $(DLLIB) #{timestamp_file("sitearchdir")}
    \t$(ECHO) installing $(DLLIB) to $(RUBYARCHDIR)
    \t#{fixup_libnames}
    \t$(Q) $(MAKEDIRS) $(RUBYARCHDIR)
    \t$(INSTALL_PROG) $(DLLIB) $(RUBYARCHDIR)

    gemclean:
    \t$(ECHO) Cleaning gem artifacts
    \t-$(Q)$(RM_RF) $(RUBYGEMS_CLEAN_DIRS) 2> /dev/null || true

    install: #{builder.clean_after_install ? "install-so gemclean" : "install-so"}

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

  gsub_cargo_command!(make_install, builder: builder)

  File.write("Makefile", make_install)
end