forked from gnustep/libs-xcode
-
Notifications
You must be signed in to change notification settings - Fork 2
/
PBXNativeTarget.m
355 lines (302 loc) · 12.8 KB
/
PBXNativeTarget.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
#import <stdlib.h>
#import "PBXCommon.h"
#import "PBXNativeTarget.h"
#import "GSXCCommon.h"
#import "GSXCBuildContext.h"
@implementation PBXNativeTarget
- (void) dealloc
{
[productReference release];
[productInstallPath release];
[productType release];
[buildRules release];
[comments release];
[productSettingsXML release];
[super dealloc];
}
// Methods....
- (PBXFileReference *) productReference // getter
{
return productReference;
}
- (void) setProductReference: (PBXFileReference *)object; // setter
{
ASSIGN(productReference,object);
}
- (NSString *) productInstallPath // getter
{
return productInstallPath;
}
- (void) setProductInstallPath: (NSString *)object; // setter
{
ASSIGN(productInstallPath,object);
}
- (NSString *) productType // getter
{
return productType;
}
- (void) setProductType: (NSString *)object; // setter
{
ASSIGN(productType,object);
}
- (NSMutableArray *) buildRules // getter
{
return buildRules;
}
- (void) setBuildRules: (NSMutableArray *)object; // setter
{
ASSIGN(buildRules,object);
}
- (NSString *) productSettingsXML // getter
{
return productSettingsXML;
}
- (void) setProductSettingsXML: (NSString *)object // setter
{
ASSIGN(productSettingsXML,object);
}
- (void) _productWrapper
{
GSXCBuildContext *context = [GSXCBuildContext sharedBuildContext];
NSString *buildDir = [NSString stringWithCString: getenv("BUILT_PRODUCTS_DIR")];
NSString *uninstalledProductsDir = [buildDir stringByAppendingPathComponent: @"UninstalledProducts"];
NSString *fullPath = [[buildDir stringByAppendingPathComponent: @"UninstalledProducts"]
stringByAppendingPathComponent: [productReference path]];
NSError *error = nil;
// Create directories...
[[NSFileManager defaultManager] createDirectoryAtPath:uninstalledProductsDir
withIntermediateDirectories:YES
attributes:nil
error:&error];
/*
[[NSFileManager defaultManager] createDirectoryAtPath:buildDir
withIntermediateDirectories:YES
attributes:nil
error:&error];
*/
if([productType isEqualToString: BUNDLE_TYPE] ||
[productType isEqualToString: APPLICATION_TYPE])
{
NSString *execName = [[fullPath lastPathComponent] stringByDeletingPathExtension];
// Create directories...
[[NSFileManager defaultManager] createDirectoryAtPath:fullPath
withIntermediateDirectories:YES
attributes:nil
error:&error];
setenv("PRODUCT_OUTPUT_DIR",[fullPath cString],1);
setenv("PRODUCT_NAME",[execName cString],1);
setenv("EXECUTABLE_NAME",[execName cString],1);
}
else if([productType isEqualToString: FRAMEWORK_TYPE])
{
NSString *derivedSourceDir =
[[NSString stringWithCString: getenv("PROJECT_ROOT")]
stringByAppendingPathComponent: @"derived_src"];
NSString *execName = [[fullPath lastPathComponent] stringByDeletingPathExtension];
NSString *derivedSourceHeaderDir = [derivedSourceDir stringByAppendingPathComponent: execName];
NSString *frameworkVersion =
[NSString stringWithCString: getenv("FRAMEWORK_VERSION")];
[context setObject: [NSString stringWithString: fullPath]
forKey: @"FRAMEWORK_DIR"];
// Above "Versions"
NSString *headersLink = [fullPath stringByAppendingPathComponent: @"Headers"];
NSString *resourcesLink = [fullPath stringByAppendingPathComponent: @"Resources"];
// Below "Versions"
fullPath = [fullPath stringByAppendingPathComponent: @"Versions"];
NSString *currentLink = [fullPath stringByAppendingPathComponent: @"Current"];
fullPath = [fullPath stringByAppendingPathComponent: frameworkVersion];
NSString *headerDir = [fullPath stringByAppendingPathComponent: @"Headers"];
NSString *resourceDir = [fullPath stringByAppendingPathComponent: @"Resources"];
// Create directories...
[[NSFileManager defaultManager] createDirectoryAtPath:fullPath
withIntermediateDirectories:YES
attributes:nil
error:&error];
[[NSFileManager defaultManager] createDirectoryAtPath:derivedSourceHeaderDir
withIntermediateDirectories:YES
attributes:nil
error:&error];
[[NSFileManager defaultManager] createDirectoryAtPath:headerDir
withIntermediateDirectories:YES
attributes:nil
error:&error];
// Create links....
[[NSFileManager defaultManager] createSymbolicLinkAtPath: currentLink
pathContent: frameworkVersion];
[[NSFileManager defaultManager] createSymbolicLinkAtPath: headersLink
pathContent: @"Versions/Current/Headers"];
[[NSFileManager defaultManager] createSymbolicLinkAtPath: resourcesLink
pathContent: @"Versions/Current/Resources"];
// Things to pass on to the next phase...
[context setObject: headerDir forKey: @"HEADER_DIR"];
[context setObject: resourceDir forKey: @"RESOURCE_DIR"];
[context setObject: derivedSourceHeaderDir forKey: @"DERIVED_SOURCE_HEADER_DIR"];
[context setObject: derivedSourceDir forKey: @"DERIVED_SOURCE_DIR"];
setenv("PRODUCT_OUTPUT_DIR",[fullPath cString],1);
setenv("PRODUCT_NAME",[execName cString],1);
setenv("EXECUTABLE_NAME",[execName cString],1);
}
else if([productType isEqualToString: LIBRARY_TYPE])
{
// for non-bundled packages...
NSString *fileName = [fullPath lastPathComponent];
NSString *path = [fullPath stringByDeletingLastPathComponent];
setenv("PRODUCT_OUTPUT_DIR",[path cString],1);
setenv("PRODUCT_NAME",[fileName cString],1);
setenv("EXECUTABLE_NAME",[fileName cString],1);
NSString *derivedSourceDir =
[[NSString stringWithCString: getenv("PROJECT_ROOT")]
stringByAppendingPathComponent: @"derived_src"];
NSString *derivedSourceHeaderDir = derivedSourceDir; // stringByAppendingPathComponent: @"Headers"];
[context setObject: derivedSourceHeaderDir forKey: @"DERIVED_SOURCE_HEADER_DIR"];
[context setObject: derivedSourceDir forKey: @"DERIVED_SOURCE_DIR"];
[[NSFileManager defaultManager] createDirectoryAtPath:derivedSourceHeaderDir
withIntermediateDirectories:YES
attributes:nil
error:&error];
}
else
{
// for non-bundled packages...
NSString *fileName = [fullPath lastPathComponent];
NSString *path = [fullPath stringByDeletingLastPathComponent];
setenv("PRODUCT_OUTPUT_DIR",[path cString],1);
setenv("PRODUCT_NAME",[fileName cString],1);
setenv("EXECUTABLE_NAME",[fileName cString],1);
}
}
- (BOOL) build
{
BOOL result = YES;
NSEnumerator *en = nil;
GSXCBuildContext *context = [GSXCBuildContext sharedBuildContext];
puts([[NSString stringWithFormat: @"=== Building Target %@",name] cString]);
[buildConfigurationList applyDefaultConfiguration];
[context setObject: productType
forKey: @"PRODUCT_TYPE"];
puts([[NSString stringWithFormat: @"=== Checking Dependencies"] cString]);
id dependency = nil;
en = [dependencies objectEnumerator];
while((dependency = [en nextObject]) != nil && result)
{
result = [dependency build];
}
puts([[NSString stringWithFormat: @"=== Done."] cString]);
puts([[NSString stringWithFormat: @"=== Executing build phases..."] cString]);
[self _productWrapper];
id phase = nil;
en = [buildPhases objectEnumerator];
while((phase = [en nextObject]) != nil && result)
{
result = [phase build];
if(NO == result)
{
puts([[NSString stringWithFormat: @"*** Failed build phase: %@",phase] cString]);
}
}
puts([[NSString stringWithFormat: @"=== Done..."] cString]);
puts([[NSString stringWithFormat: @"=== Completed Executing Target %@", name] cString]);
return result;
}
- (BOOL) clean
{
puts([[NSString stringWithFormat: @"=== Cleaning Target %@",name] cString]);
NSString *buildDir = [NSString stringWithCString: getenv("BUILT_PRODUCTS_DIR")];
NSString *command = [NSString stringWithFormat: @"rm -rf %@",buildDir];
puts([[NSString stringWithFormat: @"Cleaning build directory"] cString]);
int result = system([command cString]);
if(result == 0)
{
if([[NSFileManager defaultManager] fileExistsAtPath: @"derived_src"])
{
command = @"rm -rf derived_src";
puts([[NSString stringWithFormat: @"Cleaning derived_src directory"] cString]);
result = system([command cString]);
}
}
puts([[NSString stringWithFormat: @"=== Completed Cleaning Target %@",name] cString]);
return (result == 0);
}
- (BOOL) install
{
puts([[NSString stringWithFormat: @"=== Installing Target %@",name] cString]);
NSString *buildDir = [NSString stringWithCString: getenv("BUILT_PRODUCTS_DIR")];
NSString *uninstalledProductsDir = [buildDir stringByAppendingPathComponent: @"UninstalledProducts"];
NSString *fullPath = [uninstalledProductsDir stringByAppendingPathComponent: [productReference path]];
NSString *installDir = [NSString stringWithCString: getenv("GNUSTEP_LOCAL_ROOT")]; // FIXME: Shouldn't always be local...
NSString *fileName = [fullPath lastPathComponent];
NSString *execName = [fileName stringByDeletingPathExtension];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error = nil;
if([productType isEqualToString: APPLICATION_TYPE])
{
NSString *installDest = [[installDir stringByAppendingPathComponent: @"Applications"] stringByAppendingPathComponent: fileName];
[fileManager copyItemAtPath: fullPath
toPath: installDest
error: &error];
}
else if([productType isEqualToString: FRAMEWORK_TYPE])
{
/*
NSString *frameworkVersion =
[NSString stringWithCString: getenv("FRAMEWORK_VERSION")];
*/
NSString *installDest = [[installDir stringByAppendingPathComponent: @"Library"] stringByAppendingPathComponent: @"Frameworks"];
NSString *productDir = [installDest stringByAppendingPathComponent: [productReference path]];
NSString *headersDir = [[installDir stringByAppendingPathComponent: @"Library"] stringByAppendingPathComponent: @"Headers"];
NSString *libsDir = [[installDir stringByAppendingPathComponent: @"Library"] stringByAppendingPathComponent: @"Libraries"];
NSString *frameworksLinkDir = [[[@"../Frameworks" stringByAppendingPathComponent: [productReference path]] stringByAppendingPathComponent:@"Versions"] stringByAppendingPathComponent:@"Current"];
NSString *headersLinkDir = [[[@"../Frameworks" stringByAppendingPathComponent: [productReference path]] stringByAppendingPathComponent:@"Versions"] stringByAppendingPathComponent:@"Current"];
// Copy
[fileManager removeItemAtPath: productDir error:NULL];
[fileManager copyItemAtPath: fullPath
toPath: productDir
error: &error];
// Create links...
[fileManager removeItemAtPath: [headersDir stringByAppendingPathComponent: execName]
error:NULL];
BOOL flag = [fileManager createSymbolicLinkAtPath: [headersDir stringByAppendingPathComponent: execName]
pathContent: [headersLinkDir stringByAppendingPathComponent: @"Headers"]];
if(!flag)
{
puts([[NSString stringWithFormat: @"Error creating symbolic link..."] cString]);
}
[fileManager removeItemAtPath: [libsDir stringByAppendingPathComponent:
[NSString stringWithFormat: @"lib%@.so",execName]]
error:NULL];
flag = [fileManager createSymbolicLinkAtPath: [libsDir stringByAppendingPathComponent:
[NSString stringWithFormat: @"lib%@.so",execName]]
pathContent: [frameworksLinkDir stringByAppendingPathComponent:
[NSString stringWithFormat: @"lib%@.so",execName]]];
if(!flag)
{
puts([[NSString stringWithFormat: @"Error creating symbolic link..."] cString]);
}
}
else if([productType isEqualToString: LIBRARY_TYPE])
{
NSString *headersDir = [[installDir stringByAppendingPathComponent: @"Library"] stringByAppendingPathComponent: @"Headers"];
NSString *libsDir = [[installDir stringByAppendingPathComponent: @"Library"] stringByAppendingPathComponent: @"Libraries"];
NSString *derivedSrcDir = @"derived_src";
NSString *derivedSrcHeaderDir = derivedSrcDir;
NSString *destPath = [libsDir stringByAppendingPathComponent: [productReference path]];
puts([[NSString stringWithFormat: @"\tCopy %@ -> %@",fullPath,destPath] cString]);
[fileManager copyItemAtPath: fullPath
toPath: destPath
error: &error];
NSEnumerator *en = [fileManager enumeratorAtPath: derivedSrcHeaderDir];
id file = nil;
while((file = [en nextObject]) != nil)
{
NSString *fileName = [file lastPathComponent];
NSString *destFile = [headersDir stringByAppendingPathComponent: fileName];
puts([[NSString stringWithFormat: @"\tCopy %@ -> %@",file,destFile] cString]);
[fileManager copyItemAtPath: file
toPath: destFile
error: &error];
}
}
puts([[NSString stringWithFormat: @"=== Completed Installing Target %@",name] cString]);
return YES;
}
@end