Skip to content

Instantly share code, notes, and snippets.

@xxuejie
Created December 12, 2012 17:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xxuejie/4270033 to your computer and use it in GitHub Desktop.
Save xxuejie/4270033 to your computer and use it in GitHub Desktop.
wrapper for creating node-powered js "binaries" with emcc
#!/usr/bin/env ruby
# %% -*- ruby -*-
# This script adds a wrapper to emcc: when the value of -o option is an
# executable file without extensions(such as mrbc), we would first use
# emcc to generate a .js file, then adds a "#!/usr/bin/env node" line at
# the top, finally removes the extension and mark the file mode as
# executable to pretend creating a binary file.
WEBRUBY_ROOT = File.join(File.dirname(__FILE__), '..')
EMCC = File.join(WEBRUBY_ROOT, 'modules', 'emscripten', 'emcc')
def mark_file_executable(original_filename, js_filename)
File.open(original_filename, "w", 0755) do |f|
f.puts "#!/usr/bin/env node"
File.foreach(js_filename) do |l|
f.puts l
end
end
end
if (i = ARGV.index('-o')) &&
(i + 1 < ARGV.length) &&
(!ARGV[i + 1].split('/').last.include?('.'))
original_filename = ARGV[i + 1]
js_filename = original_filename + ".js"
ARGV[i + 1] = js_filename
ret = 255
if system(EMCC, *ARGV)
# normal execution
mark_file_executable(original_filename, js_filename)
ret = 0
end
File.delete(js_filename) if File.exists?(js_filename)
exit(ret)
else
exec(EMCC, *ARGV)
end
#!/usr/bin/env ruby
# -*- Ruby -*-
require 'fileutils'
EXECUTABLES = ['mrbc']
CURRENT_PATH = File.dirname(__FILE__)
WEBRUBY_ROOT = File.join(CURRENT_PATH, '..')
EMCC_ROOT = File.join(WEBRUBY_ROOT, 'modules', 'emscripten')
EMCC_MAP = {
:cc => File.join(EMCC_ROOT, 'emcc'),
:ll => File.join(EMCC_ROOT, 'emcc'),
:ar => File.join(EMCC_ROOT, 'emar')}
EXE = EMCC_MAP[ARGV.first.to_sym]
if !EXE
puts "Unknown program: ${ARGV.first}!"
exit 1
end
# Removes optimization arguments since this is not used in emcc
args = ARGV.drop(1).select { |i| (i != "-g") && (!i.start_with?("-O")) }
if (i = args.index('-o')) &&
(i + 1 < args.length) &&
(program_name = File.basename(args[i + 1])) &&
(EXECUTABLES.index(program_name))
original_file = args[i + 1]
args[i + 1] = original_file + ".js"
ret = 255
if system(EXE, *args)
# normal execution
FileUtils.cp(File.join(CURRENT_PATH, program_name),
original_file)
FileUtils.touch original_file
ret = 0
end
exit(ret)
else
exec(EXE, *args)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment