summaryrefslogtreecommitdiff
path: root/oowintool
blob: 087d478d3bff80c798874093abfc9bc3aa46bae1 (plain)
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
#!/usr/bin/perl -w
# -*- tab-width: 4; cperl-indent-level: 4; indent-tabs-mode: nil -*-

use File::Copy;

my $output_format = 'u';

sub reg_get_value($)
{
    # it is believed that the registry moves keys around
    # depending on OS version, this will de-mangle that
    my $key = shift;
    my $fhandle;
    my $value;

    open ($fhandle, "/proc/registry/$key") || return;
    # reg keys have 0x00 0x5c at the end
    $value = (split /\0/, <$fhandle>)[0];
    close ($fhandle);

    if ( defined $value ) {
        chomp ($value);
        $value =~ s|\r\n||;
        # print "Value '$value' at '$key'\n";
    }

    return $value;
}

sub reg_find_key($)
{
    # it is believed that the registry moves keys around
    # depending on OS version, this will de-mangle that
    my $key = shift;
    $key =~ s| |\\ |;
    $key = `cd /proc/registry/ ; ls $key 2>/dev/null`;

    return $key;
}

sub print_syntax()
{
    print "oowintool [option] ...\n";
    print " encoding options\n";
    print "   -w   - windows form\n";
    print "   -u   - unix form (default)\n";
    print " commands:\n";
    print "   --msvc-ver              - print version of MSVC eg. 6.0\n";
    print "   --msvc-copy-dlls <dest> - copy msvc[pr]??.dlls into <dest>/msvcp??/\n";
    print "   --msvc-copy-msms <dest> - copy mscrt merge modules to <dest>/msm90/\n";
    print "   --msvc-copy-msms-64 <ds>- copy the x64 mscrt merge modules to <ds>/msm90/\n";
    print "   --msvc-productdir       - print productdir\n";
    print "   --msvs-productdir       - print productdir\n";
    print "   --dotnetsdk-dir         - print .NET SDK path\n";
    print "   --csc-compilerdir       - print .NET SDK compiler path\n";
    print "   --windows-sdk-home      - print Windows SDK install dir\n";
    print "   --jdk-home              - print the jdk install dir\n";
    print "   --help                  - print this message\n";
}

sub cygpath($$$)
{
    my ($path, $input_format, $format) = @_;

    return $path if ( ! defined $path );
    # Strip trailing path separators
    if ($input_format eq 'u') {
        $path =~ s|/*\s*$||;
    } else {
        $path =~ s|\\*\s*$||;
    }

    # 'Unterminated quoted string errors' from 'ash' when
    # forking cygpath  so - reimplement cygpath in perl [ gack ]
    if ($format eq 'u' && $input_format eq 'w') {
        $path =~ s|\\|/|g;
        $path =~ s|([a-zA-Z]):/|/cygdrive/$1/|g;
    }
    elsif ($format eq 'w' && $input_format eq 'u') {
        $path =~ s|/cygdrive/([a-zA-Z])/|/$1/|g;
        $path =~ s|/|\\|g;
    }

    return $path;
}

sub print_path($$)
{
    my ($path, $unix) = @_;

    $path = cygpath ($path, $unix, $output_format);

    print $path;
}

sub print_windows_sdk_home()
{
    my ($value, $key);

    $value = reg_get_value ('HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/Microsoft SDKs/Windows/CurrentInstallFolder');

    if (!defined $value) {
        $value = reg_get_value ('HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/MicrosoftSDK/Directories/Install Dir');
    }

    if (!defined $value) {
        $key = reg_find_key ('HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/MicrosoftSDK/InstalledSDKs/*/Install Dir');
        $value = reg_get_value ($key);
    }

    defined $value || die "Windows SDK not found";

    print cygpath ($value, 'w', $output_format);
}

