]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
Add util::split_path_list function and remove PATH_DELIM macro
authorJoel Rosdahl <joel@rosdahl.net>
Tue, 6 Jul 2021 06:57:50 +0000 (08:57 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Tue, 6 Jul 2021 20:19:43 +0000 (22:19 +0200)
src/Context.cpp
src/ccache.cpp
src/execute.cpp
src/system.hpp
src/util/path_utils.cpp
src/util/path_utils.hpp
unittest/test_util_path_utils.cpp

index 27b4048139bd6ecd7ae29751a7b5f0c06923bdd8..d61a68b419d1b24709deea6ecc166227d51148b1 100644 (file)
@@ -24,6 +24,8 @@
 #include "Util.hpp"
 #include "hashutil.hpp"
 
+#include <util/path_utils.hpp>
+
 #include <algorithm>
 #include <string>
 #include <vector>
@@ -43,7 +45,7 @@ Context::Context()
   Logging::init(config);
 
   ignore_header_paths =
-    Util::split_into_strings(config.ignore_headers_in_manifest(), PATH_DELIM);
+    util::split_path_list(config.ignore_headers_in_manifest());
   set_ignore_options(Util::split_into_strings(config.ignore_options(), " "));
 
   // Set default umask for all files created by ccache from now on (if
index cdaf8ffcb9b0a47c5724ec12686ece9f069d844e..65daa54a20cc3637d2260c8d045f10fa8753d39c 100644 (file)
@@ -1370,8 +1370,8 @@ hash_common_info(const Context& ctx,
   }
 
   if (!ctx.config.extra_files_to_hash().empty()) {
-    for (const std::string& path : Util::split_into_strings(
-           ctx.config.extra_files_to_hash(), PATH_DELIM)) {
+    for (const std::string& path :
+         util::split_path_list(ctx.config.extra_files_to_hash())) {
       LOG("Hashing extra file {}", path);
       hash.hash_delimiter("extrafile");
       if (!hash_binary_file(ctx, hash, path)) {
index 86d0a8084a2393037e870178b524f96c834b7b75..4f7a9a6bde4fcf0f42bd5bd26c3ba817c3ab4af2 100644 (file)
@@ -270,7 +270,7 @@ find_executable_in_path(const std::string& name,
 
   // Search the path looking for the first compiler of the right name that isn't
   // us.
-  for (const std::string& dir : Util::split_into_strings(path, PATH_DELIM)) {
+  for (const std::string& dir : util::split_path_list(path)) {
 #ifdef _WIN32
     char namebuf[MAX_PATH];
     int ret = SearchPath(
index 424891f7e4b618c8e94dfec7697b895a26da3178..99fe9287cdf39e0231ab4b6c6928c3436d1d7d3a 100644 (file)
@@ -174,10 +174,8 @@ const mode_t S_IWUSR = mode_t(_S_IWRITE);
 #  define STDIN_FILENO 0
 #  define STDOUT_FILENO 1
 #  define STDERR_FILENO 2
-#  define PATH_DELIM ";"
 #else
 #  define DLLIMPORT
-#  define PATH_DELIM ":"
 #endif
 
 DLLIMPORT extern char** environ;
index de5f81f467c6e387fbae4e67f550ab4322bcde26..2d6b80f08f878c6184ff663142a0f14186f3c2e3 100644 (file)
 
 #include "path_utils.hpp"
 
-#include <Logging.hpp>
 #include <Util.hpp>
 #include <fmtmacros.hpp>
 
+#ifdef _WIN32
+const char k_path_delimiter[] = ";";
+#else
+const char k_path_delimiter[] = ":";
+#endif
+
 namespace util {
 
 bool
@@ -36,6 +41,12 @@ is_absolute_path(nonstd::string_view path)
   return !path.empty() && path[0] == '/';
 }
 
+std::vector<std::string>
+split_path_list(nonstd::string_view path_list)
+{
+  return Util::split_into_strings(path_list, k_path_delimiter);
+}
+
 std::string
 to_absolute_path(nonstd::string_view path)
 {
index 3ef23cbaf18a71bb4d24119a9c74225327e9560b..6e81a683de0c029c612fe279a9cd0a2f6543786f 100644 (file)
 #include <third_party/nonstd/string_view.hpp>
 
 #include <string>
+#include <vector>
 
 namespace util {
 
 // Return whether `path` is absolute.
 bool is_absolute_path(nonstd::string_view path);
 
+// Split a list of paths (such as the content of $PATH on Unix platforms or
+// %PATH% on Windows platforms) into paths.
+std::vector<std::string> split_path_list(nonstd::string_view path_list);
+
 // Make `path` an absolute path.
 std::string to_absolute_path(nonstd::string_view path);
 
index 0d6a773e1a2338a5b52c3dfc808b5e7d08ee7b4e..038e37ff84524a5b32f3531f76092ec3a30dd4f3 100644 (file)
@@ -38,6 +38,31 @@ TEST_CASE("util::is_absolute_path")
   CHECK(!util::is_absolute_path("foo/fie"));
 }
 
+TEST_CASE("util::split_path_list")
+{
+  CHECK(util::split_path_list("").empty());
+  {
+    const auto v = util::split_path_list("a");
+    REQUIRE(v.size() == 1);
+    CHECK(v[0] == "a");
+  }
+  {
+    const auto v = util::split_path_list("a/b");
+    REQUIRE(v.size() == 1);
+    CHECK(v[0] == "a/b");
+  }
+  {
+#ifdef _WIN32
+    const auto v = util::split_path_list("a/b;c");
+#else
+    const auto v = util::split_path_list("a/b:c");
+#endif
+    REQUIRE(v.size() == 2);
+    CHECK(v[0] == "a/b");
+    CHECK(v[1] == "c");
+  }
+}
+
 TEST_CASE("util::to_absolute_path")
 {
   CHECK(util::to_absolute_path("/foo/bar") == "/foo/bar");