[feat] add portable build storage mode, refs #80

This commit is contained in:
dijunkun
2026-05-20 23:53:13 +08:00
parent a3aedcb624
commit 515d517a99
5 changed files with 196 additions and 3 deletions
+5
View File
@@ -242,6 +242,11 @@ jobs:
cd "${{ github.workspace }}\scripts\windows"
makensis /DVERSION=$env:VERSION_NUM nsis_script.nsi
- name: Build Portable CrossDesk
run: |
xmake f --CROSSDESK_VERSION=${{ env.VERSION_NUM }} --USE_CUDA=true --CROSSDESK_PORTABLE=true -y
xmake b -vy crossdesk
- name: Package Portable
shell: pwsh
run: |
+96 -1
View File
@@ -1,12 +1,98 @@
#include "path_manager.h"
#include <cstdint>
#include <cstdlib>
#include <vector>
#ifndef CROSSDESK_PORTABLE
#define CROSSDESK_PORTABLE 0
#endif
#if CROSSDESK_PORTABLE
#if defined(__APPLE__)
#include <mach-o/dyld.h>
#elif !defined(_WIN32)
#include <limits.h>
#include <unistd.h>
#endif
#endif
namespace {
#if CROSSDESK_PORTABLE
std::filesystem::path GetExecutableDirectory() {
#ifdef _WIN32
std::vector<wchar_t> buffer(MAX_PATH);
while (true) {
DWORD length =
GetModuleFileNameW(nullptr, buffer.data(),
static_cast<DWORD>(buffer.size()));
if (length == 0) {
return {};
}
if (length < buffer.size()) {
return std::filesystem::path(buffer.data(), buffer.data() + length)
.parent_path();
}
if (buffer.size() >= 32768) {
return {};
}
buffer.resize(buffer.size() * 2);
}
#elif defined(__APPLE__)
uint32_t size = 0;
_NSGetExecutablePath(nullptr, &size);
std::vector<char> buffer(size + 1);
if (_NSGetExecutablePath(buffer.data(), &size) != 0) {
return {};
}
std::error_code ec;
std::filesystem::path executable =
std::filesystem::weakly_canonical(buffer.data(), ec);
if (ec) {
executable = buffer.data();
}
return executable.parent_path();
#else
std::vector<char> buffer(PATH_MAX);
while (true) {
ssize_t length = readlink("/proc/self/exe", buffer.data(),
buffer.size() - 1);
if (length <= 0) {
return {};
}
if (static_cast<size_t>(length) < buffer.size() - 1) {
buffer[static_cast<size_t>(length)] = '\0';
return std::filesystem::path(buffer.data()).parent_path();
}
buffer.resize(buffer.size() * 2);
}
#endif
}
std::filesystem::path GetPortableRootPath() {
std::filesystem::path executable_dir = GetExecutableDirectory();
if (!executable_dir.empty()) {
return executable_dir;
}
std::error_code ec;
std::filesystem::path current = std::filesystem::current_path(ec);
return ec ? std::filesystem::path(".") : current;
}
#endif
} // namespace
namespace crossdesk {
PathManager::PathManager(const std::string& app_name) : app_name_(app_name) {}
std::filesystem::path PathManager::GetConfigPath() {
#if CROSSDESK_PORTABLE
return GetPortableRootPath() / "data";
#else
#ifdef _WIN32
return GetKnownFolder(FOLDERID_RoamingAppData) / app_name_;
#elif __APPLE__
@@ -14,9 +100,13 @@ std::filesystem::path PathManager::GetConfigPath() {
#else
return GetEnvOrDefault("XDG_CONFIG_HOME", GetHome() + "/.config") / app_name_;
#endif
#endif
}
std::filesystem::path PathManager::GetCachePath() {
#if CROSSDESK_PORTABLE
return GetPortableRootPath() / "data";
#else
#ifdef _WIN32
#ifdef CROSSDESK_DEBUG
return "cache";
@@ -28,9 +118,13 @@ std::filesystem::path PathManager::GetCachePath() {
#else
return GetEnvOrDefault("XDG_CACHE_HOME", GetHome() + "/.cache") / app_name_;
#endif
#endif
}
std::filesystem::path PathManager::GetLogPath() {
#if CROSSDESK_PORTABLE
return GetPortableRootPath() / "logs";
#else
#ifdef _WIN32
return GetKnownFolder(FOLDERID_LocalAppData) / app_name_ / "logs";
#elif __APPLE__
@@ -38,6 +132,7 @@ std::filesystem::path PathManager::GetLogPath() {
#else
return GetCachePath() / "logs";
#endif
#endif
}
bool PathManager::CreateDirectories(const std::filesystem::path& p) {
@@ -93,4 +188,4 @@ std::filesystem::path PathManager::GetEnvOrDefault(const char* env_var,
return std::filesystem::path(def);
}
} // namespace crossdesk
} // namespace crossdesk
+78
View File
@@ -0,0 +1,78 @@
#include "path_manager.h"
#include <cstdint>
#include <filesystem>
#include <iostream>
#include <string>
#ifdef _WIN32
#include <windows.h>
#elif defined(__APPLE__)
#include <mach-o/dyld.h>
#include <limits.h>
#else
#include <limits.h>
#include <unistd.h>
#endif
namespace {
std::filesystem::path GetExecutableDirectory() {
#ifdef _WIN32
wchar_t buffer[MAX_PATH] = {};
DWORD length = GetModuleFileNameW(nullptr, buffer, MAX_PATH);
if (length == 0 || length == MAX_PATH) {
return {};
}
return std::filesystem::path(buffer).parent_path();
#elif defined(__APPLE__)
char buffer[PATH_MAX] = {};
uint32_t size = sizeof(buffer);
if (_NSGetExecutablePath(buffer, &size) != 0) {
return {};
}
return std::filesystem::weakly_canonical(buffer).parent_path();
#else
char buffer[PATH_MAX] = {};
ssize_t length = readlink("/proc/self/exe", buffer, sizeof(buffer) - 1);
if (length <= 0) {
return {};
}
buffer[length] = '\0';
return std::filesystem::path(buffer).parent_path();
#endif
}
bool ExpectEqual(const char* name,
const std::filesystem::path& actual,
const std::filesystem::path& expected) {
if (actual.lexically_normal() == expected.lexically_normal()) {
return true;
}
std::cerr << name << " mismatch\n"
<< " expected: " << expected.string() << "\n"
<< " actual: " << actual.string() << "\n";
return false;
}
} // namespace
int main() {
const std::filesystem::path exe_dir = GetExecutableDirectory();
if (exe_dir.empty()) {
std::cerr << "failed to resolve executable directory\n";
return 1;
}
crossdesk::PathManager path_manager("CrossDesk");
const std::filesystem::path expected_data = exe_dir / "data";
const std::filesystem::path expected_logs = exe_dir / "logs";
bool ok = true;
ok &= ExpectEqual("config path", path_manager.GetConfigPath(), expected_data);
ok &= ExpectEqual("cache path", path_manager.GetCachePath(), expected_data);
ok &= ExpectEqual("log path", path_manager.GetLogPath(), expected_logs);
return ok ? 0 : 1;
}
+8 -1
View File
@@ -23,6 +23,12 @@ function setup_options_and_dependencies()
set_description("Enable DRM capture on Linux (assumes dependencies are installed)")
option_end()
option("CROSSDESK_PORTABLE")
set_default(false)
set_showmenu(true)
set_description("Build CrossDesk as a portable package that stores data beside the executable")
option_end()
add_rules("mode.release", "mode.debug")
set_languages("c++17")
set_encodings("utf-8")
@@ -35,6 +41,7 @@ function setup_options_and_dependencies()
add_defines("USE_CUDA=" .. (is_config("USE_CUDA", true) and "1" or "0"))
add_defines("USE_WAYLAND=" .. (is_config("USE_WAYLAND", true) and "1" or "0"))
add_defines("USE_DRM=" .. (is_config("USE_DRM", true) and "1" or "0"))
add_defines("CROSSDESK_PORTABLE=" .. (is_config("CROSSDESK_PORTABLE", true) and "1" or "0"))
if is_mode("debug") then
add_defines("CROSSDESK_DEBUG")
@@ -47,4 +54,4 @@ function setup_options_and_dependencies()
add_requires("nlohmann_json 3.11.3")
add_requires("cpp-httplib v0.26.0", {configs = {ssl = true}})
add_requires("tinyfiledialogs 3.15.1")
end
end
+9 -1
View File
@@ -25,6 +25,14 @@ function setup_targets()
add_files("src/path_manager/*.cpp")
add_includedirs("src/path_manager", {public = true})
target("path_manager_portable_test")
set_kind("binary")
set_default(false)
add_defines("CROSSDESK_PORTABLE=1")
add_includedirs("src/path_manager")
add_files("tests/path_manager_portable_test.cpp",
"src/path_manager/path_manager.cpp")
target("screen_capturer")
set_kind("object")
add_deps("rd_log", "common")
@@ -195,4 +203,4 @@ function setup_targets()
add_deps("wgc_plugin", "crossdesk_service", "crossdesk_session_helper")
add_files("scripts/windows/crossdesk.rc")
end
end
end