cpp.js 0.1.1 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5,5 +5,4 @@ file(GLOB SRC_FILES "${BASE_DIR}/native/**/*.cpp" "${BASE_DIR}/native/*.cpp" "${
5
5
  add_library(cppjs STATIC ${SRC_FILES})
6
6
 
7
7
  file(GLOB HEADERS "${BASE_DIR}/node_modules/cppjs-lib-*/include")
8
- message("... ${SRC_FILES} . ${HEADERS};${BASE_DIR}/native")
9
8
  include_directories("${HEADERS};${BASE_DIR}/native;${BASE_DIR}/src/native")
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cpp.js",
3
- "version": "0.1.1",
3
+ "version": "0.2.0",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "bin": {
@@ -1,18 +1,28 @@
1
1
  import glob from 'glob';
2
+ import os from "os";
2
3
  import { execFileSync } from 'child_process';
3
4
  import pullDockerImage from './pullDockerImage.js';
5
+ import { getBaseInfo, getPathInfo, getOsUserAndGroupId } from './utils.js';
4
6
 
5
- export default function createBridge(filePath, outputPath) {
7
+ export default function createBridge(filePath, outputPath, basePath = process.cwd()) {
6
8
  pullDockerImage();
9
+
10
+ const input = getPathInfo(filePath, basePath);
11
+ const output = getPathInfo(outputPath, basePath);
12
+ const projectPath = getPathInfo(process.cwd(), basePath);
13
+ const base = getBaseInfo(basePath);
14
+
7
15
  const includePath = [
8
- glob.sync("node_modules/cppjs-lib-*-web/include", { absolute: true })
9
- ].map(path => `-I${path}`);
16
+ glob.sync("node_modules/cppjs-lib-*-web/include", { absolute: false }),
17
+ glob.sync("native", { absolute: false }),
18
+ glob.sync("src/native", { absolute: false }),
19
+ ].filter(path => !!path.toString()).map(path => `-I/live/${projectPath.relative}/${path}`);
10
20
 
11
21
  const options = { cwd: outputPath, stdio : 'pipe' };
12
22
  const args = [
13
- "run", "-v", `${outputPath}:/output`, "-v", "/:/live", "bugra9/cpp.js",
14
- "swig", "-c++", '-emscripten', '-o', `/output/${filePath.split('/').at(-1)}.cpp`, ...includePath, `/live${filePath}`
23
+ "run", "--user", getOsUserAndGroupId(), "-v", `${base.withoutSlash}:/live`, "bugra9/cpp.js",
24
+ "swig", "-c++", '-emscripten', '-o', `/live/${output.relative}/${filePath.split('/').at(-1)}.cpp`, ...includePath, `/live/${input.relative}`
15
25
  ];
16
26
  execFileSync("docker", args, options);
17
- return `${outputPath}/${filePath.split('/').at(-1)}.cpp`;
27
+ return `${base.withSlash}${output.relative}/${filePath.split('/').at(-1)}.cpp`;
18
28
  }
package/src/createWasm.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import { execFileSync } from 'child_process';
2
2
  import * as url from 'node:url';
3
3
  import pullDockerImage from './pullDockerImage.js';
4
+ import { getBaseInfo, getPathInfo, getOsUserAndGroupId } from './utils.js';
4
5
 
5
6
  const __filename = url.fileURLToPath(import.meta.url);
6
7
  const temp = __filename.split('/');
@@ -8,32 +9,42 @@ temp.pop();
8
9
  temp.pop();
9
10
  const __dirname = temp.join('/');
10
11
 
11
- export default function createWasm(cMakeFilePath, outputPath, tempPath, options = {}) {
12
+ export default function createWasm(cMakeFilePath, outputPath, tempPath, options = {}, basePath = process.cwd()) {
12
13
  pullDockerImage();
13
- cmake(cMakeFilePath, tempPath);
14
- make(tempPath);
15
- cc(tempPath, outputPath, options.cc);
14
+ cmake(cMakeFilePath, tempPath, basePath);
15
+ make(cMakeFilePath, tempPath, basePath);
16
+ cc(tempPath, outputPath, options.cc, basePath);
16
17
 
17
18
  return outputPath;
18
19
  }
19
20
 
20
- function cmake(cMakeFilePath, outputPath) {
21
+ function cmake(cMakeFilePath, outputPath, basePath) {
22
+ const output = getPathInfo(outputPath, basePath);
23
+ const projectPath = getPathInfo(process.cwd(), basePath);
24
+ const base = getBaseInfo(basePath);
25
+
21
26
  let cMakeParentPath = cMakeFilePath.split('/');
22
27
  cMakeParentPath.pop();
23
28
  cMakeParentPath = cMakeParentPath.join('/');
24
-
25
29
  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',
30
+ "run", "--user", getOsUserAndGroupId(), "-v", `${base.withoutSlash}:/live`, "-v", `${cMakeParentPath}:/cmake`, "--workdir", `/live/${output.relative}`, "bugra9/cpp.js",
31
+ "emcmake", "cmake", "/cmake", `-DBASE_DIR=/live/${projectPath.relative}`,
32
+ `-DCMAKE_INSTALL_PREFIX=/live/${output.relative}`, `-DBRIDGE_DIR=/live/${output.relative}`,
28
33
  ];
29
34
  const options = { cwd: outputPath, stdio : 'pipe' };
30
35
  execFileSync("docker", args, options);
31
36
  return outputPath;
32
37
  }
33
38
 
34
- function make(outputPath) {
39
+ function make(cMakeFilePath, outputPath, basePath) {
40
+ const output = getPathInfo(outputPath, basePath);
41
+ const base = getBaseInfo(basePath);
42
+
43
+ let cMakeParentPath = cMakeFilePath.split('/');
44
+ cMakeParentPath.pop();
45
+ cMakeParentPath = cMakeParentPath.join('/');
35
46
  const args = [
36
- "run", "-v", `${outputPath}:/output`, "-v", "/:/live", "--workdir", "/output", "bugra9/cpp.js",
47
+ "run", "--user", getOsUserAndGroupId(), "-v", `${base.withoutSlash}:/live`, "-v", `${cMakeParentPath}:/cmake`, "--workdir", `/live/${output.relative}`, "bugra9/cpp.js",
37
48
  "emmake", "make"
38
49
  ];
39
50
  const options = { cwd: outputPath, stdio : 'pipe' };
@@ -41,10 +52,13 @@ function make(outputPath) {
41
52
  return outputPath;
42
53
  }
43
54
 
44
- function cc(tempPath, outputPath, flags = []) {
55
+ function cc(tempPath, outputPath, flags = [], basePath) {
56
+ const input = getPathInfo(tempPath, basePath);
57
+ const output = getPathInfo(outputPath, basePath);
58
+ const base = getBaseInfo(basePath);
45
59
  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'
60
+ "run", "--user", getOsUserAndGroupId(), "-v", `${base.withoutSlash}:/live`, "-v", `${__dirname}:/cli`, "bugra9/cpp.js",
61
+ "emcc", "-lembind", "-Wl,--whole-archive", `/live/${input.relative}/libcppjs.a`, ...flags, "-s", "WASM=1", "-s", "MODULARIZE=1", '-o', `/live/${output.relative}/cpp.js`, '--extern-post-js', '/cli/src/extern-post.js'
48
62
  ];
49
63
  const options = { cwd: tempPath, stdio : 'pipe' };
50
64
  execFileSync("docker", args, options);
@@ -1,7 +1,12 @@
1
1
  import fs from 'fs';
2
+ import { getBaseInfo, getPathInfo } from './utils.js';
2
3
 
3
- export default function findOrCreateInterfaceFile(filePath, outputPath) {
4
- const temp = filePath.match(/^(.*)\..+?$/);
4
+ export default function findOrCreateInterfaceFile(filePath, outputPath, basePath = process.cwd()) {
5
+ const input = getPathInfo(filePath, basePath);
6
+ const output = getPathInfo(outputPath, basePath);
7
+ const base = getBaseInfo(basePath);
8
+
9
+ const temp = input.relative.match(/^(.*)\..+?$/);
5
10
  if (temp.length < 2) return null;
6
11
 
7
12
  const filePathWithoutExt = temp[1];
@@ -10,7 +15,7 @@ export default function findOrCreateInterfaceFile(filePath, outputPath) {
10
15
  if (fs.existsSync(interfaceFile)) return interfaceFile;
11
16
 
12
17
  const fileName = filePathWithoutExt.split('/').at(-1);
13
- const fileNameWithExt = filePath.split('/').at(-1);
18
+ const fileNameWithExt = input.relative.split('/').at(-1);
14
19
 
15
20
  const content =
16
21
  `#ifndef _${fileName.toUpperCase()}_I
@@ -22,12 +27,11 @@ export default function findOrCreateInterfaceFile(filePath, outputPath) {
22
27
  #include "${fileNameWithExt}"
23
28
  %}
24
29
 
25
- %include "/live${filePath}"
30
+ %include "${fileNameWithExt}"
26
31
 
27
32
  #endif
28
33
  `;
29
-
30
- const outputFilePath = outputPath+'/'+fileName+'.i';
34
+ const outputFilePath = base.withSlash + output.relative+'/'+fileName+'.i';
31
35
  fs.writeFileSync(outputFilePath, content);
32
36
  return outputFilePath;
33
37
  }
package/src/utils.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import fs from 'fs';
2
2
  import p, {dirname} from 'path';
3
- import { tmpdir } from "os";
3
+ import os, { tmpdir } from "os";
4
4
  import * as url from 'node:url';
5
5
 
6
6
  export const __filename = url.fileURLToPath(import.meta.url);
@@ -54,3 +54,48 @@ export async function getConfig(projectPath) {
54
54
 
55
55
  return config;
56
56
  }
57
+
58
+ let osUserAndGroupId;
59
+ export function getOsUserAndGroupId() {
60
+ const userInfo = os.userInfo();
61
+ if (!osUserAndGroupId) {
62
+ osUserAndGroupId = `${userInfo.uid}:${userInfo.gid}`;
63
+ }
64
+ return osUserAndGroupId;
65
+ }
66
+
67
+ export function getBaseInfo(base) {
68
+ let basePath = base;
69
+
70
+ const output = {
71
+ withSlash: '/',
72
+ withoutSlash: '/',
73
+ };
74
+ if (basePath && basePath !== '/') {
75
+ if (basePath.at(-1) !== '/') basePath += '/';
76
+
77
+ output.withSlash = basePath;
78
+ output.withoutSlash = basePath.substring(0, basePath.length - 1);
79
+ }
80
+ return output;
81
+ }
82
+
83
+
84
+ export function getPathInfo(path, base) {
85
+ let basePath = base;
86
+
87
+ const output = {
88
+ relative: path,
89
+ absolute: path,
90
+ };
91
+ if (basePath) {
92
+ if (basePath.at(-1) !== '/') basePath += '/';
93
+
94
+ if (path.substring(0, basePath.length) === basePath) {
95
+ output.relative = path.substring(basePath.length);
96
+ } else {
97
+ output.absolute = basePath + path;
98
+ }
99
+ }
100
+ return output;
101
+ }
@@ -0,0 +1,81 @@
1
+ import { assert } from 'chai';
2
+ import { getBaseInfo, getPathInfo } from './utils.js';
3
+
4
+ describe('getBaseInfo', function () {
5
+ it('empty', async function () {
6
+ const baseInfo = getBaseInfo('');
7
+ assert.equal(baseInfo.withSlash, '/');
8
+ assert.equal(baseInfo.withoutSlash, '/');
9
+ });
10
+
11
+ it('null', async function () {
12
+ const baseInfo = getBaseInfo(null);
13
+ assert.equal(baseInfo.withSlash, '/');
14
+ assert.equal(baseInfo.withoutSlash, '/');
15
+ });
16
+
17
+ it('undefined', async function () {
18
+ const baseInfo = getBaseInfo();
19
+ assert.equal(baseInfo.withSlash, '/');
20
+ assert.equal(baseInfo.withoutSlash, '/');
21
+ });
22
+
23
+ it('/', async function () {
24
+ const baseInfo = getBaseInfo('/');
25
+ assert.equal(baseInfo.withSlash, '/');
26
+ assert.equal(baseInfo.withoutSlash, '/');
27
+ });
28
+
29
+ it('with /', async function () {
30
+ const baseInfo = getBaseInfo('/home/cppjs/');
31
+ assert.equal(baseInfo.withSlash, '/home/cppjs/');
32
+ assert.equal(baseInfo.withoutSlash, '/home/cppjs');
33
+ });
34
+
35
+ it('without /', async function () {
36
+ const baseInfo = getBaseInfo('/home/cppjs');
37
+ assert.equal(baseInfo.withSlash, '/home/cppjs/');
38
+ assert.equal(baseInfo.withoutSlash, '/home/cppjs');
39
+ });
40
+ });
41
+
42
+ describe('getPathInfo', function () {
43
+ it('path: absolute', async function () {
44
+ const path = '/home/cppjs/cppjs.h';
45
+ const pathInfo = getPathInfo(path);
46
+ assert.equal(pathInfo.relative, path);
47
+ assert.equal(pathInfo.absolute, path);
48
+ });
49
+
50
+ it('path: absolute, base: /', async function () {
51
+ const basePath = '/home/cppjs/';
52
+ const path = '/home/cppjs/cppjs.h';
53
+ const pathInfo = getPathInfo(path, basePath);
54
+ assert.equal(pathInfo.relative, 'cppjs.h');
55
+ assert.equal(pathInfo.absolute, path);
56
+ });
57
+
58
+ it('path: absolute, base', async function () {
59
+ const basePath = '/home/cppjs';
60
+ const path = '/home/cppjs/cppjs.h';
61
+ const pathInfo = getPathInfo(path, basePath);
62
+ assert.equal(pathInfo.relative, 'cppjs.h');
63
+ assert.equal(pathInfo.absolute, path);
64
+ });
65
+
66
+ it('path: relative, base: /', async function () {
67
+ const basePath = '/home/cppjs/';
68
+ const path = 'cppjs.h';
69
+ const pathInfo = getPathInfo(path, basePath);
70
+ assert.equal(pathInfo.relative, 'cppjs.h');
71
+ assert.equal(pathInfo.absolute, basePath + path);
72
+ });
73
+
74
+ it('path: relative, base: ', async function () {
75
+ const basePath = '/home/cppjs';
76
+ const path = 'cppjs.h';
77
+ const pathInfo = getPathInfo(path, basePath);
78
+ assert.equal(pathInfo.relative, 'cppjs.h');
79
+ assert.equal(pathInfo.absolute, basePath + '/' + path);
80
+ });
81
+ });