blob: 839e54f824b49a4c07aacfdf3bf00512d1773de6 [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 {
35 @vars = qw{ $host_arch $host_os $host_platform $target_arch $target_os $target_platform };
36}
37
38our $VERSION = "0.014";
39our @EXPORT = qw{};
40our @EXPORT_OK = ( qw{ canon_arch canon_os legal_arch arch_opt }, @vars );
41our %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";
Jim Cownie3051f972014-08-07 10:12:54 +000053 } elsif ( $arch =~ m{\Appc64} ) {
54 $arch = "ppc64";
Andrey Churbanovcbda8682015-01-13 14:43:35 +000055 } elsif ( $arch =~ m{\Aaarch64} ) {
56 $arch = "aarch64";
Jim Cownie5e8470a2013-09-27 10:38:44 +000057 } else {
58 $arch = undef;
59 }; # if
60 }; # if
61 return $arch;
62}; # sub canon_arch
63
64{ # Return legal approved architecture name.
65 my %legal = (
66 "32" => "IA-32 architecture",
67 "32e" => "Intel(R) 64",
Jim Cownie181b4bb2013-12-23 17:28:57 +000068 "arm" => "ARM",
Andrey Churbanovcbda8682015-01-13 14:43:35 +000069 "aarch64" => "AArch64",
Jim Cownie5e8470a2013-09-27 10:38:44 +000070 );
71
72 sub legal_arch($) {
73 my ( $arch ) = @_;
74 $arch = canon_arch( $arch );
75 if ( defined( $arch ) ) {
76 $arch = $legal{ $arch };
77 }; # if
78 return $arch;
79 }; # sub legal_arch
80}
81
82{ # Return architecture name suitable for Intel compiler setup scripts.
83 my %option = (
84 "32" => "ia32",
85 "32e" => "intel64",
86 "64" => "ia64",
Jim Cownie181b4bb2013-12-23 17:28:57 +000087 "arm" => "arm",
Andrey Churbanovcbda8682015-01-13 14:43:35 +000088 "aarch64" => "aarch",
Jim Cownie5e8470a2013-09-27 10:38:44 +000089 );
90
91 sub arch_opt($) {
92 my ( $arch ) = @_;
93 $arch = canon_arch( $arch );
94 if ( defined( $arch ) ) {
95 $arch = $option{ $arch };
96 }; # if
97 return $arch;
98 }; # sub arch_opt
99}
100
101# Canonize OS name.
102sub canon_os($) {
103 my ( $os ) = @_;
104 if ( defined( $os ) ) {
105 if ( $os =~ m{\A\s*(?:Linux|lin|l)\s*\z}i ) {
106 $os = "lin";
107 } elsif ( $os =~ m{\A\s*(?:lrb)\s*\z}i ) {
108 $os = "lrb";
109 } elsif ( $os =~ m{\A\s*(?:Mac(?:\s*OS(?:\s*X)?)?|mac|m|Darwin)\s*\z}i ) {
110 $os = "mac";
111 } elsif ( $os =~ m{\A\s*(?:Win(?:dows)?(?:(?:_|\s*)?(?:NT|XP|95|98|2003))?|w)\s*\z}i ) {
112 $os = "win";
113 } else {
114 $os = undef;
115 }; # if
116 }; # if
117 return $os;
118}; # sub canon_os
119
120my ( $_host_os, $_host_arch, $_target_os, $_target_arch );
121
122sub set_target_arch($) {
123 my ( $arch ) = canon_arch( $_[ 0 ] );
124 if ( defined( $arch ) ) {
125 $_target_arch = $arch;
126 $ENV{ LIBOMP_ARCH } = $arch;
127 }; # if
128 return $arch;
129}; # sub set_target_arch
130
131sub set_target_os($) {
132 my ( $os ) = canon_os( $_[ 0 ] );
133 if ( defined( $os ) ) {
134 $_target_os = $os;
135 $ENV{ LIBOMP_OS } = $os;
136 }; # if
137 return $os;
138}; # sub set_target_os
139
140sub target_options() {
141 my @options = (
142 "target-os|os=s" =>
143 sub {
144 set_target_os( $_[ 1 ] ) or
145 die "Bad value of --target-os option: \"$_[ 1 ]\"\n";
146 },
147 "target-architecture|targert-arch|architecture|arch=s" =>
148 sub {
149 set_target_arch( $_[ 1 ] ) or
150 die "Bad value of --target-architecture option: \"$_[ 1 ]\"\n";
151 },
152 );
153 return @options;
154}; # sub target_options
155
156# Detect host arch.
157{
158 my $hardware_platform = Uname::hardware_platform();
159 if ( 0 ) {
160 } elsif ( $hardware_platform eq "i386" ) {
161 $_host_arch = "32";
162 } elsif ( $hardware_platform eq "ia64" ) {
163 $_host_arch = "64";
164 } elsif ( $hardware_platform eq "x86_64" ) {
165 $_host_arch = "32e";
Jim Cownie181b4bb2013-12-23 17:28:57 +0000166 } elsif ( $hardware_platform eq "arm" ) {
167 $_host_arch = "arm";
Jim Cownie3051f972014-08-07 10:12:54 +0000168 } elsif ( $hardware_platform eq "ppc64" ) {
169 $_host_arch = "ppc64";
Andrey Churbanovcbda8682015-01-13 14:43:35 +0000170 } elsif ( $hardware_platform eq "aarch64" ) {
171 $_host_arch = "aarch64";
Jim Cownie5e8470a2013-09-27 10:38:44 +0000172 } else {
173 die "Unsupported host hardware platform: \"$hardware_platform\"; stopped";
174 }; # if
175}
176
177# Detect host OS.
178{
179 my $operating_system = Uname::operating_system();
180 if ( 0 ) {
181 } elsif ( $operating_system eq "GNU/Linux" ) {
182 $_host_os = "lin";
Alp Toker763b9392014-02-28 09:42:41 +0000183 } elsif ( $operating_system eq "FreeBSD" ) {
184 # Host OS resembles Linux.
185 $_host_os = "lin";
Jim Cownie5e8470a2013-09-27 10:38:44 +0000186 } elsif ( $operating_system eq "Darwin" ) {
187 $_host_os = "mac";
188 } elsif ( $operating_system eq "MS Windows" ) {
189 $_host_os = "win";
190 } else {
191 die "Unsupported host operating system: \"$operating_system\"; stopped";
192 }; # if
193}
194
195# Detect target arch.
196if ( defined( $ENV{ LIBOMP_ARCH } ) ) {
197 # Use arch specified in LIBOMP_ARCH.
198 $_target_arch = canon_arch( $ENV{ LIBOMP_ARCH } );
199 if ( not defined( $_target_arch ) ) {
Jim Cownie181b4bb2013-12-23 17:28:57 +0000200 die "Unknown architecture specified in LIBOMP_ARCH environment variable: \"$ENV{ LIBOMP_ARCH }\"";
Jim Cownie5e8470a2013-09-27 10:38:44 +0000201 }; # if
202} else {
203 # Otherwise use host architecture.
204 $_target_arch = $_host_arch;
205}; # if
206$ENV{ LIBOMP_ARCH } = $_target_arch;
207
208# Detect target OS.
209if ( defined( $ENV{ LIBOMP_OS } ) ) {
210 # Use OS specified in LIBOMP_OS.
211 $_target_os = canon_os( $ENV{ LIBOMP_OS } );
212 if ( not defined( $_target_os ) ) {
Jim Cownie181b4bb2013-12-23 17:28:57 +0000213 die "Unknown OS specified in LIBOMP_OS environment variable: \"$ENV{ LIBOMP_OS }\"";
Jim Cownie5e8470a2013-09-27 10:38:44 +0000214 }; # if
215} else {
216 # Otherwise use host OS.
217 $_target_os = $_host_os;
218}; # if
219$ENV{ LIBOMP_OS } = $_target_os;
220
221use vars @vars;
222
223tie( $host_arch, "Platform::host_arch" );
224tie( $host_os, "Platform::host_os" );
225tie( $host_platform, "Platform::host_platform" );
226tie( $target_arch, "Platform::target_arch" );
227tie( $target_os, "Platform::target_os" );
228tie( $target_platform, "Platform::target_platform" );
229
230{ package Platform::base;
231
232 use Carp;
233
234 use Tie::Scalar;
235 use base "Tie::StdScalar";
236
237 sub STORE {
238 my $self = shift( @_ );
239 croak( "Modifying \$" . ref( $self ) . " is not allowed; stopped" );
240 }; # sub STORE
241
242} # package Platform::base
243
244{ package Platform::host_arch;
245 use base "Platform::base";
246 sub FETCH {
247 return $_host_arch;
248 }; # sub FETCH
249} # package Platform::host_arch
250
251{ package Platform::host_os;
252 use base "Platform::base";
253 sub FETCH {
254 return $_host_os;
255 }; # sub FETCH
256} # package Platform::host_os
257
258{ package Platform::host_platform;
259 use base "Platform::base";
260 sub FETCH {
261 return "${_host_os}_${_host_arch}";
262 }; # sub FETCH
263} # package Platform::host_platform
264
265{ package Platform::target_arch;
266 use base "Platform::base";
267 sub FETCH {
268 return $_target_arch;
269 }; # sub FETCH
270} # package Platform::target_arch
271
272{ package Platform::target_os;
273 use base "Platform::base";
274 sub FETCH {
275 return $_target_os;
276 }; # sub FETCH
277} # package Platform::target_os
278
279{ package Platform::target_platform;
280 use base "Platform::base";
281 sub FETCH {
282 return "${_target_os}_${_target_arch}";
283 }; # sub FETCH
284} # package Platform::target_platform
285
286
287return 1;
288
289__END__
290
291=pod
292
293=head1 NAME
294
295B<Platform.pm> -- Few subroutines to get OS, architecture and platform name in form suitable for
296naming files, directories, macros, etc.
297
298=head1 SYNOPSIS
299
300 use Platform ":all";
301 use tools;
302
303 my $arch = canon_arch( "em64T" ); # Returns "32e".
304 my $legal = legal_arch( "em64t" ); # Returns "Intel(R) 64".
305 my $option = arch_opt( "em64t" ); # Returns "intel64".
306 my $os = canon_os( "Windows NT" ); # Returns "win".
307
308 print( $host_arch, $host_os, $host_platform );
309 print( $taregt_arch, $target_os, $target_platform );
310
311 tools::get_options(
312 Platform::target_options(),
313 ...
314 );
315
316
317=head1 DESCRIPTION
318
319Environment variable LIBOMP_OS specifies target OS to report. If LIBOMP_OS id not defined,
320the script assumes host OS is target OS.
321
322Environment variable LIBOMP_ARCH specifies target architecture to report. If LIBOMP_ARCH is not defined,
323the script assumes host architecture is target one.
324
325=head2 Functions.
326
327=over
328
329=item B<canon_arch( $arch )>
330
331Input string is an architecture name to canonize. The function recognizes many variants, for example:
332C<32e>, C<Intel64>, C<Intel(R) 64>, etc. Returned string is a canononized architecture name,
333one of: C<32>, C<32e>, C<64>, or C<undef> is input string is not recognized.
334
335=item B<legal_arch( $arch )>
336
337Input string is architecture name. The function recognizes the same variants as C<arch_canon()> does.
338Returned string is a name approved by Intel Legal, one of: C<IA-32 architecture>, C<Intel(R) 64>
339or C<undef> if input string is not recognized.
340
341=item B<arch_opt( $arch )>
342
343Input string is architecture name. The function recognizes the same variants as C<arch_canon()> does.
344Returned string is an architecture name suitable for passing to compiler setup scripts
345(e. g. C<iccvars.sh>), one of: C<IA-32 architecture>, C<Intel(R) 64> or C<undef> if input string is not
346recognized.
347
348=item B<canon_os( $os )>
349
350Input 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>,
351C<mac>, C<win>, or C<undef> is input string is not recognized.
352
353=item B<target_options()>
354
355Returns array suitable for passing to C<tools::get_options()> to let a script recognize
356C<--target-architecture=I<str>> and C<--target-os=I<str>> options. Typical usage is:
357
358 use tools;
359 use Platform;
360
361 my ( $os, $arch, $platform ); # Global variables, not initialized.
362
363 ...
364
365 get_options(
366 Platform::target_options(), # Let script recognize --target-os and --target-arch options.
367 ...
368 );
369 # Initialize variabls after parsing command line.
370 ( $os, $arch, $platform ) = ( Platform::target_os(), Platform::target_arch(), Platform::target_platform() );
371
372=back
373
374=head2 Variables
375
376=item B<$host_arch>
377
378Canonized name of host architecture.
379
380=item B<$host_os>
381
382Canonized name of host OS.
383
384=item B<$host_platform>
385
386Host platform name (concatenated canonized OS name, underscore, and canonized architecture name).
387
388=item B<$target_arch>
389
390Canonized name of target architecture.
391
392=item B<$target_os>
393
394Canonized name of target OS.
395
396=item B<$target_platform>
397
398Target platform name (concatenated canonized OS name, underscore, and canonized architecture name).
399
400=back
401
402=cut
403
404# end of file #
405