DarwinSDKInfo.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. //===--- DarwinSDKInfo.cpp - SDK Information parser for darwin - ----------===//
  2. //
  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
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "clang/Driver/DarwinSDKInfo.h"
  9. #include "llvm/Support/ErrorOr.h"
  10. #include "llvm/Support/JSON.h"
  11. #include "llvm/Support/MemoryBuffer.h"
  12. #include "llvm/Support/Path.h"
  13. using namespace clang::driver;
  14. using namespace clang;
  15. Expected<Optional<DarwinSDKInfo>>
  16. driver::parseDarwinSDKInfo(llvm::vfs::FileSystem &VFS, StringRef SDKRootPath) {
  17. llvm::SmallString<256> Filepath = SDKRootPath;
  18. llvm::sys::path::append(Filepath, "SDKSettings.json");
  19. llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> File =
  20. VFS.getBufferForFile(Filepath);
  21. if (!File) {
  22. // If the file couldn't be read, assume it just doesn't exist.
  23. return None;
  24. }
  25. Expected<llvm::json::Value> Result =
  26. llvm::json::parse(File.get()->getBuffer());
  27. if (!Result)
  28. return Result.takeError();
  29. if (const auto *Obj = Result->getAsObject()) {
  30. auto VersionString = Obj->getString("Version");
  31. if (VersionString) {
  32. VersionTuple Version;
  33. if (!Version.tryParse(*VersionString))
  34. return DarwinSDKInfo(Version);
  35. }
  36. }
  37. return llvm::make_error<llvm::StringError>("invalid SDKSettings.json",
  38. llvm::inconvertibleErrorCode());
  39. }