| | 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 | Dl_info info; |
|---|
| | 136 | info.dli_fname = 0; |
|---|
| | 137 | const int result = dladdr(reinterpret_cast<const void*>(&vprLibraryInit), |
|---|
| | 138 | &info); |
|---|
| | 148 | #if defined(VPR_OS_IRIX) && defined(_ABIN32) |
|---|
| | 149 | const std::string bit_suffix("32"); |
|---|
| | 150 | #elif defined(VPR_OS_IRIX) && defined(_ABI64) || \ |
|---|
| | 151 | defined(VPR_OS_Linux) && defined(__x86_64__) |
|---|
| | 152 | const std::string bit_suffix("64"); |
|---|
| | 153 | #else |
|---|
| | 154 | const std::string bit_suffix(""); |
|---|
| | 155 | #endif |
|---|
| | 156 | |
|---|
| | 157 | // Get the directory containing this shared library. |
|---|
| | 158 | const fs::path lib_path = lib_file.branch_path(); |
|---|
| | 159 | |
|---|
| | 160 | // Start the search for the root of the VPR installation in the |
|---|
| | 161 | // parent of the directory containing this shared library. |
|---|
| | 162 | fs::path base_dir = lib_path.branch_path(); |
|---|
| | 163 | |
|---|
| | 164 | // Use the lib subdirectory to figure out when we have found the root |
|---|
| | 165 | // of the VPR installation tree. |
|---|
| | 166 | const fs::path lib_subdir(std::string("lib") + bit_suffix); |
|---|
| | 167 | |
|---|
| | 168 | bool found(false); |
|---|
| | 169 | while ( ! found ) |
|---|
| | 170 | { |
|---|
| | 171 | try |
|---|
| | 172 | { |
|---|
| | 173 | if ( ! fs::exists(base_dir / lib_subdir) ) |
|---|
| | 174 | { |
|---|
| | 175 | base_dir = base_dir.branch_path(); |
|---|
| | 176 | } |
|---|
| | 177 | else |
|---|
| | 178 | { |
|---|
| | 179 | found = true; |
|---|
| | 180 | } |
|---|
| | 181 | } |
|---|
| | 182 | catch (fs::filesystem_error&) |
|---|
| | 183 | { |
|---|
| | 184 | base_dir = base_dir.branch_path(); |
|---|
| | 185 | } |
|---|
| | 186 | } |
|---|
| | 187 | |
|---|
| | 188 | // We use the overwrite value of 0 as a way around testing whether |
|---|
| | 189 | // the environment variable is already set. |
|---|
| | 190 | setenv("VPR_BASE_DIR", base_dir.native_directory_string().c_str(), 0); |
|---|
| | 191 | } |
|---|
| | 192 | catch (fs::filesystem_error& ex) |
|---|
| | 193 | { |
|---|
| | 194 | std::cerr << "Automatic assignment of VPR_BASE_DIR failed:\n" |
|---|
| | 195 | << ex.what() << std::endl; |
|---|
| | 196 | } |
|---|
| | 197 | } |
|---|
| | 198 | } |
|---|