diff --git a/src/modules/hostinfo/CMakeLists.txt b/src/modules/hostinfo/CMakeLists.txt new file mode 100644 index 000000000..5236d30ef --- /dev/null +++ b/src/modules/hostinfo/CMakeLists.txt @@ -0,0 +1,29 @@ +calamares_add_plugin( hostinfo + TYPE job + EXPORT_MACRO PLUGINDLLEXPORT_PRO + SOURCES + HostInfoJob.cpp + LINK_PRIVATE_LIBRARIES + calamares + SHARED_LIB +) + +if ( KF5CoreAddons_FOUND AND KF5CoreAddons_VERSION VERSION_GREATER_EQUAL 5.58 ) + target_compile_definitions( calamares_job_hostinfo PRIVATE WITH_KOSRelease ) +endif() + +if( ECM_FOUND AND BUILD_TESTING ) + ecm_add_test( + Tests.cpp + HostInfoJob.cpp # Builds it a second time + TEST_NAME + hostinfotest + LINK_LIBRARIES + ${CALAMARES_LIBRARIES} + calamaresui + ${YAMLCPP_LIBRARY} + Qt5::Core + Qt5::Test + ) + calamares_automoc( hostinfotest ) +endif() diff --git a/src/modules/hostinfo/HostInfoJob.cpp b/src/modules/hostinfo/HostInfoJob.cpp new file mode 100644 index 000000000..1b59985d9 --- /dev/null +++ b/src/modules/hostinfo/HostInfoJob.cpp @@ -0,0 +1,163 @@ +/* === This file is part of Calamares - === + * + * Copyright 2019, Adriaan de Groot + * + * Calamares is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Calamares is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Calamares. If not, see . + */ + +#include "HostInfoJob.h" + +#include "GlobalStorage.h" +#include "JobQueue.h" +#include "utils/CalamaresUtilsSystem.h" +#include "utils/Logger.h" + +#include +#include + +#ifdef WITH_KOSRelease +#include +#endif + +#ifdef Q_OS_FREEBSD +#include +#include +#endif + +HostInfoJob::HostInfoJob( QObject* parent ) + : Calamares::CppJob( parent ) +{ +} + +HostInfoJob::~HostInfoJob() {} + + +QString +HostInfoJob::prettyName() const +{ + return tr( "Collecting information about your machine." ); +} + +QString +hostOS() +{ +#if defined( Q_OS_FREEBSD ) + return QStringLiteral( "FreeBSD" ); +#elif defined( Q_OS_LINUX ) + return QStringLiteral( "Linux" ); +#else + return QStringLiteral( "" ); +#endif +} + +QString +hostOSName() +{ +#ifdef WITH_KOSRelease + KOSRelease r; + return r.name(); +#else + return hostOS(); +#endif +} + +static QString +hostCPUmatch( const QString& s ) +{ + const QString line = s.toLower(); + if ( line.contains( "intel" ) ) + { + return QStringLiteral( "Intel" ); + } + else if ( line.contains( "amd" ) ) + { + return QStringLiteral( "AMD" ); + } + return QString(); +} + +#if defined( Q_OS_FREEBSD ) +QString +hostCPU_FreeBSD() +{ + constexpr const size_t sysctl_buffer_size = 128; + char sysctl_buffer[ sysctl_buffer_size ]; + size_t s = sysctl_buffer_size; + + memset( sysctl_buffer, 0, sizeof( sysctl_buffer ) ); + int r = sysctlbyname( "hw.model", &sysctl_buffer, &s, NULL, 0 ); + if ( r ) + { + return QString(); + } + + sysctl_buffer[ sysctl_buffer_size - 1 ] = 0; + QString model( sysctl_buffer ); + return hostCPUmatch( model ); +} +#endif + +#if defined( Q_OS_LINUX ) +QString +hostCPU_Linux() +{ + QFile cpuinfo( "/proc/cpuinfo" ); + if ( cpuinfo.open( QIODevice::ReadOnly ) ) + { + QTextStream in( &cpuinfo ); + QString line; + while ( in.readLineInto( line ) ) + { + if ( line.startsWith( "cpu_type" ) ) + { + return hostCPUmatch( line ); + } + } + } + return QString(); // Not open, or not found +} +#endif + +QString +hostCPU() +{ +#if defined( Q_OS_FREEBSD ) + return hostCPU_FreeBSD(); +#elif defined( Q_OS_LINUX ) + return hostCPU_Linux(); +#else + return QString(); +#endif +} + + +Calamares::JobResult +HostInfoJob::exec() +{ + cDebug() << "Collecting host information..."; + + auto* gs = Calamares::JobQueue::instance()->globalStorage(); + gs->insert( "hostOS", hostOS() ); + gs->insert( "hostOSName", hostOSName() ); + gs->insert( "hostCPU", hostCPU() ); + + return Calamares::JobResult::ok(); +} + +void +HostInfoJob::setConfigurationMap( const QVariantMap& ) +{ +} + +CALAMARES_PLUGIN_FACTORY_DEFINITION( HostInfoJobFactory, registerPlugin< HostInfoJob >(); ) diff --git a/src/modules/hostinfo/HostInfoJob.h b/src/modules/hostinfo/HostInfoJob.h new file mode 100644 index 000000000..1274139c3 --- /dev/null +++ b/src/modules/hostinfo/HostInfoJob.h @@ -0,0 +1,66 @@ +/* === This file is part of Calamares - === + * + * Copyright 2019, Adriaan de Groot + * + * Calamares is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Calamares is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Calamares. If not, see . + */ + +#ifndef HOSTINFOJOB_H +#define HOSTINFOJOB_H + +#include "CppJob.h" +#include "PluginDllMacro.h" +#include "utils/PluginFactory.h" + +#include +#include + +/** @brief the compile-time host OS + * + * Returns "FreeBSD" or "Linux" or empty. + */ +QString hostOS(); + +/** @brief the run-time host OS + * + * Returns os-release NAME information, or if that is blank or not available, + * the same as hostOS(). + */ +QString hostOSName(); + +/** @brief the run-time CPU architecture + * + * Returns "Intel" or "AMD" or blank, if Calamares can determine what + * CPU is currently in use (based on /proc/cpuinfo or hw.model). + */ +QString hostCPU(); + +class PLUGINDLLEXPORT HostInfoJob : public Calamares::CppJob +{ + Q_OBJECT + +public: + explicit HostInfoJob( QObject* parent = nullptr ); + virtual ~HostInfoJob() override; + + QString prettyName() const override; + + Calamares::JobResult exec() override; + + void setConfigurationMap( const QVariantMap& configurationMap ) override; +}; + +CALAMARES_PLUGIN_FACTORY_DECLARATION( HostInfoJobFactory ) + +#endif // HOSTINFOJOB_H diff --git a/src/modules/hostinfo/Tests.cpp b/src/modules/hostinfo/Tests.cpp new file mode 100644 index 000000000..32fce19ed --- /dev/null +++ b/src/modules/hostinfo/Tests.cpp @@ -0,0 +1,69 @@ +/* === This file is part of Calamares - === + * + * Copyright 2019, Adriaan de Groot + * + * Calamares is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Calamares is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Calamares. If not, see . + */ +#include "HostInfoJob.h" + +#include "GlobalStorage.h" +#include "JobQueue.h" +#include "Settings.h" + +#include "utils/Logger.h" +#include "utils/Yaml.h" + +#include + +class HostInfoTests : public QObject +{ + Q_OBJECT +public: + HostInfoTests() {} + virtual ~HostInfoTests() {} + +private Q_SLOTS: + void initTestCase(); + + void testHostOS(); +}; + +void +HostInfoTests::initTestCase() +{ + Logger::setupLogLevel( Logger::LOGDEBUG ); + cDebug() << "HostInfo test started."; +} + +void +HostInfoTests::testHostOS() +{ +#if defined( Q_OS_FREEBSD ) + QString expect( "FreeBSD" ); +#elif defined( Q_OS_LINUX ) + QString expect( "Linux" ); +#else + QString expect( "Plan8" ); // Expect failure +#endif + + QCOMPARE( expect, hostOS() ); + QCOMPARE( expect, hostOSName() ); // Might be the same + QCOMPARE( QStringLiteral( "Intel" ), hostCPU() ); // On all my developer machines +} + + +QTEST_GUILESS_MAIN( HostInfoTests ) + +#include "Tests.moc" +#include "utils/moc-warnings.h"