blob: da07dbdbf49660aa73c2f4b7a48b7daaa1b80308 [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
Brian Carlstromae826982011-11-09 01:33:42 -08006#include <iostream>
7#include <fstream>
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07008#include <string>
9#include <vector>
10
11#include "class_linker.h"
12#include "class_loader.h"
13#include "compiler.h"
Ian Rogers5e863dd2011-11-02 20:00:10 -070014#include "file.h"
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070015#include "image_writer.h"
Ian Rogers6f1dfe42011-12-08 17:28:34 -080016#include "leb128.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070017#include "oat_writer.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080018#include "object_utils.h"
Ian Rogers5e863dd2011-11-02 20:00:10 -070019#include "os.h"
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070020#include "runtime.h"
21#include "stringpiece.h"
22
23namespace art {
24
25static void usage() {
26 fprintf(stderr,
27 "Usage: dex2oat [options]...\n"
28 "\n");
29 fprintf(stderr,
Brian Carlstrom78128a62011-09-15 17:21:19 -070030 " --dex-file=<dex-file>: specifies a .dex file to compile. At least one .dex\n"
31 " file must be specified. \n"
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070032 " Example: --dex-file=/system/framework/core.jar\n"
33 "\n");
34 fprintf(stderr,
Brian Carlstromae826982011-11-09 01:33:42 -080035 " --oat=<file.oat>: specifies the required oat filename.\n"
36 " Example: --oat=/data/art-cache/boot.oat\n"
37 "\n");
38 fprintf(stderr,
39 " --image=<file.art>: specifies the output image filename.\n"
Brian Carlstrom47a0d5a2011-10-12 21:20:05 -070040 " Example: --image=/data/art-cache/boot.art\n"
Brian Carlstrome24fa612011-09-29 00:53:55 -070041 "\n");
Brian Carlstrome24fa612011-09-29 00:53:55 -070042 fprintf(stderr,
Brian Carlstromae826982011-11-09 01:33:42 -080043 " --image-classes=<classname-file>: specifies classes to include in an image.\n"
44 " Example: --image=frameworks/base/preloaded-classes\n"
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070045 "\n");
46 fprintf(stderr,
47 " --base=<hex-address>: specifies the base address when creating a boot image.\n"
48 " Example: --base=0x50000000\n"
49 "\n");
50 fprintf(stderr,
Brian Carlstrome24fa612011-09-29 00:53:55 -070051 " --boot-image=<file.art>: provide the image file for the boot class path.\n"
Brian Carlstrom47a0d5a2011-10-12 21:20:05 -070052 " Example: --boot-image=/data/art-cache/boot.art\n"
Brian Carlstromb0011262011-12-09 12:17:24 -080053 " Default: <host-prefix>/data/art-cache/boot.art\n"
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070054 "\n");
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070055 fprintf(stderr,
Brian Carlstrom58ae9412011-10-04 00:56:06 -070056 " --host-prefix may be used to translate host paths to target paths during\n"
57 " cross compilation.\n"
58 " Example: --host-prefix=out/target/product/crespo\n"
Brian Carlstromb0011262011-12-09 12:17:24 -080059 " Default: $ANDROID_PRODUCT_OUT\n"
Brian Carlstrom16192862011-09-12 17:50:06 -070060 "\n");
Brian Carlstrom27ec9612011-09-19 20:20:38 -070061 fprintf(stderr,
jeffhao5d840402011-10-24 17:09:45 -070062 " --runtime-arg <argument>: used to specify various arguments for the runtime,\n"
63 " such as initial heap size, maximum heap size, and verbose output.\n"
64 " Use a separate --runtime-arg switch for each argument.\n"
65 " Example: --runtime-arg -Xms256m\n"
Brian Carlstrom27ec9612011-09-19 20:20:38 -070066 "\n");
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070067 exit(EXIT_FAILURE);
68}
69
Brian Carlstromae826982011-11-09 01:33:42 -080070class Dex2Oat {
71 public:
72
73 static Dex2Oat* Create(Runtime::Options& options) {
74 UniquePtr<Runtime> runtime(CreateRuntime(options));
75 if (runtime.get() == NULL) {
76 return NULL;
77 }
78 return new Dex2Oat(runtime.release());
79 }
80
81 ~Dex2Oat() {
82 delete runtime_;
83 }
84
85 // Make a list of descriptors for classes to include in the image
86 const std::set<std::string>* GetImageClassDescriptors(const char* image_classes_filename) {
87 UniquePtr<std::ifstream> image_classes_file(new std::ifstream(image_classes_filename, std::ifstream::in));
88 if (image_classes_file.get() == NULL) {
89 LOG(ERROR) << "Failed to open image classes file " << image_classes_filename;
90 return NULL;
91 }
92
Ian Rogers6f1dfe42011-12-08 17:28:34 -080093 // Load all the classes specified in the file
Brian Carlstromae826982011-11-09 01:33:42 -080094 ClassLinker* class_linker = runtime_->GetClassLinker();
95 while (image_classes_file->good()) {
96 std::string dot;
97 std::getline(*image_classes_file.get(), dot);
98 if (StringPiece(dot).starts_with("#") || dot.empty()) {
99 continue;
100 }
Elliott Hughes95572412011-12-13 18:14:20 -0800101 std::string descriptor(DotToDescriptor(dot.c_str()));
Brian Carlstromae826982011-11-09 01:33:42 -0800102 SirtRef<Class> klass(class_linker->FindSystemClass(descriptor));
103 if (klass.get() == NULL) {
104 LOG(WARNING) << "Failed to find class " << descriptor;
105 Thread::Current()->ClearException();
106 }
107 }
108 image_classes_file->close();
109
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800110 // Resolve exception classes referenced by the loaded classes. The catch logic assumes
111 // exceptions are resolved by the verifier when there is a catch block in an interested method.
112 // Do this here so that exception classes appear to have been specified image classes.
113 std::set<std::pair<uint16_t, const DexFile*> > unresolved_exception_types;
114 do {
115 unresolved_exception_types.clear();
116 class_linker->VisitClasses(ResolveCatchBlockExceptionsClassVisitor,
117 &unresolved_exception_types);
118 typedef std::set<std::pair<uint16_t, const DexFile*> >::const_iterator It; // TODO: C++0x auto
119 for (It it = unresolved_exception_types.begin(),
120 end = unresolved_exception_types.end();
121 it != end; ++it) {
122 uint16_t exception_type_idx = it->first;
123 const DexFile* dex_file = it->second;
124 DexCache* dex_cache = class_linker->FindDexCache(*dex_file);
125 ClassLoader* class_loader = NULL;
126 SirtRef<Class> klass(class_linker->ResolveType(*dex_file, exception_type_idx, dex_cache,
127 class_loader));
128 if (klass.get() == NULL) {
129 const DexFile::TypeId& type_id = dex_file->GetTypeId(exception_type_idx);
130 const char* descriptor = dex_file->GetTypeDescriptor(type_id);
131 LOG(FATAL) << "Failed to resolve class " << descriptor;
132 }
133 DCHECK(klass->IsThrowableClass());
134 }
135 // Resolving exceptions may load classes that reference more exceptions, iterate until no
136 // more are found
137 } while (!unresolved_exception_types.empty());
138
Brian Carlstromae826982011-11-09 01:33:42 -0800139 // We walk the roots looking for classes so that we'll pick up the
140 // above classes plus any classes them depend on such super
141 // classes, interfaces, and the required ClassLinker roots.
142 UniquePtr<std::set<std::string> > image_classes(new std::set<std::string>());
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800143 class_linker->VisitClasses(RecordImageClassesVisitor, image_classes.get());
Brian Carlstromae826982011-11-09 01:33:42 -0800144 CHECK_NE(image_classes->size(), 0U);
145 return image_classes.release();
146 }
147
148 bool CreateOatFile(const std::string& boot_image_option,
149 const std::vector<const char*>& dex_filenames,
150 const std::string& host_prefix,
151 File* oat_file,
152 bool image,
153 const std::set<std::string>* image_classes) {
154 // SirtRef and ClassLoader creation needs to come after Runtime::Create
155 UniquePtr<SirtRef<ClassLoader> > class_loader(new SirtRef<ClassLoader>(NULL));
156 if (class_loader.get() == NULL) {
157 LOG(ERROR) << "Failed to create SirtRef for class loader";
158 return false;
159 }
160
161 std::vector<const DexFile*> dex_files;
162 if (!boot_image_option.empty()) {
163 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
164 DexFile::OpenDexFiles(dex_filenames, dex_files, host_prefix);
165 std::vector<const DexFile*> class_path_files(dex_files);
166 OpenClassPathFiles(runtime_->GetClassPath(), class_path_files);
167 for (size_t i = 0; i < class_path_files.size(); i++) {
168 class_linker->RegisterDexFile(*class_path_files[i]);
169 }
170 class_loader.get()->reset(PathClassLoader::AllocCompileTime(class_path_files));
171 } else {
172 dex_files = runtime_->GetClassLinker()->GetBootClassPath();
173 }
174
175 Compiler compiler(instruction_set_, image, image_classes);
176 compiler.CompileAll(class_loader->get(), dex_files);
177
178 if (!OatWriter::Create(oat_file, class_loader->get(), compiler)) {
179 LOG(ERROR) << "Failed to create oat file " << oat_file->name();
180 return false;
181 }
182 return true;
183 }
184
185 bool CreateImageFile(const char* image_filename,
186 uintptr_t image_base,
187 const std::set<std::string>* image_classes,
188 const std::string& oat_filename,
189 const std::string& host_prefix) {
190 // If we have an existing boot image, position new space after its oat file
191 if (Heap::GetSpaces().size() > 1) {
192 Space* last_image_space = Heap::GetSpaces()[Heap::GetSpaces().size()-2];
193 CHECK(last_image_space != NULL);
194 CHECK(last_image_space->IsImageSpace());
195 CHECK(!Heap::GetSpaces()[Heap::GetSpaces().size()-1]->IsImageSpace());
196 byte* oat_limit_addr = last_image_space->GetImageHeader().GetOatLimitAddr();
197 image_base = RoundUp(reinterpret_cast<uintptr_t>(oat_limit_addr), kPageSize);
198 }
199
200 ImageWriter image_writer(image_classes);
201 if (!image_writer.Write(image_filename, image_base, oat_filename, host_prefix)) {
202 LOG(ERROR) << "Failed to create image file " << image_filename;
203 return false;
204 }
205 return true;
206 }
207
208 private:
209
210 Dex2Oat(Runtime* runtime) : runtime_(runtime) {}
211
212 static Runtime* CreateRuntime(Runtime::Options& options) {
213 Runtime* runtime = Runtime::Create(options, false);
214 if (runtime == NULL) {
215 LOG(ERROR) << "Failed to create runtime";
216 return NULL;
217 }
218
219 // if we loaded an existing image, we will reuse values from the image roots.
220 if (!runtime->HasJniDlsymLookupStub()) {
221 runtime->SetJniDlsymLookupStub(Compiler::CreateJniDlysmLookupStub(instruction_set_));
222 }
223 if (!runtime->HasAbstractMethodErrorStubArray()) {
224 runtime->SetAbstractMethodErrorStubArray(Compiler::CreateAbstractMethodErrorStub(instruction_set_));
225 }
226 for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) {
227 Runtime::TrampolineType type = Runtime::TrampolineType(i);
228 if (!runtime->HasResolutionStubArray(type)) {
229 runtime->SetResolutionStubArray(Compiler::CreateResolutionStub(instruction_set_, type), type);
230 }
231 }
232 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
233 Runtime::CalleeSaveType type = Runtime::CalleeSaveType(i);
234 if (!runtime->HasCalleeSaveMethod(type)) {
235 runtime->SetCalleeSaveMethod(runtime->CreateCalleeSaveMethod(instruction_set_, type), type);
236 }
237 }
238 return runtime;
239 }
240
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800241 static void ResolveExceptionsForMethod(MethodHelper* mh,
242 std::set<std::pair<uint16_t, const DexFile*> >& exceptions_to_resolve) {
243 const DexFile::CodeItem* code_item = mh->GetCodeItem();
244 if (code_item == NULL) {
245 return; // native or abstract method
246 }
247 if (code_item->tries_size_ == 0) {
248 return; // nothing to process
249 }
250 const byte* encoded_catch_handler_list = DexFile::GetCatchHandlerData(*code_item, 0);
251 size_t num_encoded_catch_handlers = DecodeUnsignedLeb128(&encoded_catch_handler_list);
252 for (size_t i = 0; i < num_encoded_catch_handlers; i++) {
253 int32_t encoded_catch_handler_size = DecodeSignedLeb128(&encoded_catch_handler_list);
254 bool has_catch_all = false;
255 if (encoded_catch_handler_size <= 0) {
256 encoded_catch_handler_size = -encoded_catch_handler_size;
257 has_catch_all = true;
258 }
259 for (int32_t j = 0; j < encoded_catch_handler_size; j++) {
260 uint16_t encoded_catch_handler_handlers_type_idx =
261 DecodeUnsignedLeb128(&encoded_catch_handler_list);
262 // Add to set of types to resolve if not already in the dex cache resolved types
263 if (!mh->IsResolvedTypeIdx(encoded_catch_handler_handlers_type_idx)) {
264 exceptions_to_resolve.insert(
265 std::pair<uint16_t, const DexFile*>(encoded_catch_handler_handlers_type_idx,
266 &mh->GetDexFile()));
267 }
268 // ignore address associated with catch handler
269 DecodeUnsignedLeb128(&encoded_catch_handler_list);
270 }
271 if (has_catch_all) {
272 // ignore catch all address
273 DecodeUnsignedLeb128(&encoded_catch_handler_list);
274 }
275 }
276 }
277 static bool ResolveCatchBlockExceptionsClassVisitor(Class* c, void* arg) {
278 std::set<std::pair<uint16_t, const DexFile*> >* exceptions_to_resolve =
279 reinterpret_cast<std::set<std::pair<uint16_t, const DexFile*> >*>(arg);
280 MethodHelper mh;
281 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
282 Method* m = c->GetVirtualMethod(i);
283 mh.ChangeMethod(m);
284 ResolveExceptionsForMethod(&mh, *exceptions_to_resolve);
285 }
286 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
287 Method* m = c->GetDirectMethod(i);
288 mh.ChangeMethod(m);
289 ResolveExceptionsForMethod(&mh, *exceptions_to_resolve);
290 }
291 return true;
292 }
293 static bool RecordImageClassesVisitor(Class* klass, void* arg) {
Brian Carlstromae826982011-11-09 01:33:42 -0800294 std::set<std::string>* image_classes = reinterpret_cast<std::set<std::string>*>(arg);
295 if (klass->IsArrayClass() || klass->IsPrimitive()) {
Jesse Wilson254db0f2011-11-16 16:44:11 -0500296 return true;
297 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800298 image_classes->insert(ClassHelper(klass).GetDescriptor());
Brian Carlstromae826982011-11-09 01:33:42 -0800299 return true;
Jesse Wilson254db0f2011-11-16 16:44:11 -0500300 }
Jesse Wilson254db0f2011-11-16 16:44:11 -0500301
Brian Carlstromae826982011-11-09 01:33:42 -0800302 // Appends to dex_files any elements of class_path that it doesn't already
303 // contain. This will open those dex files as necessary.
304 static void OpenClassPathFiles(const std::string& class_path, std::vector<const DexFile*>& dex_files) {
305 std::vector<std::string> parsed;
306 Split(class_path, ':', parsed);
307 for (size_t i = 0; i < parsed.size(); ++i) {
308 if (DexFilesContains(dex_files, parsed[i])) {
309 continue;
310 }
311 const DexFile* dex_file = DexFile::Open(parsed[i], Runtime::Current()->GetHostPrefix());
312 if (dex_file == NULL) {
313 LOG(WARNING) << "Failed to open dex file " << parsed[i];
314 } else {
315 dex_files.push_back(dex_file);
316 }
Jesse Wilson254db0f2011-11-16 16:44:11 -0500317 }
318 }
Brian Carlstromae826982011-11-09 01:33:42 -0800319
320 // Returns true if dex_files has a dex with the named location.
321 static bool DexFilesContains(const std::vector<const DexFile*>& dex_files, const std::string& location) {
322 for (size_t i = 0; i < dex_files.size(); ++i) {
323 if (dex_files[i]->GetLocation() == location) {
324 return true;
325 }
326 }
327 return false;
328 }
329
330 Runtime* runtime_;
331 static const InstructionSet instruction_set_ = kThumb2;
332
333 DISALLOW_IMPLICIT_CONSTRUCTORS(Dex2Oat);
334};
Jesse Wilson254db0f2011-11-16 16:44:11 -0500335
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700336int dex2oat(int argc, char** argv) {
337 // Skip over argv[0].
338 argv++;
339 argc--;
340
341 if (argc == 0) {
342 fprintf(stderr, "no arguments specified\n");
343 usage();
344 }
345
346 std::vector<const char*> dex_filenames;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700347 std::string oat_filename;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700348 const char* image_filename = NULL;
Brian Carlstromae826982011-11-09 01:33:42 -0800349 const char* image_classes_filename = NULL;
Brian Carlstromb0011262011-12-09 12:17:24 -0800350 std::string boot_image_filename;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700351 uintptr_t image_base = 0;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700352 std::string host_prefix;
jeffhao5d840402011-10-24 17:09:45 -0700353 std::vector<const char*> runtime_args;
Brian Carlstrom16192862011-09-12 17:50:06 -0700354
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700355 for (int i = 0; i < argc; i++) {
356 const StringPiece option(argv[i]);
Brian Carlstromb7bbba42011-10-13 14:58:47 -0700357 if (false) {
358 LOG(INFO) << "dex2oat: option[" << i << "]=" << argv[i];
359 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700360 if (option.starts_with("--dex-file=")) {
361 dex_filenames.push_back(option.substr(strlen("--dex-file=")).data());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700362 } else if (option.starts_with("--oat=")) {
363 oat_filename = option.substr(strlen("--oat=")).data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700364 } else if (option.starts_with("--image=")) {
365 image_filename = option.substr(strlen("--image=")).data();
Brian Carlstromae826982011-11-09 01:33:42 -0800366 } else if (option.starts_with("--image-classes=")) {
367 image_classes_filename = option.substr(strlen("--image-classes=")).data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700368 } else if (option.starts_with("--base=")) {
369 const char* image_base_str = option.substr(strlen("--base=")).data();
370 char* end;
371 image_base = strtoul(image_base_str, &end, 16);
372 if (end == image_base_str || *end != '\0') {
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700373 fprintf(stderr, "Failed to parse hexadecimal value for option %s\n", option.data());
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700374 usage();
375 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700376 } else if (option.starts_with("--boot-image=")) {
Brian Carlstromb0011262011-12-09 12:17:24 -0800377 boot_image_filename = option.substr(strlen("--boot-image=")).data();
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700378 } else if (option.starts_with("--host-prefix=")) {
379 host_prefix = option.substr(strlen("--host-prefix=")).data();
jeffhao5d840402011-10-24 17:09:45 -0700380 } else if (option == "--runtime-arg") {
381 if (++i >= argc) {
382 fprintf(stderr, "Missing required argument for --runtime-arg\n");
383 usage();
384 }
385 runtime_args.push_back(argv[i]);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700386 } else {
387 fprintf(stderr, "unknown argument %s\n", option.data());
388 usage();
389 }
390 }
391
Brian Carlstromae826982011-11-09 01:33:42 -0800392 if (oat_filename.empty()) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700393 fprintf(stderr, "--oat file name not specified\n");
394 return EXIT_FAILURE;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700395 }
396
Brian Carlstromb0011262011-12-09 12:17:24 -0800397 if (host_prefix.empty()) {
398 const char* android_product_out = getenv("ANDROID_PRODUCT_OUT");
399 if (android_product_out != NULL) {
400 host_prefix = android_product_out;
401 }
402 }
403
Brian Carlstromae826982011-11-09 01:33:42 -0800404 bool image = (image_filename != NULL);
Brian Carlstromb0011262011-12-09 12:17:24 -0800405 if (!image && boot_image_filename.empty()) {
406 boot_image_filename += host_prefix;
407 boot_image_filename += "/data/art-cache/boot.art";
408 }
409 std::string boot_image_option;
410 if (boot_image_filename != NULL) {
411 boot_image_option += "-Ximage:";
412 boot_image_option += boot_image_filename;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700413 }
414
Brian Carlstromae826982011-11-09 01:33:42 -0800415 if (image_classes_filename != NULL && !image) {
416 fprintf(stderr, "--image-classes should only be used with --image\n");
417 return EXIT_FAILURE;
418 }
419
420 if (image_classes_filename != NULL && !boot_image_option.empty()) {
421 fprintf(stderr, "--image-classes should not be used with --boot-image\n");
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700422 return EXIT_FAILURE;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700423 }
424
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700425 if (boot_image_option.empty()) {
426 if (image_base == 0) {
427 fprintf(stderr, "non-zero --base not specified\n");
428 return EXIT_FAILURE;
429 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700430 }
431
Brian Carlstrom6ef827a2011-12-11 14:57:47 -0800432 // Check early that the result of compilation can be written
433 UniquePtr<File> oat_file(OS::OpenFile(oat_filename.c_str(), true));
434 if (oat_file.get() == NULL) {
435 PLOG(ERROR) << "Unable to create oat file " << oat_filename;
436 return EXIT_FAILURE;
Ian Rogers5e863dd2011-11-02 20:00:10 -0700437 }
Brian Carlstrom6ef827a2011-12-11 14:57:47 -0800438 LOG(INFO) << "dex2oat: " << oat_filename;
Ian Rogers5e863dd2011-11-02 20:00:10 -0700439
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700440 Runtime::Options options;
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700441 options.push_back(std::make_pair("compiler", reinterpret_cast<void*>(NULL)));
Brian Carlstromb7bbba42011-10-13 14:58:47 -0700442 std::string boot_class_path_string;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700443 if (boot_image_option.empty()) {
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700444 boot_class_path_string += "-Xbootclasspath:";
445 for (size_t i = 0; i < dex_filenames.size()-1; i++) {
446 boot_class_path_string += dex_filenames[i];
447 boot_class_path_string += ":";
448 }
449 boot_class_path_string += dex_filenames[dex_filenames.size()-1];
450 options.push_back(std::make_pair(boot_class_path_string.c_str(), reinterpret_cast<void*>(NULL)));
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700451 } else {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700452 options.push_back(std::make_pair(boot_image_option.c_str(), reinterpret_cast<void*>(NULL)));
453 }
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700454 if (!host_prefix.empty()) {
455 options.push_back(std::make_pair("host-prefix", host_prefix.c_str()));
456 }
jeffhao5d840402011-10-24 17:09:45 -0700457 for (size_t i = 0; i < runtime_args.size(); i++) {
458 options.push_back(std::make_pair(runtime_args[i], reinterpret_cast<void*>(NULL)));
459 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700460
Brian Carlstromae826982011-11-09 01:33:42 -0800461 UniquePtr<Dex2Oat> dex2oat(Dex2Oat::Create(options));
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700462
Brian Carlstromae826982011-11-09 01:33:42 -0800463 // If --image-classes was specified, calculate the full list classes to include in the image
464 UniquePtr<const std::set<std::string> > image_classes(NULL);
465 if (image_classes_filename != NULL) {
466 image_classes.reset(dex2oat->GetImageClassDescriptors(image_classes_filename));
467 if (image_classes.get() == NULL) {
468 LOG(ERROR) << "Failed to create list of image classes from " << image_classes_filename;
469 return EXIT_FAILURE;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700470 }
471 }
472
Brian Carlstromae826982011-11-09 01:33:42 -0800473 if (!dex2oat->CreateOatFile(boot_image_option,
474 dex_filenames,
475 host_prefix,
476 oat_file.get(),
477 image,
478 image_classes.get())) {
479 LOG(ERROR) << "Failed to create oat file" << oat_filename;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700480 return EXIT_FAILURE;
481 }
482
Brian Carlstromae826982011-11-09 01:33:42 -0800483 if (!image) {
Brian Carlstromae826982011-11-09 01:33:42 -0800484 LOG(INFO) << "Oat file written successfully " << oat_filename;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700485 return EXIT_SUCCESS;
486 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700487
Brian Carlstromae826982011-11-09 01:33:42 -0800488 if (!dex2oat->CreateImageFile(image_filename,
489 image_base,
490 image_classes.get(),
491 oat_filename,
492 host_prefix)) {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700493 return EXIT_FAILURE;
494 }
495
Brian Carlstromae826982011-11-09 01:33:42 -0800496 // We wrote the oat file successfully, and want to keep it.
Brian Carlstromae826982011-11-09 01:33:42 -0800497 LOG(INFO) << "Oat file written successfully " << oat_filename;
Elliott Hughes234da572011-11-03 22:13:06 -0700498 LOG(INFO) << "Image written successfully " << image_filename;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700499 return EXIT_SUCCESS;
500}
501
502} // namespace art
503
504int main(int argc, char** argv) {
505 return art::dex2oat(argc, argv);
506}