2016-07-26 10:00:42 +08:00
|
|
|
//===- Reproduce.cpp - Utilities for creating reproducers -----------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Linker
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2017-10-03 05:00:41 +08:00
|
|
|
#include "lld/Common/Reproduce.h"
|
2016-07-26 10:00:42 +08:00
|
|
|
#include "llvm/Option/Arg.h"
|
|
|
|
#include "llvm/Support/Error.h"
|
|
|
|
#include "llvm/Support/FileSystem.h"
|
|
|
|
#include "llvm/Support/Path.h"
|
|
|
|
|
|
|
|
using namespace lld;
|
|
|
|
using namespace llvm;
|
2017-01-06 10:33:53 +08:00
|
|
|
using namespace llvm::sys;
|
2016-07-26 10:00:42 +08:00
|
|
|
|
|
|
|
// Makes a given pathname an absolute path first, and then remove
|
|
|
|
// beginning /. For example, "../foo.o" is converted to "home/john/foo.o",
|
|
|
|
// assuming that the current directory is "/home/john/bar".
|
2016-11-12 14:27:42 +08:00
|
|
|
// Returned string is a forward slash separated path even on Windows to avoid
|
2016-11-11 21:23:13 +08:00
|
|
|
// a mess with backslash-as-escape and backslash-as-path-separator.
|
2016-07-26 10:00:42 +08:00
|
|
|
std::string lld::relativeToRoot(StringRef Path) {
|
|
|
|
SmallString<128> Abs = Path;
|
2017-01-06 10:33:53 +08:00
|
|
|
if (fs::make_absolute(Abs))
|
2016-07-26 10:00:42 +08:00
|
|
|
return Path;
|
|
|
|
path::remove_dots(Abs, /*remove_dot_dot=*/true);
|
|
|
|
|
|
|
|
// This is Windows specific. root_name() returns a drive letter
|
|
|
|
// (e.g. "c:") or a UNC name (//net). We want to keep it as part
|
|
|
|
// of the result.
|
|
|
|
SmallString<128> Res;
|
|
|
|
StringRef Root = path::root_name(Abs);
|
|
|
|
if (Root.endswith(":"))
|
|
|
|
Res = Root.drop_back();
|
|
|
|
else if (Root.startswith("//"))
|
|
|
|
Res = Root.substr(2);
|
|
|
|
|
|
|
|
path::append(Res, path::relative_path(Abs));
|
2017-01-09 09:47:15 +08:00
|
|
|
return path::convert_to_slash(Res);
|
2016-07-26 10:00:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Quote a given string if it contains a space character.
|
|
|
|
std::string lld::quote(StringRef S) {
|
2017-07-20 05:40:26 +08:00
|
|
|
if (S.contains(' '))
|
|
|
|
return ("\"" + S + "\"").str();
|
|
|
|
return S;
|
2016-07-26 10:00:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string lld::rewritePath(StringRef S) {
|
|
|
|
if (fs::exists(S))
|
|
|
|
return relativeToRoot(S);
|
|
|
|
return S;
|
|
|
|
}
|
|
|
|
|
2017-12-06 00:50:46 +08:00
|
|
|
std::string lld::toString(const opt::Arg &Arg) {
|
|
|
|
std::string K = Arg.getSpelling();
|
|
|
|
if (Arg.getNumValues() == 0)
|
2016-07-26 10:00:42 +08:00
|
|
|
return K;
|
2017-12-06 00:50:46 +08:00
|
|
|
std::string V = quote(Arg.getValue());
|
|
|
|
if (Arg.getOption().getRenderStyle() == opt::Option::RenderJoinedStyle)
|
2016-07-26 10:00:42 +08:00
|
|
|
return K + V;
|
|
|
|
return K + " " + V;
|
|
|
|
}
|