bc18aebb695084a53b4e5730edb1a44c7092c0ec
[platform/upstream/nodejs.git] / deps / v8 / src / mksnapshot.cc
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.
4
5 #include <errno.h>
6 #include <signal.h>
7 #include <stdio.h>
8
9 #include "src/v8.h"
10
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"
16 #include "src/list.h"
17 #include "src/natives.h"
18 #include "src/serialize.h"
19
20
21 using namespace v8;
22
23 class SnapshotWriter {
24  public:
25   explicit SnapshotWriter(const char* snapshot_file)
26       : fp_(GetFileDescriptorOrDie(snapshot_file)),
27         startup_blob_file_(NULL) {}
28
29   ~SnapshotWriter() {
30     fclose(fp_);
31     if (startup_blob_file_) fclose(startup_blob_file_);
32   }
33
34   void SetStartupBlobFile(const char* startup_blob_file) {
35     if (startup_blob_file != NULL)
36       startup_blob_file_ = GetFileDescriptorOrDie(startup_blob_file);
37   }
38
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);
44   }
45
46  private:
47   void MaybeWriteStartupBlob(const i::Vector<const i::byte>& blob) const {
48     if (!startup_blob_file_) return;
49
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");
53       exit(1);
54     }
55   }
56
57   void WriteSnapshotFile(const i::Vector<const i::byte>& blob) const {
58     WriteFilePrefix();
59     WriteData(blob);
60     WriteFileSuffix();
61   }
62
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");
70   }
71
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");
81   }
82
83   void WriteData(const i::Vector<const i::byte>& blob) const {
84     fprintf(fp_, "static const byte blob_data[] = {\n");
85     WriteSnapshotData(blob);
86     fprintf(fp_, "};\n");
87     fprintf(fp_, "static const int blob_size = %d;\n", blob.length());
88     fprintf(fp_, "\n");
89   }
90
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)));
96     }
97     fprintf(fp_, "\n");
98   }
99
100   FILE* GetFileDescriptorOrDie(const char* filename) {
101     FILE* fp = base::OS::FOpen(filename, "wb");
102     if (fp == NULL) {
103       i::PrintF("Unable to open file \"%s\" for writing.\n", filename);
104       exit(1);
105     }
106     return fp;
107   }
108
109   FILE* fp_;
110   FILE* startup_blob_file_;
111 };
112
113
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");
118   if (file == NULL) {
119     fprintf(stderr, "Failed to open '%s': errno %d\n", filename, errno);
120     exit(1);
121   }
122   fseek(file, 0, SEEK_END);
123   int size = ftell(file);
124   rewind(file);
125   char* chars = new char[size + 1];
126   chars[size] = '\0';
127   for (int i = 0; i < size;) {
128     int read = static_cast<int>(fread(&chars[i], 1, size - i, file));
129     if (read < 0) {
130       fprintf(stderr, "Failed to read '%s': errno %d\n", filename, errno);
131       exit(1);
132     }
133     i += read;
134   }
135   fclose(file);
136   return chars;
137 }
138
139
140 int main(int argc, char** argv) {
141   // By default, log code create information in the snapshot.
142   i::FLAG_log_code = true;
143
144   // Omit from the snapshot natives for features that can be turned off
145   // at runtime.
146   i::FLAG_harmony_shipping = false;
147
148   i::FLAG_logfile_per_isolate = false;
149
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;
157   }
158
159   i::CpuFeatures::Probe(true);
160   V8::InitializeICU();
161   v8::Platform* platform = v8::platform::CreateDefaultPlatform();
162   v8::V8::InitializePlatform(platform);
163   v8::V8::Initialize();
164
165   {
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);
170     CHECK(blob.data);
171     writer.WriteSnapshot(blob);
172     delete[] extra_code;
173     delete[] blob.data;
174   }
175
176   V8::Dispose();
177   V8::ShutdownPlatform();
178   delete platform;
179   return 0;
180 }