Skip to content

Commit

Permalink
[v3.8.6] Fix constants override priority for buildTimeConstants. (#88)
Browse files Browse the repository at this point in the history
  • Loading branch information
dumganhar authored Feb 12, 2025
1 parent 4033370 commit ce7623c
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 99 deletions.
12 changes: 6 additions & 6 deletions modules/build-engine/src/engine-js/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,18 @@ export async function buildJsEngine(options: Required<buildEngine.Options>): Pro
const flags = options.flags ?? {};

const overriddenConstants = statsQuery.getOverriddenConstantsOfFeatures(features);
const flagsWithOverriddenConstants = {
...overriddenConstants, // overridenConstnats in cc.config.json has lower priority
...flags, // options.flags has higher priority
};

const intrinsicFlags = statsQuery.getIntrinsicFlagsOfFeatures(features);
let buildTimeConstants = statsQuery.constantManager.genBuildTimeConstants({
mode: options.mode,
platform: options.platform,
flags,
flags: flagsWithOverriddenConstants,
});
buildTimeConstants = {
...overriddenConstants,
...intrinsicFlags,
...buildTimeConstants,
};
Expand All @@ -131,10 +134,7 @@ export async function buildJsEngine(options: Required<buildEngine.Options>): Pro
const featureUnits = statsQuery.getUnitsOfFeatures(features);

const rpVirtualOptions: Record<string, string> = {};
const flagsWithOverriddenConstants = {
...overriddenConstants,
...flags,
};


const vmInternalConstants = statsQuery.constantManager.exportStaticConstants({
platform: options.platform,
Expand Down
112 changes: 56 additions & 56 deletions test/build-engine/__snapshots__/engine-js.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2698,64 +2698,64 @@ console.log(NATIVE_CODE_BUNDLE_MODE);
exports[`engine-js cull meshopt: with wasm support 1`] = `
[
"assets/meshopt_decoder.wasm-ILPQNID3.wasm",
"cc.js",
]
`;
exports[`engine-js cull meshopt: with wasm support 2`] = `
"System.register([], (function (exports) {
'use strict';
return {
execute: (function () {
exports({
testAsm: meshopt_decoder_asm,
testWasm: meshopt_decoder_wasm$1
});
function meshopt_decoder_asm () {}
function meshopt_decoder_wasm$1 () {}
var meshopt_decoder_wasm = exports("wasmUrl", '');
function tryDefineGlobal (name, value) {
const _global = typeof window === 'undefined' ? global : window;
if (typeof _global[name] === 'undefined') {
return (_global[name] = value);
} else {
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return _global[name];
}
}
tryDefineGlobal('CC_WECHAT', true);
tryDefineGlobal('CC_BAIDU', false);
tryDefineGlobal('CC_XIAOMI', false);
tryDefineGlobal('CC_ALIPAY', false);
tryDefineGlobal('CC_BYTEDANCE', false);
tryDefineGlobal('CC_OPPO', false);
tryDefineGlobal('CC_VIVO', false);
tryDefineGlobal('CC_HUAWEI', false);
tryDefineGlobal('CC_COCOSPLAY', false);
tryDefineGlobal('CC_QTT', false);
tryDefineGlobal('CC_LINKSURE', false);
tryDefineGlobal('CC_EDITOR', false);
tryDefineGlobal('CC_PREVIEW', false);
tryDefineGlobal('CC_BUILD', true);
tryDefineGlobal('CC_TEST', false);
tryDefineGlobal('CC_DEBUG', true);
tryDefineGlobal('CC_DEV', false);
tryDefineGlobal('CC_MINIGAME', true);
tryDefineGlobal('CC_RUNTIME_BASED', false);
tryDefineGlobal('CC_SUPPORT_JIT', false);
tryDefineGlobal('CC_JSB', false);
const CULL_MESHOPT = true;
console.log(CULL_MESHOPT);
})
};
"System.register([], (function (exports, module) {
'use strict';
return {
execute: (function () {
exports("testAsm", meshopt_decoder_asm);
function meshopt_decoder_asm () {}
var MeshoptDecoder = exports("testWasm", {
test_prop: 'this is the implementation of meshopt wasm module'
});
var meshopt_decoder_wasm = exports("wasmUrl", 'assets/meshopt_decoder.wasm-ILPQNID3.wasm'); /* asset-hash:bc946de1 */
function tryDefineGlobal (name, value) {
const _global = typeof window === 'undefined' ? global : window;
if (typeof _global[name] === 'undefined') {
return (_global[name] = value);
} else {
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return _global[name];
}
}
tryDefineGlobal('CC_WECHAT', true);
tryDefineGlobal('CC_BAIDU', false);
tryDefineGlobal('CC_XIAOMI', false);
tryDefineGlobal('CC_ALIPAY', false);
tryDefineGlobal('CC_BYTEDANCE', false);
tryDefineGlobal('CC_OPPO', false);
tryDefineGlobal('CC_VIVO', false);
tryDefineGlobal('CC_HUAWEI', false);
tryDefineGlobal('CC_COCOSPLAY', false);
tryDefineGlobal('CC_QTT', false);
tryDefineGlobal('CC_LINKSURE', false);
tryDefineGlobal('CC_EDITOR', false);
tryDefineGlobal('CC_PREVIEW', false);
tryDefineGlobal('CC_BUILD', true);
tryDefineGlobal('CC_TEST', false);
tryDefineGlobal('CC_DEBUG', true);
tryDefineGlobal('CC_DEV', false);
tryDefineGlobal('CC_MINIGAME', true);
tryDefineGlobal('CC_RUNTIME_BASED', false);
tryDefineGlobal('CC_SUPPORT_JIT', false);
tryDefineGlobal('CC_JSB', false);
{
console.log('>>> CULL_MESHOPT is false');
}
})
};
}));
"
`;
Expand Down Expand Up @@ -2814,9 +2814,9 @@ exports[`engine-js cull meshopt: without wasm support 2`] = `
tryDefineGlobal('CC_SUPPORT_JIT', false);
tryDefineGlobal('CC_JSB', false);
const CULL_MESHOPT = true;
console.log(CULL_MESHOPT);
{
console.log('>>> CULL_MESHOPT is true');
}
})
};
Expand Down
2 changes: 1 addition & 1 deletion test/build-engine/__snapshots__/engine-ts.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ export const WEBGPU = false;
export let NATIVE_CODE_BUNDLE_MODE = 2;
export const EDITOR_NOT_IN_PREVIEW = false;
export const WASM_SUBPACKAGE = false;
export const CULL_MESHOPT = false;
export const CULL_MESHOPT = true;
export const USE_INTRINSIC_FLAG = false;",
},
"animation/animation.ts": {
Expand Down
9 changes: 4 additions & 5 deletions test/build-engine/engine-js.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,28 +220,27 @@ describe('engine-js', () => {
out,
mode: 'BUILD',
platform: 'WECHAT',
features: ['cull-meshopt'],
features: ['meshopt'],
moduleFormat: 'system',
nativeCodeBundleMode: 'wasm',
flags: {
CULL_MESHOPT: true,
// CULL_MESHOPT: true, // CULL_MESHOPT constants is associated in 'meshopt' feature definition, so doesn't need to set it here
},
});
expect(await getOutputDirStructure(out)).toMatchSnapshot('with wasm support');
expect(await getOutputContent(ps.join(out, 'cc.js'))).toMatchSnapshot('with wasm support');
await del(out, { force: true });


await buildEngine({
engine: ps.join(__dirname, '../test-engine-source'),
out,
mode: 'BUILD',
platform: 'BYTEDANCE',
features: ['cull-meshopt'],
features: ['meshopt'],
moduleFormat: 'system',
nativeCodeBundleMode: 'asmjs',
flags: {
CULL_MESHOPT: true,
CULL_MESHOPT: true, // Test override constant defined in feature.
},
});
expect(await getOutputDirStructure(out)).toMatchSnapshot('without wasm support');
Expand Down
2 changes: 1 addition & 1 deletion test/build-engine/engine-ts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ describe('WASM', () => {
mode: 'BUILD',
platform: 'OPEN_HARMONY',
preserveType: true,
features: ['cull-meshopt'],
features: ['meshopt'],
moduleFormat: 'system',
flags: {
CULL_MESHOPT: true,
Expand Down
6 changes: 3 additions & 3 deletions test/stats-query/__snapshots__/constant-manager.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export const NATIVE_CODE_BUNDLE_MODE = 2;
export const WASM_SUBPACKAGE = false;
export const CULL_MESHOPT = false;
export const CULL_MESHOPT = true;
export const USE_INTRINSIC_FLAG = false;
Expand Down Expand Up @@ -222,7 +222,7 @@ export const EDITOR_NOT_IN_PREVIEW = false;
export const WASM_SUBPACKAGE = false;
export const CULL_MESHOPT = false;
export const CULL_MESHOPT = true;
export const USE_INTRINSIC_FLAG = false;
Expand All @@ -237,7 +237,7 @@ exports[`genBuildTimeConstants 1`] = `
"BUILD": false,
"BYTEDANCE": false,
"COCOSPLAY": false,
"CULL_MESHOPT": false,
"CULL_MESHOPT": true,
"DEBUG": false,
"DEV": true,
"EDITOR": false,
Expand Down
46 changes: 23 additions & 23 deletions test/stats-query/constant-manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,46 +5,46 @@ const ConstantManager = StatsQuery.ConstantManager;
const cm = new ConstantManager(ps.join(__dirname, '../test-engine-source/'));

test('generateInternalConstants', () => {
expect(cm.genInternalConstants()).toMatchSnapshot();
expect(cm.genInternalConstants()).toMatchSnapshot();
});

test('generateCCEnv', () => {
expect(cm.genCCEnv()).toMatchSnapshot();
expect(cm.genCCEnv()).toMatchSnapshot();
});

test('genCCEnvConstants', () => {
expect(cm.genCCEnvConstants({
mode: 'TEST',
platform: 'NATIVE',
flags: { DEBUG: false, SERVER_MODE: true },
})).toMatchSnapshot();
})).toMatchSnapshot();
});

test('exportStaticConstants', () => {
expect(
cm.exportStaticConstants({
mode: 'PREVIEW',
platform: 'WECHAT',
flags: { DEBUG: false, SERVER_MODE: true },
})
).toMatchSnapshot();
expect(
cm.exportStaticConstants({
mode: 'PREVIEW',
platform: 'WECHAT',
flags: { DEBUG: false, SERVER_MODE: true },
})
).toMatchSnapshot();
});

test('exportDynamicConstants', () => {
expect(
cm.exportDynamicConstants({
mode: 'BUILD',
platform: 'WECHAT',
flags: { DEBUG: true, SERVER_MODE: true },
})
).toMatchSnapshot();
expect(
cm.exportDynamicConstants({
mode: 'BUILD',
platform: 'WECHAT',
flags: { DEBUG: true, SERVER_MODE: true },
})
).toMatchSnapshot();
});

test('genBuildTimeConstants', () => {
const result = cm.genBuildTimeConstants({
mode: 'TEST',
platform: 'NATIVE',
flags: { DEBUG: false, SERVER_MODE: true },
});
expect(result).toMatchSnapshot();
const result = cm.genBuildTimeConstants({
mode: 'TEST',
platform: 'NATIVE',
flags: { DEBUG: false, SERVER_MODE: true },
});
expect(result).toMatchSnapshot();
});
9 changes: 6 additions & 3 deletions test/test-engine-source/cc.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,11 @@
"module-query": {
"modules": ["module-query"]
},
"cull-meshopt": {
"modules": ["cull-meshopt"]
"meshopt": {
"modules": ["cull-meshopt"],
"overrideConstants": {
"CULL_MESHOPT": false
}
},
"dynamic-import": {
"modules": ["dynamic-import"]
Expand Down Expand Up @@ -423,7 +426,7 @@
"CULL_MESHOPT": {
"comment": "An internal constant to indicate whether we cull the meshopt wasm module and asm.js module.",
"type": "boolean",
"value": false,
"value": true,
"internal": true
},
"USE_INTRINSIC_FLAG": {
Expand Down
6 changes: 5 additions & 1 deletion test/test-engine-source/cull-meshopt/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import testWasm from 'external:wasm/emscripten/meshopt/meshopt_decoder.wasm.js';
import wasmUrl from 'external:wasm/emscripten/meshopt/meshopt_decoder.wasm.wasm';
import { CULL_MESHOPT } from 'internal:constants';

console.log(CULL_MESHOPT);
if (CULL_MESHOPT) {
console.log('>>> CULL_MESHOPT is true');
} else {
console.log('>>> CULL_MESHOPT is false');
}

export { testAsm, testWasm, wasmUrl };

0 comments on commit ce7623c

Please sign in to comment.