MCPcopy Create free account
hub / github.com/Kitware/CMake / GetOSReleaseVariables

Function GetOSReleaseVariables

Source/cmCMakeHostSystemInformationCommand.cxx:328–441  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

326}
327
328std::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

Callers 1

GetDistribValueFunction · 0.85

Calls 15

ParseOSReleaseLineFunction · 0.85
appendFunction · 0.85
cmStrToLongFunction · 0.85
cmHasPrefixFunction · 0.85
moveFunction · 0.85
c_strMethod · 0.80
has_valueMethod · 0.80
emplaceMethod · 0.80
valueMethod · 0.80
FindFilesMethod · 0.80
eraseMethod · 0.80
cmStrCatFunction · 0.70

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…