Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 50 additions & 7 deletions lib/services/ios-project-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -779,13 +779,13 @@ export class IOSProjectService
// }
// );

const metadataPath = path.relative(
this.$options.hostProjectPath,
buildFolderPath,
);
project.addToOtherLinkerFlags(
const metadataPath = path
.relative(this.$options.hostProjectPath, buildFolderPath)
.replace(/\\/g, "/");
this.setHostMetadataLinkerFlag(
project,
JSON.stringify(
`-sectcreate __DATA __TNSMetadata "${metadataPath}/metadata-arm64.bin"`,
`-sectcreate __DATA __TNSMetadata "${metadataPath}/metadata-$(CURRENT_ARCH).bin"`,
),
);

Expand Down Expand Up @@ -820,7 +820,7 @@ export class IOSProjectService

this.savePbxProj(project, projectData);
} catch (err) {
this.$logger.trace(
this.$logger.error(
"Error adding NativeScript group to host project",
err,
);
Expand Down Expand Up @@ -1971,6 +1971,49 @@ export class IOSProjectService
}
}
}

private setHostMetadataLinkerFlag(
project: any,
flag: string,
): void {
const stripMetadataDirective = (value: string): string => {
const stripped = value
.replace(
/-sectcreate\s+__DATA\s+__TNSMetadata\s+(?:\\?"[^"]*\\?"|[^\s"]+)/g,
"",
);
if (stripped === value) {
return value;
}
return stripped.replace(/\s{2,}/g, " ").trim();
};

const configurations = project.pbxXCBuildConfigurationSection();
for (const key of Object.keys(configurations || {})) {
if (key.endsWith("_comment")) {
continue;
}
const buildSettings = configurations[key]?.buildSettings;
if (!buildSettings) {
continue;
}
const ldFlags = buildSettings.OTHER_LDFLAGS;
if (Array.isArray(ldFlags)) {
buildSettings.OTHER_LDFLAGS = ldFlags
.map((entry: string) => stripMetadataDirective(String(entry)))
.filter((entry: string) => entry.length > 0 && entry !== '""');
} else if (typeof ldFlags === "string") {
const cleaned = stripMetadataDirective(ldFlags);
if (!cleaned || cleaned === '""') {
delete buildSettings.OTHER_LDFLAGS;
} else if (cleaned !== '"$(inherited)"') {
// pbxProject.addToOtherLinkerFlags calls .push(), so convert scalar to array
buildSettings.OTHER_LDFLAGS = [cleaned];
}
}
}
project.addToOtherLinkerFlags(flag);
}
}

injector.register("iOSProjectService", IOSProjectService);
96 changes: 96 additions & 0 deletions test/ios-project-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1447,3 +1447,99 @@ describe("SPM Packages", () => {
// todo: add tests for SPM packages
});
});

describe("setHostMetadataLinkerFlag", () => {
it("removes duplicate -sectcreate __DATA __TNSMetadata flags and adds once idempotently", () => {
const dir = mkdtempSync(path.join(tmpdir(), "myTestProjectPath-"));
const testInjector = createTestInjector(dir, "myTestProjectName");
const iOSProjectService = testInjector.resolve("iOSProjectService");

const addedFlags: string[] = [];
const mockConfig: any = {
buildSettings: {
PRODUCT_NAME: '"myTestProjectName"',
OTHER_LDFLAGS: [
'"$(inherited)"',
'"-ObjC"',
'"-sectcreate __DATA __TNSMetadata \\"app/metadata-arm64.bin\\""',
'"-sectcreate __DATA __TNSMetadata \\"app/metadata-arm64.bin\\""',
],
},
};
const mockProject: any = {
pbxXCBuildConfigurationSection: () => ({
CFG1: mockConfig,
CFG1_comment: "Debug",
}),
addToOtherLinkerFlags: (flag: string) => {
mockConfig.buildSettings.OTHER_LDFLAGS.push(flag);
addedFlags.push(flag);
},
};

const newFlag = JSON.stringify(
'-sectcreate __DATA __TNSMetadata "app/metadata-$(CURRENT_ARCH).bin"',
);
iOSProjectService.setHostMetadataLinkerFlag(mockProject, newFlag);
iOSProjectService.setHostMetadataLinkerFlag(mockProject, newFlag);

assert.deepStrictEqual(mockConfig.buildSettings.OTHER_LDFLAGS, [
'"$(inherited)"',
'"-ObjC"',
newFlag,
]);
assert.strictEqual(addedFlags.length, 2);
});

it("preserves unrelated linker flags in scalar strings and multi-directive array elements", () => {
const dir = mkdtempSync(path.join(tmpdir(), "myTestProjectPath-"));
const testInjector = createTestInjector(dir, "myTestProjectName");
const iOSProjectService = testInjector.resolve("iOSProjectService");

const scalarConfig: any = {
buildSettings: {
PRODUCT_NAME: '"myTestProjectName"',
OTHER_LDFLAGS:
'$(inherited) -ObjC -sectcreate __DATA __TNSMetadata "app/metadata-arm64.bin" -lHostDep',
},
};
const multiArrayConfig: any = {
buildSettings: {
PRODUCT_NAME: '"myTestProjectName"',
OTHER_LDFLAGS: [
'"$(inherited)"',
'-ObjC -sectcreate __DATA __TNSMetadata "app/metadata-arm64.bin" -lOther',
],
},
};
const mockProject: any = {
pbxXCBuildConfigurationSection: () => ({
SCALAR: scalarConfig,
MULTI: multiArrayConfig,
}),
addToOtherLinkerFlags: (flag: string) => {
for (const cfg of [scalarConfig, multiArrayConfig]) {
if (!Array.isArray(cfg.buildSettings.OTHER_LDFLAGS)) {
cfg.buildSettings.OTHER_LDFLAGS = ['"$(inherited)"'];
}
cfg.buildSettings.OTHER_LDFLAGS.push(flag);
}
},
};

const newFlag = JSON.stringify(
'-sectcreate __DATA __TNSMetadata "app/metadata-$(CURRENT_ARCH).bin"',
);
iOSProjectService.setHostMetadataLinkerFlag(mockProject, newFlag);

assert.deepStrictEqual(scalarConfig.buildSettings.OTHER_LDFLAGS, [
"$(inherited) -ObjC -lHostDep",
newFlag,
]);
assert.deepStrictEqual(multiArrayConfig.buildSettings.OTHER_LDFLAGS, [
'"$(inherited)"',
"-ObjC -lOther",
newFlag,
]);
});
});