blob: e9acbad44814a396765106cf8b41d1817384de77 [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;
Elliott Hughes35be9b12012-05-29 17:59:26 -0700165 SirtRef<Class> java_lang_Throwable(class_linker->FindSystemClass("Ljava/lang/Throwable;"));
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800166 do {
167 unresolved_exception_types.clear();
168 class_linker->VisitClasses(ResolveCatchBlockExceptionsClassVisitor,
169 &unresolved_exception_types);
170 typedef std::set<std::pair<uint16_t, const DexFile*> >::const_iterator It; // TODO: C++0x auto
171 for (It it = unresolved_exception_types.begin(),
172 end = unresolved_exception_types.end();
173 it != end; ++it) {
174 uint16_t exception_type_idx = it->first;
175 const DexFile* dex_file = it->second;
176 DexCache* dex_cache = class_linker->FindDexCache(*dex_file);
177 ClassLoader* class_loader = NULL;
178 SirtRef<Class> klass(class_linker->ResolveType(*dex_file, exception_type_idx, dex_cache,
179 class_loader));
180 if (klass.get() == NULL) {
181 const DexFile::TypeId& type_id = dex_file->GetTypeId(exception_type_idx);
182 const char* descriptor = dex_file->GetTypeDescriptor(type_id);
183 LOG(FATAL) << "Failed to resolve class " << descriptor;
184 }
Elliott Hughes35be9b12012-05-29 17:59:26 -0700185 DCHECK(java_lang_Throwable->IsAssignableFrom(klass.get()));
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800186 }
187 // Resolving exceptions may load classes that reference more exceptions, iterate until no
188 // more are found
189 } while (!unresolved_exception_types.empty());
190
Brian Carlstromae826982011-11-09 01:33:42 -0800191 // We walk the roots looking for classes so that we'll pick up the
192 // above classes plus any classes them depend on such super
193 // classes, interfaces, and the required ClassLinker roots.
194 UniquePtr<std::set<std::string> > image_classes(new std::set<std::string>());
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800195 class_linker->VisitClasses(RecordImageClassesVisitor, image_classes.get());
Brian Carlstromae826982011-11-09 01:33:42 -0800196 CHECK_NE(image_classes->size(), 0U);
197 return image_classes.release();
198 }
199
Brian Carlstromf5822582012-03-19 22:34:31 -0700200 const Compiler* CreateOatFile(const std::string& boot_image_option,
Brian Carlstrom34f1fa42012-04-05 18:28:12 -0700201 const std::string* host_prefix,
Brian Carlstromf5822582012-03-19 22:34:31 -0700202 const std::vector<const DexFile*>& dex_files,
203 File* oat_file,
Logan Chien8b977d32012-02-21 19:14:55 +0800204#if defined(ART_USE_LLVM_COMPILER)
Logan Chiende08e842012-03-21 00:34:12 +0800205 const std::string& bitcode_filename,
Logan Chien8b977d32012-02-21 19:14:55 +0800206#endif
Brian Carlstromf5822582012-03-19 22:34:31 -0700207 bool image,
Brian Carlstromba0668e2012-03-26 13:14:07 -0700208 const std::set<std::string>* image_classes,
209 bool dump_stats,
210 bool dump_timings) {
Brian Carlstromae826982011-11-09 01:33:42 -0800211 // SirtRef and ClassLoader creation needs to come after Runtime::Create
212 UniquePtr<SirtRef<ClassLoader> > class_loader(new SirtRef<ClassLoader>(NULL));
213 if (class_loader.get() == NULL) {
214 LOG(ERROR) << "Failed to create SirtRef for class loader";
Elliott Hughes60234562012-06-01 12:25:59 -0700215 return NULL;
Brian Carlstromae826982011-11-09 01:33:42 -0800216 }
217
Brian Carlstromae826982011-11-09 01:33:42 -0800218 if (!boot_image_option.empty()) {
219 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromae826982011-11-09 01:33:42 -0800220 std::vector<const DexFile*> class_path_files(dex_files);
Brian Carlstroma004aa92012-02-08 18:05:09 -0800221 OpenClassPathFiles(runtime_->GetClassPathString(), class_path_files);
Brian Carlstromae826982011-11-09 01:33:42 -0800222 for (size_t i = 0; i < class_path_files.size(); i++) {
223 class_linker->RegisterDexFile(*class_path_files[i]);
224 }
225 class_loader.get()->reset(PathClassLoader::AllocCompileTime(class_path_files));
Brian Carlstromae826982011-11-09 01:33:42 -0800226 }
227
Brian Carlstromf5822582012-03-19 22:34:31 -0700228 UniquePtr<Compiler> compiler(new Compiler(instruction_set_,
229 image,
230 thread_count_,
231 support_debugging_,
Brian Carlstromba0668e2012-03-26 13:14:07 -0700232 image_classes,
233 dump_stats,
234 dump_timings));
Logan Chien8b977d32012-02-21 19:14:55 +0800235
236#if defined(ART_USE_LLVM_COMPILER)
Brian Carlstromf5822582012-03-19 22:34:31 -0700237 compiler->SetBitcodeFileName(bitcode_filename);
Logan Chien8b977d32012-02-21 19:14:55 +0800238#endif
239
Brian Carlstromf5822582012-03-19 22:34:31 -0700240 compiler->CompileAll(class_loader->get(), dex_files);
Brian Carlstromae826982011-11-09 01:33:42 -0800241
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700242 std::string image_file_location;
243 uint32_t image_file_location_checksum = 0;
244 Heap* heap = Runtime::Current()->GetHeap();
245 if (heap->GetSpaces().size() > 1) {
246 ImageSpace* image_space = heap->GetImageSpace();
247 image_file_location_checksum = image_space->GetImageHeader().GetOatChecksum();
248 image_file_location = image_space->GetImageFilename();
Brian Carlstrom34f1fa42012-04-05 18:28:12 -0700249 if (host_prefix != NULL && StartsWith(image_file_location, host_prefix->c_str())) {
250 image_file_location = image_file_location.substr(host_prefix->size());
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700251 }
252 }
253
254 if (!OatWriter::Create(oat_file,
255 class_loader->get(),
256 dex_files,
257 image_file_location_checksum,
258 image_file_location,
Brian Carlstromf5822582012-03-19 22:34:31 -0700259 *compiler.get())) {
Brian Carlstromae826982011-11-09 01:33:42 -0800260 LOG(ERROR) << "Failed to create oat file " << oat_file->name();
Brian Carlstromf5822582012-03-19 22:34:31 -0700261 return NULL;
Brian Carlstromae826982011-11-09 01:33:42 -0800262 }
Brian Carlstromf5822582012-03-19 22:34:31 -0700263 return compiler.release();
Brian Carlstromae826982011-11-09 01:33:42 -0800264 }
265
Brian Carlstroma004aa92012-02-08 18:05:09 -0800266 bool CreateImageFile(const std::string& image_filename,
Brian Carlstromae826982011-11-09 01:33:42 -0800267 uintptr_t image_base,
268 const std::set<std::string>* image_classes,
269 const std::string& oat_filename,
Brian Carlstromf5822582012-03-19 22:34:31 -0700270 const std::string& oat_location,
271 const Compiler& compiler) {
Brian Carlstromae826982011-11-09 01:33:42 -0800272 ImageWriter image_writer(image_classes);
Brian Carlstromf5822582012-03-19 22:34:31 -0700273 if (!image_writer.Write(image_filename, image_base, oat_filename, oat_location, compiler)) {
Brian Carlstromae826982011-11-09 01:33:42 -0800274 LOG(ERROR) << "Failed to create image file " << image_filename;
275 return false;
276 }
277 return true;
278 }
279
280 private:
281
Ian Rogers49c48942012-03-11 15:15:37 -0700282 explicit Dex2Oat(Runtime* runtime, InstructionSet instruction_set, size_t thread_count,
283 bool support_debugging)
284 : instruction_set_(instruction_set),
285 runtime_(runtime),
Elliott Hughesde6e4cf2012-02-27 14:46:06 -0800286 thread_count_(thread_count),
287 support_debugging_(support_debugging),
288 start_ns_(NanoTime()) {
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800289 }
Brian Carlstromae826982011-11-09 01:33:42 -0800290
Ian Rogers49c48942012-03-11 15:15:37 -0700291 static Runtime* CreateRuntime(Runtime::Options& options, InstructionSet instruction_set) {
Brian Carlstromae826982011-11-09 01:33:42 -0800292 Runtime* runtime = Runtime::Create(options, false);
293 if (runtime == NULL) {
294 LOG(ERROR) << "Failed to create runtime";
295 return NULL;
296 }
297
298 // if we loaded an existing image, we will reuse values from the image roots.
299 if (!runtime->HasJniDlsymLookupStub()) {
Ian Rogers49c48942012-03-11 15:15:37 -0700300 runtime->SetJniDlsymLookupStub(Compiler::CreateJniDlsymLookupStub(instruction_set));
Brian Carlstromae826982011-11-09 01:33:42 -0800301 }
302 if (!runtime->HasAbstractMethodErrorStubArray()) {
Ian Rogers49c48942012-03-11 15:15:37 -0700303 runtime->SetAbstractMethodErrorStubArray(Compiler::CreateAbstractMethodErrorStub(instruction_set));
Brian Carlstromae826982011-11-09 01:33:42 -0800304 }
305 for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) {
306 Runtime::TrampolineType type = Runtime::TrampolineType(i);
307 if (!runtime->HasResolutionStubArray(type)) {
Ian Rogers49c48942012-03-11 15:15:37 -0700308 runtime->SetResolutionStubArray(Compiler::CreateResolutionStub(instruction_set, type), type);
Brian Carlstromae826982011-11-09 01:33:42 -0800309 }
310 }
Ian Rogers19846512012-02-24 11:42:47 -0800311 if (!runtime->HasResolutionMethod()) {
312 runtime->SetResolutionMethod(runtime->CreateResolutionMethod());
313 }
Brian Carlstromae826982011-11-09 01:33:42 -0800314 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
315 Runtime::CalleeSaveType type = Runtime::CalleeSaveType(i);
316 if (!runtime->HasCalleeSaveMethod(type)) {
Ian Rogers49c48942012-03-11 15:15:37 -0700317 runtime->SetCalleeSaveMethod(runtime->CreateCalleeSaveMethod(instruction_set, type), type);
Brian Carlstromae826982011-11-09 01:33:42 -0800318 }
319 }
Ian Rogers19846512012-02-24 11:42:47 -0800320 runtime->GetClassLinker()->FixupDexCaches(runtime->GetResolutionMethod());
Brian Carlstromae826982011-11-09 01:33:42 -0800321 return runtime;
322 }
323
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800324 static void ResolveExceptionsForMethod(MethodHelper* mh,
325 std::set<std::pair<uint16_t, const DexFile*> >& exceptions_to_resolve) {
326 const DexFile::CodeItem* code_item = mh->GetCodeItem();
327 if (code_item == NULL) {
328 return; // native or abstract method
329 }
330 if (code_item->tries_size_ == 0) {
331 return; // nothing to process
332 }
333 const byte* encoded_catch_handler_list = DexFile::GetCatchHandlerData(*code_item, 0);
334 size_t num_encoded_catch_handlers = DecodeUnsignedLeb128(&encoded_catch_handler_list);
335 for (size_t i = 0; i < num_encoded_catch_handlers; i++) {
336 int32_t encoded_catch_handler_size = DecodeSignedLeb128(&encoded_catch_handler_list);
337 bool has_catch_all = false;
338 if (encoded_catch_handler_size <= 0) {
339 encoded_catch_handler_size = -encoded_catch_handler_size;
340 has_catch_all = true;
341 }
342 for (int32_t j = 0; j < encoded_catch_handler_size; j++) {
343 uint16_t encoded_catch_handler_handlers_type_idx =
344 DecodeUnsignedLeb128(&encoded_catch_handler_list);
345 // Add to set of types to resolve if not already in the dex cache resolved types
346 if (!mh->IsResolvedTypeIdx(encoded_catch_handler_handlers_type_idx)) {
347 exceptions_to_resolve.insert(
348 std::pair<uint16_t, const DexFile*>(encoded_catch_handler_handlers_type_idx,
349 &mh->GetDexFile()));
350 }
351 // ignore address associated with catch handler
352 DecodeUnsignedLeb128(&encoded_catch_handler_list);
353 }
354 if (has_catch_all) {
355 // ignore catch all address
356 DecodeUnsignedLeb128(&encoded_catch_handler_list);
357 }
358 }
359 }
360 static bool ResolveCatchBlockExceptionsClassVisitor(Class* c, void* arg) {
361 std::set<std::pair<uint16_t, const DexFile*> >* exceptions_to_resolve =
362 reinterpret_cast<std::set<std::pair<uint16_t, const DexFile*> >*>(arg);
363 MethodHelper mh;
364 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
365 Method* m = c->GetVirtualMethod(i);
366 mh.ChangeMethod(m);
367 ResolveExceptionsForMethod(&mh, *exceptions_to_resolve);
368 }
369 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
370 Method* m = c->GetDirectMethod(i);
371 mh.ChangeMethod(m);
372 ResolveExceptionsForMethod(&mh, *exceptions_to_resolve);
373 }
374 return true;
375 }
376 static bool RecordImageClassesVisitor(Class* klass, void* arg) {
Brian Carlstromae826982011-11-09 01:33:42 -0800377 std::set<std::string>* image_classes = reinterpret_cast<std::set<std::string>*>(arg);
378 if (klass->IsArrayClass() || klass->IsPrimitive()) {
Jesse Wilson254db0f2011-11-16 16:44:11 -0500379 return true;
380 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800381 image_classes->insert(ClassHelper(klass).GetDescriptor());
Brian Carlstromae826982011-11-09 01:33:42 -0800382 return true;
Jesse Wilson254db0f2011-11-16 16:44:11 -0500383 }
Jesse Wilson254db0f2011-11-16 16:44:11 -0500384
Brian Carlstromae826982011-11-09 01:33:42 -0800385 // Appends to dex_files any elements of class_path that it doesn't already
386 // contain. This will open those dex files as necessary.
387 static void OpenClassPathFiles(const std::string& class_path, std::vector<const DexFile*>& dex_files) {
388 std::vector<std::string> parsed;
389 Split(class_path, ':', parsed);
390 for (size_t i = 0; i < parsed.size(); ++i) {
391 if (DexFilesContains(dex_files, parsed[i])) {
392 continue;
393 }
Brian Carlstroma004aa92012-02-08 18:05:09 -0800394 const DexFile* dex_file = DexFile::Open(parsed[i], parsed[i]);
Brian Carlstromae826982011-11-09 01:33:42 -0800395 if (dex_file == NULL) {
396 LOG(WARNING) << "Failed to open dex file " << parsed[i];
397 } else {
398 dex_files.push_back(dex_file);
399 }
Jesse Wilson254db0f2011-11-16 16:44:11 -0500400 }
401 }
Brian Carlstromae826982011-11-09 01:33:42 -0800402
403 // Returns true if dex_files has a dex with the named location.
404 static bool DexFilesContains(const std::vector<const DexFile*>& dex_files, const std::string& location) {
405 for (size_t i = 0; i < dex_files.size(); ++i) {
406 if (dex_files[i]->GetLocation() == location) {
407 return true;
408 }
409 }
410 return false;
411 }
412
Ian Rogers49c48942012-03-11 15:15:37 -0700413 const InstructionSet instruction_set_;
Brian Carlstromae826982011-11-09 01:33:42 -0800414
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800415 Runtime* runtime_;
Elliott Hughes5523ee02012-02-03 18:18:34 -0800416 size_t thread_count_;
Elliott Hughesde6e4cf2012-02-27 14:46:06 -0800417 bool support_debugging_;
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800418 uint64_t start_ns_;
419
Brian Carlstromae826982011-11-09 01:33:42 -0800420 DISALLOW_IMPLICIT_CONSTRUCTORS(Dex2Oat);
421};
Jesse Wilson254db0f2011-11-16 16:44:11 -0500422
Elliott Hughes72395bf2012-04-24 13:45:26 -0700423static bool ParseInt(const char* in, int* out) {
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800424 char* end;
425 int result = strtol(in, &end, 10);
426 if (in == end || *end != '\0') {
427 return false;
428 }
429 *out = result;
430 return true;
431}
432
Elliott Hughesbf1b4572012-05-24 19:11:39 -0700433static size_t OpenDexFiles(const std::vector<const char*>& dex_filenames,
434 const std::vector<const char*>& dex_locations,
435 std::vector<const DexFile*>& dex_files) {
436 size_t failure_count = 0;
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800437 for (size_t i = 0; i < dex_filenames.size(); i++) {
438 const char* dex_filename = dex_filenames[i];
Brian Carlstroma004aa92012-02-08 18:05:09 -0800439 const char* dex_location = dex_locations[i];
440 const DexFile* dex_file = DexFile::Open(dex_filename, dex_location);
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800441 if (dex_file == NULL) {
jeffhao60f83e32012-02-13 17:16:30 -0800442 LOG(WARNING) << "could not open .dex from file " << dex_filename;
Elliott Hughesbf1b4572012-05-24 19:11:39 -0700443 ++failure_count;
jeffhao60f83e32012-02-13 17:16:30 -0800444 } else {
445 dex_files.push_back(dex_file);
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800446 }
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800447 }
Elliott Hughesbf1b4572012-05-24 19:11:39 -0700448 return failure_count;
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800449}
450
Elliott Hughes72395bf2012-04-24 13:45:26 -0700451static int dex2oat(int argc, char** argv) {
Elliott Hughes0d39c122012-06-06 16:41:17 -0700452 InitLogging(argv);
Elliott Hughes72395bf2012-04-24 13:45:26 -0700453
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 Hughes5c599942012-06-13 16:45:05 -0700478 int thread_count = sysconf(_SC_NPROCESSORS_CONF);
Elliott Hughesde6e4cf2012-02-27 14:46:06 -0800479 bool support_debugging = false;
jeffhao9ad4f222012-05-30 18:51:19 -0700480#if defined(__arm__)
Ian Rogers49c48942012-03-11 15:15:37 -0700481 InstructionSet instruction_set = kThumb2;
jeffhao9ad4f222012-05-30 18:51:19 -0700482#elif defined(__i386__)
483 InstructionSet instruction_set = kX86;
484#elif defined(__mips__)
485 InstructionSet instruction_set = kMips;
486#else
487#error "Unsupported architecture"
488#endif
Elliott Hughes67d92002012-03-26 15:08:51 -0700489 bool dump_stats = kIsDebugBuild;
490 bool dump_timings = kIsDebugBuild;
Brian Carlstrom16192862011-09-12 17:50:06 -0700491
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700492 for (int i = 0; i < argc; i++) {
493 const StringPiece option(argv[i]);
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800494 bool log_options = false;
495 if (log_options) {
Brian Carlstromb7bbba42011-10-13 14:58:47 -0700496 LOG(INFO) << "dex2oat: option[" << i << "]=" << argv[i];
497 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700498 if (option.starts_with("--dex-file=")) {
499 dex_filenames.push_back(option.substr(strlen("--dex-file=")).data());
Brian Carlstroma004aa92012-02-08 18:05:09 -0800500 } else if (option.starts_with("--dex-location=")) {
501 dex_locations.push_back(option.substr(strlen("--dex-location=")).data());
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800502 } else if (option.starts_with("--zip-fd=")) {
503 const char* zip_fd_str = option.substr(strlen("--zip-fd=")).data();
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800504 if (!ParseInt(zip_fd_str, &zip_fd)) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800505 Usage("could not parse --zip-fd argument '%s' as an integer", zip_fd_str);
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800506 }
Brian Carlstroma004aa92012-02-08 18:05:09 -0800507 } else if (option.starts_with("--zip-location=")) {
508 zip_location = option.substr(strlen("--zip-location=")).data();
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800509 } else if (option.starts_with("--oat-file=")) {
510 oat_filename = option.substr(strlen("--oat-file=")).data();
511 } else if (option.starts_with("--oat-fd=")) {
512 const char* oat_fd_str = option.substr(strlen("--oat-fd=")).data();
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800513 if (!ParseInt(oat_fd_str, &oat_fd)) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800514 Usage("could not parse --oat-fd argument '%s' as an integer", oat_fd_str);
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800515 }
Elliott Hughesde6e4cf2012-02-27 14:46:06 -0800516 } else if (option.starts_with("-g")) {
517 support_debugging = true;
Elliott Hughes5523ee02012-02-03 18:18:34 -0800518 } else if (option.starts_with("-j")) {
Brian Carlstromb12552a2012-02-04 17:17:31 -0800519 const char* thread_count_str = option.substr(strlen("-j")).data();
Elliott Hughes5523ee02012-02-03 18:18:34 -0800520 if (!ParseInt(thread_count_str, &thread_count)) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800521 Usage("could not parse -j argument '%s' as an integer", thread_count_str);
Elliott Hughes5523ee02012-02-03 18:18:34 -0800522 }
Brian Carlstroma004aa92012-02-08 18:05:09 -0800523 } else if (option.starts_with("--oat-location=")) {
524 oat_location = option.substr(strlen("--oat-location=")).data();
Logan Chien8b977d32012-02-21 19:14:55 +0800525#if defined(ART_USE_LLVM_COMPILER)
Logan Chien8b977d32012-02-21 19:14:55 +0800526 } else if (option.starts_with("--bitcode=")) {
527 bitcode_filename = option.substr(strlen("--bitcode=")).data();
528#endif
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700529 } else if (option.starts_with("--image=")) {
530 image_filename = option.substr(strlen("--image=")).data();
Brian Carlstromae826982011-11-09 01:33:42 -0800531 } else if (option.starts_with("--image-classes=")) {
532 image_classes_filename = option.substr(strlen("--image-classes=")).data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700533 } else if (option.starts_with("--base=")) {
534 const char* image_base_str = option.substr(strlen("--base=")).data();
535 char* end;
536 image_base = strtoul(image_base_str, &end, 16);
537 if (end == image_base_str || *end != '\0') {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800538 Usage("Failed to parse hexadecimal value for option %s", option.data());
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700539 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700540 } else if (option.starts_with("--boot-image=")) {
Brian Carlstromb0011262011-12-09 12:17:24 -0800541 boot_image_filename = option.substr(strlen("--boot-image=")).data();
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700542 } else if (option.starts_with("--host-prefix=")) {
Brian Carlstrom34f1fa42012-04-05 18:28:12 -0700543 host_prefix.reset(new std::string(option.substr(strlen("--host-prefix=")).data()));
Ian Rogers49c48942012-03-11 15:15:37 -0700544 } else if (option.starts_with("--instruction-set=")) {
545 StringPiece instruction_set_str = option.substr(strlen("--instruction-set=")).data();
jeffhao1f71ae82012-05-24 16:08:24 -0700546 if (instruction_set_str == "arm") {
Ian Rogers49c48942012-03-11 15:15:37 -0700547 instruction_set = kThumb2;
jeffhao1f71ae82012-05-24 16:08:24 -0700548 } else if (instruction_set_str == "mips") {
Ian Rogers49c48942012-03-11 15:15:37 -0700549 instruction_set = kMips;
jeffhao1f71ae82012-05-24 16:08:24 -0700550 } else if (instruction_set_str == "x86") {
Ian Rogers49c48942012-03-11 15:15:37 -0700551 instruction_set = kX86;
552 }
jeffhao5d840402011-10-24 17:09:45 -0700553 } else if (option == "--runtime-arg") {
554 if (++i >= argc) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800555 Usage("Missing required argument for --runtime-arg");
jeffhao5d840402011-10-24 17:09:45 -0700556 }
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800557 if (log_options) {
558 LOG(INFO) << "dex2oat: option[" << i << "]=" << argv[i];
559 }
jeffhao5d840402011-10-24 17:09:45 -0700560 runtime_args.push_back(argv[i]);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700561 } else {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800562 Usage("unknown argument %s", option.data());
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700563 }
564 }
565
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800566 if (oat_filename.empty() && oat_fd == -1) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800567 Usage("Output must be supplied with either --oat-file or --oat-fd");
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800568 }
569
570 if (!oat_filename.empty() && oat_fd != -1) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800571 Usage("--oat-file should not be used with --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
Brian Carlstroma004aa92012-02-08 18:05:09 -0800578 if (oat_fd != -1 && !image_filename.empty()) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800579 Usage("--oat-fd should not be used with --image");
Brian Carlstrome24fa612011-09-29 00:53:55 -0700580 }
581
Brian Carlstrom34f1fa42012-04-05 18:28:12 -0700582 if (host_prefix.get() == NULL) {
Brian Carlstromb0011262011-12-09 12:17:24 -0800583 const char* android_product_out = getenv("ANDROID_PRODUCT_OUT");
584 if (android_product_out != NULL) {
Brian Carlstrom34f1fa42012-04-05 18:28:12 -0700585 host_prefix.reset(new std::string(android_product_out));
Brian Carlstromb0011262011-12-09 12:17:24 -0800586 }
587 }
588
Brian Carlstroma004aa92012-02-08 18:05:09 -0800589 bool image = (!image_filename.empty());
Brian Carlstromb0011262011-12-09 12:17:24 -0800590 if (!image && boot_image_filename.empty()) {
Brian Carlstrom34f1fa42012-04-05 18:28:12 -0700591 if (host_prefix.get() == NULL) {
Brian Carlstroma56fcd62012-02-04 21:23:01 -0800592 boot_image_filename += GetAndroidRoot();
593 } else {
Brian Carlstrom34f1fa42012-04-05 18:28:12 -0700594 boot_image_filename += *host_prefix.get();
Brian Carlstroma56fcd62012-02-04 21:23:01 -0800595 boot_image_filename += "/system";
596 }
597 boot_image_filename += "/framework/boot.art";
Brian Carlstromb0011262011-12-09 12:17:24 -0800598 }
599 std::string boot_image_option;
Brian Carlstroma004aa92012-02-08 18:05:09 -0800600 if (!boot_image_filename.empty()) {
Brian Carlstromb0011262011-12-09 12:17:24 -0800601 boot_image_option += "-Ximage:";
602 boot_image_option += boot_image_filename;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700603 }
604
Brian Carlstromae826982011-11-09 01:33:42 -0800605 if (image_classes_filename != NULL && !image) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800606 Usage("--image-classes should only be used with --image");
Brian Carlstromae826982011-11-09 01:33:42 -0800607 }
608
609 if (image_classes_filename != NULL && !boot_image_option.empty()) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800610 Usage("--image-classes should not be used with --boot-image");
Brian Carlstrom78128a62011-09-15 17:21:19 -0700611 }
612
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800613 if (dex_filenames.empty() && zip_fd == -1) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800614 Usage("Input must be supplied with either --dex-file or --zip-fd");
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800615 }
616
617 if (!dex_filenames.empty() && zip_fd != -1) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800618 Usage("--dex-file should not be used with --zip-fd");
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800619 }
620
Brian Carlstroma004aa92012-02-08 18:05:09 -0800621 if (!dex_filenames.empty() && !zip_location.empty()) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800622 Usage("--dex-file should not be used with --zip-location");
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800623 }
624
Brian Carlstroma004aa92012-02-08 18:05:09 -0800625 if (dex_locations.empty()) {
626 for (size_t i = 0; i < dex_filenames.size(); i++) {
627 dex_locations.push_back(dex_filenames[i]);
628 }
629 } else if (dex_locations.size() != dex_filenames.size()) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800630 Usage("--dex-location arguments do not match --dex-file arguments");
Brian Carlstroma004aa92012-02-08 18:05:09 -0800631 }
632
633 if (zip_fd != -1 && zip_location.empty()) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800634 Usage("--zip-location should be supplied with --zip-fd");
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800635 }
636
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700637 if (boot_image_option.empty()) {
638 if (image_base == 0) {
Brian Carlstromcbfe6fe2012-02-17 11:13:36 -0800639 Usage("non-zero --base not specified");
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700640 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700641 }
642
Brian Carlstrom6ef827a2011-12-11 14:57:47 -0800643 // Check early that the result of compilation can be written
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800644 UniquePtr<File> oat_file;
Brian Carlstrom6cd40e52012-05-03 14:15:11 -0700645 bool create_file = !oat_filename.empty(); // as opposed to using open file descriptor
646 if (create_file) {
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800647 oat_file.reset(OS::OpenFile(oat_filename.c_str(), true));
Brian Carlstroma004aa92012-02-08 18:05:09 -0800648 if (oat_location.empty()) {
649 oat_location = oat_filename;
650 }
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800651 } else {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800652 oat_file.reset(OS::FileFromFd(oat_location.c_str(), oat_fd));
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800653 }
Brian Carlstrom6ef827a2011-12-11 14:57:47 -0800654 if (oat_file.get() == NULL) {
Brian Carlstrom6cd40e52012-05-03 14:15:11 -0700655 PLOG(ERROR) << "Failed to create oat file: " << oat_location;
656 return EXIT_FAILURE;
657 }
658 if (create_file && fchmod(oat_file->Fd(), 0644) != 0) {
659 PLOG(ERROR) << "Failed to make oat file world readable: " << oat_location;
Brian Carlstrom6ef827a2011-12-11 14:57:47 -0800660 return EXIT_FAILURE;
Ian Rogers5e863dd2011-11-02 20:00:10 -0700661 }
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800662
Brian Carlstroma004aa92012-02-08 18:05:09 -0800663 LOG(INFO) << "dex2oat: " << oat_location;
Ian Rogers5e863dd2011-11-02 20:00:10 -0700664
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700665 Runtime::Options options;
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700666 options.push_back(std::make_pair("compiler", reinterpret_cast<void*>(NULL)));
Brian Carlstroma004aa92012-02-08 18:05:09 -0800667 std::vector<const DexFile*> boot_class_path;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700668 if (boot_image_option.empty()) {
Elliott Hughesbf1b4572012-05-24 19:11:39 -0700669 size_t failure_count = OpenDexFiles(dex_filenames, dex_locations, boot_class_path);
670 if (failure_count > 0) {
671 LOG(ERROR) << "Failed to open some dex files: " << failure_count;
672 return EXIT_FAILURE;
673 }
Brian Carlstroma004aa92012-02-08 18:05:09 -0800674 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 {
Elliott Hughesbf1b4572012-05-24 19:11:39 -0700715 size_t failure_count = OpenDexFiles(dex_filenames, dex_locations, dex_files);
716 if (failure_count > 0) {
717 LOG(ERROR) << "Failed to open some dex files: " << failure_count;
718 return EXIT_FAILURE;
719 }
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800720 }
721 }
722
Brian Carlstromf5822582012-03-19 22:34:31 -0700723 UniquePtr<const Compiler> compiler(dex2oat->CreateOatFile(boot_image_option,
Brian Carlstrom34f1fa42012-04-05 18:28:12 -0700724 host_prefix.get(),
Brian Carlstromf5822582012-03-19 22:34:31 -0700725 dex_files,
726 oat_file.get(),
Logan Chien8b977d32012-02-21 19:14:55 +0800727#if defined(ART_USE_LLVM_COMPILER)
Brian Carlstromf5822582012-03-19 22:34:31 -0700728 bitcode_filename,
Logan Chien8b977d32012-02-21 19:14:55 +0800729#endif
Brian Carlstromf5822582012-03-19 22:34:31 -0700730 image,
Brian Carlstromba0668e2012-03-26 13:14:07 -0700731 image_classes.get(),
732 dump_stats,
733 dump_timings));
Brian Carlstromf5822582012-03-19 22:34:31 -0700734
735 if (compiler.get() == NULL) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800736 LOG(ERROR) << "Failed to create oat file: " << oat_location;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700737 return EXIT_FAILURE;
738 }
739
Brian Carlstromae826982011-11-09 01:33:42 -0800740 if (!image) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800741 LOG(INFO) << "Oat file written successfully: " << oat_location;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700742 return EXIT_SUCCESS;
743 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700744
Brian Carlstromae826982011-11-09 01:33:42 -0800745 if (!dex2oat->CreateImageFile(image_filename,
746 image_base,
747 image_classes.get(),
748 oat_filename,
Brian Carlstromf5822582012-03-19 22:34:31 -0700749 oat_location,
750 *compiler.get())) {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700751 return EXIT_FAILURE;
752 }
753
Brian Carlstromae826982011-11-09 01:33:42 -0800754 // We wrote the oat file successfully, and want to keep it.
Elliott Hughesbb551fa2012-01-25 16:35:29 -0800755 LOG(INFO) << "Oat file written successfully: " << oat_filename;
756 LOG(INFO) << "Image written successfully: " << image_filename;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700757 return EXIT_SUCCESS;
758}
759
760} // namespace art
761
762int main(int argc, char** argv) {
763 return art::dex2oat(argc, argv);
764}