|
| 1 | +#!/usr/bin/env bash |
| 2 | +# |
| 3 | +# Script for compiling WebAssembly for a provided absolute package path. |
| 4 | +# |
| 5 | +# Usage: compile_wasm <pkg> |
| 6 | +# |
| 7 | +# Arguments: |
| 8 | +# |
| 9 | +# pkg Package directory (absolute path). |
| 10 | +# |
| 11 | + |
| 12 | + |
| 13 | +# VARIABLES # |
| 14 | + |
| 15 | +# Set the package path: |
| 16 | +pkg_path="$1" |
| 17 | + |
| 18 | +# Define the command for running Node.js: |
| 19 | +node_cmd="${NODE}" |
| 20 | +if [[ -z "${node_cmd}" ]]; then |
| 21 | + node_cmd='node' |
| 22 | +fi |
| 23 | + |
| 24 | +# Define the node path: |
| 25 | +node_path="${NODE_PATH}" |
| 26 | + |
| 27 | +# Define the path to the Emscripten `emcc` compiler: |
| 28 | +emcc_compiler="${EMCC_COMPILER}" |
| 29 | +if [[ -z "${emcc_compiler}" ]]; then |
| 30 | + emcc_compiler='emcc' |
| 31 | +fi |
| 32 | + |
| 33 | +# Define the path to an executable for converting a WebAssembly binary to the WebAssembly text format: |
| 34 | +wasm_to_wat="${WASM2WAT}" |
| 35 | +if [[ -z "${wasm_to_wat}" ]]; then |
| 36 | + wasm_to_wat='wasm2wat' |
| 37 | +fi |
| 38 | + |
| 39 | +# Define the folder containing source files: |
| 40 | +src_folder="${SRC_FOLDER}" |
| 41 | +if [[ -z "${src_folder}" ]]; then |
| 42 | + src_folder='src' |
| 43 | +fi |
| 44 | + |
| 45 | +# Define a list of external `include` directories: |
| 46 | +include="${INCLUDE}" |
| 47 | + |
| 48 | +# Define a list of external source files: |
| 49 | +source_files="${SOURCE_FILES}" |
| 50 | + |
| 51 | + |
| 52 | +# FUNCTIONS # |
| 53 | + |
| 54 | +# Defines an error handler. |
| 55 | +# |
| 56 | +# $1 - error status |
| 57 | +on_error() { |
| 58 | + echo 'ERROR: An error was encountered during execution.' >&2 |
| 59 | + cleanup |
| 60 | + exit "$1" |
| 61 | +} |
| 62 | + |
| 63 | +# Runs clean-up tasks. |
| 64 | +cleanup() { |
| 65 | + echo '' >&2 |
| 66 | +} |
| 67 | + |
| 68 | +# Prints a success message. |
| 69 | +print_success() { |
| 70 | + echo 'Success!' >&2 |
| 71 | +} |
| 72 | + |
| 73 | +# Prints usage information. |
| 74 | +usage() { |
| 75 | + echo '' >&2 |
| 76 | + echo 'Usage: compile_wasm <pkg>' >&2 |
| 77 | + echo '' >&2 |
| 78 | +} |
| 79 | + |
| 80 | +# Resolves external `include` directories. |
| 81 | +# |
| 82 | +# $1 - package directory |
| 83 | +resolve_includes() { |
| 84 | + local includes |
| 85 | + local script |
| 86 | + |
| 87 | + # Generate the script for resolving external include directories: |
| 88 | + script='"'"var path = require('path'); var arr = require('@stdlib/tools/library-manifest')(path.join('$1','manifest.json'),{},{'basedir':'$1','paths':'posix'}).include; var str = ''; for (var i = 0; i < arr.length; i++){var p = path.resolve('$1', arr[i]); if (p.indexOf('$1') === 0) {continue;}; str += '-I '+p+' ';}; console.log(str.substring(0, str.length-1));"'"' |
| 89 | + |
| 90 | + # Resolve include directories: |
| 91 | + includes=$(eval NODE_PATH="${node_path}" "${node_cmd}" -e "${script}") |
| 92 | + |
| 93 | + echo "${includes}" |
| 94 | +} |
| 95 | + |
| 96 | +# Resolves external source files. |
| 97 | +# |
| 98 | +# $1 - package directory |
| 99 | +resolve_source_files() { |
| 100 | + local source_files |
| 101 | + local script |
| 102 | + |
| 103 | + # Generate the script for resolving external source files: |
| 104 | + script='"'"var path = require('path'); var arr = require('@stdlib/tools/library-manifest')(path.join('$1','manifest.json'),{},{'basedir':'$1','paths':'posix'}).src; var str = ''; for (var i = 0; i < arr.length; i++){var p = path.resolve('$1', arr[i]); if (p.indexOf('$1') === 0) {continue;}; str += p+' ';}; console.log(str.substring(0, str.length-1));"'"' |
| 105 | + |
| 106 | + # Resolve files: |
| 107 | + source_files=$(eval NODE_PATH="${node_path}" "${node_cmd}" -e "${script}") |
| 108 | + |
| 109 | + echo "${source_files}" |
| 110 | +} |
| 111 | + |
| 112 | +# Compiles WebAssembly. |
| 113 | +# |
| 114 | +# $1 - source directory |
| 115 | +compile() { |
| 116 | + cd "$1" && EMCC_COMPILER="${emcc_compiler}" WASM2WAT="${wasm_to_wat}" INCLUDE="${include}" SOURCE_FILES="${source_files}" make wasm 2>&1 |
| 117 | + if [[ "$?" -ne 0 ]]; then |
| 118 | + echo 'Error when attempting to compile WebAssembly.' >&2 |
| 119 | + return 1 |
| 120 | + fi |
| 121 | + echo 'Successfully compiled WebAssembly.' >&2 |
| 122 | + return 0 |
| 123 | +} |
| 124 | + |
| 125 | + |
| 126 | +# MAIN # |
| 127 | + |
| 128 | +# Main execution sequence. |
| 129 | +main() { |
| 130 | + local src_dir="${pkg_path}/${src_folder}" |
| 131 | + |
| 132 | + if [[ -z "${include}" ]]; then |
| 133 | + echo 'Resolving external include directories...' >&2 |
| 134 | + include=$(resolve_includes "${pkg_path}") |
| 135 | + if [[ "$?" -ne 0 ]]; then |
| 136 | + on_error 1 |
| 137 | + fi |
| 138 | + fi |
| 139 | + if [[ -z "${source_files}" ]]; then |
| 140 | + echo 'Resolving external source files...' >&2 |
| 141 | + source_files=$(resolve_source_files "${pkg_path}") |
| 142 | + if [[ "$?" -ne 0 ]]; then |
| 143 | + on_error 1 |
| 144 | + fi |
| 145 | + fi |
| 146 | + echo 'Compiling WebAssembly...' >&2 |
| 147 | + compile "${src_dir}" |
| 148 | + if [[ "$?" -ne 0 ]]; then |
| 149 | + on_error 1 |
| 150 | + fi |
| 151 | + print_success |
| 152 | + cleanup |
| 153 | + exit 0 |
| 154 | +} |
| 155 | + |
| 156 | +# Handle arguments... |
| 157 | +if [[ "$#" -eq 0 ]]; then |
| 158 | + usage |
| 159 | + exit 0 |
| 160 | +elif [[ "$#" -gt 1 ]]; then |
| 161 | + echo 'ERROR: unrecognized arguments. Must provide a package path.' >&2 |
| 162 | + exit 1 |
| 163 | +fi |
| 164 | + |
| 165 | +# Run main: |
| 166 | +main |
0 commit comments