Repository navigation
fix(ios): keep one embed metadata linker flag across runs #6218
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
Open
Goldloli
wants to merge
1
commit into
NativeScript:main
Choose a base branch
from
Goldloli:fix/embed-metadata-linker-flag-idempotent
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| /** | ||
| * Keeps the host project's `-sectcreate __DATA __TNSMetadata` linker flag in a | ||
| * sane state when a project is prepared for embedding. | ||
| * | ||
| * `ns embed ios` forces a prepare on every run, and `addToOtherLinkerFlags` | ||
| * appends unconditionally — so every run added another copy of the directive, | ||
| * and `ld` concatenates the referenced files, growing the host executable by a | ||
| * full metadata copy (roughly 9–12 MB) per run. The runtime only reads from the | ||
| * start of the section, which is why the extra copies went unnoticed. | ||
| * | ||
| * The flag therefore has to be set, not appended: drop every existing metadata | ||
| * directive (stale ones naming another architecture or path, plus duplicates | ||
| * left by earlier runs), keep all other linker flags intact, and append the | ||
| * wanted directive once. | ||
| */ | ||
|
|
||
| /** | ||
| * A metadata directive and its path. The path is either quoted — optionally | ||
| * carrying the JSON escaping an `OTHER_LDFLAGS` array entry has, i.e. `\"p\"` — | ||
| * or unquoted with no spaces. | ||
| */ | ||
| const METADATA_DIRECTIVE = | ||
| /-sectcreate\s+__DATA\s+__TNSMetadata\s+(?:\\"(?:[^"\\]|\\[\s\S])*?\\"|"(?:[^"\\]|\\[\s\S])*?"|[^\s"\\]+)/g; | ||
|
|
||
| const INHERITED = "$(inherited)"; | ||
|
|
||
| /** The flag value as `nativescript-dev-xcode` stores it: one JSON-quoted string. */ | ||
| export function metadataSectionFlag(metadataFilePath: string): string { | ||
| return JSON.stringify( | ||
| `-sectcreate __DATA __TNSMetadata "${metadataFilePath}"`, | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Removes metadata directives from a single `OTHER_LDFLAGS` entry. | ||
| * | ||
| * Only the directive goes away: a scalar entry can hold several flags at once | ||
| * (`"$(inherited)" -ObjC`), so its other flags — and the quote that closes the | ||
| * entry — have to survive. Entries without a directive are returned untouched, | ||
| * so unrelated spacing is never rewritten. | ||
| */ | ||
| function stripMetadataDirectives(entry: string): string { | ||
| const without = entry.replace(METADATA_DIRECTIVE, " "); | ||
| if (without === entry) { | ||
| return entry; | ||
| } | ||
|
|
||
| return without.replace(/[ \t]{2,}/g, " ").trim(); | ||
| } | ||
|
|
||
| /** True when an entry held nothing but the directive once it was removed. */ | ||
| function isEmptyEntry(entry: string): boolean { | ||
| return /^[\s"']*$/.test(entry); | ||
| } | ||
|
|
||
| /** | ||
| * Rewrites the build configurations of `project`'s product so `OTHER_LDFLAGS` | ||
| * ends up with exactly one metadata directive. | ||
| * | ||
| * Configurations are selected the same way `addToOtherLinkerFlags` selects them | ||
| * (by the project's `PRODUCT_NAME`), so other targets in the host project keep | ||
| * their own linker flags. | ||
| */ | ||
| export function setMetadataSectionFlag(project: any, flag: string): void { | ||
| const configurations = project.pbxXCBuildConfigurationSection() || {}; | ||
| const productName = project.productName; | ||
|
|
||
| for (const key of Object.keys(configurations)) { | ||
| if (/_comment$/.test(key)) { | ||
| continue; | ||
| } | ||
|
|
||
| const buildSettings = configurations[key].buildSettings; | ||
| if (!buildSettings) { | ||
| continue; | ||
| } | ||
|
|
||
| const currentProductName = buildSettings.PRODUCT_NAME; | ||
| const unquotedProductName = | ||
| typeof currentProductName === "string" | ||
| ? currentProductName.replace(/^"(.*)"$/, "$1") | ||
| : currentProductName; | ||
| if (unquotedProductName !== productName) { | ||
| continue; | ||
| } | ||
|
|
||
| const current = buildSettings.OTHER_LDFLAGS; | ||
| const entries: any[] = Array.isArray(current) | ||
| ? current | ||
| : current === undefined | ||
| ? [] | ||
| : [current]; | ||
|
|
||
| const kept: any[] = []; | ||
| for (const entry of entries) { | ||
| if (typeof entry !== "string") { | ||
| kept.push(entry); | ||
| continue; | ||
| } | ||
|
|
||
| const stripped = stripMetadataDirectives(entry); | ||
| if (!isEmptyEntry(stripped)) { | ||
| kept.push(stripped); | ||
| } | ||
| } | ||
|
|
||
| // The host's flags are usually inherited from an xcconfig file, so | ||
| // `$(inherited)` has to stay in the list; `addToOtherLinkerFlags` seeds it | ||
| // the same way whenever the setting is missing. | ||
| const hasInherited = kept.some( | ||
| (entry) => typeof entry === "string" && entry.includes(INHERITED), | ||
| ); | ||
| if (!hasInherited) { | ||
| kept.unshift(JSON.stringify(INHERITED)); | ||
| } | ||
|
|
||
| kept.push(flag); | ||
| buildSettings.OTHER_LDFLAGS = kept; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Preserve whitespace inside unrelated flag arguments.
If a scalar entry contains
-force_load "libs/My Library.a"and a metadata directive, Line 48 changes the library path tolibs/My Library.a. The saved project then references a different file.Remove the global whitespace normalization. Trim only the entry boundaries, or normalize only separators outside quoted arguments. This preserves unrelated paths while still removing directive-only entries.