Introduce sky_packager
sky_packager is a tool that generates a Dart script snapshot for Dart files intended to run with dart:sky. I plan to use this tool to experiment with startup performance. R=jackson@google.com Review URL: https://codereview.chromium.org/1178213003.
This commit is contained in:
@@ -14,6 +14,7 @@ group("sky") {
|
||||
"//sky/engine/web:sky_unittests",
|
||||
"//sky/engine/wtf:unittests",
|
||||
"//sky/tools/imagediff",
|
||||
"//sky/tools/packager($host_toolchain)",
|
||||
"//sky/tools/tester",
|
||||
"//sky/viewer",
|
||||
":sky_apk",
|
||||
|
||||
28
engine/src/flutter/tools/packager/BUILD.gn
Normal file
28
engine/src/flutter/tools/packager/BUILD.gn
Normal file
@@ -0,0 +1,28 @@
|
||||
# Copyright 2014 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.
|
||||
|
||||
executable("packager") {
|
||||
output_name = "sky_packager"
|
||||
|
||||
sources = [
|
||||
"loader.cc",
|
||||
"loader.h",
|
||||
"logging.cc",
|
||||
"logging.h",
|
||||
"main.cc",
|
||||
"scopes.h",
|
||||
"switches.cc",
|
||||
"switches.h",
|
||||
"vm.cc",
|
||||
"vm.h",
|
||||
]
|
||||
|
||||
deps = [
|
||||
"//base",
|
||||
"//dart/runtime/vm:libdart_platform",
|
||||
"//dart/runtime:libdart",
|
||||
"//sky/engine/bindings:snapshot_cc",
|
||||
"//third_party/zlib",
|
||||
]
|
||||
}
|
||||
138
engine/src/flutter/tools/packager/loader.cc
Normal file
138
engine/src/flutter/tools/packager/loader.cc
Normal file
@@ -0,0 +1,138 @@
|
||||
// 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/packager/loader.h"
|
||||
|
||||
#include <utility>
|
||||
|
||||
#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 "sky/tools/packager/logging.h"
|
||||
#include "sky/tools/packager/scope.h"
|
||||
#include "sky/tools/packager/switches.h"
|
||||
|
||||
namespace {
|
||||
|
||||
static const char kSkyInternalsLibraryName[] = "dart:sky.internals";
|
||||
static const char kSkyInternalsURL[] = "package:sky/internals.dart";
|
||||
|
||||
std::string Fetch(const std::string& url) {
|
||||
base::FilePath path(url);
|
||||
std::string source;
|
||||
CHECK(base::ReadFileToString(path, &source)) << url;
|
||||
return source;
|
||||
}
|
||||
|
||||
base::FilePath SimplifyPath(const base::FilePath& path) {
|
||||
std::vector<base::FilePath::StringType> components;
|
||||
path.GetComponents(&components);
|
||||
base::FilePath result;
|
||||
for (const auto& component : components) {
|
||||
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(base::FilePath package_root);
|
||||
|
||||
std::string CanonicalizePackageURL(std::string url);
|
||||
Dart_Handle CanonicalizeURL(Dart_Handle library, Dart_Handle url);
|
||||
Dart_Handle Import(Dart_Handle url);
|
||||
Dart_Handle Source(Dart_Handle library, Dart_Handle url);
|
||||
|
||||
private:
|
||||
base::FilePath package_root_;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(Loader);
|
||||
};
|
||||
|
||||
Loader::Loader(base::FilePath package_root)
|
||||
: package_root_(std::move(package_root)) {
|
||||
}
|
||||
|
||||
std::string Loader::CanonicalizePackageURL(std::string url) {
|
||||
DCHECK(StartsWithASCII(url, "package:", true));
|
||||
ReplaceFirstSubstringAfterOffset(&url, 0, "package:", "");
|
||||
return package_root_.Append(url).AsUTF8Unsafe();
|
||||
}
|
||||
|
||||
Dart_Handle Loader::CanonicalizeURL(Dart_Handle library, Dart_Handle url) {
|
||||
std::string string = StringFromDart(url);
|
||||
if (StartsWithASCII(string, "dart:", true))
|
||||
return url;
|
||||
if (StartsWithASCII(string, "package:", true))
|
||||
return StringToDart(CanonicalizePackageURL(std::move(string)));
|
||||
base::FilePath base_path(StringFromDart(Dart_LibraryUrl(library)));
|
||||
base::FilePath resolved_path = base_path.DirName().Append(string);
|
||||
base::FilePath normalized_path = SimplifyPath(resolved_path);
|
||||
return StringToDart(normalized_path.AsUTF8Unsafe());
|
||||
}
|
||||
|
||||
Dart_Handle Loader::Import(Dart_Handle url) {
|
||||
Dart_Handle source = StringToDart(Fetch(StringFromDart(url)));
|
||||
Dart_Handle result = Dart_LoadLibrary(url, source, 0, 0);
|
||||
LogIfError(result);
|
||||
return result;
|
||||
}
|
||||
|
||||
Dart_Handle Loader::Source(Dart_Handle library, Dart_Handle url) {
|
||||
Dart_Handle source = StringToDart(Fetch(StringFromDart(url)));
|
||||
Dart_Handle result = Dart_LoadSource(library, url, source, 0, 0);
|
||||
LogIfError(result);
|
||||
return result;
|
||||
}
|
||||
|
||||
Loader* g_loader = nullptr;
|
||||
|
||||
Loader& GetLoader() {
|
||||
if (!g_loader) {
|
||||
base::CommandLine& command_line = *base::CommandLine::ForCurrentProcess();
|
||||
CHECK(command_line.HasSwitch(kPackageRoot)) << "Need --package-root";
|
||||
g_loader = new Loader(command_line.GetSwitchValuePath(kPackageRoot));
|
||||
}
|
||||
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 LoadSkyInternals() {
|
||||
DartApiScope api_scope;
|
||||
|
||||
Dart_Handle library_name = StringToDart(kSkyInternalsLibraryName);
|
||||
std::string url = GetLoader().CanonicalizePackageURL(kSkyInternalsURL);
|
||||
LogIfError(Dart_LoadLibrary(library_name, StringToDart(Fetch(url)), 0, 0));
|
||||
}
|
||||
|
||||
void LoadScript(std::string url) {
|
||||
LogIfError(
|
||||
Dart_LoadScript(StringToDart(url), StringToDart(Fetch(url)), 0, 0));
|
||||
}
|
||||
18
engine/src/flutter/tools/packager/loader.h
Normal file
18
engine/src/flutter/tools/packager/loader.h
Normal file
@@ -0,0 +1,18 @@
|
||||
// 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_PACKAGER_LOADER_H_
|
||||
#define SKY_TOOLS_PACKAGER_LOADER_H_
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "dart/runtime/include/dart_api.h"
|
||||
|
||||
Dart_Handle HandleLibraryTag(Dart_LibraryTag tag,
|
||||
Dart_Handle library,
|
||||
Dart_Handle url);
|
||||
void LoadSkyInternals();
|
||||
void LoadScript(std::string url);
|
||||
|
||||
#endif // SKY_TOOLS_PACKAGER_LOADER_H_
|
||||
37
engine/src/flutter/tools/packager/logging.cc
Normal file
37
engine/src/flutter/tools/packager/logging.cc
Normal file
@@ -0,0 +1,37 @@
|
||||
// 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/packager/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());
|
||||
}
|
||||
16
engine/src/flutter/tools/packager/logging.h
Normal file
16
engine/src/flutter/tools/packager/logging.h
Normal file
@@ -0,0 +1,16 @@
|
||||
// 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_PACKAGER_LOGGING_H_
|
||||
#define SKY_TOOLS_PACKAGER_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_PACKAGER_LOGGING_H_
|
||||
58
engine/src/flutter/tools/packager/main.cc
Normal file
58
engine/src/flutter/tools/packager/main.cc
Normal file
@@ -0,0 +1,58 @@
|
||||
// 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 <iostream>
|
||||
|
||||
#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/packager/loader.h"
|
||||
#include "sky/tools/packager/logging.h"
|
||||
#include "sky/tools/packager/scope.h"
|
||||
#include "sky/tools/packager/switches.h"
|
||||
#include "sky/tools/packager/vm.h"
|
||||
|
||||
void WriteSnapshot(base::FilePath path) {
|
||||
uint8_t* buffer;
|
||||
intptr_t size;
|
||||
CHECK(!LogIfError(Dart_CreateScriptSnapshot(&buffer, &size)));
|
||||
|
||||
CHECK_EQ(base::WriteFile(path, reinterpret_cast<const char*>(buffer), size),
|
||||
size);
|
||||
|
||||
std::cout << "Successfully wrote snapshot to " << path.LossyDisplayName()
|
||||
<< " (" << size << " bytes)." << std::endl;
|
||||
}
|
||||
|
||||
int main(int argc, const char* argv[]) {
|
||||
base::AtExitManager exit_manager;
|
||||
base::EnableTerminationOnHeapCorruption();
|
||||
base::CommandLine::Init(argc, argv);
|
||||
|
||||
InitDartVM();
|
||||
Dart_Isolate isolate = CreateDartIsolate();
|
||||
CHECK(isolate);
|
||||
|
||||
const base::CommandLine& command_line =
|
||||
*base::CommandLine::ForCurrentProcess();
|
||||
|
||||
DartIsolateScope scope(isolate);
|
||||
DartApiScope api_scope;
|
||||
|
||||
auto args = command_line.GetArgs();
|
||||
CHECK(args.size() == 1);
|
||||
LoadScript(args[0]);
|
||||
|
||||
CHECK(!LogIfError(Dart_FinalizeLoading(true)));
|
||||
|
||||
CHECK(command_line.HasSwitch(kSnapshot)) << "Need --snapshot";
|
||||
WriteSnapshot(command_line.GetSwitchValuePath(kSnapshot));
|
||||
|
||||
return 0;
|
||||
}
|
||||
33
engine/src/flutter/tools/packager/scope.h
Normal file
33
engine/src/flutter/tools/packager/scope.h
Normal file
@@ -0,0 +1,33 @@
|
||||
// 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_PACKAGER_SCOPE_H_
|
||||
#define SKY_TOOLS_PACKAGER_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_PACKAGER_SCOPE_H_
|
||||
8
engine/src/flutter/tools/packager/switches.cc
Normal file
8
engine/src/flutter/tools/packager/switches.cc
Normal file
@@ -0,0 +1,8 @@
|
||||
// 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/packager/switches.h"
|
||||
|
||||
const char kPackageRoot[] = "package-root";
|
||||
const char kSnapshot[] = "snapshot";
|
||||
11
engine/src/flutter/tools/packager/switches.h
Normal file
11
engine/src/flutter/tools/packager/switches.h
Normal file
@@ -0,0 +1,11 @@
|
||||
// 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_PACKAGER_SWITCHES_H_
|
||||
#define SKY_TOOLS_PACKAGER_SWITCHES_H_
|
||||
|
||||
extern const char kPackageRoot[];
|
||||
extern const char kSnapshot[];
|
||||
|
||||
#endif // SKY_TOOLS_PACKAGER_SWITCHES_H_
|
||||
43
engine/src/flutter/tools/packager/vm.cc
Normal file
43
engine/src/flutter/tools/packager/vm.cc
Normal file
@@ -0,0 +1,43 @@
|
||||
// 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/packager/vm.h"
|
||||
|
||||
#include "base/logging.h"
|
||||
#include "sky/tools/packager/loader.h"
|
||||
#include "sky/tools/packager/logging.h"
|
||||
|
||||
namespace blink {
|
||||
extern const uint8_t* kDartVmIsolateSnapshotBuffer;
|
||||
extern const uint8_t* kDartIsolateSnapshotBuffer;
|
||||
}
|
||||
|
||||
void InitDartVM() {
|
||||
int argc = 0;
|
||||
const char** argv = nullptr;
|
||||
|
||||
CHECK(Dart_SetVMFlags(argc, argv));
|
||||
CHECK(Dart_Initialize(blink::kDartVmIsolateSnapshotBuffer,
|
||||
nullptr, // Isolate created callback.
|
||||
nullptr, // Isolate interrupt callback.
|
||||
nullptr, // Unhandled exception callback.
|
||||
nullptr, // Isolate shutdown callback.
|
||||
// File IO callbacks.
|
||||
nullptr, nullptr, nullptr, nullptr, nullptr));
|
||||
}
|
||||
|
||||
Dart_Isolate CreateDartIsolate() {
|
||||
CHECK(blink::kDartIsolateSnapshotBuffer);
|
||||
char* error = nullptr;
|
||||
Dart_Isolate isolate =
|
||||
Dart_CreateIsolate("http://example.com", "main",
|
||||
blink::kDartIsolateSnapshotBuffer, nullptr, &error);
|
||||
|
||||
CHECK(isolate) << error;
|
||||
CHECK(!LogIfError(Dart_SetLibraryTagHandler(HandleLibraryTag)));
|
||||
LoadSkyInternals();
|
||||
|
||||
Dart_ExitIsolate();
|
||||
return isolate;
|
||||
}
|
||||
13
engine/src/flutter/tools/packager/vm.h
Normal file
13
engine/src/flutter/tools/packager/vm.h
Normal file
@@ -0,0 +1,13 @@
|
||||
// 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_PACKAGER_VM_H_
|
||||
#define SKY_TOOLS_PACKAGER_VM_H_
|
||||
|
||||
#include "dart/runtime/include/dart_api.h"
|
||||
|
||||
void InitDartVM();
|
||||
Dart_Isolate CreateDartIsolate();
|
||||
|
||||
#endif // SKY_TOOLS_PACKAGER_VM_H_
|
||||
Reference in New Issue
Block a user