|
| 1 | +var child_process = require('child_process'); |
| 2 | +var util = require('util'); |
| 3 | + |
| 4 | +module.exports = function(grunt) { |
| 5 | + 'use strict'; |
| 6 | + |
| 7 | + grunt.initConfig({ |
| 8 | + pkg: grunt.file.readJSON('package.json'), |
| 9 | + compile: { |
| 10 | + curve25519: { |
| 11 | + src_files: [ |
| 12 | + 'native/ed25519/additions/*.c', |
| 13 | + 'native/curve25519-donna.c', |
| 14 | + 'native/ed25519/*.c', |
| 15 | + 'native/ed25519/sha512/sha2big.c' |
| 16 | + ], |
| 17 | + methods: [ |
| 18 | + 'curve25519_donna', |
| 19 | + 'curve25519_sign', |
| 20 | + 'curve25519_verify', |
| 21 | + 'crypto_sign_ed25519_ref10_ge_scalarmult_base', |
| 22 | + 'sph_sha512_init', |
| 23 | + 'malloc' |
| 24 | + ] |
| 25 | + } |
| 26 | + } |
| 27 | + }); |
| 28 | + |
| 29 | + grunt.registerMultiTask('compile', 'Compile the C libraries with emscripten.', function() { |
| 30 | + var callback = this.async(); |
| 31 | + var outfile = 'build/' + this.target + '.js'; |
| 32 | + |
| 33 | + var exported_functions = this.data.methods.map(function(name) { |
| 34 | + return "'_" + name + "'"; |
| 35 | + }); |
| 36 | + var flags = [ |
| 37 | + '-O3', |
| 38 | + '--memory-init-file 0', |
| 39 | + '-Qunused-arguments', |
| 40 | + '-s INLINING_LIMIT=1', |
| 41 | + '-s WASM=0', |
| 42 | + '--minify 0', |
| 43 | + '-o', outfile, |
| 44 | + '-Inative/ed25519/nacl_includes -Inative/ed25519 -Inative/ed25519/sha512', |
| 45 | + '-s', "EXPORTED_FUNCTIONS=\"[" + exported_functions.join(',') + "]\""]; |
| 46 | + var command = [].concat('emcc', this.data.src_files, flags).join(' '); |
| 47 | + grunt.log.writeln('Compiling via emscripten to ' + outfile); |
| 48 | + |
| 49 | + var exitCode = 0; |
| 50 | + grunt.verbose.subhead(command); |
| 51 | + grunt.verbose.writeln(util.format('Expecting exit code %d', exitCode)); |
| 52 | + |
| 53 | + var child = child_process.exec(command); |
| 54 | + child.stdout.on('data', function (d) { grunt.log.write(d); }); |
| 55 | + child.stderr.on('data', function (d) { grunt.log.error(d); }); |
| 56 | + child.on('exit', function(code) { |
| 57 | + if (code !== exitCode) { |
| 58 | + grunt.log.error(util.format('Exited with code: %d.', code)); |
| 59 | + return callback(false); |
| 60 | + } |
| 61 | + |
| 62 | + grunt.verbose.ok(util.format('Exited with code: %d.', code)); |
| 63 | + callback(true); |
| 64 | + }); |
| 65 | + }); |
| 66 | + |
| 67 | + grunt.registerTask('default', ['compile']); |
| 68 | + grunt.registerTask('build', ['compile']); |
| 69 | +}; |
0 commit comments