Debian Codegraph @

XCode/PBXShellScriptBuildPhase.m

raw
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 <Foundation/Foundation.h>
26
27 #import "PBXCommon.h"
28 #import "PBXShellScriptBuildPhase.h"
29 #import "NSString+PBXAdditions.h"
30
31 #import "xcsystem.h"
32
33 extern char **environ;
34
35 @implementation PBXShellScriptBuildPhase
36
37 - (void) dealloc
38 {
39 RELEASE(_shellPath);
40 RELEASE(_shellScript);
41 RELEASE(_inputPaths);
42 RELEASE(_outputPaths);
43 RELEASE(_inputFileListPaths);
44 RELEASE(_outputFileListPaths);
45
46 [super dealloc];
47 }
48
49 // Methods....
50 - (NSMutableArray *) inputFileListPaths
51 {
52 return _inputFileListPaths;
53 }
54
55 - (void) setInputFileListPaths: (NSMutableArray *)object
56 {
57 ASSIGN(_inputFileListPaths, object);
58 }
59
60 - (NSMutableArray *) outputFileListPaths
61 {
62 return _outputFileListPaths;
63 }
64
65 - (void) setOutputFileListPaths: (NSMutableArray *)object
66 {
67 ASSIGN(_outputFileListPaths, object);
68 }
69
70 - (NSString *) shellPath // getter
71 {
72 return _shellPath;
73 }
74
75 - (void) setShellPath: (NSString *)object; // setter
76 {
77 ASSIGN(_shellPath,object);
78 }
79
80 - (NSString *) shellScript // getter
81 {
82 return _shellScript;
83 }
84
85 - (void) setShellScript: (NSString *)object; // setter
86 {
87 ASSIGN(_shellScript,object);
88 }
89
90 - (NSMutableArray *) inputPaths // getter
91 {
92 return _inputPaths;
93 }
94
95 - (void) setInputPaths: (NSMutableArray *)object; // setter
96 {
97 ASSIGN(_inputPaths,object);
98 }
99
100 - (NSMutableArray *) outputPaths // getter
101 {
102 return _outputPaths;
103 }
104
105 - (void) setOutputPaths: (NSMutableArray *)object; // setter
106 {
107 ASSIGN(_outputPaths,object);
108 }
109
110 - (NSString *) environmentVariableString
111 {
112 NSString *result = [NSString stringWithFormat: @"\n# Environment variables\n\n"];
113 char **env = NULL;
114
115 for (env = environ; *env != 0; env++)
116 {
117 char *thisEnv = *env;
118 NSString *envStr = [NSString stringWithCString: thisEnv encoding: NSUTF8StringEncoding];
119 NSArray *arr = [envStr componentsSeparatedByString: @"="];
120
121 if ([arr count] == 2)
122 {
123 NSString *var = [arr objectAtIndex: 0];
124 NSString *val = [arr objectAtIndex: 1];
125
126 if ([val containsString: @"%"] == NO)
127 {
128 if ([val containsString: @"\""] ) // val is already quoted
129 {
130 result = [result stringByAppendingString: [NSString stringWithFormat: @"export %@=%@\n", var, val]];
131 }
132 else
133 {
134 result = [result stringByAppendingString: [NSString stringWithFormat: @"export %@=\"%@\"\n", var, val]];
135 }
136 }
137 }
138 }
139
140 result = [result stringByAppendingString: @"# Done with environment setup...\n\n"];
141
142 return result;
143 }
144
145 - (NSString *) contextString
146 {
147 NSString *result = [NSString stringWithFormat: @"#!%@\n# Context variables\n\n", _shellPath];;
148 GSXCBuildContext *ctx = [GSXCBuildContext sharedBuildContext];
149 NSDictionary *dict = [ctx currentContext];
150 NSArray *keys = [dict allKeys];
151 NSEnumerator *en = [keys objectEnumerator];
152 NSString *k = nil;
153
154 result = [result stringByAppendingString: @"set -eo pipefail\n"
155 @"shopt -s inherit_errexit\n\n"];
156
157 while ((k = [en nextObject]) != nil)
158 {
159 id v = [dict objectForKey: k];
160 if ([v isKindOfClass: [NSString class]])
161 {
162 if ([v containsString: @"%"] == NO)
163 {
164 if ([v containsString: @"\""] ) // val is already quoted
165 {
166 result = [result stringByAppendingString: [NSString stringWithFormat: @"export %@=%@\n", k, v]];
167 }
168 else
169 {
170 result = [result stringByAppendingString: [NSString stringWithFormat: @"export %@=\"%@\"\n", k, v]];
171 }
172 }
173 }
174 }
175
176 result = [result stringByAppendingString: @"# Done with context setup...\n\n"];
177
178 return result;
179 }
180
181 - (NSString *) preprocessScript
182 {
183 NSDictionary *plistFile = [NSDictionary dictionaryWithContentsOfFile: @"buildtool.plist"];
184 NSDictionary *searchReplace = [plistFile objectForKey: @"searchReplace"];
185 NSString *script = nil;
186 NSString *result = nil;
187
188 ASSIGNCOPY(script, _shellScript);
189
190 // Search & replace as defined in the plist file..
191 if (searchReplace != nil)
192 {
193 #if GS_USE_ICU
194 NSEnumerator *en = [searchReplace keyEnumerator];
195 NSString *key = nil;
196
197 while ((key = [en nextObject]) != nil)
198 {
199 NSString *v = [searchReplace objectForKey: key];
200 NSError *error = NULL;
201 BOOL done = NO;
202 NSRegularExpression *regex = [NSRegularExpression
203 regularExpressionWithPattern: key
204 options: 0
205 error: &error];
206
207 // Iterate through all of the matches, but after each change start over because the ranges
208 // will shift as a result of the substitution. When there are no matches left, exit.
209 while (done == NO)
210 {
211 NSTextCheckingResult *match = [regex firstMatchInString: script
212 options: 0
213 range: NSMakeRange(0, [key length])];
214 if (match != nil)
215 {
216 NSRange matchRange = [match range];
217 script = [script stringByReplacingCharactersInRange: matchRange
218 withString: v];
219 }
220 else
221 {
222 done = YES;
223 }
224 }
225 }
226 #else
227 NSLog(@"WARNING: searchAndReplace is not available since GS was compiled without ICU support. ICU is needed for regular expressions.");
228 #endif
229 }
230
231 // Replace any variables defined in the context
232 result = [self contextString];
233 // result = [result stringByAppendingString: [self environmentVariableString]];
234 result = [result stringByAppendingString: @"\n# Script from project file...\n"];
235 result = [result stringByAppendingString: script];
236 result = [result stringByAppendingString: @"\n# Done with Xcode script\nexit $?\n"];
237
238 return result;
239 }
240
241 - (BOOL) build
242 {
243 NSError *error = nil;
244 NSString *fileName = [NSString stringWithFormat: @"script_%@_%lu", [[self name] stringByEliminatingSpecialCharacters], [_shellScript hash]];
245 NSString *tmpFilename = [NSString stringWithFormat: @"build/%@", fileName];
246 NSString *command = [NSString stringWithFormat: @"%@ %@",_shellPath,tmpFilename];
247 BOOL result = NO;
248 NSString *processedScript = [self preprocessScript];
249
250 // processedScript = [processedScript stringByReplacingEnvironmentVariablesWithValues];
251 xcprintf("=== Executing Script Build Phase... %s%s%s\n", GREEN, [_name cString], RESET);
252 xcputs([[NSString stringWithFormat: @"=== Command: %s%@%s using shell %@", YELLOW, command, RESET, _shellPath] cString]);
253 xcputs("*** script output");
254 [processedScript writeToFile: tmpFilename
255 atomically: YES
256 encoding: NSASCIIStringEncoding
257 error: &error];
258
259 result = xcsystem(command);
260 xcputs("*** script completed");
261 xcputs("=== Done Executing Script Build Phase...");
262
263 return (result == 0);
264 }
265 @end