Debian Codegraph @

Tools/buildtool/ToolDelegate.m

raw
1 /* ToolDelegate.m
2 *
3 * Copyright (C) 2023 Free Software Foundation, Inc.
4 *
5 * Author: Gregory John Casamento <[email protected]>
6 * Date: 2023
7 *
8 * This file is part of GNUstep.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 3 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111
23 * USA.
24 */
25
26 #import <Foundation/Foundation.h>
27
28 #import <XCode/PBXCoder.h>
29 #import <XCode/PBXContainer.h>
30 #import <XCode/NSString+PBXAdditions.h>
31 #import <XCode/XCWorkspaceParser.h>
32 #import <XCode/XCWorkspace.h>
33
34 #import "ToolDelegate.h"
35 #import "ArgPair.h"
36
37 NSString *findProjectFilename(NSArray *projectDirEntries)
38 {
39 NSEnumerator *e = [projectDirEntries objectEnumerator];
40 NSString *fileName;
41
42 while ((fileName = [e nextObject]))
43 {
44 NSRange range = [fileName rangeOfString:@"._"];
45 if ([[fileName pathExtension] isEqual: @"xcodeproj"] && range.location == NSNotFound)
46 {
47 return [fileName stringByAppendingPathComponent: @"project.pbxproj"];
48 }
49 }
50
51 return nil;
52 }
53
54 NSString *findWorkspaceFilename(NSArray *projectDirEntries)
55 {
56 NSEnumerator *e = [projectDirEntries objectEnumerator];
57 NSString *fileName;
58
59 while ((fileName = [e nextObject]))
60 {
61 NSRange range = [fileName rangeOfString:@"._"];
62 if ([[fileName pathExtension] isEqual: @"xcworkspace"] && range.location == NSNotFound)
63 {
64 return [fileName stringByAppendingPathComponent: @"contents.xcworkspacedata"];
65 }
66 }
67
68 return nil;
69 }
70
71 NSString *resolveProjectName(BOOL *isProject)
72 {
73 NSString *fileName = nil;
74 NSFileManager *fileManager = [NSFileManager defaultManager];
75 NSString *projectDir = [fileManager currentDirectoryPath];
76 NSArray *projectDirEntries = [fileManager directoryContentsAtPath: projectDir];
77
78 fileName = findWorkspaceFilename(projectDirEntries);
79 if (fileName != nil)
80 {
81 *isProject = NO;
82 }
83 else
84 {
85 *isProject = YES;
86 fileName = findProjectFilename(projectDirEntries);
87 }
88
89 return fileName;
90 }
91
92 // ToolDelegate...
93 @implementation ToolDelegate
94
95 - (NSDictionary *) parseArguments
96 {
97 NSMutableDictionary *result = [NSMutableDictionary dictionary];
98 NSProcessInfo *pi = [NSProcessInfo processInfo];
99 NSMutableArray *args = [NSMutableArray arrayWithArray: [pi arguments]];
100 NSEnumerator *en = [args objectEnumerator];
101 id obj = nil;
102 BOOL parse_val = NO;
103 ArgPair *pair = AUTORELEASE([[ArgPair alloc] init]);
104
105 while ((obj = [en nextObject]) != nil)
106 {
107 if (parse_val)
108 {
109 [pair setValue: obj];
110 [result setObject: pair forKey: [pair argument]];
111 parse_val = NO;
112 continue;
113 }
114 else
115 {
116 pair = AUTORELEASE([[ArgPair alloc] init]);
117
118 if ([obj isEqualToString: @"-project"])
119 {
120 [pair setArgument: obj];
121 parse_val = YES;
122 }
123
124 if ([obj isEqualToString: @"-target"])
125 {
126 [pair setArgument: obj];
127 parse_val = YES;
128 }
129
130 if ([obj isEqualToString: @"-write"])
131 {
132 [pair setArgument: obj];
133 parse_val = YES;
134 }
135
136 if ([obj isEqualToString: @"-license"])
137 {
138 [pair setArgument: obj];
139 parse_val = NO;
140 }
141
142 if ([obj isEqualToString: @"build"])
143 {
144 [pair setArgument: obj];
145 parse_val = NO;
146 }
147
148 if ([obj isEqualToString: @"install"])
149 {
150 [pair setArgument: obj];
151 parse_val = NO;
152 }
153
154 if ([obj isEqualToString: @"clean"])
155 {
156 [pair setArgument: obj];
157 parse_val = NO;
158 }
159
160 if ([obj isEqualToString: @"generate"])
161 {
162 [pair setArgument: obj];
163 parse_val = YES;
164 }
165
166 if ([obj isEqualToString: @"link"])
167 {
168 [pair setArgument: obj];
169 parse_val = NO;
170 }
171
172 if ([obj isEqualToString: @"save"])
173 {
174 [pair setArgument: obj];
175 parse_val = YES;
176 }
177
178 // If there is no parameter for the argument, set it anyway...
179 if (parse_val == NO)
180 {
181 [result setObject: pair forKey: obj];
182 }
183 }
184 }
185
186 if (parse_val == YES)
187 {
188 [self postMessage: @"# Parameter is mandatory, non specified."];
189 return nil; // exit if no parameter...
190 }
191
192 return result;
193 }
194
195 - (void) process
196 {
197 NSString *file = nil;
198 NSString *fileName = nil;
199 NSDictionary *args = [self parseArguments];
200 ArgPair *opt = nil;
201 BOOL isProject = NO;
202 NSString *parameter = nil; // @"cmake";
203
204 // NSLog(@"args = %@", args);
205 // NSLog(@"file = %@", file);
206
207 // Get the file to write out to...
208 NSString *outputFile = nil;
209
210 if (args == nil)
211 {
212 return;
213 }
214
215 opt = [args objectForKey: @"-project"];
216 if (opt != nil)
217 {
218 file = [opt value];
219 }
220
221 if (file != nil)
222 {
223 NSString *ext = [file pathExtension];
224
225 if ([ext isEqualToString: @"xcworkspace"])
226 {
227 isProject = NO;
228 fileName = file;
229 }
230 else if ([ext isEqualToString: @"xcodeproj"])
231 {
232 isProject = YES;
233 fileName = file;
234 }
235
236 if (fileName != nil)
237 {
238 fileName = [fileName stringByAppendingPathComponent:
239 @"project.pbxproj"];
240 }
241 else
242 {
243 fileName = resolveProjectName(&isProject);
244 }
245 }
246 else
247 {
248 fileName = resolveProjectName(&isProject);
249 }
250
251 // If any of these other options is defined, then don't build because we are transforming data...
252 opt = [args objectForKey: @"-write"];
253 if (opt != nil)
254 {
255 outputFile = [opt value];
256 NSLog(@"outputFile = %@", outputFile);
257 }
258
259 if (opt == nil)
260 {
261 NSString *function = nil;
262
263 // Get the current function...
264 opt = [args objectForKey: @"build"];
265 if (opt != nil)
266 {
267 function = @"build";
268 }
269
270 opt = [args objectForKey: @"install"];
271 if (opt != nil)
272 {
273 function = @"install";
274 }
275
276 opt = [args objectForKey: @"clean"];
277 if (opt != nil)
278 {
279 function = @"clean";
280 }
281
282 opt = [args objectForKey: @"generate"];
283 if (opt != nil)
284 {
285 function = @"generate";
286 ASSIGN(parameter, [opt value]);
287 }
288
289 opt = [args objectForKey: @"link"];
290 if (opt != nil)
291 {
292 function = @"link";
293 }
294
295 opt = [args objectForKey: @"save"];
296 if (opt != nil)
297 {
298 function = @"save";
299 ASSIGN(parameter, [opt value]);
300 }
301
302 // if no function is specified, build is the default...
303 if (function == nil)
304 {
305 function = @"build";
306 }
307
308 // Execute..
309 if (function != nil)
310 {
311 NS_DURING
312 {
313 NSString *display = [function stringByCapitalizingFirstCharacter];
314 SEL operation = NSSelectorFromString(function);
315
316 if (fileName == nil)
317 {
318 fileName = resolveProjectName(&isProject);
319 }
320
321 if (isProject)
322 {
323 PBXCoder *coder = nil;
324 PBXContainer *container = nil;
325
326 // Unarchive...
327 coder = [[PBXCoder alloc] initWithContentsOfFile: fileName];
328 container = [coder unarchive];
329 [container setParameter: parameter];
330
331 [coder setDelegate: self];
332
333 // Build...
334 if ([container respondsToSelector: operation])
335 {
336 // build...
337 [self postMessage: @"\033[1;32m**\033[0m Start operation %@", display];
338 if ([container performSelector: operation])
339 {
340 [self postMessage: @"\033[1;32m**\033[0m %@ Succeeded", display];
341 }
342 else
343 {
344 [self postMessage: @"\033[1;31m**\033[0m %@ Failed", display];
345 }
346 }
347 else
348 {
349 [self postMessage: @"Unknown build operation \"%@\" for %@", display, container];
350 }
351 }
352 else
353 {
354 XCWorkspaceParser *p = [XCWorkspaceParser parseWorkspaceFile: fileName];
355 XCWorkspace *w = [p workspace];
356
357 if ([w respondsToSelector: operation])
358 {
359 [w performSelector: operation];
360 }
361 }
362 }
363 NS_HANDLER
364 {
365 NSLog(@"%@", localException);
366 }
367 NS_ENDHANDLER;
368 }
369 }
370 }
371
372 @end