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