| 116 | | |
|---|
| 117 | | |
|---|
| | 123 | #else |
|---|
| | 124 | /** |
|---|
| | 125 | * Non-Windows shared library constructor. This ensures that the environment |
|---|
| | 126 | * variable \c VPR_BASE_DIR is set as soon as this shared library is loaded. |
|---|
| | 127 | * If it is not set, then it sets it based on an assumption about the |
|---|
| | 128 | * structure of a VPR installation. More specifically, an assumption is made |
|---|
| | 129 | * that this shared library lives in the \c lib subdirectory of the VPR |
|---|
| | 130 | * installation. Therefore, the root of the VPR installation is the parent of |
|---|
| | 131 | * the directory containing this shared library. |
|---|
| | 132 | */ |
|---|
| | 133 | extern "C" void __attribute ((constructor)) vprLibraryInit() |
|---|
| | 134 | { |
|---|
| | 135 | const char* env_dir = std::getenv("VPR_BASE_DIR"); |
|---|
| | 136 | |
|---|
| | 137 | if ( NULL == env_dir ) |
|---|
| | 138 | { |
|---|
| | 139 | Dl_info info; |
|---|
| | 140 | info.dli_fname = 0; |
|---|
| | 141 | const int result = |
|---|
| | 142 | #if defined(__GNUC__) && __GNUC_MAJOR__ < 4 |
|---|
| | 143 | dladdr((void*) &vprLibraryInit, &info); |
|---|
| | 144 | #else |
|---|
| | 145 | dladdr(static_cast<void*>(&vprLibraryInit), &info); |
|---|
| | 146 | #endif |
|---|
| | 147 | |
|---|
| | 148 | // NOTE: dladdr(3) really does return a non-zero value on success. |
|---|
| | 149 | if ( 0 != result ) |
|---|
| | 150 | { |
|---|
| | 151 | try |
|---|
| | 152 | { |
|---|
| | 153 | fs::path lib_file(info.dli_fname, fs::native); |
|---|
| | 154 | lib_file = fs::system_complete(lib_file); |
|---|
| | 155 | |
|---|
| | 156 | #if defined(VPR_OS_IRIX) && defined(_ABIN32) |
|---|
| | 157 | const std::string bit_suffix("32"); |
|---|
| | 158 | #elif defined(VPR_OS_IRIX) && defined(_ABI64) || \ |
|---|
| | 159 | defined(VPR_OS_Linux) && defined(__x86_64__) |
|---|
| | 160 | const std::string bit_suffix("64"); |
|---|
| | 161 | #else |
|---|
| | 162 | const std::string bit_suffix(""); |
|---|
| | 163 | #endif |
|---|
| | 164 | |
|---|
| | 165 | // Get the directory containing this shared library. |
|---|
| | 166 | const fs::path lib_path = lib_file.branch_path(); |
|---|
| | 167 | |
|---|
| | 168 | // Start the search for the root of the VPR installation in the |
|---|
| | 169 | // parent of the directory containing this shared library. |
|---|
| | 170 | fs::path base_dir = lib_path.branch_path(); |
|---|
| | 171 | |
|---|
| | 172 | // Use the lib subdirectory to figure out when we have found the |
|---|
| | 173 | // root of the VPR installation tree. |
|---|
| | 174 | const fs::path lib_subdir(std::string("lib") + bit_suffix); |
|---|
| | 175 | |
|---|
| | 176 | bool found(false); |
|---|
| | 177 | while ( ! found && ! base_dir.empty() ) |
|---|
| | 178 | { |
|---|
| | 179 | try |
|---|
| | 180 | { |
|---|
| | 181 | if ( ! fs::exists(base_dir / lib_subdir) ) |
|---|
| | 182 | { |
|---|
| | 183 | base_dir = base_dir.branch_path(); |
|---|
| | 184 | } |
|---|
| | 185 | else |
|---|
| | 186 | { |
|---|
| | 187 | found = true; |
|---|
| | 188 | } |
|---|
| | 189 | } |
|---|
| | 190 | catch (fs::filesystem_error&) |
|---|
| | 191 | { |
|---|
| | 192 | base_dir = base_dir.branch_path(); |
|---|
| | 193 | } |
|---|
| | 194 | } |
|---|
| | 195 | |
|---|
| | 196 | if ( found ) |
|---|
| | 197 | { |
|---|
| | 198 | setenv("VPR_BASE_DIR", |
|---|
| | 199 | base_dir.native_directory_string().c_str(), 1); |
|---|
| | 200 | } |
|---|
| | 201 | } |
|---|
| | 202 | catch (fs::filesystem_error& ex) |
|---|
| | 203 | { |
|---|
| | 204 | std::cerr << "Automatic assignment of VPR_BASE_DIR failed:\n" |
|---|
| | 205 | << ex.what() << std::endl; |
|---|
| | 206 | } |
|---|
| | 207 | } |
|---|
| | 208 | } |
|---|
| | 209 | } |
|---|