Move utility functions to DriverUtils.cpp.
llvm-svn: 267602
diff --git a/lld/ELF/DriverUtils.cpp b/lld/ELF/DriverUtils.cpp
index 949ab95..3953d5f 100644
--- a/lld/ELF/DriverUtils.cpp
+++ b/lld/ELF/DriverUtils.cpp
@@ -23,6 +23,7 @@
#include "llvm/Support/StringSaver.h"
using namespace llvm;
+using namespace llvm::sys;
using namespace lld;
using namespace lld::elf;
@@ -86,6 +87,33 @@
outs() << "\n";
}
+// Concatenates S and T so that the resulting path becomes S/T.
+// There are a few exceptions:
+//
+// 1. The result will never escape from S. Therefore, all ".."
+// are removed from T before concatenatig them.
+// 2. Windows drive letters are removed from T before concatenation.
+std::string elf::concat_paths(StringRef S, StringRef T) {
+ // Remove leading '/' or a drive letter, and then remove "..".
+ SmallString<128> T2(path::relative_path(T));
+ path::remove_dots(T2, /*remove_dot_dot=*/true);
+
+ SmallString<128> Res;
+ path::append(Res, S, T2);
+ return Res.str();
+}
+
+void elf::copyFile(StringRef Src, StringRef Dest) {
+ SmallString<128> Dir(Dest);
+ path::remove_filename(Dir);
+ if (std::error_code EC = sys::fs::create_directories(Dir)) {
+ error(EC, Dir + ": can't create directory");
+ return;
+ }
+ if (std::error_code EC = sys::fs::copy_file(Src, Dest))
+ error(EC, "failed to copy file: " + Dest);
+}
+
std::string elf::findFromSearchPaths(StringRef Path) {
for (StringRef Dir : Config->SearchPaths) {
std::string FullPath = buildSysrootedPath(Dir, Path);