Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame^] | 1 | # |
| 2 | # This is not a runnable script, it is a Perl module, a collection of variables, subroutines, etc. |
| 3 | # to be used in Perl scripts. |
| 4 | # |
| 5 | # To get help about exported variables and subroutines, execute the following command: |
| 6 | # |
| 7 | # perldoc Platform.pm |
| 8 | # |
| 9 | # or see POD (Plain Old Documentation) imbedded to the source... |
| 10 | # |
| 11 | # |
| 12 | # |
| 13 | #//===----------------------------------------------------------------------===// |
| 14 | #// |
| 15 | #// The LLVM Compiler Infrastructure |
| 16 | #// |
| 17 | #// This file is dual licensed under the MIT and the University of Illinois Open |
| 18 | #// Source Licenses. See LICENSE.txt for details. |
| 19 | #// |
| 20 | #//===----------------------------------------------------------------------===// |
| 21 | # |
| 22 | |
| 23 | package Platform; |
| 24 | |
| 25 | use strict; |
| 26 | use warnings; |
| 27 | |
| 28 | use base "Exporter"; |
| 29 | |
| 30 | use Uname; |
| 31 | |
| 32 | my @vars; |
| 33 | |
| 34 | BEGIN { |
| 35 | @vars = qw{ $host_arch $host_os $host_platform $target_arch $target_os $target_platform }; |
| 36 | } |
| 37 | |
| 38 | our $VERSION = "0.014"; |
| 39 | our @EXPORT = qw{}; |
| 40 | our @EXPORT_OK = ( qw{ canon_arch canon_os legal_arch arch_opt }, @vars ); |
| 41 | our %EXPORT_TAGS = ( all => [ @EXPORT_OK ], vars => \@vars ); |
| 42 | |
| 43 | # Canonize architecture name. |
| 44 | sub canon_arch($) { |
| 45 | my ( $arch ) = @_; |
| 46 | if ( defined( $arch ) ) { |
| 47 | if ( $arch =~ m{\A\s*(?:32|IA-?32|IA-?32 architecture|i[3456]86|x86)\s*\z}i ) { |
| 48 | $arch = "32"; |
| 49 | } elsif ( $arch =~ m{\A\s*(?:48|(?:ia)?32e|Intel\s*64|Intel\(R\)\s*64|x86[_-]64|x64|AMD64)\s*\z}i ) { |
| 50 | $arch = "32e"; |
| 51 | } else { |
| 52 | $arch = undef; |
| 53 | }; # if |
| 54 | }; # if |
| 55 | return $arch; |
| 56 | }; # sub canon_arch |
| 57 | |
| 58 | { # Return legal approved architecture name. |
| 59 | my %legal = ( |
| 60 | "32" => "IA-32 architecture", |
| 61 | "32e" => "Intel(R) 64", |
| 62 | ); |
| 63 | |
| 64 | sub legal_arch($) { |
| 65 | my ( $arch ) = @_; |
| 66 | $arch = canon_arch( $arch ); |
| 67 | if ( defined( $arch ) ) { |
| 68 | $arch = $legal{ $arch }; |
| 69 | }; # if |
| 70 | return $arch; |
| 71 | }; # sub legal_arch |
| 72 | } |
| 73 | |
| 74 | { # Return architecture name suitable for Intel compiler setup scripts. |
| 75 | my %option = ( |
| 76 | "32" => "ia32", |
| 77 | "32e" => "intel64", |
| 78 | "64" => "ia64", |
| 79 | ); |
| 80 | |
| 81 | sub arch_opt($) { |
| 82 | my ( $arch ) = @_; |
| 83 | $arch = canon_arch( $arch ); |
| 84 | if ( defined( $arch ) ) { |
| 85 | $arch = $option{ $arch }; |
| 86 | }; # if |
| 87 | return $arch; |
| 88 | }; # sub arch_opt |
| 89 | } |
| 90 | |
| 91 | # Canonize OS name. |
| 92 | sub canon_os($) { |
| 93 | my ( $os ) = @_; |
| 94 | if ( defined( $os ) ) { |
| 95 | if ( $os =~ m{\A\s*(?:Linux|lin|l)\s*\z}i ) { |
| 96 | $os = "lin"; |
| 97 | } elsif ( $os =~ m{\A\s*(?:lrb)\s*\z}i ) { |
| 98 | $os = "lrb"; |
| 99 | } elsif ( $os =~ m{\A\s*(?:Mac(?:\s*OS(?:\s*X)?)?|mac|m|Darwin)\s*\z}i ) { |
| 100 | $os = "mac"; |
| 101 | } elsif ( $os =~ m{\A\s*(?:Win(?:dows)?(?:(?:_|\s*)?(?:NT|XP|95|98|2003))?|w)\s*\z}i ) { |
| 102 | $os = "win"; |
| 103 | } else { |
| 104 | $os = undef; |
| 105 | }; # if |
| 106 | }; # if |
| 107 | return $os; |
| 108 | }; # sub canon_os |
| 109 | |
| 110 | my ( $_host_os, $_host_arch, $_target_os, $_target_arch ); |
| 111 | |
| 112 | sub set_target_arch($) { |
| 113 | my ( $arch ) = canon_arch( $_[ 0 ] ); |
| 114 | if ( defined( $arch ) ) { |
| 115 | $_target_arch = $arch; |
| 116 | $ENV{ LIBOMP_ARCH } = $arch; |
| 117 | }; # if |
| 118 | return $arch; |
| 119 | }; # sub set_target_arch |
| 120 | |
| 121 | sub set_target_os($) { |
| 122 | my ( $os ) = canon_os( $_[ 0 ] ); |
| 123 | if ( defined( $os ) ) { |
| 124 | $_target_os = $os; |
| 125 | $ENV{ LIBOMP_OS } = $os; |
| 126 | }; # if |
| 127 | return $os; |
| 128 | }; # sub set_target_os |
| 129 | |
| 130 | sub target_options() { |
| 131 | my @options = ( |
| 132 | "target-os|os=s" => |
| 133 | sub { |
| 134 | set_target_os( $_[ 1 ] ) or |
| 135 | die "Bad value of --target-os option: \"$_[ 1 ]\"\n"; |
| 136 | }, |
| 137 | "target-architecture|targert-arch|architecture|arch=s" => |
| 138 | sub { |
| 139 | set_target_arch( $_[ 1 ] ) or |
| 140 | die "Bad value of --target-architecture option: \"$_[ 1 ]\"\n"; |
| 141 | }, |
| 142 | ); |
| 143 | return @options; |
| 144 | }; # sub target_options |
| 145 | |
| 146 | # Detect host arch. |
| 147 | { |
| 148 | my $hardware_platform = Uname::hardware_platform(); |
| 149 | if ( 0 ) { |
| 150 | } elsif ( $hardware_platform eq "i386" ) { |
| 151 | $_host_arch = "32"; |
| 152 | } elsif ( $hardware_platform eq "ia64" ) { |
| 153 | $_host_arch = "64"; |
| 154 | } elsif ( $hardware_platform eq "x86_64" ) { |
| 155 | $_host_arch = "32e"; |
| 156 | } else { |
| 157 | die "Unsupported host hardware platform: \"$hardware_platform\"; stopped"; |
| 158 | }; # if |
| 159 | } |
| 160 | |
| 161 | # Detect host OS. |
| 162 | { |
| 163 | my $operating_system = Uname::operating_system(); |
| 164 | if ( 0 ) { |
| 165 | } elsif ( $operating_system eq "GNU/Linux" ) { |
| 166 | $_host_os = "lin"; |
| 167 | } elsif ( $operating_system eq "Darwin" ) { |
| 168 | $_host_os = "mac"; |
| 169 | } elsif ( $operating_system eq "MS Windows" ) { |
| 170 | $_host_os = "win"; |
| 171 | } else { |
| 172 | die "Unsupported host operating system: \"$operating_system\"; stopped"; |
| 173 | }; # if |
| 174 | } |
| 175 | |
| 176 | # Detect target arch. |
| 177 | if ( defined( $ENV{ LIBOMP_ARCH } ) ) { |
| 178 | # Use arch specified in LIBOMP_ARCH. |
| 179 | $_target_arch = canon_arch( $ENV{ LIBOMP_ARCH } ); |
| 180 | if ( not defined( $_target_arch ) ) { |
| 181 | die "Uknown architecture specified in LIBOMP_ARCH environment variable: \"$ENV{ LIBOMP_ARCH }\""; |
| 182 | }; # if |
| 183 | } else { |
| 184 | # Otherwise use host architecture. |
| 185 | $_target_arch = $_host_arch; |
| 186 | }; # if |
| 187 | $ENV{ LIBOMP_ARCH } = $_target_arch; |
| 188 | |
| 189 | # Detect target OS. |
| 190 | if ( defined( $ENV{ LIBOMP_OS } ) ) { |
| 191 | # Use OS specified in LIBOMP_OS. |
| 192 | $_target_os = canon_os( $ENV{ LIBOMP_OS } ); |
| 193 | if ( not defined( $_target_os ) ) { |
| 194 | die "Uknown OS specified in LIBOMP_OS environment variable: \"$ENV{ LIBOMP_OS }\""; |
| 195 | }; # if |
| 196 | } else { |
| 197 | # Otherwise use host OS. |
| 198 | $_target_os = $_host_os; |
| 199 | }; # if |
| 200 | $ENV{ LIBOMP_OS } = $_target_os; |
| 201 | |
| 202 | use vars @vars; |
| 203 | |
| 204 | tie( $host_arch, "Platform::host_arch" ); |
| 205 | tie( $host_os, "Platform::host_os" ); |
| 206 | tie( $host_platform, "Platform::host_platform" ); |
| 207 | tie( $target_arch, "Platform::target_arch" ); |
| 208 | tie( $target_os, "Platform::target_os" ); |
| 209 | tie( $target_platform, "Platform::target_platform" ); |
| 210 | |
| 211 | { package Platform::base; |
| 212 | |
| 213 | use Carp; |
| 214 | |
| 215 | use Tie::Scalar; |
| 216 | use base "Tie::StdScalar"; |
| 217 | |
| 218 | sub STORE { |
| 219 | my $self = shift( @_ ); |
| 220 | croak( "Modifying \$" . ref( $self ) . " is not allowed; stopped" ); |
| 221 | }; # sub STORE |
| 222 | |
| 223 | } # package Platform::base |
| 224 | |
| 225 | { package Platform::host_arch; |
| 226 | use base "Platform::base"; |
| 227 | sub FETCH { |
| 228 | return $_host_arch; |
| 229 | }; # sub FETCH |
| 230 | } # package Platform::host_arch |
| 231 | |
| 232 | { package Platform::host_os; |
| 233 | use base "Platform::base"; |
| 234 | sub FETCH { |
| 235 | return $_host_os; |
| 236 | }; # sub FETCH |
| 237 | } # package Platform::host_os |
| 238 | |
| 239 | { package Platform::host_platform; |
| 240 | use base "Platform::base"; |
| 241 | sub FETCH { |
| 242 | return "${_host_os}_${_host_arch}"; |
| 243 | }; # sub FETCH |
| 244 | } # package Platform::host_platform |
| 245 | |
| 246 | { package Platform::target_arch; |
| 247 | use base "Platform::base"; |
| 248 | sub FETCH { |
| 249 | return $_target_arch; |
| 250 | }; # sub FETCH |
| 251 | } # package Platform::target_arch |
| 252 | |
| 253 | { package Platform::target_os; |
| 254 | use base "Platform::base"; |
| 255 | sub FETCH { |
| 256 | return $_target_os; |
| 257 | }; # sub FETCH |
| 258 | } # package Platform::target_os |
| 259 | |
| 260 | { package Platform::target_platform; |
| 261 | use base "Platform::base"; |
| 262 | sub FETCH { |
| 263 | return "${_target_os}_${_target_arch}"; |
| 264 | }; # sub FETCH |
| 265 | } # package Platform::target_platform |
| 266 | |
| 267 | |
| 268 | return 1; |
| 269 | |
| 270 | __END__ |
| 271 | |
| 272 | =pod |
| 273 | |
| 274 | =head1 NAME |
| 275 | |
| 276 | B<Platform.pm> -- Few subroutines to get OS, architecture and platform name in form suitable for |
| 277 | naming files, directories, macros, etc. |
| 278 | |
| 279 | =head1 SYNOPSIS |
| 280 | |
| 281 | use Platform ":all"; |
| 282 | use tools; |
| 283 | |
| 284 | my $arch = canon_arch( "em64T" ); # Returns "32e". |
| 285 | my $legal = legal_arch( "em64t" ); # Returns "Intel(R) 64". |
| 286 | my $option = arch_opt( "em64t" ); # Returns "intel64". |
| 287 | my $os = canon_os( "Windows NT" ); # Returns "win". |
| 288 | |
| 289 | print( $host_arch, $host_os, $host_platform ); |
| 290 | print( $taregt_arch, $target_os, $target_platform ); |
| 291 | |
| 292 | tools::get_options( |
| 293 | Platform::target_options(), |
| 294 | ... |
| 295 | ); |
| 296 | |
| 297 | |
| 298 | =head1 DESCRIPTION |
| 299 | |
| 300 | Environment variable LIBOMP_OS specifies target OS to report. If LIBOMP_OS id not defined, |
| 301 | the script assumes host OS is target OS. |
| 302 | |
| 303 | Environment variable LIBOMP_ARCH specifies target architecture to report. If LIBOMP_ARCH is not defined, |
| 304 | the script assumes host architecture is target one. |
| 305 | |
| 306 | =head2 Functions. |
| 307 | |
| 308 | =over |
| 309 | |
| 310 | =item B<canon_arch( $arch )> |
| 311 | |
| 312 | Input string is an architecture name to canonize. The function recognizes many variants, for example: |
| 313 | C<32e>, C<Intel64>, C<Intel(R) 64>, etc. Returned string is a canononized architecture name, |
| 314 | one of: C<32>, C<32e>, C<64>, or C<undef> is input string is not recognized. |
| 315 | |
| 316 | =item B<legal_arch( $arch )> |
| 317 | |
| 318 | Input string is architecture name. The function recognizes the same variants as C<arch_canon()> does. |
| 319 | Returned string is a name approved by Intel Legal, one of: C<IA-32 architecture>, C<Intel(R) 64> |
| 320 | or C<undef> if input string is not recognized. |
| 321 | |
| 322 | =item B<arch_opt( $arch )> |
| 323 | |
| 324 | Input string is architecture name. The function recognizes the same variants as C<arch_canon()> does. |
| 325 | Returned string is an architecture name suitable for passing to compiler setup scripts |
| 326 | (e. g. C<iccvars.sh>), one of: C<IA-32 architecture>, C<Intel(R) 64> or C<undef> if input string is not |
| 327 | recognized. |
| 328 | |
| 329 | =item B<canon_os( $os )> |
| 330 | |
| 331 | Input string is OS name to canonize. The function recognizes many variants, for example: C<mac>, C<OS X>, etc. Returned string is a canonized OS name, one of: C<lin>, C<lrb>, |
| 332 | C<mac>, C<win>, or C<undef> is input string is not recognized. |
| 333 | |
| 334 | =item B<target_options()> |
| 335 | |
| 336 | Returns array suitable for passing to C<tools::get_options()> to let a script recognize |
| 337 | C<--target-architecture=I<str>> and C<--target-os=I<str>> options. Typical usage is: |
| 338 | |
| 339 | use tools; |
| 340 | use Platform; |
| 341 | |
| 342 | my ( $os, $arch, $platform ); # Global variables, not initialized. |
| 343 | |
| 344 | ... |
| 345 | |
| 346 | get_options( |
| 347 | Platform::target_options(), # Let script recognize --target-os and --target-arch options. |
| 348 | ... |
| 349 | ); |
| 350 | # Initialize variabls after parsing command line. |
| 351 | ( $os, $arch, $platform ) = ( Platform::target_os(), Platform::target_arch(), Platform::target_platform() ); |
| 352 | |
| 353 | =back |
| 354 | |
| 355 | =head2 Variables |
| 356 | |
| 357 | =item B<$host_arch> |
| 358 | |
| 359 | Canonized name of host architecture. |
| 360 | |
| 361 | =item B<$host_os> |
| 362 | |
| 363 | Canonized name of host OS. |
| 364 | |
| 365 | =item B<$host_platform> |
| 366 | |
| 367 | Host platform name (concatenated canonized OS name, underscore, and canonized architecture name). |
| 368 | |
| 369 | =item B<$target_arch> |
| 370 | |
| 371 | Canonized name of target architecture. |
| 372 | |
| 373 | =item B<$target_os> |
| 374 | |
| 375 | Canonized name of target OS. |
| 376 | |
| 377 | =item B<$target_platform> |
| 378 | |
| 379 | Target platform name (concatenated canonized OS name, underscore, and canonized architecture name). |
| 380 | |
| 381 | =back |
| 382 | |
| 383 | =cut |
| 384 | |
| 385 | # end of file # |
| 386 | |