@@ -3,6 +3,25 @@ import * as semver from "semver";
33import { injector } from "../../common/yok" ;
44import { IFileSystem } from "../../common/declarations" ;
55
6+ /**
7+ * Target types whose bundle carries its own Frameworks folder. App extensions
8+ * and frameworks only link: they load embedded frameworks from the bundle that
9+ * hosts them, and App Store validation rejects frameworks nested in an app
10+ * extension (ITMS-90206) or another framework. A watchOS 2 app keeps its code,
11+ * and so its frameworks, in its WatchKit extension.
12+ */
13+ const EMBEDDING_PRODUCT_TYPES = new Set ( [
14+ "com.apple.product-type.application" ,
15+ "com.apple.product-type.application.on-demand-install-capable" ,
16+ "com.apple.product-type.watchkit2-extension" ,
17+ ] ) ;
18+
19+ function canEmbedFrameworks ( target : any ) : boolean {
20+ return EMBEDDING_PRODUCT_TYPES . has (
21+ String ( target ?. productType ?? "" ) . replace ( / ^ " | " $ / g, "" ) ,
22+ ) ;
23+ }
24+
625/**
726 * Writes Swift Package references directly into an Xcode project's pbxproj.
827 *
@@ -17,6 +36,12 @@ import { IFileSystem } from "../../common/declarations";
1736 * 4. an entry in the target's Frameworks build phase, plus the target's
1837 * `packageProductDependencies`.
1938 *
39+ * A product listed in `embed` additionally gets a second `PBXBuildFile` in the
40+ * "Embed Frameworks" copy phase of each target that can embed
41+ * (`EMBEDDING_PRODUCT_TYPES`), which is how Xcode itself records a dynamic
42+ * package product added through "Frameworks, Libraries, and Embedded Content"
43+ * — linking alone leaves the framework out of the app bundle.
44+ *
2045 * Every entry is keyed by its pbxproj comment (e.g. `XCRemoteSwiftPackageReference
2146 * "Auth0"`), and an existing entry is updated in place rather than duplicated —
2247 * so applying the same set of packages repeatedly (which the CLI does on every
@@ -56,6 +81,8 @@ export class SPMPbxprojService implements ISPMPbxprojService {
5681 project . parseSync ( ) ;
5782
5883 let added = false ;
84+ const embeddedPackages = new Set < string > ( ) ;
85+ const linkOnlyTargets = new Map < string , string [ ] > ( ) ;
5986 for ( const { targetName, package : pkg } of assignments ) {
6087 const targetId = this . findTargetId ( project , targetName ) ;
6188 if ( ! targetId ) {
@@ -67,6 +94,25 @@ export class SPMPbxprojService implements ISPMPbxprojService {
6794
6895 if ( this . addPackageToTarget ( project , targetId , pkg , projectRoot ) ) {
6996 added = true ;
97+ if ( pkg . embed ?. length ) {
98+ const target = project . pbxNativeTargetSection ( ) [ targetId ] ;
99+ if ( canEmbedFrameworks ( target ) ) {
100+ embeddedPackages . add ( pkg . name ) ;
101+ } else {
102+ linkOnlyTargets . set ( pkg . name , [
103+ ...( linkOnlyTargets . get ( pkg . name ) ?? [ ] ) ,
104+ targetName ,
105+ ] ) ;
106+ }
107+ }
108+ }
109+ }
110+
111+ for ( const [ packageName , targetNames ] of linkOnlyTargets ) {
112+ if ( ! embeddedPackages . has ( packageName ) ) {
113+ this . $logger . warn (
114+ `SPM: package "${ packageName } " lists products in embed, but its targets (${ targetNames . join ( ", " ) } ) load frameworks from the app or WatchKit extension that hosts them. Add the package to that target too, or the products fail to load at launch.` ,
115+ ) ;
70116 }
71117 }
72118
@@ -200,6 +246,32 @@ export class SPMPbxprojService implements ISPMPbxprojService {
200246 comment : packageReferenceComment ,
201247 } ) ;
202248
249+ const embedLibs = new Set < string > ( ) ;
250+ for ( const lib of pkg . embed ?? [ ] ) {
251+ if ( pkg . libs ?. includes ( lib ) ) {
252+ embedLibs . add ( lib ) ;
253+ } else {
254+ this . $logger . warn (
255+ `SPM: package "${ pkg . name } " lists "${ lib } " in embed but not in libs — only linked products can be embedded; skipping.` ,
256+ ) ;
257+ }
258+ }
259+ if ( embedLibs . size && ! canEmbedFrameworks ( target ) ) {
260+ this . $logger . trace (
261+ `SPM: linking ${ [ ...embedLibs ] . join ( ", " ) } into ${ target . name } without embedding; it loads them from the bundle that hosts it.` ,
262+ ) ;
263+ embedLibs . clear ( ) ;
264+ }
265+ let embedBuildPhaseFiles : any [ ] | null = null ;
266+ if ( embedLibs . size ) {
267+ const embedBuildPhaseObj = this . findOrCreateEmbedFrameworksBuildPhase (
268+ project ,
269+ target ,
270+ targetId ,
271+ ) ;
272+ embedBuildPhaseFiles = embedBuildPhaseObj [ "files" ] ??= [ ] ;
273+ }
274+
203275 for ( const lib of pkg . libs ?? [ ] ) {
204276 // The comment is just the product name, which two different packages
205277 // can share (e.g. both exposing a "Core" lib) — so entries here are
@@ -245,6 +317,35 @@ export class SPMPbxprojService implements ISPMPbxprojService {
245317 value : spmBuildFileUuid ,
246318 comment : libComment ,
247319 } ) ;
320+
321+ if ( ! embedLibs . has ( lib ) ) {
322+ this . removeEmbedEntries (
323+ project ,
324+ target ,
325+ spmProductDependencyUUID ,
326+ `${ lib } in Embed Frameworks` ,
327+ ) ;
328+ continue ;
329+ }
330+
331+ const embedComment = `${ lib } in Embed Frameworks` ;
332+ const { uuid : spmEmbedBuildFileUuid } = this . addOrUpdateEntry (
333+ project ,
334+ "PBXBuildFile" ,
335+ embedComment ,
336+ {
337+ isa : "PBXBuildFile" ,
338+ productRef : spmProductDependencyUUID ,
339+ productRef_comment : lib ,
340+ settings : { ATTRIBUTES : [ "CodeSignOnCopy" ] } ,
341+ } ,
342+ ( existing ) => existing . productRef === spmProductDependencyUUID ,
343+ ) ;
344+
345+ this . addOrUpdateArrayEntry ( embedBuildPhaseFiles , spmEmbedBuildFileUuid , {
346+ value : spmEmbedBuildFileUuid ,
347+ comment : embedComment ,
348+ } ) ;
248349 }
249350
250351 return true ;
@@ -263,6 +364,87 @@ export class SPMPbxprojService implements ISPMPbxprojService {
263364 return null ;
264365 }
265366
367+ /**
368+ * Drops the embed entry this service wrote for a product from this
369+ * target's Embed Frameworks phases, and its build file once no other copy
370+ * phase lists it. Copy entries written by anyone else are left alone.
371+ */
372+ private removeEmbedEntries (
373+ project : any ,
374+ target : any ,
375+ productRef : string ,
376+ embedComment : string ,
377+ ) : void {
378+ const buildFiles = project . hash . project . objects [ "PBXBuildFile" ] ?? { } ;
379+ const copyPhases =
380+ project . hash . project . objects [ "PBXCopyFilesBuildPhase" ] ?? { } ;
381+ const isProductFile = ( file : any ) =>
382+ buildFiles [ file . value ] ?. productRef === productRef &&
383+ buildFiles [ `${ file . value } _comment` ] === embedComment ;
384+
385+ const removed = new Set < string > ( ) ;
386+ for ( const phase of target . buildPhases ?? [ ] ) {
387+ const phaseObj = copyPhases [ phase . value ] ;
388+ if ( ! phaseObj ?. files || String ( phaseObj . dstSubfolderSpec ) !== "10" ) {
389+ continue ;
390+ }
391+ for ( const file of phaseObj . files . filter ( isProductFile ) ) {
392+ removed . add ( file . value ) ;
393+ }
394+ phaseObj . files = phaseObj . files . filter (
395+ ( file : any ) => ! isProductFile ( file ) ,
396+ ) ;
397+ }
398+
399+ const stillListed = new Set (
400+ Object . keys ( copyPhases )
401+ . filter ( ( key ) => ! key . endsWith ( "_comment" ) )
402+ . flatMap ( ( key ) => copyPhases [ key ] . files ?? [ ] )
403+ . map ( ( file : any ) => file . value ) ,
404+ ) ;
405+ for ( const uuid of removed ) {
406+ if ( ! stillListed . has ( uuid ) ) {
407+ delete buildFiles [ uuid ] ;
408+ delete buildFiles [ `${ uuid } _comment` ] ;
409+ }
410+ }
411+ }
412+
413+ /**
414+ * Finds the "Embed Frameworks" copy phase listed in this target's own
415+ * buildPhases, creating one when the target has none. Matched on the
416+ * destination (`dstSubfolderSpec` 10 is the bundle's Frameworks folder)
417+ * rather than the name alone, so a copy phase that merely shares the name
418+ * but copies elsewhere is not mistaken for it.
419+ */
420+ private findOrCreateEmbedFrameworksBuildPhase (
421+ project : any ,
422+ target : any ,
423+ targetId : string ,
424+ ) : any {
425+ const section =
426+ project . hash . project . objects [ "PBXCopyFilesBuildPhase" ] ?? { } ;
427+ for ( const phase of target . buildPhases ?? [ ] ) {
428+ const phaseObj = section [ phase . value ] ;
429+ if (
430+ phaseObj &&
431+ String ( phaseObj . dstSubfolderSpec ) === "10" &&
432+ ( phaseObj . name === "Embed Frameworks" ||
433+ phaseObj . name === '"Embed Frameworks"' )
434+ ) {
435+ return phaseObj ;
436+ }
437+ }
438+
439+ return project . addBuildPhase (
440+ [ ] ,
441+ "PBXCopyFilesBuildPhase" ,
442+ "Embed Frameworks" ,
443+ targetId ,
444+ "frameworks" ,
445+ ) . buildPhase ;
446+ }
447+
266448 /** Replaces a matching array entry in place, or appends it. */
267449 private addOrUpdateArrayEntry (
268450 array : any [ ] ,
0 commit comments