]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/clang/lib/Driver/DarwinSDKInfo.cpp
Merge llvm, clang, compiler-rt, libc++, libunwind, lld, lldb and openmp
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / clang / lib / Driver / DarwinSDKInfo.cpp
1 //===--- DarwinSDKInfo.cpp - SDK Information parser for darwin - ----------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "clang/Driver/DarwinSDKInfo.h"
11 #include "llvm/Support/ErrorOr.h"
12 #include "llvm/Support/JSON.h"
13 #include "llvm/Support/MemoryBuffer.h"
14 #include "llvm/Support/Path.h"
15
16 using namespace clang::driver;
17 using namespace clang;
18
19 Expected<Optional<DarwinSDKInfo>>
20 driver::parseDarwinSDKInfo(llvm::vfs::FileSystem &VFS, StringRef SDKRootPath) {
21   llvm::SmallString<256> Filepath = SDKRootPath;
22   llvm::sys::path::append(Filepath, "SDKSettings.json");
23   llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> File =
24       VFS.getBufferForFile(Filepath);
25   if (!File) {
26     // If the file couldn't be read, assume it just doesn't exist.
27     return None;
28   }
29   Expected<llvm::json::Value> Result =
30       llvm::json::parse(File.get()->getBuffer());
31   if (!Result)
32     return Result.takeError();
33
34   if (const auto *Obj = Result->getAsObject()) {
35     auto VersionString = Obj->getString("Version");
36     if (VersionString) {
37       VersionTuple Version;
38       if (!Version.tryParse(*VersionString))
39         return DarwinSDKInfo(Version);
40     }
41   }
42   return llvm::make_error<llvm::StringError>("invalid SDKSettings.json",
43                                              llvm::inconvertibleErrorCode());
44 }