1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599
|
#!/usr/bin/perl -w
# Add source files and headers to Xcode and Visual Studio projects.
# THIS IS NOT ROBUST, THIS IS JUST RYAN AVOIDING RUNNING BETWEEN
# THREE COMPUTERS AND A BUNCH OF DEVELOPMENT ENVIRONMENTS TO ADD
# A STUPID FILE TO THE BUILD.
use warnings;
use strict;
use File::Basename;
my %xcode_references = ();
sub generate_xcode_id {
my @chars = ('0'..'9', 'A'..'F');
my $str;
do {
my $len = 16;
$str = '0000'; # start and end with '0000' so we know we added it.
while ($len--) {
$str .= $chars[rand @chars]
};
$str .= '0000'; # start and end with '0000' so we know we added it.
} while (defined($xcode_references{$str}));
$xcode_references{$str} = 1; # so future calls can't generate this one.
return $str;
}
sub process_xcode {
my $addpath = shift;
my $pbxprojfname = shift;
my $lineno;
%xcode_references = ();
my $addfname = basename($addpath);
my $addext = '';
if ($addfname =~ /\.(.*?)\Z/) {
$addext = $1;
}
my $is_public_header = ($addpath =~ /\Ainclude\/SDL3\//) ? 1 : 0;
my $filerefpath = $is_public_header ? "SDL3/$addfname" : $addfname;
my $srcs_or_headers = '';
my $addfiletype = '';
if ($addext eq 'c') {
$srcs_or_headers = 'Sources';
$addfiletype = 'sourcecode.c.c';
} elsif ($addext eq 'm') {
$srcs_or_headers = 'Sources';
$addfiletype = 'sourcecode.c.objc';
} elsif ($addext eq 'h') {
$srcs_or_headers = 'Headers';
$addfiletype = 'sourcecode.c.h';
} else {
die("Unexpected file extension '$addext'\n");
}
my $fh;
open $fh, '<', $pbxprojfname or die("Failed to open '$pbxprojfname': $!\n");
chomp(my @pbxproj = <$fh>);
close($fh);
# build a table of all ids, in case we duplicate one by some miracle.
$lineno = 0;
foreach (@pbxproj) {
$lineno++;
# like "F3676F582A7885080091160D /* SDL3.dmg */ = {"
if (/\A\t\t([A-F0-9]{24}) \/\* (.*?) \*\/ \= \{\Z/) {
$xcode_references{$1} = $2;
}
}
# build out of a tree of PBXGroup items.
my %pbxgroups = ();
my $thispbxgroup;
my $pbxgroup_children;
my $pbxgroup_state = 0;
my $pubheaders_group_hash = '';
my $libsrc_group_hash = '';
$lineno = 0;
foreach (@pbxproj) {
$lineno++;
if ($pbxgroup_state == 0) {
$pbxgroup_state++ if /\A\/\* Begin PBXGroup section \*\/\Z/;
} elsif ($pbxgroup_state == 1) {
# like "F3676F582A7885080091160D /* SDL3.dmg */ = {"
if (/\A\t\t([A-F0-9]{24}) \/\* (.*?) \*\/ \= \{\Z/) {
my %newhash = ();
$pbxgroups{$1} = \%newhash;
$thispbxgroup = \%newhash;
$pubheaders_group_hash = $1 if $2 eq 'Public Headers';
$libsrc_group_hash = $1 if $2 eq 'Library Source';
$pbxgroup_state++;
} elsif (/\A\/\* End PBXGroup section \*\/\Z/) {
last;
} else {
die("Expected pbxgroup obj on '$pbxprojfname' line $lineno\n");
}
} elsif ($pbxgroup_state == 2) {
if (/\A\t\t\tisa \= PBXGroup;\Z/) {
$pbxgroup_state++;
} else {
die("Expected pbxgroup obj's isa field on '$pbxprojfname' line $lineno\n");
}
} elsif ($pbxgroup_state == 3) {
if (/\A\t\t\tchildren \= \(\Z/) {
my %newhash = ();
$$thispbxgroup{'children'} = \%newhash;
$pbxgroup_children = \%newhash;
$pbxgroup_state++;
} else {
die("Expected pbxgroup obj's children field on '$pbxprojfname' line $lineno\n");
}
} elsif ($pbxgroup_state == 4) {
if (/\A\t\t\t\t([A-F0-9]{24}) \/\* (.*?) \*\/,\Z/) {
$$pbxgroup_children{$1} = $2;
} elsif (/\A\t\t\t\);\Z/) {
$pbxgroup_state++;
} else {
die("Expected pbxgroup obj's children element on '$pbxprojfname' line $lineno\n");
}
} elsif ($pbxgroup_state == 5) {
if (/\A\t\t\t(.*?) \= (.*?);\Z/) {
$$thispbxgroup{$1} = $2;
} elsif (/\A\t\t\};\Z/) {
$pbxgroup_state = 1;
} else {
die("Expected pbxgroup obj field on '$pbxprojfname' line $lineno\n");
}
} else {
die("bug in this script.");
}
}
die("Didn't see PBXGroup section in '$pbxprojfname'. Bug?\n") if $pbxgroup_state == 0;
die("Didn't see Public Headers PBXGroup in '$pbxprojfname'. Bug?\n") if $pubheaders_group_hash eq '';
die("Didn't see Library Source PBXGroup in '$pbxprojfname'. Bug?\n") if $libsrc_group_hash eq '';
# Some debug log dumping...
if (0) {
foreach (keys %pbxgroups) {
my $k = $_;
my $g = $pbxgroups{$k};
print("$_:\n");
foreach (keys %$g) {
print(" $_:\n");
if ($_ eq 'children') {
my $kids = $$g{$_};
foreach (keys %$kids) {
print(" $_ -> " . $$kids{$_} . "\n");
}
} else {
print(' ' . $$g{$_} . "\n");
}
}
print("\n");
}
}
# Get some unique IDs for our new thing.
my $fileref = generate_xcode_id();
my $buildfileref = generate_xcode_id();
# Figure out what group to insert this into (or what groups to make)
my $add_to_group_fileref = $fileref;
my $add_to_group_addfname = $addfname;
my $newgrptext = '';
my $grphash = '';
if ($is_public_header) {
$grphash = $pubheaders_group_hash; # done!
} else {
$grphash = $libsrc_group_hash;
my @splitpath = split(/\//, dirname($addpath));
if ($splitpath[0] eq 'src') {
shift @splitpath;
}
while (my $elem = shift(@splitpath)) {
my $g = $pbxgroups{$grphash};
my $kids = $$g{'children'};
my $found = 0;
foreach (keys %$kids) {
my $hash = $_;
my $fname = $$kids{$hash};
if (uc($fname) eq uc($elem)) {
$grphash = $hash;
$found = 1;
last;
}
}
unshift(@splitpath, $elem), last if (not $found);
}
if (@splitpath) { # still elements? We need to build groups.
my $newgroupref = generate_xcode_id();
$add_to_group_fileref = $newgroupref;
$add_to_group_addfname = $splitpath[0];
while (my $elem = shift(@splitpath)) {
my $lastelem = @splitpath ? 0 : 1;
my $childhash = $lastelem ? $fileref : generate_xcode_id();
my $childpath = $lastelem ? $addfname : $splitpath[0];
$newgrptext .= "\t\t$newgroupref /* $elem */ = {\n";
$newgrptext .= "\t\t\tisa = PBXGroup;\n";
$newgrptext .= "\t\t\tchildren = (\n";
$newgrptext .= "\t\t\t\t$childhash /* $childpath */,\n";
$newgrptext .= "\t\t\t);\n";
$newgrptext .= "\t\t\tpath = $elem;\n";
$newgrptext .= "\t\t\tsourceTree = \"<group>\";\n";
$newgrptext .= "\t\t};\n";
$newgroupref = $childhash;
}
}
}
my $tmpfname = "$pbxprojfname.tmp";
open $fh, '>', $tmpfname or die("Failed to open '$tmpfname': $!\n");
my $add_to_this_group = 0;
$pbxgroup_state = 0;
$lineno = 0;
foreach (@pbxproj) {
$lineno++;
if ($pbxgroup_state == 0) {
# Drop in new references at the end of their sections...
if (/\A\/\* End PBXBuildFile section \*\/\Z/) {
print $fh "\t\t$buildfileref /* $addfname in $srcs_or_headers */ = {isa = PBXBuildFile; fileRef = $fileref /* $addfname */;";
if ($is_public_header) {
print $fh " settings = {ATTRIBUTES = (Public, ); };";
}
print $fh " };\n";
} elsif (/\A\/\* End PBXFileReference section \*\/\Z/) {
print $fh "\t\t$fileref /* $addfname */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = $addfiletype; name = $addfname; path = $filerefpath; sourceTree = \"<group>\"; };\n";
} elsif (/\A\/\* Begin PBXGroup section \*\/\Z/) {
$pbxgroup_state = 1;
} elsif (/\A\/\* Begin PBXSourcesBuildPhase section \*\/\Z/) {
$pbxgroup_state = 5;
}
} elsif ($pbxgroup_state == 1) {
if (/\A\t\t([A-F0-9]{24}) \/\* (.*?) \*\/ \= \{\Z/) {
$pbxgroup_state++;
$add_to_this_group = $1 eq $grphash;
} elsif (/\A\/\* End PBXGroup section \*\/\Z/) {
print $fh $newgrptext;
$pbxgroup_state = 0;
}
} elsif ($pbxgroup_state == 2) {
if (/\A\t\t\tchildren \= \(\Z/) {
$pbxgroup_state++;
}
} elsif ($pbxgroup_state == 3) {
if (/\A\t\t\t\);\Z/) {
if ($add_to_this_group) {
print $fh "\t\t\t\t$add_to_group_fileref /* $add_to_group_addfname */,\n";
}
$pbxgroup_state++;
}
} elsif ($pbxgroup_state == 4) {
if (/\A\t\t\};\Z/) {
$add_to_this_group = 0;
}
$pbxgroup_state = 1;
} elsif ($pbxgroup_state == 5) {
if (/\A\t\t\t\);\Z/) {
if ($srcs_or_headers eq 'Sources') {
print $fh "\t\t\t\t$buildfileref /* $addfname in $srcs_or_headers */,\n";
}
$pbxgroup_state = 0;
}
}
print $fh "$_\n";
}
close($fh);
rename($tmpfname, $pbxprojfname);
}
my %visualc_references = ();
sub generate_visualc_id { # these are just standard Windows GUIDs.
my @chars = ('0'..'9', 'a'..'f');
my $str;
do {
my $len = 24;
$str = '0000'; # start and end with '0000' so we know we added it.
while ($len--) {
$str .= $chars[rand @chars]
};
$str .= '0000'; # start and end with '0000' so we know we added it.
$str =~ s/\A(........)(....)(....)(............)\Z/$1-$2-$3-$4/; # add dashes in the appropriate places.
} while (defined($visualc_references{$str}));
$visualc_references{$str} = 1; # so future calls can't generate this one.
return $str;
}
sub process_visualstudio {
my $addpath = shift;
my $vcxprojfname = shift;
my $lineno;
%visualc_references = ();
my $is_public_header = ($addpath =~ /\Ainclude\/SDL3\//) ? 1 : 0;
my $addfname = basename($addpath);
my $addext = '';
if ($addfname =~ /\.(.*?)\Z/) {
$addext = $1;
}
my $isheader = 0;
if ($addext eq 'c') {
$isheader = 0;
} elsif ($addext eq 'm') {
return; # don't add Objective-C files to Visual Studio projects!
} elsif ($addext eq 'h') {
$isheader = 1;
} else {
die("Unexpected file extension '$addext'\n");
}
my $fh;
open $fh, '<', $vcxprojfname or die("Failed to open '$vcxprojfname': $!\n");
chomp(my @vcxproj = <$fh>);
close($fh);
my $vcxgroup_state;
my $rawaddvcxpath = "$addpath";
$rawaddvcxpath =~ s/\//\\/g;
# Figure out relative path from vcxproj file...
my $addvcxpath = '';
my @subdirs = split(/\//, $vcxprojfname);
pop @subdirs;
foreach (@subdirs) {
$addvcxpath .= "..\\";
}
$addvcxpath .= $rawaddvcxpath;
my $prevname = undef;
my $tmpfname;
$tmpfname = "$vcxprojfname.tmp";
open $fh, '>', $tmpfname or die("Failed to open '$tmpfname': $!\n");
my $added = 0;
$added = 0;
$vcxgroup_state = 0;
$prevname = undef;
$lineno = 0;
foreach (@vcxproj) {
$lineno++;
if ($vcxgroup_state == 0) {
if (/\A \<ItemGroup\>\Z/) {
$vcxgroup_state = 1;
$prevname = undef;
}
} elsif ($vcxgroup_state == 1) {
if (/\A \<ClInclude .*\Z/) {
$vcxgroup_state = 2 if $isheader;
} elsif (/\A \<ClCompile .*\Z/) {
$vcxgroup_state = 3 if not $isheader;
} elsif (/\A \<\/ItemGroup\>\Z/) {
$vcxgroup_state = 0;
$prevname = undef;
}
}
# Don't do elsif, we need to check this line again.
if ($vcxgroup_state == 2) {
if (/\A <ClInclude Include="(.*?)" \/\>\Z/) {
my $nextname = $1;
if ((not $added) && (((not defined $prevname) || (uc($prevname) lt uc($addvcxpath))) && (uc($nextname) gt uc($addvcxpath)))) {
print $fh " <ClInclude Include=\"$addvcxpath\" />\n";
$vcxgroup_state = 0;
$added = 1;
}
$prevname = $nextname;
} elsif (/\A \<\/ItemGroup\>\Z/) {
if ((not $added) && ((not defined $prevname) || (uc($prevname) lt uc($addvcxpath)))) {
print $fh " <ClInclude Include=\"$addvcxpath\" />\n";
$vcxgroup_state = 0;
$added = 1;
}
}
} elsif ($vcxgroup_state == 3) {
if (/\A <ClCompile Include="(.*?)" \/\>\Z/) {
my $nextname = $1;
if ((not $added) && (((not defined $prevname) || (uc($prevname) lt uc($addvcxpath))) && (uc($nextname) gt uc($addvcxpath)))) {
print $fh " <ClCompile Include=\"$addvcxpath\" />\n";
$vcxgroup_state = 0;
$added = 1;
}
$prevname = $nextname;
} elsif (/\A \<\/ItemGroup\>\Z/) {
if ((not $added) && ((not defined $prevname) || (uc($prevname) lt uc($addvcxpath)))) {
print $fh " <ClCompile Include=\"$addvcxpath\" />\n";
$vcxgroup_state = 0;
$added = 1;
}
}
}
print $fh "$_\n";
}
close($fh);
rename($tmpfname, $vcxprojfname);
my $vcxfiltersfname = "$vcxprojfname.filters";
open $fh, '<', $vcxfiltersfname or die("Failed to open '$vcxfiltersfname': $!\n");
chomp(my @vcxfilters = <$fh>);
close($fh);
my $newgrptext = '';
my $filter = '';
if ($is_public_header) {
$filter = 'API Headers';
} else {
$filter = lc(dirname($addpath));
$filter =~ s/\Asrc\///; # there's no filter for the base "src/" dir, where SDL.c and friends live.
$filter =~ s/\//\\/g;
$filter = '' if $filter eq 'src';
if ($filter ne '') {
# see if the filter already exists, otherwise add it.
my %existing_filters = ();
my $current_filter = '';
my $found = 0;
foreach (@vcxfilters) {
# These lines happen to be unique, so we don't have to parse down to find this section.
if (/\A \<Filter Include\="(.*?)"\>\Z/) {
$current_filter = lc($1);
if ($current_filter eq $filter) {
$found = 1;
}
} elsif (/\A \<UniqueIdentifier\>\{(.*?)\}\<\/UniqueIdentifier\>\Z/) {
$visualc_references{$1} = $current_filter; # gather up existing GUIDs to avoid duplicates.
$existing_filters{$current_filter} = $1;
}
}
if (not $found) { # didn't find it? We need to build filters.
my $subpath = '';
my @splitpath = split(/\\/, $filter);
while (my $elem = shift(@splitpath)) {
$subpath .= "\\" if ($subpath ne '');
$subpath .= $elem;
if (not $existing_filters{$subpath}) {
my $newgroupref = generate_visualc_id();
$newgrptext .= " <Filter Include=\"$subpath\">\n";
$newgrptext .= " <UniqueIdentifier>{$newgroupref}</UniqueIdentifier>\n";
$newgrptext .= " </Filter>\n"
}
}
}
}
}
$tmpfname = "$vcxfiltersfname.tmp";
open $fh, '>', $tmpfname or die("Failed to open '$tmpfname': $!\n");
$added = 0;
$vcxgroup_state = 0;
$prevname = undef;
$lineno = 0;
foreach (@vcxfilters) {
$lineno++;
# We cheat here, because these lines are unique, we don't have to fully parse this file.
if ($vcxgroup_state == 0) {
if (/\A \<Filter Include\="(.*?)"\>\Z/) {
if ($newgrptext ne '') {
$vcxgroup_state = 1;
$prevname = undef;
}
} elsif (/\A \<ClInclude .*\Z/) {
if ($isheader) {
$vcxgroup_state = 2;
$prevname = undef;
}
} elsif (/\A \<ClCompile .*\Z/) {
if (not $isheader) {
$vcxgroup_state = 3;
$prevname = undef;
}
}
}
# Don't do elsif, we need to check this line again.
if ($vcxgroup_state == 1) {
if (/\A \<\/ItemGroup\>\Z/) {
print $fh $newgrptext;
$newgrptext = '';
$vcxgroup_state = 0;
}
} elsif ($vcxgroup_state == 2) {
if (/\A <ClInclude Include="(.*?)"/) {
my $nextname = $1;
if ((not $added) && (((not defined $prevname) || (uc($prevname) lt uc($addvcxpath))) && (uc($nextname) gt uc($addvcxpath)))) {
print $fh " <ClInclude Include=\"$addvcxpath\"";
if ($filter ne '') {
print $fh ">\n";
print $fh " <Filter>$filter</Filter>\n";
print $fh " </ClInclude>\n";
} else {
print $fh " />\n";
}
$added = 1;
}
$prevname = $nextname;
} elsif (/\A \<\/ItemGroup\>\Z/) {
if ((not $added) && ((not defined $prevname) || (uc($prevname) lt uc($addvcxpath)))) {
print $fh " <ClInclude Include=\"$addvcxpath\"";
if ($filter ne '') {
print $fh ">\n";
print $fh " <Filter>$filter</Filter>\n";
print $fh " </ClInclude>\n";
} else {
print $fh " />\n";
}
$added = 1;
}
$vcxgroup_state = 0;
}
} elsif ($vcxgroup_state == 3) {
if (/\A <ClCompile Include="(.*?)"/) {
my $nextname = $1;
if ((not $added) && (((not defined $prevname) || (uc($prevname) lt uc($addvcxpath))) && (uc($nextname) gt uc($addvcxpath)))) {
print $fh " <ClCompile Include=\"$addvcxpath\"";
if ($filter ne '') {
print $fh ">\n";
print $fh " <Filter>$filter</Filter>\n";
print $fh " </ClCompile>\n";
} else {
print $fh " />\n";
}
$added = 1;
}
$prevname = $nextname;
} elsif (/\A \<\/ItemGroup\>\Z/) {
if ((not $added) && ((not defined $prevname) || (uc($prevname) lt uc($addvcxpath)))) {
print $fh " <ClCompile Include=\"$addvcxpath\"";
if ($filter ne '') {
print $fh ">\n";
print $fh " <Filter>$filter</Filter>\n";
print $fh " </ClCompile>\n";
} else {
print $fh " />\n";
}
$added = 1;
}
$vcxgroup_state = 0;
}
}
print $fh "$_\n";
}
close($fh);
rename($tmpfname, $vcxfiltersfname);
}
# Mainline!
chdir(dirname($0)); # assumed to be in build-scripts
chdir('..'); # head to root of source tree.
foreach (@ARGV) {
s/\A\.\///; # Turn "./path/to/file.txt" into "path/to/file.txt"
my $arg = $_;
process_xcode($arg, 'Xcode/SDL/SDL.xcodeproj/project.pbxproj');
process_visualstudio($arg, 'VisualC/SDL/SDL.vcxproj');
process_visualstudio($arg, 'VisualC-GDK/SDL/SDL.vcxproj');
}
print("Done. Please run `git diff` and make sure this looks okay!\n");
exit(0);
|