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