cpp.js 0.6.1 → 1.0.0-alpha.3
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.
- package/LICENSE +1 -1
- package/package.json +10 -3
- package/src/assets/CMakeLists.txt +26 -10
- package/src/assets/browser.js +161 -0
- package/src/assets/dist.cmake +36 -11
- package/src/assets/ios.toolchain.cmake +1145 -0
- package/src/assets/node.js +130 -0
- package/src/bin.js +102 -27
- package/src/functions/buildJS.js +63 -0
- package/src/functions/createBridge.js +16 -17
- package/src/functions/createLib.js +44 -0
- package/src/functions/createWasm.js +61 -192
- package/src/functions/findOrCreateInterfaceFile.js +29 -8
- package/src/functions/finishBuild.js +43 -0
- package/src/functions/getCmakeParams.js +106 -0
- package/src/functions/getData.js +37 -0
- package/src/functions/getLibs.js +33 -0
- package/src/functions/run.js +220 -0
- package/src/functions/specs/createBridge.spec.js +8 -9
- package/src/functions/specs/findOrCreateInterfaceFile.spec.js +12 -12
- package/src/index.js +39 -3
- package/src/utils/createTempDir.js +2 -2
- package/src/utils/findCMakeListsFile.js +3 -3
- package/src/utils/getCliPath.js +4 -5
- package/src/utils/getConfig.js +63 -32
- package/src/utils/getDirName.js +2 -2
- package/src/utils/getOsUserAndGroupId.js +1 -1
- package/src/utils/getPathInfo.js +0 -2
- package/src/utils/pullDockerImage.js +11 -6
- package/src/assets/extern-post.js +0 -14
package/LICENSE
CHANGED
package/package.json
CHANGED
|
@@ -1,15 +1,22 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cpp.js",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "1.0.0-alpha.3",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"type": "module",
|
|
6
|
+
"homepage": "https://cpp.js.org",
|
|
7
|
+
"repository": "https://github.com/bugra9/cpp.js.git",
|
|
6
8
|
"bin": {
|
|
7
9
|
"cpp.js": "./src/bin.js"
|
|
8
10
|
},
|
|
9
|
-
"main": "
|
|
11
|
+
"main": "src/index.js",
|
|
10
12
|
"dependencies": {
|
|
13
|
+
"@rollup/plugin-commonjs": "^26.0.1",
|
|
14
|
+
"@rollup/plugin-node-resolve": "^15.2.3",
|
|
15
|
+
"@rollup/plugin-virtual": "^3.0.2",
|
|
11
16
|
"commander": "^9.4.1",
|
|
12
|
-
"glob": "^8.0.3"
|
|
17
|
+
"glob": "^8.0.3",
|
|
18
|
+
"rollup": "^4.18.0",
|
|
19
|
+
"rollup-plugin-uglify": "^6.0.4"
|
|
13
20
|
},
|
|
14
21
|
"devDependencies": {
|
|
15
22
|
"mocha": "^10.2.0"
|
|
@@ -1,7 +1,18 @@
|
|
|
1
1
|
cmake_minimum_required(VERSION 3.25)
|
|
2
2
|
set(CMAKE_CXX_STANDARD 17)
|
|
3
3
|
project("${PROJECT_NAME}")
|
|
4
|
-
|
|
4
|
+
|
|
5
|
+
if (CMAKE_SYSTEM_NAME STREQUAL "iOS")
|
|
6
|
+
if (CMAKE_OSX_SYSROOT MATCHES "/iPhoneOS.platform/")
|
|
7
|
+
set(PACKAGE_HOST "iOS-iphoneos")
|
|
8
|
+
elseif(CMAKE_OSX_SYSROOT MATCHES "/iPhoneSimulator.platform/")
|
|
9
|
+
set(PACKAGE_HOST "iOS-iphonesimulator")
|
|
10
|
+
else()
|
|
11
|
+
set(PACKAGE_HOST "iOS-unknown")
|
|
12
|
+
endif()
|
|
13
|
+
else()
|
|
14
|
+
set(PACKAGE_HOST "${CMAKE_SYSTEM_NAME}-${CMAKE_HOST_SYSTEM_PROCESSOR}")
|
|
15
|
+
endif()
|
|
5
16
|
|
|
6
17
|
option(BUILD_BRIDGE "Build Bridge" OFF)
|
|
7
18
|
option(BUILD_SOURCE "Build Source" OFF)
|
|
@@ -21,22 +32,27 @@ endif(BUILD_BRIDGE)
|
|
|
21
32
|
set(SRC_FILES "${BUILD_SRC_FILES}" "${BRIDGE_SRC_FILES}")
|
|
22
33
|
add_library("${PROJECT_NAME}" "${BUILD_TYPE}" ${SRC_FILES})
|
|
23
34
|
|
|
35
|
+
set(EXTRA_LINK_LIBRARIES "")
|
|
36
|
+
if(ANDROID)
|
|
37
|
+
set(EXTRA_LINK_LIBRARIES "log")
|
|
38
|
+
endif()
|
|
39
|
+
|
|
24
40
|
if (NOT ${CMAKE_SYSTEM_NAME} STREQUAL "")
|
|
25
|
-
target_link_libraries("${PROJECT_NAME}" "${DEPENDS_CMAKE_NAMES}")
|
|
41
|
+
target_link_libraries("${PROJECT_NAME}" "${DEPENDS_CMAKE_NAMES}" "${EXTRA_LINK_LIBRARIES}")
|
|
26
42
|
endif()
|
|
27
43
|
|
|
28
44
|
file(GLOB_RECURSE HEADER_FILES ${HEADER_GLOB})
|
|
29
45
|
target_include_directories("${PROJECT_NAME}" PUBLIC "${HEADER_DIR}")
|
|
30
46
|
|
|
31
47
|
if (BUILD_SOURCE)
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
48
|
+
install(TARGETS "${PROJECT_NAME}" DESTINATION "lib")
|
|
49
|
+
if(NOT "${HEADER_FILES}" STREQUAL "")
|
|
50
|
+
target_sources("${PROJECT_NAME}"
|
|
51
|
+
PUBLIC FILE_SET HEADERS
|
|
52
|
+
BASE_DIRS "${HEADER_DIR}"
|
|
53
|
+
FILES "${HEADER_FILES}")
|
|
54
|
+
install(TARGETS "${PROJECT_NAME}" FILE_SET HEADERS DESTINATION "include")
|
|
55
|
+
endif()
|
|
40
56
|
endif(BUILD_SOURCE)
|
|
41
57
|
unset(BUILD_SOURCE CACHE)
|
|
42
58
|
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
/* eslint-disable import/no-unresolved */
|
|
2
|
+
/* eslint-disable import/first */
|
|
3
|
+
/* eslint-disable no-restricted-syntax */
|
|
4
|
+
/* eslint-disable no-undef */
|
|
5
|
+
import Module from 'cpp.js/module';
|
|
6
|
+
import systemConfig from 'cpp.js/systemConfig';
|
|
7
|
+
|
|
8
|
+
const VIRTUAL_PATH = '/virtual';
|
|
9
|
+
const AUTO_MOUNTED_PATH = `${VIRTUAL_PATH}/automounted`;
|
|
10
|
+
let cppJsPromise;
|
|
11
|
+
|
|
12
|
+
function isObject(item) {
|
|
13
|
+
return (item && typeof item === 'object' && !Array.isArray(item));
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function mergeDeep(target, ...sources) {
|
|
17
|
+
if (!sources.length) return target;
|
|
18
|
+
const source = sources.shift();
|
|
19
|
+
|
|
20
|
+
if (isObject(target) && isObject(source)) {
|
|
21
|
+
for (const key in source) {
|
|
22
|
+
if (isObject(source[key])) {
|
|
23
|
+
if (!target[key]) Object.assign(target, { [key]: {} });
|
|
24
|
+
mergeDeep(target[key], source[key]);
|
|
25
|
+
} else {
|
|
26
|
+
Object.assign(target, { [key]: source[key] });
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return mergeDeep(target, ...sources);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function initCppJs(userConfig = {}) {
|
|
35
|
+
if (cppJsPromise) return cppJsPromise;
|
|
36
|
+
|
|
37
|
+
const config = mergeDeep(systemConfig, userConfig);
|
|
38
|
+
|
|
39
|
+
cppJsPromise = new Promise((resolve, reject) => {
|
|
40
|
+
const m = {
|
|
41
|
+
print(text) {
|
|
42
|
+
if (config.logHandler) {
|
|
43
|
+
config.logHandler(text, 'stdout');
|
|
44
|
+
} else {
|
|
45
|
+
console.debug(`wasm stdout: ${text}`);
|
|
46
|
+
}
|
|
47
|
+
},
|
|
48
|
+
printErr(text) {
|
|
49
|
+
if (config.errorHandler) {
|
|
50
|
+
config.errorHandler(text, 'stderr');
|
|
51
|
+
} else {
|
|
52
|
+
console.error(`wasm stderr: ${text}`);
|
|
53
|
+
}
|
|
54
|
+
},
|
|
55
|
+
locateFile(fileName) {
|
|
56
|
+
let path = fileName;
|
|
57
|
+
if (config.paths && config.paths.wasm && fileName.endsWith('.wasm')) {
|
|
58
|
+
path = config.paths.wasm;
|
|
59
|
+
} else if (config.paths && config.paths.data && (fileName.endsWith('.data.txt') || fileName.endsWith('.data'))) {
|
|
60
|
+
path = config.paths.data;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
let prefix = '';
|
|
64
|
+
if (config.path) {
|
|
65
|
+
prefix = config.path;
|
|
66
|
+
if (prefix.slice(-1) !== '/') prefix += '/';
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
let output = prefix + path;
|
|
70
|
+
if (output.endsWith('.data')) output += '.txt';
|
|
71
|
+
if (output.substring(0, 4) !== 'http' && output[0] !== '/') output = `/${output}`;
|
|
72
|
+
return output;
|
|
73
|
+
},
|
|
74
|
+
|
|
75
|
+
preRun: [
|
|
76
|
+
({ ENV }) => {
|
|
77
|
+
if (ENV && config && config.env) {
|
|
78
|
+
Object.entries(config.env).forEach(([key, value]) => {
|
|
79
|
+
// eslint-disable-next-line no-param-reassign
|
|
80
|
+
ENV[key] = value;
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
},
|
|
84
|
+
],
|
|
85
|
+
onRuntimeInitialized() {
|
|
86
|
+
m.FS.mkdir(VIRTUAL_PATH);
|
|
87
|
+
m.FS.mkdir(AUTO_MOUNTED_PATH);
|
|
88
|
+
if (config.onRuntimeInitialized) config.onRuntimeInitialized(m);
|
|
89
|
+
},
|
|
90
|
+
generateVirtualPath() {
|
|
91
|
+
const rand = Math.floor(Math.random() * 1000000);
|
|
92
|
+
const path = `${AUTO_MOUNTED_PATH}/${rand}`;
|
|
93
|
+
m.FS.mkdir(path);
|
|
94
|
+
return path;
|
|
95
|
+
},
|
|
96
|
+
unmount() {
|
|
97
|
+
if (typeof importScripts === 'function') {
|
|
98
|
+
m.FS.unmount(VIRTUAL_PATH);
|
|
99
|
+
}
|
|
100
|
+
},
|
|
101
|
+
autoMountFiles(files) {
|
|
102
|
+
return new Promise((resolve2) => {
|
|
103
|
+
if (files.length === 0) {
|
|
104
|
+
resolve2([]);
|
|
105
|
+
} else if (typeof importScripts === 'function') {
|
|
106
|
+
const virtualPath = m.generateVirtualPath();
|
|
107
|
+
m.FS.mount(m.WORKERFS, { files }, virtualPath);
|
|
108
|
+
resolve2(files.map((f) => `${virtualPath}/${f.name}`));
|
|
109
|
+
} else {
|
|
110
|
+
const promises = [];
|
|
111
|
+
[...files].forEach((file) => {
|
|
112
|
+
promises.push(file.arrayBuffer());
|
|
113
|
+
});
|
|
114
|
+
Promise.all(promises).then((buffers) => {
|
|
115
|
+
const virtualPath = m.generateVirtualPath();
|
|
116
|
+
buffers.forEach((buffer, i) => {
|
|
117
|
+
const ss = new Uint8Array(buffer);
|
|
118
|
+
m.FS.writeFile(`${virtualPath}/${files[i].name}`, ss);
|
|
119
|
+
});
|
|
120
|
+
resolve2([...files].map((f) => `${virtualPath}/${f.name}`));
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
});
|
|
124
|
+
},
|
|
125
|
+
getFileBytes(path) {
|
|
126
|
+
if (!path) return new Uint8Array();
|
|
127
|
+
return m.FS.readFile(path, { encoding: 'binary' });
|
|
128
|
+
},
|
|
129
|
+
getFileList(path = 'virtual') {
|
|
130
|
+
const contents = path.split('/').reduce((accumulator, currentValue) => accumulator.contents[currentValue], m.FS.root).contents;
|
|
131
|
+
const fileList = [];
|
|
132
|
+
Object.keys(contents).forEach((name) => {
|
|
133
|
+
const obj = contents[name];
|
|
134
|
+
if (obj.usedBytes) fileList.push({ path: `/${path}/${name}`, size: obj.usedBytes });
|
|
135
|
+
else if (obj.contents) fileList.push(...m.getFileList(`${path}/${name}`));
|
|
136
|
+
});
|
|
137
|
+
return fileList;
|
|
138
|
+
},
|
|
139
|
+
|
|
140
|
+
toArray(vector) {
|
|
141
|
+
const output = [];
|
|
142
|
+
for (let i = 0; i < vector.size(); i += 1) {
|
|
143
|
+
output.push(vector.get(i));
|
|
144
|
+
}
|
|
145
|
+
return output;
|
|
146
|
+
},
|
|
147
|
+
toVector(VectorClass, array = []) {
|
|
148
|
+
const vector = new VectorClass();
|
|
149
|
+
array.forEach((item) => {
|
|
150
|
+
vector.push_back(item);
|
|
151
|
+
});
|
|
152
|
+
return vector;
|
|
153
|
+
},
|
|
154
|
+
};
|
|
155
|
+
Module(m).then(resolve).catch(reject);
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
return cppJsPromise;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
export default initCppJs;
|
package/src/assets/dist.cmake
CHANGED
|
@@ -1,19 +1,44 @@
|
|
|
1
|
-
cmake_minimum_required(VERSION 3.
|
|
1
|
+
cmake_minimum_required(VERSION 3.28)
|
|
2
2
|
set(CMAKE_CXX_STANDARD 11)
|
|
3
3
|
set(PROJECT_NAME "___PROJECT_NAME___")
|
|
4
|
+
set(PROJECT_LIBS "___PROJECT_LIBS___")
|
|
4
5
|
project("${PROJECT_NAME}")
|
|
5
6
|
|
|
6
|
-
|
|
7
|
+
if(ANDROID)
|
|
8
|
+
set(PACKAGE_HOST "${CMAKE_SYSTEM_NAME}-${CMAKE_ANDROID_ARCH_ABI}")
|
|
9
|
+
set(PACKAGE_DIR "${PROJECT_SOURCE_DIR}/${PACKAGE_HOST}/lib")
|
|
10
|
+
elseif(APPLE)
|
|
11
|
+
if (CMAKE_SYSTEM_NAME STREQUAL "iOS")
|
|
12
|
+
set(PACKAGE_DIR "${PROJECT_SOURCE_DIR}")
|
|
13
|
+
else()
|
|
14
|
+
set(PACKAGE_HOST "${CMAKE_SYSTEM_NAME}-${CMAKE_HOST_SYSTEM_PROCESSOR}")
|
|
15
|
+
set(PACKAGE_DIR "${PROJECT_SOURCE_DIR}/${PACKAGE_HOST}/lib")
|
|
16
|
+
endif()
|
|
17
|
+
elseif(UNIX)
|
|
18
|
+
set(PACKAGE_HOST "${CMAKE_SYSTEM_NAME}-${CMAKE_HOST_SYSTEM_PROCESSOR}")
|
|
19
|
+
set(PACKAGE_DIR "${PROJECT_SOURCE_DIR}/${PACKAGE_HOST}/lib")
|
|
20
|
+
else()
|
|
21
|
+
set(PACKAGE_HOST "${CMAKE_SYSTEM_NAME}-${CMAKE_HOST_SYSTEM_PROCESSOR}")
|
|
22
|
+
set(PACKAGE_DIR "${PROJECT_SOURCE_DIR}/${PACKAGE_HOST}/lib")
|
|
23
|
+
endif()
|
|
7
24
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
25
|
+
set(PROJECT_LIBS_DIR)
|
|
26
|
+
foreach(L IN LISTS PROJECT_LIBS)
|
|
27
|
+
SET(FOUND_LIB "FOUND_LIB-NOTFOUND")
|
|
28
|
+
find_library(FOUND_LIB
|
|
29
|
+
NAMES "${L}"
|
|
30
|
+
PATHS "${PACKAGE_DIR}"
|
|
31
|
+
NO_CACHE
|
|
32
|
+
NO_DEFAULT_PATH
|
|
33
|
+
NO_CMAKE_FIND_ROOT_PATH
|
|
34
|
+
REQUIRED
|
|
35
|
+
)
|
|
36
|
+
LIST(APPEND PROJECT_LIBS_DIR ${FOUND_LIB})
|
|
37
|
+
endforeach()
|
|
16
38
|
|
|
17
39
|
add_library("${PROJECT_NAME}" INTERFACE)
|
|
18
|
-
target_link_libraries("${PROJECT_NAME}" INTERFACE "${
|
|
40
|
+
target_link_libraries("${PROJECT_NAME}" INTERFACE "${PROJECT_LIBS_DIR}")
|
|
41
|
+
|
|
42
|
+
if(NOT APPLE)
|
|
19
43
|
target_include_directories("${PROJECT_NAME}" INTERFACE "${PROJECT_SOURCE_DIR}/${PACKAGE_HOST}/include")
|
|
44
|
+
endif()
|