Remove //base dependency from sky_snapshot (flutter/engine#2872)
This commit is contained in:
2
DEPS
2
DEPS
@@ -53,7 +53,7 @@ deps = {
|
||||
Var('fuchsia_git') + '/ftl' + '@' + 'e9ebb15d179fed9467e5fb3ce6fb807cc4ad9a8b',
|
||||
|
||||
'src/lib/tonic':
|
||||
Var('fuchsia_git') + '/tonic' + '@' + '18fa4dec7495e0bafc9e3e6c1783e726c032d987',
|
||||
Var('fuchsia_git') + '/tonic' + '@' + '325db6a9d6d32351e86843e5db0dfaad5f8651bf',
|
||||
|
||||
'src/third_party/gtest':
|
||||
Var('fuchsia_git') + '/third_party/gtest' + '@' + 'c00f82917331efbbd27124b537e4ccc915a02b72',
|
||||
|
||||
@@ -4,25 +4,16 @@
|
||||
|
||||
executable("sky_snapshot") {
|
||||
sources = [
|
||||
"loader.cc",
|
||||
"loader.h",
|
||||
"logging.cc",
|
||||
"logging.h",
|
||||
"main.cc",
|
||||
"scope.h",
|
||||
"switches.cc",
|
||||
"switches.h",
|
||||
"vm.cc",
|
||||
"vm.h",
|
||||
]
|
||||
|
||||
deps = [
|
||||
"//base",
|
||||
"//dart/runtime:libdart",
|
||||
"//dart/runtime/vm:libdart_platform",
|
||||
"//lib/tonic/parsers",
|
||||
"//lib/ftl",
|
||||
"//lib/tonic/converter",
|
||||
"//lib/tonic/file_loader",
|
||||
"//sky/engine/bindings:snapshot_cc",
|
||||
"//third_party/zlib",
|
||||
]
|
||||
|
||||
if (!is_ios && !is_mac) {
|
||||
|
||||
@@ -1,242 +0,0 @@
|
||||
// Copyright 2015 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "sky/tools/sky_snapshot/loader.h"
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "base/command_line.h"
|
||||
#include "base/files/file_path.h"
|
||||
#include "base/files/file_util.h"
|
||||
#include "base/logging.h"
|
||||
#include "base/strings/string_util.h"
|
||||
#include "lib/tonic/parsers/packages_map.h"
|
||||
#include "sky/tools/sky_snapshot/logging.h"
|
||||
#include "sky/tools/sky_snapshot/scope.h"
|
||||
#include "sky/tools/sky_snapshot/switches.h"
|
||||
|
||||
namespace {
|
||||
|
||||
// Extract the scheme prefix ('package:' or 'file:' from )
|
||||
static std::string ExtractSchemePrefix(std::string url) {
|
||||
if (base::StartsWithASCII(url, "package:", true)) {
|
||||
return "package:";
|
||||
} else if (base::StartsWithASCII(url, "file:", true)) {
|
||||
return "file:";
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
// Extract the path from a package: or file: url.
|
||||
static std::string ExtractPath(std::string url) {
|
||||
if (base::StartsWithASCII(url, "package:", true)) {
|
||||
base::ReplaceFirstSubstringAfterOffset(&url, 0, "package:", "");
|
||||
} else if (base::StartsWithASCII(url, "file:", true)) {
|
||||
base::ReplaceFirstSubstringAfterOffset(&url, 0, "file:", "");
|
||||
}
|
||||
return url;
|
||||
}
|
||||
|
||||
base::FilePath SimplifyPath(const base::FilePath& path) {
|
||||
std::vector<base::FilePath::StringType> components;
|
||||
path.GetComponents(&components);
|
||||
auto it = components.begin();
|
||||
base::FilePath result(*it++);
|
||||
for (; it != components.end(); it++) {
|
||||
auto& component = *it;
|
||||
if (component == base::FilePath::kCurrentDirectory)
|
||||
continue;
|
||||
if (component == base::FilePath::kParentDirectory)
|
||||
result = result.DirName();
|
||||
else
|
||||
result = result.Append(component);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
class Loader {
|
||||
public:
|
||||
Loader();
|
||||
|
||||
void LoadPackagesMap(const base::FilePath& packages);
|
||||
|
||||
const std::set<std::string>& dependencies() const { return dependencies_; }
|
||||
|
||||
Dart_Handle CanonicalizeURL(Dart_Handle library, Dart_Handle url);
|
||||
base::FilePath GetFilePathForURL(std::string url);
|
||||
base::FilePath GetFilePathForPackageURL(std::string url);
|
||||
base::FilePath GetFilePathForFileURL(std::string url);
|
||||
std::string Fetch(const std::string& url, std::string* resolved_url);
|
||||
Dart_Handle Import(Dart_Handle url);
|
||||
Dart_Handle Source(Dart_Handle library, Dart_Handle url);
|
||||
|
||||
void set_package_root(const base::FilePath& package_root) {
|
||||
package_root_ = package_root;
|
||||
}
|
||||
|
||||
private:
|
||||
std::set<std::string> dependencies_;
|
||||
base::FilePath packages_;
|
||||
base::FilePath package_root_;
|
||||
std::unique_ptr<tonic::PackagesMap> packages_map_;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(Loader);
|
||||
};
|
||||
|
||||
Loader::Loader() {}
|
||||
|
||||
void Loader::LoadPackagesMap(const base::FilePath& packages) {
|
||||
packages_ = packages;
|
||||
dependencies_.insert(packages_.AsUTF8Unsafe());
|
||||
std::string packages_source;
|
||||
if (!base::ReadFileToString(packages_, &packages_source)) {
|
||||
fprintf(stderr, "error: Unable to load .packages file '%s'.\n",
|
||||
packages_.AsUTF8Unsafe().c_str());
|
||||
exit(1);
|
||||
}
|
||||
packages_map_.reset(new tonic::PackagesMap());
|
||||
std::string error;
|
||||
if (!packages_map_->Parse(packages_source, &error)) {
|
||||
fprintf(stderr, "error: Unable to parse .packages file '%s'.\n%s\n",
|
||||
packages_.AsUTF8Unsafe().c_str(), error.c_str());
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
Dart_Handle Loader::CanonicalizeURL(Dart_Handle library, Dart_Handle url) {
|
||||
std::string string = StringFromDart(url);
|
||||
if (base::StartsWithASCII(string, "dart:", true))
|
||||
return url;
|
||||
if (base::StartsWithASCII(string, "package:", true))
|
||||
return url;
|
||||
if (base::StartsWithASCII(string, "file:", true)) {
|
||||
base::ReplaceFirstSubstringAfterOffset(&string, 0, "file:", "");
|
||||
return StringToDart(string);
|
||||
;
|
||||
}
|
||||
|
||||
std::string library_url = StringFromDart(Dart_LibraryUrl(library));
|
||||
std::string prefix = ExtractSchemePrefix(library_url);
|
||||
std::string path = ExtractPath(library_url);
|
||||
base::FilePath base_path(path);
|
||||
base::FilePath resolved_path = base_path.DirName().Append(string);
|
||||
base::FilePath normalized_path = SimplifyPath(resolved_path);
|
||||
return StringToDart(prefix + normalized_path.AsUTF8Unsafe());
|
||||
}
|
||||
|
||||
base::FilePath Loader::GetFilePathForURL(std::string url) {
|
||||
if (base::StartsWithASCII(url, "package:", true))
|
||||
return GetFilePathForPackageURL(url);
|
||||
if (base::StartsWithASCII(url, "file:", true))
|
||||
return GetFilePathForFileURL(url);
|
||||
|
||||
return base::FilePath(url);
|
||||
}
|
||||
|
||||
base::FilePath Loader::GetFilePathForPackageURL(std::string url) {
|
||||
DCHECK(base::StartsWithASCII(url, "package:", true));
|
||||
base::ReplaceFirstSubstringAfterOffset(&url, 0, "package:", "");
|
||||
size_t slash = url.find('/');
|
||||
if (slash == std::string::npos)
|
||||
return base::FilePath();
|
||||
std::string package = url.substr(0, slash);
|
||||
std::string library_path = url.substr(slash + 1);
|
||||
std::string package_path = packages_map_->Resolve(package);
|
||||
if (package_path.empty())
|
||||
return base::FilePath();
|
||||
if (base::StartsWithASCII(package_path, "file://", true)) {
|
||||
base::ReplaceFirstSubstringAfterOffset(&package_path, 0, "file://", "");
|
||||
return base::FilePath(package_path + library_path);
|
||||
}
|
||||
return packages_.DirName().Append(package_path).Append(library_path);
|
||||
}
|
||||
|
||||
base::FilePath Loader::GetFilePathForFileURL(std::string url) {
|
||||
DCHECK(base::StartsWithASCII(url, "file://", true));
|
||||
base::ReplaceFirstSubstringAfterOffset(&url, 0, "file://", "");
|
||||
return base::FilePath(url);
|
||||
}
|
||||
|
||||
std::string Loader::Fetch(const std::string& url, std::string* resolved_url) {
|
||||
base::FilePath path = GetFilePathForURL(url);
|
||||
base::FilePath absolute_path = base::MakeAbsoluteFilePath(path);
|
||||
*resolved_url = "file://" + absolute_path.value();
|
||||
std::string source;
|
||||
if (!base::ReadFileToString(absolute_path, &source)) {
|
||||
fprintf(stderr, "error: Unable to find Dart library '%s'.\n", url.c_str());
|
||||
exit(1);
|
||||
}
|
||||
dependencies_.insert(path.AsUTF8Unsafe());
|
||||
return source;
|
||||
}
|
||||
|
||||
Dart_Handle Loader::Import(Dart_Handle url) {
|
||||
std::string resolved_url;
|
||||
Dart_Handle source = StringToDart(Fetch(StringFromDart(url), &resolved_url));
|
||||
Dart_Handle result =
|
||||
Dart_LoadLibrary(url, StringToDart(resolved_url), source, 0, 0);
|
||||
LogIfError(result);
|
||||
return result;
|
||||
}
|
||||
|
||||
Dart_Handle Loader::Source(Dart_Handle library, Dart_Handle url) {
|
||||
std::string resolved_url;
|
||||
Dart_Handle source = StringToDart(Fetch(StringFromDart(url), &resolved_url));
|
||||
Dart_Handle result =
|
||||
Dart_LoadSource(library, url, StringToDart(resolved_url), source, 0, 0);
|
||||
LogIfError(result);
|
||||
return result;
|
||||
}
|
||||
|
||||
Loader* g_loader = nullptr;
|
||||
|
||||
Loader& GetLoader() {
|
||||
if (!g_loader) {
|
||||
base::CommandLine& command_line = *base::CommandLine::ForCurrentProcess();
|
||||
g_loader = new Loader();
|
||||
if (command_line.HasSwitch(switches::kPackages)) {
|
||||
g_loader->LoadPackagesMap(
|
||||
command_line.GetSwitchValuePath(switches::kPackages));
|
||||
|
||||
} else if (command_line.HasSwitch(switches::kPackageRoot)) {
|
||||
g_loader->set_package_root(
|
||||
command_line.GetSwitchValuePath(switches::kPackageRoot));
|
||||
} else {
|
||||
fprintf(stderr, "error: Need either --packages or --package-root.\n");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
return *g_loader;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
Dart_Handle HandleLibraryTag(Dart_LibraryTag tag,
|
||||
Dart_Handle library,
|
||||
Dart_Handle url) {
|
||||
CHECK(Dart_IsLibrary(library));
|
||||
CHECK(Dart_IsString(url));
|
||||
|
||||
if (tag == Dart_kCanonicalizeUrl)
|
||||
return GetLoader().CanonicalizeURL(library, url);
|
||||
|
||||
if (tag == Dart_kImportTag)
|
||||
return GetLoader().Import(url);
|
||||
|
||||
if (tag == Dart_kSourceTag)
|
||||
return GetLoader().Source(library, url);
|
||||
|
||||
return Dart_NewApiError("Unknown library tag.");
|
||||
}
|
||||
|
||||
void LoadScript(const std::string& url) {
|
||||
std::string resolved_url;
|
||||
Dart_Handle source = StringToDart(GetLoader().Fetch(url, &resolved_url));
|
||||
LogIfError(Dart_LoadScript(StringToDart(url), StringToDart(resolved_url),
|
||||
source, 0, 0));
|
||||
}
|
||||
|
||||
const std::set<std::string>& GetDependencies() {
|
||||
return GetLoader().dependencies();
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
// Copyright 2015 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef SKY_TOOLS_SKY_SNAPSHOT_LOADER_H_
|
||||
#define SKY_TOOLS_SKY_SNAPSHOT_LOADER_H_
|
||||
|
||||
#include <set>
|
||||
#include <string>
|
||||
|
||||
#include "dart/runtime/include/dart_api.h"
|
||||
|
||||
Dart_Handle HandleLibraryTag(Dart_LibraryTag tag,
|
||||
Dart_Handle library,
|
||||
Dart_Handle url);
|
||||
void LoadScript(const std::string& url);
|
||||
const std::set<std::string>& GetDependencies();
|
||||
|
||||
#endif // SKY_TOOLS_SKY_SNAPSHOT_LOADER_H_
|
||||
@@ -1,37 +0,0 @@
|
||||
// Copyright 2015 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "sky/tools/sky_snapshot/logging.h"
|
||||
|
||||
#include "base/logging.h"
|
||||
|
||||
bool LogIfError(Dart_Handle handle) {
|
||||
if (Dart_IsError(handle)) {
|
||||
LOG(ERROR) << Dart_GetError(handle);
|
||||
|
||||
// Only unhandled exceptions have stacktraces.
|
||||
if (!Dart_ErrorHasException(handle))
|
||||
return true;
|
||||
|
||||
Dart_Handle stacktrace = Dart_ErrorGetStacktrace(handle);
|
||||
const char* stacktrace_cstr = "";
|
||||
Dart_StringToCString(Dart_ToString(stacktrace), &stacktrace_cstr);
|
||||
LOG(ERROR) << stacktrace_cstr;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string StringFromDart(Dart_Handle string) {
|
||||
CHECK(Dart_IsString(string));
|
||||
uint8_t* utf8_array;
|
||||
intptr_t length;
|
||||
Dart_StringToUTF8(string, &utf8_array, &length);
|
||||
return std::string(reinterpret_cast<const char*>(utf8_array), length);
|
||||
}
|
||||
|
||||
Dart_Handle StringToDart(const std::string& string) {
|
||||
return Dart_NewStringFromUTF8(reinterpret_cast<const uint8_t*>(string.data()),
|
||||
string.length());
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
// Copyright 2015 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef SKY_TOOLS_SKY_SNAPSHOT_LOGGING_H_
|
||||
#define SKY_TOOLS_SKY_SNAPSHOT_LOGGING_H_
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "dart/runtime/include/dart_api.h"
|
||||
|
||||
bool LogIfError(Dart_Handle handle);
|
||||
std::string StringFromDart(Dart_Handle string);
|
||||
Dart_Handle StringToDart(const std::string& string);
|
||||
|
||||
#endif // SKY_TOOLS_SKY_SNAPSHOT_LOGGING_H_
|
||||
@@ -1,108 +1,240 @@
|
||||
// Copyright 2015 The Chromium Authors. All rights reserved.
|
||||
// Copyright 2016 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include <iostream>
|
||||
#include <fcntl.h>
|
||||
|
||||
#include <iostream>
|
||||
#include <set>
|
||||
#include <string>
|
||||
|
||||
#include "base/at_exit.h"
|
||||
#include "base/basictypes.h"
|
||||
#include "base/command_line.h"
|
||||
#include "base/files/file_path.h"
|
||||
#include "base/files/file_util.h"
|
||||
#include "base/logging.h"
|
||||
#include "base/process/memory.h"
|
||||
#include "dart/runtime/include/dart_api.h"
|
||||
#include "sky/tools/sky_snapshot/loader.h"
|
||||
#include "sky/tools/sky_snapshot/logging.h"
|
||||
#include "sky/tools/sky_snapshot/scope.h"
|
||||
#include "sky/tools/sky_snapshot/switches.h"
|
||||
#include "sky/tools/sky_snapshot/vm.h"
|
||||
#include "lib/ftl/arraysize.h"
|
||||
#include "lib/ftl/command_line.h"
|
||||
#include "lib/ftl/files/directory.h"
|
||||
#include "lib/ftl/files/eintr_wrapper.h"
|
||||
#include "lib/ftl/files/file_descriptor.h"
|
||||
#include "lib/ftl/files/file.h"
|
||||
#include "lib/ftl/files/symlink.h"
|
||||
#include "lib/ftl/files/unique_fd.h"
|
||||
#include "lib/ftl/logging.h"
|
||||
#include "lib/tonic/converter/dart_converter.h"
|
||||
#include "lib/tonic/file_loader/file_loader.h"
|
||||
|
||||
extern "C" {
|
||||
extern const uint8_t* kDartVmIsolateSnapshotBuffer;
|
||||
extern const uint8_t* kDartIsolateSnapshotBuffer;
|
||||
}
|
||||
|
||||
namespace sky_snapshot {
|
||||
namespace {
|
||||
|
||||
using tonic::ToDart;
|
||||
|
||||
constexpr char kHelp[] = "help";
|
||||
constexpr char kPackages[] = "packages";
|
||||
constexpr char kSnapshot[] = "snapshot";
|
||||
constexpr char kDepfile[] = "depfile";
|
||||
constexpr char kBuildOutput[] = "build-output";
|
||||
|
||||
const char* kDartArgs[] = {
|
||||
// clang-format off
|
||||
"--enable_mirrors=false",
|
||||
"--load_deferred_eagerly=true",
|
||||
"--conditional_directives",
|
||||
// TODO(chinmaygarde): The experimental interpreter for iOS device targets
|
||||
// does not support all these flags. The build process uses its own version
|
||||
// of this snapshotter. Till support for all these flags is added, make
|
||||
// sure the snapshotter does not error out on unrecognized flags.
|
||||
"--ignore-unrecognized-flags",
|
||||
// clang-format on
|
||||
};
|
||||
|
||||
void Usage() {
|
||||
std::cerr << R"USAGE(usage: sky_snapshot --packages=PACKAGES --snapshot=SNAPSHOT_BLOB <lib/main.dart>
|
||||
[ --depfile=DEPS_FILE ] [ --build-output=BUILD_OUTPUT ] [ --help ]
|
||||
|
||||
* PACKAGES is the '.packages' file that defines where to find Dart packages.
|
||||
* SNAPSHOT_BLOB is the file to write the snapshot into.
|
||||
* DEPS_FILE is an optional '.d' file to depedendency information into.
|
||||
* BUILD_OUTPUT optionally overrides the target name used in the DEPS_FILE.
|
||||
(The default is SNAPSHOT_BLOB.)
|
||||
)USAGE";
|
||||
std::cerr
|
||||
<< "Usage: sky_snapshot --" << kPackages << "=PACKAGES" << std::endl
|
||||
<< " [ --" << kSnapshot << "=OUTPUT_SNAPSHOT ]"
|
||||
<< std::endl
|
||||
<< " [ --" << kDepfile << "=DEPFILE ]" << std::endl
|
||||
<< " [ --" << kBuildOutput << "=BUILD_OUTPUT ]"
|
||||
<< std::endl
|
||||
<< std::endl
|
||||
<< " MAIN_DART" << std::endl
|
||||
<< " * PACKAGES is the '.packages' file that defines where to find Dart "
|
||||
"packages."
|
||||
<< std::endl
|
||||
<< " * OUTPUT_SNAPSHOT is the file to write the snapshot into."
|
||||
<< std::endl
|
||||
<< " * DEPFILE is the file into which to write the '.d' depedendency "
|
||||
"information into."
|
||||
<< std::endl
|
||||
<< " * BUILD_OUTPUT determines the target name used in the " << std::endl
|
||||
<< " DEPFILE. (Required if DEPFILE is provided.) " << std::endl;
|
||||
}
|
||||
|
||||
void WriteSnapshot(base::FilePath path) {
|
||||
uint8_t* buffer;
|
||||
intptr_t size;
|
||||
CHECK(!LogIfError(Dart_CreateScriptSnapshot(&buffer, &size)));
|
||||
class DartScope {
|
||||
public:
|
||||
DartScope(Dart_Isolate isolate) {
|
||||
Dart_EnterIsolate(isolate);
|
||||
Dart_EnterScope();
|
||||
}
|
||||
|
||||
CHECK_EQ(base::WriteFile(path, reinterpret_cast<const char*>(buffer), size),
|
||||
size);
|
||||
~DartScope() {
|
||||
Dart_ExitScope();
|
||||
Dart_ExitIsolate();
|
||||
}
|
||||
};
|
||||
|
||||
void InitDartVM() {
|
||||
FTL_CHECK(Dart_SetVMFlags(arraysize(kDartArgs), kDartArgs));
|
||||
char* error = Dart_Initialize(
|
||||
kDartVmIsolateSnapshotBuffer, nullptr, nullptr, nullptr, nullptr, nullptr,
|
||||
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr);
|
||||
if (error)
|
||||
FTL_LOG(FATAL) << error;
|
||||
}
|
||||
|
||||
void WriteDependencies(base::FilePath path,
|
||||
const std::string& build_output,
|
||||
const std::set<std::string>& deps) {
|
||||
base::FilePath current_directory;
|
||||
CHECK(base::GetCurrentDirectory(¤t_directory));
|
||||
Dart_Isolate CreateDartIsolate() {
|
||||
FTL_CHECK(kDartIsolateSnapshotBuffer);
|
||||
char* error = nullptr;
|
||||
Dart_Isolate isolate =
|
||||
Dart_CreateIsolate("dart:snapshot", "main", kDartIsolateSnapshotBuffer,
|
||||
nullptr, nullptr, &error);
|
||||
FTL_CHECK(isolate) << error;
|
||||
Dart_ExitIsolate();
|
||||
return isolate;
|
||||
}
|
||||
|
||||
tonic::FileLoader* g_loader = nullptr;
|
||||
|
||||
tonic::FileLoader& GetLoader() {
|
||||
if (!g_loader)
|
||||
g_loader = new tonic::FileLoader();
|
||||
return *g_loader;
|
||||
}
|
||||
|
||||
Dart_Handle HandleLibraryTag(Dart_LibraryTag tag,
|
||||
Dart_Handle library,
|
||||
Dart_Handle url) {
|
||||
FTL_CHECK(Dart_IsLibrary(library));
|
||||
FTL_CHECK(Dart_IsString(url));
|
||||
tonic::FileLoader& loader = GetLoader();
|
||||
if (tag == Dart_kCanonicalizeUrl)
|
||||
return loader.CanonicalizeURL(library, url);
|
||||
if (tag == Dart_kImportTag)
|
||||
return loader.Import(url);
|
||||
if (tag == Dart_kSourceTag)
|
||||
return loader.Source(library, url);
|
||||
return Dart_NewApiError("Unknown library tag.");
|
||||
}
|
||||
|
||||
std::vector<char> CreateSnapshot() {
|
||||
uint8_t* buffer = nullptr;
|
||||
intptr_t size = 0;
|
||||
DART_CHECK_VALID(Dart_CreateScriptSnapshot(&buffer, &size));
|
||||
const char* begin = reinterpret_cast<const char*>(buffer);
|
||||
return std::vector<char>(begin, begin + size);
|
||||
}
|
||||
|
||||
bool WriteDepfile(const std::string& path,
|
||||
const std::string& build_output,
|
||||
const std::set<std::string>& deps) {
|
||||
std::string current_directory = files::GetCurrentDirectory();
|
||||
std::string output = build_output + ":";
|
||||
for (const auto& i : deps) {
|
||||
output += " ";
|
||||
base::FilePath dep_path(i);
|
||||
if (dep_path.IsAbsolute()) {
|
||||
output += dep_path.MaybeAsASCII();
|
||||
for (const auto& dep : deps) {
|
||||
std::string file = dep;
|
||||
FTL_DCHECK(!file.empty());
|
||||
if (file[0] != '/')
|
||||
file = current_directory + "/" + file;
|
||||
|
||||
std::string resolved_file;
|
||||
if (files::ReadSymbolicLink(file, &resolved_file)) {
|
||||
output += " " + resolved_file;
|
||||
} else {
|
||||
output += current_directory.Append(dep_path).MaybeAsASCII();
|
||||
output += " " + file;
|
||||
}
|
||||
}
|
||||
const char* data = output.c_str();
|
||||
const intptr_t data_length = output.size();
|
||||
CHECK_EQ(base::WriteFile(path, data, data_length), data_length);
|
||||
return files::WriteFile(path, output.data(), output.size());
|
||||
}
|
||||
|
||||
int main(int argc, const char* argv[]) {
|
||||
base::AtExitManager exit_manager;
|
||||
base::EnableTerminationOnHeapCorruption();
|
||||
base::CommandLine::Init(argc, argv);
|
||||
|
||||
const base::CommandLine& command_line =
|
||||
*base::CommandLine::ForCurrentProcess();
|
||||
|
||||
if (command_line.HasSwitch(switches::kHelp) ||
|
||||
command_line.GetArgs().empty()) {
|
||||
int CreateSnapshot(const ftl::CommandLine& command_line) {
|
||||
if (command_line.HasOption(kHelp, nullptr)) {
|
||||
Usage();
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!command_line.HasSwitch(switches::kSnapshot)) {
|
||||
fprintf(stderr, "error: Need --snapshot.\n");
|
||||
exit(1);
|
||||
if (command_line.positional_args().empty()) {
|
||||
Usage();
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::string packages;
|
||||
if (!command_line.GetOptionValue(kPackages, &packages)) {
|
||||
std::cerr << "error: Need --" << kPackages << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::vector<std::string> args = command_line.positional_args();
|
||||
if (args.size() != 1) {
|
||||
std::cerr << "error: Need one position argument. Got " << args.size() << "."
|
||||
<< std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::string main_dart = args[0];
|
||||
|
||||
std::string snapshot;
|
||||
if (!command_line.GetOptionValue(kSnapshot, &snapshot)) {
|
||||
std::cerr << "error: Need --" << kSnapshot << "." << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::string depfile;
|
||||
std::string build_output;
|
||||
if (command_line.GetOptionValue(kDepfile, &depfile) &&
|
||||
!command_line.GetOptionValue(kBuildOutput, &build_output)) {
|
||||
std::cerr << "error: Need --" << kBuildOutput << " if --" << kDepfile
|
||||
<< " is specified." << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
InitDartVM();
|
||||
Dart_Isolate isolate = CreateDartIsolate();
|
||||
CHECK(isolate);
|
||||
|
||||
DartIsolateScope scope(isolate);
|
||||
DartApiScope api_scope;
|
||||
|
||||
auto args = command_line.GetArgs();
|
||||
CHECK(args.size() == 1);
|
||||
LoadScript(args[0]);
|
||||
|
||||
if (LogIfError(Dart_FinalizeLoading(true)))
|
||||
tonic::FileLoader& loader = GetLoader();
|
||||
if (!loader.LoadPackagesMap(packages))
|
||||
return 1;
|
||||
|
||||
CHECK(command_line.HasSwitch(switches::kSnapshot));
|
||||
WriteSnapshot(command_line.GetSwitchValuePath(switches::kSnapshot));
|
||||
Dart_Isolate isolate = CreateDartIsolate();
|
||||
FTL_CHECK(isolate) << "Failed to create isolate.";
|
||||
|
||||
if (command_line.HasSwitch(switches::kDepfile)) {
|
||||
auto build_output = command_line.HasSwitch(switches::kBuildOutput) ?
|
||||
command_line.GetSwitchValueASCII(switches::kBuildOutput) :
|
||||
command_line.GetSwitchValueASCII(switches::kSnapshot);
|
||||
WriteDependencies(command_line.GetSwitchValuePath(switches::kDepfile),
|
||||
build_output,
|
||||
GetDependencies());
|
||||
DartScope scope(isolate);
|
||||
|
||||
DART_CHECK_VALID(Dart_SetLibraryTagHandler(HandleLibraryTag));
|
||||
DART_CHECK_VALID(Dart_LoadScript(ToDart(main_dart), Dart_Null(),
|
||||
ToDart(loader.Fetch(main_dart)), 0, 0));
|
||||
|
||||
std::vector<char> snapshot_blob = CreateSnapshot();
|
||||
|
||||
if (!snapshot.empty() &&
|
||||
!files::WriteFile(snapshot, snapshot_blob.data(), snapshot_blob.size())) {
|
||||
std::cerr << "error: Failed to write snapshot to '" << snapshot << "'."
|
||||
<< std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (!depfile.empty() &&
|
||||
!WriteDepfile(depfile, build_output, loader.dependencies())) {
|
||||
std::cerr << "error: Failed to write depfile to '" << depfile << "'."
|
||||
<< std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
} // namespace sky_snapshot
|
||||
|
||||
int main(int argc, const char* argv[]) {
|
||||
return sky_snapshot::CreateSnapshot(ftl::CommandLineFromArgcArgv(argc, argv));
|
||||
}
|
||||
|
||||
@@ -1,33 +0,0 @@
|
||||
// Copyright 2015 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef SKY_TOOLS_SKY_SNAPSHOT_SCOPE_H_
|
||||
#define SKY_TOOLS_SKY_SNAPSHOT_SCOPE_H_
|
||||
|
||||
#include "base/basictypes.h"
|
||||
#include "base/logging.h"
|
||||
#include "dart/runtime/include/dart_api.h"
|
||||
|
||||
class DartIsolateScope {
|
||||
public:
|
||||
explicit DartIsolateScope(Dart_Isolate isolate) {
|
||||
CHECK(!Dart_CurrentIsolate());
|
||||
Dart_EnterIsolate(isolate);
|
||||
}
|
||||
~DartIsolateScope() { Dart_ExitIsolate(); }
|
||||
|
||||
private:
|
||||
DISALLOW_COPY_AND_ASSIGN(DartIsolateScope);
|
||||
};
|
||||
|
||||
class DartApiScope {
|
||||
public:
|
||||
DartApiScope() { Dart_EnterScope(); }
|
||||
~DartApiScope() { Dart_ExitScope(); }
|
||||
|
||||
private:
|
||||
DISALLOW_COPY_AND_ASSIGN(DartApiScope);
|
||||
};
|
||||
|
||||
#endif // SKY_TOOLS_SKY_SNAPSHOT_SCOPE_H_
|
||||
@@ -1,16 +0,0 @@
|
||||
// Copyright 2015 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "sky/tools/sky_snapshot/switches.h"
|
||||
|
||||
namespace switches {
|
||||
|
||||
const char kBuildOutput[] = "build-output";
|
||||
const char kDepfile[] = "depfile";
|
||||
const char kHelp[] = "help";
|
||||
const char kPackages[] = "packages";
|
||||
const char kPackageRoot[] = "package-root";
|
||||
const char kSnapshot[] = "snapshot";
|
||||
|
||||
} // namespace switches
|
||||
@@ -1,19 +0,0 @@
|
||||
// Copyright 2015 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef SKY_TOOLS_SKY_SNAPSHOT_SWITCHES_H_
|
||||
#define SKY_TOOLS_SKY_SNAPSHOT_SWITCHES_H_
|
||||
|
||||
namespace switches {
|
||||
|
||||
extern const char kBuildOutput[];
|
||||
extern const char kDepfile[];
|
||||
extern const char kHelp[];
|
||||
extern const char kPackages[];
|
||||
extern const char kPackageRoot[];
|
||||
extern const char kSnapshot[];
|
||||
|
||||
} // namespace switches
|
||||
|
||||
#endif // SKY_TOOLS_SKY_SNAPSHOT_SWITCHES_H_
|
||||
@@ -1,55 +0,0 @@
|
||||
// Copyright 2015 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "sky/tools/sky_snapshot/vm.h"
|
||||
|
||||
#include "base/logging.h"
|
||||
#include "sky/tools/sky_snapshot/loader.h"
|
||||
#include "sky/tools/sky_snapshot/logging.h"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
extern "C" {
|
||||
extern void* kDartVmIsolateSnapshotBuffer;
|
||||
extern void* kDartIsolateSnapshotBuffer;
|
||||
}
|
||||
|
||||
static const char* kDartArgs[] = {
|
||||
"--enable_mirrors=false",
|
||||
"--load_deferred_eagerly=true",
|
||||
"--conditional_directives",
|
||||
// TODO(chinmaygarde): The experimental interpreter for iOS device targets
|
||||
// does not support all these flags. The build process uses its own version
|
||||
// of this snapshotter. Till support for all these flags is added, make
|
||||
// sure the snapshotter does not error out on unrecognized flags.
|
||||
"--ignore-unrecognized-flags",
|
||||
};
|
||||
|
||||
void InitDartVM() {
|
||||
CHECK(Dart_SetVMFlags(arraysize(kDartArgs), kDartArgs));
|
||||
char* init_message = Dart_Initialize(
|
||||
reinterpret_cast<uint8_t*>(&kDartVmIsolateSnapshotBuffer), nullptr,
|
||||
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
|
||||
nullptr, nullptr, nullptr, nullptr);
|
||||
if (init_message != nullptr) {
|
||||
std::cerr << "Dart_Initialize Error: " << init_message << std::endl;
|
||||
free(init_message);
|
||||
CHECK(false);
|
||||
}
|
||||
}
|
||||
|
||||
Dart_Isolate CreateDartIsolate() {
|
||||
CHECK(kDartIsolateSnapshotBuffer);
|
||||
char* error = nullptr;
|
||||
Dart_Isolate isolate = Dart_CreateIsolate(
|
||||
"dart:snapshot", "main",
|
||||
reinterpret_cast<uint8_t*>(&kDartIsolateSnapshotBuffer), nullptr, nullptr,
|
||||
&error);
|
||||
|
||||
CHECK(isolate) << error;
|
||||
CHECK(!LogIfError(Dart_SetLibraryTagHandler(HandleLibraryTag)));
|
||||
|
||||
Dart_ExitIsolate();
|
||||
return isolate;
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
// Copyright 2015 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef SKY_TOOLS_SKY_SNAPSHOT_VM_H_
|
||||
#define SKY_TOOLS_SKY_SNAPSHOT_VM_H_
|
||||
|
||||
#include "dart/runtime/include/dart_api.h"
|
||||
|
||||
void InitDartVM();
|
||||
Dart_Isolate CreateDartIsolate();
|
||||
|
||||
#endif // SKY_TOOLS_SKY_SNAPSHOT_VM_H_
|
||||
Reference in New Issue
Block a user