1 // Copyright 2006-2008 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
11 #include "include/libplatform/libplatform.h"
12 #include "src/assembler.h"
13 #include "src/base/platform/platform.h"
14 #include "src/bootstrapper.h"
15 #include "src/flags.h"
17 #include "src/natives.h"
18 #include "src/serialize.h"
23 class SnapshotWriter {
25 explicit SnapshotWriter(const char* snapshot_file)
26 : fp_(GetFileDescriptorOrDie(snapshot_file)),
27 startup_blob_file_(NULL) {}
31 if (startup_blob_file_) fclose(startup_blob_file_);
34 void SetStartupBlobFile(const char* startup_blob_file) {
35 if (startup_blob_file != NULL)
36 startup_blob_file_ = GetFileDescriptorOrDie(startup_blob_file);
39 void WriteSnapshot(v8::StartupData blob) const {
40 i::Vector<const i::byte> blob_vector(
41 reinterpret_cast<const i::byte*>(blob.data), blob.raw_size);
42 WriteSnapshotFile(blob_vector);
43 MaybeWriteStartupBlob(blob_vector);
47 void MaybeWriteStartupBlob(const i::Vector<const i::byte>& blob) const {
48 if (!startup_blob_file_) return;
50 size_t written = fwrite(blob.begin(), 1, blob.length(), startup_blob_file_);
51 if (written != static_cast<size_t>(blob.length())) {
52 i::PrintF("Writing snapshot file failed.. Aborting.\n");
57 void WriteSnapshotFile(const i::Vector<const i::byte>& blob) const {
63 void WriteFilePrefix() const {
64 fprintf(fp_, "// Autogenerated snapshot file. Do not edit.\n\n");
65 fprintf(fp_, "#include \"src/v8.h\"\n");
66 fprintf(fp_, "#include \"src/base/platform/platform.h\"\n\n");
67 fprintf(fp_, "#include \"src/snapshot.h\"\n\n");
68 fprintf(fp_, "namespace v8 {\n");
69 fprintf(fp_, "namespace internal {\n\n");
72 void WriteFileSuffix() const {
73 fprintf(fp_, "const v8::StartupData Snapshot::SnapshotBlob() {\n");
74 fprintf(fp_, " v8::StartupData blob;\n");
75 fprintf(fp_, " blob.data = reinterpret_cast<const char*>(blob_data);\n");
76 fprintf(fp_, " blob.raw_size = blob_size;\n");
77 fprintf(fp_, " return blob;\n");
78 fprintf(fp_, "}\n\n");
79 fprintf(fp_, "} // namespace internal\n");
80 fprintf(fp_, "} // namespace v8\n");
83 void WriteData(const i::Vector<const i::byte>& blob) const {
84 fprintf(fp_, "static const byte blob_data[] = {\n");
85 WriteSnapshotData(blob);
87 fprintf(fp_, "static const int blob_size = %d;\n", blob.length());
91 void WriteSnapshotData(const i::Vector<const i::byte>& blob) const {
92 for (int i = 0; i < blob.length(); i++) {
93 if ((i & 0x1f) == 0x1f) fprintf(fp_, "\n");
94 if (i > 0) fprintf(fp_, ",");
95 fprintf(fp_, "%u", static_cast<unsigned char>(blob.at(i)));
100 FILE* GetFileDescriptorOrDie(const char* filename) {
101 FILE* fp = base::OS::FOpen(filename, "wb");
103 i::PrintF("Unable to open file \"%s\" for writing.\n", filename);
110 FILE* startup_blob_file_;
114 char* GetExtraCode(char* filename) {
115 if (filename == NULL || strlen(filename) == 0) return NULL;
116 ::printf("Embedding extra script: %s\n", filename);
117 FILE* file = base::OS::FOpen(filename, "rb");
119 fprintf(stderr, "Failed to open '%s': errno %d\n", filename, errno);
122 fseek(file, 0, SEEK_END);
123 int size = ftell(file);
125 char* chars = new char[size + 1];
127 for (int i = 0; i < size;) {
128 int read = static_cast<int>(fread(&chars[i], 1, size - i, file));
130 fprintf(stderr, "Failed to read '%s': errno %d\n", filename, errno);
140 int main(int argc, char** argv) {
141 // By default, log code create information in the snapshot.
142 i::FLAG_log_code = true;
144 // Omit from the snapshot natives for features that can be turned off
146 i::FLAG_harmony_shipping = false;
148 i::FLAG_logfile_per_isolate = false;
150 // Print the usage if an error occurs when parsing the command line
151 // flags or if the help flag is set.
152 int result = i::FlagList::SetFlagsFromCommandLine(&argc, argv, true);
153 if (result > 0 || (argc != 2 && argc != 3) || i::FLAG_help) {
154 ::printf("Usage: %s [flag] ... outfile\n", argv[0]);
155 i::FlagList::PrintHelp();
156 return !i::FLAG_help;
159 i::CpuFeatures::Probe(true);
161 v8::Platform* platform = v8::platform::CreateDefaultPlatform();
162 v8::V8::InitializePlatform(platform);
163 v8::V8::Initialize();
166 SnapshotWriter writer(argv[1]);
167 if (i::FLAG_startup_blob) writer.SetStartupBlobFile(i::FLAG_startup_blob);
168 char* extra_code = GetExtraCode(argc == 3 ? argv[2] : NULL);
169 StartupData blob = v8::V8::CreateSnapshotDataBlob(extra_code);
171 writer.WriteSnapshot(blob);
177 V8::ShutdownPlatform();