| 1 |
/* |
| 2 |
Copyright (C) 2018, 2019, 2020, 2021 Free Software Foundation, Inc. |
| 3 |
|
| 4 |
Written by: Gregory John Casamento <[email protected]> |
| 5 |
Date: 2022 |
| 6 |
|
| 7 |
This file is part of the GNUstep XCode Library |
| 8 |
|
| 9 |
This library is free software; you can redistribute it and/or |
| 10 |
modify it under the terms of the GNU Lesser General Public |
| 11 |
License as published by the Free Software Foundation; either |
| 12 |
version 2 of the License, or (at your option) any later version. |
| 13 |
|
| 14 |
This library is distributed in the hope that it will be useful, |
| 15 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 |
Lesser General Public License for more details. |
| 18 |
|
| 19 |
You should have received a copy of the GNU Lesser General Public |
| 20 |
License along with this library; if not, write to the Free |
| 21 |
Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 22 |
Boston, MA 02110 USA. |
| 23 |
*/ |
| 24 |
|
| 25 |
#import "XCWorkspace.h" |
| 26 |
#import "XCFileRef.h" |
| 27 |
#import "PBXNativeTarget.h" |
| 28 |
#import "GSXCCommon.h" |
| 29 |
|
| 30 |
#import <Foundation/NSString.h> |
| 31 |
#import <Foundation/NSArray.h> |
| 32 |
#import <Foundation/NSEnumerator.h> |
| 33 |
#import <Foundation/NSAutoreleasePool.h> |
| 34 |
#import <Foundation/NSXMLDocument.h> |
| 35 |
#import <Foundation/NSFileManager.h> |
| 36 |
|
| 37 |
@implementation XCWorkspace |
| 38 |
|
| 39 |
+ (instancetype) workspace |
| 40 |
{ |
| 41 |
return AUTORELEASE([[self alloc] init]); |
| 42 |
} |
| 43 |
|
| 44 |
- (instancetype) init |
| 45 |
{ |
| 46 |
self = [super init]; |
| 47 |
|
| 48 |
if (self != nil) |
| 49 |
{ |
| 50 |
[self setFileRefs: [NSArray array]]; |
| 51 |
[self setVersion: nil]; |
| 52 |
} |
| 53 |
|
| 54 |
return self; |
| 55 |
} |
| 56 |
|
| 57 |
- (NSString *) version |
| 58 |
{ |
| 59 |
return _version; |
| 60 |
} |
| 61 |
|
| 62 |
- (void) setVersion: (NSString *)v |
| 63 |
{ |
| 64 |
ASSIGN(_version, v); |
| 65 |
} |
| 66 |
|
| 67 |
- (NSArray *) fileRefs |
| 68 |
{ |
| 69 |
return _fileRefs; |
| 70 |
} |
| 71 |
|
| 72 |
- (void) setFileRefs: (NSArray *)refs |
| 73 |
{ |
| 74 |
ASSIGN(_fileRefs, refs); |
| 75 |
} |
| 76 |
|
| 77 |
- (NSString *) filename |
| 78 |
{ |
| 79 |
return _filename; |
| 80 |
} |
| 81 |
|
| 82 |
- (void) setFilename: (NSString *)filename |
| 83 |
{ |
| 84 |
ASSIGN(_filename, filename); |
| 85 |
} |
| 86 |
|
| 87 |
- (NSString *) _collectWorkspaceLinkDirs |
| 88 |
{ |
| 89 |
NSString *result = @""; |
| 90 |
NSEnumerator *en = [_fileRefs reverseObjectEnumerator]; |
| 91 |
XCFileRef *ref = nil; |
| 92 |
|
| 93 |
while ((ref = [en nextObject]) != nil) |
| 94 |
{ |
| 95 |
NSString *loc = [ref location]; |
| 96 |
NSString *projRoot = [loc stringByDeletingLastPathComponent]; |
| 97 |
NSString *buildDir = [projRoot stringByAppendingPathComponent: @"build"]; |
| 98 |
|
| 99 |
NSArray *targets = [ref targets]; |
| 100 |
NSEnumerator *ten = [targets objectEnumerator]; |
| 101 |
PBXNativeTarget *t = nil; |
| 102 |
|
| 103 |
while ((t = [ten nextObject]) != nil) |
| 104 |
{ |
| 105 |
NSString *name = [t productName]; |
| 106 |
NSString *targetDir = [[buildDir stringByAppendingPathComponent: name] |
| 107 |
stringByReplacingOccurrencesOfString: @"group:" withString: @""]; |
| 108 |
NSString *prodDir = [targetDir stringByAppendingPathComponent: @"Products"]; |
| 109 |
result = [result stringByAppendingFormat: @" -L./%@ ", prodDir]; |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
return result; |
| 114 |
} |
| 115 |
|
| 116 |
- (NSString *) _collectWorkspaceLinkLibs |
| 117 |
{ |
| 118 |
NSString *result = @""; |
| 119 |
NSEnumerator *en = [_fileRefs reverseObjectEnumerator]; |
| 120 |
XCFileRef *ref = nil; |
| 121 |
|
| 122 |
while ((ref = [en nextObject]) != nil) |
| 123 |
{ |
| 124 |
NSArray *targets = [ref targets]; |
| 125 |
NSEnumerator *ten = [targets objectEnumerator]; |
| 126 |
PBXNativeTarget *t = nil; |
| 127 |
|
| 128 |
while ((t = [ten nextObject]) != nil) |
| 129 |
{ |
| 130 |
NSString *type = [t productType]; |
| 131 |
|
| 132 |
if ([type isEqualToString: LIBRARY_TYPE] |
| 133 |
|| [type isEqualToString: DYNAMIC_LIBRARY_TYPE] |
| 134 |
|| [type isEqualToString: FRAMEWORK_TYPE]) |
| 135 |
{ |
| 136 |
NSString *name = [t productName]; |
| 137 |
NSString *libName = [name stringByReplacingOccurrencesOfString: @"group:" withString: @""]; |
| 138 |
result = [result stringByAppendingFormat: @" -l%@ ", libName]; |
| 139 |
} |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
return result; |
| 144 |
} |
| 145 |
|
| 146 |
- (NSString *) _collectWorkspaceIncludeDirs |
| 147 |
{ |
| 148 |
NSString *result = @""; |
| 149 |
NSEnumerator *en = [_fileRefs reverseObjectEnumerator]; |
| 150 |
XCFileRef *ref = nil; |
| 151 |
|
| 152 |
while ((ref = [en nextObject]) != nil) |
| 153 |
{ |
| 154 |
NSString *loc = [ref location]; |
| 155 |
NSString *projRoot = [loc stringByDeletingLastPathComponent]; |
| 156 |
|
| 157 |
NSArray *targets = [ref targets]; |
| 158 |
NSEnumerator *ten = [targets objectEnumerator]; |
| 159 |
PBXNativeTarget *t = nil; |
| 160 |
|
| 161 |
while ((t = [ten nextObject]) != nil) |
| 162 |
{ |
| 163 |
NSString *targetDir = [projRoot stringByReplacingOccurrencesOfString: @"group:" withString: @""]; |
| 164 |
result = [result stringByAppendingFormat: @" -I./%@ ", targetDir]; |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
return result; |
| 169 |
} |
| 170 |
|
| 171 |
- (BOOL) build |
| 172 |
{ |
| 173 |
NSAutoreleasePool *p = [[NSAutoreleasePool alloc] init]; // start pool for workspace |
| 174 |
NSEnumerator *en = [_fileRefs reverseObjectEnumerator]; |
| 175 |
XCFileRef *ref = nil; |
| 176 |
NSString *display = [[self filename] |
| 177 |
stringByDeletingLastPathComponent]; |
| 178 |
NSString *workspaceLinkDirs = [self _collectWorkspaceLinkDirs]; |
| 179 |
NSString *workspaceIncDirs = [self _collectWorkspaceIncludeDirs]; |
| 180 |
NSString *workspaceLinkLibs = [self _collectWorkspaceLinkLibs]; |
| 181 |
|
| 182 |
NSDebugLog(@"\n\n\nWorkspace Include Dirs = %@\n\n\n", workspaceIncDirs); |
| 183 |
NSDebugLog(@"\n\n\nWorkspace Link Libs = %@\n\n\n", workspaceLinkLibs); |
| 184 |
printf("+++ Building projects workspace.. %s\n", [display cString]); |
| 185 |
|
| 186 |
while ((ref = [en nextObject]) != nil) |
| 187 |
{ |
| 188 |
[ref setWorkspaceIncludes: workspaceIncDirs]; |
| 189 |
[ref setWorkspaceLink: workspaceLinkDirs]; |
| 190 |
[ref setWorkspaceLibs: workspaceLinkLibs]; |
| 191 |
BOOL s = [ref build]; |
| 192 |
if (s == NO) |
| 193 |
{ |
| 194 |
printf("+++ Workspace build FAILED %s\n", [display cString]); |
| 195 |
return NO; |
| 196 |
} |
| 197 |
} |
| 198 |
printf("+++ Workspace build completed... %s\n", [display cString]); |
| 199 |
|
| 200 |
RELEASE(p); |
| 201 |
|
| 202 |
return YES; |
| 203 |
} |
| 204 |
|
| 205 |
- (BOOL) clean |
| 206 |
{ |
| 207 |
NSEnumerator *en = [_fileRefs reverseObjectEnumerator]; |
| 208 |
XCFileRef *ref = nil; |
| 209 |
|
| 210 |
printf("+++ Cleaning projects in workspace.. %s\n", [[[self filename] stringByDeletingLastPathComponent] cString]); |
| 211 |
while ((ref = [en nextObject]) != nil) |
| 212 |
{ |
| 213 |
BOOL s = [ref clean]; |
| 214 |
if (s == NO) |
| 215 |
{ |
| 216 |
return NO; |
| 217 |
} |
| 218 |
} |
| 219 |
printf("+++ Workspace clean complete\n"); |
| 220 |
|
| 221 |
return YES; |
| 222 |
} |
| 223 |
|
| 224 |
- (BOOL) install |
| 225 |
{ |
| 226 |
NSEnumerator *en = [_fileRefs reverseObjectEnumerator]; |
| 227 |
XCFileRef *ref = nil; |
| 228 |
|
| 229 |
printf("+++ Installing projects in workspace.. %s\n", |
| 230 |
[[[self filename] stringByDeletingLastPathComponent] cString]); |
| 231 |
while ((ref = [en nextObject]) != nil) |
| 232 |
{ |
| 233 |
BOOL s = [ref install]; |
| 234 |
if (s == NO) |
| 235 |
{ |
| 236 |
return NO; |
| 237 |
} |
| 238 |
} |
| 239 |
printf("+++ Workspace install complete\n"); |
| 240 |
|
| 241 |
return YES; |
| 242 |
} |
| 243 |
|
| 244 |
- (BOOL) link |
| 245 |
{ |
| 246 |
NSEnumerator *en = [_fileRefs reverseObjectEnumerator]; |
| 247 |
XCFileRef *ref = nil; |
| 248 |
NSString *display = [[self filename] |
| 249 |
stringByDeletingLastPathComponent]; |
| 250 |
|
| 251 |
printf("+++ Building projects workspace.. %s\n", [display cString]); |
| 252 |
while ((ref = [en nextObject]) != nil) |
| 253 |
{ |
| 254 |
NSAutoreleasePool *p = [[NSAutoreleasePool alloc] init]; |
| 255 |
BOOL s = [ref link]; |
| 256 |
if (s == NO) |
| 257 |
{ |
| 258 |
printf("+++ Workspace build FAILED %s\n", [display cString]); |
| 259 |
return NO; |
| 260 |
} |
| 261 |
RELEASE(p); |
| 262 |
} |
| 263 |
printf("+++ Workspace build completed... %s\n", [display cString]); |
| 264 |
|
| 265 |
return YES; |
| 266 |
} |
| 267 |
|
| 268 |
@end |