Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 1 | /* |
| 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 Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 16 | |
| 17 | #include <stdio.h> |
| 18 | #include <stdlib.h> |
Brian Carlstrom | 6cd40e5 | 2012-05-03 14:15:11 -0700 | [diff] [blame] | 19 | #include <sys/stat.h> |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 20 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 21 | #include <iostream> |
| 22 | #include <fstream> |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 23 | #include <string> |
| 24 | #include <vector> |
| 25 | |
| 26 | #include "class_linker.h" |
| 27 | #include "class_loader.h" |
| 28 | #include "compiler.h" |
Ian Rogers | 5e863dd | 2011-11-02 20:00:10 -0700 | [diff] [blame] | 29 | #include "file.h" |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 30 | #include "image_writer.h" |
Ian Rogers | 6f1dfe4 | 2011-12-08 17:28:34 -0800 | [diff] [blame] | 31 | #include "leb128.h" |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 32 | #include "oat_writer.h" |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 33 | #include "object_utils.h" |
Ian Rogers | 5e863dd | 2011-11-02 20:00:10 -0700 | [diff] [blame] | 34 | #include "os.h" |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 35 | #include "runtime.h" |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 36 | #include "ScopedLocalRef.h" |
| 37 | #include "scoped_thread_state_change.h" |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 38 | #include "stl_util.h" |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 39 | #include "stringpiece.h" |
Elliott Hughes | bb551fa | 2012-01-25 16:35:29 -0800 | [diff] [blame] | 40 | #include "timing_logger.h" |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 41 | #include "well_known_classes.h" |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 42 | #include "zip_archive.h" |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 43 | |
| 44 | namespace art { |
| 45 | |
Brian Carlstrom | cbfe6fe | 2012-02-17 11:13:36 -0800 | [diff] [blame] | 46 | static void UsageErrorV(const char* fmt, va_list ap) { |
| 47 | std::string error; |
| 48 | StringAppendV(&error, fmt, ap); |
| 49 | LOG(ERROR) << error; |
| 50 | } |
| 51 | |
| 52 | static void UsageError(const char* fmt, ...) { |
| 53 | va_list ap; |
| 54 | va_start(ap, fmt); |
| 55 | UsageErrorV(fmt, ap); |
| 56 | va_end(ap); |
| 57 | } |
| 58 | |
| 59 | static void Usage(const char* fmt, ...) { |
| 60 | va_list ap; |
| 61 | va_start(ap, fmt); |
| 62 | UsageErrorV(fmt, ap); |
| 63 | va_end(ap); |
| 64 | |
| 65 | UsageError("Usage: dex2oat [options]..."); |
| 66 | UsageError(""); |
| 67 | UsageError(" --dex-file=<dex-file>: specifies a .dex file to compile."); |
| 68 | UsageError(" Example: --dex-file=/system/framework/core.jar"); |
| 69 | UsageError(""); |
| 70 | UsageError(" --zip-fd=<file-descriptor>: specifies a file descriptor of a zip file"); |
| 71 | UsageError(" containing a classes.dex file to compile."); |
| 72 | UsageError(" Example: --zip-fd=5"); |
| 73 | UsageError(""); |
| 74 | UsageError(" --zip-location=<zip-location>: specifies a symbolic name for the file corresponding"); |
| 75 | UsageError(" to the file descriptor specified by --zip-fd."); |
| 76 | UsageError(" Example: --zip-location=/system/app/Calculator.apk"); |
| 77 | UsageError(""); |
| 78 | UsageError(" --oat-file=<file.oat>: specifies the required oat filename."); |
| 79 | UsageError(" Example: --oat-file=/system/framework/boot.oat"); |
| 80 | UsageError(""); |
| 81 | UsageError(" --oat-location=<oat-name>: specifies a symbolic name for the file corresponding"); |
| 82 | UsageError(" to the file descriptor specified by --oat-fd."); |
| 83 | UsageError(" Example: --oat-location=/data/art-cache/system@app@Calculator.apk.oat"); |
| 84 | UsageError(""); |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 85 | #if defined(ART_USE_LLVM_COMPILER) |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 86 | UsageError(" --bitcode=<file.bc>: specifies the optional bitcode filename."); |
| 87 | UsageError(" Example: --bitcode=/system/framework/boot.bc"); |
| 88 | UsageError(""); |
| 89 | #endif |
Brian Carlstrom | cbfe6fe | 2012-02-17 11:13:36 -0800 | [diff] [blame] | 90 | UsageError(" --image=<file.art>: specifies the output image filename."); |
| 91 | UsageError(" Example: --image=/system/framework/boot.art"); |
| 92 | UsageError(""); |
| 93 | UsageError(" --image-classes=<classname-file>: specifies classes to include in an image."); |
| 94 | UsageError(" Example: --image=frameworks/base/preloaded-classes"); |
| 95 | UsageError(""); |
| 96 | UsageError(" --base=<hex-address>: specifies the base address when creating a boot image."); |
| 97 | UsageError(" Example: --base=0x50000000"); |
| 98 | UsageError(""); |
| 99 | UsageError(" --boot-image=<file.art>: provide the image file for the boot class path."); |
| 100 | UsageError(" Example: --boot-image=/system/framework/boot.art"); |
| 101 | UsageError(" Default: <host-prefix>/system/framework/boot.art"); |
| 102 | UsageError(""); |
| 103 | UsageError(" --host-prefix may be used to translate host paths to target paths during"); |
| 104 | UsageError(" cross compilation."); |
| 105 | UsageError(" Example: --host-prefix=out/target/product/crespo"); |
| 106 | UsageError(" Default: $ANDROID_PRODUCT_OUT"); |
| 107 | UsageError(""); |
jeffhao | 1f71ae8 | 2012-05-24 16:08:24 -0700 | [diff] [blame] | 108 | UsageError(" --instruction-set=(arm|mips|x86): compile for a particular instruction"); |
Ian Rogers | 49c4894 | 2012-03-11 15:15:37 -0700 | [diff] [blame] | 109 | UsageError(" set."); |
jeffhao | 1f71ae8 | 2012-05-24 16:08:24 -0700 | [diff] [blame] | 110 | UsageError(" Example: --instruction-set=x86"); |
| 111 | UsageError(" Default: arm"); |
Ian Rogers | 49c4894 | 2012-03-11 15:15:37 -0700 | [diff] [blame] | 112 | UsageError(""); |
Brian Carlstrom | cbfe6fe | 2012-02-17 11:13:36 -0800 | [diff] [blame] | 113 | UsageError(" --runtime-arg <argument>: used to specify various arguments for the runtime,"); |
| 114 | UsageError(" such as initial heap size, maximum heap size, and verbose output."); |
| 115 | UsageError(" Use a separate --runtime-arg switch for each argument."); |
| 116 | UsageError(" Example: --runtime-arg -Xms256m"); |
| 117 | UsageError(""); |
| 118 | std::cerr << "See log for usage error information\n"; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 119 | exit(EXIT_FAILURE); |
| 120 | } |
| 121 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 122 | class Dex2Oat { |
| 123 | public: |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 124 | static bool Create(Dex2Oat** p_dex2oat, Runtime::Options& options, InstructionSet instruction_set, |
| 125 | size_t thread_count, bool support_debugging) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 126 | SHARED_TRYLOCK_FUNCTION(true, Locks::mutator_lock_) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 127 | if (!CreateRuntime(options, instruction_set)) { |
| 128 | *p_dex2oat = NULL; |
| 129 | return false; |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 130 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 131 | *p_dex2oat = new Dex2Oat(Runtime::Current(), instruction_set, thread_count, support_debugging); |
| 132 | return true; |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | ~Dex2Oat() { |
| 136 | delete runtime_; |
Elliott Hughes | 5523ee0 | 2012-02-03 18:18:34 -0800 | [diff] [blame] | 137 | LOG(INFO) << "dex2oat took " << PrettyDuration(NanoTime() - start_ns_) << " (threads: " << thread_count_ << ")"; |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | // Make a list of descriptors for classes to include in the image |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 141 | const std::set<std::string>* GetImageClassDescriptors(const char* image_classes_filename) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 142 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 143 | UniquePtr<std::ifstream> image_classes_file(new std::ifstream(image_classes_filename, std::ifstream::in)); |
| 144 | if (image_classes_file.get() == NULL) { |
| 145 | LOG(ERROR) << "Failed to open image classes file " << image_classes_filename; |
| 146 | return NULL; |
| 147 | } |
| 148 | |
Ian Rogers | 6f1dfe4 | 2011-12-08 17:28:34 -0800 | [diff] [blame] | 149 | // Load all the classes specified in the file |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 150 | ClassLinker* class_linker = runtime_->GetClassLinker(); |
| 151 | while (image_classes_file->good()) { |
| 152 | std::string dot; |
| 153 | std::getline(*image_classes_file.get(), dot); |
Elliott Hughes | f1a5adc | 2012-02-10 18:09:35 -0800 | [diff] [blame] | 154 | if (StartsWith(dot, "#") || dot.empty()) { |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 155 | continue; |
| 156 | } |
Elliott Hughes | 9557241 | 2011-12-13 18:14:20 -0800 | [diff] [blame] | 157 | std::string descriptor(DotToDescriptor(dot.c_str())); |
Elliott Hughes | c3b77c7 | 2011-12-15 20:56:48 -0800 | [diff] [blame] | 158 | SirtRef<Class> klass(class_linker->FindSystemClass(descriptor.c_str())); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 159 | if (klass.get() == NULL) { |
| 160 | LOG(WARNING) << "Failed to find class " << descriptor; |
| 161 | Thread::Current()->ClearException(); |
| 162 | } |
| 163 | } |
| 164 | image_classes_file->close(); |
| 165 | |
Ian Rogers | 6f1dfe4 | 2011-12-08 17:28:34 -0800 | [diff] [blame] | 166 | // Resolve exception classes referenced by the loaded classes. The catch logic assumes |
| 167 | // exceptions are resolved by the verifier when there is a catch block in an interested method. |
| 168 | // Do this here so that exception classes appear to have been specified image classes. |
| 169 | std::set<std::pair<uint16_t, const DexFile*> > unresolved_exception_types; |
Elliott Hughes | 35be9b1 | 2012-05-29 17:59:26 -0700 | [diff] [blame] | 170 | SirtRef<Class> java_lang_Throwable(class_linker->FindSystemClass("Ljava/lang/Throwable;")); |
Ian Rogers | 6f1dfe4 | 2011-12-08 17:28:34 -0800 | [diff] [blame] | 171 | do { |
| 172 | unresolved_exception_types.clear(); |
| 173 | class_linker->VisitClasses(ResolveCatchBlockExceptionsClassVisitor, |
| 174 | &unresolved_exception_types); |
| 175 | typedef std::set<std::pair<uint16_t, const DexFile*> >::const_iterator It; // TODO: C++0x auto |
| 176 | for (It it = unresolved_exception_types.begin(), |
| 177 | end = unresolved_exception_types.end(); |
| 178 | it != end; ++it) { |
| 179 | uint16_t exception_type_idx = it->first; |
| 180 | const DexFile* dex_file = it->second; |
| 181 | DexCache* dex_cache = class_linker->FindDexCache(*dex_file); |
| 182 | ClassLoader* class_loader = NULL; |
| 183 | SirtRef<Class> klass(class_linker->ResolveType(*dex_file, exception_type_idx, dex_cache, |
| 184 | class_loader)); |
| 185 | if (klass.get() == NULL) { |
| 186 | const DexFile::TypeId& type_id = dex_file->GetTypeId(exception_type_idx); |
| 187 | const char* descriptor = dex_file->GetTypeDescriptor(type_id); |
| 188 | LOG(FATAL) << "Failed to resolve class " << descriptor; |
| 189 | } |
Elliott Hughes | 35be9b1 | 2012-05-29 17:59:26 -0700 | [diff] [blame] | 190 | DCHECK(java_lang_Throwable->IsAssignableFrom(klass.get())); |
Ian Rogers | 6f1dfe4 | 2011-12-08 17:28:34 -0800 | [diff] [blame] | 191 | } |
| 192 | // Resolving exceptions may load classes that reference more exceptions, iterate until no |
| 193 | // more are found |
| 194 | } while (!unresolved_exception_types.empty()); |
| 195 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 196 | // We walk the roots looking for classes so that we'll pick up the |
| 197 | // above classes plus any classes them depend on such super |
| 198 | // classes, interfaces, and the required ClassLinker roots. |
| 199 | UniquePtr<std::set<std::string> > image_classes(new std::set<std::string>()); |
Ian Rogers | 6f1dfe4 | 2011-12-08 17:28:34 -0800 | [diff] [blame] | 200 | class_linker->VisitClasses(RecordImageClassesVisitor, image_classes.get()); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 201 | CHECK_NE(image_classes->size(), 0U); |
| 202 | return image_classes.release(); |
| 203 | } |
| 204 | |
Brian Carlstrom | f582258 | 2012-03-19 22:34:31 -0700 | [diff] [blame] | 205 | const Compiler* CreateOatFile(const std::string& boot_image_option, |
Brian Carlstrom | 34f1fa4 | 2012-04-05 18:28:12 -0700 | [diff] [blame] | 206 | const std::string* host_prefix, |
Brian Carlstrom | f582258 | 2012-03-19 22:34:31 -0700 | [diff] [blame] | 207 | const std::vector<const DexFile*>& dex_files, |
| 208 | File* oat_file, |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 209 | #if defined(ART_USE_LLVM_COMPILER) |
Logan Chien | de08e84 | 2012-03-21 00:34:12 +0800 | [diff] [blame] | 210 | const std::string& bitcode_filename, |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 211 | #endif |
Brian Carlstrom | f582258 | 2012-03-19 22:34:31 -0700 | [diff] [blame] | 212 | bool image, |
Brian Carlstrom | ba0668e | 2012-03-26 13:14:07 -0700 | [diff] [blame] | 213 | const std::set<std::string>* image_classes, |
| 214 | bool dump_stats, |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 215 | bool dump_timings) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 216 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 217 | // SirtRef and ClassLoader creation needs to come after Runtime::Create |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 218 | jobject class_loader = NULL; |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 219 | if (!boot_image_option.empty()) { |
| 220 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 221 | std::vector<const DexFile*> class_path_files(dex_files); |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 222 | OpenClassPathFiles(runtime_->GetClassPathString(), class_path_files); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 223 | for (size_t i = 0; i < class_path_files.size(); i++) { |
| 224 | class_linker->RegisterDexFile(*class_path_files[i]); |
| 225 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 226 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
| 227 | soa.Env()->AllocObject(WellKnownClasses::dalvik_system_PathClassLoader); |
| 228 | ScopedLocalRef<jobject> class_loader_local(soa.Env(), |
| 229 | soa.Env()->AllocObject(WellKnownClasses::dalvik_system_PathClassLoader)); |
| 230 | class_loader = soa.Env()->NewGlobalRef(class_loader_local.get()); |
| 231 | Runtime::Current()->SetCompileTimeClassPath(class_loader, class_path_files); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 232 | } |
| 233 | |
Brian Carlstrom | f582258 | 2012-03-19 22:34:31 -0700 | [diff] [blame] | 234 | UniquePtr<Compiler> compiler(new Compiler(instruction_set_, |
| 235 | image, |
| 236 | thread_count_, |
| 237 | support_debugging_, |
Brian Carlstrom | ba0668e | 2012-03-26 13:14:07 -0700 | [diff] [blame] | 238 | image_classes, |
| 239 | dump_stats, |
| 240 | dump_timings)); |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 241 | |
| 242 | #if defined(ART_USE_LLVM_COMPILER) |
Brian Carlstrom | f582258 | 2012-03-19 22:34:31 -0700 | [diff] [blame] | 243 | compiler->SetBitcodeFileName(bitcode_filename); |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 244 | #endif |
| 245 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 246 | Thread::Current()->TransitionFromRunnableToSuspended(kNative); |
| 247 | |
| 248 | compiler->CompileAll(class_loader, dex_files); |
| 249 | |
| 250 | Thread::Current()->TransitionFromSuspendedToRunnable(); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 251 | |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 252 | std::string image_file_location; |
| 253 | uint32_t image_file_location_checksum = 0; |
| 254 | Heap* heap = Runtime::Current()->GetHeap(); |
| 255 | if (heap->GetSpaces().size() > 1) { |
| 256 | ImageSpace* image_space = heap->GetImageSpace(); |
| 257 | image_file_location_checksum = image_space->GetImageHeader().GetOatChecksum(); |
| 258 | image_file_location = image_space->GetImageFilename(); |
Brian Carlstrom | 34f1fa4 | 2012-04-05 18:28:12 -0700 | [diff] [blame] | 259 | if (host_prefix != NULL && StartsWith(image_file_location, host_prefix->c_str())) { |
| 260 | image_file_location = image_file_location.substr(host_prefix->size()); |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 261 | } |
| 262 | } |
| 263 | |
| 264 | if (!OatWriter::Create(oat_file, |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 265 | class_loader, |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 266 | dex_files, |
| 267 | image_file_location_checksum, |
| 268 | image_file_location, |
Brian Carlstrom | f582258 | 2012-03-19 22:34:31 -0700 | [diff] [blame] | 269 | *compiler.get())) { |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 270 | LOG(ERROR) << "Failed to create oat file " << oat_file->name(); |
Brian Carlstrom | f582258 | 2012-03-19 22:34:31 -0700 | [diff] [blame] | 271 | return NULL; |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 272 | } |
Brian Carlstrom | f582258 | 2012-03-19 22:34:31 -0700 | [diff] [blame] | 273 | return compiler.release(); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 274 | } |
| 275 | |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 276 | bool CreateImageFile(const std::string& image_filename, |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 277 | uintptr_t image_base, |
| 278 | const std::set<std::string>* image_classes, |
| 279 | const std::string& oat_filename, |
Brian Carlstrom | f582258 | 2012-03-19 22:34:31 -0700 | [diff] [blame] | 280 | const std::string& oat_location, |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 281 | const Compiler& compiler) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 282 | LOCKS_EXCLUDED(Locks::mutator_lock_) { |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 283 | ImageWriter image_writer(image_classes); |
Brian Carlstrom | f582258 | 2012-03-19 22:34:31 -0700 | [diff] [blame] | 284 | if (!image_writer.Write(image_filename, image_base, oat_filename, oat_location, compiler)) { |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 285 | LOG(ERROR) << "Failed to create image file " << image_filename; |
| 286 | return false; |
| 287 | } |
| 288 | return true; |
| 289 | } |
| 290 | |
| 291 | private: |
Ian Rogers | 49c4894 | 2012-03-11 15:15:37 -0700 | [diff] [blame] | 292 | explicit Dex2Oat(Runtime* runtime, InstructionSet instruction_set, size_t thread_count, |
| 293 | bool support_debugging) |
| 294 | : instruction_set_(instruction_set), |
| 295 | runtime_(runtime), |
Elliott Hughes | de6e4cf | 2012-02-27 14:46:06 -0800 | [diff] [blame] | 296 | thread_count_(thread_count), |
| 297 | support_debugging_(support_debugging), |
| 298 | start_ns_(NanoTime()) { |
Elliott Hughes | bb551fa | 2012-01-25 16:35:29 -0800 | [diff] [blame] | 299 | } |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 300 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 301 | static bool CreateRuntime(Runtime::Options& options, InstructionSet instruction_set) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 302 | SHARED_TRYLOCK_FUNCTION(true, Locks::mutator_lock_) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 303 | if (!Runtime::Create(options, false)) { |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 304 | LOG(ERROR) << "Failed to create runtime"; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 305 | return false; |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 306 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 307 | Runtime* runtime = Runtime::Current(); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 308 | // if we loaded an existing image, we will reuse values from the image roots. |
| 309 | if (!runtime->HasJniDlsymLookupStub()) { |
Ian Rogers | 49c4894 | 2012-03-11 15:15:37 -0700 | [diff] [blame] | 310 | runtime->SetJniDlsymLookupStub(Compiler::CreateJniDlsymLookupStub(instruction_set)); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 311 | } |
| 312 | if (!runtime->HasAbstractMethodErrorStubArray()) { |
Ian Rogers | 49c4894 | 2012-03-11 15:15:37 -0700 | [diff] [blame] | 313 | runtime->SetAbstractMethodErrorStubArray(Compiler::CreateAbstractMethodErrorStub(instruction_set)); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 314 | } |
| 315 | for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) { |
| 316 | Runtime::TrampolineType type = Runtime::TrampolineType(i); |
| 317 | if (!runtime->HasResolutionStubArray(type)) { |
Ian Rogers | 49c4894 | 2012-03-11 15:15:37 -0700 | [diff] [blame] | 318 | runtime->SetResolutionStubArray(Compiler::CreateResolutionStub(instruction_set, type), type); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 319 | } |
| 320 | } |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 321 | if (!runtime->HasResolutionMethod()) { |
| 322 | runtime->SetResolutionMethod(runtime->CreateResolutionMethod()); |
| 323 | } |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 324 | for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) { |
| 325 | Runtime::CalleeSaveType type = Runtime::CalleeSaveType(i); |
| 326 | if (!runtime->HasCalleeSaveMethod(type)) { |
Ian Rogers | 49c4894 | 2012-03-11 15:15:37 -0700 | [diff] [blame] | 327 | runtime->SetCalleeSaveMethod(runtime->CreateCalleeSaveMethod(instruction_set, type), type); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 328 | } |
| 329 | } |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 330 | runtime->GetClassLinker()->FixupDexCaches(runtime->GetResolutionMethod()); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 331 | return true; |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 332 | } |
| 333 | |
Ian Rogers | 6f1dfe4 | 2011-12-08 17:28:34 -0800 | [diff] [blame] | 334 | static void ResolveExceptionsForMethod(MethodHelper* mh, |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 335 | std::set<std::pair<uint16_t, const DexFile*> >& exceptions_to_resolve) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 336 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 6f1dfe4 | 2011-12-08 17:28:34 -0800 | [diff] [blame] | 337 | const DexFile::CodeItem* code_item = mh->GetCodeItem(); |
| 338 | if (code_item == NULL) { |
| 339 | return; // native or abstract method |
| 340 | } |
| 341 | if (code_item->tries_size_ == 0) { |
| 342 | return; // nothing to process |
| 343 | } |
| 344 | const byte* encoded_catch_handler_list = DexFile::GetCatchHandlerData(*code_item, 0); |
| 345 | size_t num_encoded_catch_handlers = DecodeUnsignedLeb128(&encoded_catch_handler_list); |
| 346 | for (size_t i = 0; i < num_encoded_catch_handlers; i++) { |
| 347 | int32_t encoded_catch_handler_size = DecodeSignedLeb128(&encoded_catch_handler_list); |
| 348 | bool has_catch_all = false; |
| 349 | if (encoded_catch_handler_size <= 0) { |
| 350 | encoded_catch_handler_size = -encoded_catch_handler_size; |
| 351 | has_catch_all = true; |
| 352 | } |
| 353 | for (int32_t j = 0; j < encoded_catch_handler_size; j++) { |
| 354 | uint16_t encoded_catch_handler_handlers_type_idx = |
| 355 | DecodeUnsignedLeb128(&encoded_catch_handler_list); |
| 356 | // Add to set of types to resolve if not already in the dex cache resolved types |
| 357 | if (!mh->IsResolvedTypeIdx(encoded_catch_handler_handlers_type_idx)) { |
| 358 | exceptions_to_resolve.insert( |
| 359 | std::pair<uint16_t, const DexFile*>(encoded_catch_handler_handlers_type_idx, |
| 360 | &mh->GetDexFile())); |
| 361 | } |
| 362 | // ignore address associated with catch handler |
| 363 | DecodeUnsignedLeb128(&encoded_catch_handler_list); |
| 364 | } |
| 365 | if (has_catch_all) { |
| 366 | // ignore catch all address |
| 367 | DecodeUnsignedLeb128(&encoded_catch_handler_list); |
| 368 | } |
| 369 | } |
| 370 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 371 | |
| 372 | static bool ResolveCatchBlockExceptionsClassVisitor(Class* c, void* arg) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 373 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 6f1dfe4 | 2011-12-08 17:28:34 -0800 | [diff] [blame] | 374 | std::set<std::pair<uint16_t, const DexFile*> >* exceptions_to_resolve = |
| 375 | reinterpret_cast<std::set<std::pair<uint16_t, const DexFile*> >*>(arg); |
| 376 | MethodHelper mh; |
| 377 | for (size_t i = 0; i < c->NumVirtualMethods(); ++i) { |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 378 | AbstractMethod* m = c->GetVirtualMethod(i); |
Ian Rogers | 6f1dfe4 | 2011-12-08 17:28:34 -0800 | [diff] [blame] | 379 | mh.ChangeMethod(m); |
| 380 | ResolveExceptionsForMethod(&mh, *exceptions_to_resolve); |
| 381 | } |
| 382 | for (size_t i = 0; i < c->NumDirectMethods(); ++i) { |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 383 | AbstractMethod* m = c->GetDirectMethod(i); |
Ian Rogers | 6f1dfe4 | 2011-12-08 17:28:34 -0800 | [diff] [blame] | 384 | mh.ChangeMethod(m); |
| 385 | ResolveExceptionsForMethod(&mh, *exceptions_to_resolve); |
| 386 | } |
| 387 | return true; |
| 388 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 389 | |
| 390 | static bool RecordImageClassesVisitor(Class* klass, void* arg) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 391 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 392 | std::set<std::string>* image_classes = reinterpret_cast<std::set<std::string>*>(arg); |
| 393 | if (klass->IsArrayClass() || klass->IsPrimitive()) { |
Jesse Wilson | 254db0f | 2011-11-16 16:44:11 -0500 | [diff] [blame] | 394 | return true; |
| 395 | } |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 396 | image_classes->insert(ClassHelper(klass).GetDescriptor()); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 397 | return true; |
Jesse Wilson | 254db0f | 2011-11-16 16:44:11 -0500 | [diff] [blame] | 398 | } |
Jesse Wilson | 254db0f | 2011-11-16 16:44:11 -0500 | [diff] [blame] | 399 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 400 | // Appends to dex_files any elements of class_path that it doesn't already |
| 401 | // contain. This will open those dex files as necessary. |
| 402 | static void OpenClassPathFiles(const std::string& class_path, std::vector<const DexFile*>& dex_files) { |
| 403 | std::vector<std::string> parsed; |
| 404 | Split(class_path, ':', parsed); |
| 405 | for (size_t i = 0; i < parsed.size(); ++i) { |
| 406 | if (DexFilesContains(dex_files, parsed[i])) { |
| 407 | continue; |
| 408 | } |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 409 | const DexFile* dex_file = DexFile::Open(parsed[i], parsed[i]); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 410 | if (dex_file == NULL) { |
| 411 | LOG(WARNING) << "Failed to open dex file " << parsed[i]; |
| 412 | } else { |
| 413 | dex_files.push_back(dex_file); |
| 414 | } |
Jesse Wilson | 254db0f | 2011-11-16 16:44:11 -0500 | [diff] [blame] | 415 | } |
| 416 | } |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 417 | |
| 418 | // Returns true if dex_files has a dex with the named location. |
| 419 | static bool DexFilesContains(const std::vector<const DexFile*>& dex_files, const std::string& location) { |
| 420 | for (size_t i = 0; i < dex_files.size(); ++i) { |
| 421 | if (dex_files[i]->GetLocation() == location) { |
| 422 | return true; |
| 423 | } |
| 424 | } |
| 425 | return false; |
| 426 | } |
| 427 | |
Ian Rogers | 49c4894 | 2012-03-11 15:15:37 -0700 | [diff] [blame] | 428 | const InstructionSet instruction_set_; |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 429 | |
Elliott Hughes | bb551fa | 2012-01-25 16:35:29 -0800 | [diff] [blame] | 430 | Runtime* runtime_; |
Elliott Hughes | 5523ee0 | 2012-02-03 18:18:34 -0800 | [diff] [blame] | 431 | size_t thread_count_; |
Elliott Hughes | de6e4cf | 2012-02-27 14:46:06 -0800 | [diff] [blame] | 432 | bool support_debugging_; |
Elliott Hughes | bb551fa | 2012-01-25 16:35:29 -0800 | [diff] [blame] | 433 | uint64_t start_ns_; |
| 434 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 435 | DISALLOW_IMPLICIT_CONSTRUCTORS(Dex2Oat); |
| 436 | }; |
Jesse Wilson | 254db0f | 2011-11-16 16:44:11 -0500 | [diff] [blame] | 437 | |
Elliott Hughes | 72395bf | 2012-04-24 13:45:26 -0700 | [diff] [blame] | 438 | static bool ParseInt(const char* in, int* out) { |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 439 | char* end; |
| 440 | int result = strtol(in, &end, 10); |
| 441 | if (in == end || *end != '\0') { |
| 442 | return false; |
| 443 | } |
| 444 | *out = result; |
| 445 | return true; |
| 446 | } |
| 447 | |
Elliott Hughes | bf1b457 | 2012-05-24 19:11:39 -0700 | [diff] [blame] | 448 | static size_t OpenDexFiles(const std::vector<const char*>& dex_filenames, |
| 449 | const std::vector<const char*>& dex_locations, |
| 450 | std::vector<const DexFile*>& dex_files) { |
| 451 | size_t failure_count = 0; |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 452 | for (size_t i = 0; i < dex_filenames.size(); i++) { |
| 453 | const char* dex_filename = dex_filenames[i]; |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 454 | const char* dex_location = dex_locations[i]; |
| 455 | const DexFile* dex_file = DexFile::Open(dex_filename, dex_location); |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 456 | if (dex_file == NULL) { |
jeffhao | 60f83e3 | 2012-02-13 17:16:30 -0800 | [diff] [blame] | 457 | LOG(WARNING) << "could not open .dex from file " << dex_filename; |
Elliott Hughes | bf1b457 | 2012-05-24 19:11:39 -0700 | [diff] [blame] | 458 | ++failure_count; |
jeffhao | 60f83e3 | 2012-02-13 17:16:30 -0800 | [diff] [blame] | 459 | } else { |
| 460 | dex_files.push_back(dex_file); |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 461 | } |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 462 | } |
Elliott Hughes | bf1b457 | 2012-05-24 19:11:39 -0700 | [diff] [blame] | 463 | return failure_count; |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 464 | } |
| 465 | |
Elliott Hughes | 72395bf | 2012-04-24 13:45:26 -0700 | [diff] [blame] | 466 | static int dex2oat(int argc, char** argv) { |
Elliott Hughes | 0d39c12 | 2012-06-06 16:41:17 -0700 | [diff] [blame] | 467 | InitLogging(argv); |
Elliott Hughes | 72395bf | 2012-04-24 13:45:26 -0700 | [diff] [blame] | 468 | |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 469 | // Skip over argv[0]. |
| 470 | argv++; |
| 471 | argc--; |
| 472 | |
| 473 | if (argc == 0) { |
Brian Carlstrom | cbfe6fe | 2012-02-17 11:13:36 -0800 | [diff] [blame] | 474 | Usage("no arguments specified"); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | std::vector<const char*> dex_filenames; |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 478 | std::vector<const char*> dex_locations; |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 479 | int zip_fd = -1; |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 480 | std::string zip_location; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 481 | std::string oat_filename; |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 482 | std::string oat_location; |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 483 | int oat_fd = -1; |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 484 | #if defined(ART_USE_LLVM_COMPILER) |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 485 | std::string bitcode_filename; |
| 486 | #endif |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 487 | const char* image_classes_filename = NULL; |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 488 | std::string image_filename; |
Brian Carlstrom | b001126 | 2011-12-09 12:17:24 -0800 | [diff] [blame] | 489 | std::string boot_image_filename; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 490 | uintptr_t image_base = 0; |
Brian Carlstrom | 34f1fa4 | 2012-04-05 18:28:12 -0700 | [diff] [blame] | 491 | UniquePtr<std::string> host_prefix; |
jeffhao | 5d84040 | 2011-10-24 17:09:45 -0700 | [diff] [blame] | 492 | std::vector<const char*> runtime_args; |
buzbee | a1da8a5 | 2012-07-09 14:00:21 -0700 | [diff] [blame] | 493 | #if defined(ART_USE_QUICK_COMPILER) |
| 494 | int thread_count = 1; |
| 495 | #else |
Elliott Hughes | 5c59994 | 2012-06-13 16:45:05 -0700 | [diff] [blame] | 496 | int thread_count = sysconf(_SC_NPROCESSORS_CONF); |
buzbee | a1da8a5 | 2012-07-09 14:00:21 -0700 | [diff] [blame] | 497 | #endif |
Elliott Hughes | de6e4cf | 2012-02-27 14:46:06 -0800 | [diff] [blame] | 498 | bool support_debugging = false; |
jeffhao | 9ad4f22 | 2012-05-30 18:51:19 -0700 | [diff] [blame] | 499 | #if defined(__arm__) |
Ian Rogers | 49c4894 | 2012-03-11 15:15:37 -0700 | [diff] [blame] | 500 | InstructionSet instruction_set = kThumb2; |
jeffhao | 9ad4f22 | 2012-05-30 18:51:19 -0700 | [diff] [blame] | 501 | #elif defined(__i386__) |
| 502 | InstructionSet instruction_set = kX86; |
| 503 | #elif defined(__mips__) |
| 504 | InstructionSet instruction_set = kMips; |
| 505 | #else |
| 506 | #error "Unsupported architecture" |
| 507 | #endif |
Elliott Hughes | 67d9200 | 2012-03-26 15:08:51 -0700 | [diff] [blame] | 508 | bool dump_stats = kIsDebugBuild; |
| 509 | bool dump_timings = kIsDebugBuild; |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 510 | |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 511 | for (int i = 0; i < argc; i++) { |
| 512 | const StringPiece option(argv[i]); |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 513 | bool log_options = false; |
| 514 | if (log_options) { |
Brian Carlstrom | b7bbba4 | 2011-10-13 14:58:47 -0700 | [diff] [blame] | 515 | LOG(INFO) << "dex2oat: option[" << i << "]=" << argv[i]; |
| 516 | } |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 517 | if (option.starts_with("--dex-file=")) { |
| 518 | dex_filenames.push_back(option.substr(strlen("--dex-file=")).data()); |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 519 | } else if (option.starts_with("--dex-location=")) { |
| 520 | dex_locations.push_back(option.substr(strlen("--dex-location=")).data()); |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 521 | } else if (option.starts_with("--zip-fd=")) { |
| 522 | const char* zip_fd_str = option.substr(strlen("--zip-fd=")).data(); |
Elliott Hughes | bb551fa | 2012-01-25 16:35:29 -0800 | [diff] [blame] | 523 | if (!ParseInt(zip_fd_str, &zip_fd)) { |
Brian Carlstrom | cbfe6fe | 2012-02-17 11:13:36 -0800 | [diff] [blame] | 524 | Usage("could not parse --zip-fd argument '%s' as an integer", zip_fd_str); |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 525 | } |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 526 | } else if (option.starts_with("--zip-location=")) { |
| 527 | zip_location = option.substr(strlen("--zip-location=")).data(); |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 528 | } else if (option.starts_with("--oat-file=")) { |
| 529 | oat_filename = option.substr(strlen("--oat-file=")).data(); |
| 530 | } else if (option.starts_with("--oat-fd=")) { |
| 531 | const char* oat_fd_str = option.substr(strlen("--oat-fd=")).data(); |
Elliott Hughes | bb551fa | 2012-01-25 16:35:29 -0800 | [diff] [blame] | 532 | if (!ParseInt(oat_fd_str, &oat_fd)) { |
Brian Carlstrom | cbfe6fe | 2012-02-17 11:13:36 -0800 | [diff] [blame] | 533 | Usage("could not parse --oat-fd argument '%s' as an integer", oat_fd_str); |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 534 | } |
Elliott Hughes | de6e4cf | 2012-02-27 14:46:06 -0800 | [diff] [blame] | 535 | } else if (option.starts_with("-g")) { |
| 536 | support_debugging = true; |
Elliott Hughes | 5523ee0 | 2012-02-03 18:18:34 -0800 | [diff] [blame] | 537 | } else if (option.starts_with("-j")) { |
Brian Carlstrom | b12552a | 2012-02-04 17:17:31 -0800 | [diff] [blame] | 538 | const char* thread_count_str = option.substr(strlen("-j")).data(); |
Elliott Hughes | 5523ee0 | 2012-02-03 18:18:34 -0800 | [diff] [blame] | 539 | if (!ParseInt(thread_count_str, &thread_count)) { |
Brian Carlstrom | cbfe6fe | 2012-02-17 11:13:36 -0800 | [diff] [blame] | 540 | Usage("could not parse -j argument '%s' as an integer", thread_count_str); |
Elliott Hughes | 5523ee0 | 2012-02-03 18:18:34 -0800 | [diff] [blame] | 541 | } |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 542 | } else if (option.starts_with("--oat-location=")) { |
| 543 | oat_location = option.substr(strlen("--oat-location=")).data(); |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 544 | #if defined(ART_USE_LLVM_COMPILER) |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 545 | } else if (option.starts_with("--bitcode=")) { |
| 546 | bitcode_filename = option.substr(strlen("--bitcode=")).data(); |
| 547 | #endif |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 548 | } else if (option.starts_with("--image=")) { |
| 549 | image_filename = option.substr(strlen("--image=")).data(); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 550 | } else if (option.starts_with("--image-classes=")) { |
| 551 | image_classes_filename = option.substr(strlen("--image-classes=")).data(); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 552 | } else if (option.starts_with("--base=")) { |
| 553 | const char* image_base_str = option.substr(strlen("--base=")).data(); |
| 554 | char* end; |
| 555 | image_base = strtoul(image_base_str, &end, 16); |
| 556 | if (end == image_base_str || *end != '\0') { |
Brian Carlstrom | cbfe6fe | 2012-02-17 11:13:36 -0800 | [diff] [blame] | 557 | Usage("Failed to parse hexadecimal value for option %s", option.data()); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 558 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 559 | } else if (option.starts_with("--boot-image=")) { |
Brian Carlstrom | b001126 | 2011-12-09 12:17:24 -0800 | [diff] [blame] | 560 | boot_image_filename = option.substr(strlen("--boot-image=")).data(); |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 561 | } else if (option.starts_with("--host-prefix=")) { |
Brian Carlstrom | 34f1fa4 | 2012-04-05 18:28:12 -0700 | [diff] [blame] | 562 | host_prefix.reset(new std::string(option.substr(strlen("--host-prefix=")).data())); |
Ian Rogers | 49c4894 | 2012-03-11 15:15:37 -0700 | [diff] [blame] | 563 | } else if (option.starts_with("--instruction-set=")) { |
| 564 | StringPiece instruction_set_str = option.substr(strlen("--instruction-set=")).data(); |
jeffhao | 1f71ae8 | 2012-05-24 16:08:24 -0700 | [diff] [blame] | 565 | if (instruction_set_str == "arm") { |
Ian Rogers | 49c4894 | 2012-03-11 15:15:37 -0700 | [diff] [blame] | 566 | instruction_set = kThumb2; |
jeffhao | 1f71ae8 | 2012-05-24 16:08:24 -0700 | [diff] [blame] | 567 | } else if (instruction_set_str == "mips") { |
Ian Rogers | 49c4894 | 2012-03-11 15:15:37 -0700 | [diff] [blame] | 568 | instruction_set = kMips; |
jeffhao | 1f71ae8 | 2012-05-24 16:08:24 -0700 | [diff] [blame] | 569 | } else if (instruction_set_str == "x86") { |
Ian Rogers | 49c4894 | 2012-03-11 15:15:37 -0700 | [diff] [blame] | 570 | instruction_set = kX86; |
| 571 | } |
jeffhao | 5d84040 | 2011-10-24 17:09:45 -0700 | [diff] [blame] | 572 | } else if (option == "--runtime-arg") { |
| 573 | if (++i >= argc) { |
Brian Carlstrom | cbfe6fe | 2012-02-17 11:13:36 -0800 | [diff] [blame] | 574 | Usage("Missing required argument for --runtime-arg"); |
jeffhao | 5d84040 | 2011-10-24 17:09:45 -0700 | [diff] [blame] | 575 | } |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 576 | if (log_options) { |
| 577 | LOG(INFO) << "dex2oat: option[" << i << "]=" << argv[i]; |
| 578 | } |
jeffhao | 5d84040 | 2011-10-24 17:09:45 -0700 | [diff] [blame] | 579 | runtime_args.push_back(argv[i]); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 580 | } else { |
Brian Carlstrom | cbfe6fe | 2012-02-17 11:13:36 -0800 | [diff] [blame] | 581 | Usage("unknown argument %s", option.data()); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 582 | } |
| 583 | } |
| 584 | |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 585 | if (oat_filename.empty() && oat_fd == -1) { |
Brian Carlstrom | cbfe6fe | 2012-02-17 11:13:36 -0800 | [diff] [blame] | 586 | Usage("Output must be supplied with either --oat-file or --oat-fd"); |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | if (!oat_filename.empty() && oat_fd != -1) { |
Brian Carlstrom | cbfe6fe | 2012-02-17 11:13:36 -0800 | [diff] [blame] | 590 | Usage("--oat-file should not be used with --oat-fd"); |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | if (!oat_filename.empty() && oat_fd != -1) { |
Brian Carlstrom | cbfe6fe | 2012-02-17 11:13:36 -0800 | [diff] [blame] | 594 | Usage("--oat-file should not be used with --oat-fd"); |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 595 | } |
| 596 | |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 597 | if (oat_fd != -1 && !image_filename.empty()) { |
Brian Carlstrom | cbfe6fe | 2012-02-17 11:13:36 -0800 | [diff] [blame] | 598 | Usage("--oat-fd should not be used with --image"); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 599 | } |
| 600 | |
Brian Carlstrom | 34f1fa4 | 2012-04-05 18:28:12 -0700 | [diff] [blame] | 601 | if (host_prefix.get() == NULL) { |
Brian Carlstrom | b001126 | 2011-12-09 12:17:24 -0800 | [diff] [blame] | 602 | const char* android_product_out = getenv("ANDROID_PRODUCT_OUT"); |
| 603 | if (android_product_out != NULL) { |
Brian Carlstrom | 34f1fa4 | 2012-04-05 18:28:12 -0700 | [diff] [blame] | 604 | host_prefix.reset(new std::string(android_product_out)); |
Brian Carlstrom | b001126 | 2011-12-09 12:17:24 -0800 | [diff] [blame] | 605 | } |
| 606 | } |
| 607 | |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 608 | bool image = (!image_filename.empty()); |
Brian Carlstrom | b001126 | 2011-12-09 12:17:24 -0800 | [diff] [blame] | 609 | if (!image && boot_image_filename.empty()) { |
Brian Carlstrom | 34f1fa4 | 2012-04-05 18:28:12 -0700 | [diff] [blame] | 610 | if (host_prefix.get() == NULL) { |
Brian Carlstrom | a56fcd6 | 2012-02-04 21:23:01 -0800 | [diff] [blame] | 611 | boot_image_filename += GetAndroidRoot(); |
| 612 | } else { |
Brian Carlstrom | 34f1fa4 | 2012-04-05 18:28:12 -0700 | [diff] [blame] | 613 | boot_image_filename += *host_prefix.get(); |
Brian Carlstrom | a56fcd6 | 2012-02-04 21:23:01 -0800 | [diff] [blame] | 614 | boot_image_filename += "/system"; |
| 615 | } |
| 616 | boot_image_filename += "/framework/boot.art"; |
Brian Carlstrom | b001126 | 2011-12-09 12:17:24 -0800 | [diff] [blame] | 617 | } |
| 618 | std::string boot_image_option; |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 619 | if (!boot_image_filename.empty()) { |
Brian Carlstrom | b001126 | 2011-12-09 12:17:24 -0800 | [diff] [blame] | 620 | boot_image_option += "-Ximage:"; |
| 621 | boot_image_option += boot_image_filename; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 622 | } |
| 623 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 624 | if (image_classes_filename != NULL && !image) { |
Brian Carlstrom | cbfe6fe | 2012-02-17 11:13:36 -0800 | [diff] [blame] | 625 | Usage("--image-classes should only be used with --image"); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 626 | } |
| 627 | |
| 628 | if (image_classes_filename != NULL && !boot_image_option.empty()) { |
Brian Carlstrom | cbfe6fe | 2012-02-17 11:13:36 -0800 | [diff] [blame] | 629 | Usage("--image-classes should not be used with --boot-image"); |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 630 | } |
| 631 | |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 632 | if (dex_filenames.empty() && zip_fd == -1) { |
Brian Carlstrom | cbfe6fe | 2012-02-17 11:13:36 -0800 | [diff] [blame] | 633 | Usage("Input must be supplied with either --dex-file or --zip-fd"); |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 634 | } |
| 635 | |
| 636 | if (!dex_filenames.empty() && zip_fd != -1) { |
Brian Carlstrom | cbfe6fe | 2012-02-17 11:13:36 -0800 | [diff] [blame] | 637 | Usage("--dex-file should not be used with --zip-fd"); |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 638 | } |
| 639 | |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 640 | if (!dex_filenames.empty() && !zip_location.empty()) { |
Brian Carlstrom | cbfe6fe | 2012-02-17 11:13:36 -0800 | [diff] [blame] | 641 | Usage("--dex-file should not be used with --zip-location"); |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 642 | } |
| 643 | |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 644 | if (dex_locations.empty()) { |
| 645 | for (size_t i = 0; i < dex_filenames.size(); i++) { |
| 646 | dex_locations.push_back(dex_filenames[i]); |
| 647 | } |
| 648 | } else if (dex_locations.size() != dex_filenames.size()) { |
Brian Carlstrom | cbfe6fe | 2012-02-17 11:13:36 -0800 | [diff] [blame] | 649 | Usage("--dex-location arguments do not match --dex-file arguments"); |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | if (zip_fd != -1 && zip_location.empty()) { |
Brian Carlstrom | cbfe6fe | 2012-02-17 11:13:36 -0800 | [diff] [blame] | 653 | Usage("--zip-location should be supplied with --zip-fd"); |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 654 | } |
| 655 | |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 656 | if (boot_image_option.empty()) { |
| 657 | if (image_base == 0) { |
Brian Carlstrom | cbfe6fe | 2012-02-17 11:13:36 -0800 | [diff] [blame] | 658 | Usage("non-zero --base not specified"); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 659 | } |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 660 | } |
| 661 | |
Brian Carlstrom | 6ef827a | 2011-12-11 14:57:47 -0800 | [diff] [blame] | 662 | // Check early that the result of compilation can be written |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 663 | UniquePtr<File> oat_file; |
Brian Carlstrom | 6cd40e5 | 2012-05-03 14:15:11 -0700 | [diff] [blame] | 664 | bool create_file = !oat_filename.empty(); // as opposed to using open file descriptor |
| 665 | if (create_file) { |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 666 | oat_file.reset(OS::OpenFile(oat_filename.c_str(), true)); |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 667 | if (oat_location.empty()) { |
| 668 | oat_location = oat_filename; |
| 669 | } |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 670 | } else { |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 671 | oat_file.reset(OS::FileFromFd(oat_location.c_str(), oat_fd)); |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 672 | } |
Brian Carlstrom | 6ef827a | 2011-12-11 14:57:47 -0800 | [diff] [blame] | 673 | if (oat_file.get() == NULL) { |
Brian Carlstrom | 6cd40e5 | 2012-05-03 14:15:11 -0700 | [diff] [blame] | 674 | PLOG(ERROR) << "Failed to create oat file: " << oat_location; |
| 675 | return EXIT_FAILURE; |
| 676 | } |
| 677 | if (create_file && fchmod(oat_file->Fd(), 0644) != 0) { |
| 678 | PLOG(ERROR) << "Failed to make oat file world readable: " << oat_location; |
Brian Carlstrom | 6ef827a | 2011-12-11 14:57:47 -0800 | [diff] [blame] | 679 | return EXIT_FAILURE; |
Ian Rogers | 5e863dd | 2011-11-02 20:00:10 -0700 | [diff] [blame] | 680 | } |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 681 | |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 682 | LOG(INFO) << "dex2oat: " << oat_location; |
Ian Rogers | 5e863dd | 2011-11-02 20:00:10 -0700 | [diff] [blame] | 683 | |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 684 | Runtime::Options options; |
Brian Carlstrom | 5de8fe5 | 2011-10-16 14:10:09 -0700 | [diff] [blame] | 685 | options.push_back(std::make_pair("compiler", reinterpret_cast<void*>(NULL))); |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 686 | std::vector<const DexFile*> boot_class_path; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 687 | if (boot_image_option.empty()) { |
Elliott Hughes | bf1b457 | 2012-05-24 19:11:39 -0700 | [diff] [blame] | 688 | size_t failure_count = OpenDexFiles(dex_filenames, dex_locations, boot_class_path); |
| 689 | if (failure_count > 0) { |
| 690 | LOG(ERROR) << "Failed to open some dex files: " << failure_count; |
| 691 | return EXIT_FAILURE; |
| 692 | } |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 693 | options.push_back(std::make_pair("bootclasspath", &boot_class_path)); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 694 | } else { |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 695 | options.push_back(std::make_pair(boot_image_option.c_str(), reinterpret_cast<void*>(NULL))); |
| 696 | } |
Brian Carlstrom | 34f1fa4 | 2012-04-05 18:28:12 -0700 | [diff] [blame] | 697 | if (host_prefix.get() != NULL) { |
| 698 | options.push_back(std::make_pair("host-prefix", host_prefix->c_str())); |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 699 | } |
jeffhao | 5d84040 | 2011-10-24 17:09:45 -0700 | [diff] [blame] | 700 | for (size_t i = 0; i < runtime_args.size(); i++) { |
| 701 | options.push_back(std::make_pair(runtime_args[i], reinterpret_cast<void*>(NULL))); |
| 702 | } |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 703 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 704 | Dex2Oat* p_dex2oat; |
| 705 | if (!Dex2Oat::Create(&p_dex2oat, options, instruction_set, thread_count, support_debugging)) { |
| 706 | LOG(ERROR) << "Failed to create dex2oat"; |
| 707 | return EXIT_FAILURE; |
| 708 | } |
| 709 | UniquePtr<Dex2Oat> dex2oat(p_dex2oat); |
| 710 | // Runtime::Create acquired the mutator_lock_ that is normally given away when we Runtime::Start, |
| 711 | // give it away now and then switch to a more managable ScopedObjectAccess. |
| 712 | Thread::Current()->TransitionFromRunnableToSuspended(kNative); |
| 713 | // Whilst we're in native take the opportunity to initialize well known classes. |
| 714 | WellKnownClasses::InitClasses(Thread::Current()->GetJniEnv()); |
| 715 | ScopedObjectAccess soa(Thread::Current()); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 716 | |
Elliott Hughes | bb551fa | 2012-01-25 16:35:29 -0800 | [diff] [blame] | 717 | // If --image-classes was specified, calculate the full list of classes to include in the image |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 718 | UniquePtr<const std::set<std::string> > image_classes(NULL); |
| 719 | if (image_classes_filename != NULL) { |
| 720 | image_classes.reset(dex2oat->GetImageClassDescriptors(image_classes_filename)); |
| 721 | if (image_classes.get() == NULL) { |
| 722 | LOG(ERROR) << "Failed to create list of image classes from " << image_classes_filename; |
| 723 | return EXIT_FAILURE; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 724 | } |
| 725 | } |
| 726 | |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 727 | std::vector<const DexFile*> dex_files; |
| 728 | if (boot_image_option.empty()) { |
| 729 | dex_files = Runtime::Current()->GetClassLinker()->GetBootClassPath(); |
| 730 | } else { |
| 731 | if (dex_filenames.empty()) { |
| 732 | UniquePtr<ZipArchive> zip_archive(ZipArchive::OpenFromFd(zip_fd)); |
| 733 | if (zip_archive.get() == NULL) { |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 734 | LOG(ERROR) << "Failed to zip from file descriptor for " << zip_location; |
Brian Carlstrom | 2e3d1b2 | 2012-01-09 18:01:56 -0800 | [diff] [blame] | 735 | return EXIT_FAILURE; |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 736 | } |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 737 | const DexFile* dex_file = DexFile::Open(*zip_archive.get(), zip_location); |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 738 | if (dex_file == NULL) { |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 739 | LOG(ERROR) << "Failed to open dex from file descriptor for zip file: " << zip_location; |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 740 | return EXIT_FAILURE; |
| 741 | } |
| 742 | dex_files.push_back(dex_file); |
| 743 | } else { |
Elliott Hughes | bf1b457 | 2012-05-24 19:11:39 -0700 | [diff] [blame] | 744 | size_t failure_count = OpenDexFiles(dex_filenames, dex_locations, dex_files); |
| 745 | if (failure_count > 0) { |
| 746 | LOG(ERROR) << "Failed to open some dex files: " << failure_count; |
| 747 | return EXIT_FAILURE; |
| 748 | } |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 749 | } |
| 750 | } |
| 751 | |
Brian Carlstrom | f582258 | 2012-03-19 22:34:31 -0700 | [diff] [blame] | 752 | UniquePtr<const Compiler> compiler(dex2oat->CreateOatFile(boot_image_option, |
Brian Carlstrom | 34f1fa4 | 2012-04-05 18:28:12 -0700 | [diff] [blame] | 753 | host_prefix.get(), |
Brian Carlstrom | f582258 | 2012-03-19 22:34:31 -0700 | [diff] [blame] | 754 | dex_files, |
| 755 | oat_file.get(), |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 756 | #if defined(ART_USE_LLVM_COMPILER) |
Brian Carlstrom | f582258 | 2012-03-19 22:34:31 -0700 | [diff] [blame] | 757 | bitcode_filename, |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 758 | #endif |
Brian Carlstrom | f582258 | 2012-03-19 22:34:31 -0700 | [diff] [blame] | 759 | image, |
Brian Carlstrom | ba0668e | 2012-03-26 13:14:07 -0700 | [diff] [blame] | 760 | image_classes.get(), |
| 761 | dump_stats, |
| 762 | dump_timings)); |
Brian Carlstrom | f582258 | 2012-03-19 22:34:31 -0700 | [diff] [blame] | 763 | |
| 764 | if (compiler.get() == NULL) { |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 765 | LOG(ERROR) << "Failed to create oat file: " << oat_location; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 766 | return EXIT_FAILURE; |
| 767 | } |
| 768 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 769 | if (!image) { |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 770 | LOG(INFO) << "Oat file written successfully: " << oat_location; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 771 | return EXIT_SUCCESS; |
| 772 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 773 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 774 | Thread::Current()->TransitionFromRunnableToSuspended(kNative); |
| 775 | bool image_creation_success = dex2oat->CreateImageFile(image_filename, |
| 776 | image_base, |
| 777 | image_classes.get(), |
| 778 | oat_filename, |
| 779 | oat_location, |
| 780 | *compiler.get()); |
| 781 | Thread::Current()->TransitionFromSuspendedToRunnable(); |
| 782 | if (!image_creation_success) { |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 783 | return EXIT_FAILURE; |
| 784 | } |
| 785 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 786 | // We wrote the oat file successfully, and want to keep it. |
Elliott Hughes | bb551fa | 2012-01-25 16:35:29 -0800 | [diff] [blame] | 787 | LOG(INFO) << "Oat file written successfully: " << oat_filename; |
| 788 | LOG(INFO) << "Image written successfully: " << image_filename; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 789 | return EXIT_SUCCESS; |
| 790 | } |
| 791 | |
| 792 | } // namespace art |
| 793 | |
| 794 | int main(int argc, char** argv) { |
| 795 | return art::dex2oat(argc, argv); |
| 796 | } |