2022-09-19 12:06:23 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
|
2023-08-05 19:42:26 +03:00
|
|
|
* Copyright (c) 2023, Andrew Kaster <akaster@serenityos.org>
|
2022-09-19 12:06:23 +03:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Utilities.h"
|
2022-10-05 16:23:41 +03:00
|
|
|
#include <AK/LexicalPath.h>
|
|
|
|
#include <AK/Platform.h>
|
2024-01-30 19:16:17 +03:00
|
|
|
#include <LibCore/Environment.h>
|
2023-10-04 00:40:24 +03:00
|
|
|
#include <LibCore/ResourceImplementationFile.h>
|
2023-08-05 19:42:26 +03:00
|
|
|
#include <LibCore/System.h>
|
2023-03-21 18:35:30 +03:00
|
|
|
#include <LibFileSystem/FileSystem.h>
|
2022-10-05 16:23:41 +03:00
|
|
|
|
2024-02-24 03:02:17 +03:00
|
|
|
#define TOKENCAT(x, y) x##y
|
|
|
|
#define STRINGIFY(x) TOKENCAT(x, sv)
|
|
|
|
|
|
|
|
// This is expected to be set from the build scripts, if a packager desires
|
|
|
|
#if defined(LADYBIRD_LIBEXECDIR)
|
|
|
|
constexpr auto libexec_path = STRINGIFY(LADYBIRD_LIBEXECDIR);
|
|
|
|
#else
|
|
|
|
constexpr auto libexec_path = "libexec"sv;
|
|
|
|
#endif
|
|
|
|
|
2023-12-16 17:19:34 +03:00
|
|
|
ByteString s_serenity_resource_root;
|
2022-09-19 12:06:23 +03:00
|
|
|
|
2024-02-22 04:27:05 +03:00
|
|
|
ErrorOr<ByteString> application_directory()
|
2022-09-19 12:06:23 +03:00
|
|
|
{
|
2023-08-05 19:42:26 +03:00
|
|
|
auto current_executable_path = TRY(Core::System::current_executable_path());
|
2024-02-22 04:27:05 +03:00
|
|
|
return LexicalPath::dirname(current_executable_path);
|
2023-03-14 00:30:31 +03:00
|
|
|
}
|
|
|
|
|
2024-02-24 03:02:17 +03:00
|
|
|
[[gnu::used]] static LexicalPath find_prefix(LexicalPath const& application_directory);
|
|
|
|
static LexicalPath find_prefix(LexicalPath const& application_directory)
|
|
|
|
{
|
|
|
|
if (application_directory.string().ends_with(libexec_path)) {
|
|
|
|
// Strip libexec_path if it's there
|
|
|
|
return LexicalPath(application_directory.string().substring_view(0, application_directory.string().length() - libexec_path.length()));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, we are in $prefix/bin
|
|
|
|
return application_directory.parent();
|
|
|
|
}
|
|
|
|
|
2022-10-05 16:23:41 +03:00
|
|
|
void platform_init()
|
|
|
|
{
|
|
|
|
s_serenity_resource_root = [] {
|
2024-01-30 19:16:17 +03:00
|
|
|
auto home = Core::Environment::get("XDG_CONFIG_HOME"sv)
|
|
|
|
.value_or_lazy_evaluated_optional([]() { return Core::Environment::get("HOME"sv); });
|
|
|
|
if (home.has_value()) {
|
2024-02-24 03:02:17 +03:00
|
|
|
auto home_lagom = ByteString::formatted("{}/.lagom", home);
|
|
|
|
if (FileSystem::is_directory(home_lagom))
|
|
|
|
return home_lagom;
|
|
|
|
}
|
2024-02-22 04:27:05 +03:00
|
|
|
auto app_dir = MUST(application_directory());
|
2023-09-02 18:30:21 +03:00
|
|
|
#ifdef AK_OS_MACOS
|
2023-03-26 18:53:32 +03:00
|
|
|
return LexicalPath(app_dir).parent().append("Resources"sv).string();
|
2023-09-02 18:30:21 +03:00
|
|
|
#else
|
2024-02-24 03:02:17 +03:00
|
|
|
return find_prefix(LexicalPath(app_dir)).append("share/Lagom"sv).string();
|
2022-10-05 16:23:41 +03:00
|
|
|
#endif
|
2023-09-02 18:30:21 +03:00
|
|
|
}();
|
2024-02-23 23:40:16 +03:00
|
|
|
Core::ResourceImplementation::install(make<Core::ResourceImplementationFile>(MUST(String::from_byte_string(s_serenity_resource_root))));
|
2022-10-05 16:23:41 +03:00
|
|
|
}
|
2023-08-01 20:56:10 +03:00
|
|
|
|
2024-02-22 04:27:05 +03:00
|
|
|
ErrorOr<Vector<ByteString>> get_paths_for_helper_process(StringView process_name)
|
2023-08-01 20:56:10 +03:00
|
|
|
{
|
2023-08-05 19:42:26 +03:00
|
|
|
auto application_path = TRY(application_directory());
|
2024-02-22 04:27:05 +03:00
|
|
|
Vector<ByteString> paths;
|
2023-08-01 20:56:10 +03:00
|
|
|
|
2024-02-24 03:02:17 +03:00
|
|
|
#if !defined(AK_OS_MACOS)
|
|
|
|
auto prefix = find_prefix(LexicalPath(application_path));
|
|
|
|
TRY(paths.try_append(LexicalPath::join(prefix.string(), libexec_path, process_name).string()));
|
|
|
|
TRY(paths.try_append(LexicalPath::join(prefix.string(), "bin"sv, process_name).string()));
|
|
|
|
#endif
|
2024-02-22 04:27:05 +03:00
|
|
|
TRY(paths.try_append(ByteString::formatted("{}/{}", application_path, process_name)));
|
|
|
|
TRY(paths.try_append(ByteString::formatted("./{}", process_name)));
|
2023-08-01 20:56:10 +03:00
|
|
|
// NOTE: Add platform-specific paths here
|
|
|
|
return paths;
|
|
|
|
}
|