Permalink
Browse files

Build as single JS file

  • Loading branch information...
1 parent f19c242 commit 49b88eda6ce62f2b355d9ce89bd98007565c3574 @mental committed Jun 1, 2014
Showing with 29 additions and 8 deletions.
  1. +1 −1 .gitignore
  2. +25 −6 Rakefile
  3. 0 src/{ → js}/exports.json
  4. +3 −1 js/retrograph.js → src/js/retrograph.tmpl.js
View
2 .gitignore
@@ -1,4 +1,4 @@
Makefile
*.o
*.so
-js/retrograph_core.js
+js/*.js
View
31 Rakefile
@@ -1,4 +1,5 @@
require 'rake'
+require 'tempfile'
require 'rubygems/package_task'
begin
require 'rake/extensiontask'
@@ -18,11 +19,13 @@ require 'rake/clean'
GEM_VERSION = '0.5.2'
-JS_LIB = "js/retrograph_core.js"
+JS_EXPORTS_FILE = 'src/js/exports.json'
+JS_TEMPLATE_FILE = 'src/js/retrograph.tmpl.js'
+JS_LIB = 'js/retrograph.js'
CLEAN.include("spec/**/*-output.bmp")
CLEAN.include("lib/**/*.so")
-CLEAN.include(JS_LIB)
+CLEAN.include("js/**/*.js")
CLOBBER.include("lib/1.8")
CLOBBER.include("lib/1.9")
@@ -70,10 +73,26 @@ elsif have_rake_compiler
task :default => [:clean, :compile]
end
-file JS_LIB => FileList['src/retrograph.c', 'src/retrograph.h'] do
- exports_file = File.absolute_path('src/exports.json')
- sh 'emcc', '-O2', '-o', JS_LIB, 'src/retrograph.c', '-s', 'EXPORTED_FUNCTIONS=@' + exports_file
+directory 'js'
+
+file JS_LIB => FileList['js', 'src/**/*.c', 'src/**/*.h', JS_EXPORTS_FILE, JS_TEMPLATE_FILE] do
+ exports_file = File.absolute_path(JS_EXPORTS_FILE)
+ c_sources = FileList['src/**/*.c'].to_a
+ compiled_js_file = Tempfile.new(['compiled_js', '.js'])
+ compiled_js_file.close
+ begin
+ sh *(['emcc', '-O2', '-o', compiled_js_file.path] + c_sources + ['-s', "EXPORTED_FUNCTIONS=@#{exports_file}"])
+ compiled_js = File.read(compiled_js_file.path)
+ template = File.read(JS_TEMPLATE_FILE)
+ lib_js = template.sub('{{{ SCRIPT_CODE }}}', compiled_js)
+ puts "*** creating #{JS_LIB}"
+ File.open(JS_LIB, 'w') do |stream|
+ stream.write(lib_js)
+ end
+ ensure
+ compiled_js_file.unlink
+ end
end
desc "Compile JavaScript version"
-task :js => [JS_LIB]
+task :compile_js => [JS_LIB]
View
0 src/exports.json → src/js/exports.json
File renamed without changes.
View
4 js/retrograph.js → src/js/retrograph.tmpl.js
@@ -1,4 +1,6 @@
var Retrograph = (function () {
+{{{ SCRIPT_CODE }}}
+
// We won't ever deal in anything but numbers, so we might as well
// use a shorthand for that single cwrap case
function cfunc(name, nArgs) {
@@ -29,7 +31,7 @@ var Retrograph = (function () {
var TRANSFER_BUFFER_SIZE = RETROGRAPH_BYTES_PER_PIXEL * RETROGRAPH_DISPLAY_WIDTH;
- var checkError(vdu) {
+ function checkError(vdu) {
switch (retrograph_get_error(vdu)) {
case RETROGRAPH_OK:
break;

0 comments on commit 49b88ed

Please sign in to comment.