|
| 1 | +import { execFileSync } from 'child_process'; |
| 2 | +import * as url from 'node:url'; |
| 3 | +import pullDockerImage from './pullDockerImage.js'; |
| 4 | + |
| 5 | +const __filename = url.fileURLToPath(import.meta.url); |
| 6 | +const temp = __filename.split('/'); |
| 7 | +temp.pop(); |
| 8 | +temp.pop(); |
| 9 | +const __dirname = temp.join('/'); |
| 10 | + |
| 11 | +export default function createWasm(cMakeFilePath, outputPath, tempPath, options = {}) { |
| 12 | + pullDockerImage(); |
| 13 | + cmake(cMakeFilePath, tempPath); |
| 14 | + make(tempPath); |
| 15 | + cc(tempPath, outputPath, options.cc); |
| 16 | + |
| 17 | + return outputPath; |
| 18 | +} |
| 19 | + |
| 20 | +function cmake(cMakeFilePath, outputPath) { |
| 21 | + let cMakeParentPath = cMakeFilePath.split('/'); |
| 22 | + cMakeParentPath.pop(); |
| 23 | + cMakeParentPath = cMakeParentPath.join('/'); |
| 24 | + |
| 25 | + const args = [ |
| 26 | + "run", "-v", `${outputPath}:/output`, "-v", "/:/live", "--workdir", "/output", "bugra9/cpp.js", |
| 27 | + "emcmake", "cmake", "/live"+cMakeParentPath, "-DCMAKE_INSTALL_PREFIX=/output", `-DBASE_DIR=/live${process.cwd()}`, '-DBRIDGE_DIR=/output', |
| 28 | + ]; |
| 29 | + const options = { cwd: outputPath, stdio : 'pipe' }; |
| 30 | + execFileSync("docker", args, options); |
| 31 | + return outputPath; |
| 32 | +} |
| 33 | + |
| 34 | +function make(outputPath) { |
| 35 | + const args = [ |
| 36 | + "run", "-v", `${outputPath}:/output`, "-v", "/:/live", "--workdir", "/output", "bugra9/cpp.js", |
| 37 | + "emmake", "make" |
| 38 | + ]; |
| 39 | + const options = { cwd: outputPath, stdio : 'pipe' }; |
| 40 | + execFileSync("docker", args, options); |
| 41 | + return outputPath; |
| 42 | +} |
| 43 | + |
| 44 | +function cc(tempPath, outputPath, flags = []) { |
| 45 | + const args = [ |
| 46 | + "run", "-v", `${tempPath}:/tempPath`, "-v", `${outputPath}:/output`, "-v", `${__dirname}:/cli`, "-v", "/:/live", "bugra9/cpp.js", |
| 47 | + "emcc", "-lembind", "-Wl,--whole-archive", '/tempPath/libcppjs.a', ...flags, "-s", "WASM=1", "-s", "MODULARIZE=1", '-o', '/output/cpp.js', '--extern-post-js', '/cli/src/extern-post.js' |
| 48 | + ]; |
| 49 | + const options = { cwd: tempPath, stdio : 'pipe' }; |
| 50 | + execFileSync("docker", args, options); |
| 51 | + return outputPath; |
| 52 | +} |
0 commit comments