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