Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vars: Use uname() for architecture autodetection #1063

Merged
merged 2 commits into from
Dec 7, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 42 additions & 12 deletions libdnf5/conf/vars.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ along with libdnf. If not, see <https://www.gnu.org/licenses/>.
#include <rpm/rpmmacro.h>
#include <rpm/rpmts.h>
#include <stdlib.h>
#include <sys/auxv.h>
#include <sys/types.h>
#include <sys/utsname.h>

#include <algorithm>
#include <cstring>
Expand Down Expand Up @@ -73,14 +75,46 @@ static constexpr const char * DISTROVERPKGS[] = {
"redhat-release",
"suse-release"};

/* ARM specific HWCAP defines may be missing on non-ARM devices */
#ifndef HWCAP_ARM_VFP
#define HWCAP_ARM_VFP (1 << 6)
#endif
#ifndef HWCAP_ARM_NEON
#define HWCAP_ARM_NEON (1 << 12)
#endif

static std::string detect_arch() {
std::string value{};
char * tmp = rpmExpand("%{_host_cpu}", NULL);
if (tmp != nullptr) {
value = tmp;
free(tmp);
struct utsname un;

if (uname(&un) < 0) {
throw RuntimeError(M_("Failed to execute uname()"));
}

if (!strncmp(un.machine, "armv", 4)) {
/* un.machine is armvXE, where X is version number and E is
* endianness (b or l); we need to add modifiers such as
* h (hardfloat), n (neon). Neon is a requirement of armv8 so
* as far as rpm is concerned armv8l is the equivilent of armv7hnl
* (or 7hnb) so we don't explicitly add 'n' for 8+ as it's expected. */
char endian = un.machine[strlen(un.machine) - 1];
char * modifier = un.machine + 5;
while (isdigit(*modifier)) /* keep armv7, armv8, armv9, armv10, armv100, ... */
modifier++;
if (getauxval(AT_HWCAP) & HWCAP_ARM_VFP)
*modifier++ = 'h';
if ((atoi(un.machine + 4) == 7) && (getauxval(AT_HWCAP) & HWCAP_ARM_NEON))
*modifier++ = 'n';
*modifier++ = endian;
*modifier = 0;
}
return value;
#ifdef __MIPSEL__
// support for little endian MIPS
if (!strcmp(un.machine, "mips"))
strcpy(un.machine, "mipsel");
else if (!strcmp(un.machine, "mips64"))
strcpy(un.machine, "mips64el");
#endif
return un.machine;
}


Expand Down Expand Up @@ -377,15 +411,11 @@ void Vars::load(const std::string & installroot, const std::vector<std::string>
}

void Vars::detect_vars(const std::string & installroot) {
const char * arch = nullptr;
if (contains("arch")) {
arch = get_value("arch").c_str();
}
init_lib_rpm(arch);

set_lazy(
"arch", []() -> auto { return std::make_unique<std::string>(detect_arch()); }, Priority::AUTO);

init_lib_rpm(get_value("arch").c_str());

set_lazy(
"basearch",
[this]() -> auto {
Expand Down