Zachary Turner | e9f1df8 | 2018-09-05 23:30:17 +0000 | [diff] [blame] | 1 | |
| 2 | #include "llvm/Testing/Support/SupportHelpers.h" |
| 3 | |
| 4 | #include "llvm/ADT/SmallString.h" |
| 5 | #include "llvm/ADT/Twine.h" |
| 6 | #include "llvm/Support/Error.h" |
| 7 | #include "llvm/Support/FileSystem.h" |
| 8 | #include "llvm/Support/MemoryBuffer.h" |
| 9 | #include "llvm/Support/Path.h" |
| 10 | |
| 11 | #include "gtest/gtest.h" |
| 12 | |
| 13 | using namespace llvm; |
| 14 | using namespace llvm::unittest; |
| 15 | |
Zachary Turner | a0a738b | 2018-09-06 22:00:38 +0000 | [diff] [blame] | 16 | static std::pair<bool, SmallString<128>> findSrcDirMap(StringRef Argv0) { |
| 17 | SmallString<128> BaseDir = llvm::sys::path::parent_path(Argv0); |
| 18 | |
| 19 | llvm::sys::fs::make_absolute(BaseDir); |
| 20 | |
| 21 | SmallString<128> PathInSameDir = BaseDir; |
| 22 | llvm::sys::path::append(PathInSameDir, "llvm.srcdir.txt"); |
| 23 | |
| 24 | if (llvm::sys::fs::is_regular_file(PathInSameDir)) |
| 25 | return std::make_pair(true, std::move(PathInSameDir)); |
| 26 | |
| 27 | SmallString<128> PathInParentDir = llvm::sys::path::parent_path(BaseDir); |
| 28 | |
| 29 | llvm::sys::path::append(PathInParentDir, "llvm.srcdir.txt"); |
| 30 | if (llvm::sys::fs::is_regular_file(PathInParentDir)) |
| 31 | return std::make_pair(true, std::move(PathInParentDir)); |
| 32 | |
Zachary Turner | 54d898e | 2018-09-06 22:47:32 +0000 | [diff] [blame] | 33 | return std::pair<bool, SmallString<128>>(false, {}); |
Zachary Turner | a0a738b | 2018-09-06 22:00:38 +0000 | [diff] [blame] | 34 | } |
| 35 | |
Fangrui Song | 95dd7a2 | 2018-09-06 19:51:20 +0000 | [diff] [blame] | 36 | SmallString<128> llvm::unittest::getInputFileDirectory(const char *Argv0) { |
Zachary Turner | a0a738b | 2018-09-06 22:00:38 +0000 | [diff] [blame] | 37 | bool Found = false; |
| 38 | SmallString<128> InputFilePath; |
| 39 | std::tie(Found, InputFilePath) = findSrcDirMap(Argv0); |
Zachary Turner | e9f1df8 | 2018-09-05 23:30:17 +0000 | [diff] [blame] | 40 | |
Zachary Turner | a0a738b | 2018-09-06 22:00:38 +0000 | [diff] [blame] | 41 | EXPECT_TRUE(Found) << "Unit test source directory file does not exist."; |
Zachary Turner | e9f1df8 | 2018-09-05 23:30:17 +0000 | [diff] [blame] | 42 | |
Zachary Turner | a0a738b | 2018-09-06 22:00:38 +0000 | [diff] [blame] | 43 | auto File = MemoryBuffer::getFile(InputFilePath); |
Zachary Turner | e9f1df8 | 2018-09-05 23:30:17 +0000 | [diff] [blame] | 44 | |
| 45 | EXPECT_TRUE(static_cast<bool>(File)) |
| 46 | << "Could not open unit test source directory file."; |
| 47 | |
Zachary Turner | a0a738b | 2018-09-06 22:00:38 +0000 | [diff] [blame] | 48 | InputFilePath.clear(); |
| 49 | InputFilePath.append((*File)->getBuffer().trim()); |
| 50 | llvm::sys::path::append(InputFilePath, "Inputs"); |
| 51 | llvm::sys::path::native(InputFilePath); |
| 52 | return InputFilePath; |
Zachary Turner | e9f1df8 | 2018-09-05 23:30:17 +0000 | [diff] [blame] | 53 | } |