Skip to content

Commit

Permalink
Fix rake ci:* tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
kateinoigakukun committed Jan 3, 2024
1 parent ed134ad commit f4fd002
Show file tree
Hide file tree
Showing 9 changed files with 155 additions and 51 deletions.
51 changes: 38 additions & 13 deletions Rakefile
Expand Up @@ -63,26 +63,51 @@ BUILDS
TOOLCHAINS[toolchain.name] = toolchain
end

class BuildTask < Struct.new(:name, :target, :build_command)
def ruby_cache_key
return @key if @key
require "open3"
cmd = build_command + ["--print-ruby-cache-key"]
stdout, status = Open3.capture2(*cmd)
unless status.success?
raise "Command failed with status (#{status.exitstatus}): #{cmd.join ""}"
end
require "json"
@key = JSON.parse(stdout)
end

def hexdigest
ruby_cache_key["hexdigest"]
end
def artifact
ruby_cache_key["artifact"]
end
end

namespace :build do
BUILD_TASKS =
BUILDS.map do |src, target, profile|
name = "#{src}-#{target}-#{profile}"

desc "Cross-build Ruby for #{@target}"
build_command = [
"exe/rbwasm",
"build",
"--ruby-version",
src,
"--target",
target,
"--build-profile",
profile,
"--disable-gems",
"-o",
"/dev/null"
]
desc "Cross-build Ruby for #{target}"
task name do
sh *[
"exe/rbwasm",
"build",
"--ruby-version",
src,
"--target",
target,
"--build-profile",
profile,
"-o",
"/dev/null"
]
sh *build_command
end

BuildTask.new(name, target, build_command)
end

