require 'json' EMCC_FLAGS = ENV["EMCC_FLAGS"] || "-O0 -g4 -D_ASSERT_USED -D_ERROR_PRINT --memory-init-file 0 -s TOTAL_MEMORY=67108864 -s WASM=0" if ENV["DEBUG"] EMCC_FLAGS = ENV["EMCC_FLAGS"] || "-O3 -D_ERROR_PRINT --memory-init-file 0 -s TOTAL_MEMORY=67108864 -s WASM=0" unless ENV["DEBUG"] c_files = FileList["../src/*.c"] UGLIFY_JS = ENV["UGLIFY_JS"] || "terser" exported_functions = [ "_malloc", "_free", "_memcpy", "_h264bsdAlloc", "_h264bsdFree", "_h264bsdInit", "_h264bsdDecode", "_h264bsdShutdown", "_h264bsdNextOutputPicture", "_h264bsdNextOutputPictureRGBA", "_h264bsdPicWidth", "_h264bsdPicHeight", "_h264bsdCroppingParams", ] exported_runtime_methods = [ 'getValue', 'setValue' ] EXPORT_FLAGS = "-s MODULARIZE=1 -s EXPORT_NAME=H264bsd -s EXPORTED_RUNTIME_METHODS='#{JSON.generate(exported_runtime_methods)}' -s EXPORTED_FUNCTIONS='#{JSON.generate(exported_functions)}'" file "h264bsd_asm.js" do sh "emcc #{c_files.join(' ')} #{EMCC_FLAGS} #{EXPORT_FLAGS} -o h264bsd_asm.js" end directory "min" js_files = Rake::FileList.new("*.js").exclude("h264bsd_asm.js") js_files.each do |fl| file "min/#{fl}" => [fl, "min"] do sh "#{UGLIFY_JS} #{fl} -c -m -o min/#{fl}" end end file "min/h264bsd_asm.js" => ["h264bsd_asm.js", "min"] do cp "h264bsd_asm.js", "min/h264bsd_asm.js" end task :clean do FileUtils.rm_f("h264bsd_asm.js") FileUtils.rm_rf("min") end desc "Check for prereq tools" task :setup do sh("emcc --version") { |ok, res| fail("Can't find emscripten binaries.") unless ok } puts("Ready to go") end desc "Check for compress prereq tools" task :setup_compress do sh("#{UGLIFY_JS} --version") { |ok, res| fail("Can't find tool for minification. Please install either UglifyJS or Terser.") unless ok } puts("Compress ready to go") end task :server do require 'webrick' WEBrick::HTTPServer.new(:Port => 8000, :DocumentRoot => Dir.pwd).start end task :compress => [:build, :setup_compress, "min/h264bsd_asm.js", "min/h264bsd_canvas.js", "min/h264bsd_decoder.js", "min/h264bsd_worker.js"] task :build => [:setup, "h264bsd_asm.js"] task :test do sh "node test_node.js" end task :default => [:build]