| 96 | | catch (fs::filesystem_error& ex) |
|---|
| 97 | | { |
|---|
| 98 | | std::cerr << "Automatic assignment of SNX_BASE_DIR failed:\n" |
|---|
| 99 | | << ex.what() << std::endl; |
|---|
| 100 | | } |
|---|
| 101 | | } |
|---|
| 102 | | #if defined(_MSC_VER) && _MSC_VER >= 1400 |
|---|
| 103 | | else |
|---|
| 104 | | { |
|---|
| 105 | | std::free(env_dir); |
|---|
| 106 | | } |
|---|
| 107 | | #endif |
|---|
| | 107 | #if defined(_MSC_VER) && _MSC_VER >= 1400 |
|---|
| | 108 | else |
|---|
| | 109 | { |
|---|
| | 110 | std::free(env_dir); |
|---|
| | 111 | } |
|---|
| | 112 | #endif |
|---|
| | 113 | |
|---|
| | 114 | #if defined(_MSC_VER) && _MSC_VER >= 1400 |
|---|
| | 115 | _dupenv_s(&env_dir, &len, "SNX_DATA_DIR"); |
|---|
| | 116 | #else |
|---|
| | 117 | env_dir = std::getenv("SNX_DATA_DIR"); |
|---|
| | 118 | #endif |
|---|
| | 119 | |
|---|
| | 120 | if ( NULL == env_dir ) |
|---|
| | 121 | { |
|---|
| | 122 | fs::path data_dir(base_dir / "share" / "sonix"); |
|---|
| | 123 | const std::string data_dir_str = |
|---|
| | 124 | data_dir.native_directory_string(); |
|---|
| | 125 | |
|---|
| | 126 | #if defined(_MSC_VER) && _MSC_VER >= 1400 |
|---|
| | 127 | _putenv_s("SNX_DATA_DIR", data_dir_str.c_str()); |
|---|
| | 128 | #else |
|---|
| | 129 | std::ostringstream env_stream; |
|---|
| | 130 | env_stream << "SNX_DATA_DIR=" << data_dir_str; |
|---|
| | 131 | putenv(env_stream.str().c_str()); |
|---|
| | 132 | #endif |
|---|
| | 133 | } |
|---|
| | 134 | #if defined(_MSC_VER) && _MSC_VER >= 1400 |
|---|
| | 135 | else |
|---|
| | 136 | { |
|---|
| | 137 | std::free(env_dir); |
|---|
| | 138 | } |
|---|
| | 139 | #endif |
|---|
| | 140 | } |
|---|
| | 141 | catch (fs::filesystem_error& ex) |
|---|
| | 142 | { |
|---|
| | 143 | std::cerr << "Automatic assignment of Sonix environment " |
|---|
| | 144 | << "variables failed:\n" << ex.what() << std::endl; |
|---|
| | 145 | } |
|---|
| 116 | | |
|---|
| 117 | | |
|---|
| | 154 | #else |
|---|
| | 155 | /** |
|---|
| | 156 | * Non-Windows shared library constructor. This ensures that the environment |
|---|
| | 157 | * variable \c SNX_BASE_DIR is set as soon as this shared library is loaded. |
|---|
| | 158 | * If it is not set, then it sets it based on an assumption about the |
|---|
| | 159 | * structure of a Sonix installation. More specifically, an assumption is made |
|---|
| | 160 | * that this shared library lives in the \c lib subdirectory of the Sonix |
|---|
| | 161 | * installation. Therefore, the root of the Sonix installation is the parent |
|---|
| | 162 | * of the directory containing this shared library. |
|---|
| | 163 | */ |
|---|
| | 164 | extern "C" void __attribute ((constructor)) snxLibraryInit() |
|---|
| | 165 | { |
|---|
| | 166 | Dl_info info; |
|---|
| | 167 | info.dli_fname = 0; |
|---|
| | 168 | const int result = dladdr(reinterpret_cast<const void*>(&snxLibraryInit), |
|---|
| | 169 | &info); |
|---|
| | 170 | |
|---|
| | 171 | // NOTE: dladdr(3) really does return a non-zero value on success. |
|---|
| | 172 | if ( 0 != result ) |
|---|
| | 173 | { |
|---|
| | 174 | try |
|---|
| | 175 | { |
|---|
| | 176 | fs::path lib_file(info.dli_fname, fs::native); |
|---|
| | 177 | lib_file = fs::system_complete(lib_file); |
|---|
| | 178 | |
|---|
| | 179 | #if defined(VPR_OS_IRIX) && defined(_ABIN32) |
|---|
| | 180 | const std::string bit_suffix("32"); |
|---|
| | 181 | #elif defined(VPR_OS_IRIX) && defined(_ABI64) || \ |
|---|
| | 182 | defined(VPR_OS_Linux) && defined(__x86_64__) |
|---|
| | 183 | const std::string bit_suffix("64"); |
|---|
| | 184 | #else |
|---|
| | 185 | const std::string bit_suffix(""); |
|---|
| | 186 | #endif |
|---|
| | 187 | |
|---|
| | 188 | // Get the directory containing this shared library. |
|---|
| | 189 | const fs::path lib_path = lib_file.branch_path(); |
|---|
| | 190 | |
|---|
| | 191 | // Start the search for the root of the Sonix installation in the |
|---|
| | 192 | // parent of the directory containing this shared library. |
|---|
| | 193 | fs::path base_dir = lib_path.branch_path(); |
|---|
| | 194 | |
|---|
| | 195 | // Use the lib subdirectory to figure out when we have found the root |
|---|
| | 196 | // of the Sonix installation tree. |
|---|
| | 197 | const fs::path lib_subdir(std::string("lib") + bit_suffix); |
|---|
| | 198 | |
|---|
| | 199 | bool found(false); |
|---|
| | 200 | while ( ! found ) |
|---|
| | 201 | { |
|---|
| | 202 | try |
|---|
| | 203 | { |
|---|
| | 204 | if ( ! fs::exists(base_dir / lib_subdir) ) |
|---|
| | 205 | { |
|---|
| | 206 | base_dir = base_dir.branch_path(); |
|---|
| | 207 | } |
|---|
| | 208 | else |
|---|
| | 209 | { |
|---|
| | 210 | found = true; |
|---|
| | 211 | } |
|---|
| | 212 | } |
|---|
| | 213 | catch (fs::filesystem_error&) |
|---|
| | 214 | { |
|---|
| | 215 | base_dir = base_dir.branch_path(); |
|---|
| | 216 | } |
|---|
| | 217 | } |
|---|
| | 218 | |
|---|
| | 219 | std::cout << base_dir.native_directory_string() << std::endl; |
|---|
| | 220 | const fs::path data_dir = base_dir / SNX_SHARE_DIR; |
|---|
| | 221 | |
|---|
| | 222 | // We use the overwrite value of 0 as a way around testing whether |
|---|
| | 223 | // the environment variables are already set. |
|---|
| | 224 | setenv("SNX_BASE_DIR", base_dir.native_directory_string().c_str(), 0); |
|---|
| | 225 | setenv("SNX_DATA_DIR", data_dir.native_directory_string().c_str(), 0); |
|---|
| | 226 | } |
|---|
| | 227 | catch (fs::filesystem_error& ex) |
|---|
| | 228 | { |
|---|
| | 229 | std::cerr << "Automatic assignment of Sonix environment " |
|---|
| | 230 | << "variables failed:\n" << ex.what() << std::endl; |
|---|
| | 231 | } |
|---|
| | 232 | } |
|---|
| | 233 | } |
|---|