blob: 8b701c24b7b028195167df52596ffe28d30f8101 [file] [log] [blame]
Jim Cownie5e8470a2013-09-27 10:38:44 +00001#
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
23package Platform;
24
25use strict;
26use warnings;
27
28use base "Exporter";
29
30use Uname;
31
32my @vars;
33
34BEGIN {
Andrey Churbanovd315cea2015-01-16 12:54:51 +000035 @vars = qw{ $host_arch $host_os $host_platform $target_arch $target_mic_arch $target_os $target_platform };
Jim Cownie5e8470a2013-09-27 10:38:44 +000036}
37
38our $VERSION = "0.014";
39our @EXPORT = qw{};
Andrey Churbanovd315cea2015-01-16 12:54:51 +000040our @EXPORT_OK = ( qw{ canon_arch canon_os canon_mic_arch legal_arch arch_opt }, @vars );
Jim Cownie5e8470a2013-09-27 10:38:44 +000041our %EXPORT_TAGS = ( all => [ @EXPORT_OK ], vars => \@vars );
42
43# Canonize architecture name.
44sub 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";
Jim Cownie181b4bb2013-12-23 17:28:57 +000051 } elsif ( $arch =~ m{\Aarm(?:v7\D*)?\z} ) {
52 $arch = "arm";
Andrey Churbanovd1c55042015-01-19 18:29:35 +000053 } elsif ( $arch =~ m{\Appc64le} ) {
54 $arch = "ppc64le";
Jim Cownie3051f972014-08-07 10:12:54 +000055 } elsif ( $arch =~ m{\Appc64} ) {
56 $arch = "ppc64";
Andrey Churbanovcbda8682015-01-13 14:43:35 +000057 } elsif ( $arch =~ m{\Aaarch64} ) {
58 $arch = "aarch64";
Andrey Churbanovd315cea2015-01-16 12:54:51 +000059 } elsif ( $arch =~ m{\Amic} ) {
60 $arch = "mic";
Jim Cownie5e8470a2013-09-27 10:38:44 +000061 } else {
62 $arch = undef;
63 }; # if
64 }; # if
65 return $arch;
66}; # sub canon_arch
67
Andrey Churbanovd315cea2015-01-16 12:54:51 +000068# Canonize Intel(R) Many Integrated Core Architecture name.
69sub canon_mic_arch($) {
70 my ( $mic_arch ) = @_;
71 if ( defined( $mic_arch ) ) {
72 if ( $mic_arch =~ m{\Aknf} ) {
73 $mic_arch = "knf";
74 } elsif ( $mic_arch =~ m{\Aknc}) {
75 $mic_arch = "knc";
76 } elsif ( $mic_arch =~ m{\Aknl} ) {
77 $mic_arch = "knl";
78 } else {
79 $mic_arch = undef;
80 }; # if
81 }; # if
82 return $mic_arch;
83}; # sub canon_mic_arch
84
Jim Cownie5e8470a2013-09-27 10:38:44 +000085{ # Return legal approved architecture name.
86 my %legal = (
87 "32" => "IA-32 architecture",
88 "32e" => "Intel(R) 64",
Jim Cownie181b4bb2013-12-23 17:28:57 +000089 "arm" => "ARM",
Andrey Churbanovcbda8682015-01-13 14:43:35 +000090 "aarch64" => "AArch64",
Andrey Churbanovd315cea2015-01-16 12:54:51 +000091 "mic" => "Intel(R) Many Integrated Core Architecture",
Jim Cownie5e8470a2013-09-27 10:38:44 +000092 );
93
94 sub legal_arch($) {
95 my ( $arch ) = @_;
96 $arch = canon_arch( $arch );
97 if ( defined( $arch ) ) {
98 $arch = $legal{ $arch };
99 }; # if
100 return $arch;
101 }; # sub legal_arch
102}
103
104{ # Return architecture name suitable for Intel compiler setup scripts.
105 my %option = (
106 "32" => "ia32",
107 "32e" => "intel64",
108 "64" => "ia64",
Jim Cownie181b4bb2013-12-23 17:28:57 +0000109 "arm" => "arm",
Andrey Churbanovcbda8682015-01-13 14:43:35 +0000110 "aarch64" => "aarch",
Andrey Churbanovd315cea2015-01-16 12:54:51 +0000111 "mic" => "intel64",
Jim Cownie5e8470a2013-09-27 10:38:44 +0000112 );
113
114 sub arch_opt($) {
115 my ( $arch ) = @_;
116 $arch = canon_arch( $arch );
117 if ( defined( $arch ) ) {
118 $arch = $option{ $arch };
119 }; # if
120 return $arch;
121 }; # sub arch_opt
122}
123
124# Canonize OS name.
125sub canon_os($) {
126 my ( $os ) = @_;
127 if ( defined( $os ) ) {
128 if ( $os =~ m{\A\s*(?:Linux|lin|l)\s*\z}i ) {
129 $os = "lin";
Jim Cownie5e8470a2013-09-27 10:38:44 +0000130 } elsif ( $os =~ m{\A\s*(?:Mac(?:\s*OS(?:\s*X)?)?|mac|m|Darwin)\s*\z}i ) {
131 $os = "mac";
132 } elsif ( $os =~ m{\A\s*(?:Win(?:dows)?(?:(?:_|\s*)?(?:NT|XP|95|98|2003))?|w)\s*\z}i ) {
133 $os = "win";
134 } else {
135 $os = undef;
136 }; # if
137 }; # if
138 return $os;
139}; # sub canon_os
140
Andrey Churbanovd315cea2015-01-16 12:54:51 +0000141my ( $_host_os, $_host_arch, $_target_os, $_target_arch, $_target_mic_arch, $_default_mic_arch);
142
143# Set the default mic-arch value.
144$_default_mic_arch = "knc";
Jim Cownie5e8470a2013-09-27 10:38:44 +0000145
146sub set_target_arch($) {
147 my ( $arch ) = canon_arch( $_[ 0 ] );
148 if ( defined( $arch ) ) {
149 $_target_arch = $arch;
150 $ENV{ LIBOMP_ARCH } = $arch;
151 }; # if
152 return $arch;
153}; # sub set_target_arch
154
Andrey Churbanovd315cea2015-01-16 12:54:51 +0000155sub set_target_mic_arch($) {
156 my ( $mic_arch ) = canon_mic_arch( $_[ 0 ] );
157 if ( defined( $mic_arch ) ) {
158 $_target_mic_arch = $mic_arch;
159 $ENV{ LIBOMP_MIC_ARCH } = $mic_arch;
160 }; # if
161 return $mic_arch;
162}; # sub set_target_mic_arch
163
Jim Cownie5e8470a2013-09-27 10:38:44 +0000164sub set_target_os($) {
165 my ( $os ) = canon_os( $_[ 0 ] );
166 if ( defined( $os ) ) {
167 $_target_os = $os;
168 $ENV{ LIBOMP_OS } = $os;
169 }; # if
170 return $os;
171}; # sub set_target_os
172
173sub target_options() {
174 my @options = (
175 "target-os|os=s" =>
176 sub {
177 set_target_os( $_[ 1 ] ) or
178 die "Bad value of --target-os option: \"$_[ 1 ]\"\n";
179 },
180 "target-architecture|targert-arch|architecture|arch=s" =>
181 sub {
182 set_target_arch( $_[ 1 ] ) or
183 die "Bad value of --target-architecture option: \"$_[ 1 ]\"\n";
184 },
Andrey Churbanovd315cea2015-01-16 12:54:51 +0000185 "target-mic-architecture|targert-mic-arch|mic-architecture|mic-arch=s" =>
186 sub {
187 set_target_mic_arch( $_[ 1 ] ) or
188 die "Bad value of --target-mic-architecture option: \"$_[ 1 ]\"\n";
189 },
Jim Cownie5e8470a2013-09-27 10:38:44 +0000190 );
191 return @options;
192}; # sub target_options
193
194# Detect host arch.
195{
196 my $hardware_platform = Uname::hardware_platform();
197 if ( 0 ) {
198 } elsif ( $hardware_platform eq "i386" ) {
199 $_host_arch = "32";
200 } elsif ( $hardware_platform eq "ia64" ) {
201 $_host_arch = "64";
202 } elsif ( $hardware_platform eq "x86_64" ) {
203 $_host_arch = "32e";
Jim Cownie181b4bb2013-12-23 17:28:57 +0000204 } elsif ( $hardware_platform eq "arm" ) {
205 $_host_arch = "arm";
Andrey Churbanovd1c55042015-01-19 18:29:35 +0000206 } elsif ( $hardware_platform eq "ppc64le" ) {
207 $_host_arch = "ppc64le";
Jim Cownie3051f972014-08-07 10:12:54 +0000208 } elsif ( $hardware_platform eq "ppc64" ) {
209 $_host_arch = "ppc64";
Andrey Churbanovcbda8682015-01-13 14:43:35 +0000210 } elsif ( $hardware_platform eq "aarch64" ) {
211 $_host_arch = "aarch64";
Jim Cownie5e8470a2013-09-27 10:38:44 +0000212 } else {
213 die "Unsupported host hardware platform: \"$hardware_platform\"; stopped";
214 }; # if
215}
216
217# Detect host OS.
218{
219 my $operating_system = Uname::operating_system();
220 if ( 0 ) {
221 } elsif ( $operating_system eq "GNU/Linux" ) {
222 $_host_os = "lin";
Alp Toker763b9392014-02-28 09:42:41 +0000223 } elsif ( $operating_system eq "FreeBSD" ) {
224 # Host OS resembles Linux.
225 $_host_os = "lin";
Jim Cownie5e8470a2013-09-27 10:38:44 +0000226 } elsif ( $operating_system eq "Darwin" ) {
227 $_host_os = "mac";
228 } elsif ( $operating_system eq "MS Windows" ) {
229 $_host_os = "win";
230 } else {
231 die "Unsupported host operating system: \"$operating_system\"; stopped";
232 }; # if
233}
234
235# Detect target arch.
236if ( defined( $ENV{ LIBOMP_ARCH } ) ) {
237 # Use arch specified in LIBOMP_ARCH.
238 $_target_arch = canon_arch( $ENV{ LIBOMP_ARCH } );
239 if ( not defined( $_target_arch ) ) {
Jim Cownie181b4bb2013-12-23 17:28:57 +0000240 die "Unknown architecture specified in LIBOMP_ARCH environment variable: \"$ENV{ LIBOMP_ARCH }\"";
Jim Cownie5e8470a2013-09-27 10:38:44 +0000241 }; # if
242} else {
243 # Otherwise use host architecture.
244 $_target_arch = $_host_arch;
245}; # if
246$ENV{ LIBOMP_ARCH } = $_target_arch;
247
Andrey Churbanovd315cea2015-01-16 12:54:51 +0000248# Detect target Intel(R) Many Integrated Core Architecture.
249if ( defined( $ENV{ LIBOMP_MIC_ARCH } ) ) {
250 # Use mic arch specified in LIBOMP_MIC_ARCH.
251 $_target_mic_arch = canon_mic_arch( $ENV{ LIBOMP_MIC_ARCH } );
252 if ( not defined( $_target_mic_arch ) ) {
253 die "Unknown architecture specified in LIBOMP_MIC_ARCH environment variable: \"$ENV{ LIBOMP_MIC_ARCH }\"";
254 }; # if
255} else {
256 # Otherwise use default Intel(R) Many Integrated Core Architecture.
257 $_target_mic_arch = $_default_mic_arch;
258}; # if
259$ENV{ LIBOMP_MIC_ARCH } = $_target_mic_arch;
260
Jim Cownie5e8470a2013-09-27 10:38:44 +0000261# Detect target OS.
262if ( defined( $ENV{ LIBOMP_OS } ) ) {
263 # Use OS specified in LIBOMP_OS.
264 $_target_os = canon_os( $ENV{ LIBOMP_OS } );
265 if ( not defined( $_target_os ) ) {
Jim Cownie181b4bb2013-12-23 17:28:57 +0000266 die "Unknown OS specified in LIBOMP_OS environment variable: \"$ENV{ LIBOMP_OS }\"";
Jim Cownie5e8470a2013-09-27 10:38:44 +0000267 }; # if
268} else {
269 # Otherwise use host OS.
270 $_target_os = $_host_os;
271}; # if
272$ENV{ LIBOMP_OS } = $_target_os;
273
274use vars @vars;
275
276tie( $host_arch, "Platform::host_arch" );
277tie( $host_os, "Platform::host_os" );
278tie( $host_platform, "Platform::host_platform" );
279tie( $target_arch, "Platform::target_arch" );
Andrey Churbanovd315cea2015-01-16 12:54:51 +0000280tie( $target_mic_arch, "Platform::target_mic_arch" );
Jim Cownie5e8470a2013-09-27 10:38:44 +0000281tie( $target_os, "Platform::target_os" );
282tie( $target_platform, "Platform::target_platform" );
283
284{ package Platform::base;
285
286 use Carp;
287
288 use Tie::Scalar;
289 use base "Tie::StdScalar";
290
291 sub STORE {
292 my $self = shift( @_ );
293 croak( "Modifying \$" . ref( $self ) . " is not allowed; stopped" );
294 }; # sub STORE
295
296} # package Platform::base
297
298{ package Platform::host_arch;
299 use base "Platform::base";
300 sub FETCH {
301 return $_host_arch;
302 }; # sub FETCH
303} # package Platform::host_arch
304
305{ package Platform::host_os;
306 use base "Platform::base";
307 sub FETCH {
308 return $_host_os;
309 }; # sub FETCH
310} # package Platform::host_os
311
312{ package Platform::host_platform;
313 use base "Platform::base";
314 sub FETCH {
315 return "${_host_os}_${_host_arch}";
316 }; # sub FETCH
317} # package Platform::host_platform
318
319{ package Platform::target_arch;
320 use base "Platform::base";
321 sub FETCH {
322 return $_target_arch;
323 }; # sub FETCH
324} # package Platform::target_arch
325
Andrey Churbanovd315cea2015-01-16 12:54:51 +0000326{ package Platform::target_mic_arch;
327 use base "Platform::base";
328 sub FETCH {
329 return $_target_mic_arch;
330 }; # sub FETCH
331} # package Platform::target_mic_arch
332
Jim Cownie5e8470a2013-09-27 10:38:44 +0000333{ package Platform::target_os;
334 use base "Platform::base";
335 sub FETCH {
336 return $_target_os;
337 }; # sub FETCH
338} # package Platform::target_os
339
340{ package Platform::target_platform;
341 use base "Platform::base";
342 sub FETCH {
Andrey Churbanovd315cea2015-01-16 12:54:51 +0000343 if ($_target_arch eq "mic") {
344 return "${_target_os}_${_target_mic_arch}";
345 } else {
Jim Cownie5e8470a2013-09-27 10:38:44 +0000346 return "${_target_os}_${_target_arch}";
Andrey Churbanovd315cea2015-01-16 12:54:51 +0000347 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000348 }; # sub FETCH
349} # package Platform::target_platform
350
351
352return 1;
353
354__END__
355
356=pod
357
358=head1 NAME
359
360B<Platform.pm> -- Few subroutines to get OS, architecture and platform name in form suitable for
361naming files, directories, macros, etc.
362
363=head1 SYNOPSIS
364
365 use Platform ":all";
366 use tools;
367
368 my $arch = canon_arch( "em64T" ); # Returns "32e".
369 my $legal = legal_arch( "em64t" ); # Returns "Intel(R) 64".
370 my $option = arch_opt( "em64t" ); # Returns "intel64".
371 my $os = canon_os( "Windows NT" ); # Returns "win".
372
373 print( $host_arch, $host_os, $host_platform );
374 print( $taregt_arch, $target_os, $target_platform );
375
376 tools::get_options(
377 Platform::target_options(),
378 ...
379 );
380
381
382=head1 DESCRIPTION
383
384Environment variable LIBOMP_OS specifies target OS to report. If LIBOMP_OS id not defined,
385the script assumes host OS is target OS.
386
387Environment variable LIBOMP_ARCH specifies target architecture to report. If LIBOMP_ARCH is not defined,
388the script assumes host architecture is target one.
389
390=head2 Functions.
391
392=over
393
394=item B<canon_arch( $arch )>
395
396Input string is an architecture name to canonize. The function recognizes many variants, for example:
397C<32e>, C<Intel64>, C<Intel(R) 64>, etc. Returned string is a canononized architecture name,
Andrey Churbanovd1c55042015-01-19 18:29:35 +0000398one of: C<32>, C<32e>, C<64>, C<arm>, C<ppc64le>, C<ppc64>, C<mic>, or C<undef> is input string is not recognized.
Jim Cownie5e8470a2013-09-27 10:38:44 +0000399
400=item B<legal_arch( $arch )>
401
402Input string is architecture name. The function recognizes the same variants as C<arch_canon()> does.
403Returned string is a name approved by Intel Legal, one of: C<IA-32 architecture>, C<Intel(R) 64>
404or C<undef> if input string is not recognized.
405
406=item B<arch_opt( $arch )>
407
408Input string is architecture name. The function recognizes the same variants as C<arch_canon()> does.
409Returned string is an architecture name suitable for passing to compiler setup scripts
410(e. g. C<iccvars.sh>), one of: C<IA-32 architecture>, C<Intel(R) 64> or C<undef> if input string is not
411recognized.
412
413=item B<canon_os( $os )>
414
Andrey Churbanovd315cea2015-01-16 12:54:51 +0000415Input 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>,
Jim Cownie5e8470a2013-09-27 10:38:44 +0000416C<mac>, C<win>, or C<undef> is input string is not recognized.
417
418=item B<target_options()>
419
420Returns array suitable for passing to C<tools::get_options()> to let a script recognize
421C<--target-architecture=I<str>> and C<--target-os=I<str>> options. Typical usage is:
422
423 use tools;
424 use Platform;
425
426 my ( $os, $arch, $platform ); # Global variables, not initialized.
427
428 ...
429
430 get_options(
431 Platform::target_options(), # Let script recognize --target-os and --target-arch options.
432 ...
433 );
434 # Initialize variabls after parsing command line.
435 ( $os, $arch, $platform ) = ( Platform::target_os(), Platform::target_arch(), Platform::target_platform() );
436
437=back
438
439=head2 Variables
440
441=item B<$host_arch>
442
443Canonized name of host architecture.
444
445=item B<$host_os>
446
447Canonized name of host OS.
448
449=item B<$host_platform>
450
451Host platform name (concatenated canonized OS name, underscore, and canonized architecture name).
452
453=item B<$target_arch>
454
455Canonized name of target architecture.
456
457=item B<$target_os>
458
459Canonized name of target OS.
460
461=item B<$target_platform>
462
463Target platform name (concatenated canonized OS name, underscore, and canonized architecture name).
464
465=back
466
467=cut
468
469# end of file #
470