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