| File: | config/auto/icu.pm |
| Coverage: | 97.2% |
| line | stmt | bran | cond | sub | code |
|---|---|---|---|---|---|
| 1 | # Copyright (C) 2001-2006, Parrot Foundation. | ||||
| 2 | # $Id: icu.pm 36863 2009-02-18 19:31:53Z fperrad $ | ||||
| 3 | |||||
| 4 - 17 | =head1 NAME config/auto/icu.pm - Detect International Components for Unicode (ICU) =head1 DESCRIPTION Determines whether ICU is available. If so, configures ICU and adds appropriate targets to the Makefile. From the ICU home page (L<http://www.icu-project.org/>): "ICU is a mature, widely used set of C/C++ and Java libraries providing Unicode and Globalization support for software applications." =cut | ||||
| 18 | |||||
| 19 | package auto::icu; | ||||
| 20 | |||||
| 21 | 39 39 39 | use strict; | |||
| 22 | 39 39 39 | use warnings; | |||
| 23 | |||||
| 24 | 39 39 39 | use base qw(Parrot::Configure::Step); | |||
| 25 | |||||
| 26 | 39 39 39 | use Cwd qw(cwd); | |||
| 27 | 39 39 39 | use File::Basename; | |||
| 28 | 39 39 39 | use lib qw( lib ); | |||
| 29 | 39 39 39 | use Parrot::Configure::Utils qw(capture_output); | |||
| 30 | |||||
| 31 | |||||
| 32 | sub _init { | ||||
| 33 | 44 | my $self = shift; | |||
| 34 | 44 | my %data; | |||
| 35 | 44 | $data{description} = q{Is ICU installed}; | |||
| 36 | 44 | $data{result} = q{}; | |||
| 37 | # The following key-value pairs are defined here rather than being buried | ||||
| 38 | # deep inside subroutines below. Also, so that they can be overridden | ||||
| 39 | # during testing. | ||||
| 40 | 44 | $data{icuconfig_default} = q{icu-config}; | |||
| 41 | 44 | $data{icu_headers} = [ qw(ucnv.h utypes.h uchar.h) ]; | |||
| 42 | 44 | $data{icu_shared_pattern} = qr/-licui18n\w*/; | |||
| 43 | 44 | return \%data; | |||
| 44 | } | ||||
| 45 | |||||
| 46 | sub runstep { | ||||
| 47 | 44 | my ( $self, $conf ) = @_; | |||
| 48 | |||||
| 49 | 44 | my ( $verbose, $icushared_opt, $icuheaders_opt, | |||
| 50 | $icuconfig_opt, $without_opt ) = $conf->options->get( qw| | ||||
| 51 | verbose | ||||
| 52 | icushared | ||||
| 53 | icuheaders | ||||
| 54 | icu-config | ||||
| 55 | without-icu | ||||
| 56 | | ); | ||||
| 57 | |||||
| 58 | # If we haven't provided the path to a specific ICU configuration program | ||||
| 59 | # on the command line, then we probe to see if the program 'icu-config' is | ||||
| 60 | # available. | ||||
| 61 | |||||
| 62 | # From the icu-config(1) man page | ||||
| 63 | # (L<http://linux.die.net/man/1/icu-config>): | ||||
| 64 | |||||
| 65 | # "icu-config simplifies the task of building and linking | ||||
| 66 | # against ICU as compared to manually configuring user | ||||
| 67 | # makefiles or equivalent. Because icu-config is an executable | ||||
| 68 | # script, it also solves the problem of locating the ICU | ||||
| 69 | # libraries and headers, by allowing the system PATH to locate | ||||
| 70 | # it." | ||||
| 71 | |||||
| 72 | # $icuconfig is a string holding the name of an executable program. | ||||
| 73 | # So if it's not provided on the command line -- or if it's explicitly | ||||
| 74 | # ruled out by being provided with the value 'none' -- an empty string | ||||
| 75 | # is its most appropriate value. | ||||
| 76 | |||||
| 77 | 44 | my $icuconfig = $self->_handle_icuconfig_opt($icuconfig_opt); | |||
| 78 | |||||
| 79 | # Oooh, how I wish we had Perl 5.10's defined-or operator available! | ||||
| 80 | 44 | my $icushared = (defined $icushared_opt) | |||
| 81 | ? $icushared_opt | ||||
| 82 | : undef; | ||||
| 83 | 44 | my $icuheaders = (defined $icuheaders_opt) | |||
| 84 | ? $icuheaders_opt | ||||
| 85 | : undef; | ||||
| 86 | |||||
| 87 | # $without_opt holds user's command-line value for --without-icu=? | ||||
| 88 | # If it's a true value, there's no point in going further. We set the | ||||
| 89 | # values needed in the Parrot::Configure object, set the step result and | ||||
| 90 | # return. If, however, it's a false value, then we're going to try to | ||||
| 91 | # configure with ICU and we proceed to probe for ICU. | ||||
| 92 | |||||
| 93 | # 1st possible return point | ||||
| 94 | 44 | if ( $without_opt ) { | |||
| 95 | 2 | $self->_set_no_configure_with_icu($conf, q{not requested}); | |||
| 96 | 2 | return 1; | |||
| 97 | } | ||||
| 98 | |||||
| 99 | 42 | my $autodetect = ( ! defined($icushared) ) | |||
| 100 | && | ||||
| 101 | ( ! defined($icuheaders) ); | ||||
| 102 | |||||
| 103 | 42 | my $without = 0; | |||
| 104 | 42 | ($icuconfig, $autodetect, $without) = | |||
| 105 | $self->_handle_autodetect( { | ||||
| 106 | icuconfig => $icuconfig, | ||||
| 107 | autodetect => $autodetect, | ||||
| 108 | without => $without, | ||||
| 109 | verbose => $verbose, | ||||
| 110 | } ); | ||||
| 111 | # Inside _handle_autodetect(), $without can be set to 1 by | ||||
| 112 | # _handle_search_for_icu_config(). In that case, we're abandoning our | ||||
| 113 | # attempt to configure with ICU and so may return here. | ||||
| 114 | # 2nd possible return point | ||||
| 115 | 42 | if ( $without ) { | |||
| 116 | 2 | $self->_set_no_configure_with_icu($conf, q{no icu-config}); | |||
| 117 | 2 | print "Could not locate an icu-config program\n" | |||
| 118 | if $verbose; | ||||
| 119 | 2 | return 1; | |||
| 120 | } | ||||
| 121 | |||||
| 122 | 40 | ($without, $icushared, $icuheaders) = | |||
| 123 | $self->_try_icuconfig( | ||||
| 124 | $conf, | ||||
| 125 | { | ||||
| 126 | without => $without, | ||||
| 127 | autodetect => $autodetect, | ||||
| 128 | icuconfig => $icuconfig, | ||||
| 129 | verbose => $verbose, | ||||
| 130 | icushared => $icushared, | ||||
| 131 | icuheaders => $icuheaders, | ||||
| 132 | }, | ||||
| 133 | ); | ||||
| 134 | # 3rd possible return point | ||||
| 135 | 40 | if ( $without ) { | |||
| 136 | 0 | $self->_set_no_configure_with_icu($conf, q{not found}); | |||
| 137 | 0 | return 1; | |||
| 138 | } | ||||
| 139 | |||||
| 140 | 40 | _verbose_report($verbose, $icuconfig, $icushared, $icuheaders); | |||
| 141 | |||||
| 142 | 40 | $icuheaders = $self->_handle_icuconfig_errors( { | |||
| 143 | icushared => $icushared, | ||||
| 144 | icuheaders => $icuheaders, | ||||
| 145 | } ); | ||||
| 146 | # 4th possible return point. This one is a step configuration failure | ||||
| 147 | # because we would have really expected it to succeed. | ||||
| 148 | 40 | return unless defined $icuheaders; | |||
| 149 | |||||
| 150 | 38 | my $icudir = dirname($icuheaders); | |||
| 151 | 38 | $conf->data->set( | |||
| 152 | has_icu => 1, | ||||
| 153 | icu_shared => $icushared, | ||||
| 154 | icu_dir => $icudir, | ||||
| 155 | ); | ||||
| 156 | |||||
| 157 | # Add -I $Icuheaders if necessary. | ||||
| 158 | 38 | my $header = "unicode/ucnv.h"; | |||
| 159 | 38 | $conf->data->set( TEMP_testheaders => "#include <$header>\n" ); | |||
| 160 | 38 | $conf->data->set( TEMP_testheader => "$header" ); | |||
| 161 | 38 | $conf->cc_gen('config/auto/headers/test_c.in'); | |||
| 162 | |||||
| 163 | # Clean up. | ||||
| 164 | 38 | $conf->data->set( TEMP_testheaders => undef ); | |||
| 165 | 38 | $conf->data->set( TEMP_testheader => undef ); | |||
| 166 | 38 38 | eval { $conf->cc_build(); }; | |||
| 167 | 38 | my $ccflags_status = ( ! $@ && $conf->cc_run() =~ /^$header OK/ ); | |||
| 168 | 38 | _handle_ccflags_status( | |||
| 169 | $conf, | ||||
| 170 | { | ||||
| 171 | ccflags_status => $ccflags_status, | ||||
| 172 | verbose => $verbose, | ||||
| 173 | icuheaders => $icuheaders, | ||||
| 174 | }, | ||||
| 175 | ); | ||||
| 176 | 38 | $conf->cc_clean(); | |||
| 177 | 38 | $self->set_result("yes"); | |||
| 178 | # 5th possible return point; this is the only really successful return. | ||||
| 179 | 38 | return 1; | |||
| 180 | } | ||||
| 181 | |||||
| 182 | ########## INTERNAL SUBROUTINES ########## | ||||
| 183 | |||||
| 184 | sub _set_no_configure_with_icu { | ||||
| 185 | 5 | my ($self, $conf, $result) = @_; | |||
| 186 | 5 | $conf->data->set( | |||
| 187 | has_icu => 0, | ||||
| 188 | icu_shared => '', # used for generating src/dynpmc/Makefile | ||||
| 189 | icu_dir => '', | ||||
| 190 | ); | ||||
| 191 | 5 | $self->set_result($result); | |||
| 192 | } | ||||
| 193 | |||||
| 194 | sub _handle_icuconfig_opt { | ||||
| 195 | 48 | my ($self, $icuconfig_opt) = @_; | |||
| 196 | 48 | my $icuconfig; | |||
| 197 | 48 | if ( ( ! $icuconfig_opt ) or ( $icuconfig_opt eq q{none} ) ) { | |||
| 198 | 45 | $icuconfig = q{}; | |||
| 199 | } | ||||
| 200 | elsif ( $icuconfig_opt eq '1' ) { | ||||
| 201 | 2 | $icuconfig = $self->{icuconfig_default}; | |||
| 202 | } | ||||
| 203 | else { | ||||
| 204 | 1 | $icuconfig = $icuconfig_opt; | |||
| 205 | } | ||||
| 206 | 48 | return $icuconfig; | |||
| 207 | } | ||||
| 208 | |||||
| 209 | sub _die_message { | ||||
| 210 | 4 | my $die = <<"HELP"; | |||
| 211 | Something is wrong with your ICU installation! | ||||
| 212 | |||||
| 213 | If you do not have a full ICU installation: | ||||
| 214 | |||||
| 215 | --without-icu Build parrot without ICU support | ||||
| 216 | --icu-config=(file) Location of icu-config | ||||
| 217 | --icuheaders=(path) Location of ICU headers without /unicode | ||||
| 218 | --icushared=(flags) Full linker command to create shared libraries | ||||
| 219 | HELP | ||||
| 220 | #/ | ||||
| 221 | 4 | return $die; | |||
| 222 | } | ||||
| 223 | |||||
| 224 | sub _handle_search_for_icu_config { | ||||
| 225 | 44 | my $self = shift; | |||
| 226 | 44 | my $arg = shift; | |||
| 227 | 44 | if ( $arg->{ret} ) { | |||
| 228 | 4 | undef $arg->{icuconfig}; | |||
| 229 | 4 | $arg->{autodetect} = 0; | |||
| 230 | 4 | $arg->{without} = 1; | |||
| 231 | } | ||||
| 232 | else { | ||||
| 233 | 40 | $arg->{icuconfig} = $self->{icuconfig_default}; | |||
| 234 | 40 | if ($arg->{verbose}) { | |||
| 235 | 1 | print "icu-config found... good!\n"; | |||
| 236 | } | ||||
| 237 | } | ||||
| 238 | 44 | return ( $arg->{icuconfig}, $arg->{autodetect}, $arg->{without} ); | |||
| 239 | } | ||||
| 240 | |||||
| 241 | sub _handle_autodetect { | ||||
| 242 | 44 | my $self = shift; | |||
| 243 | 44 | my $arg = shift; | |||
| 244 | 44 | if ( $arg->{autodetect} ) { | |||
| 245 | 41 | if ( ! $arg->{icuconfig} ) { | |||
| 246 | |||||
| 247 | 40 | my ( undef, undef, $ret ) = | |||
| 248 | capture_output( $self->{icuconfig_default}, "--exists" ); | ||||
| 249 | |||||
| 250 | 40 | print "Discovered $self->{icuconfig_default} --exists returns $ret\n" | |||
| 251 | if $arg->{verbose}; | ||||
| 252 | |||||
| 253 | 40 | ($arg->{icuconfig}, $arg->{autodetect}, $arg->{without}) = | |||
| 254 | $self->_handle_search_for_icu_config( { | ||||
| 255 | icuconfig => $arg->{icuconfig}, | ||||
| 256 | autodetect => $arg->{autodetect}, | ||||
| 257 | without => $arg->{without}, | ||||
| 258 | verbose => $arg->{verbose}, | ||||
| 259 | ret => $ret, | ||||
| 260 | } ); | ||||
| 261 | } | ||||
| 262 | } # end $autodetect true | ||||
| 263 | else { | ||||
| 264 | 3 | if ($arg->{verbose}) { | |||
| 265 | 1 | print "Specified an ICU config parameter,\n"; | |||
| 266 | 1 | print "ICU autodetection disabled.\n"; | |||
| 267 | } | ||||
| 268 | } # end $autodetect false | ||||
| 269 | 44 | return ( $arg->{icuconfig}, $arg->{autodetect}, $arg->{without} ); | |||
| 270 | } | ||||
| 271 | |||||
| 272 | sub _try_icuconfig { | ||||
| 273 | 44 | my $self = shift; | |||
| 274 | 44 | my $conf = shift; | |||
| 275 | 44 | my $arg = shift; | |||
| 276 | 44 | my $icushared = ( defined $arg->{icushared} ) | |||
| 277 | ? $arg->{icushared} | ||||
| 278 | : undef; | ||||
| 279 | 44 | my $icuheaders = ( defined $arg->{icuheaders} ) | |||
| 280 | ? $arg->{icuheaders} | ||||
| 281 | : undef; | ||||
| 282 | 44 | if ( | |||
| 283 | ( ! $arg->{without} ) && | ||||
| 284 | $arg->{autodetect} && | ||||
| 285 | $arg->{icuconfig} | ||||
| 286 | ) { | ||||
| 287 | # ldflags | ||||
| 288 | 39 | print "Trying $arg->{icuconfig} with '--ldflags'\n" | |||
| 289 | if $arg->{verbose}; | ||||
| 290 | 39 | $icushared = capture_output("$arg->{icuconfig} --ldflags"); | |||
| 291 | 39 | print "icushared: captured $icushared\n" | |||
| 292 | if $arg->{verbose}; | ||||
| 293 | 39 | ($icushared, $arg->{without}) = | |||
| 294 | $self->_handle_icushared($icushared, $arg->{without}); | ||||
| 295 | 39 | print "For icushared, found $icushared and $arg->{without}\n" | |||
| 296 | if $arg->{verbose}; | ||||
| 297 | |||||
| 298 | # location of header files | ||||
| 299 | 39 | print "Trying $arg->{icuconfig} with '--prefix'\n" | |||
| 300 | if $arg->{verbose}; | ||||
| 301 | 39 | $icuheaders = capture_output("$arg->{icuconfig} --prefix"); | |||
| 302 | 39 | print "icuheaders: captured $icuheaders\n" | |||
| 303 | if $arg->{verbose}; | ||||
| 304 | 39 | ($icuheaders, $arg->{without}) = | |||
| 305 | $self->_handle_icuheaders($conf, $icuheaders, $arg->{without}); | ||||
| 306 | 39 | print "For icuheaders, found $icuheaders and $arg->{without}\n" | |||
| 307 | if $arg->{verbose}; | ||||
| 308 | } | ||||
| 309 | |||||
| 310 | 44 | return ($arg->{without}, $icushared, $icuheaders); | |||
| 311 | } | ||||
| 312 | |||||
| 313 | sub _handle_icushared { | ||||
| 314 | 42 | my $self = shift; | |||
| 315 | 42 | my ($icushared, $without) = @_; | |||
| 316 | 42 | if ( defined $icushared ) { | |||
| 317 | 41 | chomp $icushared; | |||
| 318 | 41 | $icushared =~ s/$self->{icu_shared_pattern}//; # "-licui18n32" too | |||
| 319 | 41 | if (length $icushared == 0) { | |||
| 320 | 1 | $without = 1; | |||
| 321 | } | ||||
| 322 | } | ||||
| 323 | 42 | return ($icushared, $without); | |||
| 324 | } | ||||
| 325 | |||||
| 326 | sub _handle_icuheaders { | ||||
| 327 | 43 | my $self = shift; | |||
| 328 | 43 | my ($conf, $icuheaders, $without) = @_; | |||
| 329 | 43 | if ( defined $icuheaders ) { | |||
| 330 | 42 | chomp $icuheaders; | |||
| 331 | 42 | if (! -d $icuheaders) { | |||
| 332 | 1 | $without = 1; | |||
| 333 | } | ||||
| 334 | 42 | my $slash = $conf->data->get('slash'); | |||
| 335 | 42 | $icuheaders .= "${slash}include"; | |||
| 336 | 42 | if (! -d $icuheaders) { | |||
| 337 | 2 | $without = 1; | |||
| 338 | } | ||||
| 339 | } | ||||
| 340 | 43 | return ($icuheaders, $without); | |||
| 341 | } | ||||
| 342 | |||||
| 343 | sub _verbose_report { | ||||
| 344 | 44 | my ($verbose, $icuconfig, $icushared, $icuheaders) = @_; | |||
| 345 | 44 | if ($verbose) { | |||
| 346 | 3 | print "icuconfig: $icuconfig\n" if defined $icuconfig; | |||
| 347 | 3 | print "icushared='$icushared'\n" if defined $icushared; | |||
| 348 | 3 | print "headers='$icuheaders'\n" if defined $icuheaders; | |||
| 349 | } | ||||
| 350 | } | ||||
| 351 | |||||
| 352 | sub _handle_icuconfig_errors { | ||||
| 353 | 41 | my $self = shift; | |||
| 354 | 41 | my $arg = shift; | |||
| 355 | 41 | my $icuconfig_errors = 0; | |||
| 356 | |||||
| 357 | 41 | if ( ! defined $arg->{icushared} ) { | |||
| 358 | 2 | warn "error: icushared not defined\n"; | |||
| 359 | 2 | $icuconfig_errors++; | |||
| 360 | } | ||||
| 361 | |||||
| 362 | 41 | if ( ! ( defined $arg->{icuheaders} and -d $arg->{icuheaders} ) ) { | |||
| 363 | 3 | warn "error: icuheaders not defined or invalid\n"; | |||
| 364 | 3 | $icuconfig_errors++; | |||
| 365 | } | ||||
| 366 | else { | ||||
| 367 | 38 | $arg->{icuheaders} =~ s![\\/]$!!; | |||
| 368 | 38 38 | foreach my $header ( @{ $self->{icu_headers} } ) { | |||
| 369 | 114 | $header = "$arg->{icuheaders}/unicode/$header"; | |||
| 370 | 114 | if ( ! -e $header ) { | |||
| 371 | 0 | $icuconfig_errors++; | |||
| 372 | 0 | warn "error: ICU header '$header' not found\n"; | |||
| 373 | } | ||||
| 374 | } | ||||
| 375 | } | ||||
| 376 | |||||
| 377 | 41 | if ($icuconfig_errors) { | |||
| 378 | 3 | warn _die_message(); | |||
| 379 | 3 | return; | |||
| 380 | } | ||||
| 381 | else { | ||||
| 382 | 38 | return $arg->{icuheaders}; | |||
| 383 | } | ||||
| 384 | } | ||||
| 385 | |||||
| 386 | sub _handle_ccflags_status { | ||||
| 387 | 41 | my $conf = shift; | |||
| 388 | 41 | my $arg = shift; | |||
| 389 | 41 | if ($arg->{ccflags_status}) { | |||
| 390 | # Ok, we don't need anything more. | ||||
| 391 | 39 | if ($arg->{verbose}) { | |||
| 392 | 1 | print "Your compiler found the icu headers... good!\n"; | |||
| 393 | } | ||||
| 394 | } | ||||
| 395 | else { | ||||
| 396 | 2 | if ($arg->{verbose}) { | |||
| 397 | 1 | print "Adding -I $arg->{icuheaders} to ccflags for icu headers.\n"; | |||
| 398 | } | ||||
| 399 | 2 | $conf->data->add( ' ', ccflags => "-I $arg->{icuheaders}" ); | |||
| 400 | } | ||||
| 401 | } | ||||
| 402 | |||||
| 403 | 1; | ||||
| 404 | |||||
| 405 | # Local Variables: | ||||
| 406 | # mode: cperl | ||||
| 407 | # cperl-indent-level: 4 | ||||
| 408 | # fill-column: 100 | ||||
| 409 | # End: | ||||
| 410 | # vim: expandtab shiftwidth=4: | ||||