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

Constant Summary collapse

GLOBAL_RUSTFLAGS =
["--cfg=rb_sys_gem"]

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)


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
# 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 = "$(srcdir)/#{builder.ext_dir}".chomp("/")
  RbConfig.expand(srcdir = srcprefix.dup)

  full_cargo_command = cargo_command(srcdir, builder)

  make_install = +"    \#{conditional_assign(\"RB_SYS_BUILD_DIR\", File.join(Dir.pwd, \".rb-sys\"))}\n    \#{conditional_assign(\"CARGO\", \"cargo\")}\n    \#{conditional_assign(\"CARGO_BUILD_TARGET\", builder.target)}\n    \#{conditional_assign(\"SOEXT\", builder.so_ext)}\n\n    # Determine the prefix Cargo uses for the lib.\n    \#{if_neq_stmt(\"$(SOEXT)\", \"dll\")}\n    \#{conditional_assign(\"SOEXT_PREFIX\", \"lib\", indent: 1)}\n    \#{endif_stmt}\n\n    \#{conditional_assign(\"RB_SYS_CARGO_PROFILE\", builder.profile)}\n    \#{conditional_assign(\"RB_SYS_CARGO_FEATURES\", builder.features.join(\",\"))}\n    \#{conditional_assign(\"RB_SYS_GLOBAL_RUSTFLAGS\", GLOBAL_RUSTFLAGS.join(\" \"))}\n    \#{conditional_assign(\"RB_SYS_EXTRA_RUSTFLAGS\", builder.extra_rustflags.join(\" \"))}\n\n    # Set dirname for the profile, since the profiles do not directly map to target dir (i.e. dev -> debug)\n    \#{if_eq_stmt(\"$(RB_SYS_CARGO_PROFILE)\", \"dev\")}\n    \#{conditional_assign(\"RB_SYS_CARGO_PROFILE_DIR\", \"debug\", indent: 1)}\n    \#{else_stmt}\n    \#{conditional_assign(\"RB_SYS_CARGO_PROFILE_DIR\", \"$(RB_SYS_CARGO_PROFILE)\", indent: 1)}\n    \#{endif_stmt}\n\n    # Set the build profile (dev, release, etc.) Compat with Rust 1.51.\n    \#{if_eq_stmt(\"$(RB_SYS_CARGO_PROFILE)\", \"release\")}\n    \#{assign_stmt(\"RB_SYS_CARGO_PROFILE_FLAG\", \"--release\", indent: 1)}\n    \#{else_stmt}\n    \#{assign_stmt(\"RB_SYS_CARGO_PROFILE_FLAG\", \"--profile $(RB_SYS_CARGO_PROFILE)\", indent: 1)}\n    \#{endif_stmt}\n\n    # Account for sub-directories when using `--target` argument with Cargo\n    \#{if_neq_stmt(\"$(CARGO_BUILD_TARGET)\", \"\")}\n    \#{assign_stmt(\"RB_SYS_CARGO_BUILD_TARGET_DIR\", \"target/$(CARGO_BUILD_TARGET)\", indent: 1)}\n    \#{else_stmt}\n    \#{assign_stmt(\"RB_SYS_CARGO_BUILD_TARGET_DIR\", \"target\", indent: 1)}\n    \#{endif_stmt}\n\n    target_prefix = \#{target_prefix}\n    TARGET_NAME = \#{target[/\\A\\w+/]}\n    TARGET_ENTRY = \#{RbConfig::CONFIG[\"EXPORT_PREFIX\"]}Init_$(TARGET_NAME)\n    RUBYARCHDIR   = $(sitearchdir)$(target_prefix)\n    TARGET = \#{target}\n    DLLIB = $(TARGET).\#{RbConfig::CONFIG[\"DLEXT\"]}\n    TARGET_DIR = \#{Dir.pwd}/$(RB_SYS_CARGO_BUILD_TARGET_DIR)/$(RB_SYS_CARGO_PROFILE_DIR)\n    RUSTLIB = $(TARGET_DIR)/$(SOEXT_PREFIX)$(TARGET_NAME).$(SOEXT)\n\n    CLEANOBJS = $(TARGET_DIR)/.fingerprint $(TARGET_DIR)/incremental $(TARGET_DIR)/examples $(TARGET_DIR)/deps $(TARGET_DIR)/build $(TARGET_DIR)/.cargo-lock $(TARGET_DIR)/*.d $(TARGET_DIR)/*.rlib $(RB_SYS_BUILD_DIR)\n    DEFFILE = $(TARGET_DIR)/$(TARGET)-$(arch).def\n    CLEANLIBS = $(DLLIB) $(RUSTLIB) $(DEFFILE)\n\n    \#{base_makefile(srcdir)}\n\n    \#{if_neq_stmt(\"$(RB_SYS_VERBOSE)\", \"\")}\n    \#{assign_stmt(\"Q\", \"$(0=@)\", indent: 1)}\n    \#{endif_stmt}\n\n    \#{env_vars(builder)}\n    \#{export_env(\"RUSTFLAGS\", \"$(RB_SYS_GLOBAL_RUSTFLAGS) $(RB_SYS_EXTRA_RUSTFLAGS) $(RUSTFLAGS)\")}\n\n    FORCE: ;\n\n    $(TARGET_DIR):\n    \\t$(ECHO) creating target directory \\\\($(@)\\\\)\n    \\t$(Q) $(MAKEDIRS) $(TARGET_DIR)\n\n    \#{deffile_definition}\n\n    \#{optional_rust_toolchain(builder)}\n\n    $(RUSTLIB): \#{deffile_definition ? \"$(DEFFILE) \" : nil}FORCE\n    \\t$(ECHO) generating $(@) \\\\(\"$(RB_SYS_CARGO_PROFILE)\"\\\\)\n    \\t$(Q) \#{full_cargo_command}\n\n    $(DLLIB): $(RUSTLIB)\n    \\t$(Q) $(COPY) \"$(RUSTLIB)\" $@\n\n    install-so: $(DLLIB)\n    \\t$(ECHO) installing $(DLLIB) to $(RUBYARCHDIR)\n    \\t$(Q) $(MAKEDIRS) $(RUBYARCHDIR)\n    \\t$(INSTALL_PROG) $(DLLIB) $(RUBYARCHDIR)\n\n    install: \#{builder.clean_after_install ? \"install-so realclean\" : \"install-so\"}\n\n    all: \#{$extout ? \"install\" : \"$(DLLIB)\"}\n  MAKE\n\n  gsub_cargo_command!(make_install, builder: builder)\n\n  File.write(\"Makefile\", make_install)\nend\n"