From 10c4a1570b10299f08f0992d777e63488613f509 Mon Sep 17 00:00:00 2001 From: Cloud Date: Thu, 16 Jul 2026 22:15:54 +0800 Subject: [PATCH] fix: build error on Windows fix build error on Windows --- include/DtkCore/DLog | 2 ++ src/CMakeLists.txt | 21 +++++++++++------ src/dconfig.cpp | 7 ++++++ src/dsgapplication.cpp | 38 ++++++++++++++++++++++--------- src/dsysinfo.cpp | 5 +++- src/filesystem/dstandardpaths.cpp | 18 ++++++++++++++- src/log/LogManager.cpp | 11 ++++++++- tools/CMakeLists.txt | 10 ++++---- 8 files changed, 87 insertions(+), 25 deletions(-) diff --git a/include/DtkCore/DLog b/include/DtkCore/DLog index 0c4a85af..ad10452f 100644 --- a/include/DtkCore/DLog +++ b/include/DtkCore/DLog @@ -5,5 +5,7 @@ #include #include #include +#ifdef BUILD_WITH_SYSTEMD #include +#endif #include diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index db33425a..dbad37f6 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -8,11 +8,10 @@ set(CMAKE_AUTORCC ON) find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Core) find_package(Dtk${DTK_NAME_SUFFIX}Log REQUIRED) -find_package(DBus1 REQUIRED) +find_package(PkgConfig REQUIRED) +find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS DBus) if(LINUX) - find_package(PkgConfig REQUIRED) - find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS DBus) - + find_package(DBus1 REQUIRED) if(${QT_VERSION_MAJOR} EQUAL 5) pkg_check_modules(QGSettings REQUIRED IMPORTED_TARGET gsettings-qt) #Dtk6 removed. elseif(${QT_VERSION_MAJOR} EQUAL 6) @@ -89,6 +88,9 @@ if(LINUX) endif() else() + if("${QT_VERSION_MAJOR}" STREQUAL "6") + find_package(Qt6 REQUIRED COMPONENTS CorePrivate) + endif() add_library(${LIB_NAME} SHARED ${base_SRCS} ${dci_SRCS} @@ -101,10 +103,13 @@ else() target_link_libraries( ${LIB_NAME} PUBLIC Qt${QT_VERSION_MAJOR}::Core + Qt${QT_VERSION_MAJOR}::DBus Qt${QT_VERSION_MAJOR}::Xml + Dtk${DTK_NAME_SUFFIX}::Log ) target_link_libraries(${LIB_NAME} PRIVATE Qt${QT_VERSION_MAJOR}::CorePrivate + iconv ) if("${QT_VERSION_MAJOR}" STREQUAL "5") target_link_libraries(${LIB_NAME} PRIVATE @@ -127,9 +132,11 @@ target_compile_definitions(${LIB_NAME} PRIVATE LIBDTKCORE_LIBRARY ) -target_link_libraries(${LIB_NAME} PRIVATE - dbus-1 -) +if(LINUX) + target_link_libraries(${LIB_NAME} PRIVATE + dbus-1 + ) +endif() target_include_directories(${LIB_NAME} PRIVATE ${uchardet_INCLUDE_DIRS} ) diff --git a/src/dconfig.cpp b/src/dconfig.cpp index 2c1cdfe6..b6031ced 100644 --- a/src/dconfig.cpp +++ b/src/dconfig.cpp @@ -17,13 +17,20 @@ #include #include +#include +#ifdef Q_OS_LINUX #include +#endif // https://gitlabwh.uniontech.com/wuhan/se/deepin-specifications/-/issues/3 DCORE_BEGIN_NAMESPACE +#ifdef D_DISABLE_DCONFIG +Q_LOGGING_CATEGORY(cfLog, "dtk.dsg.config") +#else Q_DECLARE_LOGGING_CATEGORY(cfLog) +#endif static QString NoAppId; /*! diff --git a/src/dsgapplication.cpp b/src/dsgapplication.cpp index a7cfdb7e..930b5135 100644 --- a/src/dsgapplication.cpp +++ b/src/dsgapplication.cpp @@ -1,11 +1,16 @@ -// SPDX-FileCopyrightText: 2022-2023 UnionTech Software Technology Co., Ltd. +// SPDX-FileCopyrightText: 2022-2026 UnionTech Software Technology Co., Ltd. // // SPDX-License-Identifier: LGPL-3.0-or-later #include "dsgapplication.h" +#ifdef Q_OS_LINUX #include #include +#include +#else +#include +#endif #include #include @@ -14,8 +19,7 @@ #include #include #include - -#include +#include #ifdef QT_DEBUG Q_LOGGING_CATEGORY(dsgApp, "dtk.core.dsg") @@ -25,6 +29,8 @@ Q_LOGGING_CATEGORY(dsgApp, "dtk.core.dsg", QtInfoMsg) DCORE_BEGIN_NAMESPACE +#ifdef Q_OS_LINUX + // D-Bus resource deleters for RAII management static void dbusErrorDeleter(DBusError *error) { if (error) { dbus_error_free(error); delete error; } @@ -217,19 +223,13 @@ static QByteArray callDBusIdentifyMethod(const QByteArray &serviceName, const QB return appId; } -static inline QByteArray getSelfAppId() { - // The env is set by the application starter(eg, org.desktopspec.ApplicationManager service) - QByteArray selfId = qgetenv("DSG_APP_ID"); - if (!selfId.isEmpty()) - return selfId; - return DSGApplication::getId(QCoreApplication::applicationPid()); -} - static bool isServiceActivatable(const QByteArray &service) { return checkDBusServiceActivatable(service); } +#endif // Q_OS_LINUX + // Format appId to valid. static QByteArray formatAppId(const QByteArray &appId) { @@ -243,6 +243,14 @@ static QByteArray formatAppId(const QByteArray &appId) return format.toLocal8Bit(); } +static inline QByteArray getSelfAppId() { + // The env is set by the application starter(eg, org.desktopspec.ApplicationManager service) + QByteArray selfId = qgetenv("DSG_APP_ID"); + if (!selfId.isEmpty()) + return selfId; + return DSGApplication::getId(QCoreApplication::applicationPid()); +} + QByteArray DSGApplication::id() { static QByteArray selfId = getSelfAppId(); @@ -251,6 +259,7 @@ QByteArray DSGApplication::id() QByteArray result = selfId; if (!qEnvironmentVariableIsSet("DTK_DISABLED_FALLBACK_APPID")) { result = QCoreApplication::applicationName().toLocal8Bit(); +#ifdef Q_OS_LINUX if (result.isEmpty()) { QFile file("/proc/self/cmdline"); if (file.open(QIODevice::ReadOnly)) @@ -261,6 +270,7 @@ QByteArray DSGApplication::id() if (file.exists()) result = file.absoluteFilePath().toLocal8Bit(); } +#endif if (!result.isEmpty()) { result = formatAppId(result); qCDebug(dsgApp) << "The applicatiion ID is fallback to " << result; @@ -286,6 +296,7 @@ QByteArray DSGApplication::id() */ QByteArray DSGApplication::getId(qint64 pid) { +#ifdef Q_OS_LINUX if (!isServiceActivatable("org.desktopspec.ApplicationManager1")) { qCInfo(dsgApp) << "Can't getId from AM for the " << pid << ", because AM is unavailable."; return QByteArray(); @@ -305,6 +316,11 @@ QByteArray DSGApplication::getId(qint64 pid) close(pidfd); return appId; +#else + Q_UNUSED(pid) + // On non-Linux platforms, D-Bus ApplicationManager is not available + return QByteArray(); +#endif } DCORE_END_NAMESPACE diff --git a/src/dsysinfo.cpp b/src/dsysinfo.cpp index c26fc82e..2a58e8a1 100644 --- a/src/dsysinfo.cpp +++ b/src/dsysinfo.cpp @@ -1,4 +1,4 @@ -// SPDX-FileCopyrightText: 2017 - 2023 UnionTech Software Technology Co., Ltd. +// SPDX-FileCopyrightText: 2017 - 2026 UnionTech Software Technology Co., Ltd. // // SPDX-License-Identifier: LGPL-3.0-or-later @@ -13,6 +13,9 @@ #include #include #include +#ifdef Q_OS_WIN +#include +#endif #include #include #include diff --git a/src/filesystem/dstandardpaths.cpp b/src/filesystem/dstandardpaths.cpp index a7492074..fa1a9c03 100644 --- a/src/filesystem/dstandardpaths.cpp +++ b/src/filesystem/dstandardpaths.cpp @@ -1,12 +1,15 @@ -// SPDX-FileCopyrightText: 2017 - 2022 UnionTech Software Technology Co., Ltd. +// SPDX-FileCopyrightText: 2017 - 2026 UnionTech Software Technology Co., Ltd. // // SPDX-License-Identifier: LGPL-3.0-or-later #include "dstandardpaths.h" #include +#include +#ifdef Q_OS_LINUX #include #include +#endif DCORE_BEGIN_NAMESPACE @@ -118,7 +121,11 @@ QString DStandardPaths::homePath() if (!home.isEmpty()) return QString::fromLocal8Bit(home); +#ifdef Q_OS_LINUX return homePath(getuid()); +#else + return QDir::homePath(); +#endif } QString DStandardPaths::path(DStandardPaths::XDG type) @@ -146,7 +153,11 @@ QString DStandardPaths::path(DStandardPaths::XDG type) const QByteArray &path = qgetenv("XDG_RUNTIME_DIR"); if (!path.isEmpty()) return QString::fromLocal8Bit(path); +#ifdef Q_OS_LINUX return QStringLiteral("/run/user/") + QString::number(getuid()); +#else + return QDir::tempPath(); +#endif } case XDG::StateHome: { const QByteArray &path = qgetenv("XDG_STATE_HOME"); @@ -213,6 +224,7 @@ QString DStandardPaths::filePath(DStandardPaths::DSG type, const QString fileNam QString DStandardPaths::homePath(const uint uid) { +#ifdef Q_OS_LINUX struct passwd *pw = getpwuid(uid); if (!pw) @@ -220,6 +232,10 @@ QString DStandardPaths::homePath(const uint uid) const char *homedir = pw->pw_dir; return QString::fromLocal8Bit(homedir); +#else + Q_UNUSED(uid) + return QDir::homePath(); +#endif } DCORE_END_NAMESPACE diff --git a/src/log/LogManager.cpp b/src/log/LogManager.cpp index be123885..654d284d 100644 --- a/src/log/LogManager.cpp +++ b/src/log/LogManager.cpp @@ -8,7 +8,9 @@ #include #include #include +#if defined(BUILD_WITH_SYSTEMD) && defined(Q_OS_LINUX) #include +#endif #ifdef Q_OS_LINUX #include @@ -56,7 +58,9 @@ class DLogManagerPrivate { QString m_logPath; ConsoleAppender* m_consoleAppender = nullptr; RollingFileAppender* m_rollingFileAppender = nullptr; +#if defined(BUILD_WITH_SYSTEMD) && defined(Q_OS_LINUX) JournalAppender* m_journalAppender = nullptr; +#endif QScopedPointer m_dsgConfig; QScopedPointer m_fallbackConfig; @@ -134,7 +138,7 @@ bool DLogManagerPrivate::shouldSkipConsoleAppender() const if (qEnvironmentVariableIsSet("DTK_FORCE_CONSOLE_LOGGING")) return false; - +#ifdef Q_OS_LINUX // Skip ConsoleAppender if running under systemd and stdout is connected to journal to avoid duplicate logging QByteArray journalStream = qgetenv("JOURNAL_STREAM"); if (journalStream.isEmpty()) @@ -153,6 +157,9 @@ bool DLogManagerPrivate::shouldSkipConsoleAppender() const auto ino = parts[1].toULongLong(&ok2); return ok1 && ok2 && st.st_dev == dev && st.st_ino == ino; +#else + return false; +#endif } /*! @@ -172,11 +179,13 @@ DLogManager::DLogManager() void DLogManager::initConsoleAppender(){ Q_D(DLogManager); +#if defined(BUILD_WITH_SYSTEMD) && defined(Q_OS_LINUX) // Skip ConsoleAppender if JournalAppender is active under systemd to avoid duplicate logging if (d->shouldSkipConsoleAppender() && d->m_journalAppender) { qDebug() << "Running under systemd with JournalAppender enabled, skipping ConsoleAppender to avoid duplicate logging"; return; } +#endif d->m_consoleAppender = new ConsoleAppender; d->m_consoleAppender->setFormat(d->m_format); diff --git a/tools/CMakeLists.txt b/tools/CMakeLists.txt index 63b8fc92..e2096bc5 100644 --- a/tools/CMakeLists.txt +++ b/tools/CMakeLists.txt @@ -1,6 +1,8 @@ -add_subdirectory(dci) -add_subdirectory(deepin-os-release) +if(LINUX) + add_subdirectory(dci) + add_subdirectory(deepin-os-release) + add_subdirectory(settings) + add_subdirectory(ch2py) +endif() add_subdirectory(qdbusxml2cpp) -add_subdirectory(settings) -add_subdirectory(ch2py) add_subdirectory(dconfig2cpp)