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