desc "Clean build directories"
Expand Down
6 changes: 5 additions & 1 deletion lib/ruby_wasm/build.rb
Expand Up @@ -51,7 +51,11 @@ def initialize(
@openssl = RubyWasm::OpenSSLProduct.new(@build_dir, @target, @toolchain)

build_params =
RubyWasm::BuildParams.new(options.merge(name: name, target: target))
RubyWasm::BuildParams.new(
name: name,
target: target,
default_exts: options[:default_exts]
)

@crossruby =
RubyWasm::CrossRubyProduct.new(
Expand Down
2 changes: 1 addition & 1 deletion lib/ruby_wasm/build/product/crossruby.rb
Expand Up @@ -257,7 +257,7 @@ def dest_dir
end

def artifact
File.join(@rubies_dir, "ruby-#{name}.tar.gz")
File.join(@rubies_dir, "#{name}.tar.gz")
end

def extinit_obj
Expand Down
60 changes: 50 additions & 10 deletions lib/ruby_wasm/cli.rb
Expand Up @@ -48,7 +48,8 @@ def build(args)
ruby_version: "3.3",
target_triplet: "wasm32-unknown-wasi",
profile: "full",
stdlib: true
stdlib: true,
disable_gems: false
}
OptionParser
.new do |opts|
Expand Down Expand Up @@ -90,11 +91,29 @@ def build(args)
opts.on("--[no-]stdlib", "Include stdlib") do |stdlib|
options[:stdlib] = stdlib
end

opts.on("--disable-gems", "Disable gems") do
options[:disable_gems] = true
end

opts.on("--format FORMAT", "Output format") do |format|
options[:format] = format
end

opts.on("--print-ruby-cache-key", "Print Ruby cache key") do
options[:print_ruby_cache_key] = true
end
end
.parse!(args)

verbose = RubyWasm.logger.level == :debug
executor = RubyWasm::BuildExecutor.new(verbose: verbose)
packager = self.derive_packager(options)

if options[:print_ruby_cache_key]
self.do_print_ruby_cache_key(packager)
exit
end

unless options[:output]
@stderr.puts "Output file is not specified"
Expand All @@ -105,11 +124,13 @@ def build(args)

if options[:save_temps]
tmpdir = Dir.mktmpdir
self.do_build(executor, tmpdir, options)
self.do_build(executor, tmpdir, packager, options)
@stderr.puts "Temporary files are saved to #{tmpdir}"
exit
else
Dir.mktmpdir { |tmpdir| self.do_build(executor, tmpdir, options) }
Dir.mktmpdir do |tmpdir|
self.do_build(executor, tmpdir, packager, options)
end
end
end

Expand All @@ -119,20 +140,39 @@ def build_config(options)
config = { target: options[:target_triplet], src: options[:ruby_version] }
case options[:profile]
when "full"
config[:profile] = RubyWasm::Packager::ALL_DEFAULT_EXTS
config[:default_exts] = RubyWasm::Packager::ALL_DEFAULT_EXTS
when "minimal"
config[:profile] = ""
config[:default_exts] = ""
else
RubyWasm.logger.error "Unknown profile: #{options[:profile]} (available: full, minimal)"
exit 1
end
config[:suffix] = "-#{options[:profile]}"
config
end

def derive_packager(options)
if defined?(Bundler) && !options[:disable_gems]
definition = Bundler.definition
end
RubyWasm::Packager.new(build_config(options), definition)
end

def do_print_ruby_cache_key(packager)
ruby_core_build = packager.ruby_core_build
require "digest"
digest = Digest::SHA256.new
ruby_core_build.cache_key(digest)
hexdigest = digest.hexdigest
require "json"
@stdout.puts JSON.generate(
hexdigest: hexdigest,
artifact: ruby_core_build.artifact
)
end

def do_build(executor, tmp_dir, options)
definition = Bundler.definition if defined?(Bundler)
packager =
RubyWasm::Packager.new(tmp_dir, options[:target_triplet], definition)
wasm_bytes = packager.package(executor, options)
def do_build(executor, tmpdir, packager, options)
wasm_bytes = packager.package(executor, tmpdir, options)
@stderr.puts "Size: #{SizeFormatter.format(wasm_bytes.size)}"
case options[:output]
when "-"
Expand Down
18 changes: 10 additions & 8 deletions lib/ruby_wasm/packager.rb
Expand Up @@ -2,25 +2,24 @@
class RubyWasm::Packager
# Initializes a new instance of the RubyWasm::Packager class.
#
# @param dest_dir [String] The destination used to construct the filesystem.
# @param config [Hash] The build config used for building Ruby.
# @param definition [Bundler::Definition] The Bundler definition.
def initialize(dest_dir, config = nil, definition = nil)
@dest_dir = dest_dir
def initialize(config = nil, definition = nil)
@definition = definition
@config = config
end

# Packages the Ruby code into a Wasm binary. (including extensions)
#
# @param executor [RubyWasm::BuildExecutor] The executor for building the Wasm binary.
# @param dest_dir [String] The destination used to construct the filesystem.
# @param options [Hash] The packaging options.
# @return [Array<Integer>] The bytes of the packaged Wasm binary.
def package(executor, options)
ruby_core = RubyWasm::Packager::Core.new(self)
def package(executor, dest_dir, options)
ruby_core = self.ruby_core_build()
tarball = ruby_core.build(executor, options)

fs = RubyWasm::Packager::FileSystem.new(@dest_dir, self)
fs = RubyWasm::Packager::FileSystem.new(dest_dir, self)
fs.package_ruby_root(tarball, executor)

ruby_wasm_bin = File.expand_path("bin/ruby", fs.ruby_root)
Expand All @@ -40,6 +39,10 @@ def package(executor, options)
wasm_bytes
end

def ruby_core_build
@ruby_core_build ||= RubyWasm::Packager::Core.new(self)
end

# The list of excluded gems from the Bundler definition.
EXCLUDED_GEMS = %w[ruby_wasm bundler]

Expand Down Expand Up @@ -120,8 +123,7 @@ def build_options
src: "3.3",
default_exts: ALL_DEFAULT_EXTS
}
override = {}
override = @config["build_options"] || {} if @config
override = @config || {}
# Merge the default options with the config options
default.merge(override)
end
Expand Down
46 changes: 37 additions & 9 deletions lib/ruby_wasm/packager/core.rb
@@ -1,3 +1,5 @@
require "forwardable"

