blob: 660115c7d3833e76a96f0e8f7d8c33964aef0419 [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
41namespace art {
42
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -080043static void UsageErrorV(const char* fmt, va_list ap) {
44 std::string error;
45 StringAppendV(&error, fmt, ap);
46 LOG(ERROR) << error;
47}
48
49static void UsageError(const char* fmt, ...) {
50 va_list ap;
51 va_start(ap, fmt);
52 UsageErrorV(fmt, ap);
53 va_end(ap);
54}
55
56static void Usage(const char* fmt, ...) {
57 va_list ap;
58 va_start(ap, fmt);
59 UsageErrorV(fmt, ap);
60 va_end(ap);
61
62 UsageError("Usage: dex2oat [options]...");
63 UsageError("");
64 UsageError(" --dex-file=<dex-file>: specifies a .dex file to compile.");
65 UsageError(" Example: --dex-file=/system/framework/core.jar");
66 UsageError("");
67 UsageError(" --zip-fd=<file-descriptor>: specifies a file descriptor of a zip file");
68 UsageError(" containing a classes.dex file to compile.");
69 UsageError(" Example: --zip-fd=5");
70 UsageError("");
71 UsageError(" --zip-location=<zip-location>: specifies a symbolic name for the file corresponding");
72 UsageError(" to the file descriptor specified by --zip-fd.");
73 UsageError(" Example: --zip-location=/system/app/Calculator.apk");
74 UsageError("");
75 UsageError(" --oat-file=<file.oat>: specifies the required oat filename.");
76 UsageError(" Example: --oat-file=/system/framework/boot.oat");
77 UsageError("");
78 UsageError(" --oat-location=<oat-name>: specifies a symbolic name for the file corresponding");
79 UsageError(" to the file descriptor specified by --oat-fd.");
80 UsageError(" Example: --oat-location=/data/art-cache/system@app@Calculator.apk.oat");
81 UsageError("");
Logan Chien8b977d32012-02-21 19:14:55 +080082#if defined(ART_USE_LLVM_COMPILER)
Logan Chien8b977d32012-02-21 19:14:55 +080083 UsageError(" --bitcode=<file.bc>: specifies the optional bitcode filename.");
84 UsageError(" Example: --bitcode=/system/framework/boot.bc");
85 UsageError("");
86#endif
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -080087 UsageError(" --image=<file.art>: specifies the output image filename.");
88 UsageError(" Example: --image=/system/framework/boot.art");
89 UsageError("");
90 UsageError(" --image-classes=<classname-file>: specifies classes to include in an image.");
91 UsageError(" Example: --image=frameworks/base/preloaded-classes");
92 UsageError("");
93 UsageError(" --base=<hex-address>: specifies the base address when creating a boot image.");
94 UsageError(" Example: --base=0x50000000");
95 UsageError("");
96 UsageError(" --boot-image=<file.art>: provide the image file for the boot class path.");
97 UsageError(" Example: --boot-image=/system/framework/boot.art");
98 UsageError(" Default: <host-prefix>/system/framework/boot.art");
99 UsageError("");
100 UsageError(" --host-prefix may be used to translate host paths to target paths during");
101 UsageError(" cross compilation.");
102 UsageError(" Example: --host-prefix=out/target/product/crespo");
103 UsageError(" Default: $ANDROID_PRODUCT_OUT");
104 UsageError("");
jeffhao1f71ae82012-05-24 16:08:24 -0700105 UsageError(" --instruction-set=(arm|mips|x86): compile for a particular instruction");
Ian Rogers49c48942012-03-11 15:15:37 -0700106 UsageError(" set.");
jeffhao1f71ae82012-05-24 16:08:24 -0700107 UsageError(" Example: --instruction-set=x86");
108 UsageError(" Default: arm");
Ian Rogers49c48942012-03-11 15:15:37 -0700109 UsageError("");
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800110 UsageError(" --runtime-arg <argument>: used to specify various arguments for the runtime,");
111 UsageError(" such as initial heap size, maximum heap size, and verbose output.");
112 UsageError(" Use a separate --runtime-arg switch for each argument.");
113 UsageError(" Example: --runtime-arg -Xms256m");
114 UsageError("");
115 std::cerr << "See log for usage error information\n";
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700116 exit(EXIT_FAILURE);
117}
118
Brian Carlstromae826982011-11-09 01:33:42 -0800119class Dex2Oat {
120 public:
121
Ian Rogers49c48942012-03-11 15:15:37 -0700122 static Dex2Oat* Create(Runtime::Options& options, InstructionSet instruction_set,
123 size_t thread_count, bool support_debugging) {
124 UniquePtr<Runtime> runtime(CreateRuntime(options, instruction_set));
Brian Carlstromae826982011-11-09 01:33:42 -0800125 if (runtime.get() == NULL) {
126 return NULL;
127 }
Ian Rogers49c48942012-03-11 15:15:37 -0700128 return new Dex2Oat(runtime.release(), instruction_set, thread_count, support_debugging);
Brian Carlstromae826982011-11-09 01:33:42 -0800129 }
130
131 ~Dex2Oat() {
132 delete runtime_;
Elliott Hughes5523ee02012-02-03 18:18:34 -0800133 LOG(INFO) << "dex2oat took " << PrettyDuration(NanoTime() - start_ns_) << " (threads: " << thread_count_ << ")";
Brian Carlstromae826982011-11-09 01:33:42 -0800134 }
135
136 // Make a list of descriptors for classes to include in the image
137 const std::set<std::string>* GetImageClassDescriptors(const char* image_classes_filename) {
138 UniquePtr<std::ifstream> image_classes_file(new std::ifstream(image_classes_filename, std::ifstream::in));
139 if (image_classes_file.get() == NULL) {
140 LOG(ERROR) << "Failed to open image classes file " << image_classes_filename;
141 return NULL;
142 }
143
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800144 // Load all the classes specified in the file
Brian Carlstromae826982011-11-09 01:33:42 -0800145 ClassLinker* class_linker = runtime_->GetClassLinker();
146 while (image_classes_file->good()) {
147 std::string dot;
148 std::getline(*image_classes_file.get(), dot);
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800149 if (StartsWith(dot, "#") || dot.empty()) {
Brian Carlstromae826982011-11-09 01:33:42 -0800150 continue;
151 }
Elliott Hughes95572412011-12-13 18:14:20 -0800152 std::string descriptor(DotToDescriptor(dot.c_str()));
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800153 SirtRef<Class> klass(class_linker->FindSystemClass(descriptor.c_str()));
Brian Carlstromae826982011-11-09 01:33:42 -0800154 if (klass.get() == NULL) {
155 LOG(WARNING) << "Failed to find class " << descriptor;
156 Thread::Current()->ClearException();
157 }
158 }
159 image_classes_file->close();
160
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800161 // Resolve exception classes referenced by the loaded classes. The catch logic assumes
162 // exceptions are resolved by the verifier when there is a catch block in an interested method.
163 // Do this here so that exception classes appear to have been specified image classes.
164 std::set<std::pair<uint16_t, const DexFile*> > unresolved_exception_types;
165 do {
166 unresolved_exception_types.clear();
167 class_linker->VisitClasses(ResolveCatchBlockExceptionsClassVisitor,
168 &unresolved_exception_types);
169 typedef std::set<std::pair<uint16_t, const DexFile*> >::const_iterator It; // TODO: C++0x auto
170 for (It it = unresolved_exception_types.begin(),
171 end = unresolved_exception_types.end();
172 it != end; ++it) {
173 uint16_t exception_type_idx = it->first;
174 const DexFile* dex_file = it->second;
175 DexCache* dex_cache = class_linker->FindDexCache(*dex_file);
176 ClassLoader* class_loader = NULL;
177 SirtRef<Class> klass(class_linker->ResolveType(*dex_file, exception_type_idx, dex_cache,
178 class_loader));
179 if (klass.get() == NULL) {
180 const DexFile::TypeId& type_id = dex_file->GetTypeId(exception_type_idx);
181 const char* descriptor = dex_file->GetTypeDescriptor(type_id);
182 LOG(FATAL) << "Failed to resolve class " << descriptor;
183 }
184 DCHECK(klass->IsThrowableClass());
185 }
186 // Resolving exceptions may load classes that reference more exceptions, iterate until no
187 // more are found
188 } while (!unresolved_exception_types.empty());
189
Brian Carlstromae826982011-11-09 01:33:42 -0800190 // We walk the roots looking for classes so that we'll pick up the
191 // above classes plus any classes them depend on such super
192 // classes, interfaces, and the required ClassLinker roots.
193 UniquePtr<std::set<std::string> > image_classes(new std::set<std::string>());
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800194 class_linker->VisitClasses(RecordImageClassesVisitor, image_classes.get());
Brian Carlstromae826982011-11-09 01:33:42 -0800195 CHECK_NE(image_classes->size(), 0U);
196 return image_classes.release();
197 }
198
Brian Carlstromf5822582012-03-19 22:34:31 -0700199 const Compiler* CreateOatFile(const std::string& boot_image_option,
Brian Carlstrom34f1fa42012-04-05 18:28:12 -0700200 const std::string* host_prefix,
Brian Carlstromf5822582012-03-19 22:34:31 -0700201 const std::vector<const DexFile*>& dex_files,
202 File* oat_file,
Logan Chien8b977d32012-02-21 19:14:55 +0800203#if defined(ART_USE_LLVM_COMPILER)
Logan Chiende08e842012-03-21 00:34:12 +0800204 const std::string& bitcode_filename,
Logan Chien8b977d32012-02-21 19:14:55 +0800205#endif
Brian Carlstromf5822582012-03-19 22:34:31 -0700206 bool image,
Brian Carlstromba0668e2012-03-26 13:14:07 -0700207 const std::set<std::string>* image_classes,
208 bool dump_stats,
209 bool dump_timings) {
Brian Carlstromae826982011-11-09 01:33:42 -0800210 // SirtRef and ClassLoader creation needs to come after Runtime::Create
211 UniquePtr<SirtRef<ClassLoader> > class_loader(new SirtRef<ClassLoader>(NULL));
212 if (class_loader.get() == NULL) {
213 LOG(ERROR) << "Failed to create SirtRef for class loader";
214 return false;
215 }
216
Brian Carlstromae826982011-11-09 01:33:42 -0800217 if (!boot_image_option.empty()) {
218 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromae826982011-11-09 01:33:42 -0800219 std::vector<const DexFile*> class_path_files(dex_files);
Brian Carlstroma004aa92012-02-08 18:05:09 -0800220 OpenClassPathFiles(runtime_->GetClassPathString(), class_path_files);
Brian Carlstromae826982011-11-09 01:33:42 -0800221 for (size_t i = 0; i < class_path_files.size(); i++) {
222 class_linker->RegisterDexFile(*class_path_files[i]);
223 }
224 class_loader.get()->reset(PathClassLoader::AllocCompileTime(class_path_files));
Brian Carlstromae826982011-11-09 01:33:42 -0800225 }
226
Brian Carlstromf5822582012-03-19 22:34:31 -0700227 UniquePtr<Compiler> compiler(new Compiler(instruction_set_,
228 image,
229 thread_count_,
230 support_debugging_,
Brian Carlstromba0668e2012-03-26 13:14:07 -0700231 image_classes,
232 dump_stats,
233 dump_timings));
Logan Chien8b977d32012-02-21 19:14:55 +0800234
235#if defined(ART_USE_LLVM_COMPILER)
Brian Carlstromf5822582012-03-19 22:34:31 -0700236 compiler->SetBitcodeFileName(bitcode_filename);
Logan Chien8b977d32012-02-21 19:14:55 +0800237#endif
238
Brian Carlstromf5822582012-03-19 22:34:31 -0700239 compiler->CompileAll(class_loader->get(), dex_files);
Brian Carlstromae826982011-11-09 01:33:42 -0800240
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700241 std::string image_file_location;
242 uint32_t image_file_location_checksum = 0;
243 Heap* heap = Runtime::Current()->GetHeap();
244 if (heap->GetSpaces().size() > 1) {
245 ImageSpace* image_space = heap->GetImageSpace();
246 image_file_location_checksum = image_space->GetImageHeader().GetOatChecksum();
247 image_file_location = image_space->GetImageFilename();
Brian Carlstrom34f1fa42012-04-05 18:28:12 -0700248 if (host_prefix != NULL && StartsWith(image_file_location, host_prefix->c_str())) {
249 image_file_location = image_file_location.substr(host_prefix->size());
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700250 }
251 }
252
253 if (!OatWriter::Create(oat_file,
254 class_loader->get(),
255 dex_files,
256 image_file_location_checksum,
257 image_file_location,
Brian Carlstromf5822582012-03-19 22:34:31 -0700258 *compiler.get())) {
Brian Carlstromae826982011-11-09 01:33:42 -0800259 LOG(ERROR) << "Failed to create oat file " << oat_file->name();
Brian Carlstromf5822582012-03-19 22:34:31 -0700260 return NULL;
Brian Carlstromae826982011-11-09 01:33:42 -0800261 }
Brian Carlstromf5822582012-03-19 22:34:31 -0700262 return compiler.release();
Brian Carlstromae826982011-11-09 01:33:42 -0800263 }
264
Brian Carlstroma004aa92012-02-08 18:05:09 -0800265 bool CreateImageFile(const std::string& image_filename,
Brian Carlstromae826982011-11-09 01:33:42 -0800266 uintptr_t image_base,
267 const std::set<std::string>* image_classes,
268 const std::string& oat_filename,
Brian Carlstromf5822582012-03-19 22:34:31 -0700269 const std::string& oat_location,
270 const Compiler& compiler) {
Brian Carlstromae826982011-11-09 01:33:42 -0800271 ImageWriter image_writer(image_classes);
Brian Carlstromf5822582012-03-19 22:34:31 -0700272 if (!image_writer.Write(image_filename, image_base, oat_filename, oat_location, compiler)) {
Brian Carlstromae826982011-11-09 01:33:42 -0800273 LOG(ERROR) << "Failed to create image file " << image_filename;
274 return false;
275 }
276 return true;
277 }
278
279 private:
280
Ian Rogers49c48942012-03-11 15:15:37 -0700281 explicit Dex2Oat(Runtime* runtime, InstructionSet instruction_set, size_t thread_count,
282 bool support_debugging)
283 : instruction_set_(instruction_set),
284 runtime_(runtime),
Elliott Hughesde6e4cf2012-02-27 14:46:06 -0800285 thread_count_(thread_count),
286 support_debugging_(support_debugging),
287 start_ns_(NanoTime()) {
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800288 }
Brian Carlstromae826982011-11-09 01:33:42 -0800289
Ian Rogers49c48942012-03-11 15:15:37 -0700290 static Runtime* CreateRuntime(Runtime::Options& options, InstructionSet instruction_set) {
Brian Carlstromae826982011-11-09 01:33:42 -0800291 Runtime* runtime = Runtime::Create(options, false);
292 if (runtime == NULL) {
293 LOG(ERROR) << "Failed to create runtime";
294 return NULL;
295 }
296
297 // if we loaded an existing image, we will reuse values from the image roots.
298 if (!runtime->HasJniDlsymLookupStub()) {
Ian Rogers49c48942012-03-11 15:15:37 -0700299 runtime->SetJniDlsymLookupStub(Compiler::CreateJniDlsymLookupStub(instruction_set));
Brian Carlstromae826982011-11-09 01:33:42 -0800300 }
301 if (!runtime->HasAbstractMethodErrorStubArray()) {
Ian Rogers49c48942012-03-11 15:15:37 -0700302 runtime->SetAbstractMethodErrorStubArray(Compiler::CreateAbstractMethodErrorStub(instruction_set));
Brian Carlstromae826982011-11-09 01:33:42 -0800303 }
304 for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) {
305 Runtime::TrampolineType type = Runtime::TrampolineType(i);
306 if (!runtime->HasResolutionStubArray(type)) {
Ian Rogers49c48942012-03-11 15:15:37 -0700307 runtime->SetResolutionStubArray(Compiler::CreateResolutionStub(instruction_set, type), type);
Brian Carlstromae826982011-11-09 01:33:42 -0800308 }
309 }
Ian Rogers19846512012-02-24 11:42:47 -0800310 if (!runtime->HasResolutionMethod()) {
311 runtime->SetResolutionMethod(runtime->CreateResolutionMethod());
312 }
Brian Carlstromae826982011-11-09 01:33:42 -0800313 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
314 Runtime::CalleeSaveType type = Runtime::CalleeSaveType(i);
315 if (!runtime->HasCalleeSaveMethod(type)) {
Ian Rogers49c48942012-03-11 15:15:37 -0700316 runtime->SetCalleeSaveMethod(runtime->CreateCalleeSaveMethod(instruction_set, type), type);
Brian Carlstromae826982011-11-09 01:33:42 -0800317 }
318 }
Ian Rogers19846512012-02-24 11:42:47 -0800319 runtime->GetClassLinker()->FixupDexCaches(runtime->GetResolutionMethod());
Brian Carlstromae826982011-11-09 01:33:42 -0800320 return runtime;
321 }
322
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800323 static void ResolveExceptionsForMethod(MethodHelper* mh,
324 std::set<std::pair<uint16_t, const DexFile*> >& exceptions_to_resolve) {
325 const DexFile::CodeItem* code_item = mh->GetCodeItem();
326 if (code_item == NULL) {
327 return; // native or abstract method
328 }
329 if (code_item->tries_size_ == 0) {
330 return; // nothing to process
331 }
332 const byte* encoded_catch_handler_list = DexFile::GetCatchHandlerData(*code_item, 0);
333 size_t num_encoded_catch_handlers = DecodeUnsignedLeb128(&encoded_catch_handler_list);
334 for (size_t i = 0; i < num_encoded_catch_handlers; i++) {
335 int32_t encoded_catch_handler_size = DecodeSignedLeb128(&encoded_catch_handler_list);
336 bool has_catch_all = false;
337 if (encoded_catch_handler_size <= 0) {
338 encoded_catch_handler_size = -encoded_catch_handler_size;
339 has_catch_all = true;
340 }
341 for (int32_t j = 0; j < encoded_catch_handler_size; j++) {
342 uint16_t encoded_catch_handler_handlers_type_idx =
343 DecodeUnsignedLeb128(&encoded_catch_handler_list);
344 // Add to set of types to resolve if not already in the dex cache resolved types
345 if (!mh->IsResolvedTypeIdx(encoded_catch_handler_handlers_type_idx)) {
346 exceptions_to_resolve.insert(
347 std::pair<uint16_t, const DexFile*>(encoded_catch_handler_handlers_type_idx,
348 &mh->GetDexFile()));
349 }
350 // ignore address associated with catch handler
351 DecodeUnsignedLeb128(&encoded_catch_handler_list);
352 }
353 if (has_catch_all) {
354 // ignore catch all address
355 DecodeUnsignedLeb128(&encoded_catch_handler_list);
356 }
357 }
358 }
359 static bool ResolveCatchBlockExceptionsClassVisitor(Class* c, void* arg) {
360 std::set<std::pair<uint16_t, const DexFile*> >* exceptions_to_resolve =
361 reinterpret_cast<std::set<std::pair<uint16_t, const DexFile*> >*>(arg);
362 MethodHelper mh;
363 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
364 Method* m = c->GetVirtualMethod(i);
365 mh.ChangeMethod(m);
366 ResolveExceptionsForMethod(&mh, *exceptions_to_resolve);
367 }
368 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
369 Method* m = c->GetDirectMethod(i);
370 mh.ChangeMethod(m);
371 ResolveExceptionsForMethod(&mh, *exceptions_to_resolve);
372 }
373 return true;
374 }
375 static bool RecordImageClassesVisitor(Class* klass, void* arg) {
Brian Carlstromae826982011-11-09 01:33:42 -0800376 std::set<std::string>* image_classes = reinterpret_cast<std::set<std::string>*>(arg);
377 if (klass->IsArrayClass() || klass->IsPrimitive()) {
Jesse Wilson254db0f2011-11-16 16:44:11 -0500378 return true;
379 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800380 image_classes->insert(ClassHelper(klass).GetDescriptor());
Brian Carlstromae826982011-11-09 01:33:42 -0800381 return true;
Jesse Wilson254db0f2011-11-16 16:44:11 -0500382 }
Jesse Wilson254db0f2011-11-16 16:44:11 -0500383
Brian Carlstromae826982011-11-09 01:33:42 -0800384 // Appends to dex_files any elements of class_path that it doesn't already
385 // contain. This will open those dex files as necessary.
386 static void OpenClassPathFiles(const std::string& class_path, std::vector<const DexFile*>& dex_files) {
387 std::vector<std::string> parsed;
388 Split(class_path, ':', parsed);
389 for (size_t i = 0; i < parsed.size(); ++i) {
390 if (DexFilesContains(dex_files, parsed[i])) {
391 continue;
392 }
Brian Carlstroma004aa92012-02-08 18:05:09 -0800393 const DexFile* dex_file = DexFile::Open(parsed[i], parsed[i]);
Brian Carlstromae826982011-11-09 01:33:42 -0800394 if (dex_file == NULL) {
395 LOG(WARNING) << "Failed to open dex file " << parsed[i];
396 } else {
397 dex_files.push_back(dex_file);
398 }
Jesse Wilson254db0f2011-11-16 16:44:11 -0500399 }
400 }
Brian Carlstromae826982011-11-09 01:33:42 -0800401
402 // Returns true if dex_files has a dex with the named location.
403 static bool DexFilesContains(const std::vector<const DexFile*>& dex_files, const std::string& location) {
404 for (size_t i = 0; i < dex_files.size(); ++i) {
405 if (dex_files[i]->GetLocation() == location) {
406 return true;
407 }
408 }
409 return false;
410 }
411
Ian Rogers49c48942012-03-11 15:15:37 -0700412 const InstructionSet instruction_set_;
Brian Carlstromae826982011-11-09 01:33:42 -0800413
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800414 Runtime* runtime_;
Elliott Hughes5523ee02012-02-03 18:18:34 -0800415 size_t thread_count_;
Elliott Hughesde6e4cf2012-02-27 14:46:06 -0800416 bool support_debugging_;
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800417 uint64_t start_ns_;
418
Brian Carlstromae826982011-11-09 01:33:42 -0800419 DISALLOW_IMPLICIT_CONSTRUCTORS(Dex2Oat);
420};
Jesse Wilson254db0f2011-11-16 16:44:11 -0500421
Elliott Hughes72395bf2012-04-24 13:45:26 -0700422static bool ParseInt(const char* in, int* out) {
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800423 char* end;
424 int result = strtol(in, &end, 10);
425 if (in == end || *end != '\0') {
426 return false;
427 }
428 *out = result;
429 return true;
430}
431
Elliott Hughesbf1b4572012-05-24 19:11:39 -0700432static size_t OpenDexFiles(const std::vector<const char*>& dex_filenames,
433 const std::vector<const char*>& dex_locations,
434 std::vector<const DexFile*>& dex_files) {
435 size_t failure_count = 0;
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800436 for (size_t i = 0; i < dex_filenames.size(); i++) {
437 const char* dex_filename = dex_filenames[i];
Brian Carlstroma004aa92012-02-08 18:05:09 -0800438 const char* dex_location = dex_locations[i];
439 const DexFile* dex_file = DexFile::Open(dex_filename, dex_location);
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800440 if (dex_file == NULL) {
jeffhao60f83e32012-02-13 17:16:30 -0800441 LOG(WARNING) << "could not open .dex from file " << dex_filename;
Elliott Hughesbf1b4572012-05-24 19:11:39 -0700442 ++failure_count;
jeffhao60f83e32012-02-13 17:16:30 -0800443 } else {
444 dex_files.push_back(dex_file);
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800445 }
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800446 }
Elliott Hughesbf1b4572012-05-24 19:11:39 -0700447 return failure_count;
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800448}
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();
jeffhao1f71ae82012-05-24 16:08:24 -0700537 if (instruction_set_str == "arm") {
Ian Rogers49c48942012-03-11 15:15:37 -0700538 instruction_set = kThumb2;
jeffhao1f71ae82012-05-24 16:08:24 -0700539 } else if (instruction_set_str == "mips") {
Ian Rogers49c48942012-03-11 15:15:37 -0700540 instruction_set = kMips;
jeffhao1f71ae82012-05-24 16:08:24 -0700541 } else if (instruction_set_str == "x86") {
Ian Rogers49c48942012-03-11 15:15:37 -0700542 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
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800557 if (oat_filename.empty() && oat_fd == -1) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800558 Usage("Output must be supplied with either --oat-file or --oat-fd");
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800559 }
560
561 if (!oat_filename.empty() && oat_fd != -1) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800562 Usage("--oat-file should not be used with --oat-fd");
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800563 }
564
565 if (!oat_filename.empty() && oat_fd != -1) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800566 Usage("--oat-file should not be used with --oat-fd");
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800567 }
568
Brian Carlstroma004aa92012-02-08 18:05:09 -0800569 if (oat_fd != -1 && !image_filename.empty()) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800570 Usage("--oat-fd should not be used with --image");
Brian Carlstrome24fa612011-09-29 00:53:55 -0700571 }
572
Brian Carlstrom34f1fa42012-04-05 18:28:12 -0700573 if (host_prefix.get() == NULL) {
Brian Carlstromb0011262011-12-09 12:17:24 -0800574 const char* android_product_out = getenv("ANDROID_PRODUCT_OUT");
575 if (android_product_out != NULL) {
Brian Carlstrom34f1fa42012-04-05 18:28:12 -0700576 host_prefix.reset(new std::string(android_product_out));
Brian Carlstromb0011262011-12-09 12:17:24 -0800577 }
578 }
579
Brian Carlstroma004aa92012-02-08 18:05:09 -0800580 bool image = (!image_filename.empty());
Brian Carlstromb0011262011-12-09 12:17:24 -0800581 if (!image && boot_image_filename.empty()) {
Brian Carlstrom34f1fa42012-04-05 18:28:12 -0700582 if (host_prefix.get() == NULL) {
Brian Carlstroma56fcd62012-02-04 21:23:01 -0800583 boot_image_filename += GetAndroidRoot();
584 } else {
Brian Carlstrom34f1fa42012-04-05 18:28:12 -0700585 boot_image_filename += *host_prefix.get();
Brian Carlstroma56fcd62012-02-04 21:23:01 -0800586 boot_image_filename += "/system";
587 }
588 boot_image_filename += "/framework/boot.art";
Brian Carlstromb0011262011-12-09 12:17:24 -0800589 }
590 std::string boot_image_option;
Brian Carlstroma004aa92012-02-08 18:05:09 -0800591 if (!boot_image_filename.empty()) {
Brian Carlstromb0011262011-12-09 12:17:24 -0800592 boot_image_option += "-Ximage:";
593 boot_image_option += boot_image_filename;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700594 }
595
Brian Carlstromae826982011-11-09 01:33:42 -0800596 if (image_classes_filename != NULL && !image) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800597 Usage("--image-classes should only be used with --image");
Brian Carlstromae826982011-11-09 01:33:42 -0800598 }
599
600 if (image_classes_filename != NULL && !boot_image_option.empty()) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800601 Usage("--image-classes should not be used with --boot-image");
Brian Carlstrom78128a62011-09-15 17:21:19 -0700602 }
603
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800604 if (dex_filenames.empty() && zip_fd == -1) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800605 Usage("Input must be supplied with either --dex-file or --zip-fd");
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800606 }
607
608 if (!dex_filenames.empty() && zip_fd != -1) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800609 Usage("--dex-file should not be used with --zip-fd");
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800610 }
611
Brian Carlstroma004aa92012-02-08 18:05:09 -0800612 if (!dex_filenames.empty() && !zip_location.empty()) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800613 Usage("--dex-file should not be used with --zip-location");
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800614 }
615
Brian Carlstroma004aa92012-02-08 18:05:09 -0800616 if (dex_locations.empty()) {
617 for (size_t i = 0; i < dex_filenames.size(); i++) {
618 dex_locations.push_back(dex_filenames[i]);
619 }
620 } else if (dex_locations.size() != dex_filenames.size()) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800621 Usage("--dex-location arguments do not match --dex-file arguments");
Brian Carlstroma004aa92012-02-08 18:05:09 -0800622 }
623
624 if (zip_fd != -1 && zip_location.empty()) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800625 Usage("--zip-location should be supplied with --zip-fd");
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800626 }
627
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700628 if (boot_image_option.empty()) {
629 if (image_base == 0) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800630 Usage("non-zero --base not specified");
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700631 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700632 }
633
Brian Carlstrom6ef827a2011-12-11 14:57:47 -0800634 // Check early that the result of compilation can be written
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800635 UniquePtr<File> oat_file;
Brian Carlstrom6cd40e52012-05-03 14:15:11 -0700636 bool create_file = !oat_filename.empty(); // as opposed to using open file descriptor
637 if (create_file) {
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800638 oat_file.reset(OS::OpenFile(oat_filename.c_str(), true));
Brian Carlstroma004aa92012-02-08 18:05:09 -0800639 if (oat_location.empty()) {
640 oat_location = oat_filename;
641 }
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800642 } else {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800643 oat_file.reset(OS::FileFromFd(oat_location.c_str(), oat_fd));
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800644 }
Brian Carlstrom6ef827a2011-12-11 14:57:47 -0800645 if (oat_file.get() == NULL) {
Brian Carlstrom6cd40e52012-05-03 14:15:11 -0700646 PLOG(ERROR) << "Failed to create oat file: " << oat_location;
647 return EXIT_FAILURE;
648 }
649 if (create_file && fchmod(oat_file->Fd(), 0644) != 0) {
650 PLOG(ERROR) << "Failed to make oat file world readable: " << oat_location;
Brian Carlstrom6ef827a2011-12-11 14:57:47 -0800651 return EXIT_FAILURE;
Ian Rogers5e863dd2011-11-02 20:00:10 -0700652 }
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800653
Brian Carlstroma004aa92012-02-08 18:05:09 -0800654 LOG(INFO) << "dex2oat: " << oat_location;
Ian Rogers5e863dd2011-11-02 20:00:10 -0700655
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700656 Runtime::Options options;
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700657 options.push_back(std::make_pair("compiler", reinterpret_cast<void*>(NULL)));
Brian Carlstroma004aa92012-02-08 18:05:09 -0800658 std::vector<const DexFile*> boot_class_path;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700659 if (boot_image_option.empty()) {
Elliott Hughesbf1b4572012-05-24 19:11:39 -0700660 size_t failure_count = OpenDexFiles(dex_filenames, dex_locations, boot_class_path);
661 if (failure_count > 0) {
662 LOG(ERROR) << "Failed to open some dex files: " << failure_count;
663 return EXIT_FAILURE;
664 }
Brian Carlstroma004aa92012-02-08 18:05:09 -0800665 options.push_back(std::make_pair("bootclasspath", &boot_class_path));
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700666 } else {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700667 options.push_back(std::make_pair(boot_image_option.c_str(), reinterpret_cast<void*>(NULL)));
668 }
Brian Carlstrom34f1fa42012-04-05 18:28:12 -0700669 if (host_prefix.get() != NULL) {
670 options.push_back(std::make_pair("host-prefix", host_prefix->c_str()));
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700671 }
jeffhao5d840402011-10-24 17:09:45 -0700672 for (size_t i = 0; i < runtime_args.size(); i++) {
673 options.push_back(std::make_pair(runtime_args[i], reinterpret_cast<void*>(NULL)));
674 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700675
Ian Rogers49c48942012-03-11 15:15:37 -0700676 UniquePtr<Dex2Oat> dex2oat(Dex2Oat::Create(options, instruction_set, thread_count,
677 support_debugging));
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700678
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800679 // If --image-classes was specified, calculate the full list of classes to include in the image
Brian Carlstromae826982011-11-09 01:33:42 -0800680 UniquePtr<const std::set<std::string> > image_classes(NULL);
681 if (image_classes_filename != NULL) {
682 image_classes.reset(dex2oat->GetImageClassDescriptors(image_classes_filename));
683 if (image_classes.get() == NULL) {
684 LOG(ERROR) << "Failed to create list of image classes from " << image_classes_filename;
685 return EXIT_FAILURE;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700686 }
687 }
688
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800689 std::vector<const DexFile*> dex_files;
690 if (boot_image_option.empty()) {
691 dex_files = Runtime::Current()->GetClassLinker()->GetBootClassPath();
692 } else {
693 if (dex_filenames.empty()) {
694 UniquePtr<ZipArchive> zip_archive(ZipArchive::OpenFromFd(zip_fd));
695 if (zip_archive.get() == NULL) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800696 LOG(ERROR) << "Failed to zip from file descriptor for " << zip_location;
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -0800697 return EXIT_FAILURE;
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800698 }
Brian Carlstroma004aa92012-02-08 18:05:09 -0800699 const DexFile* dex_file = DexFile::Open(*zip_archive.get(), zip_location);
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800700 if (dex_file == NULL) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800701 LOG(ERROR) << "Failed to open dex from file descriptor for zip file: " << zip_location;
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800702 return EXIT_FAILURE;
703 }
704 dex_files.push_back(dex_file);
705 } else {
Elliott Hughesbf1b4572012-05-24 19:11:39 -0700706 size_t failure_count = OpenDexFiles(dex_filenames, dex_locations, dex_files);
707 if (failure_count > 0) {
708 LOG(ERROR) << "Failed to open some dex files: " << failure_count;
709 return EXIT_FAILURE;
710 }
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800711 }
712 }
713
Brian Carlstromf5822582012-03-19 22:34:31 -0700714 UniquePtr<const Compiler> compiler(dex2oat->CreateOatFile(boot_image_option,
Brian Carlstrom34f1fa42012-04-05 18:28:12 -0700715 host_prefix.get(),
Brian Carlstromf5822582012-03-19 22:34:31 -0700716 dex_files,
717 oat_file.get(),
Logan Chien8b977d32012-02-21 19:14:55 +0800718#if defined(ART_USE_LLVM_COMPILER)
Brian Carlstromf5822582012-03-19 22:34:31 -0700719 bitcode_filename,
Logan Chien8b977d32012-02-21 19:14:55 +0800720#endif
Brian Carlstromf5822582012-03-19 22:34:31 -0700721 image,
Brian Carlstromba0668e2012-03-26 13:14:07 -0700722 image_classes.get(),
723 dump_stats,
724 dump_timings));
Brian Carlstromf5822582012-03-19 22:34:31 -0700725
726 if (compiler.get() == NULL) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800727 LOG(ERROR) << "Failed to create oat file: " << oat_location;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700728 return EXIT_FAILURE;
729 }
730
Brian Carlstromae826982011-11-09 01:33:42 -0800731 if (!image) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800732 LOG(INFO) << "Oat file written successfully: " << oat_location;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700733 return EXIT_SUCCESS;
734 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700735
Brian Carlstromae826982011-11-09 01:33:42 -0800736 if (!dex2oat->CreateImageFile(image_filename,
737 image_base,
738 image_classes.get(),
739 oat_filename,
Brian Carlstromf5822582012-03-19 22:34:31 -0700740 oat_location,
741 *compiler.get())) {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700742 return EXIT_FAILURE;
743 }
744
Brian Carlstromae826982011-11-09 01:33:42 -0800745 // We wrote the oat file successfully, and want to keep it.
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800746 LOG(INFO) << "Oat file written successfully: " << oat_filename;
747 LOG(INFO) << "Image written successfully: " << image_filename;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700748 return EXIT_SUCCESS;
749}
750
751} // namespace art
752
753int main(int argc, char** argv) {
754 return art::dex2oat(argc, argv);
755}