22#ifndef SRC_HIPBIN_BASE_H_
23#define SRC_HIPBIN_BASE_H_
26#include "hipBin_util.h"
33# define HIP_ROCCLR_HOME "HIP_ROCCLR_HOME"
34# define HIP_PATH "HIP_PATH"
35# define ROCM_PATH "ROCM_PATH"
36# define CUDA_PATH "CUDA_PATH"
37# define HSA_PATH "HSA_PATH"
38# define HIP_CLANG_PATH "HIP_CLANG_PATH"
39# define HIP_PLATFORM "HIP_PLATFORM"
40# define HIP_COMPILER "HIP_COMPILER"
41# define HIP_RUNTIME "HIP_RUNTIME"
42# define LD_LIBRARY_PATH "LD_LIBRARY_PATH"
45# define HIPCC_COMPILE_FLAGS_APPEND "HIPCC_COMPILE_FLAGS_APPEND"
46# define HIPCC_LINK_FLAGS_APPEND "HIPCC_LINK_FLAGS_APPEND"
47# define HIP_LIB_PATH "HIP_LIB_PATH"
48# define DEVICE_LIB_PATH "DEVICE_LIB_PATH"
49# define HIP_CLANG_HCC_COMPAT_MODE "HIP_CLANG_HCC_COMPAT_MODE"
50# define HIP_COMPILE_CXX_AS_HIP "HIP_COMPILE_CXX_AS_HIP"
51# define HIPCC_VERBOSE "HIPCC_VERBOSE"
52# define HCC_AMDGPU_TARGET "HCC_AMDGPU_TARGET"
54# define HIP_BASE_VERSION_MAJOR "4"
55# define HIP_BASE_VERSION_MINOR "4"
56# define HIP_BASE_VERSION_PATCH "0"
57# define HIP_BASE_VERSION_GITHASH "0"
66string PlatformTypeStr(PlatformType platform) {
74 return "invalid platform";
85string CompilerTypeStr(CompilerType compiler) {
93 return "invalid CompilerType";
104string RuntimeTypeStr(RuntimeType runtime) {
112 return "invalid RuntimeType";
122string OsTypeStr(OsType os) {
130 return "invalid OsType";
135 PlatformType platform;
136 CompilerType compiler;
143 string hipPathEnv_ =
"";
144 string hipRocclrPathEnv_ =
"";
145 string roccmPathEnv_ =
"";
146 string cudaPathEnv_ =
"";
147 string hsaPathEnv_ =
"";
148 string hipClangPathEnv_ =
"";
149 string hipPlatformEnv_ =
"";
150 string hipCompilerEnv_ =
"";
151 string hipRuntimeEnv_ =
"";
152 string ldLibraryPathEnv_ =
"";
153 string verboseEnv_ =
"";
154 string hipccCompileFlagsAppendEnv_ =
"";
155 string hipccLinkFlagsAppendEnv_ =
"";
156 string hipLibPathEnv_ =
"";
157 string deviceLibPathEnv_ =
"";
158 string hipClangHccCompactModeEnv_ =
"";
159 string hipCompileCxxAsHipEnv_ =
"";
160 string hccAmdGpuTargetEnv_ =
"";
161 friend std::ostream& operator <<(std::ostream& os,
const EnvVariables& var) {
162 os <<
"Path: " << var.path_ << endl;
163 os <<
"Hip Path: " << var.hipPathEnv_ << endl;
164 os <<
"Hip Rocclr Path: " << var.hipRocclrPathEnv_ << endl;
165 os <<
"Roccm Path: " << var.roccmPathEnv_ << endl;
166 os <<
"Cuda Path: " << var.cudaPathEnv_ << endl;
167 os <<
"Hsa Path: " << var.hsaPathEnv_ << endl;
168 os <<
"Hip Clang Path: " << var.hipClangPathEnv_ << endl;
169 os <<
"Hip Platform: " << var.hipPlatformEnv_ << endl;
170 os <<
"Hip Compiler: " << var.hipCompilerEnv_ << endl;
171 os <<
"Hip Runtime: " << var.hipRuntimeEnv_ << endl;
172 os <<
"LD Library Path: " << var.ldLibraryPathEnv_ << endl;
173 os <<
"Verbose: " << var.verboseEnv_ << endl;
174 os <<
"Hipcc Compile Flags Append: " <<
175 var.hipccCompileFlagsAppendEnv_ << endl;
176 os <<
"Hipcc Link Flags Append: " <<
177 var.hipccLinkFlagsAppendEnv_ << endl;
178 os <<
"Hip lib Path: " << var.hipLibPathEnv_ << endl;
179 os <<
"Device lib Path: " << var.deviceLibPathEnv_ << endl;
180 os <<
"Hip Clang HCC Compact mode: " <<
181 var.hipClangHccCompactModeEnv_ << endl;
182 os <<
"Hip Compile Cxx as Hip: " <<
183 var.hipCompileCxxAsHipEnv_ << endl;
184 os <<
"Hcc Amd Gpu Target: " << var.hccAmdGpuTargetEnv_ << endl;
211 virtual void constructCompilerPath() = 0;
212 virtual void printFull() = 0;
213 virtual bool detectPlatform() = 0;
214 virtual const string& getCompilerPath()
const = 0;
215 virtual void printCompilerInfo()
const = 0;
216 virtual string getCompilerVersion() = 0;
217 virtual const PlatformInfo& getPlatformInfo()
const = 0;
218 virtual string getCppConfig() = 0;
219 virtual void checkHipconfig() = 0;
220 virtual string getDeviceLibPath()
const = 0;
221 virtual string getHipLibPath()
const = 0;
222 virtual string getHipCC()
const = 0;
223 virtual string getHipInclude()
const = 0;
224 virtual void initializeHipCXXFlags() = 0;
225 virtual void initializeHipCFlags() = 0;
226 virtual void initializeHipLdFlags() = 0;
227 virtual const string& getHipCXXFlags()
const = 0;
228 virtual const string& getHipCFlags()
const = 0;
229 virtual const string& getHipLdFlags()
const = 0;
230 virtual void executeHipCCCmd(vector<string> argv) = 0;
232 void getSystemInfo()
const;
233 void printEnvironmentVariables()
const;
235 const OsType& getOSInfo()
const;
236 const string& getHipPath()
const;
237 const string& getRoccmPath()
const;
238 const string& getHipVersion()
const;
239 void printUsage()
const;
240 bool canRunCompiler(
string exeName,
string& cmdOut);
241 HipBinCommand gethipconfigCmd(
string argument);
242 const string& getrocm_pathOption()
const;
248 string rocm_pathOption_ =
"";
250 void readEnvVariables();
251 void constructHipPath();
252 void constructRoccmPath();
253 void readHipVersion();
262HipBinBase::HipBinBase() {
263 hipBinUtilPtr_ = hipBinUtilPtr_->getInstance();
269void HipBinBase::readOSInfo() {
270#if defined _WIN32 || defined _WIN64
272#elif defined __unix || defined __linux__
279void HipBinBase::readEnvVariables() {
280 if (
const char* path = std::getenv(PATH))
281 envVariables_.path_ = path;
282 if (
const char* hip = std::getenv(HIP_PATH))
283 envVariables_.hipPathEnv_ = hip;
284 if (
const char* hip_rocclr = std::getenv(HIP_ROCCLR_HOME))
285 envVariables_.hipRocclrPathEnv_ = hip_rocclr;
286 if (
const char* roccm = std::getenv(ROCM_PATH))
287 envVariables_.roccmPathEnv_ = roccm;
288 if (
const char* cuda = std::getenv(CUDA_PATH))
289 envVariables_.cudaPathEnv_ = cuda;
290 if (
const char* hsa = std::getenv(HSA_PATH))
291 envVariables_.hsaPathEnv_ = hsa;
292 if (
const char* hipClang = std::getenv(HIP_CLANG_PATH))
293 envVariables_.hipClangPathEnv_ = hipClang;
294 if (
const char* hipPlatform = std::getenv(HIP_PLATFORM))
295 envVariables_.hipPlatformEnv_ = hipPlatform;
296 if (
const char* hipCompiler = std::getenv(HIP_COMPILER))
297 envVariables_.hipCompilerEnv_ = hipCompiler;
298 if (
const char* hipRuntime = std::getenv(HIP_RUNTIME))
299 envVariables_.hipRuntimeEnv_ = hipRuntime;
300 if (
const char* ldLibaryPath = std::getenv(LD_LIBRARY_PATH))
301 envVariables_.ldLibraryPathEnv_ = ldLibaryPath;
302 if (
const char* hccAmdGpuTarget = std::getenv(HCC_AMDGPU_TARGET))
303 envVariables_.hccAmdGpuTargetEnv_ = hccAmdGpuTarget;
304 if (
const char* verbose = std::getenv(HIPCC_VERBOSE))
305 envVariables_.verboseEnv_ = verbose;
306 if (
const char* hipccCompileFlagsAppend =
307 std::getenv(HIPCC_COMPILE_FLAGS_APPEND))
308 envVariables_.hipccCompileFlagsAppendEnv_ = hipccCompileFlagsAppend;
309 if (
const char* hipccLinkFlagsAppend = std::getenv(HIPCC_LINK_FLAGS_APPEND))
310 envVariables_.hipccLinkFlagsAppendEnv_ = hipccLinkFlagsAppend;
311 if (
const char* hipLibPath = std::getenv(HIP_LIB_PATH))
312 envVariables_.hipLibPathEnv_ = hipLibPath;
313 if (
const char* deviceLibPath = std::getenv(DEVICE_LIB_PATH))
314 envVariables_.deviceLibPathEnv_ = deviceLibPath;
315 if (
const char* hipClangHccCompactMode =
316 std::getenv(HIP_CLANG_HCC_COMPAT_MODE))
317 envVariables_.hipClangHccCompactModeEnv_ = hipClangHccCompactMode;
318 if (
const char* hipCompileCxxAsHip = std::getenv(HIP_COMPILE_CXX_AS_HIP))
319 envVariables_.hipCompileCxxAsHipEnv_ = hipCompileCxxAsHip;
323void HipBinBase::constructHipPath() {
324 fs::path full_path(hipBinUtilPtr_->getSelfPath());
325 if (envVariables_.hipPathEnv_.empty())
326 variables_.hipPathEnv_ = (full_path.parent_path()).
string();
328 variables_.hipPathEnv_ = envVariables_.hipPathEnv_;
333void HipBinBase::constructRoccmPath() {
335 string rocm_path_name = getrocm_pathOption();
338 if (!rocm_path_name.empty())
339 variables_.roccmPathEnv_ = rocm_path_name;
340 else if (envVariables_.roccmPathEnv_.empty()) {
341 const string& hipPath = getHipPath();
342 fs::path roccm_path(hipPath);
343 roccm_path = roccm_path.parent_path();
344 fs::path rocm_agent_enumerator_file(roccm_path);
345 rocm_agent_enumerator_file /=
"bin/rocm_agent_enumerator";
346 if (!fs::exists(rocm_agent_enumerator_file)) {
347 roccm_path =
"/opt/rocm";
350 variables_.roccmPathEnv_ = envVariables_.roccmPathEnv_;}
354void HipBinBase::readHipVersion() {
356 const string& hipPath = getHipPath();
357 fs::path hipVersionPath = hipPath;
358 hipVersionPath /=
"bin/.hipVersion";
359 map<string, string> hipVersionMap;
360 hipVersionMap = hipBinUtilPtr_->parseConfigFile(hipVersionPath);
361 string hip_version_major, hip_version_minor,
362 hip_version_patch, hip_version_githash;
363 hip_version_major = hipBinUtilPtr_->readConfigMap(
364 hipVersionMap,
"HIP_VERSION_MAJOR",
365 HIP_BASE_VERSION_MAJOR);
366 hip_version_minor = hipBinUtilPtr_->readConfigMap(
367 hipVersionMap,
"HIP_VERSION_MINOR",
368 HIP_BASE_VERSION_MINOR);
369 hip_version_patch = hipBinUtilPtr_->readConfigMap(
370 hipVersionMap,
"HIP_VERSION_PATCH",
371 HIP_BASE_VERSION_PATCH);
372 hip_version_githash = hipBinUtilPtr_->readConfigMap(
373 hipVersionMap,
"HIP_VERSION_GITHASH",
374 HIP_BASE_VERSION_GITHASH);
375 hipVersion = hip_version_major +
"." + hip_version_minor +
376 "." + hip_version_patch +
"-" + hip_version_githash;
377 hipVersion_ = hipVersion;
381void HipBinBase::getSystemInfo()
const {
382 const OsType& os = getOSInfo();
384 cout << endl <<
"== Windows Display Drivers" << endl;
385 cout <<
"Hostname :";
387 system(
"wmic path win32_VideoController get AdapterCompatibility,"
388 "InstalledDisplayDrivers,Name | findstr /B /C:\"Advanced Micro Devices\"");
391 cout << endl <<
"== Linux Kernel" << endl;
392 cout <<
"Hostname :" << endl;
399void HipBinBase::printEnvironmentVariables()
const {
400 const OsType& os = getOSInfo();
402 cout <<
"PATH=" << envVariables_.path_ <<
"\n" << endl;
403 system(
"set | findstr"
404 " /B /C:\"HIP\" /C:\"HSA\" /C:\"CUDA\" /C:\"LD_LIBRARY_PATH\"");
406 string cmd =
"echo PATH =";
407 cmd += envVariables_.path_;
409 system(
"env | egrep '^HIP|^HSA|^CUDA|^LD_LIBRARY_PATH'");
414const EnvVariables& HipBinBase::getEnvVariables()
const {
415 return envVariables_;
420const OsType& HipBinBase::getOSInfo()
const {
425const string& HipBinBase::getHipPath()
const {
426 return variables_.hipPathEnv_;
430const string& HipBinBase::getRoccmPath()
const {
431 return variables_.roccmPathEnv_;
435const string& HipBinBase::getHipVersion()
const {
440void HipBinBase::printUsage()
const {
441 cout <<
"usage: hipconfig [OPTIONS]\n";
442 cout <<
" --path, -p :"
443 " print HIP_PATH (use env var if set, else determine from hipconfig path)\n";
444 cout <<
" --rocmpath, -R :"
445 " print ROCM_PATH (use env var if set,"
446 " else determine from hip path or /opt/rocm)\n";
447 cout <<
" --cpp_config, -C : print C++ compiler options\n";
448 cout <<
" --compiler, -c : print compiler (clang or nvcc)\n";
449 cout <<
" --platform, -P : print platform (amd or nvidia)\n";
450 cout <<
" --runtime, -r : print runtime (rocclr or cuda)\n";
451 cout <<
" --hipclangpath, -l : print HIP_CLANG_PATH\n";
452 cout <<
" --full, -f : print full config\n";
453 cout <<
" --version, -v : print hip version\n";
454 cout <<
" --check : check configuration\n";
455 cout <<
" --newline, -n : print newline\n";
456 cout <<
" --help, -h : print help message\n";
462bool HipBinBase::canRunCompiler(
string exeName,
string& cmdOut) {
463 string complierName = exeName;
464 string temp_dir = hipBinUtilPtr_->getTempDir();
465 fs::path templateFs = temp_dir;
466 templateFs /=
"canRunXXXXXX";
467 string tmpFileName = hipBinUtilPtr_->mktempFile(templateFs.string());
468 complierName +=
" --version > " + tmpFileName +
" 2>&1";
469 bool executable =
false;
470 if (system(
const_cast<char*
>(complierName.c_str()))) {
475 fp.open(tmpFileName);
477 while (std::getline(fp, myline)) {
487HipBinCommand HipBinBase::gethipconfigCmd(
string argument) {
488 vector<string> pathStrs = {
"-p",
"--path",
"-path",
"--p" };
489 if (hipBinUtilPtr_->checkCmd(pathStrs, argument))
491 vector<string> rocmPathStrs = {
"-R",
"--rocmpath",
"-rocmpath",
"--R" };
492 if (hipBinUtilPtr_->checkCmd(rocmPathStrs, argument))
494 vector<string> cppConfigStrs = {
"-C",
"--cpp_config",
495 "-cpp_config",
"--C", };
496 if (hipBinUtilPtr_->checkCmd(cppConfigStrs, argument))
498 vector<string> CompilerStrs = {
"-c",
"--compiler",
"-compiler",
"--c" };
499 if (hipBinUtilPtr_->checkCmd(CompilerStrs, argument))
501 vector<string> platformStrs = {
"-P",
"--platform",
"-platform",
"--P" };
502 if (hipBinUtilPtr_->checkCmd(platformStrs, argument))
504 vector<string> runtimeStrs = {
"-r",
"--runtime",
"-runtime",
"--r" };
505 if (hipBinUtilPtr_->checkCmd(runtimeStrs, argument))
507 vector<string> hipClangPathStrs = {
"-l",
"--hipclangpath",
508 "-hipclangpath",
"--l" };
509 if (hipBinUtilPtr_->checkCmd(hipClangPathStrs, argument))
511 vector<string> fullStrs = {
"-f",
"--full",
"-full",
"--f" };
512 if (hipBinUtilPtr_->checkCmd(fullStrs, argument))
514 vector<string> versionStrs = {
"-v",
"--version",
"-version",
"--v" };
515 if (hipBinUtilPtr_->checkCmd(versionStrs, argument))
517 vector<string> checkStrs = {
"--check",
"-check" };
518 if (hipBinUtilPtr_->checkCmd(checkStrs, argument))
520 vector<string> newlineStrs = {
"--n",
"-n",
"--newline",
"-newline" };
521 if (hipBinUtilPtr_->checkCmd(newlineStrs, argument))
523 vector<string> helpStrs = {
"-h",
"--help",
"-help",
"--h" };
524 if (hipBinUtilPtr_->checkCmd(helpStrs, argument))
529const string& HipBinBase::getrocm_pathOption()
const {
530 return rocm_pathOption_;
Definition: hipBin_base.h:207
Definition: hipBin_util.h:144
Definition: hipBin_base.h:141