summaryrefslogtreecommitdiff
path: root/devtools/bin/mksln.pl
blob: aec1c8897e635a6e7e5da02bacf996354e89ff73 (plain) (blame)
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
#!perl
use XML::Simple;
use Data::Dumper;
use Getopt::Long;
use File::Basename;
use Cwd 'abs_path';
use Cwd;
use Digest::MD5  qw(md5 md5_hex md5_base64);


GetOptions( "verbose"=>\$verbose,
			"projlist"=>\$projlist,
			"x360"=>\$x360 );

$sln_name=shift || &PrintArgumentSummaryAndExit;

my $curdir=cwd;

$curdir=~ (m@(^.*/[a-z_]*src[0-9]?)@i) || die "Can't determine srcroot from current directory $curdir";

$srcroot=lc($1);

$linker_tool_name="VCLinkerTool";
$linker_tool_name="VCX360LinkerTool" if ($x360);

@output_only_projects_dependent_upon = ();

&ReadVPCProjects;
if ( $sln_name =~ /^\@(\S+)/)
{
	$sln_name=$1;
	&ReadGroup($1);
	foreach $proj (@PROJS)
	{
		&AddProject(lc(abs_path($proj)),1);
	}
	if ( $projlist )
	{
		&WriteProjectListFile( $sln_name );
	}
	else
	{
		&WriteSolutionFile($sln_name);
	}
}
else
{
	# normal mode
	while($_ = shift )
	{
	  if ( /^\@(\S+)/)
		{
		  # accept group names
		  &ReadGroup($1);
		  foreach $proj (@PROJS)
			{
			  &AddProject(lc(abs_path($proj)),1);
			}
		}
	  elsif ( /^\*(\S+)/)
		{
		  push( @output_only_projects_dependent_upon, lc( $1 ) );
		  &ReadGroup("everything");
		  foreach $proj (@PROJS)
			{
			  &AddProject(lc(abs_path($proj)),0);
			}
		}
	  else
		{
		  unless(/\.vcproj/)		# if no extension specified, assume its a project name from projects.vgc
			{
			  $_=$projpath{lc($_)} if length($projpath{lc($_)});
			}
		  foreach $path (split(/\s+/,$_))
			{
			  $path=~s@\s+@@g;
			  &AddProject(lc(abs_path($path)),1) if length($path);
			}
		}
	}
	if ( $projlist )
	{
		&WriteProjectListFile( $sln_name );
	}
	else
	{
		&WriteSolutionFile($sln_name);
	}
}


sub WriteSolutionFile
{
	local($sln)=@_;
	$sln="$sln.sln" unless ( $sln=~/\./); # add extension if needed
	if ( ( -e $sln && ( ! ( -w $sln ) ) ) )
	{
		print STDERR "$sln is write-protected. Doing p4 edit.\n";
		print `p4 edit $sln`;
		die "Failed to make $sln writeable" if ( ! ( -w $sln ) );
	}
	open(SLN,">$sln" ) || die "can't open output $sln";
	# generate a guid for the sln
	my $sln_guid="8BC9CEB8-8B4A-11D0-8D11-".substr(uc(md5_hex(basename($sln))),0,12);

	print SLN "\xef\xbb\xbf\nMicrosoft Visual Studio Solution File, Format Version 9.00\n# Visual Studio 2005\n";
	foreach $proj (@PROJECTS)
	{
	  # check dependencies for "*" projects
	  if ( (! length( $force_project_inclusion{$proj} ) ) &&
		   ( @output_only_projects_dependent_upon ))
		{
			my $skip_it = 1;
			foreach $output_only ( @output_only_projects_dependent_upon )
			{
				$skip_it = 0 if ( $output_only eq lc( $proj ) );
				foreach $lib (split(/,/,$depends_on{$proj}))
				{
					$skip_it = 0 if ( $output_only eq lc($lib) );
					foreach $prvd (split(/,/,$provider{$lib}))
					{
						$skip_it = 0 if ( $output_only eq $prvd );
					}
				}
			}
			next if ( $skip_it );
		}

	  print SLN "Project(\"{",$sln_guid,"}\") = \"$proj\", \"$relpath{$proj}\", \"$guid{$proj}\"\n";
	  
	  #, now do dependencies
	  if ( length($depends_on{$proj} ) )
		{
		  print SLN "\tProjectSection(ProjectDependencies) = postProject\n";
		  foreach $lib (split(/,/,$depends_on{$proj}))
			{
			  if ( length($provider{$lib}) )
				{
				  foreach $prvd (split(/,/,$provider{$lib}))
					{
					  print SLN "\t\t$guid{$prvd} = $guid{$prvd}\n" if ( length($prvd) );
					}
				}
			  else
				{
				  print "I don't know who provides $lib for $proj\n" if ( $verbose && length($lib) );
				}
			  
			}
		  print SLN "\tEndProjectSection\n";
		}
	  print SLN "EndProject\n";
	}
	
	print SLN "Global\n";
	print SLN "\tGlobalSection(SolutionProperties) = preSolution\n";
	print SLN "\t\tHideSolutionNode = FALSE\n";
	print SLN "\tEndGlobalSection\n";
	print SLN "EndGlobal\n";
	close SLN;
}

