teak-llvm/clang/lib/Driver/DarwinSDKInfo.cpp
Alex Lorenz 0a264f3928 [darwin] parse the SDK settings from SDKSettings.json if it exists and
pass in the -target-sdk-version to the compiler and backend

This commit adds support for reading the SDKSettings.json file in the Darwin
driver. This file is used by the driver to determine the SDK's version, and it
uses that information to pass it down to the compiler using the new
-target-sdk-version= option. This option is then used to set the appropriate
SDK Version module metadata introduced in r349119.

Note: I had to adjust the two ast tests as the SDKROOT environment variable
on macOS caused SDK version to be picked up for the compilation of source file
but not the AST.

rdar://45774000

Differential Revision: https://reviews.llvm.org/D55673

llvm-svn: 349380
2018-12-17 19:19:15 +00:00

45 lines
1.5 KiB
C++

//===--- DarwinSDKInfo.cpp - SDK Information parser for darwin - ----------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "clang/Driver/DarwinSDKInfo.h"
#include "llvm/Support/ErrorOr.h"
#include "llvm/Support/JSON.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/Path.h"
using namespace clang::driver;
using namespace clang;
Expected<Optional<DarwinSDKInfo>>
driver::parseDarwinSDKInfo(llvm::vfs::FileSystem &VFS, StringRef SDKRootPath) {
llvm::SmallString<256> Filepath = SDKRootPath;
llvm::sys::path::append(Filepath, "SDKSettings.json");
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> File =
VFS.getBufferForFile(Filepath);
if (!File) {
// If the file couldn't be read, assume it just doesn't exist.
return None;
}
Expected<llvm::json::Value> Result =
llvm::json::parse(File.get()->getBuffer());
if (!Result)
return Result.takeError();
if (const auto *Obj = Result->getAsObject()) {
auto VersionString = Obj->getString("Version");
if (VersionString) {
VersionTuple Version;
if (!Version.tryParse(*VersionString))
return DarwinSDKInfo(Version);
}
}
return llvm::make_error<llvm::StringError>("invalid SDKSettings.json",
llvm::inconvertibleErrorCode());
}