cpp.js 2.0.0-beta.2 → 2.0.0-beta.4

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.
@@ -2,12 +2,12 @@ import getData from '../actions/getData.js';
2
2
  import loadJson from '../utils/loadJson.js';
3
3
  import state from '../state/index.js';
4
4
 
5
- export default function getCppJsScript(platform, bridgePath = null) {
6
- if (!platform || !state.platforms.All.includes(platform)) {
7
- throw new Error('The platform is not available!');
5
+ export default function getCppJsScript(target, bridgePath = null) {
6
+ if (!target) {
7
+ throw new Error('The target is not available!');
8
8
  }
9
- const env = JSON.stringify(getData('env', platform));
10
- const getPlatformScript = state.platforms.WebAssembly.includes(platform) ? getWebScript : getReactNativeScript;
9
+ const env = JSON.stringify(getData('env', target));
10
+ const getPlatformScript = target.platform === 'wasm' ? getWebScript : getReactNativeScript;
11
11
 
12
12
  const bridgeExportFile = `${bridgePath}.exports.json`;
13
13
  let symbols = null;
@@ -1,7 +1,7 @@
1
1
  import fs from 'node:fs';
2
2
  import state from '../state/index.js';
3
3
 
4
- export default function getDependFilePath(source, platform) {
4
+ export default function getDependFilePath(source, target) {
5
5
  const headerRegex = new RegExp(`\\.(${state.config.ext.header.join('|')})$`);
6
6
  const moduleRegex = new RegExp(`\\.(${state.config.ext.module.join('|')})$`);
7
7
 
@@ -13,11 +13,11 @@ export default function getDependFilePath(source, platform) {
13
13
 
14
14
  let path;
15
15
  if (headerRegex.test(source)) {
16
- path = `${dependPackage.paths.output}/prebuilt/${platform}/include`;
16
+ path = `${dependPackage.paths.output}/prebuilt/${target.path}/include`;
17
17
  } else if (moduleRegex.test(source)) {
18
- path = `${dependPackage.paths.output}/prebuilt/${platform}/swig`;
18
+ path = `${dependPackage.paths.output}/prebuilt/${target.path}/swig`;
19
19
  } else {
20
- path = `${dependPackage.paths.output}/prebuilt/${platform}`;
20
+ path = `${dependPackage.paths.output}/prebuilt/${target.path}`;
21
21
  }
22
22
 
23
23
  if (fs.existsSync(`${path}/${configName}/${filePath}`)) {
@@ -27,23 +27,32 @@ export default function calculateDependencyParameters(config) {
27
27
  setPath(cmakeDepends, config, 'this', cmakeFilter);
28
28
  cmakeDepends = [...new Set(cmakeDepends)];
29
29
 
30
- const pathsOfCmakeDepends = [];
31
- const nameOfCmakeDepends = [];
32
- cmakeDepends.forEach((d) => {
33
- const dependPath = getParentPath(d.paths.cmake);
34
- if (!pathsOfCmakeDepends.includes(dependPath)) {
35
- pathsOfCmakeDepends.push(dependPath);
36
- nameOfCmakeDepends.push(d.general.name);
37
- }
38
- });
30
+ const getCmakeDepends = (target, variants = []) => {
31
+ return cmakeDepends.filter(d => d.functions.isEnabled(target, variants));
32
+ };
33
+
34
+ const getCmakeDependsPathAndName = (target, variants = []) => {
35
+ const pathsOfCmakeDepends = [];
36
+ const nameOfCmakeDepends = [];
37
+ getCmakeDepends(target, variants).forEach((d) => {
38
+ const dependPath = d.paths.cmakeDir;
39
+ if (!pathsOfCmakeDepends.includes(dependPath)) {
40
+ pathsOfCmakeDepends.push(dependPath);
41
+ nameOfCmakeDepends.push(d.general.name);
42
+ }
43
+ });
44
+ return {
45
+ pathsOfCmakeDepends: pathsOfCmakeDepends,
46
+ nameOfCmakeDepends: nameOfCmakeDepends,
47
+ };
48
+ }
39
49
 
40
50
  return {
41
51
  nativeGlob,
42
52
  headerGlob,
43
53
  headerPathWithDepends,
44
- cmakeDepends,
45
- pathsOfCmakeDepends: pathsOfCmakeDepends.join(';'),
46
- nameOfCmakeDepends: nameOfCmakeDepends.join(';'),
54
+ getCmakeDepends,
55
+ getCmakeDependsPathAndName,
47
56
  };
48
57
  }
49
58
 
@@ -62,9 +71,3 @@ function setPath(arr, dependency, type, filter = () => { }) {
62
71
  setPath(arr, dep, type, filter);
63
72
  });
64
73
  }
65
-
66
- function getParentPath(path) {
67
- const pathArray = path.split('/');
68
- pathArray.pop();
69
- return pathArray.join('/');
70
- }
@@ -5,12 +5,168 @@ import loadConfig from './loadConfig.js';
5
5
  const cacheDir = `${process.cwd()}/.cppjs`;
6
6
 
7
7
  const state = {
8
- platforms: {
9
- All: ['Emscripten-x86_64', 'Android-arm64-v8a', 'Android-x86_64', 'iOS-iphoneos', 'iOS-iphonesimulator'],
10
- WebAssembly: ['Emscripten-x86_64'],
11
- Android: ['Android-arm64-v8a', 'Android-x86_64'],
12
- iOS: ['iOS-iphoneos', 'iOS-iphonesimulator'],
13
- },
8
+ targets: [
9
+ {
10
+ platform: 'wasm',
11
+ arch: 'wasm32',
12
+ runtime: 'st',
13
+ buildType: 'release',
14
+ runtimeEnv: 'browser',
15
+ },
16
+ {
17
+ platform: 'wasm',
18
+ arch: 'wasm32',
19
+ runtime: 'st',
20
+ buildType: 'release',
21
+ runtimeEnv: 'node',
22
+ },
23
+ {
24
+ platform: 'wasm',
25
+ arch: 'wasm32',
26
+ runtime: 'st',
27
+ buildType: 'debug',
28
+ runtimeEnv: 'browser',
29
+ },
30
+ {
31
+ platform: 'wasm',
32
+ arch: 'wasm32',
33
+ runtime: 'st',
34
+ buildType: 'debug',
35
+ runtimeEnv: 'node',
36
+ },
37
+ {
38
+ platform: 'wasm',
39
+ arch: 'wasm32',
40
+ runtime: 'mt',
41
+ buildType: 'release',
42
+ runtimeEnv: 'browser',
43
+ },
44
+ {
45
+ platform: 'wasm',
46
+ arch: 'wasm32',
47
+ runtime: 'mt',
48
+ buildType: 'release',
49
+ runtimeEnv: 'node',
50
+ },
51
+ {
52
+ platform: 'wasm',
53
+ arch: 'wasm32',
54
+ runtime: 'mt',
55
+ buildType: 'debug',
56
+ runtimeEnv: 'browser',
57
+ },
58
+ {
59
+ platform: 'wasm',
60
+ arch: 'wasm32',
61
+ runtime: 'mt',
62
+ buildType: 'debug',
63
+ runtimeEnv: 'node',
64
+ },
65
+ {
66
+ platform: 'wasm',
67
+ arch: 'wasm64',
68
+ runtime: 'st',
69
+ buildType: 'release',
70
+ runtimeEnv: 'browser',
71
+ },
72
+ {
73
+ platform: 'wasm',
74
+ arch: 'wasm64',
75
+ runtime: 'st',
76
+ buildType: 'release',
77
+ runtimeEnv: 'node',
78
+ },
79
+ {
80
+ platform: 'wasm',
81
+ arch: 'wasm64',
82
+ runtime: 'st',
83
+ buildType: 'debug',
84
+ runtimeEnv: 'browser',
85
+ },
86
+ {
87
+ platform: 'wasm',
88
+ arch: 'wasm64',
89
+ runtime: 'st',
90
+ buildType: 'debug',
91
+ runtimeEnv: 'node',
92
+ },
93
+ {
94
+ platform: 'wasm',
95
+ arch: 'wasm64',
96
+ runtime: 'mt',
97
+ buildType: 'release',
98
+ runtimeEnv: 'browser',
99
+ },
100
+ {
101
+ platform: 'wasm',
102
+ arch: 'wasm64',
103
+ runtime: 'mt',
104
+ buildType: 'release',
105
+ runtimeEnv: 'node',
106
+ },
107
+ {
108
+ platform: 'wasm',
109
+ arch: 'wasm64',
110
+ runtime: 'mt',
111
+ buildType: 'debug',
112
+ runtimeEnv: 'browser',
113
+ },
114
+ {
115
+ platform: 'wasm',
116
+ arch: 'wasm64',
117
+ runtime: 'mt',
118
+ buildType: 'debug',
119
+ runtimeEnv: 'node',
120
+ },
121
+ {
122
+ platform: 'android',
123
+ arch: 'arm64-v8a',
124
+ runtime: 'mt',
125
+ buildType: 'release',
126
+ },
127
+ {
128
+ platform: 'android',
129
+ arch: 'arm64-v8a',
130
+ runtime: 'mt',
131
+ buildType: 'debug',
132
+ },
133
+ {
134
+ platform: 'android',
135
+ arch: 'x86_64',
136
+ runtime: 'mt',
137
+ buildType: 'release',
138
+ },
139
+ {
140
+ platform: 'android',
141
+ arch: 'x86_64',
142
+ runtime: 'mt',
143
+ buildType: 'debug',
144
+ },
145
+ {
146
+ platform: 'ios',
147
+ arch: 'iphoneos',
148
+ runtime: 'mt',
149
+ buildType: 'release',
150
+ },
151
+ {
152
+ platform: 'ios',
153
+ arch: 'iphoneos',
154
+ runtime: 'mt',
155
+ buildType: 'debug',
156
+ },
157
+ {
158
+ platform: 'ios',
159
+ arch: 'iphonesimulator',
160
+ runtime: 'mt',
161
+ buildType: 'release',
162
+ },
163
+ {
164
+ platform: 'ios',
165
+ arch: 'iphonesimulator',
166
+ runtime: 'mt',
167
+ buildType: 'debug',
168
+ },
169
+ ],
14
170
  config: null,
15
171
  cache: {
16
172
  hashes: {},
@@ -30,6 +186,19 @@ await initProcessState();
30
186
  async function initProcessState() {
31
187
  state.cache = loadCacheState();
32
188
  state.config = await loadConfig();
189
+
190
+ state.targets.forEach((target) => {
191
+ target.path = `${target.platform}-${target.arch}-${target.runtime}-${target.buildType}`;
192
+ target.releasePath = `${target.platform}-${target.arch}-${target.runtime}-release`;
193
+ if (target.runtimeEnv && target.platform === 'wasm') {
194
+ target.rawJsName = `${state.config.general.name}-${target.path}.js`;
195
+ target.jsName = `${state.config.general.name}-${target.path}.${target.runtimeEnv}.js`;
196
+ target.wasmName = `${state.config.general.name}-${target.path}.wasm`;
197
+ target.dataName = `${state.config.general.name}-${target.path}.data`;
198
+ target.dataTxtName = `${state.config.general.name}-${target.path}.data.txt`;
199
+ }
200
+ });
201
+
33
202
  setAllDependecyPaths();
34
203
  if (state.config.build?.setState) {
35
204
  state.config.build.setState(state);
@@ -43,23 +212,28 @@ function loadCacheState() {
43
212
 
44
213
  function setAllDependecyPaths() {
45
214
  state.config.allDependencyPaths = {};
46
- state.platforms.All.forEach((platform) => {
47
- const basePlatform = platform.split('-', 1)[0];
48
- state.config.allDependencyPaths[platform] = { cmake: {} };
215
+ state.targets.forEach((target) => {
216
+ state.config.allDependencyPaths[target.path] = { cmake: {} };
49
217
  state.config.allDependencies.forEach((d) => {
50
- state.config.allDependencyPaths[platform].cmake[d.general.name] = `${d.paths.output}/prebuilt`;
218
+ state.config.allDependencyPaths[target.path].cmake[d.general.name] = `${d.paths.output}/prebuilt`;
51
219
  d.export.libName.forEach((name) => {
52
- state.config.allDependencyPaths[platform][name] = {
53
- root: `${d.paths.output}/prebuilt/${platform}`,
220
+ state.config.allDependencyPaths[target.path][name] = {
221
+ root: `${d.paths.output}/prebuilt/${target.path}`,
54
222
  };
55
- const platformConfig = d.platform[platform] || {};
56
- const isDynamicLib = basePlatform === 'Android' && platformConfig.libType !== 'static';
57
- const dep = state.config.allDependencyPaths[platform][name];
58
- if (basePlatform === 'iOS') {
223
+ const entryArray = d?.targetSpecs?.filter(t => (
224
+ (!t.platform || t.platform === target.platform)
225
+ && (!t.arch || t.arch === target.arch)
226
+ && (!t.runtime || t.runtime === target.runtime)
227
+ && (!t.buildType || t.buildType === target.buildType)
228
+ )).map(t => t?.specs);
229
+ const platformConfig = Object.assign({}, ...entryArray);
230
+ const isDynamicLib = target.platform === 'android' && platformConfig.libType !== 'static';
231
+ const dep = state.config.allDependencyPaths[target.path][name];
232
+ if (target.platform === 'ios') {
59
233
  let xcRoot;
60
- if (platform === 'iOS-iphoneos') {
234
+ if (target.arch === 'iphoneos') {
61
235
  xcRoot = `${d.paths.project}/${name}.xcframework/ios-arm64_arm64e`;
62
- } else if (platform === 'iOS-iphonesimulator') {
236
+ } else if (target.arch === 'iphonesimulator') {
63
237
  xcRoot = `${d.paths.project}/${name}.xcframework/ios-arm64_x86_64-simulator`;
64
238
  }
65
239
  dep.header = `${xcRoot}/Headers`;
@@ -1,4 +1,5 @@
1
1
  import os from 'node:os';
2
+ import fs from 'node:fs';
2
3
  import systemKeys from '../utils/systemKeys.js';
3
4
  import loadJs from '../utils/loadJs.js';
4
5
  import loadJson from '../utils/loadJson.js';
@@ -38,10 +39,12 @@ function getFilledConfig(config, options = { isDepend: false }) {
38
39
  paths: config.paths || {},
39
40
  ext: config.ext || {},
40
41
  export: config.export || {},
41
- platform: config.platform || {},
42
+ targetSpecs: config.targetSpecs || [],
42
43
  build: config.build || {},
44
+ target: config.target || {},
43
45
  extensions: config.extensions || [],
44
46
  package: null,
47
+ functions: config.functions || {},
45
48
  };
46
49
 
47
50
  if (newConfig.paths.config && !newConfig.paths.project) {
@@ -88,14 +91,6 @@ function getFilledConfig(config, options = { isDepend: false }) {
88
91
  newConfig.export.libName = newConfig.export.libName || [newConfig.general.name];
89
92
  newConfig.export.binHeaders = newConfig.export.binHeaders || [];
90
93
 
91
- newConfig.platform['Emscripten-x86_64'] = newConfig.platform['Emscripten-x86_64'] || {};
92
- newConfig.platform['Emscripten-x86_64-browser'] = newConfig.platform['Emscripten-x86_64-browser'] || {};
93
- newConfig.platform['Emscripten-x86_64-node'] = newConfig.platform['Emscripten-x86_64-node'] || {};
94
- newConfig.platform['Android-arm64-v8a'] = newConfig.platform['Android-arm64-v8a'] || {};
95
- newConfig.platform['Android-x86_64'] = newConfig.platform['Android-x86_64'] || {};
96
- newConfig.platform['iOS-iphoneos'] = newConfig.platform['iOS-iphoneos'] || {};
97
- newConfig.platform['iOS-iphonesimulator'] = newConfig.platform['iOS-iphonesimulator'] || {};
98
-
99
94
  newConfig.allDependencies = (() => {
100
95
  const output = {};
101
96
  [...newConfig.dependencies, ...newConfig.dependencies.map((d) => d.allDependencies).flat()].forEach((d) => {
@@ -108,12 +103,18 @@ function getFilledConfig(config, options = { isDepend: false }) {
108
103
  e?.loadConfig?.after(newConfig);
109
104
  });
110
105
 
111
- newConfig.build.usePthread = newConfig.build.usePthread || false;
112
-
113
- if (!newConfig.build.usePthread) {
114
- newConfig.build.usePthread = newConfig.allDependencies.some((d) => d?.build?.usePthread);
106
+ if (newConfig.target.runtime !== 'mt' && newConfig.allDependencies.some((d) => d?.target?.runtime === 'mt')) {
107
+ newConfig.target.runtime = 'mt';
115
108
  }
116
109
 
110
+ newConfig.functions.isEnabled = newConfig.functions.isEnabled || ((target) => {
111
+ return (
112
+ fs.existsSync(`${newConfig.paths.cmakeDir}/${target.path}`)
113
+ || fs.existsSync(`${newConfig.paths.cmakeDir}/${target.releasePath}`)
114
+ || (target.platform === 'ios' && fs.existsSync(`${newConfig.paths.cmakeDir}/../../${newConfig.general.name}-${target.runtime}.xcframework`))
115
+ );
116
+ });
117
+
117
118
  newConfig.dependencyParameters = calculateDependencyParameters(newConfig);
118
119
  // newConfig.cmakeParameters = getCmakeParameters(newConfig);
119
120