sub WriteProjectListFile
{
	local($txtfile) = @_;
	$txtfile = "$txtfile.txt" unless ( $txtfile=~/\./); # add extension if needed
	open(SLN,">$txtfile" ) || die "can't open output $txtfile";

	foreach $proj (@PROJECTS)
	{
	  # check dependencies for "*" projects
	  if ( (! length( $force_project_inclusion{$proj} ) ) &&
		   ( @output_only_projects_dependent_upon ))
		{
			my $skip_it = 1;
			foreach $output_only ( @output_only_projects_dependent_upon )
			{
				$skip_it = 0 if ( $output_only eq lc( $proj ) );
				foreach $lib (split(/,/,$depends_on{$proj}))
				{
					$skip_it = 0 if ( $output_only eq lc($lib) );
					foreach $prvd (split(/,/,$provider{$lib}))
					{
						$skip_it = 0 if ( $output_only eq $prvd );
					}
				}
			}
			next if ( $skip_it );
		}

	  push @plist, $proj;
	}
	# now, we need to satisfy all dependencies
	while( $#plist >= 0)
	{
		@worklist=@plist;
		undef @plist;
	  PROJECT: foreach $proj( @worklist )
	  {
		  if ( length($depends_on{$proj} ) )
		  {
			  foreach $lib (split(/,/,$depends_on{$proj}))
			  {
				  if ( length($provider{$lib}) )
				  {
					  foreach $prvd (split(/,/,$provider{$lib}))
					  {
						  if ( length( $prvd ) && ( !$already_did{$prvd} ) )
						  {
							  push @plist, $proj;			# can't do it yet
							next PROJECT;
						  }
					  }
				  }
			  }
		  }
		  $already_did{$proj} = 1;
		  print SLN "$relpath{$proj}\n";
	  }
	}
	close SLN;
}

sub PrintArgumentSummaryAndExit
{
	print "Format of command is\n";
	my $switches="[ -projlist -verbose -x360 ]";
	print "\t MKSLN $switches <solutionname.sln > proj1.vcproj proj2.vcproj ...\n";
	print "OR\t MKSLN $switches \@vpcgroupname\n";
	print "OR\t MKSLN $switches sln_name \@vpcgroupname\n";
	print "OR\t MKSLN $switches sln_name *project create a solution including only that project and things dependent on it.\n";
}


sub AddProject
{
	local($fname, $force )=@_;
	local($/);
	print "add project $fname\n" if ( $verbose );
	open( VCP_IN, $fname ) || die "can't open $fname";
	my $xmltext=<VCP_IN>;
	close VCP_IN;
	my $xml=XMLin($xmltext, forcearray => [ 'File', 'Filter' ] ); #, keyattr =>[ 'name', 'key', 'id', 'Name']);
	my $pname=lc($xml->{Name});
	$force_project_inclusion{$pname} = "yes" if ( $force );
	return if ($already_processed{$pname} );
	$already_processed{$pname}=1;
	my $id=$xml->{ProjectGUID};
	unless( length($id) )
	{
	  die "project $fname doesn't have a guid. Generated by an old VPC?";
	}
	$id = "{".$id."}" unless( $id=~ /}/);
	$guid{$pname}=$id;
	push @PROJECTS,$pname;
	$vcprojpath{$pname}=$fname;

	#get targetname
	my $targetname=$xml->{Name};
	# get output target

	my $tools=$xml->{Configurations}->{Configuration}[0];
	# walk the tool list to see if this project outpus a .lib that something might depend on
	my $outputtarget;
	foreach $tool (@{$tools->{'Tool'}})
	{
		if ( $tool->{Name} eq "VCLibrarianTool" )
		{
			my $outputtarget=lc(basename($tool->{OutputFile}));
			$provider{$outputtarget}.=",$pname";
			print "$pname provides $outputtarget\n" if ($verbose);
		}
		if ( $tool->{Name} eq $linker_tool_name )
		  {
			my $outputtarget=$tool->{'ImportLibrary'};
			if ( length($outputtarget) )
			  {
				$outputtarget=~s/\$\(TargetName\)/$targetname/i;
				$outputtarget=lc(basename($outputtarget));
				$outputtarget =~ s/\.lib/_360.lib/ if ( $x360 );
				$provider{$outputtarget}=basename($pname);
				print "$pname provides $outputtarget\n" if ($verbose);
			  }
		  }
	}

	foreach $filter (@{$xml->{Files}->{Filter}})
	{
		foreach $file (@{$filter->{File}})
		{
			my $f = lc($file->{RelativePath});
			if ( $f=~/\.lib$/i) # library dependency
			{
			  my $libname=basename($f);
			  $depends_on{$pname}.=",".$libname;
			  print "$pname depends on $libname\n" if ($verbose);
			}
		}
	}
	# generate relative pathname
	$fname=~s@^$srcroot/@@i;
	$fname=~s@/@\\@g;
	$relpath{$pname}=$fname;
	
}

