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

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cpp.js",
3
- "version": "2.0.0-beta.4",
3
+ "version": "2.0.0-beta.6",
4
4
  "license": "MIT",
5
5
  "homepage": "https://cpp.js.org",
6
6
  "repository": "https://github.com/bugra9/cpp.js.git",
@@ -26,7 +26,7 @@ export default function createXCFramework(overrideConfig = null) {
26
26
  const relativeOutput = upath.relative(projectPath, output);
27
27
 
28
28
  const targetParams = overrideConfig?.targetParams || getTargetParams();
29
- const buildTargets = getFilteredBuildTargets(targetParams, { platform: 'ios', runtime: 'mt', buildType: (targetParams.buildType && targetParams.buildType !== 'all') ? targetParams.buildType : 'release' });
29
+ const buildTargets = getFilteredBuildTargets(targetParams, { platform: 'ios', runtime: 'mt', buildType: (targetParams.buildType && targetParams.buildType.length > 0) ? targetParams.buildType[0] : 'release' });
30
30
 
31
31
  if (buildTargets.some(t => !fs.existsSync(`${output}/prebuilt/${t.path}/lib`))) {
32
32
  console.log('XCFramework not created because some of the build targets are not built.');
@@ -1,14 +1,26 @@
1
-
2
-
1
+ import fs from 'node:fs';
3
2
  import state from '../state/index.js';
4
- import { getFilteredTargetSpec } from './target.js';
3
+ import { getFilteredTargetSpec, getBuildTargets } from './target.js';
5
4
 
6
5
  function getRecursiveData(obj, config, dependency, field, target) {
7
6
  const entryArray = getFilteredTargetSpec(dependency?.targetSpecs, target).map(s => s[field]);
8
7
  const entries = Object.assign({}, ...entryArray);
9
8
  Object.entries(entries).forEach(([dKey, value]) => {
10
9
  if (field === 'data') {
11
- const key = `${dependency.paths.project}/dist/prebuilt/${target.path}/${dKey}`;
10
+ let key;
11
+ if (fs.existsSync(`${dependency.paths.project}/dist/prebuilt/${target.path}`)) {
12
+ key = `${dependency.paths.project}/dist/prebuilt/${target.path}/${dKey}`;
13
+ } else {
14
+ const releaseTarget = getBuildTargets({
15
+ platform: [target.platform], arch: [target.arch], runtime: [target.runtime],
16
+ runtimeEnv: [target.runtimeEnv], buildType: ['release']
17
+ })?.[0];
18
+ if (releaseTarget) {
19
+ key = `${dependency.paths.project}/dist/prebuilt/${releaseTarget.path}/${dKey}`;
20
+ } else {
21
+ throw new Error('Data not found');
22
+ }
23
+ }
12
24
  obj[key] = value;
13
25
  } else {
14
26
  if (typeof value === 'object' && Array.isArray(value)) {
@@ -8,7 +8,7 @@ export default function getDependLibs(target) {
8
8
  ];
9
9
  state.config.dependencyParameters.getCmakeDepends(target).forEach((d) => {
10
10
  if (d.export.libName) {
11
- const ignoreLibNames = getFilteredTargetSpec(d?.targetSpecs).map(s => s.ignoreLibName).flat();
11
+ const ignoreLibNames = getFilteredTargetSpec(d?.targetSpecs, target).map(s => s.ignoreLibName).flat();
12
12
  d.export.libName.forEach((fileName) => {
13
13
  if (ignoreLibNames?.includes(fileName)) return;
14
14
  let libPaths = findFiles(`${d.paths.output}/prebuilt/${target.path}/lib/lib${fileName}.a`, { cwd: d.paths.project });
@@ -1,33 +1,39 @@
1
1
  import state from '../state/index.js';
2
2
 
3
+ const platforms = [...new Set(state.targets.map(target => target.platform).filter(t => t))];
4
+ const archs = [...new Set(state.targets.map(target => target.arch).filter(t => t))];
5
+ const runtimes = [...new Set(state.targets.map(target => target.runtime).filter(t => t))];
6
+ const buildTypes = [...new Set(state.targets.map(target => target.buildType).filter(t => t))];
7
+ const runtimeEnvs = [...new Set(state.targets.map(target => target.runtimeEnv).filter(t => t))];
8
+
3
9
  export function getTargetParams(givenTargetParams = {}, noParamCheck = false) {
4
10
  const { platform, arch, runtime, buildType, runtimeEnv } = givenTargetParams;
5
11
  if (!noParamCheck && (
6
- (state.config.target.platform && platform && platform !== 'all' && state.config.target.platform !== platform)
7
- || (state.config.target.arch && arch && arch !== 'all' && state.config.target.arch !== arch)
8
- || (state.config.target.runtime && runtime && runtime !== 'all' && state.config.target.runtime !== runtime)
9
- || (state.config.target.buildType && buildType && buildType !== 'all' && state.config.target.buildType !== buildType)
10
- || (state.config.target.runtimeEnv && runtimeEnv && runtimeEnv !== 'all' && state.config.target.runtimeEnv !== runtimeEnv)
12
+ (state.config.target.platform && platform && !platform.includes(state.config.target.platform))
13
+ || (state.config.target.arch && arch && !arch.includes(state.config.target.arch))
14
+ || (state.config.target.runtime && runtime && !runtime.includes(state.config.target.runtime))
15
+ || (state.config.target.buildType && buildType && !buildType.includes(state.config.target.buildType))
16
+ || (state.config.target.runtimeEnv && runtimeEnv && !runtimeEnv.includes(state.config.target.runtimeEnv))
11
17
  )) {
12
18
  throw new Error('Invalid target parameters');
13
19
  }
14
20
  return {
15
- platform: state.config.target.platform || platform || 'all',
16
- arch: state.config.target.arch || arch || 'all',
17
- runtime: state.config.target.runtime || runtime || 'all',
18
- buildType: state.config.target.buildType || buildType || 'all',
19
- runtimeEnv: state.config.target.runtimeEnv || runtimeEnv || 'all',
21
+ platform: state.config.target.platform ? [state.config.target.platform] : platform || platforms,
22
+ arch: state.config.target.arch ? [state.config.target.arch] : arch || archs,
23
+ runtime: state.config.target.runtime ? [state.config.target.runtime] : runtime || runtimes,
24
+ buildType: state.config.target.buildType ? [state.config.target.buildType] : buildType || buildTypes,
25
+ runtimeEnv: state.config.target.runtimeEnv ? [state.config.target.runtimeEnv] : runtimeEnv || runtimeEnvs,
20
26
  };
21
27
  }
22
28
 
23
29
  export function getBuildTargets(targetParams) {
24
30
  const { platform, arch, runtime, buildType, runtimeEnv } = targetParams;
25
31
  return state.targets.filter(t => (
26
- (platform === 'all' || t.platform === platform)
27
- && (arch === 'all' || t.arch === arch)
28
- && (runtime === 'all' || t.runtime === runtime)
29
- && (buildType === 'all' || t.buildType === buildType)
30
- && (runtimeEnv === 'all' || t.runtimeEnv === runtimeEnv)
32
+ (!t.platform || platform.includes(t.platform))
33
+ && (!t.arch || arch.includes(t.arch))
34
+ && (!t.runtime || runtime.includes(t.runtime))
35
+ && (!t.buildType || buildType.includes(t.buildType))
36
+ && (!t.runtimeEnv || runtimeEnv.includes(t.runtimeEnv))
31
37
  ));
32
38
  }
33
39
 
package/src/bin.js CHANGED
@@ -22,6 +22,12 @@ import findFiles from './utils/findFiles.js';
22
22
 
23
23
  const packageJSON = JSON.parse(fs.readFileSync(new URL('../package.json', import.meta.url)));
24
24
 
25
+ const platforms = [...new Set(state.targets.map(target => target.platform).filter(t => t))];
26
+ const archs = [...new Set(state.targets.map(target => target.arch).filter(t => t))];
27
+ const runtimes = [...new Set(state.targets.map(target => target.runtime).filter(t => t))];
28
+ const buildTypes = [...new Set(state.targets.map(target => target.buildType).filter(t => t))];
29
+ const runtimeEnvs = [...new Set(state.targets.map(target => target.runtimeEnv).filter(t => t))];
30
+
25
31
  const program = new Command();
26
32
 
27
33
  program
@@ -32,11 +38,11 @@ program
32
38
 
33
39
  const commandBuild = program.command('build')
34
40
  .description('compile the project that was set up using Cpp.js')
35
- .addOption(new Option('-p, --platform <platform>', 'target platform').default('all').choices(['all', 'wasm', 'android', 'ios']))
36
- .addOption(new Option('-a, --arch <arch>', 'target architecture').default('all').choices(['all', 'wasm32', 'wasm64', 'x86_64', 'arm64-v8a', 'iphoneos', 'iphonesimulator']))
37
- .addOption(new Option('-r, --runtime <runtime>', 'target runtime').default('all').choices(['all', 'st', 'mt']))
38
- .addOption(new Option('-b, --build-type <buildType>', 'target build type').default('release').choices(['all', 'release', 'debug']))
39
- .addOption(new Option('-re, --runtime-env <runtimeEnv>', 'target runtime environment').default('all').choices(['all', 'browser', 'node']));
41
+ .addOption(new Option('-p, --platform <platform>', 'target platform').argParser(createListParser(platforms)))
42
+ .addOption(new Option('-a, --arch <arch>', 'target architecture').argParser(createListParser(archs)))
43
+ .addOption(new Option('-r, --runtime <runtime>', 'target runtime').argParser(createListParser(runtimes)))
44
+ .addOption(new Option('-b, --build-type <buildType>', 'target build type').argParser(createListParser(buildTypes)))
45
+ .addOption(new Option('-re, --runtime-env <runtimeEnv>', 'target runtime environment').argParser(createListParser(runtimeEnvs)));
40
46
 
41
47
  const commandDocker = program.command('docker')
42
48
  .description('manage docker');
@@ -60,9 +66,20 @@ program.parse(process.argv);
60
66
  switch (program.args[0]) {
61
67
  case 'build': {
62
68
  const targetParams = commandBuild.opts();
63
- if (targetParams.platform === 'wasm' && targetParams.arch === 'all') {
64
- targetParams.arch = 'wasm32';
69
+
70
+ if (!targetParams.arch) {
71
+ targetParams.arch = archs.filter(item => item !== 'wasm64');
72
+ }
73
+ if (!targetParams.buildType) {
74
+ targetParams.buildType = ['release'];
65
75
  }
76
+
77
+ targetParams.platform = targetParams.platform || platforms;
78
+ targetParams.arch = targetParams.arch || archs;
79
+ targetParams.runtime = targetParams.runtime || runtimes;
80
+ targetParams.buildType = targetParams.buildType || buildTypes;
81
+ targetParams.runtimeEnv = targetParams.runtimeEnv || runtimeEnvs;
82
+
66
83
  if (state.config.build.withBuildConfig) {
67
84
  buildExternal(targetParams);
68
85
  } else {
@@ -320,7 +337,7 @@ function buildLib(targetParams) {
320
337
 
321
338
  createXCFramework();
322
339
 
323
- const iosTargets = getBuildTargets({ platform: 'ios', arch: 'iphoneos', runtime: 'mt', buildType: 'release' });
340
+ const iosTargets = getBuildTargets({ platform: ['ios'], arch: ['iphoneos'], runtime: ['mt'], buildType: ['release'] });
324
341
  const podSpecs = findFiles('*.podspec', { cwd: state.config.paths.project });
325
342
  if (podSpecs.length === 0 && targets.length > 0) {
326
343
  const iosTarget = iosTargets[0];
@@ -390,3 +407,15 @@ async function createWasmJs(targetParams) {
390
407
  }
391
408
  }
392
409
  }
410
+
411
+ function createListParser(validList) {
412
+ return (value, previous) => {
413
+ const items = value.split(',').map(item => item.trim());
414
+ for (const item of items) {
415
+ if (!validList.includes(item)) {
416
+ throw new Error(`Invalid value: "${item}". Allowed values: ${validList.join(', ')}`);
417
+ }
418
+ }
419
+ return previous ? previous.concat(items) : items;
420
+ };
421
+ }