my %msvs_2008 = (
    'ver' => '9.0',
    'key' => 'Microsoft/VisualStudio/9.0/Setup/VS/ProductDir',
    'dll_path' => 'VC/redist/x86/Microsoft.VC90.CRT',
    'dll_suffix' => '90'
);
my %msvc_2008 = (
    'ver' => '9.0',
    'key' => 'Microsoft/VisualStudio/9.0/Setup/VC/ProductDir',
    'dll_path' => 'redist/x86/Microsoft.VC90.CRT',
    'dll_suffix' => '90'
);
my %msvs_express_2008 = (
    'ver' => '9.0',
    'key' => 'Microsoft/VCExpress/9.0/Setup/VS/ProductDir',
    'dll_path' => 'VC/redist/x86/Microsoft.VC90.CRT',
    'dll_suffix' => '90'
);
my %msvc_express_2008 = (
    'ver' => '9.0',
    'key' => 'Microsoft/VCExpress/9.0/Setup/VC/ProductDir',
    'dll_path' => 'redist/x86/Microsoft.VC90.CRT',
    'dll_suffix' => '90'
);
my %msvs_2010 = (
    'ver' => '10.0',
    'key' => 'Microsoft/VisualStudio/10.0/Setup/VS/ProductDir',
    'dll_path' => 'VC/redist/x86/Microsoft.VC100.CRT',
    'dll_suffix' => '100'
);
my %msvc_2010 = (
    'ver' => '10.0',
    'key' => 'Microsoft/VisualStudio/10.0/Setup/VC/ProductDir',
    'dll_path' => 'redist/x86/Microsoft.VC100.CRT',
    'dll_suffix' => '100'
);

sub find_msvs()
{
    my @ms_versions = ( \%msvs_2008, \%msvs_express_2008, \%msvs_2010 );

    for $ver (@ms_versions) {
        my $install = reg_get_value ("HKEY_LOCAL_MACHINE/SOFTWARE/" . $ver->{'key'});
        if (defined $install && $install ne '') {
            $ver->{'product_dir'} = $install;
            return $ver;
        }
    }
    die "Can't find MS Visual Studio / VC++";
}

sub find_msvc()
{
    my @ms_versions = ( \%msvc_2008, \%msvc_express_2008, \%msvc_2010 );

    for $ver (@ms_versions) {
        my $install = reg_get_value ("HKEY_LOCAL_MACHINE/SOFTWARE/" . $ver->{'key'});
        if (defined $install && $install ne '') {
            $ver->{'product_dir'} = $install;
            return $ver;
        }
    }
    die "Can't find MS Visual Studio / VC++";
}

sub print_msvc_ver()
{
    my $ver = find_msvc();
    print $ver->{'ver'};
}

sub print_msvc_product_dir()
{
    my $ver = find_msvc();
    print cygpath ($ver->{'product_dir'}, 'w', $output_format);
}

sub print_msvs_productdir()
{
    my $ver = find_msvs();
    print cygpath ($ver->{'product_dir'}, 'w', $output_format);
}

sub print_csc_compiler_dir()
{
    my $dir = cygpath (reg_get_value ("HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/.NETFramework/InstallRoot"), 'w', $output_format);
    my $csc_exe = `/bin/find "$dir" -iname csc.exe | grep "v3\.5\." | head -n 1` ||
                  `/bin/find "$dir" -iname csc.exe | grep "v4\." | head -n 1` ||
                  `/bin/find "$dir" -iname csc.exe | grep "v2\." | head -n 1`;
    print `dirname $csc_exe`;
}

sub print_dotnetsdk_dir()
{
    my $dir =
          reg_get_value ("HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/.NETFramework/sdkInstallRootv1.1") ||
          reg_get_value ("HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/.NETFramework/sdkInstallRootv2.0");
    if ($dir) {
        print cygpath ($dir, 'w', $output_format);
    }
}

sub print_jdk_dir()
{
    my $dir =
          reg_get_value ("HKEY_LOCAL_MACHINE/SOFTWARE/JavaSoft/Java\ Development\ Kit/1.7/JavaHome") ||
          reg_get_value ("HKEY_LOCAL_MACHINE/SOFTWARE/JavaSoft/Java\ Development\ Kit/1.6/JavaHome") ||
      reg_get_value ("HKEY_LOCAL_MACHINE/SOFTWARE/JavaSoft/Java\ Development\ Kit/1.5/JavaHome") ||
      reg_get_value ("HKEY_LOCAL_MACHINE/SOFTWARE/JavaSoft/Java\ Development\ Kit/1.4/JavaHome") ||
      reg_get_value ("HKEY_LOCAL_MACHINE/SOFTWARE/JavaSoft/Java\ Development\ Kit/1.3/JavaHome");
    print cygpath($dir, 'w', $output_format);
}

sub copy_dll($$$)
{
    my ($src, $fname, $dest) = @_;

    -f "$src/$fname" || die "can't find $src";
    -d $dest || die "no directory $dest";

    print STDERR "Copying $src/$fname to $dest\n";
    copy ("$src/$fname", $dest) || die "copy failed: $!";
    chmod (0755, "$dest/$fname") || die "failed to set dll executable: $!";
}

