Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3.8.3] Refactor wasm/asmjs, add nativeCodeBundleMode #51

Merged
merged 13 commits into from
Jan 9, 2024
34 changes: 5 additions & 29 deletions .api/public.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
declare module "@cocos/ccbuild" {

Check warning on line 1 in .api/public.d.ts

View workflow job for this annotation

GitHub Actions / test

File ignored by default.
/**
* @group Merged Types
*/
Expand Down Expand Up @@ -71,20 +71,12 @@
*/
split?: boolean;
/**
* 使用的 ammo.js 版本,也即 `@cocos/ammo` 映射到的版本。
* - 为 `true` 时使用 WebAssembly 版本的 ammo.js;
* - 为 `false` 时使用 asm.js 版本的 ammo.js;
* - 为 `'fallback` 时同时在结果中包含两个版本的 ammo.js,并自动根据环境 fallback 选择。
*
* 注意,`'fallback'` 只有在 SystemJS 和 Async functions 同时支持时才有效。
* @default true
*
* @deprecated 从 1.1.5 版本开始,该选项只会影响 FORCE_BANNING_BULLET_WASM 宏的值
* - 为 `true` 时,FORCE_BANNING_BULLET_WASM 为 false
* - 为 `false` 时,FORCE_BANNING_BULLET_WASM 为 true
* - 为 `'fallback'` 时, FORCE_BANNING_BULLET_WASM 为 false
* 原生代码的打包模式
* - 为 `wasm` 时使用 wasm 版本原生库
* - 为 `asmjs` 时使用 asmjs 版本的原生库
* - 为 `both` 时同时在结果中包含 wasm 与 asmjs 两个版本的原生库
*/
ammoJsWasm?: boolean | "fallback";
nativeCodeBundleMode?: "wasm" | "asmjs" | "both";
/**
* If true, all deprecated features/API are excluded.
* You can also specify a version range(in semver range) to exclude deprecations in specified version(s).
Expand Down Expand Up @@ -247,28 +239,12 @@
NOT_PACK_PHYSX_LIBS: boolean;
WEBGPU: boolean;
WASM_SUPPORT_MODE: number;
FORCE_BANNING_BULLET_WASM: boolean;
/**
* Whether cull the asm.js module.
* If this is true, the external modules ending with '.asm.js' or 'js.mem' will be culled.
*
* @default false
*/
CULL_ASM_JS_MODULE: boolean;
/**
* An internal constant to indicate whether we cull the meshopt wasm module and asm.js module.
*
* @default false
*/
CULL_MESHOPT: boolean;
/**
* An internal constant to indicate whether need a fallback of wasm.
* If true, we build a wasm fallback module for the compatibility of wasm files compiled by different version of emscripten.
* This is useful when we use wasm on different version of Safari browsers.
*
* @default false
*/
WASM_FALLBACK: boolean;
/**
* An internal constant to indicate whether we use wasm assets as minigame subpackage.
* This is useful when we need to reduce code size.
Expand Down
13 changes: 11 additions & 2 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: test
run-name: ${{ github.actor }} - test
on: [pull_request]
on: [pull_request, push]
jobs:
test:
strategy:
Expand All @@ -14,6 +14,15 @@ jobs:
with:
node-version: '18.17.0'

- run: node --version
- run: npm --version

- run: npm ci

- run: npm test
- run: npm test

# - name: Upload Artifact
# uses: actions/upload-artifact@v3
# with:
# name: ${{ matrix.os }}
# path: test/build-engine
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"editor.codeActionsOnSave": {
"source.fixAll": true
"source.fixAll": "explicit"
},
"cSpell.enabled": true,
"cSpell.words": [
Expand Down
22 changes: 12 additions & 10 deletions modules/build-engine/src/engine-js/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,16 @@
}


let { ammoJsWasm } = options;
ammoJsWasm ??= true; // default is true
const forceBanningBulletWasm = !ammoJsWasm;
let { nativeCodeBundleMode } = options;
nativeCodeBundleMode ??= 'both'; // default is true

const flags = options.flags ?? {};
flags.FORCE_BANNING_BULLET_WASM = forceBanningBulletWasm;

// meshopt is only used in 3d module, so cull it if 3d module is disabled.
if (flags.CULL_MESHOPT === undefined) {
flags.CULL_MESHOPT = !features.includes('3d');
}

const intrinsicFlags = statsQuery.getIntrinsicFlagsOfFeatures(features);
let buildTimeConstants = statsQuery.constantManager.genBuildTimeConstants({
mode: options.mode,
Expand Down Expand Up @@ -171,7 +176,7 @@
presetEnvOptions.targets = options.targets;
}

const babelPlugins: any[] = [];

Check warning on line 179 in modules/build-engine/src/engine-js/index.ts

View workflow job for this annotation

GitHub Actions / test

Unexpected any. Specify a different type
if (!options.targets) {
babelPlugins.push([babelPluginTransformForOf, {
loose: true,
Expand Down Expand Up @@ -241,18 +246,15 @@
rollupPlugins.push(
externalWasmLoader({
externalRoot: ps.join(engineRoot, 'native/external'),
wasmSupportMode: buildTimeConstants.WASM_SUPPORT_MODE,
forceBanningBulletWasm,
cullAsmJsModule: buildTimeConstants.CULL_ASM_JS_MODULE,
nativeCodeBundleMode,
cullMeshopt: buildTimeConstants.CULL_MESHOPT,
format: 'relative-from-chunk',
wasmFallback: buildTimeConstants.WASM_FALLBACK,
wasmSubpackage: buildTimeConstants.WASM_SUBPACKAGE,
}),

{
name: '@cocos/ccbuild|module-overrides',
resolveId(source, importer): string | null {

Check warning on line 257 in modules/build-engine/src/engine-js/index.ts

View workflow job for this annotation

GitHub Actions / test

'importer' is defined but never used
if (moduleOverrides[source]) {
return source;
} else {
Expand Down Expand Up @@ -301,6 +303,7 @@
skipPreflightCheck: true,
...babelOptions,
}),

);

// if (options.progress) {
Expand Down Expand Up @@ -457,6 +460,5 @@
}

result.hasCriticalWarns = hasCriticalWarns;

return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

const suffixReplaceConfig: ISuffixReplaceConfig = {
'.mem': '.mem.bin',
'.wasm.fallback': '.wasm.fallback.bin',
};

/**
Expand Down Expand Up @@ -68,22 +67,14 @@
}
}

function shouldCullBulletWasmModule (options: externalWasmLoader.Options, id: string): boolean {
return options.forceBanningBulletWasm && id.includes('bullet');
}

function shouldCullAsmJsModule (options: externalWasmLoader.Options, id: string): boolean {
return options.wasmSupportMode !== 0 && options.cullAsmJsModule;
}

function shouldCullMeshoptModule (options: externalWasmLoader.Options, id: string): boolean {
return options.cullMeshopt && id.includes('meshopt');
}

const loadConfig: ILoadConfig = {
'.wasm': {
shouldCullModule (options: externalWasmLoader.Options, id: string): boolean {
return options.wasmSupportMode === 0 || shouldCullBulletWasmModule(options, id) || shouldCullMeshoptModule(options, id);
return options.nativeCodeBundleMode === 'asmjs' || shouldCullMeshoptModule(options, id);
},
shouldEmitAsset (options: externalWasmLoader.Options, id: string): boolean {
return !this.shouldCullModule(options, id);
Expand All @@ -92,7 +83,7 @@
},
'.js.mem': {
shouldCullModule (options: externalWasmLoader.Options, id: string): boolean {
return (options.wasmSupportMode === 1 && !shouldCullBulletWasmModule(options, id)) || shouldCullAsmJsModule(options, id) || shouldCullMeshoptModule(options, id);
return options.nativeCodeBundleMode === 'wasm' || shouldCullMeshoptModule(options, id);
},
shouldEmitAsset (options: externalWasmLoader.Options, id: string): boolean {
return !this.shouldCullModule(options, id);
Expand All @@ -101,36 +92,18 @@
},
'.wasm.js': {
shouldCullModule (options: externalWasmLoader.Options, id: string): boolean {
return options.wasmSupportMode === 0 || shouldCullBulletWasmModule(options, id) || shouldCullMeshoptModule(options, id);
return options.nativeCodeBundleMode === 'asmjs' || shouldCullMeshoptModule(options, id);
},
shouldEmitAsset (options: externalWasmLoader.Options, id: string): boolean {

Check warning on line 97 in modules/build-engine/src/engine-js/rollup-plugins/external-wasm-loader.ts

View workflow job for this annotation

GitHub Actions / test

'options' is defined but never used

Check warning on line 97 in modules/build-engine/src/engine-js/rollup-plugins/external-wasm-loader.ts

View workflow job for this annotation

GitHub Actions / test

'id' is defined but never used
return false;
},
cullingContent: `export default function () {}`,
},
'.asm.js': {
shouldCullModule (options: externalWasmLoader.Options, id: string): boolean {
return (options.wasmSupportMode === 1 && !shouldCullBulletWasmModule(options, id)) || shouldCullAsmJsModule(options, id) || shouldCullMeshoptModule(options, id);
},
shouldEmitAsset (options: externalWasmLoader.Options, id: string): boolean {
return false;
},
cullingContent: `export default function () {}`,
},
'.wasm.fallback': {
shouldCullModule (options: externalWasmLoader.Options, id: string): boolean {
return loadConfig['.wasm'].shouldCullModule(options, id) || !options.wasmFallback;
},
shouldEmitAsset (options: externalWasmLoader.Options, id: string): boolean {
return !this.shouldCullModule(options, id);
},
cullingContent: `export default '';`,
},
'.wasm.fallback.js': {
shouldCullModule (options: externalWasmLoader.Options, id: string): boolean {
return loadConfig['.wasm.js'].shouldCullModule(options, id) || !options.wasmFallback;
return options.nativeCodeBundleMode === 'wasm' || shouldCullMeshoptModule(options, id);
},
shouldEmitAsset (options: externalWasmLoader.Options, id: string): boolean {

Check warning on line 106 in modules/build-engine/src/engine-js/rollup-plugins/external-wasm-loader.ts

View workflow job for this annotation

GitHub Actions / test

'options' is defined but never used

Check warning on line 106 in modules/build-engine/src/engine-js/rollup-plugins/external-wasm-loader.ts

View workflow job for this annotation

GitHub Actions / test

'id' is defined but never used
return false;
},
cullingContent: `export default function () {}`,
Expand Down Expand Up @@ -257,7 +230,7 @@
return {
name: '@cocos/ccbuild|external-loader',

async resolveId (this, source, importer): Promise<string | { id: string; external: true; } | null> {

Check warning on line 233 in modules/build-engine/src/engine-js/rollup-plugins/external-wasm-loader.ts

View workflow job for this annotation

GitHub Actions / test

'importer' is defined but never used
if (source.startsWith(externalOrigin)) {
if (options.wasmSubpackage) {
externalWasmModules.push(source);
Expand All @@ -271,7 +244,7 @@
return null;
},

async load (id): Promise<any> {

Check warning on line 247 in modules/build-engine/src/engine-js/rollup-plugins/external-wasm-loader.ts

View workflow job for this annotation

GitHub Actions / test

Unexpected any. Specify a different type
if (id.startsWith(externalOrigin)) {
const filePath = normalizePath(ps.join(options.externalRoot, id.substring(externalOrigin.length)));
for (const suffix in loadConfig) {
Expand All @@ -282,12 +255,12 @@
} else if (config.shouldEmitAsset(options, id)) {
return emitAsset(this, filePath);
} else {
return await fs.readFile(filePath, 'utf8');
return (await fs.readFile(filePath, 'utf8')).replace(/\r\n/g, '\n');
}
}
}
// some external module that doesn't obey the suffix specification, we return its content by default.
return await fs.readFile(filePath, 'utf8');
return (await fs.readFile(filePath, 'utf8')).replace(/\r\n/g, '\n');
}
return null;
},
Expand All @@ -311,7 +284,7 @@
}
},

async generateBundle (opts, bundles): Promise<void> {

Check warning on line 287 in modules/build-engine/src/engine-js/rollup-plugins/external-wasm-loader.ts

View workflow job for this annotation

GitHub Actions / test

'bundles' is defined but never used
if (externalWasmModules.length !== 0) {
const bundler = new ExternalWasmModuleBundler({
...options,
Expand All @@ -331,26 +304,11 @@
* The root path of external repository
*/
externalRoot: string,

/**
* The wasm support mode:
* 0. not support
* 1. support
* 2. maybe support
*/
wasmSupportMode: number;
/**
* Whether need a fallback of wasm.
* If true, we need to build a wasm fallback module for the compatibility of wasm files compiled by different version of emscripten.
*/
wasmFallback: boolean;
/**
* Whether force banning to emit bullet wasm.
*/
forceBanningBulletWasm: boolean;
/**
* Whether cull asm js module.
* The bundle mode of native code while building scripts.
*/
cullAsmJsModule: boolean;
nativeCodeBundleMode: 'wasm' | 'asmjs' | 'both';
/**
* Whether cull meshopt module, including wasm and asm.js.
*/
Expand Down
18 changes: 0 additions & 18 deletions modules/build-engine/src/engine-ts/plugins/external-wasm-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,24 +85,6 @@ const loadConfig: ILoadConfig = {
},
cullingContent: `let $: any;export default $;`,
},
'.wasm.fallback': {
shouldCullModule (options: externalWasmLoaderFactory.Options, id: string): boolean {
return true; // we don't need wasm.fallback module for OH platform
},
shouldEmitAsset (options: externalWasmLoaderFactory.Options, id: string): boolean {
return !this.shouldCullModule(options, id);
},
cullingContent: `export default '';`,
},
'.wasm.fallback.js': {
shouldCullModule (options: externalWasmLoaderFactory.Options, id: string): boolean {
return true; // we don't need wasm.fallback module for OH platform
},
shouldEmitAsset (options: externalWasmLoaderFactory.Options, id: string): boolean {
return false;
},
cullingContent: `export default function () {}`,
},
};

