1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
|
////////////////////////////////////////////////////////////////////////////////
//
// MIT License
//
// Copyright (c) 2017 - 2024 Advanced Micro Devices, Inc. All rights Reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
//
////////////////////////////////////////////////////////////////////////////////
#include <string.h>
#include <stdlib.h>
#include <limits.h> /* PATH_MAX */
#include <stdio.h>
#include <link.h>
#include <dlfcn.h>
#include "rocm_getpath.h"
/* Macro for NULL CHECK */
#define NULL_CHECK(ptr) if(!ptr) return PathIncorrecPararmeters;
/* Target Library Install Dir */
#define TARGET_LIB_INSTALL_DIR TARGET_LIBRARY_INSTALL_DIR
/* Target Library Name Buf Size */
#define LIBRARY_FILENAME_BUFSZ (PATH_MAX+1)
/* Internal Function to get Base Path - Ref from Icarus Logic*/
static int getROCmBase(char *buf);
/* Public Function to get the ROCm Install Base Path
// Argument1 (out) : InstallPath (char** pointer which will return InstallPath found)
// Argument2 (out) : InstallPathLen (Pointer to integer (size of InstallPath) returned)
// Usage :
// char *installPath=NULL;
// int installPathLen = 0;
// installStatus = getROCmInstallPath( &installPath, &installPathLen );
// if(installStatus !=PathSuccess ){ // error occured
// ...
// }
// free(installPath); //caller must free allocated memory after usage.
// ...
*/
PathErrors_t getROCmInstallPath( char** InstallPath, unsigned int *InstallPathLen ) {
NULL_CHECK(InstallPath);
NULL_CHECK(InstallPathLen);
int ret = PathErrorMAX;
char *bufPtr = (char *)NULL;
unsigned int bufSz = 0;
bufPtr = (char *)malloc( LIBRARY_FILENAME_BUFSZ * sizeof(char) );
memset( bufPtr, 0, LIBRARY_FILENAME_BUFSZ );
*InstallPathLen = 0;
*InstallPath = NULL;
ret = getROCmBase(bufPtr);
if (0 > ret){
free(bufPtr);
return (PathErrors_t)ret;
}
else if (0 == ret){
free(bufPtr);
return PathFailedToGetBase;
}
else{
bufSz = ret;//additional char for null termination
}
*InstallPath = bufPtr;
*InstallPathLen = bufSz;
return PathSuccess;
}
/* General purpose function that fills the directory to find rocm related stuff */
/* returns the offset into the buffer for the terminating NUL or -1 for error */
/* The buffer should be at least PATH_MAX */
static int getROCmBase(char *buf)
{
int len=0;
char *envStr=NULL;
char libFileName[LIBRARY_FILENAME_BUFSZ];
char *end=NULL;
// Check Environment Variable is set for ROCM
// install base path, then use it directly.
if ((envStr = getenv("ROCM_PATH"))) {
/* User space override, essentially just copied through as long as it is not too long */
len = strlen(envStr);
if (len > 0) {
if (envStr[len] == '/') {
/* Already has at least one terminating */
len--;
}
if (len > PATH_MAX-1 ) {
return PathValuesTooLong;
}
strncpy(buf, envStr, len);
buf[len]='/';
buf[len+1]='\0';
/* Length of string including trailing '/' */
return len+1;
}
}
// If Environment Variable is not set
// use dl APIs to get target lib path
// and get rocm base install path using the lib Path.
#if BUILD_SHARED_LIBS
sprintf(libFileName, "lib%s.so", TARGET_LIBRARY_NAME);
void *handle=dlopen(libFileName,RTLD_NOW);
if (!handle){
/* We can't find the library */
return PathLinuxRuntimeErrors;
}
/* Variable to hold the return value from dlinfo */
struct link_map *map = (struct link_map*)NULL;
/* Query the runtime linker */
dlinfo(handle,RTLD_DI_LINKMAP,&map);
if (map ->l_name && realpath(map ->l_name,buf)) {
/* Get Library Directory Path */
char *end = strrchr(buf, '/');
if (end && end > buf) {
*end = '\0';
}
}
else{
/* If l_name is NULL or realpath() failed
* Close handle before return error */
dlclose(handle);
return PathLinuxRuntimeErrors;
}
dlclose(handle);
/* find the start of substring TARGET_LIB_INSTALL_DIR
* To strip down Path up to Parent Directory of TARGET_LIB_INSTALL_DIR. */
end=strstr(buf, TARGET_LIB_INSTALL_DIR);
if( NULL == end ){
/* We can't find the library install directory*/
return PathLinuxRuntimeErrors;
}
*end = '\0';
#endif
/* Length of Path String up to Parent Directoy (ROCm Base Path)
* with trailing '/'.*/
len = strlen(buf);
return len;
}
|