-
-
Notifications
You must be signed in to change notification settings - Fork 35
/
Rakefile
189 lines (152 loc) · 5.09 KB
/
Rakefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
EXAMPLES = Dir["examples/*"]
CLEAN = Rake::FileList.new.tap do |list|
list.include("**/target")
list.include("**/tmp")
end
def extra_args
seperator_index = ARGV.index("--")
seperator_index && ARGV[(seperator_index + 1)..-1] || []
end
def cargo_test_task(name, *args, crate: name)
task_name = "cargo:#{name}"
desc "Run cargo tests for #{name.inspect} against current Ruby"
task task_name do
default_args = ENV["CI"] || extra_args.include?("--verbose") ? [] : ["--quiet"]
test_args = ENV["CI"] || extra_args.include?("--verbose") ? ["--", "--nocapture"] : []
sh "cargo", "test", *default_args, *extra_args, *args, "-p", crate, *test_args
puts "=" * 80
end
task cargo: task_name
end
namespace :test do
cargo_test_task "rb-sys", "--features", "bindgen-layout-tests"
cargo_test_task "rb-sys-build"
cargo_test_task "rb-sys-tests"
cargo_test_task "rb-sys-env"
cargo_test_task "rb-sys-test-helpers"
cargo_test_task "rb-sys", "--all-targets", "--features", "bindgen-impl-debug,link-ruby" # Cover debian use case
desc "Test against all installed Rubies"
task :rubies do
cmd = <<~SH
gem install bundler:2.3.7 > /dev/null 2>&1
bundle check || bundle install -j3 > /dev/null 2>&1
bundle exec rake test:cargo
SH
sh "./script/xruby", "-c", cmd
end
namespace :examples do
task :rust_reverse do
cargo_args = extra_args || []
envs = [{}, {"ALTERNATE_CONFIG_SCRIPT" => "extconf_bare.rb"}]
Dir.chdir("examples/rust_reverse") do
envs.each do |env|
sh env, "rake", "clean", "compile", "test", *cargo_args
end
end
end
end
desc "Test all examples against all installed Rubies"
task examples: ["test:examples:rust_reverse"]
desc "Run unit tests for the gem"
task :gem do
Dir.chdir("gem") do
sh "rake"
end
end
end
desc "Run all tests"
task test: ["test:cargo", "test:gem", "test:examples"]
desc "Pretty the files"
task :fmt do
sh "cargo fmt"
sh "bundle exec standardrb --fix" if RUBY_VERSION >= "2.6.0"
sh "npx prettier --write $(git ls-files '*.yml')"
md_files = `git ls-files '*.md'`.split("\n").select { |f| File.exist?(f) }
sh "npx", "prettier", "--write", "--print-width=120", "--prose-wrap=always", *md_files
end
task format: [:fmt]
desc "Lint"
task :lint do
sh "bundle exec standardrb --format #{ENV.key?("CI") ? "github" : "progress"}" if RUBY_VERSION >= "2.6.0"
sh "cargo fmt --check"
sh "cargo clippy"
sh "shellcheck $(git ls-files '*.sh')"
end
namespace :data do
desc "Derive useful data from data/toolchains.json"
task :derive do
puts "Deriving data from data/toolchains.json"
require "json"
gen = ->(name, value) { File.write(File.join("data/derived", name), JSON.pretty_generate(value)) }
toolchains = JSON.parse(File.read("data/toolchains.json"))
toolchain_info_data_path = "gem/lib/rb_sys/toolchain_info/data.rb"
toolchain_data = {}
toolchains["toolchains"].each do |t|
tc = t.dup
tc.delete("dockerfile")
toolchain_data[tc.delete("ruby-platform")] = tc
end
File.write toolchain_info_data_path, <<~RUBY
# frozen_string_literal: true
# THIS FILE IS AUTO-GENERATED BY `rake data:derive`
module RbSys
class ToolchainInfo
# @private
DATA = #{toolchain_data.inspect}
end
end
RUBY
sh "bundle exec standardrb --fix #{toolchain_info_data_path}"
ruby_to_rust = {}
toolchains["toolchains"].each do |t|
raise "no dockerfile" unless File.exist?(t["dockerfile"])
raise "wrong ruby target" unless File.read(t["dockerfile"]).include?(t["ruby-platform"])
ruby_to_rust[t["ruby-platform"]] = t["rust-target"]
end
github_actions_matrix = toolchains["toolchains"]
.select { |t| t["supported"] }
.map { |t| t.slice("ruby-platform", "rust-target") if t["supported"] }
gen.call("ruby-to-rust.json", ruby_to_rust)
gen.call("github-actions-matrix.json", {include: github_actions_matrix})
end
end
namespace :bindings do
desc "Copy bindings to /tmp/bindings"
task :generate do
require "rbconfig"
c = RbConfig::CONFIG
version = RbSys::VERSION
sh "cargo build || env RB_SYS_DEBUG_BUILD=1 cargo build"
out_dir = "/tmp/bindings/rb-sys-#{version}/#{c["ruby_version"]}/#{c["arch"]}"
bindings_file = Dir["target/debug/build/rb-sys-*/out/bindings.rs"].max_by { |f| File.mtime(f) }
abort "No bindings file found" unless bindings_file
puts "Copying #{bindings_file} to #{out_dir}/bindings.rs"
FileUtils.mkdir_p(out_dir)
FileUtils.cp(bindings_file, out_dir)
end
end
namespace :debug do
task :mkmf do
require "tmpdir"
tmpdir = Dir.mktmpdir
chdir(tmpdir) do
touch "testing.c"
touch "testing.h"
sh "ruby", "-rmkmf", "-e", "create_makefile('testing')"
puts File.read("Makefile")
end
rm_rf(tmpdir)
end
end
desc "Clean up"
task :clean do
CLEAN.each { |f| rm_rf(f) }
end
desc "Run criterion benchmarks"
task :bench do
Dir.chdir("bench") do
extra_args = ARGV[(ARGV.index("bench") + 1)..-1] || []
sh "cargo", "bench", *extra_args
end
end
task default: :test