// NOTE: we use rollup to transform CommonJS to ES Module.
Expand Down
18 changes: 5 additions & 13 deletions modules/build-engine/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,20 +111,12 @@ export namespace buildEngine {
split?: boolean;

/**
* 使用的 ammo.js 版本,也即 `@cocos/ammo` 映射到的版本。
* - 为 `true` 时使用 WebAssembly 版本的 ammo.js;
* - 为 `false` 时使用 asm.js 版本的 ammo.js;
* - 为 `'fallback` 时同时在结果中包含两个版本的 ammo.js,并自动根据环境 fallback 选择。
*
* 注意,`'fallback'` 只有在 SystemJS 和 Async functions 同时支持时才有效。
* @default true
*
* @deprecated 从 1.1.5 版本开始,该选项只会影响 FORCE_BANNING_BULLET_WASM 宏的值
* - 为 `true` 时,FORCE_BANNING_BULLET_WASM 为 false
* - 为 `false` 时,FORCE_BANNING_BULLET_WASM 为 true
* - 为 `'fallback'` 时, FORCE_BANNING_BULLET_WASM 为 false
* 原生代码的打包模式
* - 为 `wasm` 时使用 wasm 版本原生库
* - 为 `asmjs` 时使用 asmjs 版本的原生库
* - 为 `both` 时同时在结果中包含 wasm 与 asmjs 两个版本的原生库
*/
ammoJsWasm?: boolean | 'fallback';
nativeCodeBundleMode?: 'wasm' | 'asmjs' | 'both';

/**
* If true, all deprecated features/API are excluded.
Expand Down
23 changes: 4 additions & 19 deletions modules/stats-query/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,28 +293,13 @@ export namespace StatsQuery {
NOT_PACK_PHYSX_LIBS: boolean;
WEBGPU: boolean;
WASM_SUPPORT_MODE: number;
FORCE_BANNING_BULLET_WASM: boolean;
/**
* Whether cull the asm.js module.
* If this is true, the external modules ending with '.asm.js' or 'js.mem' will be culled.
*
* @default false
*/
CULL_ASM_JS_MODULE: boolean;

/**
* An internal constant to indicate whether we cull the meshopt wasm module and asm.js module.
*
* @default false
*/
CULL_MESHOPT: boolean;
/**
* An internal constant to indicate whether need a fallback of wasm.
* If true, we build a wasm fallback module for the compatibility of wasm files compiled by different version of emscripten.
* This is useful when we use wasm on different version of Safari browsers.
*
* @default false
*/
WASM_FALLBACK: boolean;
/**
* An internal constant to indicate whether we use wasm assets as minigame subpackage.
* This is useful when we need to reduce code size.
Expand Down Expand Up @@ -373,10 +358,10 @@ export namespace StatsQuery {
// init helper
let result = '';
if (this._hasCCGlobal(config)) {
result += fs.readFileSync(ps.join(__dirname, '../../../static/helper-global-exporter.txt'), 'utf8') + '\n';
result += fs.readFileSync(ps.join(__dirname, '../../../static/helper-global-exporter.txt'), 'utf8').replace(/\r\n/g, '\n') + '\n';
}
if (this._hasDynamic(config)) {
result += fs.readFileSync(ps.join(__dirname, '../../../static/helper-dynamic-constants.txt'), 'utf8') + '\n';
result += fs.readFileSync(ps.join(__dirname, '../../../static/helper-dynamic-constants.txt'), 'utf8').replace(/\r\n/g, '\n') + '\n';
}

// update value
Expand Down Expand Up @@ -463,7 +448,7 @@ export namespace StatsQuery {
// init helper
let result = '';
if (this._hasCCGlobal(config)) {
result += fs.readFileSync(ps.join(__dirname, '../../../static/helper-global-exporter.txt'), 'utf8') + '\n';
result += fs.readFileSync(ps.join(__dirname, '../../../static/helper-global-exporter.txt'), 'utf8').replace(/\r\n/g, '\n') + '\n';
}

// update value
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.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cocos/ccbuild",
"version": "2.0.3",
"version": "2.1.0",
"description": "The next generation of build tool for Cocos engine.",
"main": "./lib/index.js",
"types": "./lib/index.d.ts",
Expand Down Expand Up @@ -66,9 +66,9 @@
"typedoc": "^0.24.8"
},
"dependencies": {
"@ccbuild/dts-bundler": "*",
"@ccbuild/build-engine": "*",
"@ccbuild/bundler": "*",
"@ccbuild/dts-bundler": "*",
"@ccbuild/modularize": "*",
"@ccbuild/stats-query": "*",
"@ccbuild/transformer": "*",
Expand Down
Loading