xref: /aoo42x/main/oowintool (revision 01d9dfef)
1#!/usr/bin/perl -w
2
3use File::Copy;
4
5my $output_format = 'u';
6
7sub reg_get_value($)
8{
9    # it is believed that the registry moves keys around
10    # depending on OS version, this will de-mangle that
11    my $key = shift;
12    my $fhandle;
13    my $value;
14
15    open ($fhandle, "/proc/registry/$key") || return;
16    # reg keys have 0x00 0x5c at the end
17    $value = (split /\0/, <$fhandle>)[0];
18    close ($fhandle);
19
20    if ( defined $value ) {
21        chomp ($value);
22        $value =~ s|\r\n||;
23#    print "Value '$value' at '$key'\n";
24    }
25
26    return $value;
27}
28
29sub reg_find_key($)
30{
31    # it is believed that the registry moves keys around
32    # depending on OS version, this will de-mangle that
33    my $key = shift;
34    $key =~ s| |\\ |;
35    $key = `cd /proc/registry/ ; ls $key`;
36
37    return $key;
38}
39
40sub print_syntax()
41{
42    print "oowintool [option] ...\n";
43    print " encoding options\n";
44    print "   -w   - windows form\n";
45    print "   -u   - unix form (default)\n";
46    print " commands:\n";
47    print "   --msvc-ver              - dump version of MSVC eg. 6.0\n";
48    print "   --msvc-copy-dlls <dest> - copy msvc[pr]??.dlls into <dest>/msvcp??/\n";
49    print "   --msvc-productdir       - dump productdir\n";
50    print "   --msvs-productdir       - dump productdir\n";
51    print "   --dotnetsdk-dir         - dump .Net SDK path\n";
52    print "   --csc-compilerdir       - dump .Net SDK compiler path\n";
53    print "   --psdk-home             - dump psdk install dir\n";
54    print "   --jdk-home              - dump the jdk install dir\n";
55    print "   --nsis-dir              - dump NSIS path\n";
56    print "   --help                  - this message\n";
57}
58
59sub cygpath($$$)
60{
61    my ($path, $input_format, $format) = @_;
62
63    return $path if ( ! defined $path );
64    # Strip trailing path separators
65    if ($input_format eq 'u') {
66	$path =~ s|/*\s*$||;
67    } else {
68	$path =~ s|\\*\s*$||;
69    }
70
71    # 'Unterminated quoted string errors' from 'ash' when
72    # forking cygpath  so - reimplement cygpath in perl [ gack ]
73    if ($format eq 'u' && $input_format eq 'w') {
74	$path =~ s|\\|/|g;
75	$path =~ s|([a-zA-Z]):/|/cygdrive/$1/|g;
76    }
77    elsif ($format eq 'w' && $input_format eq 'u') {
78	$path =~ s|/cygdrive/([a-zA-Z])/|/$1/|g;
79	$path =~ s|/|\\|g;
80    }
81
82    return $path;
83}
84
85sub print_path($$)
86{
87    my ($path, $unix) = @_;
88
89    $path = cygpath ($path, $unix, $output_format);
90
91    print $path;
92}
93
94sub print_psdk_home()
95{
96    my ($value, $key);
97    $value = reg_get_value ('HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/Microsoft SDKs/Windows/v6.1/InstallationFolder');
98    if (!defined $value)
99    {
100        $value = reg_get_value ('HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/Microsoft SDKs/Windows/CurrentInstallFolder');
101    }
102    if (!defined $value)
103    {
104	    $value = reg_get_value ('HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/MicrosoftSDK/Directories/Install Dir');
105    }
106    if (!defined $value)
107    {
108	    $key = reg_find_key ('HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/MicrosoftSDK/InstalledSDKs/*/Install Dir');
109	    $value = reg_get_value ($key);
110    }
111    if (!defined $value)
112    {
113        my $dir = cygpath (find_msvc()->{'product_dir'}, 'w', $output_format);
114		$value = `/bin/find "$dir" -iname platformsdk | head -n 1`;
115    }
116
117    defined $value || die "psdk not found";
118
119    print cygpath ($value, 'w', $output_format);
120}
121
122my %msvc_net_2003 = (
123    'ver' => '7.1',
124    'key' => 'Microsoft/VisualStudio/7.1/Setup/VC/ProductDir',
125    'dll_path' => '../Visual Studio .NET Professional 2003 - English',
126    'dll_suffix' => '71'
127);
128my %msvs_net_2003 = (
129    'ver' => '7.1',
130    'key' => 'Microsoft/VisualStudio/7.1/Setup/VS/ProductDir',
131    'dll_path' => 'Visual Studio .NET Professional 2003 - English',
132    'dll_suffix' => '71'
133);
134my %msvs_net_2003_ea = (
135    'ver' => '7.1',
136    'key' => 'Microsoft/VisualStudio/7.1/Setup/VS/ProductDir',
137    'dll_path' => 'Visual Studio .NET Enterprise Architect 2003 - English', # testme ...
138    'dll_suffix' => '71'
139);
140my %msvs_express_2005 = (
141    'ver' => '8.0',
142    'key' => 'Microsoft/VCExpress/8.0/Setup/VS/ProductDir',
143    'dll_path' => '../SDK/v2.0/Bin',
144    'dll_suffix' => '80'
145);
146my %msvc_express_2005 = (
147    'ver' => '8.0',
148    'key' => 'Microsoft/VCExpress/8.0/Setup/VC/ProductDir',
149    'dll_path' => '../SDK/v2.0/Bin',
150    'dll_suffix' => '80'
151);
152my %msvs_2005 = (
153    'ver' => '8.0',
154    'key' => 'Microsoft/VisualStudio/8.0/Setup/VS/ProductDir',
155    'dll_path' => 'Visual Studio .NET Professional 2005 - English',
156    'dll_suffix' => '80'
157);
158my %msvc_2005 = (
159    'ver' => '8.0',
160    'key' => 'Microsoft/VisualStudio/8.0/Setup/VC/ProductDir',
161    'dll_path' => '../SDK/v2.0/Bin',
162    'dll_suffix' => '80'
163);
164my %msvs_2008 = (
165    'ver' => '9.0',
166    'key' => 'Microsoft/VisualStudio/9.0/Setup/VS/ProductDir',
167    'dll_path' => 'VC/redist/x86/Microsoft.VC90.CRT',
168    'dll_suffix' => '90'
169);
170my %msvc_2008 = (
171    'ver' => '9.0',
172    'key' => 'Microsoft/VisualStudio/9.0/Setup/VC/ProductDir',
173    'dll_path' => 'redist/x86/Microsoft.VC90.CRT',
174    'dll_suffix' => '90'
175);
176my %msvs_express_2008 = (
177    'ver' => '9.0',
178    'key' => 'Microsoft/VCExpress/9.0/Setup/VS/ProductDir',
179    'dll_path' => 'VC/redist/x86/Microsoft.VC90.CRT',
180    'dll_suffix' => '90'
181);
182my %msvc_express_2008 = (
183    'ver' => '9.0',
184    'key' => 'Microsoft/VCExpress/9.0/Setup/VC/ProductDir',
185    'dll_path' => 'redist/x86/Microsoft.VC90.CRT',
186    'dll_suffix' => '90'
187);
188
189sub find_msvs()
190{
191    my @ms_versions = ( \%msvs_2008, \%msvs_express_2008, \%msvs_2005, \%msvs_express_2005, \%msvs_net_2003_ea, \%msvs_net_2003 );
192
193    for $ver (@ms_versions)
194    {
195	my $install = reg_get_value ("HKEY_LOCAL_MACHINE/SOFTWARE/" . $ver->{'key'});
196	if (defined $install && $install ne '') {
197	    $ver->{'product_dir'} = $install;
198	    return $ver;
199	}
200    }
201    die "Can't find MS Visual Studio / VC++";
202}
203
204sub find_msvc()
205{
206    my @ms_versions = ( \%msvc_2008, \%msvc_express_2008, \%msvc_2005, \%msvc_express_2005, \%msvc_net_2003 );
207
208    for $ver (@ms_versions)
209    {
210	my $install = reg_get_value ("HKEY_LOCAL_MACHINE/SOFTWARE/" . $ver->{'key'});
211	if (defined $install && $install ne '') {
212	    $ver->{'product_dir'} = $install;
213	    return $ver;
214	}
215    }
216    die "Can't find MS Visual Studio / VC++";
217}
218
219sub print_msvc_ver()
220{
221    my $ver = find_msvc();
222    print $ver->{'ver'};
223}
224
225sub print_msvc_product_dir()
226{
227    my $ver = find_msvc();
228    print cygpath ($ver->{'product_dir'}, 'w', $output_format);
229}
230
231sub print_msvs_productdir()
232{
233    my $ver = find_msvs();
234    print cygpath ($ver->{'product_dir'}, 'w', $output_format);
235}
236
237sub print_csc_compiler_dir()
238{
239    my $dir = cygpath (reg_get_value ("HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/.NETFramework/InstallRoot"), 'w', $output_format);
240    my $csc_exe = `/bin/find "$dir" -iname csc.exe | grep "v2\." | head -n 1`;
241    print `dirname $csc_exe`;
242}
243
244sub print_dotnetsdk_dir()
245{
246    my $dir =
247          reg_get_value ("HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/.NETFramework/sdkInstallRootv1.1") ||
248          reg_get_value ("HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/.NETFramework/sdkInstallRootv2.0");
249    print cygpath ($dir, 'w', $output_format);
250}
251
252sub print_jdk_dir()
253{
254    my $dir =
255	  reg_get_value ("HKEY_LOCAL_MACHINE/SOFTWARE/JavaSoft/Java\ Development\ Kit/1.7/JavaHome") ||
256	  reg_get_value ("HKEY_LOCAL_MACHINE/SOFTWARE/JavaSoft/Java\ Development\ Kit/1.6/JavaHome") ||
257	  reg_get_value ("HKEY_LOCAL_MACHINE/SOFTWARE/JavaSoft/Java\ Development\ Kit/1.5/JavaHome") ||
258	  reg_get_value ("HKEY_LOCAL_MACHINE/SOFTWARE/JavaSoft/Java\ Development\ Kit/1.4/JavaHome") ||
259	  reg_get_value ("HKEY_LOCAL_MACHINE/SOFTWARE/JavaSoft/Java\ Development\ Kit/1.3/JavaHome");
260    print cygpath($dir, 'w', $output_format);
261}
262
263sub print_nsis_dir()
264{
265    my $dir = reg_get_value ("HKEY_LOCAL_MACHINE/SOFTWARE/NSIS/@");
266    print cygpath ($dir, 'w', $output_format) if defined $dir;
267}
268
269sub copy_dll($$$)
270{
271    my ($src, $fname, $dest) = @_;
272
273    -f "$src/$fname" || die "can't find $src";
274    -d $dest || die "no directory $dest";
275
276	print STDERR "Copying $src/$fname to $dest\n";
277    copy ("$src/$fname", $dest) || die "copy failed: $!";
278    chmod (0755, "$dest/$fname") || die "failed to set dll executable: $!";
279}
280
281sub msvc_find_version($)
282{
283    my $checkpath = shift;
284    my $ver = find_msvc();
285    my $srcdir = (cygpath ($ver->{'product_dir'}, 'w', 'u') . '/' .
286		  $ver->{$checkpath});
287    -d $srcdir && return $ver;
288    $ver = find_msvs();
289    $srcdir = (cygpath ($ver->{'product_dir'}, 'w', 'u') . '/' .
290		  $ver->{$checkpath});
291    -d $srcdir && return $ver;
292    return undef;
293}
294
295sub msvc_copy_dlls($)
296{
297    my $dest = shift;
298    my $ver = msvc_find_version('dll_path');
299    defined $ver || return;
300    my $srcdir = (cygpath ($ver->{'product_dir'}, 'w', 'u') . '/' .
301		  $ver->{'dll_path'});
302
303    copy_dll ($srcdir, "msvcp" . $ver->{'dll_suffix'} . ".dll",
304	      $dest . $ver->{'dll_suffix'});
305    copy_dll ($srcdir, "msvcr" . $ver->{'dll_suffix'} . ".dll",
306	      $dest . $ver->{'dll_suffix'});
307    if ($ver->{'dll_suffix'} >= 90) {
308        copy_dll ($srcdir, "msvcm" . $ver->{'dll_suffix'} . ".dll",
309                  $dest . $ver->{'dll_suffix'});
310        copy_dll ($srcdir, "Microsoft.VC90.CRT.manifest", $dest . $ver->{'dll_suffix'});
311    }
312}
313
314if (!@ARGV) {
315    print_syntax();
316    exit 1;
317}
318
319my @commands = ();
320my $opt;
321while (@ARGV) {
322    $opt = shift @ARGV;
323
324    if ($opt eq '-w' || $opt eq '-u') {
325	$output_format = substr($opt, 1, 1);
326    } else {
327	push @commands, $opt;
328    }
329}
330
331while (@commands) {
332    $opt = shift @commands;
333
334    if (0) {
335    } elsif ($opt eq '--msvc-ver') {
336	print_msvc_ver();
337    } elsif ($opt eq '--msvc-copy-dlls') {
338	my $dest = shift @commands;
339	defined $dest || die "copy-dlls requires a destination directory";
340	msvc_copy_dlls( $dest );
341    } elsif ($opt eq '--msvs-productdir') {
342	print_msvs_productdir();
343    } elsif ($opt eq '--msvc-productdir') {
344	print_msvc_product_dir();
345    } elsif ($opt eq '--dotnetsdk-dir') {
346	print_dotnetsdk_dir();
347    } elsif ($opt eq '--csc-compilerdir') {
348	print_csc_compiler_dir();
349    } elsif ($opt eq '--psdk-home') {
350	print_psdk_home();
351    } elsif ($opt eq '--jdk-home') {
352	print_jdk_dir();
353    } elsif ($opt eq '--nsis-dir') {
354	print_nsis_dir();
355    } elsif ($opt eq '--help' || $opt eq '/?') {
356	print_syntax();
357    } else {
358	print "Unknown option '$opt'\n";
359	print_syntax();
360	exit 1;
361    }
362}
363
364