forked from virtuald/vcxproj2cmake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vcxproj2cmake.pl
445 lines (389 loc) · 12.6 KB
/
vcxproj2cmake.pl
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
#!/usr/bin/env perl
use strict;
use warnings;
use utf8;
use feature 'say';
use Readonly;
use autobox::Core;
use Try::Tiny;
use XML::TreePP;
use XML::TreePP::XMLPath;
use Text::Xslate;
use Data::Dumper;
use FindBin;
use File::Basename ();
my $filepath = $ARGV[0]
|| die "Usage: $FindBin::Script <vcxproj path> <target configuration>";
my $target_conf = $ARGV[1]
|| die "Usage: $FindBin::Script <vcxproj path> <target configuration>";
my $filters_path = $filepath . '.filters';
my $vcxproj_xml = XML::TreePP->new()->parsefile($filepath);
my $vcxproj_filters_xml = XML::TreePP->new()->parsefile($filters_path);
my $target = $vcxproj_xml->{Project}->{PropertyGroup}->[0]->{RootNamespace};
my $target_prop = get_target_property($target_conf,
get_propertys($vcxproj_xml));
my $filenode_ref = get_file_node($vcxproj_xml);
my $item_def_ref = get_item_def_group($vcxproj_xml,
$target_prop->{-Condition});
my %filters = get_filters($vcxproj_filters_xml);
make($filenode_ref, $item_def_ref, %filters);
sub get_filters {
my ($vcxproj_filters_xml) = @_;
my %filters = ();
my @itemgroups = @{$vcxproj_filters_xml->{Project}->{ItemGroup}};
# first itemgroup is list of filters
for my $filter ($itemgroups[0]->{Filter}) {
my $filtername = $filter->{-Include};
my @files = ();
@{$filters{$filtername}} = @files;
}
# second itemgroup contains sorce files filter information
for my $clcompile (@{$itemgroups[1]->{ClCompile}}) {
my $filename = $clcompile->{-Include};
my $filter = $clcompile->{Filter};
push @{$filters{$filter}}, $filename;
}
# third itemgroup contains header files filter information
if (ref($itemgroups[2]->{ClInclude}) eq "HASH") {
my $clinclude = $itemgroups[2]->{ClInclude};
my $filename = $clinclude->{-Include};
my $filter = $clinclude->{Filter};
push @{$filters{$filter}}, $filename;
} else {
for my $clinclude (@{$itemgroups[2]->{ClInclude}}) {
my $filename = $clinclude->{-Include};
my $filter = $clinclude->{Filter};
push @{$filters{$filter}}, $filename;
}
}
# fourth itemgroup contains filter information for files that should not compiled (like readme)
# fifth itemgroup contains recources filter
# remove empty arrays
my @toDelete;
for my $filter (keys %filters) {
my @filterfiles = @{$filters{$filter}};
if (scalar @filterfiles == 0) {
push @toDelete, $filter;
}
}
#delete @filters{@toDelete};
return %filters;
}
sub get_target_property {
my ($target_conf, @property_groups) = @_;
my $target_prop;
for my $property (@property_groups) {
if ($property->{-Condition} =~ m{$target_conf}) {
$target_prop = $property;
last;
}
}
return $target_prop;
}
sub get_item_def_group {
my ($vcxproj_xml, $cond) = @_;
my $tppx = new XML::TreePP::XMLPath;
# my $xpath = '/Project/ItemDefinitionGroup[@Condition="'.$cond.'"]';
# warn $xpath;
# my $item_def_ref = $tppx->filterXMLDoc($vcxproj_xml, $xpath);
my $item_def_ref;
for my $item_def ($tppx->filterXMLDoc($vcxproj_xml,
'/Project/ItemDefinitionGroup')) {
if ($item_def->{-Condition} eq $cond) {
$item_def_ref = $item_def;
last;
}
}
return $item_def_ref;
}
sub get_propertys {
my ($vcxproj_xml) = @_;
my $property_node_ref;
my $tppx = new XML::TreePP::XMLPath;
my @property_groups
= $tppx->filterXMLDoc($vcxproj_xml,
'/Project/PropertyGroup[@Label="Configuration"]');
return @property_groups;
}
sub get_file_node {
my ($vcxproj_xml) = @_;
my @itemgroups = @{$vcxproj_xml->{Project}->{ItemGroup}};
my $filenode_ref;
for my $itemgroup (@itemgroups) {
next if (defined $itemgroup->{-Label}
&& $itemgroup->{-Label} eq 'ProjectConfigurations');
for my $nodename (keys %{$itemgroup}) {
my @nodes;
if (ref($itemgroup->{$nodename}) eq 'HASH') {
push @nodes, $itemgroup->{$nodename};
} else {
@nodes = @{$itemgroup->{$nodename}};
}
for my $node (@nodes) {
unless (defined $node->{ExcludedFromBuild}
&& $node->{ExcludedFromBuild}->{-Condition} eq $target_prop->{-Condition}) {
push @{$filenode_ref->{$nodename}}, $node->{-Include};
}
}
}
}
return $filenode_ref;
}
sub render_find_packages {
my ($tx, @srcs) = @_;
my %find_packages_supported_by_cmake = (
# include_pattern => package_name
#'' => 'ALSA',
#'' => 'Armadillo',
#'' => 'ASPELL',
#'' => 'AVIFile',
#'' => 'BISON',
#'' => 'BLAS',
'boost/' => 'Boost',
#'' => 'Bullet',
#'' => 'BZip2',
#'' => 'CABLE',
#'' => 'Coin3D',
#'' => 'CUDA',
#'' => 'Cups',
#'' => 'CURL',
#'' => 'Curses',
#'' => 'CVS',
#'' => 'CxxTest',
#'' => 'Cygwin',
#'' => 'Dart',
#'' => 'DCMTK',
#'' => 'DevIL',
#'' => 'Doxygen',
#'' => 'EXPAT',
#'' => 'FLEX',
#'' => 'FLTK',
#'' => 'FLTK2',
#'' => 'Freetype',
#'' => 'GCCXML',
#'' => 'GDAL',
#'' => 'Gettext',
#'' => 'GIF',
#'' => 'Git',
#'' => 'GLU',
#'' => 'GLUT',
#'' => 'Gnuplot',
#'' => 'GnuTLS',
#'' => 'GTest',
#'' => 'GTK',
#'' => 'GTK2',
#'' => 'HDF5',
#'' => 'HSPELL',
#'' => 'HTMLHelp',
#'' => 'ImageMagick',
#'' => 'ITK',
#'' => 'Jasper',
#'' => 'Java',
#'' => 'JNI',
#'' => 'JPEG',
#'' => 'KDE3',
#'' => 'KDE4',
#'' => 'LAPACK',
#'' => 'LATEX',
#'' => 'LibArchive',
#'' => 'LibLZMA',
#'' => 'LibXml2',
#'' => 'LibXslt',
#'' => 'Lua50',
#'' => 'Lua51',
#'' => 'Matlab',
#'' => 'MFC',
#'' => 'Motif',
#'' => 'MPEG',
#'' => 'MPEG2',
#'' => 'MPI',
#'' => 'OpenAL',
#'' => 'OpenGL',
#'' => 'OpenMP',
#'' => 'OpenSceneGraph',
'openssl/' => 'OpenSSL',
#'' => 'OpenThreads',
#'' => 'osg',
#'' => 'osgAnimation',
#'' => 'osgDB',
#'' => 'osgFX',
#'' => 'osgGA',
#'' => 'osgIntrospection',
#'' => 'osgManipulator',
#'' => 'osgParticle',
#'' => 'osgPresentation',
#'' => 'osgProducer',
#'' => 'osgQt',
#'' => 'osgShadow',
#'' => 'osgSim',
#'' => 'osgTerrain',
#'' => 'osgText',
#'' => 'osgUtil',
#'' => 'osgViewer',
#'' => 'osgVolume',
#'' => 'osgWidget',
#'' => 'osg_functions',
#'' => 'PackageHandleStandardArgs',
#'' => 'PackageMessage',
#'' => 'Perl',
#'' => 'PerlLibs',
#'' => 'PHP4',
#'' => 'PhysFS',
#'' => 'Pike',
#'' => 'PkgConfig',
#'' => 'PNG',
#'' => 'PostgreSQL',
#'' => 'Producer',
#'' => 'Protobuf',
#'' => 'PythonInterp',
#'' => 'PythonLibs',
#'' => 'Qt',
#'' => 'Qt3',
#'' => 'Qt4',
#'' => 'QuickTime',
#'' => 'RTI',
#'' => 'Ruby',
'SDL/' => 'SDL',
#'' => 'SDL_image',
#'' => 'SDL_mixer',
#'' => 'SDL_net',
#'' => 'SDL_sound',
#'' => 'SDL_ttf',
#'' => 'SelfPackers',
#'' => 'Squish',
#'' => 'Subversion',
#'' => 'SWIG',
#'' => 'TCL',
#'' => 'Tclsh',
#'' => 'TclStub',
#'' => 'Threads',
#'' => 'TIFF',
#'' => 'UnixCommands',
#'' => 'VTK',
#'' => 'Wget',
#'' => 'Wish',
#'' => 'wxWidgets',
#'' => 'wxWindows',
#'' => 'X11',
#'' => 'XMLRPC',
'zlib.h' => 'ZLIB',
);
my $rendered_find_packages = '';
# find all libs that used
my @libs_to_find_patterns = keys(%find_packages_supported_by_cmake);
my @libs;
for my $file (@srcs) {
# find libs that the file uses
open my $info, $file or die "Could not open $file: $!";
while( my $line = <$info>) {
if ($line =~ /#\s*include/) {
my @lib_pattern_indexes_to_delete;
for my $lib_match (@libs_to_find_patterns) {
if ($line =~ m/^\s*#\s*include\s*<$lib_match/) {
my $lib = $find_packages_supported_by_cmake{$lib_match};
push @libs, $lib;
my $lib_index = 0;
$lib_index++ until $libs_to_find_patterns[$lib_index] eq $lib_match;
push @lib_pattern_indexes_to_delete, $lib_index;
}
}
for my $index (@lib_pattern_indexes_to_delete) {
splice(@libs_to_find_patterns, $index, 1);
}
}
}
close $info;
}
for my $lib (@libs) {
my %vars = (
package => $lib,
package_upcase => uc $lib,
);
warn Dumper(%vars);
try {
my $render = $tx->render( 'CMakeFindPackage.tx', \%vars );
$rendered_find_packages = $rendered_find_packages . $render;
} catch {
print "caught error: $_\n";
};
}
return Text::Xslate::Util::mark_raw($rendered_find_packages);
}
sub render_source_groups {
my ($tx, %filters) = @_;
my $rendered_source_groups = '';
for my $filter (keys %filters) {
my @files = @{$filters{$filter}};
$filter =~ s/\\/\\\\/g;
my %vars = (
flter => $filter,
files => +(join "\n\t", @files),
);
warn Dumper(%vars);
try {
my $render = $tx->render( 'CMakeSourceGroup.tx', \%vars );
$rendered_source_groups = $rendered_source_groups . $render;
} catch {
print "caught error: $_\n";
};
}
return Text::Xslate::Util::mark_raw($rendered_source_groups);
}
sub make {
my ($filenode_ref, $item_def_ref, %filters) = @_;
my @srcs = @{$filenode_ref->{ClCompile}};
my @headers = @{$filenode_ref->{ClInclude}};
my @deps = ();
my @defs = ();
my @includes = ();
my $add_dir = $item_def_ref->{ClCompile}->{AdditionalIncludeDirectories};
if (defined($add_dir)) {
$add_dir =~ s{\\}{/}g;
my @includes =
grep { $_ ne '%(AdditionalIncludeDirectories)' }
split ';', $add_dir;
}
my $def = $item_def_ref->{ClCompile}->{PreprocessorDefinitions};
# add prefix -D. example> -DSHP
if (defined($def)) {
@defs =
map { "-D$_"; }
grep { $_ ne '%(PreprocessorDefinitions)' }
split ';', $def;
}
my $add_dep = $item_def_ref->{Link}->{AdditionalDependencies};
if (defined($add_dep)) {
$add_dep =~ s{\\}{/}g;
my @deps =
grep { $_ ne '%(AdditionalDependencies)' }
split ';', $add_dep;
}
warn Dumper(@includes);
my $tx = Text::Xslate->new(
syntax => 'TTerse',
path => [ File::Basename::dirname($0) ],
);
my $rendered_find_packages = render_find_packages($tx, @headers, @srcs);
my $rendered_source_groups = render_source_groups($tx, %filters);
my %vars = (
type => $target_prop->{ConfigurationType},
charset => $target_prop->{CharacterSet},
target => $target,
def => +(join "\n\t", @defs),
lib => +(join "\n\t", @deps),
include => +(join "\n\t", @includes),
src => +(join "\n\t", @srcs),
header => +(join "\n\t", @headers),
source_groups => $rendered_source_groups,
find_packages => $rendered_find_packages,
);
warn Dumper(%vars);
my $render_text;
try {
$render_text = $tx->render( 'CMakeLists.tx', \%vars );
} catch {
print "caught error: $_\n";
};
open my $cmakelists_file, '>', 'CMakeLists.txt';
print {$cmakelists_file} $render_text;
}