blob: 5b399f738662e9f04083c01c5104e9084514c9e5 [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";
Alp Toker763b9392014-02-28 09:42:41 +0000173 } elsif ( $operating_system eq "FreeBSD" ) {
174 # Host OS resembles Linux.
175 $_host_os = "lin";
Jim Cownie5e8470a2013-09-27 10:38:44 +0000176 } elsif ( $operating_system eq "Darwin" ) {
177 $_host_os = "mac";
178 } elsif ( $operating_system eq "MS Windows" ) {
179 $_host_os = "win";
180 } else {
181 die "Unsupported host operating system: \"$operating_system\"; stopped";
182 }; # if
183}
184
185# Detect target arch.
186if ( defined( $ENV{ LIBOMP_ARCH } ) ) {
187 # Use arch specified in LIBOMP_ARCH.
188 $_target_arch = canon_arch( $ENV{ LIBOMP_ARCH } );
189 if ( not defined( $_target_arch ) ) {
Jim Cownie181b4bb2013-12-23 17:28:57 +0000190 die "Unknown architecture specified in LIBOMP_ARCH environment variable: \"$ENV{ LIBOMP_ARCH }\"";
Jim Cownie5e8470a2013-09-27 10:38:44 +0000191 }; # if
192} else {
193 # Otherwise use host architecture.
194 $_target_arch = $_host_arch;
195}; # if
196$ENV{ LIBOMP_ARCH } = $_target_arch;
197
198# Detect target OS.
199if ( defined( $ENV{ LIBOMP_OS } ) ) {
200 # Use OS specified in LIBOMP_OS.
201 $_target_os = canon_os( $ENV{ LIBOMP_OS } );
202 if ( not defined( $_target_os ) ) {
Jim Cownie181b4bb2013-12-23 17:28:57 +0000203 die "Unknown OS specified in LIBOMP_OS environment variable: \"$ENV{ LIBOMP_OS }\"";
Jim Cownie5e8470a2013-09-27 10:38:44 +0000204 }; # if
205} else {
206 # Otherwise use host OS.
207 $_target_os = $_host_os;
208}; # if
209$ENV{ LIBOMP_OS } = $_target_os;
210
211use vars @vars;
212
213tie( $host_arch, "Platform::host_arch" );
214tie( $host_os, "Platform::host_os" );
215tie( $host_platform, "Platform::host_platform" );
216tie( $target_arch, "Platform::target_arch" );
217tie( $target_os, "Platform::target_os" );
218tie( $target_platform, "Platform::target_platform" );
219
220{ package Platform::base;
221
222 use Carp;
223
224 use Tie::Scalar;
225 use base "Tie::StdScalar";
226
227 sub STORE {
228 my $self = shift( @_ );
229 croak( "Modifying \$" . ref( $self ) . " is not allowed; stopped" );
230 }; # sub STORE
231
232} # package Platform::base
233
234{ package Platform::host_arch;
235 use base "Platform::base";
236 sub FETCH {
237 return $_host_arch;
238 }; # sub FETCH
239} # package Platform::host_arch
240
241{ package Platform::host_os;
242 use base "Platform::base";
243 sub FETCH {
244 return $_host_os;
245 }; # sub FETCH
246} # package Platform::host_os
247
248{ package Platform::host_platform;
249 use base "Platform::base";
250 sub FETCH {
251 return "${_host_os}_${_host_arch}";
252 }; # sub FETCH
253} # package Platform::host_platform
254
255{ package Platform::target_arch;
256 use base "Platform::base";
257 sub FETCH {
258 return $_target_arch;
259 }; # sub FETCH
260} # package Platform::target_arch
261
262{ package Platform::target_os;
263 use base "Platform::base";
264 sub FETCH {
265 return $_target_os;
266 }; # sub FETCH
267} # package Platform::target_os
268
269{ package Platform::target_platform;
270 use base "Platform::base";
271 sub FETCH {
272 return "${_target_os}_${_target_arch}";
273 }; # sub FETCH
274} # package Platform::target_platform
275
276
277return 1;
278
279__END__
280
281=pod
282
283=head1 NAME
284
285B<Platform.pm> -- Few subroutines to get OS, architecture and platform name in form suitable for
286naming files, directories, macros, etc.
287
288=head1 SYNOPSIS
289
290 use Platform ":all";
291 use tools;
292
293 my $arch = canon_arch( "em64T" ); # Returns "32e".
294 my $legal = legal_arch( "em64t" ); # Returns "Intel(R) 64".
295 my $option = arch_opt( "em64t" ); # Returns "intel64".
296 my $os = canon_os( "Windows NT" ); # Returns "win".
297
298 print( $host_arch, $host_os, $host_platform );
299 print( $taregt_arch, $target_os, $target_platform );
300
301 tools::get_options(
302 Platform::target_options(),
303 ...
304 );
305
306
307=head1 DESCRIPTION
308
309Environment variable LIBOMP_OS specifies target OS to report. If LIBOMP_OS id not defined,
310the script assumes host OS is target OS.
311
312Environment variable LIBOMP_ARCH specifies target architecture to report. If LIBOMP_ARCH is not defined,
313the script assumes host architecture is target one.
314
315=head2 Functions.
316
317=over
318
319=item B<canon_arch( $arch )>
320
321Input string is an architecture name to canonize. The function recognizes many variants, for example:
322C<32e>, C<Intel64>, C<Intel(R) 64>, etc. Returned string is a canononized architecture name,
323one of: C<32>, C<32e>, C<64>, or C<undef> is input string is not recognized.
324
325=item B<legal_arch( $arch )>
326
327Input string is architecture name. The function recognizes the same variants as C<arch_canon()> does.
328Returned string is a name approved by Intel Legal, one of: C<IA-32 architecture>, C<Intel(R) 64>
329or C<undef> if input string is not recognized.
330
331=item B<arch_opt( $arch )>
332
333Input string is architecture name. The function recognizes the same variants as C<arch_canon()> does.
334Returned string is an architecture name suitable for passing to compiler setup scripts
335(e. g. C<iccvars.sh>), one of: C<IA-32 architecture>, C<Intel(R) 64> or C<undef> if input string is not
336recognized.
337
338=item B<canon_os( $os )>
339
340Input 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>,
341C<mac>, C<win>, or C<undef> is input string is not recognized.
342
343=item B<target_options()>
344
345Returns array suitable for passing to C<tools::get_options()> to let a script recognize
346C<--target-architecture=I<str>> and C<--target-os=I<str>> options. Typical usage is:
347
348 use tools;
349 use Platform;
350
351 my ( $os, $arch, $platform ); # Global variables, not initialized.
352
353 ...
354
355 get_options(
356 Platform::target_options(), # Let script recognize --target-os and --target-arch options.
357 ...
358 );
359 # Initialize variabls after parsing command line.
360 ( $os, $arch, $platform ) = ( Platform::target_os(), Platform::target_arch(), Platform::target_platform() );
361
362=back
363
364=head2 Variables
365
366=item B<$host_arch>
367
368Canonized name of host architecture.
369
370=item B<$host_os>
371
372Canonized name of host OS.
373
374=item B<$host_platform>
375
376Host platform name (concatenated canonized OS name, underscore, and canonized architecture name).
377
378=item B<$target_arch>
379
380Canonized name of target architecture.
381
382=item B<$target_os>
383
384Canonized name of target OS.
385
386=item B<$target_platform>
387
388Target platform name (concatenated canonized OS name, underscore, and canonized architecture name).
389
390=back
391
392=cut
393
394# end of file #
395