blob: 42a326671bfd084a6c023976ef238be761b03fb7 [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>
Brian Carlstrom6cd40e52012-05-03 14:15:11 -070019#include <sys/stat.h>
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070020
Brian Carlstromae826982011-11-09 01:33:42 -080021#include <iostream>
22#include <fstream>
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070023#include <string>
24#include <vector>
25
26#include "class_linker.h"
27#include "class_loader.h"
28#include "compiler.h"
Ian Rogers5e863dd2011-11-02 20:00:10 -070029#include "file.h"
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070030#include "image_writer.h"
Ian Rogers6f1dfe42011-12-08 17:28:34 -080031#include "leb128.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070032#include "oat_writer.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080033#include "object_utils.h"
Ian Rogers5e863dd2011-11-02 20:00:10 -070034#include "os.h"
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070035#include "runtime.h"
Brian Carlstroma004aa92012-02-08 18:05:09 -080036#include "stl_util.h"
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070037#include "stringpiece.h"
Elliott Hughesbb551fa2012-01-25 16:35:29 -080038#include "timing_logger.h"
Brian Carlstroma6cc8932012-01-04 14:44:07 -080039#include "zip_archive.h"
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070040
Elliott Hughesb08e8a32012-04-02 10:51:41 -070041#if defined(__APPLE__)
42#include "AvailabilityMacros.h" // For MAC_OS_X_VERSION_MAX_ALLOWED
43#endif
44
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070045namespace art {
46
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -080047static void UsageErrorV(const char* fmt, va_list ap) {
48 std::string error;
49 StringAppendV(&error, fmt, ap);
50 LOG(ERROR) << error;
51}
52
53static void UsageError(const char* fmt, ...) {
54 va_list ap;
55 va_start(ap, fmt);
56 UsageErrorV(fmt, ap);
57 va_end(ap);
58}
59
60static void Usage(const char* fmt, ...) {
61 va_list ap;
62 va_start(ap, fmt);
63 UsageErrorV(fmt, ap);
64 va_end(ap);
65
66 UsageError("Usage: dex2oat [options]...");
67 UsageError("");
68 UsageError(" --dex-file=<dex-file>: specifies a .dex file to compile.");
69 UsageError(" Example: --dex-file=/system/framework/core.jar");
70 UsageError("");
71 UsageError(" --zip-fd=<file-descriptor>: specifies a file descriptor of a zip file");
72 UsageError(" containing a classes.dex file to compile.");
73 UsageError(" Example: --zip-fd=5");
74 UsageError("");
75 UsageError(" --zip-location=<zip-location>: specifies a symbolic name for the file corresponding");
76 UsageError(" to the file descriptor specified by --zip-fd.");
77 UsageError(" Example: --zip-location=/system/app/Calculator.apk");
78 UsageError("");
79 UsageError(" --oat-file=<file.oat>: specifies the required oat filename.");
80 UsageError(" Example: --oat-file=/system/framework/boot.oat");
81 UsageError("");
82 UsageError(" --oat-location=<oat-name>: specifies a symbolic name for the file corresponding");
83 UsageError(" to the file descriptor specified by --oat-fd.");
84 UsageError(" Example: --oat-location=/data/art-cache/system@app@Calculator.apk.oat");
85 UsageError("");
Logan Chien8b977d32012-02-21 19:14:55 +080086#if defined(ART_USE_LLVM_COMPILER)
Logan Chien8b977d32012-02-21 19:14:55 +080087 UsageError(" --bitcode=<file.bc>: specifies the optional bitcode filename.");
88 UsageError(" Example: --bitcode=/system/framework/boot.bc");
89 UsageError("");
90#endif
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -080091 UsageError(" --image=<file.art>: specifies the output image filename.");
92 UsageError(" Example: --image=/system/framework/boot.art");
93 UsageError("");
94 UsageError(" --image-classes=<classname-file>: specifies classes to include in an image.");
95 UsageError(" Example: --image=frameworks/base/preloaded-classes");
96 UsageError("");
97 UsageError(" --base=<hex-address>: specifies the base address when creating a boot image.");
98 UsageError(" Example: --base=0x50000000");
99 UsageError("");
100 UsageError(" --boot-image=<file.art>: provide the image file for the boot class path.");
101 UsageError(" Example: --boot-image=/system/framework/boot.art");
102 UsageError(" Default: <host-prefix>/system/framework/boot.art");
103 UsageError("");
104 UsageError(" --host-prefix may be used to translate host paths to target paths during");
105 UsageError(" cross compilation.");
106 UsageError(" Example: --host-prefix=out/target/product/crespo");
107 UsageError(" Default: $ANDROID_PRODUCT_OUT");
108 UsageError("");
Ian Rogers49c48942012-03-11 15:15:37 -0700109 UsageError(" --instruction-set=(ARM|Thumb2|MIPS|X86): compile for a particular instruction");
110 UsageError(" set.");
111 UsageError(" Example: --instruction-set=X86");
112 UsageError(" Default: Thumb2");
113 UsageError("");
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800114 UsageError(" --runtime-arg <argument>: used to specify various arguments for the runtime,");
115 UsageError(" such as initial heap size, maximum heap size, and verbose output.");
116 UsageError(" Use a separate --runtime-arg switch for each argument.");
117 UsageError(" Example: --runtime-arg -Xms256m");
118 UsageError("");
119 std::cerr << "See log for usage error information\n";
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700120 exit(EXIT_FAILURE);
121}
122
Brian Carlstromae826982011-11-09 01:33:42 -0800123class Dex2Oat {
124 public:
125
Ian Rogers49c48942012-03-11 15:15:37 -0700126 static Dex2Oat* Create(Runtime::Options& options, InstructionSet instruction_set,
127 size_t thread_count, bool support_debugging) {
128 UniquePtr<Runtime> runtime(CreateRuntime(options, instruction_set));
Brian Carlstromae826982011-11-09 01:33:42 -0800129 if (runtime.get() == NULL) {
130 return NULL;
131 }
Ian Rogers49c48942012-03-11 15:15:37 -0700132 return new Dex2Oat(runtime.release(), instruction_set, thread_count, support_debugging);
Brian Carlstromae826982011-11-09 01:33:42 -0800133 }
134
135 ~Dex2Oat() {
136 delete runtime_;
Elliott Hughes5523ee02012-02-03 18:18:34 -0800137 LOG(INFO) << "dex2oat took " << PrettyDuration(NanoTime() - start_ns_) << " (threads: " << thread_count_ << ")";
Brian Carlstromae826982011-11-09 01:33:42 -0800138 }
139
140 // Make a list of descriptors for classes to include in the image
141 const std::set<std::string>* GetImageClassDescriptors(const char* image_classes_filename) {
142 UniquePtr<std::ifstream> image_classes_file(new std::ifstream(image_classes_filename, std::ifstream::in));
143 if (image_classes_file.get() == NULL) {
144 LOG(ERROR) << "Failed to open image classes file " << image_classes_filename;
145 return NULL;
146 }
147
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800148 // Load all the classes specified in the file
Brian Carlstromae826982011-11-09 01:33:42 -0800149 ClassLinker* class_linker = runtime_->GetClassLinker();
150 while (image_classes_file->good()) {
151 std::string dot;
152 std::getline(*image_classes_file.get(), dot);
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800153 if (StartsWith(dot, "#") || dot.empty()) {
Brian Carlstromae826982011-11-09 01:33:42 -0800154 continue;
155 }
Elliott Hughes95572412011-12-13 18:14:20 -0800156 std::string descriptor(DotToDescriptor(dot.c_str()));
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800157 SirtRef<Class> klass(class_linker->FindSystemClass(descriptor.c_str()));
Brian Carlstromae826982011-11-09 01:33:42 -0800158 if (klass.get() == NULL) {
159 LOG(WARNING) << "Failed to find class " << descriptor;
160 Thread::Current()->ClearException();
161 }
162 }
163 image_classes_file->close();
164
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800165 // Resolve exception classes referenced by the loaded classes. The catch logic assumes
166 // exceptions are resolved by the verifier when there is a catch block in an interested method.
167 // Do this here so that exception classes appear to have been specified image classes.
168 std::set<std::pair<uint16_t, const DexFile*> > unresolved_exception_types;
169 do {
170 unresolved_exception_types.clear();
171 class_linker->VisitClasses(ResolveCatchBlockExceptionsClassVisitor,
172 &unresolved_exception_types);
173 typedef std::set<std::pair<uint16_t, const DexFile*> >::const_iterator It; // TODO: C++0x auto
174 for (It it = unresolved_exception_types.begin(),
175 end = unresolved_exception_types.end();
176 it != end; ++it) {
177 uint16_t exception_type_idx = it->first;
178 const DexFile* dex_file = it->second;
179 DexCache* dex_cache = class_linker->FindDexCache(*dex_file);
180 ClassLoader* class_loader = NULL;
181 SirtRef<Class> klass(class_linker->ResolveType(*dex_file, exception_type_idx, dex_cache,
182 class_loader));
183 if (klass.get() == NULL) {
184 const DexFile::TypeId& type_id = dex_file->GetTypeId(exception_type_idx);
185 const char* descriptor = dex_file->GetTypeDescriptor(type_id);
186 LOG(FATAL) << "Failed to resolve class " << descriptor;
187 }
188 DCHECK(klass->IsThrowableClass());
189 }
190 // Resolving exceptions may load classes that reference more exceptions, iterate until no
191 // more are found
192 } while (!unresolved_exception_types.empty());
193
Brian Carlstromae826982011-11-09 01:33:42 -0800194 // We walk the roots looking for classes so that we'll pick up the
195 // above classes plus any classes them depend on such super
196 // classes, interfaces, and the required ClassLinker roots.
197 UniquePtr<std::set<std::string> > image_classes(new std::set<std::string>());
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800198 class_linker->VisitClasses(RecordImageClassesVisitor, image_classes.get());
Brian Carlstromae826982011-11-09 01:33:42 -0800199 CHECK_NE(image_classes->size(), 0U);
200 return image_classes.release();
201 }
202
Brian Carlstromf5822582012-03-19 22:34:31 -0700203 const Compiler* CreateOatFile(const std::string& boot_image_option,
Brian Carlstrom34f1fa42012-04-05 18:28:12 -0700204 const std::string* host_prefix,
Brian Carlstromf5822582012-03-19 22:34:31 -0700205 const std::vector<const DexFile*>& dex_files,
206 File* oat_file,
Logan Chien8b977d32012-02-21 19:14:55 +0800207#if defined(ART_USE_LLVM_COMPILER)
Logan Chiende08e842012-03-21 00:34:12 +0800208 const std::string& bitcode_filename,
Logan Chien8b977d32012-02-21 19:14:55 +0800209#endif
Brian Carlstromf5822582012-03-19 22:34:31 -0700210 bool image,
Brian Carlstromba0668e2012-03-26 13:14:07 -0700211 const std::set<std::string>* image_classes,
212 bool dump_stats,
213 bool dump_timings) {
Brian Carlstromae826982011-11-09 01:33:42 -0800214 // SirtRef and ClassLoader creation needs to come after Runtime::Create
215 UniquePtr<SirtRef<ClassLoader> > class_loader(new SirtRef<ClassLoader>(NULL));
216 if (class_loader.get() == NULL) {
217 LOG(ERROR) << "Failed to create SirtRef for class loader";
218 return false;
219 }
220
Brian Carlstromae826982011-11-09 01:33:42 -0800221 if (!boot_image_option.empty()) {
222 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromae826982011-11-09 01:33:42 -0800223 std::vector<const DexFile*> class_path_files(dex_files);
Brian Carlstroma004aa92012-02-08 18:05:09 -0800224 OpenClassPathFiles(runtime_->GetClassPathString(), class_path_files);
Brian Carlstromae826982011-11-09 01:33:42 -0800225 for (size_t i = 0; i < class_path_files.size(); i++) {
226 class_linker->RegisterDexFile(*class_path_files[i]);
227 }
228 class_loader.get()->reset(PathClassLoader::AllocCompileTime(class_path_files));
Brian Carlstromae826982011-11-09 01:33:42 -0800229 }
230
Brian Carlstromf5822582012-03-19 22:34:31 -0700231 UniquePtr<Compiler> compiler(new Compiler(instruction_set_,
232 image,
233 thread_count_,
234 support_debugging_,
Brian Carlstromba0668e2012-03-26 13:14:07 -0700235 image_classes,
236 dump_stats,
237 dump_timings));
Logan Chien8b977d32012-02-21 19:14:55 +0800238
239#if defined(ART_USE_LLVM_COMPILER)
Brian Carlstromf5822582012-03-19 22:34:31 -0700240 compiler->SetBitcodeFileName(bitcode_filename);
Logan Chien8b977d32012-02-21 19:14:55 +0800241#endif
242
Brian Carlstromf5822582012-03-19 22:34:31 -0700243 compiler->CompileAll(class_loader->get(), dex_files);
Brian Carlstromae826982011-11-09 01:33:42 -0800244
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700245 std::string image_file_location;
246 uint32_t image_file_location_checksum = 0;
247 Heap* heap = Runtime::Current()->GetHeap();
248 if (heap->GetSpaces().size() > 1) {
249 ImageSpace* image_space = heap->GetImageSpace();
250 image_file_location_checksum = image_space->GetImageHeader().GetOatChecksum();
251 image_file_location = image_space->GetImageFilename();
Brian Carlstrom34f1fa42012-04-05 18:28:12 -0700252 if (host_prefix != NULL && StartsWith(image_file_location, host_prefix->c_str())) {
253 image_file_location = image_file_location.substr(host_prefix->size());
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700254 }
255 }
256
257 if (!OatWriter::Create(oat_file,
258 class_loader->get(),
259 dex_files,
260 image_file_location_checksum,
261 image_file_location,
Brian Carlstromf5822582012-03-19 22:34:31 -0700262 *compiler.get())) {
Brian Carlstromae826982011-11-09 01:33:42 -0800263 LOG(ERROR) << "Failed to create oat file " << oat_file->name();
Brian Carlstromf5822582012-03-19 22:34:31 -0700264 return NULL;
Brian Carlstromae826982011-11-09 01:33:42 -0800265 }
Brian Carlstromf5822582012-03-19 22:34:31 -0700266 return compiler.release();
Brian Carlstromae826982011-11-09 01:33:42 -0800267 }
268
Brian Carlstroma004aa92012-02-08 18:05:09 -0800269 bool CreateImageFile(const std::string& image_filename,
Brian Carlstromae826982011-11-09 01:33:42 -0800270 uintptr_t image_base,
271 const std::set<std::string>* image_classes,
272 const std::string& oat_filename,
Brian Carlstromf5822582012-03-19 22:34:31 -0700273 const std::string& oat_location,
274 const Compiler& compiler) {
Brian Carlstromae826982011-11-09 01:33:42 -0800275 ImageWriter image_writer(image_classes);
Brian Carlstromf5822582012-03-19 22:34:31 -0700276 if (!image_writer.Write(image_filename, image_base, oat_filename, oat_location, compiler)) {
Brian Carlstromae826982011-11-09 01:33:42 -0800277 LOG(ERROR) << "Failed to create image file " << image_filename;
278 return false;
279 }
280 return true;
281 }
282
283 private:
284
Ian Rogers49c48942012-03-11 15:15:37 -0700285 explicit Dex2Oat(Runtime* runtime, InstructionSet instruction_set, size_t thread_count,
286 bool support_debugging)
287 : instruction_set_(instruction_set),
288 runtime_(runtime),
Elliott Hughesde6e4cf2012-02-27 14:46:06 -0800289 thread_count_(thread_count),
290 support_debugging_(support_debugging),
291 start_ns_(NanoTime()) {
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800292 }
Brian Carlstromae826982011-11-09 01:33:42 -0800293
Ian Rogers49c48942012-03-11 15:15:37 -0700294 static Runtime* CreateRuntime(Runtime::Options& options, InstructionSet instruction_set) {
Brian Carlstromae826982011-11-09 01:33:42 -0800295 Runtime* runtime = Runtime::Create(options, false);
296 if (runtime == NULL) {
297 LOG(ERROR) << "Failed to create runtime";
298 return NULL;
299 }
300
301 // if we loaded an existing image, we will reuse values from the image roots.
302 if (!runtime->HasJniDlsymLookupStub()) {
Ian Rogers49c48942012-03-11 15:15:37 -0700303 runtime->SetJniDlsymLookupStub(Compiler::CreateJniDlsymLookupStub(instruction_set));
Brian Carlstromae826982011-11-09 01:33:42 -0800304 }
305 if (!runtime->HasAbstractMethodErrorStubArray()) {
Ian Rogers49c48942012-03-11 15:15:37 -0700306 runtime->SetAbstractMethodErrorStubArray(Compiler::CreateAbstractMethodErrorStub(instruction_set));
Brian Carlstromae826982011-11-09 01:33:42 -0800307 }
308 for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) {
309 Runtime::TrampolineType type = Runtime::TrampolineType(i);
310 if (!runtime->HasResolutionStubArray(type)) {
Ian Rogers49c48942012-03-11 15:15:37 -0700311 runtime->SetResolutionStubArray(Compiler::CreateResolutionStub(instruction_set, type), type);
Brian Carlstromae826982011-11-09 01:33:42 -0800312 }
313 }
Ian Rogers19846512012-02-24 11:42:47 -0800314 if (!runtime->HasResolutionMethod()) {
315 runtime->SetResolutionMethod(runtime->CreateResolutionMethod());
316 }
Brian Carlstromae826982011-11-09 01:33:42 -0800317 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
318 Runtime::CalleeSaveType type = Runtime::CalleeSaveType(i);
319 if (!runtime->HasCalleeSaveMethod(type)) {
Ian Rogers49c48942012-03-11 15:15:37 -0700320 runtime->SetCalleeSaveMethod(runtime->CreateCalleeSaveMethod(instruction_set, type), type);
Brian Carlstromae826982011-11-09 01:33:42 -0800321 }
322 }
Ian Rogers19846512012-02-24 11:42:47 -0800323 runtime->GetClassLinker()->FixupDexCaches(runtime->GetResolutionMethod());
Brian Carlstromae826982011-11-09 01:33:42 -0800324 return runtime;
325 }
326
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800327 static void ResolveExceptionsForMethod(MethodHelper* mh,
328 std::set<std::pair<uint16_t, const DexFile*> >& exceptions_to_resolve) {
329 const DexFile::CodeItem* code_item = mh->GetCodeItem();
330 if (code_item == NULL) {
331 return; // native or abstract method
332 }
333 if (code_item->tries_size_ == 0) {
334 return; // nothing to process
335 }
336 const byte* encoded_catch_handler_list = DexFile::GetCatchHandlerData(*code_item, 0);
337 size_t num_encoded_catch_handlers = DecodeUnsignedLeb128(&encoded_catch_handler_list);
338 for (size_t i = 0; i < num_encoded_catch_handlers; i++) {
339 int32_t encoded_catch_handler_size = DecodeSignedLeb128(&encoded_catch_handler_list);
340 bool has_catch_all = false;
341 if (encoded_catch_handler_size <= 0) {
342 encoded_catch_handler_size = -encoded_catch_handler_size;
343 has_catch_all = true;
344 }
345 for (int32_t j = 0; j < encoded_catch_handler_size; j++) {
346 uint16_t encoded_catch_handler_handlers_type_idx =
347 DecodeUnsignedLeb128(&encoded_catch_handler_list);
348 // Add to set of types to resolve if not already in the dex cache resolved types
349 if (!mh->IsResolvedTypeIdx(encoded_catch_handler_handlers_type_idx)) {
350 exceptions_to_resolve.insert(
351 std::pair<uint16_t, const DexFile*>(encoded_catch_handler_handlers_type_idx,
352 &mh->GetDexFile()));
353 }
354 // ignore address associated with catch handler
355 DecodeUnsignedLeb128(&encoded_catch_handler_list);
356 }
357 if (has_catch_all) {
358 // ignore catch all address
359 DecodeUnsignedLeb128(&encoded_catch_handler_list);
360 }
361 }
362 }
363 static bool ResolveCatchBlockExceptionsClassVisitor(Class* c, void* arg) {
364 std::set<std::pair<uint16_t, const DexFile*> >* exceptions_to_resolve =
365 reinterpret_cast<std::set<std::pair<uint16_t, const DexFile*> >*>(arg);
366 MethodHelper mh;
367 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
368 Method* m = c->GetVirtualMethod(i);
369 mh.ChangeMethod(m);
370 ResolveExceptionsForMethod(&mh, *exceptions_to_resolve);
371 }
372 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
373 Method* m = c->GetDirectMethod(i);
374 mh.ChangeMethod(m);
375 ResolveExceptionsForMethod(&mh, *exceptions_to_resolve);
376 }
377 return true;
378 }
379 static bool RecordImageClassesVisitor(Class* klass, void* arg) {
Brian Carlstromae826982011-11-09 01:33:42 -0800380 std::set<std::string>* image_classes = reinterpret_cast<std::set<std::string>*>(arg);
381 if (klass->IsArrayClass() || klass->IsPrimitive()) {
Jesse Wilson254db0f2011-11-16 16:44:11 -0500382 return true;
383 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800384 image_classes->insert(ClassHelper(klass).GetDescriptor());
Brian Carlstromae826982011-11-09 01:33:42 -0800385 return true;
Jesse Wilson254db0f2011-11-16 16:44:11 -0500386 }
Jesse Wilson254db0f2011-11-16 16:44:11 -0500387
Brian Carlstromae826982011-11-09 01:33:42 -0800388 // Appends to dex_files any elements of class_path that it doesn't already
389 // contain. This will open those dex files as necessary.
390 static void OpenClassPathFiles(const std::string& class_path, std::vector<const DexFile*>& dex_files) {
391 std::vector<std::string> parsed;
392 Split(class_path, ':', parsed);
393 for (size_t i = 0; i < parsed.size(); ++i) {
394 if (DexFilesContains(dex_files, parsed[i])) {
395 continue;
396 }
Brian Carlstroma004aa92012-02-08 18:05:09 -0800397 const DexFile* dex_file = DexFile::Open(parsed[i], parsed[i]);
Brian Carlstromae826982011-11-09 01:33:42 -0800398 if (dex_file == NULL) {
399 LOG(WARNING) << "Failed to open dex file " << parsed[i];
400 } else {
401 dex_files.push_back(dex_file);
402 }
Jesse Wilson254db0f2011-11-16 16:44:11 -0500403 }
404 }
Brian Carlstromae826982011-11-09 01:33:42 -0800405
406 // Returns true if dex_files has a dex with the named location.
407 static bool DexFilesContains(const std::vector<const DexFile*>& dex_files, const std::string& location) {
408 for (size_t i = 0; i < dex_files.size(); ++i) {
409 if (dex_files[i]->GetLocation() == location) {
410 return true;
411 }
412 }
413 return false;
414 }
415
Ian Rogers49c48942012-03-11 15:15:37 -0700416 const InstructionSet instruction_set_;
Brian Carlstromae826982011-11-09 01:33:42 -0800417
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800418 Runtime* runtime_;
Elliott Hughes5523ee02012-02-03 18:18:34 -0800419 size_t thread_count_;
Elliott Hughesde6e4cf2012-02-27 14:46:06 -0800420 bool support_debugging_;
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800421 uint64_t start_ns_;
422
Brian Carlstromae826982011-11-09 01:33:42 -0800423 DISALLOW_IMPLICIT_CONSTRUCTORS(Dex2Oat);
424};
Jesse Wilson254db0f2011-11-16 16:44:11 -0500425
Elliott Hughes72395bf2012-04-24 13:45:26 -0700426static bool ParseInt(const char* in, int* out) {
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800427 char* end;
428 int result = strtol(in, &end, 10);
429 if (in == end || *end != '\0') {
430 return false;
431 }
432 *out = result;
433 return true;
434}
435
Elliott Hughes72395bf2012-04-24 13:45:26 -0700436static void OpenDexFiles(const std::vector<const char*>& dex_filenames,
437 const std::vector<const char*>& dex_locations,
438 std::vector<const DexFile*>& dex_files) {
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800439 for (size_t i = 0; i < dex_filenames.size(); i++) {
440 const char* dex_filename = dex_filenames[i];
Brian Carlstroma004aa92012-02-08 18:05:09 -0800441 const char* dex_location = dex_locations[i];
442 const DexFile* dex_file = DexFile::Open(dex_filename, dex_location);
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800443 if (dex_file == NULL) {
jeffhao60f83e32012-02-13 17:16:30 -0800444 LOG(WARNING) << "could not open .dex from file " << dex_filename;
445 } else {
446 dex_files.push_back(dex_file);
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800447 }
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800448 }
449}
450
Elliott Hughes72395bf2012-04-24 13:45:26 -0700451static int dex2oat(int argc, char** argv) {
452 InitLogging();
453
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700454 // Skip over argv[0].
455 argv++;
456 argc--;
457
458 if (argc == 0) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800459 Usage("no arguments specified");
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700460 }
461
462 std::vector<const char*> dex_filenames;
Brian Carlstroma004aa92012-02-08 18:05:09 -0800463 std::vector<const char*> dex_locations;
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800464 int zip_fd = -1;
Brian Carlstroma004aa92012-02-08 18:05:09 -0800465 std::string zip_location;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700466 std::string oat_filename;
Brian Carlstroma004aa92012-02-08 18:05:09 -0800467 std::string oat_location;
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800468 int oat_fd = -1;
Logan Chien8b977d32012-02-21 19:14:55 +0800469#if defined(ART_USE_LLVM_COMPILER)
Logan Chien8b977d32012-02-21 19:14:55 +0800470 std::string bitcode_filename;
471#endif
Brian Carlstromae826982011-11-09 01:33:42 -0800472 const char* image_classes_filename = NULL;
Brian Carlstroma004aa92012-02-08 18:05:09 -0800473 std::string image_filename;
Brian Carlstromb0011262011-12-09 12:17:24 -0800474 std::string boot_image_filename;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700475 uintptr_t image_base = 0;
Brian Carlstrom34f1fa42012-04-05 18:28:12 -0700476 UniquePtr<std::string> host_prefix;
jeffhao5d840402011-10-24 17:09:45 -0700477 std::vector<const char*> runtime_args;
Elliott Hughes5523ee02012-02-03 18:18:34 -0800478 int thread_count = 2;
Elliott Hughesde6e4cf2012-02-27 14:46:06 -0800479 bool support_debugging = false;
Ian Rogers49c48942012-03-11 15:15:37 -0700480 InstructionSet instruction_set = kThumb2;
Elliott Hughes67d92002012-03-26 15:08:51 -0700481 bool dump_stats = kIsDebugBuild;
482 bool dump_timings = kIsDebugBuild;
Brian Carlstrom16192862011-09-12 17:50:06 -0700483
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700484 for (int i = 0; i < argc; i++) {
485 const StringPiece option(argv[i]);
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800486 bool log_options = false;
487 if (log_options) {
Brian Carlstromb7bbba42011-10-13 14:58:47 -0700488 LOG(INFO) << "dex2oat: option[" << i << "]=" << argv[i];
489 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700490 if (option.starts_with("--dex-file=")) {
491 dex_filenames.push_back(option.substr(strlen("--dex-file=")).data());
Brian Carlstroma004aa92012-02-08 18:05:09 -0800492 } else if (option.starts_with("--dex-location=")) {
493 dex_locations.push_back(option.substr(strlen("--dex-location=")).data());
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800494 } else if (option.starts_with("--zip-fd=")) {
495 const char* zip_fd_str = option.substr(strlen("--zip-fd=")).data();
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800496 if (!ParseInt(zip_fd_str, &zip_fd)) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800497 Usage("could not parse --zip-fd argument '%s' as an integer", zip_fd_str);
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800498 }
Brian Carlstroma004aa92012-02-08 18:05:09 -0800499 } else if (option.starts_with("--zip-location=")) {
500 zip_location = option.substr(strlen("--zip-location=")).data();
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800501 } else if (option.starts_with("--oat-file=")) {
502 oat_filename = option.substr(strlen("--oat-file=")).data();
503 } else if (option.starts_with("--oat-fd=")) {
504 const char* oat_fd_str = option.substr(strlen("--oat-fd=")).data();
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800505 if (!ParseInt(oat_fd_str, &oat_fd)) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800506 Usage("could not parse --oat-fd argument '%s' as an integer", oat_fd_str);
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800507 }
Elliott Hughesde6e4cf2012-02-27 14:46:06 -0800508 } else if (option.starts_with("-g")) {
509 support_debugging = true;
Elliott Hughes5523ee02012-02-03 18:18:34 -0800510 } else if (option.starts_with("-j")) {
Brian Carlstromb12552a2012-02-04 17:17:31 -0800511 const char* thread_count_str = option.substr(strlen("-j")).data();
Elliott Hughes5523ee02012-02-03 18:18:34 -0800512 if (!ParseInt(thread_count_str, &thread_count)) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800513 Usage("could not parse -j argument '%s' as an integer", thread_count_str);
Elliott Hughes5523ee02012-02-03 18:18:34 -0800514 }
Brian Carlstroma004aa92012-02-08 18:05:09 -0800515 } else if (option.starts_with("--oat-location=")) {
516 oat_location = option.substr(strlen("--oat-location=")).data();
Logan Chien8b977d32012-02-21 19:14:55 +0800517#if defined(ART_USE_LLVM_COMPILER)
Logan Chien8b977d32012-02-21 19:14:55 +0800518 } else if (option.starts_with("--bitcode=")) {
519 bitcode_filename = option.substr(strlen("--bitcode=")).data();
520#endif
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700521 } else if (option.starts_with("--image=")) {
522 image_filename = option.substr(strlen("--image=")).data();
Brian Carlstromae826982011-11-09 01:33:42 -0800523 } else if (option.starts_with("--image-classes=")) {
524 image_classes_filename = option.substr(strlen("--image-classes=")).data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700525 } else if (option.starts_with("--base=")) {
526 const char* image_base_str = option.substr(strlen("--base=")).data();
527 char* end;
528 image_base = strtoul(image_base_str, &end, 16);
529 if (end == image_base_str || *end != '\0') {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800530 Usage("Failed to parse hexadecimal value for option %s", option.data());
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700531 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700532 } else if (option.starts_with("--boot-image=")) {
Brian Carlstromb0011262011-12-09 12:17:24 -0800533 boot_image_filename = option.substr(strlen("--boot-image=")).data();
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700534 } else if (option.starts_with("--host-prefix=")) {
Brian Carlstrom34f1fa42012-04-05 18:28:12 -0700535 host_prefix.reset(new std::string(option.substr(strlen("--host-prefix=")).data()));
Ian Rogers49c48942012-03-11 15:15:37 -0700536 } else if (option.starts_with("--instruction-set=")) {
537 StringPiece instruction_set_str = option.substr(strlen("--instruction-set=")).data();
538 if (instruction_set_str == "Thumb2" || instruction_set_str == "ARM") {
539 instruction_set = kThumb2;
540 } else if (instruction_set_str == "MIPS") {
541 instruction_set = kMips;
542 } else if (instruction_set_str == "X86") {
543 instruction_set = kX86;
544 }
jeffhao5d840402011-10-24 17:09:45 -0700545 } else if (option == "--runtime-arg") {
546 if (++i >= argc) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800547 Usage("Missing required argument for --runtime-arg");
jeffhao5d840402011-10-24 17:09:45 -0700548 }
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800549 if (log_options) {
550 LOG(INFO) << "dex2oat: option[" << i << "]=" << argv[i];
551 }
jeffhao5d840402011-10-24 17:09:45 -0700552 runtime_args.push_back(argv[i]);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700553 } else {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800554 Usage("unknown argument %s", option.data());
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700555 }
556 }
557
Elliott Hughesb907a3f2012-04-02 16:11:36 -0700558#if defined(__APPLE__) && MAC_OS_X_VERSION_MAX_ALLOWED < 1060
559 // Something is broken on Mac OS 10.5, and we don't have direct access to any machines running it.
560 // Restricting dex2oat to a single thread on Mac OS 10.5 appears to be a workaround.
561 thread_count = 1;
562#endif
563
Logan Chien08e1ba32012-05-08 15:08:51 +0800564#if defined(ART_USE_LLVM_COMPILER) && defined(ART_TARGET)
565 // To avoid high memory usage, always run dex2oat in single thread mode when
566 // we are using LLVM-based compiler.
567 thread_count = 1;
568#endif
569
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800570 if (oat_filename.empty() && oat_fd == -1) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800571 Usage("Output must be supplied with either --oat-file or --oat-fd");
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800572 }
573
574 if (!oat_filename.empty() && oat_fd != -1) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800575 Usage("--oat-file should not be used with --oat-fd");
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800576 }
577
578 if (!oat_filename.empty() && oat_fd != -1) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800579 Usage("--oat-file should not be used with --oat-fd");
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800580 }
581
Brian Carlstroma004aa92012-02-08 18:05:09 -0800582 if (oat_fd != -1 && !image_filename.empty()) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800583 Usage("--oat-fd should not be used with --image");
Brian Carlstrome24fa612011-09-29 00:53:55 -0700584 }
585
Brian Carlstrom34f1fa42012-04-05 18:28:12 -0700586 if (host_prefix.get() == NULL) {
Brian Carlstromb0011262011-12-09 12:17:24 -0800587 const char* android_product_out = getenv("ANDROID_PRODUCT_OUT");
588 if (android_product_out != NULL) {
Brian Carlstrom34f1fa42012-04-05 18:28:12 -0700589 host_prefix.reset(new std::string(android_product_out));
Brian Carlstromb0011262011-12-09 12:17:24 -0800590 }
591 }
592
Brian Carlstroma004aa92012-02-08 18:05:09 -0800593 bool image = (!image_filename.empty());
Brian Carlstromb0011262011-12-09 12:17:24 -0800594 if (!image && boot_image_filename.empty()) {
Brian Carlstrom34f1fa42012-04-05 18:28:12 -0700595 if (host_prefix.get() == NULL) {
Brian Carlstroma56fcd62012-02-04 21:23:01 -0800596 boot_image_filename += GetAndroidRoot();
597 } else {
Brian Carlstrom34f1fa42012-04-05 18:28:12 -0700598 boot_image_filename += *host_prefix.get();
Brian Carlstroma56fcd62012-02-04 21:23:01 -0800599 boot_image_filename += "/system";
600 }
601 boot_image_filename += "/framework/boot.art";
Brian Carlstromb0011262011-12-09 12:17:24 -0800602 }
603 std::string boot_image_option;
Brian Carlstroma004aa92012-02-08 18:05:09 -0800604 if (!boot_image_filename.empty()) {
Brian Carlstromb0011262011-12-09 12:17:24 -0800605 boot_image_option += "-Ximage:";
606 boot_image_option += boot_image_filename;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700607 }
608
Brian Carlstromae826982011-11-09 01:33:42 -0800609 if (image_classes_filename != NULL && !image) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800610 Usage("--image-classes should only be used with --image");
Brian Carlstromae826982011-11-09 01:33:42 -0800611 }
612
613 if (image_classes_filename != NULL && !boot_image_option.empty()) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800614 Usage("--image-classes should not be used with --boot-image");
Brian Carlstrom78128a62011-09-15 17:21:19 -0700615 }
616
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800617 if (dex_filenames.empty() && zip_fd == -1) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800618 Usage("Input must be supplied with either --dex-file or --zip-fd");
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800619 }
620
621 if (!dex_filenames.empty() && zip_fd != -1) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800622 Usage("--dex-file should not be used with --zip-fd");
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800623 }
624
Brian Carlstroma004aa92012-02-08 18:05:09 -0800625 if (!dex_filenames.empty() && !zip_location.empty()) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800626 Usage("--dex-file should not be used with --zip-location");
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800627 }
628
Brian Carlstroma004aa92012-02-08 18:05:09 -0800629 if (dex_locations.empty()) {
630 for (size_t i = 0; i < dex_filenames.size(); i++) {
631 dex_locations.push_back(dex_filenames[i]);
632 }
633 } else if (dex_locations.size() != dex_filenames.size()) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800634 Usage("--dex-location arguments do not match --dex-file arguments");
Brian Carlstroma004aa92012-02-08 18:05:09 -0800635 }
636
637 if (zip_fd != -1 && zip_location.empty()) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800638 Usage("--zip-location should be supplied with --zip-fd");
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800639 }
640
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700641 if (boot_image_option.empty()) {
642 if (image_base == 0) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800643 Usage("non-zero --base not specified");
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700644 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700645 }
646
Brian Carlstrom6ef827a2011-12-11 14:57:47 -0800647 // Check early that the result of compilation can be written
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800648 UniquePtr<File> oat_file;
Brian Carlstrom6cd40e52012-05-03 14:15:11 -0700649 bool create_file = !oat_filename.empty(); // as opposed to using open file descriptor
650 if (create_file) {
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800651 oat_file.reset(OS::OpenFile(oat_filename.c_str(), true));
Brian Carlstroma004aa92012-02-08 18:05:09 -0800652 if (oat_location.empty()) {
653 oat_location = oat_filename;
654 }
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800655 } else {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800656 oat_file.reset(OS::FileFromFd(oat_location.c_str(), oat_fd));
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800657 }
Brian Carlstrom6ef827a2011-12-11 14:57:47 -0800658 if (oat_file.get() == NULL) {
Brian Carlstrom6cd40e52012-05-03 14:15:11 -0700659 PLOG(ERROR) << "Failed to create oat file: " << oat_location;
660 return EXIT_FAILURE;
661 }
662 if (create_file && fchmod(oat_file->Fd(), 0644) != 0) {
663 PLOG(ERROR) << "Failed to make oat file world readable: " << oat_location;
Brian Carlstrom6ef827a2011-12-11 14:57:47 -0800664 return EXIT_FAILURE;
Ian Rogers5e863dd2011-11-02 20:00:10 -0700665 }
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800666
Brian Carlstroma004aa92012-02-08 18:05:09 -0800667 LOG(INFO) << "dex2oat: " << oat_location;
Ian Rogers5e863dd2011-11-02 20:00:10 -0700668
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700669 Runtime::Options options;
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700670 options.push_back(std::make_pair("compiler", reinterpret_cast<void*>(NULL)));
Brian Carlstroma004aa92012-02-08 18:05:09 -0800671 std::vector<const DexFile*> boot_class_path;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700672 if (boot_image_option.empty()) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800673 OpenDexFiles(dex_filenames, dex_locations, boot_class_path);
674 options.push_back(std::make_pair("bootclasspath", &boot_class_path));
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700675 } else {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700676 options.push_back(std::make_pair(boot_image_option.c_str(), reinterpret_cast<void*>(NULL)));
677 }
Brian Carlstrom34f1fa42012-04-05 18:28:12 -0700678 if (host_prefix.get() != NULL) {
679 options.push_back(std::make_pair("host-prefix", host_prefix->c_str()));
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700680 }
jeffhao5d840402011-10-24 17:09:45 -0700681 for (size_t i = 0; i < runtime_args.size(); i++) {
682 options.push_back(std::make_pair(runtime_args[i], reinterpret_cast<void*>(NULL)));
683 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700684
Ian Rogers49c48942012-03-11 15:15:37 -0700685 UniquePtr<Dex2Oat> dex2oat(Dex2Oat::Create(options, instruction_set, thread_count,
686 support_debugging));
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700687
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800688 // If --image-classes was specified, calculate the full list of classes to include in the image
Brian Carlstromae826982011-11-09 01:33:42 -0800689 UniquePtr<const std::set<std::string> > image_classes(NULL);
690 if (image_classes_filename != NULL) {
691 image_classes.reset(dex2oat->GetImageClassDescriptors(image_classes_filename));
692 if (image_classes.get() == NULL) {
693 LOG(ERROR) << "Failed to create list of image classes from " << image_classes_filename;
694 return EXIT_FAILURE;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700695 }
696 }
697
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800698 std::vector<const DexFile*> dex_files;
699 if (boot_image_option.empty()) {
700 dex_files = Runtime::Current()->GetClassLinker()->GetBootClassPath();
701 } else {
702 if (dex_filenames.empty()) {
703 UniquePtr<ZipArchive> zip_archive(ZipArchive::OpenFromFd(zip_fd));
704 if (zip_archive.get() == NULL) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800705 LOG(ERROR) << "Failed to zip from file descriptor for " << zip_location;
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -0800706 return EXIT_FAILURE;
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800707 }
Brian Carlstroma004aa92012-02-08 18:05:09 -0800708 const DexFile* dex_file = DexFile::Open(*zip_archive.get(), zip_location);
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800709 if (dex_file == NULL) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800710 LOG(ERROR) << "Failed to open dex from file descriptor for zip file: " << zip_location;
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800711 return EXIT_FAILURE;
712 }
713 dex_files.push_back(dex_file);
714 } else {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800715 OpenDexFiles(dex_filenames, dex_locations, dex_files);
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800716 }
717 }
718
Brian Carlstromf5822582012-03-19 22:34:31 -0700719 UniquePtr<const Compiler> compiler(dex2oat->CreateOatFile(boot_image_option,
Brian Carlstrom34f1fa42012-04-05 18:28:12 -0700720 host_prefix.get(),
Brian Carlstromf5822582012-03-19 22:34:31 -0700721 dex_files,
722 oat_file.get(),
Logan Chien8b977d32012-02-21 19:14:55 +0800723#if defined(ART_USE_LLVM_COMPILER)
Brian Carlstromf5822582012-03-19 22:34:31 -0700724 bitcode_filename,
Logan Chien8b977d32012-02-21 19:14:55 +0800725#endif
Brian Carlstromf5822582012-03-19 22:34:31 -0700726 image,
Brian Carlstromba0668e2012-03-26 13:14:07 -0700727 image_classes.get(),
728 dump_stats,
729 dump_timings));
Brian Carlstromf5822582012-03-19 22:34:31 -0700730
731 if (compiler.get() == NULL) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800732 LOG(ERROR) << "Failed to create oat file: " << oat_location;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700733 return EXIT_FAILURE;
734 }
735
Brian Carlstromae826982011-11-09 01:33:42 -0800736 if (!image) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800737 LOG(INFO) << "Oat file written successfully: " << oat_location;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700738 return EXIT_SUCCESS;
739 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700740
Brian Carlstromae826982011-11-09 01:33:42 -0800741 if (!dex2oat->CreateImageFile(image_filename,
742 image_base,
743 image_classes.get(),
744 oat_filename,
Brian Carlstromf5822582012-03-19 22:34:31 -0700745 oat_location,
746 *compiler.get())) {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700747 return EXIT_FAILURE;
748 }
749
Brian Carlstromae826982011-11-09 01:33:42 -0800750 // We wrote the oat file successfully, and want to keep it.
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800751 LOG(INFO) << "Oat file written successfully: " << oat_filename;
752 LOG(INFO) << "Image written successfully: " << image_filename;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700753 return EXIT_SUCCESS;
754}
755
756} // namespace art
757
758int main(int argc, char** argv) {
759 return art::dex2oat(argc, argv);
760}