sub ReadGroup
{
	local($matchgroup)=@_;
	my $curmatch=0;
	open(GROUPS,"$srcroot/vpc_scripts/groups.vgc") || die "can't open groups.vgc";
	while(<GROUPS>)
	{
		&FixupVPCLine;
		if (/^\$Group\s+(.*)$/)
		{
			my $groups=" $1 ";
			$groups=~s@\"@@g;
			$curmatch=0;
			$curmatch=1 if ( $groups=~/ $matchgroup /i );
		}
		elsif ( $curmatch && (/^\s*\"([^\"]+)\"/) )
		{
			my $proj=lc($1);
			my $path=$projpath{$proj};
			if (length($path))
			{
			  foreach $prj (split(/\s+/, $path))
				{
				  $prj=~s@\s+@@g;
				  next unless (length($prj));
				  if ( -e $prj )
					{
					  push @PROJS,$prj;
					  print "found proj $prj\n" if ($verbose);
					}
				  else
					{
					  print STDERR "can't find $prj\n";
					}

				}
			}
			else
			{
				print STDERR "couldn't find project name $proj (group = $matchgroup )\n";
			}
		}
		else
		{
			$curmatch = 0 if (/\}/);
		}			

	}
}

sub ReadVPCProjects
{
	# group mode. ugh 100x harder to parse vpc than .vcproj
	open(PROJS,"$srcroot/vpc_scripts/projects.vgc" ) || die "can't open projects.vgc";
	while(<PROJS>)
	{
		&FixupVPCLine;
		if (/^\s*\$Project\s+\"([^\"]+)\"/)
		{
			$curproj=$1;
		}
		elsif (/^\s*\"([^\"]+)\.vpc\"/)
		{
			my $base = $1;
			$base="$base"."_x360" if ( $x360 );
			$projpath{lc($curproj)}.=" $base.vcproj";
		}
	}
	close PROJS;
}

sub FixupVPCLine
{
	s@[\n\r]@@g;
	s@//.*$@@g;				# kill comments

	# use [] skips. need something smarter here. for now, implicit /allgames except hl1 and portalmp
	$_=undef if ( /\$HL1/);
	$_=undef if ( /\$PORTALMP/);
}