From e50ee9b853757b0a88a9d7f905185ba1c8ef6e43 Mon Sep 17 00:00:00 2001 From: Luke Mcildoon Date: Fri, 19 Oct 2018 12:12:18 +1100 Subject: [PATCH 1/3] fix build process and readme, permit using a vendored mruby per-project --- README.md | 25 ++++++++++++++++++++----- bin/ruby-wasm | 32 ++++++++++++++++++++++++++++++-- 2 files changed, 50 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index cf4764c..1083f36 100644 --- a/README.md +++ b/README.md @@ -35,17 +35,32 @@ What it doesn't do yet (maybe something you'd like to work on 🤔): First, make sure you have the WebAssembly toolchain installed and activated — see the ["Getting Started" guide](http://webassembly.org/getting-started/developers-guide) for details. -Start by cloning this repo (the `--recursive` option also grabs the MRuby submodule and initializes it): +Start by installing this gem: ``` -git clone --recursive https://github.com/blacktm/ruby-wasm.git +gem install wasm ``` -`cd` into the directory. Notice there's a file called `hello.rb` — we're going to build it for WebAssembly! - Make sure you have this `wasm` gem installed. Remember, you can check for issues using `ruby-wasm doctor` -Now, we'll build (or compile) the "Hello Ruby!" app using: +Then create a test dir for some example Ruby code: + +``` +mkdir ruby-wasm-test +cd ruby-wasm-test +echo 'puts "#{MRUBY_DESCRIPTION}"' > hello.rb +``` + +If you've installed using `gem`, you won't have a working mruby library, so run: + +``` +ruby-wasm vendor_mruby +ruby-wasm build_mruby +``` + +This will create a dir called 'mruby' with the emscripten-compatible build_config.rb copied over. You can edit this to ie. include more mrb-gems in your build. + +Now we'll finally compile that Ruby file for the browser: ``` ruby-wasm build hello.rb diff --git a/bin/ruby-wasm b/bin/ruby-wasm index 58bfc74..9ed6b5c 100755 --- a/bin/ruby-wasm +++ b/bin/ruby-wasm @@ -39,15 +39,17 @@ def build(rb_file) print "\nCompiling #{rb_file}..." # Create MRuby bytecode from Ruby source file - `mrbc -Bruby_app -obuild/app.c #{rb_file}` + cmd("#{mrbc} -Bruby_app -obuild/app.c #{rb_file}") # Add MRuby init code to app bytecode open('build/app.c', 'a') do |f| f << "\n\n" << File.read("#{@gem_dir}/assets/mruby_init.c") end + include_dir = @gem_dir + '/assets/mruby/include' + libmruby = first_extant_file( "./mruby/build/emscripten/lib/libmruby.a" , ENV["LIBMRUBY"] , @gem_dir + '/assets/mruby/libmruby.a' ) # Compile using Emscripten - `emcc -s WASM=1 #{ if @optimize then '-Os' end } -I #{@gem_dir + '/assets/mruby/include'} build/app.c #{@gem_dir + '/assets/mruby/libmruby.a'} -o build/app.js #{ if @optimize then '--closure 1' end }` + cmd("emcc -s WASM=1 #{ if @optimize then '-Os' end } -I #{include_dir} build/app.c #{libmruby} -o build/app.js #{ if @optimize then '--closure 1' end }") # Copy HTML template from gem assets to build directory FileUtils.cp "#{@gem_dir}/assets/template.html", 'build/app.html' @@ -147,6 +149,26 @@ def doctor(mode = nil) errors end +def vendor_mruby + if File.exist?("#{@gem_dir}/mruby/README.md") + Dir.mkdir('mruby') + FileUtils.cp_r "#{@gem_dir}/mruby", "." + FileUtils.cp "#{@gem_dir}/build_config.rb", "./mruby/build_config.rb" + else + puts "Gem was checked out without submodules, cannot vendor mruby" + puts "Try these commands yourself:" + puts " git clone https://github.com/mruby/mruby" + puts " cp #{@gem_dir}/build_config.rb ./mruby/build_config.rb" + exit 1 + end +end + +def compile_mruby + Dir.chdir('mruby') do + system 'rake' + end +end + # Check Command-line Arguments ################################################# usage = 'Ruby on WebAssembly'.bold + "\n @@ -159,6 +181,8 @@ Summary of commands and options: serve Serve the build WebAssembly binary -o|--open Open the default web browser after serving doctor Check for problems with your WebAssembly toolchain + vendor_mruby Copy mruby source code into ./mruby + compile_mruby Compile mruby from ./mruby -v|--version Prints the installed version\n\n" case ARGV[0] @@ -174,6 +198,10 @@ when 'serve' end when 'doctor' doctor +when 'vendor_mruby' + vendor_mruby +when 'compile_mruby' + compile_mruby when '-v', '--version' puts WASM::VERSION else From b35fece147cf3f50c1dc6ceff6e54d0b8e91733b Mon Sep 17 00:00:00 2001 From: Luke Mcildoon Date: Fri, 19 Oct 2018 12:12:18 +1100 Subject: [PATCH 2/3] fix build process and readme, permit using a vendored mruby per-project --- bin/ruby-wasm | 45 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 43 insertions(+), 2 deletions(-) diff --git a/bin/ruby-wasm b/bin/ruby-wasm index 9ed6b5c..502d1c9 100755 --- a/bin/ruby-wasm +++ b/bin/ruby-wasm @@ -9,6 +9,28 @@ require 'wasm/version' # Compilation optimizations flag @optimize = false +# Extending `String` to include some fancy colors +class String + def colorize(c); "\e[#{c}m#{self}\e[0m" end + def bold; colorize('1') end + def success; colorize('1;32') end + def error; colorize('1;31') end +end + +def first_extant_file(*files) + files.each do |file| + if File.exist?( File.expand_path(file || "") ) + return file + end + end + return files[-1] +end + +def cmd(str) + # puts str + `#{str}` +end + # Build a Ruby file def build(rb_file) @@ -36,8 +58,7 @@ def build(rb_file) # Create the build directory FileUtils.mkdir_p 'build' - print "\nCompiling #{rb_file}..." - + mrbc = first_extant_file( "./mruby/build/host/bin/mrbc" , ENV["MRBC"] , "mrbc") # Create MRuby bytecode from Ruby source file cmd("#{mrbc} -Bruby_app -obuild/app.c #{rb_file}") @@ -169,6 +190,26 @@ def compile_mruby end end +def vendor_mruby + if File.exist?("#{@gem_dir}/mruby/README.md") + Dir.mkdir('mruby') + FileUtils.cp_r "#{@gem_dir}/mruby", "." + FileUtils.cp "#{@gem_dir}/build_config.rb", "./mruby/build_config.rb" + else + puts "Gem was checked out without submodules, cannot vendor mruby" + puts "Try these commands yourself:" + puts " git clone https://github.com/mruby/mruby" + puts " cp #{@gem_dir}/build_config.rb ./mruby/build_config.rb" + exit 1 + end +end + +def compile_mruby + Dir.chdir('mruby') do + system 'rake' + end +end + # Check Command-line Arguments ################################################# usage = 'Ruby on WebAssembly'.bold + "\n From 043be511c290ce16afc15a8bd4ab6d571bf257de Mon Sep 17 00:00:00 2001 From: Tom Black Date: Wed, 24 Oct 2018 17:38:28 -0700 Subject: [PATCH 3/3] Rebase with master branch, resolve conflicts --- bin/ruby-wasm | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/bin/ruby-wasm b/bin/ruby-wasm index 502d1c9..a287e93 100755 --- a/bin/ruby-wasm +++ b/bin/ruby-wasm @@ -9,14 +9,6 @@ require 'wasm/version' # Compilation optimizations flag @optimize = false -# Extending `String` to include some fancy colors -class String - def colorize(c); "\e[#{c}m#{self}\e[0m" end - def bold; colorize('1') end - def success; colorize('1;32') end - def error; colorize('1;31') end -end - def first_extant_file(*files) files.each do |file| if File.exist?( File.expand_path(file || "") ) @@ -58,6 +50,8 @@ def build(rb_file) # Create the build directory FileUtils.mkdir_p 'build' + print "\nCompiling #{rb_file}..." + mrbc = first_extant_file( "./mruby/build/host/bin/mrbc" , ENV["MRBC"] , "mrbc") # Create MRuby bytecode from Ruby source file cmd("#{mrbc} -Bruby_app -obuild/app.c #{rb_file}")