blob: 4d071a78c3df5282b534fcbff4200af40fcf4c4d [file] [log] [blame]
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
3#include <stdio.h>
4#include <stdlib.h>
5
6#include <string>
7#include <vector>
8
9#include "class_linker.h"
10#include "class_loader.h"
11#include "compiler.h"
12#include "image_writer.h"
13#include "runtime.h"
14#include "stringpiece.h"
15
16namespace art {
17
18static void usage() {
19 fprintf(stderr,
20 "Usage: dex2oat [options]...\n"
21 "\n");
22 fprintf(stderr,
23 " --dex-file=<dex-file>: specifies a .dex files to compile. At least one .dex\n"
24 " but more than one may be included. \n"
25 " Example: --dex-file=/system/framework/core.jar\n"
26 "\n");
27 fprintf(stderr,
28 " --image=<file>: specifies the required output image filename.\n"
29 " Example: --image=/system/framework/boot.oat\n"
30 "\n");
31 fprintf(stderr,
32 " --base=<hex-address>: specifies the base address when creating a boot image.\n"
33 " Example: --base=0x50000000\n"
34 "\n");
35 fprintf(stderr,
36 " --boot=<oat-file>: provide the oat file for the boot class path.\n"
37 " Example: --boot=/system/framework/boot.oat\n"
38 "\n");
39 // TODO: remove this by making boot image contain boot DexFile information?
40 fprintf(stderr,
41 " --boot-dex-file=<dex-file>: specifies a .dex file that is part of the boot\n"
42 " image specified with --boot. \n"
43 " Example: --boot-dex-file=/system/framework/core.jar\n"
44 "\n");
45 fprintf(stderr,
46 " --method may be used to limit compilation to a subset of methods.\n"
47 " Example: --method=Ljava/lang/Object;<init>()V\n"
48 "\n");
49 exit(EXIT_FAILURE);
50}
51
52static void OpenDexFiles(std::vector<const char*>& dex_filenames,
53 std::vector<const DexFile*>& dex_files) {
54 for (size_t i = 0; i < dex_filenames.size(); i++) {
55 const char* dex_filename = dex_filenames[i];
56 const DexFile* dex_file = DexFile::Open(dex_filename);
57 if (dex_file == NULL) {
58 fprintf(stderr, "could not open .dex from file %s\n", dex_filename);
59 exit(EXIT_FAILURE);
60 }
61 dex_files.push_back(dex_file);
62 }
63}
64
65int dex2oat(int argc, char** argv) {
66 // Skip over argv[0].
67 argv++;
68 argc--;
69
70 if (argc == 0) {
71 fprintf(stderr, "no arguments specified\n");
72 usage();
73 }
74
75 std::vector<const char*> dex_filenames;
76 std::vector<const char*> method_names;
77 const char* image_filename = NULL;
78 std::string boot_image_option;
79 std::vector<const char*> boot_dex_filenames;
80 uintptr_t image_base = 0;
81 for (int i = 0; i < argc; i++) {
82 const StringPiece option(argv[i]);
83 if (option.starts_with("--dex-file=")) {
84 dex_filenames.push_back(option.substr(strlen("--dex-file=")).data());
85 } else if (option.starts_with("--method=")) {
86 method_names.push_back(option.substr(strlen("--method=")).data());
87 } else if (option.starts_with("--image=")) {
88 image_filename = option.substr(strlen("--image=")).data();
89 } else if (option.starts_with("--base=")) {
90 const char* image_base_str = option.substr(strlen("--base=")).data();
91 char* end;
92 image_base = strtoul(image_base_str, &end, 16);
93 if (end == image_base_str || *end != '\0') {
94 fprintf(stderr, "could not parse hexadecimal value for option %s\n", option.data());
95 usage();
96 }
97 } else if (option.starts_with("--boot=")) {
98 const char* boot_image_filename = option.substr(strlen("--boot=")).data();
99 boot_image_option.clear();
100 boot_image_option += "-Xbootimage:";
101 boot_image_option += boot_image_filename;
102 } else if (option.starts_with("--boot-dex-file=")) {
103 boot_dex_filenames.push_back(option.substr(strlen("--boot-dex-file=")).data());
104 } else {
105 fprintf(stderr, "unknown argument %s\n", option.data());
106 usage();
107 }
108 }
109
110 if (image_filename == NULL) {
111 fprintf(stderr, "--image file name not specified\n");
112 return EXIT_FAILURE;
113 }
114
115 if (boot_image_option.empty()) {
116 if (image_base == 0) {
117 fprintf(stderr, "non-zero --base not specified\n");
118 return EXIT_FAILURE;
119 }
120 } else {
121 if (boot_dex_filenames.empty()) {
122 fprintf(stderr, "no --boot-dex-file specified with --boot\n");
123 return EXIT_FAILURE;
124 }
125 }
126
127 std::vector<const DexFile*> dex_files;
128 OpenDexFiles(dex_filenames, dex_files);
129
130 std::vector<const DexFile*> boot_dex_files;
131 OpenDexFiles(boot_dex_filenames, boot_dex_files);
132
133 Runtime::Options options;
134 if (boot_image_option.empty()) {
135 options.push_back(std::make_pair("bootclasspath", &dex_files));
136 } else {
137 options.push_back(std::make_pair("bootclasspath", &boot_dex_files));
138 options.push_back(std::make_pair(boot_image_option.c_str(), reinterpret_cast<void*>(NULL)));
139 }
140 UniquePtr<Runtime> runtime(Runtime::Create(options, false));
141 if (runtime.get() == NULL) {
142 fprintf(stderr, "could not create runtime\n");
143 return EXIT_FAILURE;
144 }
145 ClassLinker* class_linker = runtime->GetClassLinker();
146
147 // If we have an existing boot image, position new space after it
148 if (!boot_image_option.empty()) {
149 Space* boot_space = Heap::GetBootSpace();
150 CHECK(boot_space != NULL);
151 image_base = RoundUp(reinterpret_cast<uintptr_t>(boot_space->GetLimit()), kPageSize);
152 }
153
154 // ClassLoader creation needs to come after Runtime::Create
155 const ClassLoader* class_loader;
156 if (boot_image_option.empty()) {
157 class_loader = NULL;
158 } else {
159 for (size_t i = 0; i < dex_files.size(); i++) {
160 class_linker->RegisterDexFile(*dex_files[i]);
161 }
162 class_loader = PathClassLoader::Alloc(dex_files);
163 }
164
165 Compiler compiler;
166 if (method_names.empty()) {
167 compiler.CompileAll(class_loader);
168 } else {
169 for (size_t i = 0; i < method_names.size(); i++) {
170 // names are actually class_descriptor + name + signature.
171 // example: Ljava/lang/Object;<init>()V
172 StringPiece method_name = method_names[i];
173 size_t end_of_class_descriptor = method_name.find(';');
174 if (end_of_class_descriptor == method_name.npos) {
175 fprintf(stderr, "could not find class descriptor in method %s\n", method_name.data());
176 return EXIT_FAILURE;
177 }
178 end_of_class_descriptor++; // want to include ;
179 std::string class_descriptor = method_name.substr(0, end_of_class_descriptor).ToString();
180 size_t end_of_name = method_name.find('(', end_of_class_descriptor);
181 if (end_of_name == method_name.npos) {
182 fprintf(stderr, "could not find start of method signature in method %s\n", method_name.data());
183 return EXIT_FAILURE;
184 }
185 std::string name = method_name.substr(end_of_class_descriptor,
186 end_of_name - end_of_class_descriptor).ToString();
187 std::string signature = method_name.substr(end_of_name).ToString();
188
189 Class* klass = class_linker->FindClass(class_descriptor, class_loader);
190 if (klass == NULL) {
191 fprintf(stderr, "could not find class for descriptor %s in method %s\n",
192 class_descriptor.c_str(), method_name.data());
193 return EXIT_FAILURE;
194 }
195 Method* method = klass->FindDirectMethod(name, signature);
196 if (method == NULL) {
197 method = klass->FindVirtualMethod(name, signature);
198 }
199 if (method == NULL) {
200 fprintf(stderr, "could not find method %s with signature %s in class %s for method argument %s\n",
201 name.c_str(),
202 signature.c_str(),
203 class_descriptor.c_str(),
204 method_name.data());
205 return EXIT_FAILURE;
206 }
207 compiler.CompileOne(method);
208 }
209 }
210
211 ImageWriter writer;
212 if (!writer.Write(image_filename, image_base)) {
213 fprintf(stderr, "could not write image %s\n", image_filename);
214 return EXIT_FAILURE;
215 }
216
217 return EXIT_SUCCESS;
218}
219
220} // namespace art
221
222int main(int argc, char** argv) {
223 return art::dex2oat(argc, argv);
224}