Description: use binaries from Debian packages
Author: Yadd <yadd@debian.org>
Forwarded: not-needed
Last-Update: 2023-09-14

--- a/inwasm/src/cli.ts
+++ b/inwasm/src/cli.ts
@@ -7,7 +7,10 @@
 import { IWasmDefinition, CompilerRunner, _IWasmCtx, OutputMode, OutputType } from '.';
 
 import * as chokidar from 'chokidar';
-import { globSync, hasMagic } from 'glob';
+//import { globSync, hasMagic } from 'glob';
+import glob from 'glob';
+const globSync = glob.sync;
+const hasMagic = glob.hasMagic;
 
 import * as acorn from 'acorn';
 import * as walk from 'acorn-walk';
@@ -649,7 +652,7 @@
     return 1;
   }
   // minimal globbing support to work around window shell limitations
-  const files = args.length === 1 && hasMagic(args, { magicalBraces: true })
+  const files = args.length === 1 && hasMagic(args[0])
     ? globSync(args[0])
     : args;
   const startTime = Date.now();
--- a/inwasm/src/config.ts
+++ b/inwasm/src/config.ts
@@ -93,7 +93,7 @@
   let folder = process.env.PWD || process.cwd();
   let found = '';
   while (folder !== path.dirname(folder)) {
-    if (fs.existsSync(path.join(folder, 'package.json')) && fs.existsSync(path.join(folder, 'node_modules'))) {
+    if (fs.existsSync(path.join(folder, 'package.json'))) {
       found = folder;
       break;
     };
@@ -184,18 +184,10 @@
 }
 export const CONFIG = getConfig();
 
-
-function getWabtPath(): string {
-  const inApp = path.join(APP_ROOT, 'node_modules', 'wabt', 'bin');
-  if (fs.existsSync(inApp)) {
-    return inApp;
-  }
-  return path.join(PROJECT_ROOT, 'node_modules', 'wabt', 'bin');
-}
-export const WABT_PATH = getWabtPath();
+export const WABT_PATH = '/usr/bin';
 
 // shell to be executed
-export const SHELL = process.platform === 'win32' ? 'cmd.exe' : execSync('which bash', {encoding: 'utf-8'}).trim();
+export const SHELL = '/bin/bash';
 
 // simply assume any OS != windows being POSIX compatible
 export const isPosix = process.platform !== 'win32';
@@ -214,7 +206,7 @@
 
 // wabt tool path abstraction
 function getWabtTool(): IWabtToolPath {
-  const p = (name: string) => `"${process.execPath}" "${path.join(getWabtPath(), name)}"`;
+  const p = (name: string) => `/usr/bin/${name}`;
   return {
     'wasm2c': p('wasm2c'),
     'wasm-decompile': p('wasm-decompile'),
--- a/inwasm/src/runners/clang_c.ts
+++ b/inwasm/src/runners/clang_c.ts
@@ -1,7 +1,8 @@
 import * as fs from 'fs';
 import * as path from 'path';
-import { emscriptenRun, getClangBinPath } from '../sdks/emscripten';
+//import { emscriptenRun, getClangBinPath } from '../sdks/emscripten';
 import { IMemorySettings, IWasmDefinition } from '..';
+import { spawnSync } from 'child_process';
 
 
 /**
@@ -31,7 +32,7 @@
   // TODO: apply compile options properly
   const opt = `-O3`;
   const defines = Object.entries(def.compile?.defines || {})
-    .map(el => `-D${el[0]}=${el[1]}`).join(' ');
+    .map(el => `-D${el[0]}=${el[1]}`);
   let switches: string[] = [];
 
   // memory settings
@@ -56,8 +57,9 @@
     .filter(el => typeof el[1] === 'function' || el[1] instanceof WebAssembly.Global)
     .map(el => `--export=${el[0]}`)
     .join(',');
-  const clang = path.join(getClangBinPath(), 'clang');
-  const call = `${clang} --target=wasm32-unknown-unknown --no-standard-libraries -Wl,${ff} -Wl,--no-entry -Wl,--lto-O3 ${opt} ${switches.join(' ')} -flto ${defines} -o ${target} ${src}`;
-  emscriptenRun(call);
+  const clang = '/usr/bin/clang';
+  console.log(`${clang} --target=wasm32-unknown-unknown --no-standard-libraries -Wl,${ff} -Wl,--no-entry -Wl,--lto-O3 ${opt} ${switches.join(' ')} -flto ${defines} -o ${target} ${src}`);
+  const res = spawnSync( clang, ['--target=wasm32-unknown-unknown', '--no-standard-libraries', `-Wl,${ff}`, '-Wl,--no-entry', '-Wl,--lto-O3', opt, ...switches, '-flto', ...defines, '-o', target, src] );
+  if (res.stderr) console.log(res.stderr.toString());
   return fs.readFileSync(target);
 }
--- a/inwasm/src/runners/emscripten_c.ts
+++ b/inwasm/src/runners/emscripten_c.ts
@@ -1,9 +1,8 @@
 import * as fs from 'fs';
 import * as path from 'path';
-import { emscriptenRun, getEmscriptenPath } from '../sdks/emscripten';
+//import { emscriptenRun, getEmscriptenPath } from '../sdks/emscripten';
 import { IMemorySettings, IWasmDefinition } from '..';
-import { isPosix } from '../config';
-
+import { spawnSync } from 'child_process';
 
 export default function(def: IWasmDefinition, buildDir: string, filename: string, memorySettings: IMemorySettings): Uint8Array {
   // TODO: copy additional files
@@ -14,7 +13,7 @@
   // TODO: apply compile options properly
   const opt = `-O3`;
   const defines = Object.entries(def.compile?.defines || {})
-    .map(el => `-D${el[0]}=${el[1]}`).join(' ');
+    .map(el => `-D${el[0]}=${el[1]}`);
   const _funcs = Object.entries(def.exports)
     .filter(el => typeof el[1] === 'function')
     .map(el => `_${el[0]}`)
@@ -45,10 +44,10 @@
   // FIXME:
   switches.push(...['-s ERROR_ON_UNDEFINED_SYMBOLS=0', '-s WARN_ON_UNDEFINED_SYMBOLS=0']);
 
-  const funcs = `-s EXPORTED_FUNCTIONS=${_funcs}`;
-  // FIXME: for unknown reason windows shell cannot find emcc, thus give path explicitly
-  const bin = isPosix ? 'emcc' : path.join(getEmscriptenPath(), 'upstream', 'emscripten', 'emcc.bat');
-  const call = `${bin} ${opt} ${defines} ${funcs} ${switches.join(' ')} --no-entry ${src} -o ${target}`;
-  emscriptenRun(call);
+  const funcs = ['-s', `EXPORTED_FUNCTIONS=${_funcs}`];
+  const bin = '/usr/bin/emcc';
+  console.log(`${bin} ${opt} ${defines} ${funcs.join(' ')} ${switches.join(' ')} --no-entry ${src} -o ${target}`);
+  const res = spawnSync( bin, [ opt, ...defines, ...funcs, ...switches, '--no-entry', src, '-o', target ] );
+  if (res.stderr) console.log(res.stderr.toString());
   return fs.readFileSync(target);
 }
--- a/inwasm/tsconfig.json
+++ b/inwasm/tsconfig.json
@@ -5,13 +5,15 @@
     "rootDir": "src",
     "outDir": "lib",
     "sourceMap": true,
+    "esModuleInterop": true,
     "declaration": true,
     "noImplicitAny": true,
     "strict": true,
     "removeComments": false
   },
   "exclude": [
+    "**/*.test-d.ts",
     "node_modules",
     "lib"
   ]
-}
\ No newline at end of file
+}
--- a/testproject/custom/build.sh
+++ b/testproject/custom/build.sh
@@ -1,8 +1,4 @@
 #!/usr/bin/env bash
 
 # some custom build steps (just calling wat2wasm for simplicity)
-if [ -d "../node_modules/inwasm/node_modules/wabt/bin/" ]; then
-  ../node_modules/inwasm/node_modules/wabt/bin/wat2wasm module.wat
-else
-  ../node_modules/wabt/bin/wat2wasm module.wat
-fi
+wat2wasm module.wat
