Skip to content

Commit f9121fc

Browse files
committed
Add missing hipvars.pm
1 parent 41957ea commit f9121fc

File tree

1 file changed

+158
-0
lines changed

1 file changed

+158
-0
lines changed

amd/hipcc/bin/hipvars.pm

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
#!/usr/bin/env perl
2+
# Copyright (c) 2020 - 2021 Advanced Micro Devices, Inc. All rights reserved.
3+
#
4+
# Permission is hereby granted, free of charge, to any person obtaining a copy
5+
# of this software and associated documentation files (the "Software"), to deal
6+
# in the Software without restriction, including without limitation the rights
7+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
# copies of the Software, and to permit persons to whom the Software is
9+
# furnished to do so, subject to the following conditions:
10+
#
11+
# The above copyright notice and this permission notice shall be included in
12+
# all copies or substantial portions of the Software.
13+
#
14+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+
# THE SOFTWARE.
21+
22+
package hipvars;
23+
use warnings;
24+
use Getopt::Long;
25+
use Cwd;
26+
use File::Basename;
27+
28+
$HIP_BASE_VERSION_MAJOR = "5";
29+
$HIP_BASE_VERSION_MINOR = "5";
30+
$HIP_BASE_VERSION_PATCH = "0";
31+
32+
#---
33+
# Function to parse config file
34+
sub parse_config_file {
35+
my ($file, $config) = @_;
36+
if (open (CONFIG, "$file")) {
37+
while (<CONFIG>) {
38+
my $config_line=$_;
39+
chop ($config_line);
40+
$config_line =~ s/^\s*//;
41+
$config_line =~ s/\s*$//;
42+
if (($config_line !~ /^#/) && ($config_line ne "")) {
43+
my ($name, $value) = split (/=/, $config_line);
44+
$$config{$name} = $value;
45+
}
46+
}
47+
close(CONFIG);
48+
}
49+
}
50+
51+
#---
52+
# Function to check if executable can be run
53+
sub can_run {
54+
my ($exe) = @_;
55+
`$exe --version 2>&1`;
56+
if ($? == 0) {
57+
return 1;
58+
} else {
59+
return 0;
60+
}
61+
}
62+
63+
$isWindows = ($^O eq 'MSWin32' or $^O eq 'msys');
64+
65+
#
66+
# TODO: Fix rpath LDFLAGS settings
67+
#
68+
# Since this hipcc script gets installed at two uneven hierarchical levels,
69+
# linked by symlink, the absolute path of this script should be used to
70+
# derive HIP_PATH, as dirname $0 could be /opt/rocm/bin or /opt/rocm/hip/bin
71+
# depending on how it gets invoked.
72+
# ROCM_PATH which points to <rocm_install_dir> is determined based on whether
73+
# we find bin/rocm_agent_enumerator in the parent of HIP_PATH or not. If it is found,
74+
# ROCM_PATH is defined relative to HIP_PATH else it is hardcoded to /opt/rocm.
75+
#
76+
$HIP_PATH=$ENV{'HIP_PATH'} // dirname(Cwd::abs_path("$0/../")); # use parent directory of hipcc
77+
if (-e "$HIP_PATH/bin/rocm_agent_enumerator") {
78+
$ROCM_PATH=$ENV{'ROCM_PATH'} // "$HIP_PATH"; # use HIP_PATH
79+
}elsif (-e "$HIP_PATH/../bin/rocm_agent_enumerator") { # case for backward compatibility
80+
$ROCM_PATH=$ENV{'ROCM_PATH'} // dirname("$HIP_PATH"); # use parent directory of HIP_PATH
81+
} else {
82+
$ROCM_PATH=$ENV{'ROCM_PATH'} // "/opt/rocm";
83+
}
84+
$CUDA_PATH=$ENV{'CUDA_PATH'} // '/usr/local/cuda';
85+
86+
# Windows has a different structure, all binaries are inside hip/bin
87+
if ($isWindows) {
88+
$HIP_CLANG_PATH=$ENV{'HIP_CLANG_PATH'} // "$HIP_PATH/bin";
89+
} else {
90+
$HIP_CLANG_PATH=$ENV{'HIP_CLANG_PATH'} // "$ROCM_PATH/llvm/bin";
91+
}
92+
# HIP_ROCCLR_HOME is used by Windows builds
93+
$HIP_ROCCLR_HOME=$ENV{'HIP_ROCCLR_HOME'};
94+
95+
if (defined $HIP_ROCCLR_HOME) {
96+
$HIP_INFO_PATH= "$HIP_ROCCLR_HOME/lib/.hipInfo";
97+
} else {
98+
$HIP_INFO_PATH= "$HIP_PATH/lib/.hipInfo"; # use actual file
99+
}
100+
#---
101+
#HIP_PLATFORM controls whether to use nvidia or amd platform:
102+
$HIP_PLATFORM=$ENV{'HIP_PLATFORM'};
103+
# Read .hipInfo
104+
my %hipInfo = ();
105+
parse_config_file("$HIP_INFO_PATH", \%hipInfo);
106+
# Prioritize Env first, otherwise use the hipInfo config file
107+
$HIP_COMPILER = $ENV{'HIP_COMPILER'} // $hipInfo{'HIP_COMPILER'} // "clang";
108+
$HIP_RUNTIME = $ENV{'HIP_RUNTIME'} // $hipInfo{'HIP_RUNTIME'} // "rocclr";
109+
110+
# If using ROCclr runtime, need to find HIP_ROCCLR_HOME
111+
if (defined $HIP_RUNTIME and $HIP_RUNTIME eq "rocclr" and !defined $HIP_ROCCLR_HOME) {
112+
my $hipvars_dir = dirname(Cwd::abs_path($0));
113+
if (-e "$hipvars_dir/../lib/bitcode") {
114+
$HIP_ROCCLR_HOME = Cwd::abs_path($hipvars_dir . "/.."); #FILE_REORG Backward compatibility
115+
} elsif (-e "$hipvars_dir/lib/bitcode") {
116+
$HIP_ROCCLR_HOME = Cwd::abs_path($hipvars_dir);
117+
} else {
118+
$HIP_ROCCLR_HOME = $HIP_PATH; # use HIP_PATH
119+
}
120+
}
121+
122+
if (not defined $HIP_PLATFORM) {
123+
if (can_run("$HIP_CLANG_PATH/clang++") or can_run("clang++")) {
124+
$HIP_PLATFORM = "amd";
125+
} elsif (can_run("$CUDA_PATH/bin/nvcc") or can_run("nvcc")) {
126+
$HIP_PLATFORM = "nvidia";
127+
$HIP_COMPILER = "nvcc";
128+
$HIP_RUNTIME = "cuda";
129+
} else {
130+
# Default to amd for now
131+
$HIP_PLATFORM = "amd";
132+
}
133+
} elsif ($HIP_PLATFORM eq "hcc") {
134+
$HIP_PLATFORM = "amd";
135+
warn("Warning: HIP_PLATFORM=hcc is deprecated. Please use HIP_PLATFORM=amd. \n")
136+
} elsif ($HIP_PLATFORM eq "nvcc") {
137+
$HIP_PLATFORM = "nvidia";
138+
$HIP_COMPILER = "nvcc";
139+
$HIP_RUNTIME = "cuda";
140+
warn("Warning: HIP_PLATFORM=nvcc is deprecated. Please use HIP_PLATFORM=nvidia. \n")
141+
}
142+
143+
if ($HIP_COMPILER eq "clang") {
144+
# Windows does not have clang at linux default path
145+
if (defined $HIP_ROCCLR_HOME and (-e "$HIP_ROCCLR_HOME/bin/clang" or -e "$HIP_ROCCLR_HOME/bin/clang.exe")) {
146+
$HIP_CLANG_PATH = "$HIP_ROCCLR_HOME/bin";
147+
}
148+
}
149+
150+
#---
151+
# Read .hipVersion
152+
my %hipVersion = ();
153+
parse_config_file("$hipvars::HIP_PATH/bin/.hipVersion", \%hipVersion);
154+
$HIP_VERSION_MAJOR = $hipVersion{'HIP_VERSION_MAJOR'} // $HIP_BASE_VERSION_MAJOR;
155+
$HIP_VERSION_MINOR = $hipVersion{'HIP_VERSION_MINOR'} // $HIP_BASE_VERSION_MINOR;
156+
$HIP_VERSION_PATCH = $hipVersion{'HIP_VERSION_PATCH'} // $HIP_BASE_VERSION_PATCH;
157+
$HIP_VERSION_GITHASH = $hipVersion{'HIP_VERSION_GITHASH'} // 0;
158+
$HIP_VERSION="$HIP_VERSION_MAJOR.$HIP_VERSION_MINOR.$HIP_VERSION_PATCH-$HIP_VERSION_GITHASH";

0 commit comments

Comments
 (0)