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.
5 # To get help about exported variables and subroutines, execute the following command:
9 # or see POD (Plain Old Documentation) imbedded to the source...
13 #//===----------------------------------------------------------------------===//
15 #// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
16 #// See https://llvm.org/LICENSE.txt for license information.
17 #// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
19 #//===----------------------------------------------------------------------===//
34 @vars = qw{ $host_arch $host_os $host_platform $target_arch $target_mic_arch $target_os $target_platform };
37 our $VERSION = "0.014";
39 our @EXPORT_OK = ( qw{ canon_arch canon_os canon_mic_arch legal_arch arch_opt
}, @vars );
40 our %EXPORT_TAGS = ( all
=> [ @EXPORT_OK ], vars
=> \
@vars );
42 # Canonize architecture name.
45 if ( defined( $arch ) ) {
46 if ( $arch =~ m{\A\s*(?:32|IA-?32|IA-?32 architecture|i[3456]86|x86)\s*\z}i ) {
48 } elsif ( $arch =~ m{\A\s*(?:48|(?:ia)?32e|Intel\s*64|Intel\(R\)\s*64|x86[_-]64|x64|AMD64)\s*\z}i ) {
50 } elsif ( $arch =~ m{\Aarm(?:v7\D*)?\z} ) {
52 } elsif ( $arch =~ m{\Appc64le} ) {
54 } elsif ( $arch =~ m{\Appc64} ) {
56 } elsif ( $arch =~ m{\Aaarch64} ) {
58 } elsif ( $arch =~ m{\Amic} ) {
60 } elsif ( $arch =~ m{\Amips64} ) {
62 } elsif ( $arch =~ m{\Amips} ) {
64 } elsif ( $arch =~ m{\Ariscv64} ) {
66 } elsif ( $arch =~ m{\Aloongarch64} ) {
67 $arch = "loongarch64";
75 # Canonize Intel(R) Many Integrated Core Architecture name.
76 sub canon_mic_arch
($) {
77 my ( $mic_arch ) = @_;
78 if ( defined( $mic_arch ) ) {
79 if ( $mic_arch =~ m{\Aknf} ) {
81 } elsif ( $mic_arch =~ m{\Aknc}) {
83 } elsif ( $mic_arch =~ m{\Aknl} ) {
90 }; # sub canon_mic_arch
92 { # Return legal approved architecture name.
94 "32" => "IA-32 architecture",
95 "32e" => "Intel(R) 64",
97 "aarch64" => "AArch64",
98 "loongarch64" => "LoongArch64",
99 "mic" => "Intel(R) Many Integrated Core Architecture",
101 "mips64" => "MIPS64",
102 "riscv64" => "RISC-V (64-bit)",
107 $arch = canon_arch
( $arch );
108 if ( defined( $arch ) ) {
109 $arch = $legal{ $arch };
115 { # Return architecture name suitable for Intel compiler setup scripts.
121 "aarch64" => "aarch",
124 "mips64" => "MIPS64",
129 $arch = canon_arch
( $arch );
130 if ( defined( $arch ) ) {
131 $arch = $option{ $arch };
140 if ( defined( $os ) ) {
141 if ( $os =~ m{\A\s*(?:Linux|lin|l)\s*\z}i ) {
143 } elsif ( $os =~ m{\A\s*(?:Mac(?:\s*OS(?:\s*X)?)?|mac|m|Darwin)\s*\z}i ) {
145 } elsif ( $os =~ m{\A\s*(?:Win(?:dows)?(?:(?:_|\s*)?(?:NT|XP|95|98|2003))?|w)\s*\z}i ) {
154 my ( $_host_os, $_host_arch, $_target_os, $_target_arch, $_target_mic_arch, $_default_mic_arch);
156 # Set the default mic-arch value.
157 $_default_mic_arch = "knc";
159 sub set_target_arch
($) {
160 my ( $arch ) = canon_arch
( $_[ 0 ] );
161 if ( defined( $arch ) ) {
162 $_target_arch = $arch;
163 $ENV{ LIBOMP_ARCH
} = $arch;
166 }; # sub set_target_arch
168 sub set_target_mic_arch
($) {
169 my ( $mic_arch ) = canon_mic_arch
( $_[ 0 ] );
170 if ( defined( $mic_arch ) ) {
171 $_target_mic_arch = $mic_arch;
172 $ENV{ LIBOMP_MIC_ARCH
} = $mic_arch;
175 }; # sub set_target_mic_arch
177 sub set_target_os
($) {
178 my ( $os ) = canon_os
( $_[ 0 ] );
179 if ( defined( $os ) ) {
181 $ENV{ LIBOMP_OS
} = $os;
184 }; # sub set_target_os
186 sub target_options
() {
190 set_target_os
( $_[ 1 ] ) or
191 die "Bad value of --target-os option: \"$_[ 1 ]\"\n";
193 "target-architecture|target-arch|architecture|arch=s" =>
195 set_target_arch
( $_[ 1 ] ) or
196 die "Bad value of --target-architecture option: \"$_[ 1 ]\"\n";
198 "target-mic-architecture|target-mic-arch|mic-architecture|mic-arch=s" =>
200 set_target_mic_arch
( $_[ 1 ] ) or
201 die "Bad value of --target-mic-architecture option: \"$_[ 1 ]\"\n";
205 }; # sub target_options
209 my $hardware_platform = Uname
::hardware_platform
();
211 } elsif ( $hardware_platform eq "i386" ) {
213 } elsif ( $hardware_platform eq "ia64" ) {
215 } elsif ( $hardware_platform eq "x86_64" ) {
217 } elsif ( $hardware_platform eq "arm" ) {
219 } elsif ( $hardware_platform eq "ppc64le" ) {
220 $_host_arch = "ppc64le";
221 } elsif ( $hardware_platform eq "ppc64" ) {
222 $_host_arch = "ppc64";
223 } elsif ( $hardware_platform eq "aarch64" ) {
224 $_host_arch = "aarch64";
225 } elsif ( $hardware_platform eq "mips64" ) {
226 $_host_arch = "mips64";
227 } elsif ( $hardware_platform eq "mips" ) {
228 $_host_arch = "mips";
229 } elsif ( $hardware_platform eq "riscv64" ) {
230 $_host_arch = "riscv64";
231 } elsif ( $hardware_platform eq "loongarch64" ) {
232 $_host_arch = "loongarch64";
234 die "Unsupported host hardware platform: \"$hardware_platform\"; stopped";
240 my $operating_system = Uname
::operating_system
();
242 } elsif ( $operating_system eq "GNU/Linux" ) {
244 } elsif ( $operating_system eq "FreeBSD" ) {
245 # Host OS resembles Linux.
247 } elsif ( $operating_system eq "NetBSD" ) {
248 # Host OS resembles Linux.
250 } elsif ( $operating_system eq "Darwin" ) {
252 } elsif ( $operating_system eq "MS Windows" ) {
255 die "Unsupported host operating system: \"$operating_system\"; stopped";
259 # Detect target arch.
260 if ( defined( $ENV{ LIBOMP_ARCH
} ) ) {
261 # Use arch specified in LIBOMP_ARCH.
262 $_target_arch = canon_arch
( $ENV{ LIBOMP_ARCH
} );
263 if ( not defined( $_target_arch ) ) {
264 die "Unknown architecture specified in LIBOMP_ARCH environment variable: \"$ENV{ LIBOMP_ARCH }\"";
267 # Otherwise use host architecture.
268 $_target_arch = $_host_arch;
270 $ENV{ LIBOMP_ARCH
} = $_target_arch;
272 # Detect target Intel(R) Many Integrated Core Architecture.
273 if ( defined( $ENV{ LIBOMP_MIC_ARCH
} ) ) {
274 # Use mic arch specified in LIBOMP_MIC_ARCH.
275 $_target_mic_arch = canon_mic_arch
( $ENV{ LIBOMP_MIC_ARCH
} );
276 if ( not defined( $_target_mic_arch ) ) {
277 die "Unknown architecture specified in LIBOMP_MIC_ARCH environment variable: \"$ENV{ LIBOMP_MIC_ARCH }\"";
280 # Otherwise use default Intel(R) Many Integrated Core Architecture.
281 $_target_mic_arch = $_default_mic_arch;
283 $ENV{ LIBOMP_MIC_ARCH
} = $_target_mic_arch;
286 if ( defined( $ENV{ LIBOMP_OS
} ) ) {
287 # Use OS specified in LIBOMP_OS.
288 $_target_os = canon_os
( $ENV{ LIBOMP_OS
} );
289 if ( not defined( $_target_os ) ) {
290 die "Unknown OS specified in LIBOMP_OS environment variable: \"$ENV{ LIBOMP_OS }\"";
293 # Otherwise use host OS.
294 $_target_os = $_host_os;
296 $ENV{ LIBOMP_OS
} = $_target_os;
300 tie
( $host_arch, "Platform::host_arch" );
301 tie
( $host_os, "Platform::host_os" );
302 tie
( $host_platform, "Platform::host_platform" );
303 tie
( $target_arch, "Platform::target_arch" );
304 tie
( $target_mic_arch, "Platform::target_mic_arch" );
305 tie
( $target_os, "Platform::target_os" );
306 tie
( $target_platform, "Platform::target_platform" );
308 { package Platform
::base
;
313 use base
"Tie::StdScalar";
316 my $self = shift( @_ );
317 croak
( "Modifying \$" . ref( $self ) . " is not allowed; stopped" );
320 } # package Platform::base
322 { package Platform
::host_arch
;
323 use base
"Platform::base";
327 } # package Platform::host_arch
329 { package Platform
::host_os
;
330 use base
"Platform::base";
334 } # package Platform::host_os
336 { package Platform
::host_platform
;
337 use base
"Platform::base";
339 return "${_host_os}_${_host_arch}";
341 } # package Platform::host_platform
343 { package Platform
::target_arch
;
344 use base
"Platform::base";
346 return $_target_arch;
348 } # package Platform::target_arch
350 { package Platform
::target_mic_arch
;
351 use base
"Platform::base";
353 return $_target_mic_arch;
355 } # package Platform::target_mic_arch
357 { package Platform
::target_os
;
358 use base
"Platform::base";
362 } # package Platform::target_os
364 { package Platform
::target_platform
;
365 use base
"Platform::base";
367 if ($_target_arch eq "mic") {
368 return "${_target_os}_${_target_mic_arch}";
370 return "${_target_os}_${_target_arch}";
373 } # package Platform::target_platform
384 B<Platform.pm> -- Few subroutines to get OS, architecture and platform name in form suitable for
385 naming files, directories, macros, etc.
392 my $arch = canon_arch( "em64T" ); # Returns "32e".
393 my $legal = legal_arch( "em64t" ); # Returns "Intel(R) 64".
394 my $option = arch_opt( "em64t" ); # Returns "intel64".
395 my $os = canon_os( "Windows NT" ); # Returns "win".
397 print( $host_arch, $host_os, $host_platform );
398 print( $target_arch, $target_os, $target_platform );
401 Platform::target_options(),
408 Environment variable LIBOMP_OS specifies target OS to report. If LIBOMP_OS id not defined,
409 the script assumes host OS is target OS.
411 Environment variable LIBOMP_ARCH specifies target architecture to report. If LIBOMP_ARCH is not defined,
412 the script assumes host architecture is target one.
418 =item B<canon_arch( $arch )>
420 Input string is an architecture name to canonize. The function recognizes many variants, for example:
421 C<32e>, C<Intel64>, C<Intel(R) 64>, etc. Returned string is a canonized architecture name,
422 one of: C<32>, C<32e>, C<64>, C<arm>, C<ppc64le>, C<ppc64>, C<mic>, C<mips>, C<mips64>, C<riscv64>, C<loongarch64> or C<undef> is input string is not recognized.
424 =item B<legal_arch( $arch )>
426 Input string is architecture name. The function recognizes the same variants as C<arch_canon()> does.
427 Returned string is a name approved by Intel Legal, one of: C<IA-32 architecture>, C<Intel(R) 64>
428 or C<undef> if input string is not recognized.
430 =item B<arch_opt( $arch )>
432 Input string is architecture name. The function recognizes the same variants as C<arch_canon()> does.
433 Returned string is an architecture name suitable for passing to compiler setup scripts
434 (e. g. C<iccvars.sh>), one of: C<IA-32 architecture>, C<Intel(R) 64> or C<undef> if input string is not
437 =item B<canon_os( $os )>
439 Input 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>,
440 C<mac>, C<win>, or C<undef> is input string is not recognized.
442 =item B<target_options()>
444 Returns array suitable for passing to C<tools::get_options()> to let a script recognize
445 C<--target-architecture=I<str>> and C<--target-os=I<str>> options. Typical usage is:
450 my ( $os, $arch, $platform ); # Global variables, not initialized.
455 Platform::target_options(), # Let script recognize --target-os and --target-arch options.
458 # Initialize variables after parsing command line.
459 ( $os, $arch, $platform ) = ( Platform::target_os(), Platform::target_arch(), Platform::target_platform() );
467 Canonized name of host architecture.
471 Canonized name of host OS.
473 =item B<$host_platform>
475 Host platform name (concatenated canonized OS name, underscore, and canonized architecture name).
477 =item B<$target_arch>
479 Canonized name of target architecture.
483 Canonized name of target OS.
485 =item B<$target_platform>
487 Target platform name (concatenated canonized OS name, underscore, and canonized architecture name).