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