0
# Target Management
1
2
Build target management including creating new targets, managing target dependencies, and configuring target-specific build settings for iOS, macOS, watchOS, and extension targets.
3
4
## Capabilities
5
6
### Target Creation
7
8
Create new build targets with automatic configuration and integration.
9
10
```javascript { .api }
11
/**
12
* Add new build target to project
13
* Creates target with default Debug/Release configurations and proper setup
14
* @param {string} name - Target name
15
* @param {string} type - Target type (application, app_extension, framework, etc.)
16
* @param {string} subfolder - Target subfolder for organization
17
* @param {string} bundleId - Bundle identifier for the target
18
* @returns {object} Target object with UUID and configuration
19
*/
20
addTarget(name, type, subfolder, bundleId);
21
```
22
23
**Usage Examples:**
24
25
```javascript
26
// Create iOS app target
27
const appTarget = proj.addTarget('MyApp', 'application', 'MyApp', 'com.company.myapp');
28
console.log('Created app target:', appTarget.uuid);
29
30
// Create app extension target
31
const extensionTarget = proj.addTarget(
32
'MyExtension',
33
'app_extension',
34
'MyExtension',
35
'com.company.myapp.extension'
36
);
37
38
// Create framework target
39
const frameworkTarget = proj.addTarget('MyFramework', 'framework', 'MyFramework');
40
41
// Create watch app target
42
const watchTarget = proj.addTarget(
43
'MyWatchApp',
44
'watch2_app',
45
'MyWatchApp',
46
'com.company.myapp.watchkitapp'
47
);
48
49
// Create static library target
50
const libTarget = proj.addTarget('MyLibrary', 'static_library', 'MyLibrary');
51
```
52
53
### Target Dependencies
54
55
Manage dependencies between targets for proper build ordering.
56
57
```javascript { .api }
58
/**
59
* Add target dependency relationship
60
* Ensures dependent targets are built before the main target
61
* @param {string} target - Target UUID that depends on others
62
* @param {string[]} dependencyTargets - Array of dependency target UUIDs
63
* @returns {object} Target object with updated dependencies
64
*/
65
addTargetDependency(target, dependencyTargets);
66
```
67
68
**Usage Examples:**
69
70
```javascript
71
// Make app depend on framework
72
const appTargetUuid = proj.getFirstTarget().uuid;
73
const frameworkTargetUuid = frameworkTarget.uuid;
74
75
proj.addTargetDependency(appTargetUuid, [frameworkTargetUuid]);
76
77
// Make watch extension depend on watch app
78
const watchExtensionTarget = proj.addTarget(
79
'MyWatchExtension',
80
'watch2_extension',
81
'MyWatchExtension'
82
);
83
84
proj.addTargetDependency(watchTarget.uuid, [watchExtensionTarget.uuid]);
85
86
// Multiple dependencies
87
const testTarget = proj.addTarget('MyTests', 'unit_test_bundle', 'MyTests');
88
proj.addTargetDependency(testTarget.uuid, [appTargetUuid, frameworkTargetUuid]);
89
```
90
91
### Target Access and Search
92
93
Find and access existing targets in the project.
94
95
```javascript { .api }
96
/**
97
* Get first target in project
98
* @returns {object} Target object with UUID and target data
99
*/
100
getFirstTarget();
101
102
/**
103
* Find target by product type
104
* @param {string} productType - Product type to search for
105
* @returns {object|null} Target object or null if not found
106
*/
107
getTarget(productType);
108
109
/**
110
* Find target by name
111
* @param {string} name - Target name to search for
112
* @returns {object|null} Target object or null if not found
113
*/
114
pbxTargetByName(name);
115
116
/**
117
* Find target UUID by name
118
* @param {string} name - Target name to search for
119
* @returns {string|null} Target UUID or null if not found
120
*/
121
findTargetKey(name);
122
```
123
124
**Usage Examples:**
125
126
```javascript
127
// Get main app target
128
const mainTarget = proj.getFirstTarget();
129
console.log('Main target name:', mainTarget.firstTarget.name);
130
131
// Find target by name
132
const myTarget = proj.pbxTargetByName('MyFramework');
133
if (myTarget) {
134
console.log('Found target UUID:', proj.findTargetKey('MyFramework'));
135
}
136
137
// Find target by product type
138
const appTarget = proj.getTarget('com.apple.product-type.application');
139
const frameworkTarget = proj.getTarget('com.apple.product-type.framework');
140
```
141
142
### Target Attributes
143
144
Manage target-specific attributes and capabilities.
145
146
```javascript { .api }
147
/**
148
* Add target attribute (used for capabilities, team settings, etc.)
149
* @param {string} prop - Attribute property name
150
* @param {any} value - Attribute value
151
* @param {object} target - Target object (optional, defaults to first target)
152
*/
153
addTargetAttribute(prop, value, target);
154
155
/**
156
* Remove target attribute
157
* @param {string} prop - Attribute property name to remove
158
* @param {object} target - Target object (optional, defaults to first target)
159
*/
160
removeTargetAttribute(prop, target);
161
```
162
163
**Usage Examples:**
164
165
```javascript
166
// Add development team
167
proj.addTargetAttribute('DevelopmentTeam', 'ABCD123456');
168
169
// Add target-specific attribute
170
const extensionTarget = proj.pbxTargetByName('MyExtension');
171
proj.addTargetAttribute('SystemCapabilities', {
172
'com.apple.ApplicationGroups.iOS': { enabled: 1 }
173
}, extensionTarget);
174
175
// Add provisioning profile
176
proj.addTargetAttribute('ProvisioningStyle', 'Manual');
177
proj.addTargetAttribute('ProvisioningProfile', 'profile-uuid-here');
178
179
// Remove attribute
180
proj.removeTargetAttribute('DevelopmentTeam');
181
```
182
183
### Supported Target Types
184
185
```javascript { .api }
186
/**
187
* Supported target types and their product types
188
*/
189
const TARGET_TYPES = {
190
'application': 'com.apple.product-type.application',
191
'app_extension': 'com.apple.product-type.app-extension',
192
'bundle': 'com.apple.product-type.bundle',
193
'command_line_tool': 'com.apple.product-type.tool',
194
'dynamic_library': 'com.apple.product-type.library.dynamic',
195
'framework': 'com.apple.product-type.framework',
196
'static_library': 'com.apple.product-type.library.static',
197
'unit_test_bundle': 'com.apple.product-type.bundle.unit-test',
198
'watch_app': 'com.apple.product-type.application.watchapp',
199
'watch2_app': 'com.apple.product-type.application.watchapp2',
200
'watch_extension': 'com.apple.product-type.watchkit-extension',
201
'watch2_extension': 'com.apple.product-type.watchkit2-extension'
202
};
203
```
204
205
### Target Structure Types
206
207
```javascript { .api }
208
/**
209
* Target object structure
210
*/
211
interface Target {
212
/** Target UUID */
213
uuid: string;
214
215
/** PBXNativeTarget configuration */
216
pbxNativeTarget: {
217
/** Target type */
218
isa: 'PBXNativeTarget';
219
220
/** Target name */
221
name: string;
222
223
/** Product name */
224
productName: string;
225
226
/** Product reference UUID */
227
productReference: string;
228
229
/** Product type */
230
productType: string;
231
232
/** Build configuration list UUID */
233
buildConfigurationList: string;
234
235
/** Build phases array */
236
buildPhases: BuildPhaseReference[];
237
238
/** Build rules array */
239
buildRules: any[];
240
241
/** Dependencies array */
242
dependencies: DependencyReference[];
243
};
244
}
245
246
interface BuildPhaseReference {
247
/** Build phase UUID */
248
value: string;
249
250
/** Build phase type comment */
251
comment: string;
252
}
253
254
interface DependencyReference {
255
/** Dependency UUID */
256
value: string;
257
258
/** Dependency type comment */
259
comment: string;
260
}
261
```
262
263
### Advanced Target Patterns
264
265
**iOS App with Extension:**
266
```javascript
267
// Create main app
268
const mainApp = proj.addTarget(
269
'MyApp',
270
'application',
271
'MyApp',
272
'com.company.myapp'
273
);
274
275
// Create share extension
276
const shareExtension = proj.addTarget(
277
'ShareExtension',
278
'app_extension',
279
'ShareExtension',
280
'com.company.myapp.share'
281
);
282
283
// Set up dependency (app embeds extension)
284
proj.addTargetDependency(mainApp.uuid, [shareExtension.uuid]);
285
286
// Configure extension attributes
287
proj.addTargetAttribute('SystemCapabilities', {
288
'com.apple.ApplicationGroups.iOS': { enabled: 1 }
289
}, shareExtension);
290
```
291
292
**Framework with Tests:**
293
```javascript
294
// Create framework
295
const framework = proj.addTarget(
296
'MyFramework',
297
'framework',
298
'MyFramework',
299
'com.company.myframework'
300
);
301
302
// Create test bundle
303
const tests = proj.addTarget(
304
'MyFrameworkTests',
305
'unit_test_bundle',
306
'MyFrameworkTests',
307
'com.company.myframework.tests'
308
);
309
310
// Tests depend on framework
311
proj.addTargetDependency(tests.uuid, [framework.uuid]);
312
313
// Add test host if needed
314
proj.addTargetAttribute('TEST_HOST', '$(BUILT_PRODUCTS_DIR)/MyApp.app/MyApp', tests);
315
```
316
317
**watchOS App Setup:**
318
```javascript
319
// Create watch app
320
const watchApp = proj.addTarget(
321
'MyWatchApp',
322
'watch2_app',
323
'MyWatchApp',
324
'com.company.myapp.watchkitapp'
325
);
326
327
// Create watch extension
328
const watchExtension = proj.addTarget(
329
'MyWatchExtension',
330
'watch2_extension',
331
'MyWatchExtension',
332
'com.company.myapp.watchkitapp.watchkitextension'
333
);
334
335
// Set up dependencies
336
const mainApp = proj.getFirstTarget();
337
proj.addTargetDependency(mainApp.uuid, [watchApp.uuid]);
338
proj.addTargetDependency(watchApp.uuid, [watchExtension.uuid]);
339
340
// Configure watch-specific attributes
341
proj.addTargetAttribute('WATCHOS_DEPLOYMENT_TARGET', '6.0', watchApp);
342
proj.addTargetAttribute('WATCHOS_DEPLOYMENT_TARGET', '6.0', watchExtension);
343
```
344
345
**Multi-Platform Framework:**
346
```javascript
347
// Create iOS framework
348
const iosFramework = proj.addTarget(
349
'MyFramework-iOS',
350
'framework',
351
'MyFramework-iOS'
352
);
353
354
// Create macOS framework
355
const macosFramework = proj.addTarget(
356
'MyFramework-macOS',
357
'framework',
358
'MyFramework-macOS'
359
);
360
361
// Configure platform-specific settings
362
proj.addTargetAttribute('SUPPORTED_PLATFORMS', 'iphoneos iphonesimulator', iosFramework);
363
proj.addTargetAttribute('SUPPORTED_PLATFORMS', 'macosx', macosFramework);
364
```
365
366
### Target Configuration Examples
367
368
**Development Team Setup:**
369
```javascript
370
// Add development team to all targets
371
const targets = [
372
proj.getFirstTarget(),
373
proj.pbxTargetByName('MyExtension'),
374
proj.pbxTargetByName('MyFramework')
375
];
376
377
targets.forEach(target => {
378
if (target) {
379
proj.addTargetAttribute('DevelopmentTeam', 'TEAM123456', target);
380
proj.addTargetAttribute('ProvisioningStyle', 'Automatic', target);
381
}
382
});
383
```
384
385
**Capability Configuration:**
386
```javascript
387
// Enable app groups for main app and extension
388
const capabilities = {
389
'com.apple.ApplicationGroups.iOS': { enabled: 1 },
390
'com.apple.BackgroundModes': { enabled: 1 },
391
'com.apple.Push': { enabled: 1 }
392
};
393
394
proj.addTargetAttribute('SystemCapabilities', capabilities);
395
```