From: Joel Rosdahl Date: Wed, 23 Jul 2025 08:44:00 +0000 (+0200) Subject: refactor: Move Args to util namespace X-Git-Url: http://git.ipfire.org/gitweb/gitweb.cgi?a=commitdiff_plain;h=4790f35dc953f234100ed538c56ca3d4572d2731;p=thirdparty%2Fccache.git refactor: Move Args to util namespace --- diff --git a/src/ccache/CMakeLists.txt b/src/ccache/CMakeLists.txt index 97616b68..03129125 100644 --- a/src/ccache/CMakeLists.txt +++ b/src/ccache/CMakeLists.txt @@ -1,7 +1,6 @@ set( source_files argprocessing.cpp - args.cpp ccache.cpp compopt.cpp config.cpp diff --git a/src/ccache/argprocessing.cpp b/src/ccache/argprocessing.cpp index fcb2b8de..31d58091 100644 --- a/src/ccache/argprocessing.cpp +++ b/src/ccache/argprocessing.cpp @@ -18,13 +18,13 @@ #include "argprocessing.hpp" -#include #include #include #include #include #include #include +#include #include #include #include @@ -151,9 +151,9 @@ public: } private: - Args m_preprocessor_args; - Args m_compiler_args; - Args m_extra_args_to_hash; + util::Args m_preprocessor_args; + util::Args m_compiler_args; + util::Args m_extra_args_to_hash; }; bool @@ -363,7 +363,7 @@ std::optional process_option_arg(const Context& ctx, ArgsInfo& args_info, Config& config, - Args& args, + util::Args& args, size_t& args_index, ArgumentProcessingState& state) { @@ -420,7 +420,7 @@ process_option_arg(const Context& ctx, ++argpath; } auto file_args = - Args::from_response_file(argpath, config.response_file_format()); + util::Args::from_response_file(argpath, config.response_file_format()); if (!file_args) { LOG("Couldn't read arg file {}", argpath); return Statistic::bad_compiler_arguments; @@ -443,8 +443,8 @@ process_option_arg(const Context& ctx, // Argument is a comma-separated list of files. auto paths = util::split_into_strings(args[i], ","); for (auto it = paths.rbegin(); it != paths.rend(); ++it) { - auto file_args = - Args::from_response_file(*it, Args::ResponseFileFormat::posix); + auto file_args = util::Args::from_response_file( + *it, util::Args::ResponseFileFormat::posix); if (!file_args) { LOG("Couldn't read CUDA options file {}", *it); return Statistic::bad_compiler_arguments; @@ -1251,7 +1251,7 @@ Statistic process_arg(const Context& ctx, ArgsInfo& args_info, Config& config, - Args& args, + util::Args& args, size_t& args_index, ArgumentProcessingState& state) { @@ -1312,7 +1312,7 @@ process_args(Context& ctx) // args is a copy of the original arguments given to the compiler but with // arguments from @file and similar constructs expanded. It's only used as a // temporary data structure to loop over. - Args args = ctx.orig_args; + util::Args args = ctx.orig_args; ArgumentProcessingState state; state.add_common_arg(args[0]); // Compiler diff --git a/src/ccache/argprocessing.hpp b/src/ccache/argprocessing.hpp index 6ffc3459..7eabf5a1 100644 --- a/src/ccache/argprocessing.hpp +++ b/src/ccache/argprocessing.hpp @@ -18,8 +18,8 @@ #pragma once -#include #include +#include #include @@ -34,13 +34,13 @@ struct ProcessArgsResult { // Arguments (except "-E -o output.i") to send to the preprocessor. These are // part of the input hash (except those marked as AFFECTS_CPP in compopt.cpp). - Args preprocessor_args; + util::Args preprocessor_args; // Arguments to send to the real compiler. Not part of the input hash. - Args compiler_args; + util::Args compiler_args; // Arguments not sent to the preprocessor but added to the input hash anyway. - Args extra_args_to_hash; + util::Args extra_args_to_hash; // Whether to include the actual CWD in the input hash. bool hash_actual_cwd = false; diff --git a/src/ccache/argsinfo.hpp b/src/ccache/argsinfo.hpp index e7f878ed..3f78b91d 100644 --- a/src/ccache/argsinfo.hpp +++ b/src/ccache/argsinfo.hpp @@ -1,4 +1,4 @@ -// Copyright (C) 2020-2024 Joel Rosdahl and other contributors +// Copyright (C) 2020-2025 Joel Rosdahl and other contributors // // See doc/AUTHORS.adoc for a complete list of contributors. // @@ -18,7 +18,7 @@ #pragma once -#include +#include #include #include diff --git a/src/ccache/ccache.cpp b/src/ccache/ccache.cpp index 4ab2eacc..bf8e9185 100644 --- a/src/ccache/ccache.cpp +++ b/src/ccache/ccache.cpp @@ -20,7 +20,6 @@ #include "ccache.hpp" #include -#include #include #include #include @@ -44,6 +43,7 @@ #include #include #include +#include #include #include #include @@ -174,13 +174,15 @@ should_disable_ccache_for_input_file(const fs::path& path) } static void -add_prefix(const Context& ctx, Args& args, const std::string& prefix_command) +add_prefix(const Context& ctx, + util::Args& args, + const std::string& prefix_command) { if (prefix_command.empty()) { return; } - Args prefixes; + util::Args prefixes; for (const auto& prefix : util::split_into_strings(prefix_command, " ")) { prefixes.push_back(prefix); } @@ -807,7 +809,7 @@ result_key_from_includes(Context& ctx, Hash& hash, std::string_view stdout_data) // Execute the compiler/preprocessor, with logic to retry without requesting // colored diagnostics messages if that fails. static tl::expected -do_execute(Context& ctx, Args& args, const bool capture_stdout = true) +do_execute(Context& ctx, util::Args& args, const bool capture_stdout = true) { util::UmaskScope umask_scope(ctx.original_umask); @@ -1174,7 +1176,7 @@ source_file_is_too_new(const Context& ctx, const fs::path& path) // Run the real compiler and put the result in cache. Returns the result key. static tl::expected to_cache(Context& ctx, - Args& args, + util::Args& args, std::optional result_key, Hash* depend_mode_hash) { @@ -1353,7 +1355,7 @@ process_cuda_chunk(Context& ctx, } static bool -get_clang_cu_enable_verbose_mode(const Args& args) +get_clang_cu_enable_verbose_mode(const util::Args& args) { for (size_t i = 1; i < args.size(); i++) { if (args[i] == "-v") { @@ -1367,7 +1369,7 @@ get_clang_cu_enable_verbose_mode(const Args& args) // Find the result key by running the compiler in preprocessor mode and // hashing the result. static tl::expected -get_result_key_from_cpp(Context& ctx, Args& args, Hash& hash) +get_result_key_from_cpp(Context& ctx, util::Args& args, Hash& hash) { fs::path preprocessed_path; util::Bytes cpp_stderr_data; @@ -1549,7 +1551,7 @@ hash_nvcc_host_compiler(const Context& ctx, // update a hash with information common for the direct and preprocessor modes. static tl::expected -hash_common_info(const Context& ctx, const Args& args, Hash& hash) +hash_common_info(const Context& ctx, const util::Args& args, Hash& hash) { hash.hash(HASH_PREFIX); @@ -1779,7 +1781,7 @@ hash_common_info(const Context& ctx, const Args& args, Hash& hash) static std::tuple, std::optional> -get_option_and_value(std::string_view option, const Args& args, size_t& i) +get_option_and_value(std::string_view option, const util::Args& args, size_t& i) { if (args[i] == option) { if (i + 1 < args.size()) { @@ -1797,7 +1799,7 @@ get_option_and_value(std::string_view option, const Args& args, size_t& i) static tl::expected hash_argument(const Context& ctx, - const Args& args, + const util::Args& args, size_t& i, Hash& hash, const bool is_clang, @@ -2240,9 +2242,9 @@ static tl::expected< std::pair, std::optional>, Failure> calculate_result_and_manifest_key(Context& ctx, - const Args& args, + const util::Args& args, Hash& hash, - Args* preprocessor_args) + util::Args* preprocessor_args) { bool direct_mode = !preprocessor_args; bool found_ccbin = false; @@ -2578,7 +2580,7 @@ split_argv(int argc, const char* const* argv) argv_parts.config_settings.emplace_back(argv[i]); ++i; } - argv_parts.compiler_and_args = Args::from_argv(argc - i, argv + i); + argv_parts.compiler_and_args = util::Args::from_argv(argc - i, argv + i); return argv_parts; } @@ -2589,7 +2591,7 @@ cache_compilation(int argc, const char* const* argv) tzset(); // Needed for localtime_r. bool fall_back_to_original_compiler = false; - Args saved_orig_args; + util::Args saved_orig_args; std::optional original_umask; fs::path saved_temp_dir; @@ -2795,7 +2797,7 @@ do_cache_compilation(Context& ctx) Hash direct_hash = common_hash; init_hash_debug(ctx, direct_hash, 'd', "DIRECT MODE", debug_text_file); - Args args_to_hash = process_args_result->preprocessor_args; + util::Args args_to_hash = process_args_result->preprocessor_args; args_to_hash.push_back(process_args_result->extra_args_to_hash); bool put_result_in_manifest = false; diff --git a/src/ccache/ccache.hpp b/src/ccache/ccache.hpp index bb23027e..eda52ddd 100644 --- a/src/ccache/ccache.hpp +++ b/src/ccache/ccache.hpp @@ -1,5 +1,5 @@ // Copyright (C) 2002-2007 Andrew Tridgell -// Copyright (C) 2009-2024 Joel Rosdahl and other contributors +// Copyright (C) 2009-2025 Joel Rosdahl and other contributors // // See doc/AUTHORS.adoc for a complete list of contributors. // @@ -19,8 +19,8 @@ #pragma once -#include #include +#include #include #include @@ -43,7 +43,7 @@ struct ArgvParts { bool masquerading_as_compiler = true; std::vector config_settings; - Args compiler_and_args; + util::Args compiler_and_args; }; ArgvParts split_argv(int argc, const char* const* argv); diff --git a/src/ccache/config.cpp b/src/ccache/config.cpp index 9ccf43ef..ef16a8dc 100644 --- a/src/ccache/config.cpp +++ b/src/ccache/config.cpp @@ -225,15 +225,15 @@ const std::unordered_map k_env_variable_table = { {"UMASK", "umask" }, }; -Args::ResponseFileFormat +util::Args::ResponseFileFormat parse_response_file_format(const std::string& value) { if (value == "posix") { - return Args::ResponseFileFormat::posix; + return util::Args::ResponseFileFormat::posix; } else if (value == "windows") { - return Args::ResponseFileFormat::windows; + return util::Args::ResponseFileFormat::windows; } else { - return Args::ResponseFileFormat::auto_guess; + return util::Args::ResponseFileFormat::auto_guess; } } @@ -544,14 +544,15 @@ home_directory() } std::string -response_file_format_to_string(Args::ResponseFileFormat response_file_format) +response_file_format_to_string( + util::Args::ResponseFileFormat response_file_format) { switch (response_file_format) { - case Args::ResponseFileFormat::auto_guess: + case util::Args::ResponseFileFormat::auto_guess: return "auto"; - case Args::ResponseFileFormat::posix: + case util::Args::ResponseFileFormat::posix: return "posix"; - case Args::ResponseFileFormat::windows: + case util::Args::ResponseFileFormat::windows: return "windows"; } diff --git a/src/ccache/config.hpp b/src/ccache/config.hpp index 40d87c76..2bb79975 100644 --- a/src/ccache/config.hpp +++ b/src/ccache/config.hpp @@ -18,8 +18,8 @@ #pragma once -#include #include +#include #include #include #include @@ -57,7 +57,7 @@ public: void read(const std::vector& cmdline_config_settings = {}); bool absolute_paths_in_stderr() const; - Args::ResponseFileFormat response_file_format() const; + util::Args::ResponseFileFormat response_file_format() const; const std::filesystem::path& base_dir() const; const std::filesystem::path& cache_dir() const; const std::string& compiler() const; @@ -171,8 +171,8 @@ private: std::filesystem::path m_system_config_path; bool m_absolute_paths_in_stderr = false; - Args::ResponseFileFormat m_response_file_format = - Args::ResponseFileFormat::auto_guess; + util::Args::ResponseFileFormat m_response_file_format = + util::Args::ResponseFileFormat::auto_guess; std::filesystem::path m_base_dir; std::filesystem::path m_cache_dir; std::string m_compiler; @@ -240,15 +240,15 @@ Config::absolute_paths_in_stderr() const return m_absolute_paths_in_stderr; } -inline Args::ResponseFileFormat +inline util::Args::ResponseFileFormat Config::response_file_format() const { - if (m_response_file_format != Args::ResponseFileFormat::auto_guess) { + if (m_response_file_format != util::Args::ResponseFileFormat::auto_guess) { return m_response_file_format; } - return is_compiler_group_msvc() ? Args::ResponseFileFormat::windows - : Args::ResponseFileFormat::posix; + return is_compiler_group_msvc() ? util::Args::ResponseFileFormat::windows + : util::Args::ResponseFileFormat::posix; } inline const std::filesystem::path& diff --git a/src/ccache/context.cpp b/src/ccache/context.cpp index 62650f98..681eec93 100644 --- a/src/ccache/context.cpp +++ b/src/ccache/context.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2020-2024 Joel Rosdahl and other contributors +// Copyright (C) 2020-2025 Joel Rosdahl and other contributors // // See doc/AUTHORS.adoc for a complete list of contributors. // @@ -51,7 +51,7 @@ Context::Context() } void -Context::initialize(Args&& compiler_and_args, +Context::initialize(util::Args&& compiler_and_args, const std::vector& cmdline_config_settings) { orig_args = std::move(compiler_and_args); diff --git a/src/ccache/context.hpp b/src/ccache/context.hpp index 71fa7165..6732c68d 100644 --- a/src/ccache/context.hpp +++ b/src/ccache/context.hpp @@ -1,4 +1,4 @@ -// Copyright (C) 2020-2024 Joel Rosdahl and other contributors +// Copyright (C) 2020-2025 Joel Rosdahl and other contributors // // See doc/AUTHORS.adoc for a complete list of contributors. // @@ -18,12 +18,12 @@ #pragma once -#include #include #include #include #include #include +#include #include #include #include @@ -52,7 +52,7 @@ public: // Read configuration, initialize logging, etc. Typically not called from unit // tests. - void initialize(Args&& compiler_and_args, + void initialize(util::Args&& compiler_and_args, const std::vector& cmdline_config_settings); ArgsInfo args_info; @@ -65,7 +65,7 @@ public: std::filesystem::path apparent_cwd; // The original argument list. - Args orig_args; + util::Args orig_args; // Files included by the preprocessor and their hashes. std::unordered_map included_files; diff --git a/src/ccache/hashutil.cpp b/src/ccache/hashutil.cpp index 1a8d4583..426930a6 100644 --- a/src/ccache/hashutil.cpp +++ b/src/ccache/hashutil.cpp @@ -18,12 +18,12 @@ #include "hashutil.hpp" -#include #include #include #include #include #include +#include #include #include #include @@ -357,7 +357,7 @@ hash_command_output(Hash& hash, } else { using_cmd_exe = false; } - Args args = Args::from_string(adjusted_command); + util::Args args = util::Args::from_string(adjusted_command); { auto full_path = find_executable_in_path(args[0], util::getenv_path_list("PATH")).string(); @@ -366,7 +366,7 @@ hash_command_output(Hash& hash, } } #else - Args args = Args::from_string(command); + util::Args args = util::Args::from_string(command); #endif for (size_t i = 0; i < args.size(); i++) { diff --git a/src/ccache/util/CMakeLists.txt b/src/ccache/util/CMakeLists.txt index 87a0722f..ba2f7632 100644 --- a/src/ccache/util/CMakeLists.txt +++ b/src/ccache/util/CMakeLists.txt @@ -1,5 +1,6 @@ set( sources + args.cpp assertions.cpp bytes.cpp clang.cpp diff --git a/src/ccache/args.cpp b/src/ccache/util/args.cpp similarity index 99% rename from src/ccache/args.cpp rename to src/ccache/util/args.cpp index bfdf1b36..153b421d 100644 --- a/src/ccache/args.cpp +++ b/src/ccache/util/args.cpp @@ -25,6 +25,8 @@ #include #include +namespace util { + Args::Args(Args&& other) noexcept : m_args(std::move(other.m_args)) { @@ -274,3 +276,5 @@ Args::replace(size_t index, const Args& args) insert(index, args); } } + +} // namespace util diff --git a/src/ccache/args.hpp b/src/ccache/util/args.hpp similarity index 99% rename from src/ccache/args.hpp rename to src/ccache/util/args.hpp index 6b46fa50..b29280a4 100644 --- a/src/ccache/args.hpp +++ b/src/ccache/util/args.hpp @@ -26,6 +26,8 @@ #include #include +namespace util { + class Args { public: @@ -159,3 +161,5 @@ Args::push_back(const std::filesystem::path& arg) { m_args.push_back(arg.string()); } + +} // namespace util diff --git a/unittest/CMakeLists.txt b/unittest/CMakeLists.txt index ac14cbb7..746c1e05 100644 --- a/unittest/CMakeLists.txt +++ b/unittest/CMakeLists.txt @@ -2,7 +2,6 @@ set( source_files main.cpp test_argprocessing.cpp - test_args.cpp test_ccache.cpp test_compopt.cpp test_compression_types.cpp @@ -18,6 +17,7 @@ set( test_hashutil.cpp test_storage_local_statsfile.cpp test_storage_local_util.cpp + test_util_args.cpp test_util_bitset.cpp test_util_bytes.cpp test_util_clang.cpp diff --git a/unittest/test_argprocessing.cpp b/unittest/test_argprocessing.cpp index b8b75a09..5e91902a 100644 --- a/unittest/test_argprocessing.cpp +++ b/unittest/test_argprocessing.cpp @@ -19,10 +19,10 @@ #include "testutil.hpp" #include -#include #include #include #include +#include #include #include #include @@ -38,6 +38,7 @@ namespace fs = util::filesystem; using core::Statistic; using TestUtil::TestContext; +using util::Args; namespace { diff --git a/unittest/test_ccache.cpp b/unittest/test_ccache.cpp index fcf7326e..9b43a116 100644 --- a/unittest/test_ccache.cpp +++ b/unittest/test_ccache.cpp @@ -38,6 +38,7 @@ namespace fs = util::filesystem; using TestUtil::TestContext; +using util::Args; TEST_SUITE_BEGIN("ccache"); diff --git a/unittest/test_config.cpp b/unittest/test_config.cpp index c8fb8cde..c0c4e46f 100644 --- a/unittest/test_config.cpp +++ b/unittest/test_config.cpp @@ -307,7 +307,7 @@ TEST_CASE("Config::update_from_environment") TEST_CASE("Config::response_file_format") { - using ResponseFileFormat = Args::ResponseFileFormat; + using ResponseFileFormat = util::Args::ResponseFileFormat; Config config; diff --git a/unittest/test_args.cpp b/unittest/test_util_args.cpp similarity index 99% rename from unittest/test_args.cpp rename to unittest/test_util_args.cpp index 549a106c..e43ace0c 100644 --- a/unittest/test_args.cpp +++ b/unittest/test_util_args.cpp @@ -18,8 +18,8 @@ #include "testutil.hpp" -#include #include +#include #include #include @@ -27,6 +27,7 @@ TEST_SUITE_BEGIN("Args"); using TestUtil::TestContext; +using util::Args; TEST_CASE("Args default constructor") {