sub msvc_find_version($)
{
    my $checkpath = shift;
    my $ver = find_msvc();
    my $srcdir = (cygpath ($ver->{'product_dir'}, 'w', 'u') . '/' .
          $ver->{$checkpath});
    -d $srcdir && return $ver;
    $ver = find_msvs();
    $srcdir = (cygpath ($ver->{'product_dir'}, 'w', 'u') . '/' .
          $ver->{$checkpath});
    -d $srcdir && return $ver;
    return undef;
}

sub msvc_copy_dlls($)
{
    my $dest = shift;
    my $ver = msvc_find_version('dll_path');
    defined $ver || return;
    my $srcdir = (cygpath ($ver->{'product_dir'}, 'w', 'u') . '/' .
          $ver->{'dll_path'});

    copy_dll ($srcdir, "msvcp" . $ver->{'dll_suffix'} . ".dll",
          $dest . $ver->{'dll_suffix'});
    copy_dll ($srcdir, "msvcr" . $ver->{'dll_suffix'} . ".dll",
          $dest . $ver->{'dll_suffix'});
    if ($ver->{'dll_suffix'} == 90) {
        copy_dll ($srcdir, "msvcm" . $ver->{'dll_suffix'} . ".dll",
                  $dest . $ver->{'dll_suffix'});
        copy_dll ($srcdir, "Microsoft.VC90.CRT.manifest", $dest . $ver->{'dll_suffix'});
    }
}

sub msvc_copy_msms($$)
{
    # $postfix is empty for x86, and '_x64' for x64
    my ($dest, $postfix) = @_;

    my $ver = find_msvc();
    (defined $ver && ($ver->{'ver'} eq '9.0')) || return;

    my $msm_path = (cygpath reg_get_value ("HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/VisualStudio/9.0/Setup/VS/MSMDir"), 'w', $output_format);
    defined $msm_path || die "MSMDir not found";
    foreach $fname ("Microsoft_VC90_CRT_x86$postfix.msm", "policy_9_0_Microsoft_VC90_CRT_x86$postfix.msm") {
        print STDERR "Copying $msm_path/$fname to $dest\n";
        copy ("$msm_path/$fname", $dest) || die "copy failed: $!";
    }
    foreach $fname ("Microsoft_VC100_CRT_x86$postfix.msm") {
        print STDERR "Copying $msm_path/$fname to $dest\n";
        copy ("$msm_path/$fname", $dest) || print "copy failed: $!";
    }
}

if (!@ARGV) {
    print_syntax();
    exit 1;
}

my @commands = ();
my $opt;
while (@ARGV) {
    $opt = shift @ARGV;

    if ($opt eq '-w' || $opt eq '-u') {
        $output_format = substr($opt, 1, 1);
    } else {
        push @commands, $opt;
    }
}

while (@commands) {
    $opt = shift @commands;

    if (0) {
    } elsif ($opt eq '--msvc-ver') {
        print_msvc_ver();
    } elsif ($opt eq '--msvc-copy-dlls') {
        my $dest = shift @commands;
        defined $dest || die "copy-dlls requires a destination directory";
        msvc_copy_dlls( $dest );
    } elsif ($opt eq '--msvc-copy-msms') {
        my $dest = shift @commands;
        defined $dest || die "copy-msms requires a destination directory";
        msvc_copy_msms( $dest, '' );
    } elsif ($opt eq '--msvc-copy-msms-64') {
        my $dest = shift @commands;
        defined $dest || die "copy-msms-64 requires a destination directory";
        msvc_copy_msms( $dest, '_x64' );
    } elsif ($opt eq '--msvs-productdir') {
        print_msvs_productdir();
    } elsif ($opt eq '--msvc-productdir') {
        print_msvc_product_dir();
    } elsif ($opt eq '--dotnetsdk-dir') {
        print_dotnetsdk_dir();
    } elsif ($opt eq '--csc-compilerdir') {
        print_csc_compiler_dir();
    } elsif ($opt eq '--windows-sdk-home') {
        print_windows_sdk_home();
    } elsif ($opt eq '--jdk-home') {
        print_jdk_dir();
    } elsif ($opt eq '--help' || $opt eq '/?') {
        print_syntax();
    } else {
        print "Unknown option '$opt'\n";
        print_syntax();
        exit 1;
    }
}

# vim:set shiftwidth=4 softtabstop=4 expandtab: