blob: 47193e55d9e2498c0ee13b59096ca8c180f16de7 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070016
17#include <stdio.h>
18#include <stdlib.h>
19
Brian Carlstromae826982011-11-09 01:33:42 -080020#include <iostream>
21#include <fstream>
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070022#include <string>
23#include <vector>
24
25#include "class_linker.h"
26#include "class_loader.h"
27#include "compiler.h"
Ian Rogers5e863dd2011-11-02 20:00:10 -070028#include "file.h"
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070029#include "image_writer.h"
Ian Rogers6f1dfe42011-12-08 17:28:34 -080030#include "leb128.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070031#include "oat_writer.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080032#include "object_utils.h"
Ian Rogers5e863dd2011-11-02 20:00:10 -070033#include "os.h"
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070034#include "runtime.h"
Brian Carlstroma004aa92012-02-08 18:05:09 -080035#include "stl_util.h"
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070036#include "stringpiece.h"
Elliott Hughesbb551fa2012-01-25 16:35:29 -080037#include "timing_logger.h"
Brian Carlstroma6cc8932012-01-04 14:44:07 -080038#include "zip_archive.h"
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070039
Elliott Hughesb08e8a32012-04-02 10:51:41 -070040#if defined(__APPLE__)
41#include "AvailabilityMacros.h" // For MAC_OS_X_VERSION_MAX_ALLOWED
42#endif
43
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070044namespace art {
45
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -080046static void UsageErrorV(const char* fmt, va_list ap) {
47 std::string error;
48 StringAppendV(&error, fmt, ap);
49 LOG(ERROR) << error;
50}
51
52static void UsageError(const char* fmt, ...) {
53 va_list ap;
54 va_start(ap, fmt);
55 UsageErrorV(fmt, ap);
56 va_end(ap);
57}
58
59static void Usage(const char* fmt, ...) {
60 va_list ap;
61 va_start(ap, fmt);
62 UsageErrorV(fmt, ap);
63 va_end(ap);
64
65 UsageError("Usage: dex2oat [options]...");
66 UsageError("");
67 UsageError(" --dex-file=<dex-file>: specifies a .dex file to compile.");
68 UsageError(" Example: --dex-file=/system/framework/core.jar");
69 UsageError("");
70 UsageError(" --zip-fd=<file-descriptor>: specifies a file descriptor of a zip file");
71 UsageError(" containing a classes.dex file to compile.");
72 UsageError(" Example: --zip-fd=5");
73 UsageError("");
74 UsageError(" --zip-location=<zip-location>: specifies a symbolic name for the file corresponding");
75 UsageError(" to the file descriptor specified by --zip-fd.");
76 UsageError(" Example: --zip-location=/system/app/Calculator.apk");
77 UsageError("");
78 UsageError(" --oat-file=<file.oat>: specifies the required oat filename.");
79 UsageError(" Example: --oat-file=/system/framework/boot.oat");
80 UsageError("");
81 UsageError(" --oat-location=<oat-name>: specifies a symbolic name for the file corresponding");
82 UsageError(" to the file descriptor specified by --oat-fd.");
83 UsageError(" Example: --oat-location=/data/art-cache/system@app@Calculator.apk.oat");
84 UsageError("");
Logan Chien8b977d32012-02-21 19:14:55 +080085#if defined(ART_USE_LLVM_COMPILER)
Logan Chien8b977d32012-02-21 19:14:55 +080086 UsageError(" --bitcode=<file.bc>: specifies the optional bitcode filename.");
87 UsageError(" Example: --bitcode=/system/framework/boot.bc");
88 UsageError("");
89#endif
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -080090 UsageError(" --image=<file.art>: specifies the output image filename.");
91 UsageError(" Example: --image=/system/framework/boot.art");
92 UsageError("");
93 UsageError(" --image-classes=<classname-file>: specifies classes to include in an image.");
94 UsageError(" Example: --image=frameworks/base/preloaded-classes");
95 UsageError("");
96 UsageError(" --base=<hex-address>: specifies the base address when creating a boot image.");
97 UsageError(" Example: --base=0x50000000");
98 UsageError("");
99 UsageError(" --boot-image=<file.art>: provide the image file for the boot class path.");
100 UsageError(" Example: --boot-image=/system/framework/boot.art");
101 UsageError(" Default: <host-prefix>/system/framework/boot.art");
102 UsageError("");
103 UsageError(" --host-prefix may be used to translate host paths to target paths during");
104 UsageError(" cross compilation.");
105 UsageError(" Example: --host-prefix=out/target/product/crespo");
106 UsageError(" Default: $ANDROID_PRODUCT_OUT");
107 UsageError("");
Ian Rogers49c48942012-03-11 15:15:37 -0700108 UsageError(" --instruction-set=(ARM|Thumb2|MIPS|X86): compile for a particular instruction");
109 UsageError(" set.");
110 UsageError(" Example: --instruction-set=X86");
111 UsageError(" Default: Thumb2");
112 UsageError("");
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800113 UsageError(" --runtime-arg <argument>: used to specify various arguments for the runtime,");
114 UsageError(" such as initial heap size, maximum heap size, and verbose output.");
115 UsageError(" Use a separate --runtime-arg switch for each argument.");
116 UsageError(" Example: --runtime-arg -Xms256m");
117 UsageError("");
118 std::cerr << "See log for usage error information\n";
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700119 exit(EXIT_FAILURE);
120}
121
Brian Carlstromae826982011-11-09 01:33:42 -0800122class Dex2Oat {
123 public:
124
Ian Rogers49c48942012-03-11 15:15:37 -0700125 static Dex2Oat* Create(Runtime::Options& options, InstructionSet instruction_set,
126 size_t thread_count, bool support_debugging) {
127 UniquePtr<Runtime> runtime(CreateRuntime(options, instruction_set));
Brian Carlstromae826982011-11-09 01:33:42 -0800128 if (runtime.get() == NULL) {
129 return NULL;
130 }
Ian Rogers49c48942012-03-11 15:15:37 -0700131 return new Dex2Oat(runtime.release(), instruction_set, thread_count, support_debugging);
Brian Carlstromae826982011-11-09 01:33:42 -0800132 }
133
134 ~Dex2Oat() {
135 delete runtime_;
Elliott Hughes5523ee02012-02-03 18:18:34 -0800136 LOG(INFO) << "dex2oat took " << PrettyDuration(NanoTime() - start_ns_) << " (threads: " << thread_count_ << ")";
Brian Carlstromae826982011-11-09 01:33:42 -0800137 }
138
139 // Make a list of descriptors for classes to include in the image
140 const std::set<std::string>* GetImageClassDescriptors(const char* image_classes_filename) {
141 UniquePtr<std::ifstream> image_classes_file(new std::ifstream(image_classes_filename, std::ifstream::in));
142 if (image_classes_file.get() == NULL) {
143 LOG(ERROR) << "Failed to open image classes file " << image_classes_filename;
144 return NULL;
145 }
146
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800147 // Load all the classes specified in the file
Brian Carlstromae826982011-11-09 01:33:42 -0800148 ClassLinker* class_linker = runtime_->GetClassLinker();
149 while (image_classes_file->good()) {
150 std::string dot;
151 std::getline(*image_classes_file.get(), dot);
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800152 if (StartsWith(dot, "#") || dot.empty()) {
Brian Carlstromae826982011-11-09 01:33:42 -0800153 continue;
154 }
Elliott Hughes95572412011-12-13 18:14:20 -0800155 std::string descriptor(DotToDescriptor(dot.c_str()));
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800156 SirtRef<Class> klass(class_linker->FindSystemClass(descriptor.c_str()));
Brian Carlstromae826982011-11-09 01:33:42 -0800157 if (klass.get() == NULL) {
158 LOG(WARNING) << "Failed to find class " << descriptor;
159 Thread::Current()->ClearException();
160 }
161 }
162 image_classes_file->close();
163
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800164 // Resolve exception classes referenced by the loaded classes. The catch logic assumes
165 // exceptions are resolved by the verifier when there is a catch block in an interested method.
166 // Do this here so that exception classes appear to have been specified image classes.
167 std::set<std::pair<uint16_t, const DexFile*> > unresolved_exception_types;
168 do {
169 unresolved_exception_types.clear();
170 class_linker->VisitClasses(ResolveCatchBlockExceptionsClassVisitor,
171 &unresolved_exception_types);
172 typedef std::set<std::pair<uint16_t, const DexFile*> >::const_iterator It; // TODO: C++0x auto
173 for (It it = unresolved_exception_types.begin(),
174 end = unresolved_exception_types.end();
175 it != end; ++it) {
176 uint16_t exception_type_idx = it->first;
177 const DexFile* dex_file = it->second;
178 DexCache* dex_cache = class_linker->FindDexCache(*dex_file);
179 ClassLoader* class_loader = NULL;
180 SirtRef<Class> klass(class_linker->ResolveType(*dex_file, exception_type_idx, dex_cache,
181 class_loader));
182 if (klass.get() == NULL) {
183 const DexFile::TypeId& type_id = dex_file->GetTypeId(exception_type_idx);
184 const char* descriptor = dex_file->GetTypeDescriptor(type_id);
185 LOG(FATAL) << "Failed to resolve class " << descriptor;
186 }
187 DCHECK(klass->IsThrowableClass());
188 }
189 // Resolving exceptions may load classes that reference more exceptions, iterate until no
190 // more are found
191 } while (!unresolved_exception_types.empty());
192
Brian Carlstromae826982011-11-09 01:33:42 -0800193 // We walk the roots looking for classes so that we'll pick up the
194 // above classes plus any classes them depend on such super
195 // classes, interfaces, and the required ClassLinker roots.
196 UniquePtr<std::set<std::string> > image_classes(new std::set<std::string>());
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800197 class_linker->VisitClasses(RecordImageClassesVisitor, image_classes.get());
Brian Carlstromae826982011-11-09 01:33:42 -0800198 CHECK_NE(image_classes->size(), 0U);
199 return image_classes.release();
200 }
201
Brian Carlstromf5822582012-03-19 22:34:31 -0700202 const Compiler* CreateOatFile(const std::string& boot_image_option,
Brian Carlstrom34f1fa42012-04-05 18:28:12 -0700203 const std::string* host_prefix,
Brian Carlstromf5822582012-03-19 22:34:31 -0700204 const std::vector<const DexFile*>& dex_files,
205 File* oat_file,
Logan Chien8b977d32012-02-21 19:14:55 +0800206#if defined(ART_USE_LLVM_COMPILER)
Logan Chiende08e842012-03-21 00:34:12 +0800207 const std::string& bitcode_filename,
Logan Chien8b977d32012-02-21 19:14:55 +0800208#endif
Brian Carlstromf5822582012-03-19 22:34:31 -0700209 bool image,
Brian Carlstromba0668e2012-03-26 13:14:07 -0700210 const std::set<std::string>* image_classes,
211 bool dump_stats,
212 bool dump_timings) {
Brian Carlstromae826982011-11-09 01:33:42 -0800213 // SirtRef and ClassLoader creation needs to come after Runtime::Create
214 UniquePtr<SirtRef<ClassLoader> > class_loader(new SirtRef<ClassLoader>(NULL));
215 if (class_loader.get() == NULL) {
216 LOG(ERROR) << "Failed to create SirtRef for class loader";
217 return false;
218 }
219
Brian Carlstromae826982011-11-09 01:33:42 -0800220 if (!boot_image_option.empty()) {
221 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromae826982011-11-09 01:33:42 -0800222 std::vector<const DexFile*> class_path_files(dex_files);
Brian Carlstroma004aa92012-02-08 18:05:09 -0800223 OpenClassPathFiles(runtime_->GetClassPathString(), class_path_files);
Brian Carlstromae826982011-11-09 01:33:42 -0800224 for (size_t i = 0; i < class_path_files.size(); i++) {
225 class_linker->RegisterDexFile(*class_path_files[i]);
226 }
227 class_loader.get()->reset(PathClassLoader::AllocCompileTime(class_path_files));
Brian Carlstromae826982011-11-09 01:33:42 -0800228 }
229
Brian Carlstromf5822582012-03-19 22:34:31 -0700230 UniquePtr<Compiler> compiler(new Compiler(instruction_set_,
231 image,
232 thread_count_,
233 support_debugging_,
Brian Carlstromba0668e2012-03-26 13:14:07 -0700234 image_classes,
235 dump_stats,
236 dump_timings));
Logan Chien8b977d32012-02-21 19:14:55 +0800237
238#if defined(ART_USE_LLVM_COMPILER)
Brian Carlstromf5822582012-03-19 22:34:31 -0700239 compiler->SetBitcodeFileName(bitcode_filename);
Logan Chien8b977d32012-02-21 19:14:55 +0800240#endif
241
Brian Carlstromf5822582012-03-19 22:34:31 -0700242 compiler->CompileAll(class_loader->get(), dex_files);
Brian Carlstromae826982011-11-09 01:33:42 -0800243
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700244 std::string image_file_location;
245 uint32_t image_file_location_checksum = 0;
246 Heap* heap = Runtime::Current()->GetHeap();
247 if (heap->GetSpaces().size() > 1) {
248 ImageSpace* image_space = heap->GetImageSpace();
249 image_file_location_checksum = image_space->GetImageHeader().GetOatChecksum();
250 image_file_location = image_space->GetImageFilename();
Brian Carlstrom34f1fa42012-04-05 18:28:12 -0700251 if (host_prefix != NULL && StartsWith(image_file_location, host_prefix->c_str())) {
252 image_file_location = image_file_location.substr(host_prefix->size());
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700253 }
254 }
255
256 if (!OatWriter::Create(oat_file,
257 class_loader->get(),
258 dex_files,
259 image_file_location_checksum,
260 image_file_location,
Brian Carlstromf5822582012-03-19 22:34:31 -0700261 *compiler.get())) {
Brian Carlstromae826982011-11-09 01:33:42 -0800262 LOG(ERROR) << "Failed to create oat file " << oat_file->name();
Brian Carlstromf5822582012-03-19 22:34:31 -0700263 return NULL;
Brian Carlstromae826982011-11-09 01:33:42 -0800264 }
Brian Carlstromf5822582012-03-19 22:34:31 -0700265 return compiler.release();
Brian Carlstromae826982011-11-09 01:33:42 -0800266 }
267
Brian Carlstroma004aa92012-02-08 18:05:09 -0800268 bool CreateImageFile(const std::string& image_filename,
Brian Carlstromae826982011-11-09 01:33:42 -0800269 uintptr_t image_base,
270 const std::set<std::string>* image_classes,
271 const std::string& oat_filename,
Brian Carlstromf5822582012-03-19 22:34:31 -0700272 const std::string& oat_location,
273 const Compiler& compiler) {
Brian Carlstromae826982011-11-09 01:33:42 -0800274 ImageWriter image_writer(image_classes);
Brian Carlstromf5822582012-03-19 22:34:31 -0700275 if (!image_writer.Write(image_filename, image_base, oat_filename, oat_location, compiler)) {
Brian Carlstromae826982011-11-09 01:33:42 -0800276 LOG(ERROR) << "Failed to create image file " << image_filename;
277 return false;
278 }
279 return true;
280 }
281
282 private:
283
Ian Rogers49c48942012-03-11 15:15:37 -0700284 explicit Dex2Oat(Runtime* runtime, InstructionSet instruction_set, size_t thread_count,
285 bool support_debugging)
286 : instruction_set_(instruction_set),
287 runtime_(runtime),
Elliott Hughesde6e4cf2012-02-27 14:46:06 -0800288 thread_count_(thread_count),
289 support_debugging_(support_debugging),
290 start_ns_(NanoTime()) {
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800291 }
Brian Carlstromae826982011-11-09 01:33:42 -0800292
Ian Rogers49c48942012-03-11 15:15:37 -0700293 static Runtime* CreateRuntime(Runtime::Options& options, InstructionSet instruction_set) {
Brian Carlstromae826982011-11-09 01:33:42 -0800294 Runtime* runtime = Runtime::Create(options, false);
295 if (runtime == NULL) {
296 LOG(ERROR) << "Failed to create runtime";
297 return NULL;
298 }
299
300 // if we loaded an existing image, we will reuse values from the image roots.
301 if (!runtime->HasJniDlsymLookupStub()) {
Ian Rogers49c48942012-03-11 15:15:37 -0700302 runtime->SetJniDlsymLookupStub(Compiler::CreateJniDlsymLookupStub(instruction_set));
Brian Carlstromae826982011-11-09 01:33:42 -0800303 }
304 if (!runtime->HasAbstractMethodErrorStubArray()) {
Ian Rogers49c48942012-03-11 15:15:37 -0700305 runtime->SetAbstractMethodErrorStubArray(Compiler::CreateAbstractMethodErrorStub(instruction_set));
Brian Carlstromae826982011-11-09 01:33:42 -0800306 }
307 for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) {
308 Runtime::TrampolineType type = Runtime::TrampolineType(i);
309 if (!runtime->HasResolutionStubArray(type)) {
Ian Rogers49c48942012-03-11 15:15:37 -0700310 runtime->SetResolutionStubArray(Compiler::CreateResolutionStub(instruction_set, type), type);
Brian Carlstromae826982011-11-09 01:33:42 -0800311 }
312 }
Ian Rogers19846512012-02-24 11:42:47 -0800313 if (!runtime->HasResolutionMethod()) {
314 runtime->SetResolutionMethod(runtime->CreateResolutionMethod());
315 }
Brian Carlstromae826982011-11-09 01:33:42 -0800316 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
317 Runtime::CalleeSaveType type = Runtime::CalleeSaveType(i);
318 if (!runtime->HasCalleeSaveMethod(type)) {
Ian Rogers49c48942012-03-11 15:15:37 -0700319 runtime->SetCalleeSaveMethod(runtime->CreateCalleeSaveMethod(instruction_set, type), type);
Brian Carlstromae826982011-11-09 01:33:42 -0800320 }
321 }
Ian Rogers19846512012-02-24 11:42:47 -0800322 runtime->GetClassLinker()->FixupDexCaches(runtime->GetResolutionMethod());
Brian Carlstromae826982011-11-09 01:33:42 -0800323 return runtime;
324 }
325
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800326 static void ResolveExceptionsForMethod(MethodHelper* mh,
327 std::set<std::pair<uint16_t, const DexFile*> >& exceptions_to_resolve) {
328 const DexFile::CodeItem* code_item = mh->GetCodeItem();
329 if (code_item == NULL) {
330 return; // native or abstract method
331 }
332 if (code_item->tries_size_ == 0) {
333 return; // nothing to process
334 }
335 const byte* encoded_catch_handler_list = DexFile::GetCatchHandlerData(*code_item, 0);
336 size_t num_encoded_catch_handlers = DecodeUnsignedLeb128(&encoded_catch_handler_list);
337 for (size_t i = 0; i < num_encoded_catch_handlers; i++) {
338 int32_t encoded_catch_handler_size = DecodeSignedLeb128(&encoded_catch_handler_list);
339 bool has_catch_all = false;
340 if (encoded_catch_handler_size <= 0) {
341 encoded_catch_handler_size = -encoded_catch_handler_size;
342 has_catch_all = true;
343 }
344 for (int32_t j = 0; j < encoded_catch_handler_size; j++) {
345 uint16_t encoded_catch_handler_handlers_type_idx =
346 DecodeUnsignedLeb128(&encoded_catch_handler_list);
347 // Add to set of types to resolve if not already in the dex cache resolved types
348 if (!mh->IsResolvedTypeIdx(encoded_catch_handler_handlers_type_idx)) {
349 exceptions_to_resolve.insert(
350 std::pair<uint16_t, const DexFile*>(encoded_catch_handler_handlers_type_idx,
351 &mh->GetDexFile()));
352 }
353 // ignore address associated with catch handler
354 DecodeUnsignedLeb128(&encoded_catch_handler_list);
355 }
356 if (has_catch_all) {
357 // ignore catch all address
358 DecodeUnsignedLeb128(&encoded_catch_handler_list);
359 }
360 }
361 }
362 static bool ResolveCatchBlockExceptionsClassVisitor(Class* c, void* arg) {
363 std::set<std::pair<uint16_t, const DexFile*> >* exceptions_to_resolve =
364 reinterpret_cast<std::set<std::pair<uint16_t, const DexFile*> >*>(arg);
365 MethodHelper mh;
366 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
367 Method* m = c->GetVirtualMethod(i);
368 mh.ChangeMethod(m);
369 ResolveExceptionsForMethod(&mh, *exceptions_to_resolve);
370 }
371 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
372 Method* m = c->GetDirectMethod(i);
373 mh.ChangeMethod(m);
374 ResolveExceptionsForMethod(&mh, *exceptions_to_resolve);
375 }
376 return true;
377 }
378 static bool RecordImageClassesVisitor(Class* klass, void* arg) {
Brian Carlstromae826982011-11-09 01:33:42 -0800379 std::set<std::string>* image_classes = reinterpret_cast<std::set<std::string>*>(arg);
380 if (klass->IsArrayClass() || klass->IsPrimitive()) {
Jesse Wilson254db0f2011-11-16 16:44:11 -0500381 return true;
382 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800383 image_classes->insert(ClassHelper(klass).GetDescriptor());
Brian Carlstromae826982011-11-09 01:33:42 -0800384 return true;
Jesse Wilson254db0f2011-11-16 16:44:11 -0500385 }
Jesse Wilson254db0f2011-11-16 16:44:11 -0500386
Brian Carlstromae826982011-11-09 01:33:42 -0800387 // Appends to dex_files any elements of class_path that it doesn't already
388 // contain. This will open those dex files as necessary.
389 static void OpenClassPathFiles(const std::string& class_path, std::vector<const DexFile*>& dex_files) {
390 std::vector<std::string> parsed;
391 Split(class_path, ':', parsed);
392 for (size_t i = 0; i < parsed.size(); ++i) {
393 if (DexFilesContains(dex_files, parsed[i])) {
394 continue;
395 }
Brian Carlstroma004aa92012-02-08 18:05:09 -0800396 const DexFile* dex_file = DexFile::Open(parsed[i], parsed[i]);
Brian Carlstromae826982011-11-09 01:33:42 -0800397 if (dex_file == NULL) {
398 LOG(WARNING) << "Failed to open dex file " << parsed[i];
399 } else {
400 dex_files.push_back(dex_file);
401 }
Jesse Wilson254db0f2011-11-16 16:44:11 -0500402 }
403 }
Brian Carlstromae826982011-11-09 01:33:42 -0800404
405 // Returns true if dex_files has a dex with the named location.
406 static bool DexFilesContains(const std::vector<const DexFile*>& dex_files, const std::string& location) {
407 for (size_t i = 0; i < dex_files.size(); ++i) {
408 if (dex_files[i]->GetLocation() == location) {
409 return true;
410 }
411 }
412 return false;
413 }
414
Ian Rogers49c48942012-03-11 15:15:37 -0700415 const InstructionSet instruction_set_;
Brian Carlstromae826982011-11-09 01:33:42 -0800416
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800417 Runtime* runtime_;
Elliott Hughes5523ee02012-02-03 18:18:34 -0800418 size_t thread_count_;
Elliott Hughesde6e4cf2012-02-27 14:46:06 -0800419 bool support_debugging_;
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800420 uint64_t start_ns_;
421
Brian Carlstromae826982011-11-09 01:33:42 -0800422 DISALLOW_IMPLICIT_CONSTRUCTORS(Dex2Oat);
423};
Jesse Wilson254db0f2011-11-16 16:44:11 -0500424
Elliott Hughes72395bf2012-04-24 13:45:26 -0700425static bool ParseInt(const char* in, int* out) {
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800426 char* end;
427 int result = strtol(in, &end, 10);
428 if (in == end || *end != '\0') {
429 return false;
430 }
431 *out = result;
432 return true;
433}
434
Elliott Hughes72395bf2012-04-24 13:45:26 -0700435static void OpenDexFiles(const std::vector<const char*>& dex_filenames,
436 const std::vector<const char*>& dex_locations,
437 std::vector<const DexFile*>& dex_files) {
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800438 for (size_t i = 0; i < dex_filenames.size(); i++) {
439 const char* dex_filename = dex_filenames[i];
Brian Carlstroma004aa92012-02-08 18:05:09 -0800440 const char* dex_location = dex_locations[i];
441 const DexFile* dex_file = DexFile::Open(dex_filename, dex_location);
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800442 if (dex_file == NULL) {
jeffhao60f83e32012-02-13 17:16:30 -0800443 LOG(WARNING) << "could not open .dex from file " << dex_filename;
444 } else {
445 dex_files.push_back(dex_file);
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800446 }
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800447 }
448}
449
Elliott Hughes72395bf2012-04-24 13:45:26 -0700450static int dex2oat(int argc, char** argv) {
451 InitLogging();
452
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700453 // Skip over argv[0].
454 argv++;
455 argc--;
456
457 if (argc == 0) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800458 Usage("no arguments specified");
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700459 }
460
461 std::vector<const char*> dex_filenames;
Brian Carlstroma004aa92012-02-08 18:05:09 -0800462 std::vector<const char*> dex_locations;
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800463 int zip_fd = -1;
Brian Carlstroma004aa92012-02-08 18:05:09 -0800464 std::string zip_location;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700465 std::string oat_filename;
Brian Carlstroma004aa92012-02-08 18:05:09 -0800466 std::string oat_location;
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800467 int oat_fd = -1;
Logan Chien8b977d32012-02-21 19:14:55 +0800468#if defined(ART_USE_LLVM_COMPILER)
Logan Chien8b977d32012-02-21 19:14:55 +0800469 std::string bitcode_filename;
470#endif
Brian Carlstromae826982011-11-09 01:33:42 -0800471 const char* image_classes_filename = NULL;
Brian Carlstroma004aa92012-02-08 18:05:09 -0800472 std::string image_filename;
Brian Carlstromb0011262011-12-09 12:17:24 -0800473 std::string boot_image_filename;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700474 uintptr_t image_base = 0;
Brian Carlstrom34f1fa42012-04-05 18:28:12 -0700475 UniquePtr<std::string> host_prefix;
jeffhao5d840402011-10-24 17:09:45 -0700476 std::vector<const char*> runtime_args;
Elliott Hughes5523ee02012-02-03 18:18:34 -0800477 int thread_count = 2;
Elliott Hughesde6e4cf2012-02-27 14:46:06 -0800478 bool support_debugging = false;
Ian Rogers49c48942012-03-11 15:15:37 -0700479 InstructionSet instruction_set = kThumb2;
Elliott Hughes67d92002012-03-26 15:08:51 -0700480 bool dump_stats = kIsDebugBuild;
481 bool dump_timings = kIsDebugBuild;
Brian Carlstrom16192862011-09-12 17:50:06 -0700482
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700483 for (int i = 0; i < argc; i++) {
484 const StringPiece option(argv[i]);
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800485 bool log_options = false;
486 if (log_options) {
Brian Carlstromb7bbba42011-10-13 14:58:47 -0700487 LOG(INFO) << "dex2oat: option[" << i << "]=" << argv[i];
488 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700489 if (option.starts_with("--dex-file=")) {
490 dex_filenames.push_back(option.substr(strlen("--dex-file=")).data());
Brian Carlstroma004aa92012-02-08 18:05:09 -0800491 } else if (option.starts_with("--dex-location=")) {
492 dex_locations.push_back(option.substr(strlen("--dex-location=")).data());
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800493 } else if (option.starts_with("--zip-fd=")) {
494 const char* zip_fd_str = option.substr(strlen("--zip-fd=")).data();
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800495 if (!ParseInt(zip_fd_str, &zip_fd)) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800496 Usage("could not parse --zip-fd argument '%s' as an integer", zip_fd_str);
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800497 }
Brian Carlstroma004aa92012-02-08 18:05:09 -0800498 } else if (option.starts_with("--zip-location=")) {
499 zip_location = option.substr(strlen("--zip-location=")).data();
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800500 } else if (option.starts_with("--oat-file=")) {
501 oat_filename = option.substr(strlen("--oat-file=")).data();
502 } else if (option.starts_with("--oat-fd=")) {
503 const char* oat_fd_str = option.substr(strlen("--oat-fd=")).data();
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800504 if (!ParseInt(oat_fd_str, &oat_fd)) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800505 Usage("could not parse --oat-fd argument '%s' as an integer", oat_fd_str);
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800506 }
Elliott Hughesde6e4cf2012-02-27 14:46:06 -0800507 } else if (option.starts_with("-g")) {
508 support_debugging = true;
Elliott Hughes5523ee02012-02-03 18:18:34 -0800509 } else if (option.starts_with("-j")) {
Brian Carlstromb12552a2012-02-04 17:17:31 -0800510 const char* thread_count_str = option.substr(strlen("-j")).data();
Elliott Hughes5523ee02012-02-03 18:18:34 -0800511 if (!ParseInt(thread_count_str, &thread_count)) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800512 Usage("could not parse -j argument '%s' as an integer", thread_count_str);
Elliott Hughes5523ee02012-02-03 18:18:34 -0800513 }
Brian Carlstroma004aa92012-02-08 18:05:09 -0800514 } else if (option.starts_with("--oat-location=")) {
515 oat_location = option.substr(strlen("--oat-location=")).data();
Logan Chien8b977d32012-02-21 19:14:55 +0800516#if defined(ART_USE_LLVM_COMPILER)
Logan Chien8b977d32012-02-21 19:14:55 +0800517 } else if (option.starts_with("--bitcode=")) {
518 bitcode_filename = option.substr(strlen("--bitcode=")).data();
519#endif
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700520 } else if (option.starts_with("--image=")) {
521 image_filename = option.substr(strlen("--image=")).data();
Brian Carlstromae826982011-11-09 01:33:42 -0800522 } else if (option.starts_with("--image-classes=")) {
523 image_classes_filename = option.substr(strlen("--image-classes=")).data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700524 } else if (option.starts_with("--base=")) {
525 const char* image_base_str = option.substr(strlen("--base=")).data();
526 char* end;
527 image_base = strtoul(image_base_str, &end, 16);
528 if (end == image_base_str || *end != '\0') {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800529 Usage("Failed to parse hexadecimal value for option %s", option.data());
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700530 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700531 } else if (option.starts_with("--boot-image=")) {
Brian Carlstromb0011262011-12-09 12:17:24 -0800532 boot_image_filename = option.substr(strlen("--boot-image=")).data();
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700533 } else if (option.starts_with("--host-prefix=")) {
Brian Carlstrom34f1fa42012-04-05 18:28:12 -0700534 host_prefix.reset(new std::string(option.substr(strlen("--host-prefix=")).data()));
Ian Rogers49c48942012-03-11 15:15:37 -0700535 } else if (option.starts_with("--instruction-set=")) {
536 StringPiece instruction_set_str = option.substr(strlen("--instruction-set=")).data();
537 if (instruction_set_str == "Thumb2" || instruction_set_str == "ARM") {
538 instruction_set = kThumb2;
539 } else if (instruction_set_str == "MIPS") {
540 instruction_set = kMips;
541 } else if (instruction_set_str == "X86") {
542 instruction_set = kX86;
543 }
jeffhao5d840402011-10-24 17:09:45 -0700544 } else if (option == "--runtime-arg") {
545 if (++i >= argc) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800546 Usage("Missing required argument for --runtime-arg");
jeffhao5d840402011-10-24 17:09:45 -0700547 }
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800548 if (log_options) {
549 LOG(INFO) << "dex2oat: option[" << i << "]=" << argv[i];
550 }
jeffhao5d840402011-10-24 17:09:45 -0700551 runtime_args.push_back(argv[i]);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700552 } else {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800553 Usage("unknown argument %s", option.data());
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700554 }
555 }
556
Elliott Hughesb907a3f2012-04-02 16:11:36 -0700557#if defined(__APPLE__) && MAC_OS_X_VERSION_MAX_ALLOWED < 1060
558 // Something is broken on Mac OS 10.5, and we don't have direct access to any machines running it.
559 // Restricting dex2oat to a single thread on Mac OS 10.5 appears to be a workaround.
560 thread_count = 1;
561#endif
562
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800563 if (oat_filename.empty() && oat_fd == -1) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800564 Usage("Output must be supplied with either --oat-file or --oat-fd");
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800565 }
566
567 if (!oat_filename.empty() && oat_fd != -1) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800568 Usage("--oat-file should not be used with --oat-fd");
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800569 }
570
571 if (!oat_filename.empty() && oat_fd != -1) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800572 Usage("--oat-file should not be used with --oat-fd");
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800573 }
574
Brian Carlstroma004aa92012-02-08 18:05:09 -0800575 if (oat_fd != -1 && !image_filename.empty()) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800576 Usage("--oat-fd should not be used with --image");
Brian Carlstrome24fa612011-09-29 00:53:55 -0700577 }
578
Brian Carlstrom34f1fa42012-04-05 18:28:12 -0700579 if (host_prefix.get() == NULL) {
Brian Carlstromb0011262011-12-09 12:17:24 -0800580 const char* android_product_out = getenv("ANDROID_PRODUCT_OUT");
581 if (android_product_out != NULL) {
Brian Carlstrom34f1fa42012-04-05 18:28:12 -0700582 host_prefix.reset(new std::string(android_product_out));
Brian Carlstromb0011262011-12-09 12:17:24 -0800583 }
584 }
585
Brian Carlstroma004aa92012-02-08 18:05:09 -0800586 bool image = (!image_filename.empty());
Brian Carlstromb0011262011-12-09 12:17:24 -0800587 if (!image && boot_image_filename.empty()) {
Brian Carlstrom34f1fa42012-04-05 18:28:12 -0700588 if (host_prefix.get() == NULL) {
Brian Carlstroma56fcd62012-02-04 21:23:01 -0800589 boot_image_filename += GetAndroidRoot();
590 } else {
Brian Carlstrom34f1fa42012-04-05 18:28:12 -0700591 boot_image_filename += *host_prefix.get();
Brian Carlstroma56fcd62012-02-04 21:23:01 -0800592 boot_image_filename += "/system";
593 }
594 boot_image_filename += "/framework/boot.art";
Brian Carlstromb0011262011-12-09 12:17:24 -0800595 }
596 std::string boot_image_option;
Brian Carlstroma004aa92012-02-08 18:05:09 -0800597 if (!boot_image_filename.empty()) {
Brian Carlstromb0011262011-12-09 12:17:24 -0800598 boot_image_option += "-Ximage:";
599 boot_image_option += boot_image_filename;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700600 }
601
Brian Carlstromae826982011-11-09 01:33:42 -0800602 if (image_classes_filename != NULL && !image) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800603 Usage("--image-classes should only be used with --image");
Brian Carlstromae826982011-11-09 01:33:42 -0800604 }
605
606 if (image_classes_filename != NULL && !boot_image_option.empty()) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800607 Usage("--image-classes should not be used with --boot-image");
Brian Carlstrom78128a62011-09-15 17:21:19 -0700608 }
609
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800610 if (dex_filenames.empty() && zip_fd == -1) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800611 Usage("Input must be supplied with either --dex-file or --zip-fd");
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800612 }
613
614 if (!dex_filenames.empty() && zip_fd != -1) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800615 Usage("--dex-file should not be used with --zip-fd");
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800616 }
617
Brian Carlstroma004aa92012-02-08 18:05:09 -0800618 if (!dex_filenames.empty() && !zip_location.empty()) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800619 Usage("--dex-file should not be used with --zip-location");
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800620 }
621
Brian Carlstroma004aa92012-02-08 18:05:09 -0800622 if (dex_locations.empty()) {
623 for (size_t i = 0; i < dex_filenames.size(); i++) {
624 dex_locations.push_back(dex_filenames[i]);
625 }
626 } else if (dex_locations.size() != dex_filenames.size()) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800627 Usage("--dex-location arguments do not match --dex-file arguments");
Brian Carlstroma004aa92012-02-08 18:05:09 -0800628 }
629
630 if (zip_fd != -1 && zip_location.empty()) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800631 Usage("--zip-location should be supplied with --zip-fd");
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800632 }
633
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700634 if (boot_image_option.empty()) {
635 if (image_base == 0) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800636 Usage("non-zero --base not specified");
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700637 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700638 }
639
Brian Carlstrom6ef827a2011-12-11 14:57:47 -0800640 // Check early that the result of compilation can be written
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800641 UniquePtr<File> oat_file;
642 if (!oat_filename.empty()) {
643 oat_file.reset(OS::OpenFile(oat_filename.c_str(), true));
Brian Carlstroma004aa92012-02-08 18:05:09 -0800644 if (oat_location.empty()) {
645 oat_location = oat_filename;
646 }
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800647 } else {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800648 oat_file.reset(OS::FileFromFd(oat_location.c_str(), oat_fd));
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800649 }
Brian Carlstrom6ef827a2011-12-11 14:57:47 -0800650 if (oat_file.get() == NULL) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800651 PLOG(ERROR) << "Unable to create oat file: " << oat_location;
Brian Carlstrom6ef827a2011-12-11 14:57:47 -0800652 return EXIT_FAILURE;
Ian Rogers5e863dd2011-11-02 20:00:10 -0700653 }
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800654
Brian Carlstroma004aa92012-02-08 18:05:09 -0800655 LOG(INFO) << "dex2oat: " << oat_location;
Ian Rogers5e863dd2011-11-02 20:00:10 -0700656
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700657 Runtime::Options options;
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700658 options.push_back(std::make_pair("compiler", reinterpret_cast<void*>(NULL)));
Brian Carlstroma004aa92012-02-08 18:05:09 -0800659 std::vector<const DexFile*> boot_class_path;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700660 if (boot_image_option.empty()) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800661 OpenDexFiles(dex_filenames, dex_locations, boot_class_path);
662 options.push_back(std::make_pair("bootclasspath", &boot_class_path));
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700663 } else {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700664 options.push_back(std::make_pair(boot_image_option.c_str(), reinterpret_cast<void*>(NULL)));
665 }
Brian Carlstrom34f1fa42012-04-05 18:28:12 -0700666 if (host_prefix.get() != NULL) {
667 options.push_back(std::make_pair("host-prefix", host_prefix->c_str()));
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700668 }
jeffhao5d840402011-10-24 17:09:45 -0700669 for (size_t i = 0; i < runtime_args.size(); i++) {
670 options.push_back(std::make_pair(runtime_args[i], reinterpret_cast<void*>(NULL)));
671 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700672
Ian Rogers49c48942012-03-11 15:15:37 -0700673 UniquePtr<Dex2Oat> dex2oat(Dex2Oat::Create(options, instruction_set, thread_count,
674 support_debugging));
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700675
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800676 // If --image-classes was specified, calculate the full list of classes to include in the image
Brian Carlstromae826982011-11-09 01:33:42 -0800677 UniquePtr<const std::set<std::string> > image_classes(NULL);
678 if (image_classes_filename != NULL) {
679 image_classes.reset(dex2oat->GetImageClassDescriptors(image_classes_filename));
680 if (image_classes.get() == NULL) {
681 LOG(ERROR) << "Failed to create list of image classes from " << image_classes_filename;
682 return EXIT_FAILURE;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700683 }
684 }
685
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800686 std::vector<const DexFile*> dex_files;
687 if (boot_image_option.empty()) {
688 dex_files = Runtime::Current()->GetClassLinker()->GetBootClassPath();
689 } else {
690 if (dex_filenames.empty()) {
691 UniquePtr<ZipArchive> zip_archive(ZipArchive::OpenFromFd(zip_fd));
692 if (zip_archive.get() == NULL) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800693 LOG(ERROR) << "Failed to zip from file descriptor for " << zip_location;
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -0800694 return EXIT_FAILURE;
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800695 }
Brian Carlstroma004aa92012-02-08 18:05:09 -0800696 const DexFile* dex_file = DexFile::Open(*zip_archive.get(), zip_location);
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800697 if (dex_file == NULL) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800698 LOG(ERROR) << "Failed to open dex from file descriptor for zip file: " << zip_location;
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800699 return EXIT_FAILURE;
700 }
701 dex_files.push_back(dex_file);
702 } else {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800703 OpenDexFiles(dex_filenames, dex_locations, dex_files);
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800704 }
705 }
706
Brian Carlstromf5822582012-03-19 22:34:31 -0700707 UniquePtr<const Compiler> compiler(dex2oat->CreateOatFile(boot_image_option,
Brian Carlstrom34f1fa42012-04-05 18:28:12 -0700708 host_prefix.get(),
Brian Carlstromf5822582012-03-19 22:34:31 -0700709 dex_files,
710 oat_file.get(),
Logan Chien8b977d32012-02-21 19:14:55 +0800711#if defined(ART_USE_LLVM_COMPILER)
Brian Carlstromf5822582012-03-19 22:34:31 -0700712 bitcode_filename,
Logan Chien8b977d32012-02-21 19:14:55 +0800713#endif
Brian Carlstromf5822582012-03-19 22:34:31 -0700714 image,
Brian Carlstromba0668e2012-03-26 13:14:07 -0700715 image_classes.get(),
716 dump_stats,
717 dump_timings));
Brian Carlstromf5822582012-03-19 22:34:31 -0700718
719 if (compiler.get() == NULL) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800720 LOG(ERROR) << "Failed to create oat file: " << oat_location;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700721 return EXIT_FAILURE;
722 }
723
Brian Carlstromae826982011-11-09 01:33:42 -0800724 if (!image) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800725 LOG(INFO) << "Oat file written successfully: " << oat_location;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700726 return EXIT_SUCCESS;
727 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700728
Brian Carlstromae826982011-11-09 01:33:42 -0800729 if (!dex2oat->CreateImageFile(image_filename,
730 image_base,
731 image_classes.get(),
732 oat_filename,
Brian Carlstromf5822582012-03-19 22:34:31 -0700733 oat_location,
734 *compiler.get())) {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700735 return EXIT_FAILURE;
736 }
737
Brian Carlstromae826982011-11-09 01:33:42 -0800738 // We wrote the oat file successfully, and want to keep it.
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800739 LOG(INFO) << "Oat file written successfully: " << oat_filename;
740 LOG(INFO) << "Image written successfully: " << image_filename;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700741 return EXIT_SUCCESS;
742}
743
744} // namespace art
745
746int main(int argc, char** argv) {
747 return art::dex2oat(argc, argv);
748}