class RubyWasm::Packager::Core
def initialize(packager)
@packager = packager
Expand All @@ -8,6 +10,10 @@ def build(executor, options)
strategy.build(executor, options)
end

extend Forwardable

def_delegators :build_strategy, :cache_key, :artifact

private

def build_strategy
Expand Down Expand Up @@ -44,13 +50,42 @@ def specs_with_extensions
[spec, exts]
end
end

def cache_key(digest)
raise NotImplementedError
end

def artifact
raise NotImplementedError
end
end

class DynamicLinking < BuildStrategy
end

class StaticLinking < BuildStrategy
def build(executor, options)
build = derive_build
if defined?(Bundler)
Bundler.with_unbundled_env do
build.crossruby.build(executor, remake: options[:remake])
end
else
build.crossruby.build(executor, remake: options[:remake])
end
build.crossruby.artifact
end

def cache_key(digest)
derive_build.cache_key(digest)
end

def artifact
derive_build.crossruby.artifact
end

def derive_build
return @build if @build
@build ||= RubyWasm::Build.new(name, **@packager.full_build_options)
@build.crossruby.user_exts = user_exts
@build.crossruby.debugflags = %w[-g]
Expand All @@ -63,14 +98,7 @@ def build(executor, options)
-Xlinker
stack-size=16777216
]
if defined?(Bundler)
Bundler.with_unbundled_env do
@build.crossruby.build(executor, remake: options[:remake])
end
else
@build.crossruby.build(executor, remake: options[:remake])
end
@build.crossruby.artifact
@build
end

def user_exts
Expand All @@ -94,7 +122,7 @@ def name
options = @packager.full_build_options
src_channel = options[:src][:name]
target_triplet = options[:target]
base = "ruby-#{src_channel}-static-#{target_triplet}"
base = "ruby-#{src_channel}-#{target_triplet}#{options[:suffix]}"
exts = specs_with_extensions.sort
hash = ::Digest::MD5.new
specs_with_extensions.each { |spec, _| hash << spec.full_name }
Expand Down
9 changes: 7 additions & 2 deletions lib/ruby_wasm/packager/file_system.rb
Expand Up @@ -83,8 +83,13 @@ def remove_non_runtime_files(executor)
end
end

def bundle_dir = File.join(@dest_dir, bundle_relative_path)
def ruby_root = @ruby_root
def bundle_dir
File.join(@dest_dir, bundle_relative_path)
end

def ruby_root
@ruby_root
end

private

Expand Down
7 changes: 2 additions & 5 deletions tasks/ci.rake
Expand Up @@ -54,11 +54,8 @@ def rake_task_matrix
{
task: "build:#{build.name}",
artifact:
Pathname
.new(build.crossruby.artifact)
.relative_path_from(LIB_ROOT)
.to_s,
artifact_name: File.basename(build.crossruby.artifact, ".tar.gz"),
Pathname.new(build.artifact).relative_path_from(LIB_ROOT).to_s,
artifact_name: File.basename(build.artifact, ".tar.gz"),
builder: build.target,
rubies_cache_key: ruby_cache_keys[build.name]
}
Expand Down
7 changes: 5 additions & 2 deletions tasks/gem.rake
Expand Up @@ -7,6 +7,9 @@ Rake::TestTask.new(:test) do |t|
t.test_files = FileList["test/**/test_*.rb"]
end

require "rb_sys/extensiontask"
begin
require "rb_sys/extensiontask"

RbSys::ExtensionTask.new("ruby_wasm") { |ext| ext.lib_dir = "lib/ruby_wasm" }
RbSys::ExtensionTask.new("ruby_wasm") { |ext| ext.lib_dir = "lib/ruby_wasm" }
rescue LoadError
end

0 comments on commit f4fd002

Please sign in to comment.