blob: f60699c9257cddd4ca35c3581a9a35b6c5634df6 [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,
Brian Carlstrom78128a62011-09-15 17:21:19 -070023 " --dex-file=<dex-file>: specifies a .dex file to compile. At least one .dex\n"
24 " file must be specified. \n"
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070025 " 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");
Brian Carlstrom16192862011-09-12 17:50:06 -070049 fprintf(stderr,
50 " --strip-prefix may be used to strip a path prefix from dex file names in the\n"
51 " the generated image to match the target file system layout.\n"
52 " Example: --strip-prefix=out/target/product/crespo\n"
53 "\n");
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070054 exit(EXIT_FAILURE);
55}
56
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070057int dex2oat(int argc, char** argv) {
58 // Skip over argv[0].
59 argv++;
60 argc--;
61
62 if (argc == 0) {
63 fprintf(stderr, "no arguments specified\n");
64 usage();
65 }
66
67 std::vector<const char*> dex_filenames;
68 std::vector<const char*> method_names;
69 const char* image_filename = NULL;
70 std::string boot_image_option;
71 std::vector<const char*> boot_dex_filenames;
72 uintptr_t image_base = 0;
Brian Carlstrom16192862011-09-12 17:50:06 -070073 std::string strip_location_prefix;
74
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070075 for (int i = 0; i < argc; i++) {
76 const StringPiece option(argv[i]);
77 if (option.starts_with("--dex-file=")) {
78 dex_filenames.push_back(option.substr(strlen("--dex-file=")).data());
79 } else if (option.starts_with("--method=")) {
80 method_names.push_back(option.substr(strlen("--method=")).data());
81 } else if (option.starts_with("--image=")) {
82 image_filename = option.substr(strlen("--image=")).data();
83 } else if (option.starts_with("--base=")) {
84 const char* image_base_str = option.substr(strlen("--base=")).data();
85 char* end;
86 image_base = strtoul(image_base_str, &end, 16);
87 if (end == image_base_str || *end != '\0') {
88 fprintf(stderr, "could not parse hexadecimal value for option %s\n", option.data());
89 usage();
90 }
91 } else if (option.starts_with("--boot=")) {
92 const char* boot_image_filename = option.substr(strlen("--boot=")).data();
93 boot_image_option.clear();
94 boot_image_option += "-Xbootimage:";
95 boot_image_option += boot_image_filename;
96 } else if (option.starts_with("--boot-dex-file=")) {
97 boot_dex_filenames.push_back(option.substr(strlen("--boot-dex-file=")).data());
Brian Carlstrom16192862011-09-12 17:50:06 -070098 } else if (option.starts_with("--strip-prefix=")) {
99 strip_location_prefix = option.substr(strlen("--strip-prefix=")).data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700100 } else {
101 fprintf(stderr, "unknown argument %s\n", option.data());
102 usage();
103 }
104 }
105
106 if (image_filename == NULL) {
107 fprintf(stderr, "--image file name not specified\n");
108 return EXIT_FAILURE;
109 }
110
Brian Carlstrom78128a62011-09-15 17:21:19 -0700111 if (dex_filenames.empty()) {
112 fprintf(stderr, "no --dex-file values specified\n");
113 return EXIT_FAILURE;
114 }
115
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700116 if (boot_image_option.empty()) {
117 if (image_base == 0) {
118 fprintf(stderr, "non-zero --base not specified\n");
119 return EXIT_FAILURE;
120 }
121 } else {
122 if (boot_dex_filenames.empty()) {
Brian Carlstrom78128a62011-09-15 17:21:19 -0700123 fprintf(stderr, "no --boot-dex-file values specified with --boot\n");
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700124 return EXIT_FAILURE;
125 }
126 }
127
128 std::vector<const DexFile*> dex_files;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700129 DexFile::OpenDexFiles(dex_filenames, dex_files, strip_location_prefix);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700130
131 std::vector<const DexFile*> boot_dex_files;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700132 DexFile::OpenDexFiles(boot_dex_filenames, boot_dex_files, strip_location_prefix);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700133
134 Runtime::Options options;
135 if (boot_image_option.empty()) {
136 options.push_back(std::make_pair("bootclasspath", &dex_files));
137 } else {
138 options.push_back(std::make_pair("bootclasspath", &boot_dex_files));
139 options.push_back(std::make_pair(boot_image_option.c_str(), reinterpret_cast<void*>(NULL)));
140 }
141 UniquePtr<Runtime> runtime(Runtime::Create(options, false));
142 if (runtime.get() == NULL) {
143 fprintf(stderr, "could not create runtime\n");
144 return EXIT_FAILURE;
145 }
146 ClassLinker* class_linker = runtime->GetClassLinker();
147
148 // If we have an existing boot image, position new space after it
149 if (!boot_image_option.empty()) {
150 Space* boot_space = Heap::GetBootSpace();
151 CHECK(boot_space != NULL);
152 image_base = RoundUp(reinterpret_cast<uintptr_t>(boot_space->GetLimit()), kPageSize);
153 }
154
155 // ClassLoader creation needs to come after Runtime::Create
156 const ClassLoader* class_loader;
157 if (boot_image_option.empty()) {
158 class_loader = NULL;
159 } else {
160 for (size_t i = 0; i < dex_files.size(); i++) {
161 class_linker->RegisterDexFile(*dex_files[i]);
162 }
163 class_loader = PathClassLoader::Alloc(dex_files);
164 }
165
Brian Carlstrom16192862011-09-12 17:50:06 -0700166 // if we loaded an existing image, we will reuse its stub array.
167 if (!runtime->HasJniStubArray()) {
168 runtime->SetJniStubArray(JniCompiler::CreateJniStub(kThumb2));
169 }
170
Ian Rogers2c8f6532011-09-02 17:16:34 -0700171 Compiler compiler(kThumb2);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700172 if (method_names.empty()) {
173 compiler.CompileAll(class_loader);
174 } else {
175 for (size_t i = 0; i < method_names.size(); i++) {
176 // names are actually class_descriptor + name + signature.
177 // example: Ljava/lang/Object;<init>()V
178 StringPiece method_name = method_names[i];
179 size_t end_of_class_descriptor = method_name.find(';');
180 if (end_of_class_descriptor == method_name.npos) {
181 fprintf(stderr, "could not find class descriptor in method %s\n", method_name.data());
182 return EXIT_FAILURE;
183 }
184 end_of_class_descriptor++; // want to include ;
185 std::string class_descriptor = method_name.substr(0, end_of_class_descriptor).ToString();
186 size_t end_of_name = method_name.find('(', end_of_class_descriptor);
187 if (end_of_name == method_name.npos) {
188 fprintf(stderr, "could not find start of method signature in method %s\n", method_name.data());
189 return EXIT_FAILURE;
190 }
191 std::string name = method_name.substr(end_of_class_descriptor,
192 end_of_name - end_of_class_descriptor).ToString();
193 std::string signature = method_name.substr(end_of_name).ToString();
194
195 Class* klass = class_linker->FindClass(class_descriptor, class_loader);
196 if (klass == NULL) {
197 fprintf(stderr, "could not find class for descriptor %s in method %s\n",
198 class_descriptor.c_str(), method_name.data());
199 return EXIT_FAILURE;
200 }
201 Method* method = klass->FindDirectMethod(name, signature);
202 if (method == NULL) {
203 method = klass->FindVirtualMethod(name, signature);
204 }
205 if (method == NULL) {
206 fprintf(stderr, "could not find method %s with signature %s in class %s for method argument %s\n",
207 name.c_str(),
208 signature.c_str(),
209 class_descriptor.c_str(),
210 method_name.data());
211 return EXIT_FAILURE;
212 }
213 compiler.CompileOne(method);
214 }
215 }
216
217 ImageWriter writer;
218 if (!writer.Write(image_filename, image_base)) {
219 fprintf(stderr, "could not write image %s\n", image_filename);
220 return EXIT_FAILURE;
221 }
222
223 return EXIT_SUCCESS;
224}
225
226} // namespace art
227
228int main(int argc, char** argv) {
229 return art::dex2oat(argc, argv);
230}