return format_bool(m_debug);
case ConfigItem::debug_dir:
- return m_debug_dir;
+ return m_debug_dir.string();
case ConfigItem::debug_level:
return FMT("{}", m_debug_level);
#include <core/Sloppiness.hpp>
#include <util/NonCopyable.hpp>
+#include <util/filesystem.hpp>
#include <util/string.hpp>
#include <sys/types.h>
int8_t compression_level() const;
const std::string& cpp_extension() const;
bool debug() const;
- const std::string& debug_dir() const;
+ const std::filesystem::path& debug_dir() const;
uint8_t debug_level() const;
bool depend_mode() const;
bool direct_mode() const;
int8_t m_compression_level = 0; // Use default level
std::string m_cpp_extension;
bool m_debug = false;
- std::string m_debug_dir;
+ std::filesystem::path m_debug_dir;
uint8_t m_debug_level = 2;
bool m_depend_mode = false;
bool m_direct_mode = true;
return m_debug;
}
-inline const std::string&
+inline const std::filesystem::path&
Config::debug_dir() const
{
return m_debug_dir;
}
static std::string
-prepare_debug_path(const std::string& debug_dir,
+prepare_debug_path(const fs::path& cwd,
+ const fs::path& debug_dir,
const util::TimePoint& time_of_invocation,
- const std::string& output_obj,
+ const fs::path& output_obj,
std::string_view suffix)
{
- auto prefix = debug_dir.empty()
- ? output_obj
- : debug_dir + util::to_absolute_path_no_drive(output_obj);
+ auto prefix =
+ debug_dir.empty()
+ ? output_obj
+ : (debug_dir
+ / (output_obj.is_absolute()
+ ? output_obj
+ : fs::weakly_canonical(cwd / output_obj).value_or(output_obj))
+ .relative_path());
// Ignore any error from fs::create_directories since we can't handle an error
// in another way in this context. The caller takes care of logging when
// trying to open the path for writing.
- fs::create_directories(Util::dir_name(prefix));
+ fs::create_directories(prefix.parent_path());
char timestamp[100];
const auto tm = util::localtime(time_of_invocation);
static_cast<long long unsigned int>(time_of_invocation.sec()));
}
return FMT("{}.{}_{:06}.ccache-{}",
- prefix,
+ prefix.string(),
timestamp,
time_of_invocation.nsec_decimal_part() / 1000,
suffix);
return;
}
- const auto path = prepare_debug_path(ctx.config.debug_dir(),
+ const auto path = prepare_debug_path(ctx.apparent_cwd,
+ ctx.config.debug_dir(),
ctx.time_of_invocation,
ctx.args_info.output_obj,
FMT("input-{}", type));
// Dump log buffer last to not lose any logs.
if (ctx.config.debug() && !ctx.args_info.output_obj.empty()) {
- util::logging::dump_log(prepare_debug_path(ctx.config.debug_dir(),
+ util::logging::dump_log(prepare_debug_path(ctx.apparent_cwd,
+ ctx.config.debug_dir(),
ctx.time_of_invocation,
ctx.args_info.output_obj,
"log"));
MTR_META_THREAD_NAME(ctx.args_info.output_obj.c_str());
if (ctx.config.debug() && ctx.config.debug_level() >= 2) {
- const auto path = prepare_debug_path(ctx.config.debug_dir(),
+ const auto path = prepare_debug_path(ctx.apparent_cwd,
+ ctx.config.debug_dir(),
ctx.time_of_invocation,
ctx.args_info.orig_output_obj,
"input-text");
DEF_WRAP_1_R(remove, bool, const path&, p)
DEF_WRAP_1_R(remove_all, std::uintmax_t, const path&, p)
DEF_WRAP_0_R(temp_directory_path, path)
+DEF_WRAP_1_R(weakly_canonical, path, const path&, p)
// clang-format on
return real_path ? real_path->string() : std::string(path);
}
-std::string
-to_absolute_path(std::string_view path)
-{
- if (is_absolute_path(path)) {
- return std::string(path);
- } else {
- return Util::normalize_abstract_absolute_path(
- FMT("{}/{}", actual_cwd(), path));
- }
-}
-
-std::string
-to_absolute_path_no_drive(std::string_view path)
-{
- std::string abs_path = to_absolute_path(path);
-#ifdef _WIN32
- if (abs_path.length() >= 2 && abs_path[1] == ':') {
- abs_path.erase(0, 2);
- }
-#endif
- return abs_path;
-}
-
} // namespace util
// doesn't exist) path is returned unmodified.
std::string real_path(std::string_view path);
-// Make `path` an absolute path.
-std::string to_absolute_path(std::string_view path);
-
-// Make `path` an absolute path, but do not include Windows drive.
-std::string to_absolute_path_no_drive(std::string_view path);
-
// --- Inline implementations ---
inline bool
#endif
}
-TEST_CASE("util::to_absolute_path")
-{
- CHECK(util::to_absolute_path("/foo/bar") == "/foo/bar");
-
-#ifdef _WIN32
- CHECK(util::to_absolute_path("C:\\foo\\bar") == "C:\\foo\\bar");
-#endif
-
- const auto cwd = util::actual_cwd();
-
- CHECK(util::to_absolute_path("") == cwd);
- CHECK(util::to_absolute_path(".") == cwd);
- CHECK(util::to_absolute_path("..") == Util::dir_name(cwd));
- CHECK(util::to_absolute_path("foo") == FMT("{}/foo", cwd));
- CHECK(util::to_absolute_path("../foo/bar")
- == FMT("{}/foo/bar", Util::dir_name(cwd)));
-}
-
-TEST_CASE("util::to_absolute_path_no_drive")
-{
- CHECK(util::to_absolute_path_no_drive("/foo/bar") == "/foo/bar");
-
-#ifdef _WIN32
- CHECK(util::to_absolute_path_no_drive("C:\\foo\\bar") == "\\foo\\bar");
-#endif
-
- auto cwd = util::actual_cwd();
-#ifdef _WIN32
- cwd = cwd.substr(2);
-#endif
-
- CHECK(util::to_absolute_path_no_drive("") == cwd);
- CHECK(util::to_absolute_path_no_drive(".") == cwd);
- CHECK(util::to_absolute_path_no_drive("..") == Util::dir_name(cwd));
- CHECK(util::to_absolute_path_no_drive("foo") == FMT("{}/foo", cwd));
- CHECK(util::to_absolute_path_no_drive("../foo/bar")
- == FMT("{}/foo/bar", Util::dir_name(cwd)));
-}
-
TEST_CASE("util::path_starts_with")
{
CHECK(!util::path_starts_with("", ""));