1 //===--- DarwinSDKInfo.cpp - SDK Information parser for darwin - ----------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "clang/Driver/DarwinSDKInfo.h"
10 #include "llvm/Support/ErrorOr.h"
11 #include "llvm/Support/JSON.h"
12 #include "llvm/Support/MemoryBuffer.h"
13 #include "llvm/Support/Path.h"
15 using namespace clang::driver;
16 using namespace clang;
18 Expected<Optional<DarwinSDKInfo>>
19 driver::parseDarwinSDKInfo(llvm::vfs::FileSystem &VFS, StringRef SDKRootPath) {
20 llvm::SmallString<256> Filepath = SDKRootPath;
21 llvm::sys::path::append(Filepath, "SDKSettings.json");
22 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> File =
23 VFS.getBufferForFile(Filepath);
25 // If the file couldn't be read, assume it just doesn't exist.
28 Expected<llvm::json::Value> Result =
29 llvm::json::parse(File.get()->getBuffer());
31 return Result.takeError();
33 if (const auto *Obj = Result->getAsObject()) {
34 auto VersionString = Obj->getString("Version");
37 if (!Version.tryParse(*VersionString))
38 return DarwinSDKInfo(Version);
41 return llvm::make_error<llvm::StringError>("invalid SDKSettings.json",
42 llvm::inconvertibleErrorCode());