| 326 | } |
| 327 | |
| 328 | std::map<std::string, std::string> GetOSReleaseVariables( |
| 329 | cmExecutionStatus& status) |
| 330 | { |
| 331 | auto& makefile = status.GetMakefile(); |
| 332 | auto const& sysroot = makefile.GetSafeDefinition("CMAKE_SYSROOT"); |
| 333 | |
| 334 | std::map<std::string, std::string> data; |
| 335 | // Based on |
| 336 | // https://www.freedesktop.org/software/systemd/man/latest/os-release.html |
| 337 | for (auto name : { "/etc/os-release"_s, "/usr/lib/os-release"_s }) { |
| 338 | auto const& filename = cmStrCat(sysroot, name); |
| 339 | if (cmSystemTools::FileExists(filename)) { |
| 340 | cmsys::ifstream fin(filename.c_str()); |
| 341 | for (std::string line; !std::getline(fin, line).fail();) { |
| 342 | auto kv = ParseOSReleaseLine(line); |
| 343 | if (kv.has_value()) { |
| 344 | data.emplace(kv.value()); |
| 345 | } |
| 346 | } |
| 347 | break; |
| 348 | } |
| 349 | } |
| 350 | // Got smth? |
| 351 | if (!data.empty()) { |
| 352 | return data; |
| 353 | } |
| 354 | |
| 355 | // Ugh, it could be some pre-os-release distro. |
| 356 | // Lets try some fallback getters. |
| 357 | // See also: |
| 358 | // - http://linuxmafia.com/faq/Admin/release-files.html |
| 359 | |
| 360 | // 1. CMake provided |
| 361 | cmsys::Glob gl; |
| 362 | std::vector<std::string> scripts; |
| 363 | auto const findExpr = cmStrCat(cmSystemTools::GetCMakeRoot(), |
| 364 | "/Modules/Internal/OSRelease/*.cmake"); |
| 365 | if (gl.FindFiles(findExpr)) { |
| 366 | scripts = gl.GetFiles(); |
| 367 | } |
| 368 | |
| 369 | // 2. User provided (append to the CMake provided) |
| 370 | cmList::append( |
| 371 | scripts, makefile.GetDefinition("CMAKE_GET_OS_RELEASE_FALLBACK_SCRIPTS")); |
| 372 | |
| 373 | // Filter out files that are not in format `NNN-name.cmake` |
| 374 | auto checkName = [](std::string const& filepath) -> bool { |
| 375 | auto const& filename = cmSystemTools::GetFilenameName(filepath); |
| 376 | // NOTE Minimum filename length expected: |
| 377 | // NNN-<at-least-one-char-name>.cmake --> 11 |
| 378 | return (filename.size() < 11) || !cmsysString_isdigit(filename[0]) || |
| 379 | !cmsysString_isdigit(filename[1]) || !cmsysString_isdigit(filename[2]) || |
| 380 | filename[3] != '-'; |
| 381 | }; |
| 382 | scripts.erase(std::remove_if(scripts.begin(), scripts.end(), checkName), |
| 383 | scripts.end()); |
| 384 | |
| 385 | // Make sure scripts are running in desired order |
no test coverage detected
searching dependent graphs…