Class: RbSys::ExtensionTask

Inherits:
Rake::ExtensionTask
  • Object
show all
Defined in:
lib/rb_sys/extensiontask.rb

Overview

ExtensionTask is a Rake::ExtensionTask subclass that is used to tailored for Rust extensions. It has the same options a ‘Rake::ExtensionTask`.

Examples:

RbSys::ExtensionTask.new("my-crate", my_gemspec) do |ext|
  ext.lib_dir = "lib/my-crate"
end

Returns:

  • (Rake::ExtensionTask)

See Also:

Instance Method Summary collapse

Constructor Details

#initialize(name = nil, gem_spec = :undefined) ⇒ ExtensionTask

Returns a new instance of ExtensionTask.



25
26
27
# File 'lib/rb_sys/extensiontask.rb', line 25

def initialize(name = nil, gem_spec = :undefined)
  super
end

Instance Method Details

#binary(_platf) ⇒ Object



67
68
69
# File 'lib/rb_sys/extensiontask.rb', line 67

def binary(_platf)
  super.tr("-", "_")
end

#cargo_metadataObject



59
60
61
# File 'lib/rb_sys/extensiontask.rb', line 59

def 
  @cargo_metadata ||= Cargo::Metadata.new_or_inferred(@name)
end

#cross_compiling(&block) ⇒ Object



84
85
86
# File 'lib/rb_sys/extensiontask.rb', line 84

def cross_compiling(&block)
  @cross_compiling_blocks << block if block
end

#defineObject



52
53
54
55
56
57
# File 'lib/rb_sys/extensiontask.rb', line 52

def define
  super
  define_env_tasks

  CLEAN.include(target_directory) if defined?(CLEAN)
end

#define_env_tasksObject



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

def define_env_tasks
  task "rb_sys:env:default" do
    ENV["RB_SYS_CARGO_TARGET_DIR"] ||= target_directory
    ENV["RB_SYS_CARGO_MANIFEST_DIR"] ||= .manifest_directory
    ENV["RB_SYS_CARGO_PROFILE"] ||= "release"
  end

  desc "Use the debug profile for building native Rust extensions"
  task "rb_sys:env:dev" do
    ENV["RB_SYS_CARGO_PROFILE"] = "dev"
  end

  desc "Use the release profile for building native Rust extensions"
  task "rb_sys:env:release" do
    ENV["RB_SYS_CARGO_PROFILE"] = "release"
  end

  file extconf => "rb_sys:env:default"

  desc 'Compile the native Rust extension with the "dev" profile'
  task "compile:dev" => ["rb_sys:env:dev", "compile"]

  desc 'Compile the native Rust extension with the "release" profile'
  task "compile:release" => ["rb_sys:env:release", "compile"]
end

#define_native_tasks(for_platform = nil, ruby_ver = RUBY_VERSION, callback = nil) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
# File 'lib/rb_sys/extensiontask.rb', line 92

def define_native_tasks(for_platform = nil, ruby_ver = RUBY_VERSION, callback = nil)
  cb = proc do |gemspec|
    callback&.call(gemspec)

    @cross_compiling_blocks.each do |block|
      block.call(gemspec)
    end
  end

  super(for_platform, ruby_ver, cb)
end

#extconfObject



63
64
65
# File 'lib/rb_sys/extensiontask.rb', line 63

def extconf
  File.join(.manifest_directory, "extconf.rb")
end

#init(name = nil, gem_spec = nil) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/rb_sys/extensiontask.rb', line 29

def init(name = nil, gem_spec = nil)
  super(name, lint_gem_spec(name, gem_spec))

  @orginal_ext_dir = @ext_dir
  @ext_dir = .manifest_directory
  @source_pattern = nil
  @compiled_pattern = "*.{obj,so,bundle,dSYM}"
  @cross_compile = ENV.key?("RUBY_TARGET")
  @cross_platform = [ENV["RUBY_TARGET"]].compact
  @cross_compiling_blocks = []
  @cross_compiling_blocks << proc do |gemspec|
    warn "Removing unneeded dependencies from native gemspec"
    gemspec.dependencies.reject! { |d| d.name == "rb_sys" }
  end
  @cross_compiling_blocks << proc do |gemspec|
    warn "Removing source files from native gemspec"
    gemspec.files.reject! { |f| f.end_with?(".rs") }
    gemspec.files.reject! { |f| f.match?(/Cargo.(toml|lock)$/) }
    gemspec.files.reject! { |f| extconf.end_with?(f) }
    gemspec.extensions.reject! { |f| f.end_with?("Cargo.toml") }
  end
end

#source_filesObject

I’m not sure why this is necessary, can it be removed?



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/rb_sys/extensiontask.rb', line 72

def source_files
  list = FileList[
    "#{ext_dir}/**/*.{rs,rb,c,h,toml}",
    "**/Cargo.{toml,lock}",
    "**/.cargo/**/*",
    "#{ext_dir}/lib/**/*"
  ]
  list.include("#{ext_dir}/#{@source_pattern}") if @source_pattern
  list.exclude(File.join(target_directory, "**/*"))
  list
end

#target_directoryObject



88
89
90
# File 'lib/rb_sys/extensiontask.rb', line 88

def target_directory
  .target_directory
end