| 1 |
// Released under the terms of LGPL 2.1, Please see COPYING.LIB |
| 2 |
|
| 3 |
#import "GSXCCMakeGenerator.h" |
| 4 |
#import "PBXNativeTarget.h" |
| 5 |
#import "XCConfigurationList.h" |
| 6 |
#import "PBXBuildFile.h" |
| 7 |
#import "PBXFileReference.h" |
| 8 |
#import "NSString+PBXAdditions.h" |
| 9 |
|
| 10 |
@interface GSXCCMakeGenerator (Private) |
| 11 |
- (void) createInfoPlist; |
| 12 |
@end |
| 13 |
|
| 14 |
@implementation GSXCCMakeGenerator |
| 15 |
|
| 16 |
- (instancetype) init |
| 17 |
{ |
| 18 |
self = [super init]; |
| 19 |
|
| 20 |
if (self != nil) |
| 21 |
{ |
| 22 |
_append = NO; |
| 23 |
_projectName = nil; |
| 24 |
_projectType = nil; |
| 25 |
} |
| 26 |
|
| 27 |
return self; |
| 28 |
} |
| 29 |
|
| 30 |
- (NSString *) cmakePreamble |
| 31 |
{ |
| 32 |
NSString *output = @""; |
| 33 |
NSString *header = [NSString stringWithFormat: @"## CMake Generated by buildtool - %@\n\n", _projectName]; |
| 34 |
NSString *project = [NSString stringWithFormat: @"project(%@ C CXX)\n", _projectName]; |
| 35 |
NSString *ccompiler = [NSString stringForCommand: @"gnustep-config --variable=CC"]; |
| 36 |
NSString *cxxcompiler = [NSString stringForCommand: @"gnustep-config --variable=CXX"]; |
| 37 |
|
| 38 |
output = [output stringByAppendingString: header]; |
| 39 |
output = [output stringByAppendingString: @"## Begin header\n"]; |
| 40 |
output = [output stringByAppendingString: @"cmake_minimum_required(VERSION 3.13)\n"]; |
| 41 |
output = [output stringByAppendingString: @"set(CMAKE_EXPORT_COMPILE_COMMANDS ON)\n"]; |
| 42 |
output = [output stringByAppendingString: @"if (EXISTS CMake/common.cmake)\n include(CMake/common.cmake)\nendif()\n"]; |
| 43 |
output = [output stringByAppendingString: @"set(CMAKE_CXX_STANDARD 11)\n"]; |
| 44 |
output = [output stringByAppendingString: [NSString stringWithFormat: @"set(CMAKE_C_COMPILER \"%@\")\n", ccompiler]]; // These should be set dynamically... |
| 45 |
output = [output stringByAppendingString: [NSString stringWithFormat: @"set(CMAKE_CXX_COMPILER \"%@\")\n", cxxcompiler]]; // So should this one... |
| 46 |
output = [output stringByAppendingString: project]; |
| 47 |
output = [output stringByAppendingString: @"set(BUILD_FOLDER_NAME \"build\")\n"]; |
| 48 |
output = [output stringByAppendingString: @"set(CMAKE_SOURCE_DIR \".\")\n\n"]; |
| 49 |
|
| 50 |
return output; |
| 51 |
} |
| 52 |
|
| 53 |
- (NSString *) cmakeSourceFiles: (NSArray *)array |
| 54 |
{ |
| 55 |
NSString *output = @""; |
| 56 |
NSEnumerator *en = [array objectEnumerator]; |
| 57 |
NSString *file = nil; |
| 58 |
|
| 59 |
if ([array count] > 0) |
| 60 |
{ |
| 61 |
output = [output stringByAppendingString: @"# Begin sources\n"]; |
| 62 |
|
| 63 |
if (_append == NO) |
| 64 |
{ |
| 65 |
output = [output stringByAppendingString: @"set (SOURCES\n"]; |
| 66 |
_append = YES; |
| 67 |
} |
| 68 |
else |
| 69 |
{ |
| 70 |
output = [output stringByAppendingString: @"list (APPEND SOURCES\n"]; |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
while((file = [en nextObject]) != nil) |
| 75 |
{ |
| 76 |
output = [output stringByAppendingString: [NSString stringWithFormat: @" ${CMAKE_SOURCE_DIR}/%@\n", file]]; |
| 77 |
} |
| 78 |
|
| 79 |
if ([array count] > 0) |
| 80 |
{ |
| 81 |
output = [output stringByAppendingString: @")\n\n"]; |
| 82 |
output = [output stringByAppendingString: @"if (EXISTS CMake/sources.cmake)\n include(CMake/sources.cmake)\nendif()\n\n"]; |
| 83 |
} |
| 84 |
|
| 85 |
return output; |
| 86 |
} |
| 87 |
|
| 88 |
- (NSString *) cmakeHeaderFiles: (NSArray *)array |
| 89 |
{ |
| 90 |
NSString *output = @""; |
| 91 |
NSEnumerator *en = [array objectEnumerator]; |
| 92 |
id file = nil; |
| 93 |
|
| 94 |
if ([array count] > 0) |
| 95 |
{ |
| 96 |
output = [output stringByAppendingString: @"# Begin headers\n"]; |
| 97 |
output = [output stringByAppendingString: @"set (HEADERS\n"]; |
| 98 |
} |
| 99 |
|
| 100 |
while((file = [en nextObject]) != nil) |
| 101 |
{ |
| 102 |
if ([file isKindOfClass: [PBXBuildFile class]]) |
| 103 |
{ |
| 104 |
output = [output stringByAppendingString: [NSString stringWithFormat: @" ${CMAKE_SOURCE_DIR}/%@\n", [file buildPath]]]; |
| 105 |
} |
| 106 |
else if ([file isKindOfClass: [NSString class]]) |
| 107 |
{ |
| 108 |
output = [output stringByAppendingString: [NSString stringWithFormat: @" ${CMAKE_SOURCE_DIR}/%@\n", file]]; |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
if ([array count] > 0) |
| 113 |
{ |
| 114 |
output = [output stringByAppendingString: @")\n\n"]; |
| 115 |
|
| 116 |
if ([_projectType isEqualToString: @"library"]) |
| 117 |
{ |
| 118 |
} |
| 119 |
else if ([_projectType isEqualToString: @"framework"]) |
| 120 |
{ |
| 121 |
output = [output stringByAppendingString: |
| 122 |
[NSString stringWithFormat: @"file(COPY ${HEADERS} DESTINATION \"%@.framework/Headers\")\n", |
| 123 |
_projectName]]; |
| 124 |
} |
| 125 |
|
| 126 |
output = [output stringByAppendingString: @"if (EXISTS CMake/headers.cmake)\n include(CMake/headers.cmake)\nendif()\n\n"]; |
| 127 |
} |
| 128 |
|
| 129 |
return output; |
| 130 |
} |
| 131 |
|
| 132 |
- (NSString *) cmakeLibraryFiles: (NSArray *)array |
| 133 |
{ |
| 134 |
NSString *output = @""; |
| 135 |
|
| 136 |
if ([_projectType isEqualToString: @"application"]) |
| 137 |
{ |
| 138 |
} |
| 139 |
else if ([_projectType isEqualToString: @"bundle"]) |
| 140 |
{ |
| 141 |
} |
| 142 |
else if ([_projectType isEqualToString: @"library"]) |
| 143 |
{ |
| 144 |
} |
| 145 |
else if ([_projectType isEqualToString: @"tool"]) |
| 146 |
{ |
| 147 |
} |
| 148 |
|
| 149 |
return output; |
| 150 |
} |
| 151 |
|
| 152 |
- (NSString *) cmakeResourceFiles: (NSArray *)array |
| 153 |
{ |
| 154 |
NSString *output = @""; |
| 155 |
NSString *file = nil; |
| 156 |
|
| 157 |
if ([_projectType isEqualToString: @"tool"] || array == nil || [array count] == 0) |
| 158 |
{ |
| 159 |
return @""; |
| 160 |
} |
| 161 |
|
| 162 |
// handle info plist file... |
| 163 |
NSMutableArray *resources = [NSMutableArray arrayWithArray: array]; |
| 164 |
NSString *infoPlistName = [NSString stringWithFormat: @"%@Info.plist", _projectName]; |
| 165 |
NSEnumerator *en = [resources objectEnumerator]; |
| 166 |
|
| 167 |
[resources removeObject: infoPlistName]; // remove the plist if it exists... |
| 168 |
[resources addObject: @"Info-gnustep.plist"]; |
| 169 |
|
| 170 |
[self createInfoPlist]; |
| 171 |
|
| 172 |
if ([array count] > 0) |
| 173 |
{ |
| 174 |
output = [output stringByAppendingString: @"# Begin resources\n"]; |
| 175 |
output = [output stringByAppendingString: @"set (GLOBAL_RESOURCES\n"]; |
| 176 |
} |
| 177 |
|
| 178 |
while((file = [en nextObject]) != nil) |
| 179 |
{ |
| 180 |
output = [output stringByAppendingString: [NSString stringWithFormat: @" ${CMAKE_SOURCE_DIR}/%@\n", file]]; |
| 181 |
} |
| 182 |
|
| 183 |
if ([array count] > 0) |
| 184 |
{ |
| 185 |
output = [output stringByAppendingString: @")\n\n"]; |
| 186 |
output = [output stringByAppendingString: @"if (EXISTS CMake/resources.cmake)\n include(CMake/resources.cmake)\nendif()\n\n"]; |
| 187 |
} |
| 188 |
|
| 189 |
return output; |
| 190 |
} |
| 191 |
|
| 192 |
- (NSString *) cmakeDeclareProject |
| 193 |
{ |
| 194 |
NSString *output = @"# Copy resources\n"; |
| 195 |
|
| 196 |
if ([_projectType isEqualToString: @"application"]) |
| 197 |
{ |
| 198 |
output = [output stringByAppendingString: |
| 199 |
[NSString stringWithFormat: @"set_target_properties(%@ PROPERTIES RUNTIME_OUTPUT_DIRECTORY \"%@.app\")\n", |
| 200 |
_projectName, _projectName]]; |
| 201 |
output = [output stringByAppendingString: |
| 202 |
[NSString stringWithFormat: @"file(COPY ${GLOBAL_RESOURCES} DESTINATION \"%@.app/Resources\")\n", |
| 203 |
_projectName]]; |
| 204 |
} |
| 205 |
else if ([_projectType isEqualToString: @"bundle"]) |
| 206 |
{ |
| 207 |
NSString *bundleName = @"bundle"; // this should be set by a variable/setting. |
| 208 |
output = [output stringByAppendingString: |
| 209 |
[NSString stringWithFormat: @"set_target_properties(%@ PROPERTIES RUNTIME_OUTPUT_DIRECTORY \"%@.%@\")\n", |
| 210 |
_projectName, _projectName, bundleName]]; |
| 211 |
output = [output stringByAppendingString: |
| 212 |
[NSString stringWithFormat: @"file(COPY ${GLOBAL_RESOURCES} DESTINATION \"%@.%@/Resources\")\n", |
| 213 |
_projectName, bundleName]]; |
| 214 |
} |
| 215 |
else if ([_projectType isEqualToString: @"framework"]) |
| 216 |
{ |
| 217 |
output = [output stringByAppendingString: |
| 218 |
[NSString stringWithFormat: @"set_target_properties(%@ PROPERTIES RUNTIME_OUTPUT_DIRECTORY \"%@.framework\")\n", |
| 219 |
_projectName, _projectName]]; |
| 220 |
output = [output stringByAppendingString: |
| 221 |
[NSString stringWithFormat: @"file(COPY ${GLOBAL_RESOURCES} DESTINATION \"%@.framework/Resources\")\n", |
| 222 |
_projectName]]; |
| 223 |
} |
| 224 |
else // if ([_projectType isEqualToString: @"tool"]) // ([_projectType isEqualToString: @"library"]) |
| 225 |
{ |
| 226 |
output = [output stringByAppendingString: |
| 227 |
[NSString stringWithFormat: @"set_target_properties(%@ PROPERTIES RUNTIME_OUTPUT_DIRECTORY .)\n", |
| 228 |
_projectName]]; |
| 229 |
} |
| 230 |
|
| 231 |
return output; |
| 232 |
} |
| 233 |
|
| 234 |
- (NSString *) cmakeIncludeDirList: (NSArray *)includeArray |
| 235 |
{ |
| 236 |
NSString *output = [NSString stringWithFormat: @"# Include directories\ntarget_include_directories(%@ PRIVATE\n", _projectName]; |
| 237 |
NSEnumerator *en = [includeArray objectEnumerator]; |
| 238 |
NSString *includeDir = nil; |
| 239 |
|
| 240 |
while ((includeDir = [en nextObject]) != nil) |
| 241 |
{ |
| 242 |
output = [output stringByAppendingString: [NSString stringWithFormat: @" %@\n", includeDir]]; |
| 243 |
} |
| 244 |
|
| 245 |
// Add canonical directories... |
| 246 |
NSString *localIncludes = [NSString stringForCommand: @"gnustep-config --variable=GNUSTEP_LOCAL_HEADERS"]; |
| 247 |
output = [output stringByAppendingString: [NSString stringWithFormat: @" %@\n", localIncludes]]; |
| 248 |
|
| 249 |
NSString *systemIncludes = [NSString stringForCommand: @"gnustep-config --variable=GNUSTEP_SYSTEM_HEADERS"]; |
| 250 |
output = [output stringByAppendingString: [NSString stringWithFormat: @" %@\n", systemIncludes]]; |
| 251 |
|
| 252 |
// Close declaration... |
| 253 |
output = [output stringByAppendingString: @")\n\n"]; |
| 254 |
output = [output stringByAppendingString: @"if (EXISTS CMake/include.cmake)\n include(CMake/include.cmake)\nendif()\n\n"]; |
| 255 |
|
| 256 |
return output; |
| 257 |
} |
| 258 |
|
| 259 |
- (NSString *) cmakeTargetCompileOptions |
| 260 |
{ |
| 261 |
NSString *output = [NSString stringWithFormat: @"# Compile options\n" |
| 262 |
@"target_compile_options(%@ PRIVATE ", _projectName]; |
| 263 |
NSString *command = [NSString stringForCommand: @"gnustep-config --objc-flags"]; |
| 264 |
|
| 265 |
output = [output stringByAppendingString: command]; |
| 266 |
output = [output stringByAppendingString: @")\n\n"]; |
| 267 |
|
| 268 |
return output; |
| 269 |
} |
| 270 |
|
| 271 |
- (NSString *) cmakeTargetLinkOptions |
| 272 |
{ |
| 273 |
NSString *output = [NSString stringWithFormat: @"# Link options\n" |
| 274 |
@"target_link_options(%@ PRIVATE ", _projectName]; |
| 275 |
NSString *command = nil; |
| 276 |
|
| 277 |
if ([_projectType isEqualToString: @"application"]) |
| 278 |
{ |
| 279 |
command = [NSString stringForCommand: @"gnustep-config --gui-libs"]; |
| 280 |
} |
| 281 |
else if ([_projectType isEqualToString: @"bundle"]) |
| 282 |
{ |
| 283 |
command = [NSString stringForCommand: @"gnustep-config --base-libs"]; |
| 284 |
} |
| 285 |
else if ([_projectType isEqualToString: @"library"]) |
| 286 |
{ |
| 287 |
command = [NSString stringForCommand: @"gnustep-config --base-libs"]; |
| 288 |
} |
| 289 |
else if ([_projectType isEqualToString: @"framework"]) |
| 290 |
{ |
| 291 |
command = [NSString stringForCommand: @"gnustep-config --base-libs"]; |
| 292 |
} |
| 293 |
else // tool |
| 294 |
{ |
| 295 |
command = [NSString stringForCommand: @"gnustep-config --base-libs"]; |
| 296 |
} |
| 297 |
|
| 298 |
output = [output stringByAppendingString: command]; |
| 299 |
output = [output stringByAppendingString: @")\n\n"]; |
| 300 |
output = [output stringByAppendingString: @"if (EXISTS CMake/link.cmake)\n include(CMake/link.cmake)\nendif()\n\n"]; |
| 301 |
|
| 302 |
return output; |
| 303 |
} |
| 304 |
|
| 305 |
- (NSString *) cmakeDeclareTarget |
| 306 |
{ |
| 307 |
NSString *output = @""; |
| 308 |
|
| 309 |
output = [output stringByAppendingString: |
| 310 |
[NSString stringWithFormat: |
| 311 |
@"# Declare target\n" |
| 312 |
@"add_executable(\n" |
| 313 |
@" %@\n" |
| 314 |
@" ${SOURCES}\n" |
| 315 |
@")\n\n", _projectName]]; |
| 316 |
|
| 317 |
return output; |
| 318 |
} |
| 319 |
|
| 320 |
// TODO: Need to find a way to make this a common function.... |
| 321 |
- (BOOL) processInfoPlistInput: (NSString *)inputFileName |
| 322 |
output: (NSString *)outputFileName |
| 323 |
{ |
| 324 |
if (inputFileName != nil) |
| 325 |
{ |
| 326 |
GSXCBuildContext *context = [GSXCBuildContext sharedBuildContext]; |
| 327 |
NSString *settings = [context objectForKey: @"PRODUCT_SETTINGS_XML"]; |
| 328 |
if(settings == nil) |
| 329 |
{ |
| 330 |
NSString *inputFileString = [NSString stringWithContentsOfFile: inputFileName]; |
| 331 |
NSString *outputFileString = [inputFileString stringByReplacingEnvironmentVariablesWithValues]; |
| 332 |
NSMutableDictionary *plistDict = [NSMutableDictionary dictionaryWithDictionary: [outputFileString propertyList]]; |
| 333 |
// NSString *filename = [self processAssets]; |
| 334 |
/* |
| 335 |
if (filename != nil) |
| 336 |
{ |
| 337 |
[plistDict setObject: filename forKey: @"NSIcon"]; |
| 338 |
} |
| 339 |
*/ |
| 340 |
[plistDict writeToFile: outputFileName |
| 341 |
atomically: YES]; |
| 342 |
|
| 343 |
NSDebugLog(@"%@", plistDict); |
| 344 |
} |
| 345 |
else |
| 346 |
{ |
| 347 |
[settings writeToFile: outputFileName |
| 348 |
atomically: YES |
| 349 |
encoding: NSUTF8StringEncoding |
| 350 |
error: NULL]; |
| 351 |
} |
| 352 |
} |
| 353 |
else |
| 354 |
{ |
| 355 |
NSArray *keys = [NSArray arrayWithObjects: @"NSPrincipalClass", @"NSMainNibFile", nil]; |
| 356 |
NSArray *objs = [NSArray arrayWithObjects: @"NSApplication", @"MainMenu", nil]; |
| 357 |
NSDictionary *ipl = [NSDictionary dictionaryWithObjects: objs |
| 358 |
forKeys: keys]; |
| 359 |
[ipl writeToFile: outputFileName |
| 360 |
atomically: YES]; |
| 361 |
} |
| 362 |
|
| 363 |
return YES; |
| 364 |
} |
| 365 |
|
| 366 |
- (void) createInfoPlist |
| 367 |
{ |
| 368 |
if ([_projectType isEqualToString: @"tool"]) |
| 369 |
{ |
| 370 |
return; |
| 371 |
} |
| 372 |
|
| 373 |
NSString *inputFileName = [NSString stringWithFormat: @"%@Info.plist", _projectName]; |
| 374 |
[self processInfoPlistInput: inputFileName output: @"Info-gnustep.plist"]; |
| 375 |
} |
| 376 |
|
| 377 |
- (BOOL) generate |
| 378 |
{ |
| 379 |
BOOL result = YES; |
| 380 |
GSXCBuildContext *context = [GSXCBuildContext sharedBuildContext]; |
| 381 |
NSString *projectType = [context objectForKey: @"PROJECT_TYPE"]; |
| 382 |
NSString *name = [_target name]; |
| 383 |
NSString *projName = [name stringByDeletingPathExtension]; |
| 384 |
NSString *outputName = @"CMakeLists.txt"; |
| 385 |
NSString *outputString = @""; |
| 386 |
|
| 387 |
// Assign to ivars... |
| 388 |
ASSIGN(_projectType, projectType); |
| 389 |
ASSIGN(_projectName, projName); |
| 390 |
|
| 391 |
// Construct the makefile out of the data we have thusfar collected. |
| 392 |
xcputs("\t* Generating CMakeLists.txt..."); |
| 393 |
|
| 394 |
// Sometimes the build will generate all of the target makefiles in one place, |
| 395 |
// depending on the version of Xcode the project was created with. |
| 396 |
if([[NSFileManager defaultManager] fileExistsAtPath: @"CMakeLists.txt"]) |
| 397 |
{ |
| 398 |
// if it collides with the existing name, add the target name... |
| 399 |
outputName = [outputName stringByAppendingString: [NSString stringWithFormat: @"_%@.txt", _projectName]]; |
| 400 |
} |
| 401 |
|
| 402 |
// Initial setup... |
| 403 |
outputString = [self cmakePreamble]; |
| 404 |
|
| 405 |
// Add sources... |
| 406 |
outputString = [outputString stringByAppendingString: |
| 407 |
[self cmakeSourceFiles: [context objectForKey: @"OBJC_FILES"]]]; |
| 408 |
outputString = [outputString stringByAppendingString: |
| 409 |
[self cmakeSourceFiles: [context objectForKey: @"C_FILES"]]]; |
| 410 |
outputString = [outputString stringByAppendingString: |
| 411 |
[self cmakeSourceFiles: [context objectForKey: @"CPP_FILES"]]]; |
| 412 |
outputString = [outputString stringByAppendingString: |
| 413 |
[self cmakeSourceFiles: [context objectForKey: @"OBJCPP_FILES"]]]; |
| 414 |
|
| 415 |
// Declare target |
| 416 |
outputString = [outputString stringByAppendingString: [self cmakeDeclareTarget]]; |
| 417 |
|
| 418 |
// Headers |
| 419 |
outputString = [outputString stringByAppendingString: |
| 420 |
[self cmakeHeaderFiles: [context objectForKey: @"HEADERS"]]]; |
| 421 |
|
| 422 |
outputString = [outputString stringByAppendingString: |
| 423 |
[self cmakeLibraryFiles: [context objectForKey: @"ADDITIONAL_OBJC_LIBS"]]]; |
| 424 |
// Include dirs |
| 425 |
outputString = [outputString stringByAppendingString: [self cmakeIncludeDirList: nil]]; |
| 426 |
|
| 427 |
// Resources |
| 428 |
outputString = [outputString stringByAppendingString: |
| 429 |
[self cmakeResourceFiles: [context objectForKey: @"RESOURCES"]]]; |
| 430 |
|
| 431 |
// Compile options... |
| 432 |
outputString = [outputString stringByAppendingString: [self cmakeTargetCompileOptions]]; |
| 433 |
|
| 434 |
// Link options... |
| 435 |
outputString = [outputString stringByAppendingString: [self cmakeTargetLinkOptions]]; |
| 436 |
|
| 437 |
// Handle project type... this builds the directory structure needed for a given type. |
| 438 |
outputString = [outputString stringByAppendingString: [self cmakeDeclareProject]]; |
| 439 |
|
| 440 |
|
| 441 |
NSDebugLog(@"output = %@", outputString); |
| 442 |
[outputString writeToFile: outputName atomically: YES]; |
| 443 |
xcputs([[NSString stringWithFormat: @"=== Completed generation for target %@", name] cString]); |
| 444 |
|
| 445 |
return result; |
| 446 |
} |
| 447 |
|
| 448 |
@end |