Bläddra i källkod

cmSystemTools: Add helpers for reading and parsing PATH env vars

Brad King 2 år sedan
förälder
incheckning
acd9636d9d
2 ändrade filer med 29 tillägg och 0 borttagningar
  1. 26 0
      Source/cmSystemTools.cxx
  2. 3 0
      Source/cmSystemTools.h

+ 26 - 0
Source/cmSystemTools.cxx

@@ -1648,6 +1648,32 @@ std::string cmSystemTools::RelativeIfUnder(std::string const& top,
   return out;
 }
 
+cm::optional<std::string> cmSystemTools::GetEnvVar(std::string const& var)
+{
+  cm::optional<std::string> result;
+  {
+    std::string value;
+    if (cmSystemTools::GetEnv(var, value)) {
+      result = std::move(value);
+    }
+  }
+  return result;
+}
+
+std::vector<std::string> cmSystemTools::SplitEnvPath(std::string const& value)
+{
+#if defined(_WIN32) && !defined(__CYGWIN__)
+  static cm::string_view sep = ";"_s;
+#else
+  static cm::string_view sep = ":"_s;
+#endif
+  std::vector<std::string> paths = cmTokenize(value, sep);
+  for (std::string& p : paths) {
+    SystemTools::ConvertToUnixSlashes(p);
+  }
+  return paths;
+}
+
 #ifndef CMAKE_BOOTSTRAP
 bool cmSystemTools::UnsetEnv(const char* value)
 {

+ 3 - 0
Source/cmSystemTools.h

@@ -399,6 +399,9 @@ public:
   static std::string RelativeIfUnder(std::string const& top,
                                      std::string const& in);
 
+  static cm::optional<std::string> GetEnvVar(std::string const& var);
+  static std::vector<std::string> SplitEnvPath(std::string const& value);
+
 #ifndef CMAKE_BOOTSTRAP
   /** Remove an environment variable */
   static bool UnsetEnv(const char* value);