Skip to content

Commit

Permalink
Delete WASM_SUPPORT_MODE and add NATIVE_CODE_BUNDLE_MODE (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
dumganhar authored Jan 10, 2024
1 parent b4bcbb8 commit 4bb461d
Show file tree
Hide file tree
Showing 13 changed files with 66 additions and 46 deletions.
6 changes: 5 additions & 1 deletion .api/public.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,11 @@ declare module "@cocos/ccbuild" {
SERVER_MODE: boolean;
NOT_PACK_PHYSX_LIBS: boolean;
WEBGPU: boolean;
WASM_SUPPORT_MODE: number;
/**
* Native code (wasm/asmjs) bundle mode, 0: asmjs, 1: wasm, 2: both
* @default 2
*/
NATIVE_CODE_BUNDLE_MODE: number;
/**
* An internal constant to indicate whether we cull the meshopt wasm module and asm.js module.
*
Expand Down
15 changes: 15 additions & 0 deletions modules/build-engine/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,20 @@ function verifyCache (options: buildEngine.Options): boolean {
function applyDefaultOptions (options: buildEngine.Options): void {
options.preserveType ??= false;
options.loose = true; // force using true
if (!options.flags) {
options.flags = {};
}
switch (options.nativeCodeBundleMode) {
case 'asmjs':
options.flags.NATIVE_CODE_BUNDLE_MODE = 0;
break;
case 'wasm':
options.flags.NATIVE_CODE_BUNDLE_MODE = 1;
break;
default:
options.flags.NATIVE_CODE_BUNDLE_MODE = 2;
break;
}
}

function moduleOptionsToBabelEnvModules(moduleOptions: buildEngine.ModuleFormat): false | 'commonjs' | 'amd' | 'umd' | 'systemjs' | 'auto' {
Expand All @@ -34,6 +48,7 @@ export async function buildEngine (options: buildEngine.Options): Promise<buildE
if (verifyCache(options)) {
throw 'TODO';
}

if (options.platform === 'OPEN_HARMONY') {
if (options.preserveType) {
// we use a custom engine builder for OPEN_HARMONY platform when enable preserveType option.
Expand Down
8 changes: 6 additions & 2 deletions modules/stats-query/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,11 @@ export namespace StatsQuery {
SERVER_MODE: boolean;
NOT_PACK_PHYSX_LIBS: boolean;
WEBGPU: boolean;
WASM_SUPPORT_MODE: number;
/**
* Native code (wasm/asmjs) bundle mode, 0: asmjs, 1: wasm, 2: both
* @default 2
*/
NATIVE_CODE_BUNDLE_MODE: number;

/**
* An internal constant to indicate whether we cull the meshopt wasm module and asm.js module.
Expand Down Expand Up @@ -485,7 +489,7 @@ export namespace StatsQuery {
const value = info.value;

let declarationKind = 'const';
if (platform === 'OPEN_HARMONY' && key === 'WASM_SUPPORT_MODE') {
if (platform === 'OPEN_HARMONY' && key === 'NATIVE_CODE_BUNDLE_MODE') {
declarationKind = 'let'; // HACK: on OH platform, we cannot compile successfully when declarationKind is const.
}
result += `export ${declarationKind} ${key} = ${value};\n`;
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cocos/ccbuild",
"version": "2.1.0",
"version": "2.2.0",
"description": "The next generation of build tool for Cocos engine.",
"main": "./lib/index.js",
"types": "./lib/index.d.ts",
Expand Down
30 changes: 16 additions & 14 deletions test/build-engine/__snapshots__/engine-js.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ exports[`engine-js build WASM module on platform maybe supporting WASM 2`] = `

exports[`engine-js build WASM module on platform maybe supporting WASM: cull asm.js module 1`] = `
[
"_virtual_cc-aa67b7c2.js",
"_virtual_cc-3076d6c7.js",
"assets/wasm_c-8515ca89.wasm",
"assets/wasm_rust-404aff2f.wasm",
"cc.js",
"wasm_c-a7853123.js",
"wasm_c.wasm-12bb28a2.js",
"wasm_c.wasm-5417fe99.js",
]
`;

Expand Down Expand Up @@ -265,9 +265,9 @@ tryDefineGlobal('CC_RUNTIME_BASED', false);
tryDefineGlobal('CC_SUPPORT_JIT', false);
tryDefineGlobal('CC_JSB', false);
const WASM_SUPPORT_MODE = 0;
const NATIVE_CODE_BUNDLE_MODE = 0;
console.log(WASM_SUPPORT_MODE);
console.log(NATIVE_CODE_BUNDLE_MODE);
"
`;

Expand Down Expand Up @@ -303,9 +303,9 @@ tryDefineGlobal('CC_RUNTIME_BASED', false);
tryDefineGlobal('CC_SUPPORT_JIT', false);
tryDefineGlobal('CC_JSB', false);
const WASM_SUPPORT_MODE = 0;
const NATIVE_CODE_BUNDLE_MODE = 2;
console.log(WASM_SUPPORT_MODE);
console.log(NATIVE_CODE_BUNDLE_MODE);
"
`;

Expand Down Expand Up @@ -341,9 +341,9 @@ tryDefineGlobal('CC_RUNTIME_BASED', false);
tryDefineGlobal('CC_SUPPORT_JIT', false);
tryDefineGlobal('CC_JSB', false);
const WASM_SUPPORT_MODE = 0;
const NATIVE_CODE_BUNDLE_MODE = 1;
console.log(WASM_SUPPORT_MODE);
console.log(NATIVE_CODE_BUNDLE_MODE);
"
`;

Expand Down Expand Up @@ -478,28 +478,30 @@ exports[`engine-js cull meshopt: without wasm support 2`] = `
exports[`engine-js enumerate dependents 1`] = `
[
"assets/wasm_rust-404aff2f.wasm",
"assets/wasm_c.js.mem-429cc1f7.bin",
"assets/wasm_c-8515ca89.wasm",
"wasm-test.js",
"wasm-test-1db8bc36.js",
"wasm_c-a7853123.js",
"wasm_c.wasm-7cf70cb9.js",
"wasm-test-4a045bf0.js",
"module",
"wasm_c-a7853123.js",
"wasm_c.wasm-e63dbb4a.js",
]
`;

exports[`engine-js enumerate dependents 2`] = `
[
"wasm-test.js",
"wasm-test-1db8bc36.js",
"wasm_c-a7853123.js",
"wasm_c.wasm-7cf70cb9.js",
"wasm-test-4a045bf0.js",
"module",
"wasm_c-a7853123.js",
"wasm_c.wasm-e63dbb4a.js",
]
`;

exports[`engine-js enumerate dependents 3`] = `
[
"assets/wasm_rust-404aff2f.wasm",
"assets/wasm_c.js.mem-429cc1f7.bin",
"assets/wasm_c-8515ca89.wasm",
]
`;
Expand Down
8 changes: 4 additions & 4 deletions test/build-engine/__snapshots__/engine-ts.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export { Module as default };
"native/external/wasm/emscripten/wasm_c.js.mem.0e186.ts": "export default 'assets/wasm_c.js.mem';",
"native/external/wasm/emscripten/wasm_c.wasm.195b9.ts": "export default '';",
"native/external/wasm/emscripten/wasm_c.wasm.js.0e998.ts": "let $: any;export default $;",
"wasm/emscripten.ts": "import { WASM_SUPPORT_MODE } from "../__virtual__/internal_constants";
"wasm/emscripten.ts": "import { NATIVE_CODE_BUNDLE_MODE } from "../__virtual__/internal_constants";
import { isSupportWASM } from './is-support-wasm';
import asmFactory from "../native/external/wasm/emscripten/wasm_c.asm.js.36955";
import asmJsMemUrl from "../native/external/wasm/emscripten/wasm_c.js.mem.0e186";
Expand Down Expand Up @@ -68,11 +68,11 @@ function initializeAsm(): Promise<any> {
}).catch(reject);
});
}
if (WASM_SUPPORT_MODE === 1) {
if (NATIVE_CODE_BUNDLE_MODE === 1) {
initializeWasm().then(inst => {
inst._hello();
});
} else if (WASM_SUPPORT_MODE === 2) {
} else if (NATIVE_CODE_BUNDLE_MODE === 2) {
if (isSupportWASM) {
initializeWasm().then(inst => {
inst._hello();
Expand Down Expand Up @@ -254,7 +254,7 @@ tryDefineGlobal('CC_JSB', true);
export const NOT_PACK_PHYSX_LIBS = false;
export const NET_MODE = 0;
export const WEBGPU = false;
export let WASM_SUPPORT_MODE = 0;
export let NATIVE_CODE_BUNDLE_MODE = 2;
export const EDITOR_NOT_IN_PREVIEW = false;
export const WASM_SUBPACKAGE = false;
export const CULL_MESHOPT = false;",
Expand Down
4 changes: 1 addition & 3 deletions test/build-engine/engine-js.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,7 @@ describe('engine-js', () => {
platform: 'OPEN_HARMONY',
features: ['wasm-test'],
nativeCodeBundleMode: 'wasm',
flags: {
WASM_SUPPORT_MODE: 1, // force flag value to test inline dynamic import
},
flags: {},
moduleFormat: 'esm',
});
expect(await getOutputDirStructure(out)).toMatchSnapshot();
Expand Down
13 changes: 5 additions & 8 deletions test/stats-query/__snapshots__/constant-manager.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export const NET_MODE = 0;
export const WEBGPU = false;
export const WASM_SUPPORT_MODE = 1;
export const NATIVE_CODE_BUNDLE_MODE = 2;
export const WASM_SUBPACKAGE = false;
Expand Down Expand Up @@ -214,7 +214,7 @@ export const NET_MODE = 0;
export const WEBGPU = false;
export const WASM_SUPPORT_MODE = 1;
export const NATIVE_CODE_BUNDLE_MODE = 2;
export const EDITOR_NOT_IN_PREVIEW = false;
Expand Down Expand Up @@ -247,6 +247,7 @@ exports[`genBuildTimeConstants 1`] = `
"MAC": false,
"MINIGAME": false,
"NATIVE": true,
"NATIVE_CODE_BUNDLE_MODE": 2,
"NET_MODE": 0,
"NOT_PACK_PHYSX_LIBS": false,
"OHOS": false,
Expand All @@ -262,7 +263,6 @@ exports[`genBuildTimeConstants 1`] = `
"TEST": true,
"VIVO": false,
"WASM_SUBPACKAGE": false,
"WASM_SUPPORT_MODE": 1,
"WEBGPU": false,
"WECHAT": false,
"WECHAT_MINI_PROGRAM": false,
Expand Down Expand Up @@ -686,12 +686,9 @@ exports[`generateInternalConstants 1`] = `
export const WEBGPU: boolean;
/**
* Whether support wasm, here we provide 3 options:
* 0: The platform doesn't support WASM
* 1: The platform supports WASM
* 2: The platform may support WASM, especially on Web platform
* Native code (wasm/asmjs) bundle mode, 0: asmjs, 1: wasm, 2: both
*/
export const WASM_SUPPORT_MODE: number;
export const NATIVE_CODE_BUNDLE_MODE: number;
/**
* Run in editor but not in editor preview.
Expand Down
6 changes: 3 additions & 3 deletions test/test-engine-source/cc.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -377,10 +377,10 @@
"value": false,
"internal": true
},
"WASM_SUPPORT_MODE": {
"comment": "Whether support wasm, here we provide 3 options:\n0: The platform doesn't support WASM\n1: The platform supports WASM\n2: The platform may support WASM, especially on Web platform",
"NATIVE_CODE_BUNDLE_MODE": {
"comment": "Native code (wasm/asmjs) bundle mode, 0: asmjs, 1: wasm, 2: both",
"type": "number",
"value": "$HTML5 ? 2 : ($NATIVE ? ($OPEN_HARMONY?0:1): ($MINIGAME? ($WECHAT?1:0) :0))",
"value": 2,
"internal": true
},
"EDITOR_NOT_IN_PREVIEW": {
Expand Down
4 changes: 2 additions & 2 deletions test/test-engine-source/exports/internal-constants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { WASM_SUPPORT_MODE } from 'internal:constants';
import { NATIVE_CODE_BUNDLE_MODE } from 'internal:constants';

console.log(WASM_SUPPORT_MODE);
console.log(NATIVE_CODE_BUNDLE_MODE);
6 changes: 3 additions & 3 deletions test/test-engine-source/wasm/emscripten.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { WASM_SUPPORT_MODE } from 'internal:constants';
import { NATIVE_CODE_BUNDLE_MODE } from 'internal:constants';
import { isSupportWASM } from './is-support-wasm';

import asmFactory from 'external:wasm/emscripten/wasm_c.asm.js';
Expand Down Expand Up @@ -39,11 +39,11 @@ function initializeAsm (): Promise<any> {
});
}

if (WASM_SUPPORT_MODE === 1) {
if (NATIVE_CODE_BUNDLE_MODE === 1) {
initializeWasm().then(inst => {
inst._hello();
});
} else if (WASM_SUPPORT_MODE === 2) {
} else if (NATIVE_CODE_BUNDLE_MODE === 2) {
if (isSupportWASM) {
initializeWasm().then(inst => {
inst._hello();
Expand Down
6 changes: 3 additions & 3 deletions test/test-engine-source/wasm/wasm-pack.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { WASM_SUPPORT_MODE } from 'internal:constants';
import { NATIVE_CODE_BUNDLE_MODE } from 'internal:constants';
import wasmUrl from 'external:wasm/wasm-pack/wasm_rust.wasm';
import init from 'external:wasm/wasm-pack/wasm_rust.js';
import { isSupportWASM } from './is-support-wasm';

if (WASM_SUPPORT_MODE === 1) {
if (NATIVE_CODE_BUNDLE_MODE === 1) {
init(wasmUrl).then(inst => {
inst.greet();
});
} else if (WASM_SUPPORT_MODE === 2) {
} else if (NATIVE_CODE_BUNDLE_MODE === 2) {
if (isSupportWASM) {
init(wasmUrl).then(inst => {
inst.greet();
Expand Down

0 comments on commit 4bb461d

Please sign in to comment.