@@ -1567,6 +1567,200 @@ describe("workspace DerivedData location", () => {
15671567 } ) ;
15681568} ) ;
15691569
1570+ describe ( "embed metadata linker flag" , ( ) => {
1571+ const projectName = "embedHost" ;
1572+ const targetName = "TNSBlank" ;
1573+
1574+ /**
1575+ * A host project with a real pbxproj (test/files/project.pbxproj), wired so
1576+ * the service reads and writes that file instead of a mock.
1577+ */
1578+ function setup ( ) {
1579+ const projectPath = mkdtempSync (
1580+ path . join ( tmpdir ( ) , "embedMetadataProject-" ) ,
1581+ ) ;
1582+ const hostProjectPath = mkdtempSync (
1583+ path . join ( tmpdir ( ) , "embedMetadataHost-" ) ,
1584+ ) ;
1585+ const pbxProjPath = path . join (
1586+ hostProjectPath ,
1587+ `${ targetName } .xcodeproj` ,
1588+ "project.pbxproj" ,
1589+ ) ;
1590+ require ( "fs" ) . mkdirSync ( path . dirname ( pbxProjPath ) , { recursive : true } ) ;
1591+ require ( "fs" ) . copyFileSync (
1592+ path . join ( __dirname , "files" , "project.pbxproj" ) ,
1593+ pbxProjPath ,
1594+ ) ;
1595+
1596+ const testInjector = createTestInjector ( projectPath , projectName , xcode ) ;
1597+ const iOSProjectService : IOSProjectService =
1598+ testInjector . resolve ( "iOSProjectService" ) ;
1599+ ( < any > testInjector . resolve ( "options" ) ) . hostProjectPath = hostProjectPath ;
1600+
1601+ // `--hostProjectPath` moves the platform root into the host project.
1602+ iOSProjectService . getPlatformData ( testInjector . resolve ( "projectData" ) ) ;
1603+ testInjector . resolve ( "xcprojService" ) . findXcodeProject = ( ) =>
1604+ path . dirname ( pbxProjPath ) ;
1605+
1606+ const flagsOfTarget = ( ) : any [ ] => {
1607+ const project = new xcode . project ( pbxProjPath ) ;
1608+ project . parseSync ( ) ;
1609+ const configurations = project . pbxXCBuildConfigurationSection ( ) ;
1610+ return Object . keys ( configurations )
1611+ . filter ( ( key ) => ! / _ c o m m e n t $ / . test ( key ) )
1612+ . map ( ( key ) => configurations [ key ] . buildSettings )
1613+ . filter (
1614+ ( buildSettings : any ) =>
1615+ buildSettings && buildSettings . PRODUCT_NAME === targetName ,
1616+ )
1617+ . map ( ( buildSettings : any ) => buildSettings . OTHER_LDFLAGS ) ;
1618+ } ;
1619+
1620+ const seedFlags = ( entries : string [ ] ) => {
1621+ const project = new xcode . project ( pbxProjPath ) ;
1622+ project . parseSync ( ) ;
1623+ const configurations = project . pbxXCBuildConfigurationSection ( ) ;
1624+ for ( const key of Object . keys ( configurations ) ) {
1625+ if ( / _ c o m m e n t $ / . test ( key ) ) {
1626+ continue ;
1627+ }
1628+ const buildSettings = configurations [ key ] . buildSettings ;
1629+ if ( buildSettings && buildSettings . PRODUCT_NAME === targetName ) {
1630+ buildSettings . OTHER_LDFLAGS = entries ;
1631+ }
1632+ }
1633+ require ( "fs" ) . writeFileSync ( pbxProjPath , project . writeSync ( { } ) ) ;
1634+ } ;
1635+
1636+ const prepare = ( ) =>
1637+ iOSProjectService . prepareProject (
1638+ testInjector . resolve ( "projectData" ) ,
1639+ < any > { } ,
1640+ ) ;
1641+
1642+ return { prepare, flagsOfTarget, seedFlags, projectName } ;
1643+ }
1644+
1645+ const metadataDirectives = ( flags : any ) : string [ ] =>
1646+ ( Array . isArray ( flags ) ? flags : [ flags ] )
1647+ . filter ( ( entry : any ) => typeof entry === "string" )
1648+ . filter ( ( entry : string ) => entry . includes ( "__TNSMetadata" ) ) ;
1649+
1650+ it ( "keeps a single metadata directive no matter how often embed runs" , async ( ) => {
1651+ const { prepare, flagsOfTarget, projectName } = setup ( ) ;
1652+
1653+ await prepare ( ) ;
1654+ await prepare ( ) ;
1655+ await prepare ( ) ;
1656+
1657+ const configurations = flagsOfTarget ( ) ;
1658+ assert . isNotEmpty ( configurations ) ;
1659+ for ( const flags of configurations ) {
1660+ const directives = metadataDirectives ( flags ) ;
1661+ assert . lengthOf (
1662+ directives ,
1663+ 1 ,
1664+ `expected one metadata directive, got ${ JSON . stringify ( flags ) } ` ,
1665+ ) ;
1666+ assert . include ( directives [ 0 ] , `${ projectName } /metadata-arm64.bin` ) ;
1667+ }
1668+ } ) ;
1669+
1670+ it ( "replaces duplicates left by earlier versions and keeps unrelated flags" , async ( ) => {
1671+ const stale = JSON . stringify (
1672+ '-sectcreate __DATA __TNSMetadata "../embedHost/metadata-arm64.bin"' ,
1673+ ) ;
1674+ const unrelated = JSON . stringify ( "-ObjC" ) ;
1675+ const { prepare, flagsOfTarget, seedFlags, projectName } = setup ( ) ;
1676+ seedFlags ( [ '"$(inherited)"' , unrelated , stale , stale ] ) ;
1677+
1678+ await prepare ( ) ;
1679+
1680+ for ( const flags of flagsOfTarget ( ) ) {
1681+ assert . lengthOf ( metadataDirectives ( flags ) , 1 ) ;
1682+ assert . include ( flags , unrelated ) ;
1683+ assert . include ( flags , '"$(inherited)"' ) ;
1684+ assert . include (
1685+ JSON . stringify ( flags ) ,
1686+ `${ projectName } /metadata-arm64.bin` ,
1687+ ) ;
1688+ }
1689+ } ) ;
1690+
1691+ it ( "does not add $(inherited) to a host that left it out" , async ( ) => {
1692+ const unrelated = JSON . stringify ( "-ObjC" ) ;
1693+ const { prepare, flagsOfTarget, seedFlags } = setup ( ) ;
1694+ seedFlags ( [ unrelated ] ) ;
1695+
1696+ await prepare ( ) ;
1697+
1698+ for ( const flags of flagsOfTarget ( ) ) {
1699+ assert . lengthOf ( metadataDirectives ( flags ) , 1 ) ;
1700+ assert . include ( flags , unrelated ) ;
1701+ assert . notInclude ( flags , '"$(inherited)"' ) ;
1702+ }
1703+ } ) ;
1704+
1705+ it ( "preserves repeated whitespace in unrelated flag arguments when stripping a directive" , async ( ) => {
1706+ const combined = JSON . stringify (
1707+ '-force_load "libs/My Library.a" -sectcreate __DATA __TNSMetadata "../embedHost/metadata-arm64.bin"' ,
1708+ ) ;
1709+ const { prepare, flagsOfTarget, seedFlags } = setup ( ) ;
1710+ seedFlags ( [ '"$(inherited)"' , combined ] ) ;
1711+
1712+ await prepare ( ) ;
1713+
1714+ for ( const flags of flagsOfTarget ( ) ) {
1715+ assert . lengthOf ( metadataDirectives ( flags ) , 1 ) ;
1716+ assert . include ( flags , JSON . stringify ( '-force_load "libs/My Library.a"' ) ) ;
1717+ }
1718+ } ) ;
1719+
1720+ it ( "reports a broken host project instead of hiding it at trace level" , async ( ) => {
1721+ const logged : any [ ] = [ ] ;
1722+ const testInjector = createTestInjector (
1723+ mkdtempSync ( path . join ( tmpdir ( ) , "embedMetadataBroken-" ) ) ,
1724+ projectName ,
1725+ xcode ,
1726+ ) ;
1727+ // Registered before the service resolves its logger.
1728+ testInjector . register ( "logger" , {
1729+ error : ( ...args : any [ ] ) => logged . push ( args ) ,
1730+ warn : ( ) : void => undefined ,
1731+ info : ( ) : void => undefined ,
1732+ debug : ( ) : void => undefined ,
1733+ trace : ( ...args : any [ ] ) => logged . push ( [ "trace" , ...args ] ) ,
1734+ } ) ;
1735+ const iOSProjectService : IOSProjectService =
1736+ testInjector . resolve ( "iOSProjectService" ) ;
1737+ ( < any > testInjector . resolve ( "options" ) ) . hostProjectPath = mkdtempSync (
1738+ path . join ( tmpdir ( ) , "embedMetadataBrokenHost-" ) ,
1739+ ) ;
1740+ // The host project's pbxproj exists but the group cannot be added to it.
1741+ testInjector . resolve ( "xcprojService" ) . findXcodeProject = ( ) =>
1742+ ( < any > testInjector . resolve ( "options" ) ) . hostProjectPath ;
1743+ ( < any > iOSProjectService ) . createPbxProj = ( ) : any => ( {
1744+ addPbxGroup : ( ) => {
1745+ throw new Error ( "host project is read-only" ) ;
1746+ } ,
1747+ pbxGroupByName : ( ) : any => null ,
1748+ } ) ;
1749+
1750+ await iOSProjectService . prepareProject (
1751+ testInjector . resolve ( "projectData" ) ,
1752+ < any > { } ,
1753+ ) ;
1754+
1755+ const reported = logged . filter ( ( entry ) => entry [ 0 ] !== "trace" ) ;
1756+ assert . isNotEmpty (
1757+ reported ,
1758+ `host-project setup failures must not stay at trace level, got ${ JSON . stringify ( logged ) } ` ,
1759+ ) ;
1760+ assert . include ( String ( reported [ 0 ] [ 0 ] ) , "host project" ) ;
1761+ } ) ;
1762+ } ) ;
1763+
15701764describe ( "SPM Packages" , ( ) => {
15711765 it ( "should add SPM packages to the project" , async ( ) => {
15721766 // todo: add tests for SPM packages
0 commit comments