blob: 85c11a099eb278e9595f4a5db1f4e49cf621f387 [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("");
jeffhao1f71ae82012-05-24 16:08:24 -0700109 UsageError(" --instruction-set=(arm|mips|x86): compile for a particular instruction");
Ian Rogers49c48942012-03-11 15:15:37 -0700110 UsageError(" set.");
jeffhao1f71ae82012-05-24 16:08:24 -0700111 UsageError(" Example: --instruction-set=x86");
112 UsageError(" Default: arm");
Ian Rogers49c48942012-03-11 15:15:37 -0700113 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();
jeffhao1f71ae82012-05-24 16:08:24 -0700538 if (instruction_set_str == "arm") {
Ian Rogers49c48942012-03-11 15:15:37 -0700539 instruction_set = kThumb2;
jeffhao1f71ae82012-05-24 16:08:24 -0700540 } else if (instruction_set_str == "mips") {
Ian Rogers49c48942012-03-11 15:15:37 -0700541 instruction_set = kMips;
jeffhao1f71ae82012-05-24 16:08:24 -0700542 } else if (instruction_set_str == "x86") {
Ian Rogers49c48942012-03-11 15:15:37 -0700543 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
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800564 if (oat_filename.empty() && oat_fd == -1) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800565 Usage("Output must be supplied with either --oat-file or --oat-fd");
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800566 }
567
568 if (!oat_filename.empty() && oat_fd != -1) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800569 Usage("--oat-file should not be used with --oat-fd");
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800570 }
571
572 if (!oat_filename.empty() && oat_fd != -1) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800573 Usage("--oat-file should not be used with --oat-fd");
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800574 }
575
Brian Carlstroma004aa92012-02-08 18:05:09 -0800576 if (oat_fd != -1 && !image_filename.empty()) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800577 Usage("--oat-fd should not be used with --image");
Brian Carlstrome24fa612011-09-29 00:53:55 -0700578 }
579
Brian Carlstrom34f1fa42012-04-05 18:28:12 -0700580 if (host_prefix.get() == NULL) {
Brian Carlstromb0011262011-12-09 12:17:24 -0800581 const char* android_product_out = getenv("ANDROID_PRODUCT_OUT");
582 if (android_product_out != NULL) {
Brian Carlstrom34f1fa42012-04-05 18:28:12 -0700583 host_prefix.reset(new std::string(android_product_out));
Brian Carlstromb0011262011-12-09 12:17:24 -0800584 }
585 }
586
Brian Carlstroma004aa92012-02-08 18:05:09 -0800587 bool image = (!image_filename.empty());
Brian Carlstromb0011262011-12-09 12:17:24 -0800588 if (!image && boot_image_filename.empty()) {
Brian Carlstrom34f1fa42012-04-05 18:28:12 -0700589 if (host_prefix.get() == NULL) {
Brian Carlstroma56fcd62012-02-04 21:23:01 -0800590 boot_image_filename += GetAndroidRoot();
591 } else {
Brian Carlstrom34f1fa42012-04-05 18:28:12 -0700592 boot_image_filename += *host_prefix.get();
Brian Carlstroma56fcd62012-02-04 21:23:01 -0800593 boot_image_filename += "/system";
594 }
595 boot_image_filename += "/framework/boot.art";
Brian Carlstromb0011262011-12-09 12:17:24 -0800596 }
597 std::string boot_image_option;
Brian Carlstroma004aa92012-02-08 18:05:09 -0800598 if (!boot_image_filename.empty()) {
Brian Carlstromb0011262011-12-09 12:17:24 -0800599 boot_image_option += "-Ximage:";
600 boot_image_option += boot_image_filename;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700601 }
602
Brian Carlstromae826982011-11-09 01:33:42 -0800603 if (image_classes_filename != NULL && !image) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800604 Usage("--image-classes should only be used with --image");
Brian Carlstromae826982011-11-09 01:33:42 -0800605 }
606
607 if (image_classes_filename != NULL && !boot_image_option.empty()) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800608 Usage("--image-classes should not be used with --boot-image");
Brian Carlstrom78128a62011-09-15 17:21:19 -0700609 }
610
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800611 if (dex_filenames.empty() && zip_fd == -1) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800612 Usage("Input must be supplied with either --dex-file or --zip-fd");
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800613 }
614
615 if (!dex_filenames.empty() && zip_fd != -1) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800616 Usage("--dex-file should not be used with --zip-fd");
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800617 }
618
Brian Carlstroma004aa92012-02-08 18:05:09 -0800619 if (!dex_filenames.empty() && !zip_location.empty()) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800620 Usage("--dex-file should not be used with --zip-location");
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800621 }
622
Brian Carlstroma004aa92012-02-08 18:05:09 -0800623 if (dex_locations.empty()) {
624 for (size_t i = 0; i < dex_filenames.size(); i++) {
625 dex_locations.push_back(dex_filenames[i]);
626 }
627 } else if (dex_locations.size() != dex_filenames.size()) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800628 Usage("--dex-location arguments do not match --dex-file arguments");
Brian Carlstroma004aa92012-02-08 18:05:09 -0800629 }
630
631 if (zip_fd != -1 && zip_location.empty()) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800632 Usage("--zip-location should be supplied with --zip-fd");
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800633 }
634
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700635 if (boot_image_option.empty()) {
636 if (image_base == 0) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800637 Usage("non-zero --base not specified");
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700638 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700639 }
640
Brian Carlstrom6ef827a2011-12-11 14:57:47 -0800641 // Check early that the result of compilation can be written
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800642 UniquePtr<File> oat_file;
Brian Carlstrom6cd40e52012-05-03 14:15:11 -0700643 bool create_file = !oat_filename.empty(); // as opposed to using open file descriptor
644 if (create_file) {
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800645 oat_file.reset(OS::OpenFile(oat_filename.c_str(), true));
Brian Carlstroma004aa92012-02-08 18:05:09 -0800646 if (oat_location.empty()) {
647 oat_location = oat_filename;
648 }
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800649 } else {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800650 oat_file.reset(OS::FileFromFd(oat_location.c_str(), oat_fd));
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800651 }
Brian Carlstrom6ef827a2011-12-11 14:57:47 -0800652 if (oat_file.get() == NULL) {
Brian Carlstrom6cd40e52012-05-03 14:15:11 -0700653 PLOG(ERROR) << "Failed to create oat file: " << oat_location;
654 return EXIT_FAILURE;
655 }
656 if (create_file && fchmod(oat_file->Fd(), 0644) != 0) {
657 PLOG(ERROR) << "Failed to make oat file world readable: " << oat_location;
Brian Carlstrom6ef827a2011-12-11 14:57:47 -0800658 return EXIT_FAILURE;
Ian Rogers5e863dd2011-11-02 20:00:10 -0700659 }
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800660
Brian Carlstroma004aa92012-02-08 18:05:09 -0800661 LOG(INFO) << "dex2oat: " << oat_location;
Ian Rogers5e863dd2011-11-02 20:00:10 -0700662
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700663 Runtime::Options options;
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700664 options.push_back(std::make_pair("compiler", reinterpret_cast<void*>(NULL)));
Brian Carlstroma004aa92012-02-08 18:05:09 -0800665 std::vector<const DexFile*> boot_class_path;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700666 if (boot_image_option.empty()) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800667 OpenDexFiles(dex_filenames, dex_locations, boot_class_path);
668 options.push_back(std::make_pair("bootclasspath", &boot_class_path));
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700669 } else {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700670 options.push_back(std::make_pair(boot_image_option.c_str(), reinterpret_cast<void*>(NULL)));
671 }
Brian Carlstrom34f1fa42012-04-05 18:28:12 -0700672 if (host_prefix.get() != NULL) {
673 options.push_back(std::make_pair("host-prefix", host_prefix->c_str()));
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700674 }
jeffhao5d840402011-10-24 17:09:45 -0700675 for (size_t i = 0; i < runtime_args.size(); i++) {
676 options.push_back(std::make_pair(runtime_args[i], reinterpret_cast<void*>(NULL)));
677 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700678
Ian Rogers49c48942012-03-11 15:15:37 -0700679 UniquePtr<Dex2Oat> dex2oat(Dex2Oat::Create(options, instruction_set, thread_count,
680 support_debugging));
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700681
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800682 // If --image-classes was specified, calculate the full list of classes to include in the image
Brian Carlstromae826982011-11-09 01:33:42 -0800683 UniquePtr<const std::set<std::string> > image_classes(NULL);
684 if (image_classes_filename != NULL) {
685 image_classes.reset(dex2oat->GetImageClassDescriptors(image_classes_filename));
686 if (image_classes.get() == NULL) {
687 LOG(ERROR) << "Failed to create list of image classes from " << image_classes_filename;
688 return EXIT_FAILURE;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700689 }
690 }
691
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800692 std::vector<const DexFile*> dex_files;
693 if (boot_image_option.empty()) {
694 dex_files = Runtime::Current()->GetClassLinker()->GetBootClassPath();
695 } else {
696 if (dex_filenames.empty()) {
697 UniquePtr<ZipArchive> zip_archive(ZipArchive::OpenFromFd(zip_fd));
698 if (zip_archive.get() == NULL) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800699 LOG(ERROR) << "Failed to zip from file descriptor for " << zip_location;
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -0800700 return EXIT_FAILURE;
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800701 }
Brian Carlstroma004aa92012-02-08 18:05:09 -0800702 const DexFile* dex_file = DexFile::Open(*zip_archive.get(), zip_location);
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800703 if (dex_file == NULL) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800704 LOG(ERROR) << "Failed to open dex from file descriptor for zip file: " << zip_location;
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800705 return EXIT_FAILURE;
706 }
707 dex_files.push_back(dex_file);
708 } else {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800709 OpenDexFiles(dex_filenames, dex_locations, dex_files);
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800710 }
711 }
712
Brian Carlstromf5822582012-03-19 22:34:31 -0700713 UniquePtr<const Compiler> compiler(dex2oat->CreateOatFile(boot_image_option,
Brian Carlstrom34f1fa42012-04-05 18:28:12 -0700714 host_prefix.get(),
Brian Carlstromf5822582012-03-19 22:34:31 -0700715 dex_files,
716 oat_file.get(),
Logan Chien8b977d32012-02-21 19:14:55 +0800717#if defined(ART_USE_LLVM_COMPILER)
Brian Carlstromf5822582012-03-19 22:34:31 -0700718 bitcode_filename,
Logan Chien8b977d32012-02-21 19:14:55 +0800719#endif
Brian Carlstromf5822582012-03-19 22:34:31 -0700720 image,
Brian Carlstromba0668e2012-03-26 13:14:07 -0700721 image_classes.get(),
722 dump_stats,
723 dump_timings));
Brian Carlstromf5822582012-03-19 22:34:31 -0700724
725 if (compiler.get() == NULL) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800726 LOG(ERROR) << "Failed to create oat file: " << oat_location;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700727 return EXIT_FAILURE;
728 }
729
Brian Carlstromae826982011-11-09 01:33:42 -0800730 if (!image) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800731 LOG(INFO) << "Oat file written successfully: " << oat_location;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700732 return EXIT_SUCCESS;
733 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700734
Brian Carlstromae826982011-11-09 01:33:42 -0800735 if (!dex2oat->CreateImageFile(image_filename,
736 image_base,
737 image_classes.get(),
738 oat_filename,
Brian Carlstromf5822582012-03-19 22:34:31 -0700739 oat_location,
740 *compiler.get())) {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700741 return EXIT_FAILURE;
742 }
743
Brian Carlstromae826982011-11-09 01:33:42 -0800744 // We wrote the oat file successfully, and want to keep it.
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800745 LOG(INFO) << "Oat file written successfully: " << oat_filename;
746 LOG(INFO) << "Image written successfully: " << image_filename;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700747 return EXIT_SUCCESS;
748}
749
750} // namespace art
751
752int main(int argc, char** argv) {
753 return art::dex2oat(argc, argv);
754}