Home | History | Annotate | Download | only in genrb
      1 #!/usr/bin/perl
      2 #  ********************************************************************
      3 #  * COPYRIGHT:
      4 #  * Copyright (c) 2002-2008, International Business Machines Corporation and
      5 #  * others. All Rights Reserved.
      6 #  ********************************************************************
      7 
      8 # Script to generate the icudata.jar and testdata.jar files.  This file is
      9 # part of icu4j.  It is checked into CVS.  It is generated from
     10 # locale data in the icu4c project.  See usage() notes (below)
     11 # for more information.
     12 
     13 # This script requires perl.  For Win32, I recommend www.activestate.com.
     14 
     15 # Ram Viswanadha
     16 # copied heavily from genrbjar.pl
     17 # 
     18 # 6/25/08 - Modified to better handle cygwin paths - Brian Rower
     19 #
     20 use File::Find;
     21 use File::Basename;
     22 use IO::File;
     23 use Cwd;
     24 use File::Copy;
     25 use Getopt::Long;
     26 use File::Path;
     27 use File::Copy;
     28 use Cwd;
     29 use Cwd 'abs_path'; 
     30 
     31 main();
     32 
     33 #------------------------------------------------------------------
     34 sub main(){
     35 
     36     GetOptions(
     37              "--icu-root=s" => \$icuRootDir,
     38              "--jar=s" => \$jarDir,
     39              "--icu4j-root=s" => \$icu4jDir,
     40              "--version=s" => \$version,
     41              "--verbose"   => \$verbose,
     42              "--help"      => \$help
     43              );
     44     $cwd = abs_path(getcwd);
     45     
     46     if($help){
     47         usage();
     48     }
     49     unless (defined $icuRootDir){
     50         $icuRootDir =abs_path($cwd."/../../..");
     51     }
     52     unless (defined $icu4jDir){
     53         $icu4jDir =abs_path($icuRootDir."/../icu4j");
     54     }
     55     unless (defined $jarDir){
     56         if(defined $ENV{'JAVA_HOME'}){
     57             $jarDir=$ENV{'JAVA_HOME'}."/bin";
     58         }else{
     59             print("ERROR: JAVA_HOME enviroment variable undefined and --jar argument not specifed.\n");
     60             usage(); 
     61         }
     62     }
     63     
     64     $platform = getPlatform();
     65     $icuBinDir = $icuRootDir;
     66     
     67     $path=$ENV{'PATH'};
     68     
     69     if(($platform eq "cygwin") || ($platform eq "linux")){
     70         $icuBinDir .= "/source/bin";
     71         $icuLibDir = abs_path($icuBinDir."/../lib");
     72         $path .=":$icuBinDir:$icuLibDir";
     73         
     74         $libpath = $ENV{'LD_LIBRARY_PATH'}.":$icuLibDir";
     75         $ENV{'LD_LIBRARY_PATH'} = $libpath;
     76         
     77         #print ("#####  LD_LIBRARY_PATH = $ENV{'LD_LIBRARY_PATH'}\n");
     78     
     79     }elsif($platform eq "aix"){
     80     
     81         $icuBinDir .= "/source/bin";
     82         $icuLibDir = abs_path($icuBinDir."/../lib");
     83         $path .=":$icuBinDir:$icuLibDir";
     84 
     85         $libpath = $ENV{'LIBPATH'}.":$icuLibDir";
     86         $ENV{'LIBPATH'} = $libpath;
     87         #print ("#####  LIBPATH = $ENV{'LIBPATH'}\n");
     88     }elsif($platform eq "darwin"){ 
     89         $icuBinDir .= "/source/bin";
     90         $icuLibDir = abs_path($icuBinDir."/../lib");
     91         $path .=":$icuBinDir:$icuLibDir";
     92         
     93         $libpath = $ENV{'DYLD_LIBRARY_PATH'}.":$icuLibDir";
     94         $ENV{'DYLD_LIBRARY_PATH'} = $libpath;
     95        
     96     }elsif($platform eq "MSWin32"){
     97         $icuBinDir =$icuRootDir."/bin";
     98         $path .=$icuBinDir;
     99         
    100     }
    101     $ENV{'PATH'} = $path;
    102     #print ("#####  PATH = $ENV{'PATH'}\n");
    103     # TODO add more platforms and test on Linux and Unix
    104     
    105     $icuBuildDir =$icuRootDir."/source/data/out/build";
    106     $icuTestDataSrcDir =$icuRootDir."/source/test/testdata/";
    107     $icuTestDataDir =$icuRootDir."/source/test/testdata/out/build/";    
    108     
    109     # now build ICU
    110     buildICU($platform, $icuRootDir, $icuTestDataDir, $verbose);
    111     
    112     #figure out the version and endianess
    113     unless (defined $version){
    114         ($version, $endian) = getVersion();
    115         #print "#################### $version, $endian ######\n";
    116     }
    117     
    118     $icupkg = $icuBinDir."/icupkg -tb";
    119     $tempDir = $cwd."/temp";
    120     $version =~ s/\.//;
    121     $icu4jImpl = "com/ibm/icu/impl/data/";
    122     $icu4jDataDir = $icu4jImpl."icudt".$version."b";
    123     $icu4jDevDataDir = "com/ibm/icu/dev/data/";
    124     $icu4jTestDataDir = "$icu4jDevDataDir/testdata";
    125     
    126     $icuDataDir =$icuBuildDir."/icudt".$version.checkPlatformEndianess();
    127     
    128     #remove the stale directories
    129     unlink($tempDir);
    130     
    131     convertData($icuDataDir, $icupkg, $tempDir, $icu4jDataDir, $verbose);
    132     #convertData($icuDataDir."/coll/", $icupkg, $tempDir, $icu4jDataDir."/coll");
    133     createJar("\"$jarDir/jar\"", "icudata.jar", $tempDir, $icu4jDataDir, $verbose);
    134     
    135     convertTestData($icuTestDataDir, $icupkg, $tempDir, $icu4jTestDataDir, $verbose);
    136     createJar("\"$jarDir/jar\"", "testdata.jar", $tempDir, $icu4jTestDataDir, $verbose);
    137     copyData($icu4jDir, $icu4jImpl, $icu4jDevDataDir, $tempDir, $verbose);
    138 }
    139 
    140 #-----------------------------------------------------------------------
    141 sub buildICU{
    142     local($platform, $icuRootDir, $icuTestDataDir, $verbose) = @_;
    143     $icuSrcDir = $icuRootDir."/source";
    144     $icuSrcDataDir = $icuSrcDir."/data";
    145     
    146     chdir($icuSrcDir);
    147     # clean the data directories
    148     unlink($icuBuildDir."../"); 
    149     unlink($icuTestDataDir."../"); 
    150     
    151     if(($platform eq "cygwin")||($platform eq "darwin")||($platform eq "linux")){
    152         
    153         # make all in ICU
    154         cmd("make all", $verbose);
    155         chdir($icuSrcDataDir);
    156         cmd("make uni-core-data", $verbose);
    157         if(chdir($icuTestDataSrcDir)){
    158             print("Invoking make in directory $icuTestDataSrcDir\n");
    159             cmd("make JAVA_OUT_DIR=\"$icu4jDir/src/com/ibm/icu/dev/test/util/\" all java-output", $verbose);
    160         }else{
    161 	    die "Could not cd to $icuTestDataSrcDir\n";
    162         }
    163     }elsif($platform eq "aix"){
    164         # make all in ICU
    165         cmd("gmake all", $verbose);
    166         chdir($icuSrcDataDir);
    167         cmd("gmake uni-core-data", $verbose);
    168         chdir($icuTestDataDir."../../");
    169         cmd("gmake JAVA_OUT_DIR=\"$icu4jDir/src/com/ibm/icu/dev/test/util/\" all java-output", $verbose);
    170     }elsif($platform eq "MSWin32"){
    171         #devenv.com $projectFileName \/build $configurationName > \"$cLogFile\" 2>&1
    172         cmd("devenv.com allinone/allinone.sln /useenv /build Debug", $verbose);
    173         # build required data. this is required coz building icu will not build all the data
    174         chdir($icuSrcDataDir);
    175         cmd("NMAKE /f makedata.mak ICUMAKE=\"$icuSrcDataDir\" CFG=debug uni-core-data", $verbose);
    176         print "WARNING: Don't know how to build java-output on $platform. \n";
    177     }else{
    178         print "ERROR: Could not build ICU unknown platform $platform. \n";
    179         exit(-1);
    180     }
    181     
    182     chdir($cwd);    
    183 }
    184 #-----------------------------------------------------------------------
    185 sub getVersion{
    186     my @list;
    187     opendir(DIR,$icuBuildDir);
    188     
    189     @list =  readdir(DIR);
    190     closedir(DIR);
    191     
    192     if(scalar(@list)>3){
    193         print("ERROR: More than 1 directory in build. Can't decide the version");
    194         exit(-1);
    195     }
    196     foreach $item (@list){
    197         next if($item eq "." || $item eq "..");
    198         my ($ver, $end) =$item =~ m/icudt(.*)(l|b|e)$/;
    199         return $ver,$end;
    200     }
    201 }
    202 
    203 #-----------------------------------------------------------------------
    204 sub getPlatform{
    205     $platform = $^O;
    206     return $platform;
    207 }
    208 #-----------------------------------------------------------------------
    209 sub createJar{
    210     local($jar, $jarFile, $tempDir, $dirToJar, $verbose) = @_;
    211     chdir($tempDir);
    212     $command="";
    213     print "INFO: Creating $jarFile\n";
    214     if($platform eq "cygwin") {
    215         #make sure the given path is a cygwin path not a windows path
    216 	$jar = `cygpath -au $jar`;
    217 	chop($jar);
    218 	
    219 	#added by Brian Rower 6/25/08
    220 	#The following code deals with spaces in the path
    221 	if(index($jar, "/ ") > 0)
    222 	{
    223 		$jar =~ s/[\/]\s/\\ /g;
    224 	}
    225 	elsif(index($jar, " ") > 0)
    226 	{
    227 		$jar =~ s/\s/\\ /g;
    228 	}
    229 	$tempDir = `cygpath -aw $tempDir`;
    230         chop($tempDir);
    231         $tempDir =~ s/\\/\\\\/g;
    232     }
    233     if(defined $verbose){
    234         $command = "$jar cvf $jarFile -C $tempDir $dirToJar";
    235     }else{
    236         $command = "$jar cf $jarFile -C $tempDir $dirToJar";
    237     }
    238     cmd($command, $verbose);
    239 }
    240 #-----------------------------------------------------------------------
    241 sub checkPlatformEndianess {
    242     my $is_big_endian = unpack("h*", pack("s", 1)) =~ /01/;
    243     if ($is_big_endian) {
    244         return "b";
    245     }else{
    246         return "l";
    247     }
    248 }
    249 #-----------------------------------------------------------------------
    250 sub copyData{
    251     local($icu4jDir, $icu4jImpl, $icu4jDevDataDir, $tempDir) =@_;
    252     print("INFO: Copying $tempDir/icudata.jar to $icu4jDir/src/$icu4jImpl\n");
    253     mkpath("$icu4jDir/src/$icu4jImpl");
    254     copy("$tempDir/icudata.jar", "$icu4jDir/src/$icu4jImpl"); 
    255     print("INFO: Copying $tempDir/testdata.jar $icu4jDir/src/$icu4jDevDataDir\n");
    256     mkpath("$icu4jDir/src/$icu4jDevDataDir");
    257     copy("$tempDir/testdata.jar","$icu4jDir/src/$icu4jDevDataDir");
    258 }
    259 #-----------------------------------------------------------------------
    260 sub convertData{
    261     local($icuDataDir, $icupkg, $tempDir, $icu4jDataDir)  =@_;
    262     my $dir = $tempDir."/".$icu4jDataDir;
    263     # create the temp directory
    264     mkpath($dir) ;
    265     # cd to the temp directory
    266     chdir($tempDir);
    267     my $endian = checkPlatformEndianess();
    268     my @list;
    269     opendir(DIR,$icuDataDir);
    270     #print $icuDataDir;
    271     @list =  readdir(DIR);
    272     closedir(DIR);
    273     my $op = $icupkg;
    274     #print "####### $endian ############\n";
    275     if($endian eq "l"){
    276         print "INFO: {Command: $op $icuDataDir/*.*}\n";
    277     }else{
    278        print "INFO: {Command: copy($icuDataDir/*.*, $tempDir/$icu4jDataDir/*)}\n";
    279     } 
    280     
    281     $i=0;
    282     # now convert
    283     foreach $item (@list){
    284         next if($item eq "." || $item eq "..");	 
    285        # next if($item =~ /^t_.*$\.res/ ||$item =~ /^translit_.*$\.res/ ||
    286        #         $item=~/$\.crs/ || $item=~ /$\.txt/ ||
    287        #         $item=~/icudata\.res/ || $item=~/$\.exp/ || $item=~/$\.lib/ ||
    288        #         $item=~/$\.obj/ || $item=~/$\.lst/);
    289         next if($item =~ /^t_.*$\.res/ ||$item =~ /^translit_.*$\.res/  ||
    290                $item=~/$\.crs/ || $item=~ /$\.txt/ ||
    291                $item=~/icudata\.res/ || $item=~/$\.exp/ || $item=~/$\.lib/ || $item=~/$\.obj/ ||
    292                $item=~/$\.lst/);
    293         if(-d "$icuDataDir/$item"){
    294             convertData("$icuDataDir/$item/", $icupkg, $tempDir, "$icu4jDataDir/$item/");
    295             next;
    296         }
    297         if($endian eq "l"){
    298            $command = $icupkg." $icuDataDir/$item $tempDir/$icu4jDataDir/$item";
    299            cmd($command, $verbose);
    300         }else{
    301            $rc = copy("$icuDataDir/$item", "$tempDir/$icu4jDataDir/$item");
    302            if($rc==1){
    303              #die "ERROR: Could not copy $icuDataDir/$item to $tempDir/$icu4jDataDir/$item, $!";
    304            }
    305         }
    306 
    307     }
    308     chdir("..");
    309     print "INFO: DONE\n";
    310 }
    311 #-----------------------------------------------------------------------
    312 sub convertTestData{
    313     local($icuDataDir, $icupkg, $tempDir, $icu4jDataDir)  =@_;
    314     my $dir = $tempDir."/".$icu4jDataDir;
    315     # create the temp directory
    316     mkpath($dir);
    317     # cd to the temp directory
    318     chdir($tempDir);
    319     my $op = $icupkg;
    320     print "INFO: {Command: $op $icuDataDir/*.*}\n";
    321     my @list;
    322     opendir(DIR,$icuDataDir) or die "ERROR: Could not open the $icuDataDir directory for reading $!";
    323     #print $icuDataDir;
    324     @list =  readdir(DIR);
    325     closedir(DIR);
    326     my $endian = checkPlatformEndianess();
    327     $i=0;
    328     # now convert
    329     foreach $item (@list){
    330         next if($item eq "." || $item eq "..");
    331         next if( item=~/$\.crs/ || $item=~ /$\.txt/ ||
    332                 $item=~/$\.exp/ || $item=~/$\.lib/ || $item=~/$\.obj/ ||
    333                 $item=~/$\.mak/ || $item=~/test\.icu/ || $item=~/$\.lst/);
    334         $file = $item;
    335         $file =~ s/testdata_//g;
    336         if($endian eq "l"){ 
    337             $command = "$icupkg $icuDataDir/$item $tempDir/$icu4jDataDir/$file";
    338             cmd($command, $verbose);
    339         }else{
    340             #print("Copying $icuDataDir/$item $tempDir/$icu4jDataDir/$file\n");
    341             copy("$icuDataDir/$item", "$tempDir/$icu4jDataDir/$file");
    342         }
    343         
    344 
    345     }
    346     chdir("..");
    347     print "INFO: DONE\n";
    348 }
    349 #------------------------------------------------------------------------------------------------
    350 sub cmd {
    351     my $cmd = shift;
    352     my $verbose = shift;
    353     my $prompt = shift;
    354     
    355     $prompt = "Command: $cmd.." unless ($prompt);
    356     if(defined $verbose){
    357         print $prompt."\n";
    358     }
    359     system($cmd);
    360     my $exit_value  = $? >> 8;
    361     #my $signal_num  = $? & 127;
    362     #my $dumped_core = $? & 128;
    363     if ($exit_value == 0) {
    364         if(defined $verbose){
    365             print "ok\n";
    366         }
    367     } else {
    368         ++$errCount;
    369         print "ERROR: Execution of $prompt returned ($exit_value)\n";
    370         exit(1);
    371     }
    372 }
    373 #-----------------------------------------------------------------------
    374 sub usage {
    375     print << "END";
    376 Usage:
    377 gendtjar.pl
    378 Options:
    379         --icu-root=<directory where icu4c lives>
    380         --jar=<directory where jar.exe lives>
    381         --icu4j-root=<directory>
    382         --version=<ICU4C version>
    383         --verbose
    384         --help
    385 e.g:
    386 gendtjar.pl --icu-root=\\work\\icu --jar=\\jdk1.4.1\\bin --icu4j-root=\\work\\icu4j --version=3.0
    387 END
    388   exit(0);
    389 }
    390 
    391 
    392