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