Skip to content

Commit

Permalink
use CMAKE_SUPPRESS_REGENERATION to skip project update (cocos#14512)
Browse files Browse the repository at this point in the history
  • Loading branch information
PatriceJiang authored Mar 9, 2023
1 parent c4be6be commit 27b3c2e
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 31 deletions.
9 changes: 6 additions & 3 deletions scripts/native-pack-tool/source/base/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,14 +213,14 @@ export abstract class NativePackTool {
return false;
}

if(!fs.existsSync(srcFile)) {
if (!fs.existsSync(srcFile)) {
console.warn(`${f} not exists in ${commonSrc}`);
return false;
}

if(!compFile(srcFile, dstFile)) {
if (!compFile(srcFile, dstFile)) {
console.log(`File ${dstFile} differs from ${srcFile}`);
return false;
return false;
}
}
return true;
Expand Down Expand Up @@ -440,6 +440,9 @@ export abstract class NativePackTool {
args.push(`-DRES_DIR="${cchelper.fixPath(this.paths.buildDir)}"`);
args.push(`-DAPP_NAME="${this.params.projectName}"`);
args.push(`-DLAUNCH_TYPE="${this.buildType}"`);
if (this.params.platformParams?.skipUpdateXcodeProject) {
args.push(`-DCMAKE_SUPPRESS_REGENERATION=ON`);
}
}


Expand Down
2 changes: 1 addition & 1 deletion scripts/native-pack-tool/source/platforms/ios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export class IOSPackTool extends MacOSPackTool {
await toolHelper.runCmake(['-S', `"${this.paths.platformTemplateDirInPrj}"`, '-GXcode', `-B"${nativePrjDir}"`, '-T', `buildsystem=${ver}`,
'-DCMAKE_SYSTEM_NAME=iOS'].concat(ext));

await this.skipUpdateXcodeProject();
await this.modifyXcodeProject();

return true;
}
Expand Down
46 changes: 20 additions & 26 deletions scripts/native-pack-tool/source/platforms/mac-os.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export abstract class MacOSPackTool extends NativePackTool {
return true;
}

abstract generate() :Promise<boolean>;
abstract generate(): Promise<boolean>;

shouldSkipGenerate() {
const nativePrjDir = this.paths.nativePrjDir;
Expand All @@ -50,7 +50,7 @@ export abstract class MacOSPackTool extends NativePackTool {
return /Apple/.test(model) && process.platform === 'darwin';
}

protected getXcodeMajorVerion():number {
protected getXcodeMajorVerion(): number {
try {
const output = execSync('xcrun xcodebuild -version').toString('utf8');
return Number.parseInt(output.match(/Xcode\s(\d+)\.\d+/)![1]);
Expand All @@ -61,17 +61,24 @@ export abstract class MacOSPackTool extends NativePackTool {
}
}

async skipUpdateXcodeProject() {
async modifyXcodeProject() {
if (this.params.platformParams.skipUpdateXcodeProject) {
if(this.getXcodeMajorVerion() < 12) {
if (this.getXcodeMajorVerion() < 12) {
console.error(`SKIP UPDATE XCODE PROJECT is only supported with Xcode 12 or later`);
return;
}
await this.xcodeDestroyZEROCHECK();
await this.xcodeFixAssetsReferences();
}
}

async xcodeDestroyZEROCHECK() {

/**
* When "Skip Xcode Project Update" is checked, changes to the contents of the "data" directory
* still need to be synchronized with Xcode. One way to achieve this is to modify the Xcode
* project file directly and use directory references to access the "data" directory.
* However, this method is not supported in Xcode 11 and earlier project formats due to
* differences in their formats.
*/
async xcodeFixAssetsReferences() {
const nativePrjDir = this.paths.nativePrjDir;
const xcode = require(ps.join(this.params.enginePath, 'scripts/native-pack-tool/xcode'));
const projs = fs.readdirSync(nativePrjDir).filter((x) => x.endsWith('.xcodeproj')).map((x) => ps.join(nativePrjDir, x));
Expand All @@ -83,7 +90,7 @@ export abstract class MacOSPackTool extends NativePackTool {
const pbxfile = ps.join(proj, 'project.pbxproj');
console.log(`parsing pbxfile ${pbxfile}`);
const projectFile = xcode.project(pbxfile);
await (function() {
await (function () {
return new Promise((resolve, reject) => {
projectFile.parse((err: Error) => {
if (err) {
Expand All @@ -99,12 +106,12 @@ export abstract class MacOSPackTool extends NativePackTool {
const assetsDir = this.paths.buildDir;
const objects = projectFile.hash.project.objects;
const KeyResource = `Resources`;
type ResourceItem = [string, {children:{value:string, comment:string}[]}];
const resources:ResourceItem[] = Object.entries(objects.PBXGroup).filter(([, x]) => (x as any).name === KeyResource) as any;
let hash : string = resources[0][0];
type ResourceItem = [string, { children: { value: string, comment: string }[] }];
const resources: ResourceItem[] = Object.entries(objects.PBXGroup).filter(([, x]) => (x as any).name === KeyResource) as any;
let hash: string = resources[0][0];
if (resources.length > 1) {
console.log(` multiple Resources/ group found!`);
const itemWeight = (a:ResourceItem) : number => {
const itemWeight = (a: ResourceItem): number => {
const hasImageAsset = a[1].children.filter((c) => c.comment.endsWith('.xcassets')).length > 0;
const finalBuildTarget = a[1].children.filter((c) => c.comment.indexOf(`CMakeFiles/${this.params.projectName}`) > -1).length > 0;
console.log(` ${a[0]} hasImageAsset ${hasImageAsset}, is final target ${finalBuildTarget}`);
Expand All @@ -114,7 +121,7 @@ export abstract class MacOSPackTool extends NativePackTool {
console.log(` select ${hash}`);
}

const filterFolders = (name:string) :boolean => {
const filterFolders = (name: string): boolean => {
// NOTE: `assets/remote` should not be linked into Resources/
// return name !== '.' && name !== '..' && name !== 'remote';
return name === 'data'; // only accept `data` folder
Expand Down Expand Up @@ -150,19 +157,6 @@ export abstract class MacOSPackTool extends NativePackTool {
}
});
}
{
// remove all actions in ZERO_CHECK target
const scriptBuildPhase = projectFile.hash.project.objects.PBXShellScriptBuildPhase;
const keys = Object.keys(scriptBuildPhase);
const zeroChecks: any[] = [];
for (const t of keys) {
const x = scriptBuildPhase[t];
if (x.name && x.name.indexOf('ZERO_CHECK') > 0) {
zeroChecks.push(x);
}
}
zeroChecks.forEach((c) => c.shellScript = `"echo 'Skip Xcode Update'"`);
}
fs.writeFileSync(pbxfile, projectFile.writeSync());
console.log(` replace pbxfile: ${pbxfile}.`);
}
Expand Down
2 changes: 1 addition & 1 deletion scripts/native-pack-tool/source/platforms/mac.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class MacPackTool extends MacOSPackTool {

await toolHelper.runCmake(cmakeArgs);

await this.skipUpdateXcodeProject();
await this.modifyXcodeProject();
return true;
}

Expand Down

0 comments on commit 27b3c2e

Please sign in to comment.