diff options
author | Paul Cristian Sarbu <paul.cristian.sarbu@huawei.com> | 2024-07-08 11:28:13 +0200 |
---|---|---|
committer | Paul Cristian Sarbu <paul.cristian.sarbu@huawei.com> | 2024-07-16 17:51:12 +0200 |
commit | 978de9de55d9592c258052dd52dc25c788a89d78 (patch) | |
tree | dff959d84f1d95af1f0546471d3b2c7eaa47dbc2 /src/buildtool/execution_api | |
parent | b4cd4d0c0d1b526eab3549c9cba12179dbba3a3f (diff) | |
download | justbuild-978de9de55d9592c258052dd52dc25c788a89d78.tar.gz |
Remove the LocalExecutionConfig singleton
...and replace it with passed instances created early via a builder
pattern.
Diffstat (limited to 'src/buildtool/execution_api')
-rw-r--r-- | src/buildtool/execution_api/local/TARGETS | 7 | ||||
-rw-r--r-- | src/buildtool/execution_api/local/config.hpp | 65 | ||||
-rw-r--r-- | src/buildtool/execution_api/local/local_action.cpp | 3 |
3 files changed, 39 insertions, 36 deletions
diff --git a/src/buildtool/execution_api/local/TARGETS b/src/buildtool/execution_api/local/TARGETS index 2c848bf7..53b7f717 100644 --- a/src/buildtool/execution_api/local/TARGETS +++ b/src/buildtool/execution_api/local/TARGETS @@ -5,14 +5,9 @@ , "deps": [ ["@", "fmt", "", "fmt"] , ["@", "json", "", "json"] - , ["@", "gsl", "", "gsl"] - , ["src/buildtool/common", "common"] - , ["src/buildtool/file_system", "file_system_manager"] - , ["src/buildtool/file_system", "object_type"] , ["src/buildtool/logging", "log_level"] , ["src/buildtool/logging", "logging"] - , ["src/buildtool/execution_api/remote", "config"] - , ["src/buildtool/compatibility", "compatibility"] + , ["src/utils/cpp", "expected"] ] , "stage": ["src", "buildtool", "execution_api", "local"] } diff --git a/src/buildtool/execution_api/local/config.hpp b/src/buildtool/execution_api/local/config.hpp index 92d6606a..f2c8bf57 100644 --- a/src/buildtool/execution_api/local/config.hpp +++ b/src/buildtool/execution_api/local/config.hpp @@ -15,49 +15,56 @@ #ifndef INCLUDED_SRC_BUILDTOOL_EXECUTION_API_LOCAL_CONFIG_HPP #define INCLUDED_SRC_BUILDTOOL_EXECUTION_API_LOCAL_CONFIG_HPP -#include <filesystem> -#include <functional> +#include <exception> +#include <optional> #include <string> +#include <utility> // std::move #include <vector> -#include "gsl/gsl" +#include "fmt/core.h" #include "nlohmann/json.hpp" -#include "src/buildtool/common/artifact_digest.hpp" -#include "src/buildtool/file_system/file_system_manager.hpp" -#include "src/buildtool/file_system/object_type.hpp" #include "src/buildtool/logging/log_level.hpp" #include "src/buildtool/logging/logger.hpp" +#include "src/utils/cpp/expected.hpp" -/// \brief Store global build system configuration. -class LocalExecutionConfig { +/// \brief Store local execution configuration. +struct LocalExecutionConfig final { + class Builder; + + // Launcher to be prepended to action's command before executed. + // Default: ["env", "--"] + std::vector<std::string> const launcher = {"env", "--"}; +}; + +class LocalExecutionConfig::Builder final { public: - [[nodiscard]] static auto SetLauncher( - std::vector<std::string> const& launcher) noexcept -> bool { - try { - Instance().launcher_ = launcher; - } catch (std::exception const& e) { - Logger::Log(LogLevel::Error, - "when setting the local launcher\n{}", - e.what()); - return false; - } - return true; + auto SetLauncher(std::vector<std::string> launcher) noexcept -> Builder& { + launcher_ = std::move(launcher); + return *this; } - [[nodiscard]] static auto GetLauncher() noexcept - -> std::vector<std::string> { - return Instance().launcher_; - } + /// \brief Finalize building and create LocalExecutionConfig. + /// \return LocalExecutionConfig on success, an error string on failure. + [[nodiscard]] auto Build() const noexcept + -> expected<LocalExecutionConfig, std::string> { + // To not duplicate default arguments in builder, create a default + // config and copy arguments from there. + LocalExecutionConfig const default_config; + auto launcher = default_config.launcher; + if (launcher_.has_value()) { + try { + launcher = *launcher_; + } catch (std::exception const& ex) { + return unexpected{ + fmt::format("Failed to set launcher:\n{}", ex.what())}; + } + } - [[nodiscard]] static auto Instance() noexcept -> LocalExecutionConfig& { - static LocalExecutionConfig config; - return config; + return LocalExecutionConfig{.launcher = std::move(launcher)}; } private: - // Launcher to be prepended to action's command before executed. - // Default: ["env", "--"] - std::vector<std::string> launcher_ = {"env", "--"}; + std::optional<std::vector<std::string>> launcher_; }; #endif // INCLUDED_SRC_BUILDTOOL_EXECUTION_API_LOCAL_CONFIG_HPP diff --git a/src/buildtool/execution_api/local/local_action.cpp b/src/buildtool/execution_api/local/local_action.cpp index 73d78e36..b44fbd5c 100644 --- a/src/buildtool/execution_api/local/local_action.cpp +++ b/src/buildtool/execution_api/local/local_action.cpp @@ -165,7 +165,8 @@ auto LocalAction::Run(bazel_re::Digest const& action_id) const noexcept return std::nullopt; } - auto cmdline = exec_config_.GetLauncher(); + // prepare actual command by including the launcher + auto cmdline = exec_config_.launcher; std::copy(cmdline_.begin(), cmdline_.end(), std::back_inserter(cmdline)); SystemCommand system{"LocalExecution"}; |