Debian Codegraph @

Tools/pc2xc/main.m

raw
1 /*
2 Project: pc2xc
3
4 Author: Gregory John Casamento,,,
5
6 Created: 2023-10-16 23:37:42 -0400 by heron
7 */
8
9 #import <Foundation/Foundation.h>
10
11 #import <XCode/XCode.h>
12 #import <XCode/xcsystem.h>
13
14 NSMutableArray *buildFileReferences(NSArray *allFiles,
15 NSString *ext)
16 {
17 NSMutableArray *result = [NSMutableArray array];
18 NSEnumerator *en = [allFiles objectEnumerator];
19 NSString *filename = nil;
20
21 while ((filename = [en nextObject]) != nil)
22 {
23 if (ext != nil)
24 {
25 filename = [filename stringByAppendingPathExtension: ext];
26 }
27
28 PBXFileReference *fileRef = AUTORELEASE([[PBXFileReference alloc] initWithPath: filename]);
29 [result addObject: fileRef];
30 }
31
32 return result;
33 }
34
35 NSString *typeForProjectType(NSString *projectType)
36 {
37 NSString *result = @"";
38
39 if ([projectType isEqualToString: @"Application"])
40 {
41 result = @"com.apple.product-type.application";
42 }
43 else if ([projectType isEqualToString: @"Tool"])
44 {
45 result = @"com.apple.product-type.tool";
46 }
47 else if ([projectType isEqualToString: @"Library"])
48 {
49 result = @"com.apple.product-type.library";
50 }
51 else if ([projectType isEqualToString: @"Framework"])
52 {
53 result = @"com.apple.product-type.framework";
54 }
55
56 return result;
57 }
58
59 PBXGroup *productReferenceGroup(NSString *projectName,
60 NSString *projectType)
61 {
62 PBXGroup *group = AUTORELEASE([[PBXGroup alloc] init]);
63 NSString *type = typeForProjectType(projectType);
64 NSString *ext = [PBXFileReference extForFileType: type];
65 NSString *path = (ext == nil || [ext isEqualToString: @""]) ? projectName :
66 [projectName stringByAppendingPathExtension: ext];
67 PBXFileReference *productFileRef = AUTORELEASE([[PBXFileReference alloc] initWithPath: path]);
68 NSMutableArray *children = [NSMutableArray arrayWithObject: productFileRef];
69
70 [group setChildren: children];
71 [group setName: @"Products"];
72
73 return group;
74 }
75
76 PBXGroup *mainGroupBuild(NSArray *files, PBXGroup *productReferenceGroup)
77 {
78 PBXGroup *mainGroup = AUTORELEASE([[PBXGroup alloc] init]);
79 NSMutableArray *buildGroupFiles = buildFileReferences(files, nil);
80 PBXGroup *buildFileGroup = AUTORELEASE([[PBXGroup alloc] init]);
81 [buildFileGroup setChildren: buildGroupFiles];
82
83 NSMutableArray *mainGroupChildren = [NSMutableArray arrayWithObjects: buildFileGroup,
84 productReferenceGroup, nil];
85 [mainGroup setChildren: mainGroupChildren];
86
87 return mainGroup;
88 }
89
90 void buildPhase(NSArray *items, PBXBuildPhase *phase)
91 {
92 NSMutableArray *sources = [NSMutableArray arrayWithCapacity: [items count]];
93 NSEnumerator *en = [items objectEnumerator];
94 NSString *item = nil;
95
96 while ((item = [en nextObject]) != nil)
97 {
98 PBXFileReference *fileRef = AUTORELEASE([[PBXFileReference alloc] initWithPath: item]);
99 PBXBuildFile *buildFile = AUTORELEASE([[PBXBuildFile alloc] init]);
100 [buildFile setFileRef: fileRef];
101 [sources addObject: buildFile];
102 }
103
104 [phase setFiles: sources];
105 }
106
107 NSMutableArray *buildTargets(NSString *projectName,
108 NSString *projectType,
109 NSArray *files,
110 NSArray *headers,
111 NSArray *resources,
112 NSArray *frameworks,
113 PBXGroup *prodRefGroup)
114 {
115 NSMutableArray *result = [NSMutableArray array];
116 PBXNativeTarget *target = AUTORELEASE([[PBXNativeTarget alloc] init]);
117
118 PBXSourcesBuildPhase *sourcePhase = AUTORELEASE([[PBXSourcesBuildPhase alloc] init]);
119 buildPhase(files, sourcePhase);
120
121 PBXResourcesBuildPhase *resourcePhase = AUTORELEASE([[PBXResourcesBuildPhase alloc] init]);
122 buildPhase(resources, resourcePhase);
123
124 // PBXFrameworksBuildPhase *frameworksPhase = AUTORELEASE([[PBXFrameworksBuildPhase alloc] init]);
125 // buildPhase(frameworks, frameworksPhase);
126
127 PBXFileReference *productRef = [[prodRefGroup children] objectAtIndex: 0];
128 NSMutableArray *phases = [NSMutableArray arrayWithObjects: sourcePhase, resourcePhase, nil]; // frameworksPhase, nil];
129
130 [target setBuildPhases: phases];
131 [target setName: projectName];
132 [target setProductName: [productRef path]];
133 [target setProductType: projectType];
134
135 [result addObject: target];
136
137 return result;
138 }
139
140
141 PBXContainer *buildContainer(NSString *projectName,
142 NSString *projectType,
143 NSArray *files,
144 NSArray *headers,
145 NSArray *resources,
146 NSArray *other,
147 NSArray *frameworks)
148 {
149 NSMutableArray *allFiles = [NSMutableArray arrayWithArray: files];
150 PBXProject *project = AUTORELEASE([[PBXProject alloc] init]);
151 PBXContainer *container = AUTORELEASE([[PBXContainer alloc] initWithRootObject: project]);
152 XCBuildConfiguration *buildConfigDebug = AUTORELEASE([[XCBuildConfiguration alloc] init]);
153 XCBuildConfiguration *buildConfigRelease = AUTORELEASE([[XCBuildConfiguration alloc] initWithName: @"Release"]);
154 NSMutableArray *configArray = [NSMutableArray arrayWithObjects: buildConfigDebug, buildConfigRelease, nil];
155 XCConfigurationList *configList = AUTORELEASE([[XCConfigurationList alloc] initWithConfigurations: configArray]);
156 NSString *type = typeForProjectType(projectType);
157
158 // Add all files to the main group...
159 [allFiles addObjectsFromArray: other];
160 [allFiles addObjectsFromArray: resources];
161
162 // Set up groups...
163 PBXGroup *productRefGroup = productReferenceGroup(projectName, projectType); // AUTORELEASE([[PBXGroup alloc] init]);
164 PBXGroup *mainGroup = mainGroupBuild(allFiles, productRefGroup); // AUTORELEASE([[PBXGroup alloc] init]);
165 NSMutableArray *targets = buildTargets(projectName, type, allFiles, headers, resources, frameworks, productRefGroup);
166
167 [project setMainGroup: mainGroup];
168 [project setProductRefGroup: productRefGroup];
169 [project setBuildConfigurationList: configList];
170 // [project setContainer: container];
171 [project setTargets: targets];
172
173 return container;
174 }
175
176
177 PBXContainer *convertPBProject(NSDictionary *proj)
178 {
179 NSString *projectType = [proj objectForKey: @"PROJECTTYPE"];
180 NSString *projectName = [proj objectForKey: @"PROJECTNAME"];
181 NSDictionary *filesTable = [proj objectForKey: @"FILESTABLE"];
182 NSArray *files = [filesTable objectForKey: @"CLASSES"];
183 NSArray *headers = [filesTable objectForKey: @"H_FILES"];
184 NSArray *resources = [filesTable objectForKey: @"INTERFACES"];
185 NSArray *other = [filesTable objectForKey: @"OTHER_LINKED"];
186 NSArray *frameworks = [filesTable objectForKey: @"FRAMEWORKS"];
187 NSArray *images = [filesTable objectForKey: @"IMAGES"];
188 NSMutableArray *allResources = [NSMutableArray arrayWithArray: resources];
189
190 [allResources addObjectsFromArray: images];
191 return buildContainer(projectName, projectType, files, headers, allResources, other, frameworks);
192 }
193
194 PBXContainer *convertPCProject(NSDictionary *proj)
195 {
196 NSString *projectType = [proj objectForKey: @"PROJECT_TYPE"];
197 NSString *projectName = [proj objectForKey: @"PROJECT_NAME"];
198 NSArray *files = [proj objectForKey: @"CLASS_FILES"];
199 NSArray *headers = [proj objectForKey: @"HEADER_FILES"];
200 NSArray *resources = [proj objectForKey: @"LOCALIZED_RESOURCES"];
201 NSArray *other = [proj objectForKey: @"OTHER_SOURCES"];
202 NSArray *frameworks = [proj objectForKey: @"LIBRARIES"];
203 NSArray *images = [proj objectForKey: @"IMAGES"];
204 NSMutableArray *allResources = [NSMutableArray arrayWithArray: resources];
205
206 [allResources addObjectsFromArray: images];
207
208 return buildContainer(projectName, projectType, files, headers, allResources, other, frameworks);
209 }
210
211 BOOL buildXCodeProj(PBXContainer *container, NSString *dn)
212 {
213 NSError *error = nil;
214 NSString *directoryName = [dn stringByAppendingPathExtension: @"xcodeproj"];
215 NSString *fn = [directoryName stringByAppendingPathComponent: @"project.pbxproj"];
216 NSFileManager *fm = [NSFileManager defaultManager];
217 BOOL created = [fm createDirectoryAtPath: directoryName
218 withIntermediateDirectories: YES
219 attributes: NULL
220 error: &error];
221 BOOL result = NO;
222
223 if (created)
224 {
225 xcprintf("=== Saving Project %s%s%s%s -> %s%s%s\n",
226 BOLD, YELLOW, [fn cString], RESET, GREEN,
227 [dn cString], RESET);
228
229 // [container save]; // Setup to save...
230
231 // Save the project...
232 if (created && !error)
233 {
234 id dictionary = [PBXCoder archiveWithRootObject: container];
235
236 result = [dictionary writeToFile: fn atomically: YES];
237 if (result)
238 {
239 xcprintf("=== Done Saving Project %s%s%s%s\n",
240 BOLD, GREEN, [dn cString], RESET);
241 }
242 else
243 {
244 xcprintf("=== Error Saving Project %s%s%s%s\n",
245 BOLD, GREEN, [dn cString], RESET);
246 }
247 }
248 }
249
250 return result;
251 }
252
253 int main(int argc, const char *argv[])
254 {
255 id pool = [[NSAutoreleasePool alloc] init];
256
257 if (argc > 1)
258 {
259 NSString *input = [NSString stringWithUTF8String: argv[1]];
260 NSString *output = [NSString stringWithUTF8String: argv[2]];
261 PBXContainer *container = nil;
262
263 if ([[input lastPathComponent] isEqualToString: @"PB.project"])
264 {
265 NSDictionary *proj = [NSDictionary dictionaryWithContentsOfFile: input];
266
267 xcprintf("== Parsing an old style NeXT project: %s -> %s\n",
268 [input UTF8String], [output UTF8String]);
269 container = convertPBProject(proj);
270 }
271 else if ([[input pathExtension] isEqualToString: @"pcproj"])
272 {
273 NSString *path = [input stringByAppendingPathComponent: @"PC.project"];
274 NSDictionary *proj = [NSDictionary dictionaryWithContentsOfFile: path];
275
276 printf("== Parsing a ProjectCenter project: %s -> %s\n",
277 [input UTF8String], [output UTF8String]);
278 container = convertPCProject(proj);
279 }
280 else
281 {
282 xcprintf("== Unknown project type");
283 }
284
285 if (container == nil)
286 {
287 xcprintf("== Unable to parse project file %@", input);
288 return 255;
289 }
290 else
291 {
292 BOOL result = buildXCodeProj(container, output);
293 if (result == NO)
294 {
295 return 255;
296 }
297 }
298 }
299 else
300 {
301 xcprintf("== Not enough arguments");
302 }
303
304 [pool release];
305
306 return 0;
307 }
308