Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
| 2 | |
| 3 | #include <stdio.h> |
| 4 | #include <stdlib.h> |
| 5 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 6 | #include <iostream> |
| 7 | #include <fstream> |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 8 | #include <string> |
| 9 | #include <vector> |
| 10 | |
| 11 | #include "class_linker.h" |
| 12 | #include "class_loader.h" |
| 13 | #include "compiler.h" |
Ian Rogers | 5e863dd | 2011-11-02 20:00:10 -0700 | [diff] [blame] | 14 | #include "file.h" |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 15 | #include "image_writer.h" |
Ian Rogers | 6f1dfe4 | 2011-12-08 17:28:34 -0800 | [diff] [blame] | 16 | #include "leb128.h" |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 17 | #include "oat_writer.h" |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 18 | #include "object_utils.h" |
Ian Rogers | 5e863dd | 2011-11-02 20:00:10 -0700 | [diff] [blame] | 19 | #include "os.h" |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 20 | #include "runtime.h" |
| 21 | #include "stringpiece.h" |
| 22 | |
| 23 | namespace art { |
| 24 | |
| 25 | static void usage() { |
| 26 | fprintf(stderr, |
| 27 | "Usage: dex2oat [options]...\n" |
| 28 | "\n"); |
| 29 | fprintf(stderr, |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 30 | " --dex-file=<dex-file>: specifies a .dex file to compile. At least one .dex\n" |
| 31 | " file must be specified. \n" |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 32 | " Example: --dex-file=/system/framework/core.jar\n" |
| 33 | "\n"); |
| 34 | fprintf(stderr, |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 35 | " --oat=<file.oat>: specifies the required oat filename.\n" |
| 36 | " Example: --oat=/data/art-cache/boot.oat\n" |
| 37 | "\n"); |
| 38 | fprintf(stderr, |
| 39 | " --image=<file.art>: specifies the output image filename.\n" |
Brian Carlstrom | 47a0d5a | 2011-10-12 21:20:05 -0700 | [diff] [blame] | 40 | " Example: --image=/data/art-cache/boot.art\n" |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 41 | "\n"); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 42 | fprintf(stderr, |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 43 | " --image-classes=<classname-file>: specifies classes to include in an image.\n" |
| 44 | " Example: --image=frameworks/base/preloaded-classes\n" |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 45 | "\n"); |
| 46 | fprintf(stderr, |
| 47 | " --base=<hex-address>: specifies the base address when creating a boot image.\n" |
| 48 | " Example: --base=0x50000000\n" |
| 49 | "\n"); |
| 50 | fprintf(stderr, |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 51 | " --boot-image=<file.art>: provide the image file for the boot class path.\n" |
Brian Carlstrom | 47a0d5a | 2011-10-12 21:20:05 -0700 | [diff] [blame] | 52 | " Example: --boot-image=/data/art-cache/boot.art\n" |
Brian Carlstrom | b001126 | 2011-12-09 12:17:24 -0800 | [diff] [blame] | 53 | " Default: <host-prefix>/data/art-cache/boot.art\n" |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 54 | "\n"); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 55 | fprintf(stderr, |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 56 | " --host-prefix may be used to translate host paths to target paths during\n" |
| 57 | " cross compilation.\n" |
| 58 | " Example: --host-prefix=out/target/product/crespo\n" |
Brian Carlstrom | b001126 | 2011-12-09 12:17:24 -0800 | [diff] [blame] | 59 | " Default: $ANDROID_PRODUCT_OUT\n" |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 60 | "\n"); |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 61 | fprintf(stderr, |
jeffhao | 5d84040 | 2011-10-24 17:09:45 -0700 | [diff] [blame] | 62 | " --runtime-arg <argument>: used to specify various arguments for the runtime,\n" |
| 63 | " such as initial heap size, maximum heap size, and verbose output.\n" |
| 64 | " Use a separate --runtime-arg switch for each argument.\n" |
| 65 | " Example: --runtime-arg -Xms256m\n" |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 66 | "\n"); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 67 | exit(EXIT_FAILURE); |
| 68 | } |
| 69 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 70 | class Dex2Oat { |
| 71 | public: |
| 72 | |
| 73 | static Dex2Oat* Create(Runtime::Options& options) { |
| 74 | UniquePtr<Runtime> runtime(CreateRuntime(options)); |
| 75 | if (runtime.get() == NULL) { |
| 76 | return NULL; |
| 77 | } |
| 78 | return new Dex2Oat(runtime.release()); |
| 79 | } |
| 80 | |
| 81 | ~Dex2Oat() { |
| 82 | delete runtime_; |
| 83 | } |
| 84 | |
| 85 | // Make a list of descriptors for classes to include in the image |
| 86 | const std::set<std::string>* GetImageClassDescriptors(const char* image_classes_filename) { |
| 87 | UniquePtr<std::ifstream> image_classes_file(new std::ifstream(image_classes_filename, std::ifstream::in)); |
| 88 | if (image_classes_file.get() == NULL) { |
| 89 | LOG(ERROR) << "Failed to open image classes file " << image_classes_filename; |
| 90 | return NULL; |
| 91 | } |
| 92 | |
Ian Rogers | 6f1dfe4 | 2011-12-08 17:28:34 -0800 | [diff] [blame] | 93 | // Load all the classes specified in the file |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 94 | ClassLinker* class_linker = runtime_->GetClassLinker(); |
| 95 | while (image_classes_file->good()) { |
| 96 | std::string dot; |
| 97 | std::getline(*image_classes_file.get(), dot); |
| 98 | if (StringPiece(dot).starts_with("#") || dot.empty()) { |
| 99 | continue; |
| 100 | } |
Elliott Hughes | 9557241 | 2011-12-13 18:14:20 -0800 | [diff] [blame^] | 101 | std::string descriptor(DotToDescriptor(dot.c_str())); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 102 | SirtRef<Class> klass(class_linker->FindSystemClass(descriptor)); |
| 103 | if (klass.get() == NULL) { |
| 104 | LOG(WARNING) << "Failed to find class " << descriptor; |
| 105 | Thread::Current()->ClearException(); |
| 106 | } |
| 107 | } |
| 108 | image_classes_file->close(); |
| 109 | |
Ian Rogers | 6f1dfe4 | 2011-12-08 17:28:34 -0800 | [diff] [blame] | 110 | // Resolve exception classes referenced by the loaded classes. The catch logic assumes |
| 111 | // exceptions are resolved by the verifier when there is a catch block in an interested method. |
| 112 | // Do this here so that exception classes appear to have been specified image classes. |
| 113 | std::set<std::pair<uint16_t, const DexFile*> > unresolved_exception_types; |
| 114 | do { |
| 115 | unresolved_exception_types.clear(); |
| 116 | class_linker->VisitClasses(ResolveCatchBlockExceptionsClassVisitor, |
| 117 | &unresolved_exception_types); |
| 118 | typedef std::set<std::pair<uint16_t, const DexFile*> >::const_iterator It; // TODO: C++0x auto |
| 119 | for (It it = unresolved_exception_types.begin(), |
| 120 | end = unresolved_exception_types.end(); |
| 121 | it != end; ++it) { |
| 122 | uint16_t exception_type_idx = it->first; |
| 123 | const DexFile* dex_file = it->second; |
| 124 | DexCache* dex_cache = class_linker->FindDexCache(*dex_file); |
| 125 | ClassLoader* class_loader = NULL; |
| 126 | SirtRef<Class> klass(class_linker->ResolveType(*dex_file, exception_type_idx, dex_cache, |
| 127 | class_loader)); |
| 128 | if (klass.get() == NULL) { |
| 129 | const DexFile::TypeId& type_id = dex_file->GetTypeId(exception_type_idx); |
| 130 | const char* descriptor = dex_file->GetTypeDescriptor(type_id); |
| 131 | LOG(FATAL) << "Failed to resolve class " << descriptor; |
| 132 | } |
| 133 | DCHECK(klass->IsThrowableClass()); |
| 134 | } |
| 135 | // Resolving exceptions may load classes that reference more exceptions, iterate until no |
| 136 | // more are found |
| 137 | } while (!unresolved_exception_types.empty()); |
| 138 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 139 | // We walk the roots looking for classes so that we'll pick up the |
| 140 | // above classes plus any classes them depend on such super |
| 141 | // classes, interfaces, and the required ClassLinker roots. |
| 142 | UniquePtr<std::set<std::string> > image_classes(new std::set<std::string>()); |
Ian Rogers | 6f1dfe4 | 2011-12-08 17:28:34 -0800 | [diff] [blame] | 143 | class_linker->VisitClasses(RecordImageClassesVisitor, image_classes.get()); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 144 | CHECK_NE(image_classes->size(), 0U); |
| 145 | return image_classes.release(); |
| 146 | } |
| 147 | |
| 148 | bool CreateOatFile(const std::string& boot_image_option, |
| 149 | const std::vector<const char*>& dex_filenames, |
| 150 | const std::string& host_prefix, |
| 151 | File* oat_file, |
| 152 | bool image, |
| 153 | const std::set<std::string>* image_classes) { |
| 154 | // SirtRef and ClassLoader creation needs to come after Runtime::Create |
| 155 | UniquePtr<SirtRef<ClassLoader> > class_loader(new SirtRef<ClassLoader>(NULL)); |
| 156 | if (class_loader.get() == NULL) { |
| 157 | LOG(ERROR) << "Failed to create SirtRef for class loader"; |
| 158 | return false; |
| 159 | } |
| 160 | |
| 161 | std::vector<const DexFile*> dex_files; |
| 162 | if (!boot_image_option.empty()) { |
| 163 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 164 | DexFile::OpenDexFiles(dex_filenames, dex_files, host_prefix); |
| 165 | std::vector<const DexFile*> class_path_files(dex_files); |
| 166 | OpenClassPathFiles(runtime_->GetClassPath(), class_path_files); |
| 167 | for (size_t i = 0; i < class_path_files.size(); i++) { |
| 168 | class_linker->RegisterDexFile(*class_path_files[i]); |
| 169 | } |
| 170 | class_loader.get()->reset(PathClassLoader::AllocCompileTime(class_path_files)); |
| 171 | } else { |
| 172 | dex_files = runtime_->GetClassLinker()->GetBootClassPath(); |
| 173 | } |
| 174 | |
| 175 | Compiler compiler(instruction_set_, image, image_classes); |
| 176 | compiler.CompileAll(class_loader->get(), dex_files); |
| 177 | |
| 178 | if (!OatWriter::Create(oat_file, class_loader->get(), compiler)) { |
| 179 | LOG(ERROR) << "Failed to create oat file " << oat_file->name(); |
| 180 | return false; |
| 181 | } |
| 182 | return true; |
| 183 | } |
| 184 | |
| 185 | bool CreateImageFile(const char* image_filename, |
| 186 | uintptr_t image_base, |
| 187 | const std::set<std::string>* image_classes, |
| 188 | const std::string& oat_filename, |
| 189 | const std::string& host_prefix) { |
| 190 | // If we have an existing boot image, position new space after its oat file |
| 191 | if (Heap::GetSpaces().size() > 1) { |
| 192 | Space* last_image_space = Heap::GetSpaces()[Heap::GetSpaces().size()-2]; |
| 193 | CHECK(last_image_space != NULL); |
| 194 | CHECK(last_image_space->IsImageSpace()); |
| 195 | CHECK(!Heap::GetSpaces()[Heap::GetSpaces().size()-1]->IsImageSpace()); |
| 196 | byte* oat_limit_addr = last_image_space->GetImageHeader().GetOatLimitAddr(); |
| 197 | image_base = RoundUp(reinterpret_cast<uintptr_t>(oat_limit_addr), kPageSize); |
| 198 | } |
| 199 | |
| 200 | ImageWriter image_writer(image_classes); |
| 201 | if (!image_writer.Write(image_filename, image_base, oat_filename, host_prefix)) { |
| 202 | LOG(ERROR) << "Failed to create image file " << image_filename; |
| 203 | return false; |
| 204 | } |
| 205 | return true; |
| 206 | } |
| 207 | |
| 208 | private: |
| 209 | |
| 210 | Dex2Oat(Runtime* runtime) : runtime_(runtime) {} |
| 211 | |
| 212 | static Runtime* CreateRuntime(Runtime::Options& options) { |
| 213 | Runtime* runtime = Runtime::Create(options, false); |
| 214 | if (runtime == NULL) { |
| 215 | LOG(ERROR) << "Failed to create runtime"; |
| 216 | return NULL; |
| 217 | } |
| 218 | |
| 219 | // if we loaded an existing image, we will reuse values from the image roots. |
| 220 | if (!runtime->HasJniDlsymLookupStub()) { |
| 221 | runtime->SetJniDlsymLookupStub(Compiler::CreateJniDlysmLookupStub(instruction_set_)); |
| 222 | } |
| 223 | if (!runtime->HasAbstractMethodErrorStubArray()) { |
| 224 | runtime->SetAbstractMethodErrorStubArray(Compiler::CreateAbstractMethodErrorStub(instruction_set_)); |
| 225 | } |
| 226 | for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) { |
| 227 | Runtime::TrampolineType type = Runtime::TrampolineType(i); |
| 228 | if (!runtime->HasResolutionStubArray(type)) { |
| 229 | runtime->SetResolutionStubArray(Compiler::CreateResolutionStub(instruction_set_, type), type); |
| 230 | } |
| 231 | } |
| 232 | for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) { |
| 233 | Runtime::CalleeSaveType type = Runtime::CalleeSaveType(i); |
| 234 | if (!runtime->HasCalleeSaveMethod(type)) { |
| 235 | runtime->SetCalleeSaveMethod(runtime->CreateCalleeSaveMethod(instruction_set_, type), type); |
| 236 | } |
| 237 | } |
| 238 | return runtime; |
| 239 | } |
| 240 | |
Ian Rogers | 6f1dfe4 | 2011-12-08 17:28:34 -0800 | [diff] [blame] | 241 | static void ResolveExceptionsForMethod(MethodHelper* mh, |
| 242 | std::set<std::pair<uint16_t, const DexFile*> >& exceptions_to_resolve) { |
| 243 | const DexFile::CodeItem* code_item = mh->GetCodeItem(); |
| 244 | if (code_item == NULL) { |
| 245 | return; // native or abstract method |
| 246 | } |
| 247 | if (code_item->tries_size_ == 0) { |
| 248 | return; // nothing to process |
| 249 | } |
| 250 | const byte* encoded_catch_handler_list = DexFile::GetCatchHandlerData(*code_item, 0); |
| 251 | size_t num_encoded_catch_handlers = DecodeUnsignedLeb128(&encoded_catch_handler_list); |
| 252 | for (size_t i = 0; i < num_encoded_catch_handlers; i++) { |
| 253 | int32_t encoded_catch_handler_size = DecodeSignedLeb128(&encoded_catch_handler_list); |
| 254 | bool has_catch_all = false; |
| 255 | if (encoded_catch_handler_size <= 0) { |
| 256 | encoded_catch_handler_size = -encoded_catch_handler_size; |
| 257 | has_catch_all = true; |
| 258 | } |
| 259 | for (int32_t j = 0; j < encoded_catch_handler_size; j++) { |
| 260 | uint16_t encoded_catch_handler_handlers_type_idx = |
| 261 | DecodeUnsignedLeb128(&encoded_catch_handler_list); |
| 262 | // Add to set of types to resolve if not already in the dex cache resolved types |
| 263 | if (!mh->IsResolvedTypeIdx(encoded_catch_handler_handlers_type_idx)) { |
| 264 | exceptions_to_resolve.insert( |
| 265 | std::pair<uint16_t, const DexFile*>(encoded_catch_handler_handlers_type_idx, |
| 266 | &mh->GetDexFile())); |
| 267 | } |
| 268 | // ignore address associated with catch handler |
| 269 | DecodeUnsignedLeb128(&encoded_catch_handler_list); |
| 270 | } |
| 271 | if (has_catch_all) { |
| 272 | // ignore catch all address |
| 273 | DecodeUnsignedLeb128(&encoded_catch_handler_list); |
| 274 | } |
| 275 | } |
| 276 | } |
| 277 | static bool ResolveCatchBlockExceptionsClassVisitor(Class* c, void* arg) { |
| 278 | std::set<std::pair<uint16_t, const DexFile*> >* exceptions_to_resolve = |
| 279 | reinterpret_cast<std::set<std::pair<uint16_t, const DexFile*> >*>(arg); |
| 280 | MethodHelper mh; |
| 281 | for (size_t i = 0; i < c->NumVirtualMethods(); ++i) { |
| 282 | Method* m = c->GetVirtualMethod(i); |
| 283 | mh.ChangeMethod(m); |
| 284 | ResolveExceptionsForMethod(&mh, *exceptions_to_resolve); |
| 285 | } |
| 286 | for (size_t i = 0; i < c->NumDirectMethods(); ++i) { |
| 287 | Method* m = c->GetDirectMethod(i); |
| 288 | mh.ChangeMethod(m); |
| 289 | ResolveExceptionsForMethod(&mh, *exceptions_to_resolve); |
| 290 | } |
| 291 | return true; |
| 292 | } |
| 293 | static bool RecordImageClassesVisitor(Class* klass, void* arg) { |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 294 | std::set<std::string>* image_classes = reinterpret_cast<std::set<std::string>*>(arg); |
| 295 | if (klass->IsArrayClass() || klass->IsPrimitive()) { |
Jesse Wilson | 254db0f | 2011-11-16 16:44:11 -0500 | [diff] [blame] | 296 | return true; |
| 297 | } |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 298 | image_classes->insert(ClassHelper(klass).GetDescriptor()); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 299 | return true; |
Jesse Wilson | 254db0f | 2011-11-16 16:44:11 -0500 | [diff] [blame] | 300 | } |
Jesse Wilson | 254db0f | 2011-11-16 16:44:11 -0500 | [diff] [blame] | 301 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 302 | // Appends to dex_files any elements of class_path that it doesn't already |
| 303 | // contain. This will open those dex files as necessary. |
| 304 | static void OpenClassPathFiles(const std::string& class_path, std::vector<const DexFile*>& dex_files) { |
| 305 | std::vector<std::string> parsed; |
| 306 | Split(class_path, ':', parsed); |
| 307 | for (size_t i = 0; i < parsed.size(); ++i) { |
| 308 | if (DexFilesContains(dex_files, parsed[i])) { |
| 309 | continue; |
| 310 | } |
| 311 | const DexFile* dex_file = DexFile::Open(parsed[i], Runtime::Current()->GetHostPrefix()); |
| 312 | if (dex_file == NULL) { |
| 313 | LOG(WARNING) << "Failed to open dex file " << parsed[i]; |
| 314 | } else { |
| 315 | dex_files.push_back(dex_file); |
| 316 | } |
Jesse Wilson | 254db0f | 2011-11-16 16:44:11 -0500 | [diff] [blame] | 317 | } |
| 318 | } |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 319 | |
| 320 | // Returns true if dex_files has a dex with the named location. |
| 321 | static bool DexFilesContains(const std::vector<const DexFile*>& dex_files, const std::string& location) { |
| 322 | for (size_t i = 0; i < dex_files.size(); ++i) { |
| 323 | if (dex_files[i]->GetLocation() == location) { |
| 324 | return true; |
| 325 | } |
| 326 | } |
| 327 | return false; |
| 328 | } |
| 329 | |
| 330 | Runtime* runtime_; |
| 331 | static const InstructionSet instruction_set_ = kThumb2; |
| 332 | |
| 333 | DISALLOW_IMPLICIT_CONSTRUCTORS(Dex2Oat); |
| 334 | }; |
Jesse Wilson | 254db0f | 2011-11-16 16:44:11 -0500 | [diff] [blame] | 335 | |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 336 | int dex2oat(int argc, char** argv) { |
| 337 | // Skip over argv[0]. |
| 338 | argv++; |
| 339 | argc--; |
| 340 | |
| 341 | if (argc == 0) { |
| 342 | fprintf(stderr, "no arguments specified\n"); |
| 343 | usage(); |
| 344 | } |
| 345 | |
| 346 | std::vector<const char*> dex_filenames; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 347 | std::string oat_filename; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 348 | const char* image_filename = NULL; |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 349 | const char* image_classes_filename = NULL; |
Brian Carlstrom | b001126 | 2011-12-09 12:17:24 -0800 | [diff] [blame] | 350 | std::string boot_image_filename; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 351 | uintptr_t image_base = 0; |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 352 | std::string host_prefix; |
jeffhao | 5d84040 | 2011-10-24 17:09:45 -0700 | [diff] [blame] | 353 | std::vector<const char*> runtime_args; |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 354 | |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 355 | for (int i = 0; i < argc; i++) { |
| 356 | const StringPiece option(argv[i]); |
Brian Carlstrom | b7bbba4 | 2011-10-13 14:58:47 -0700 | [diff] [blame] | 357 | if (false) { |
| 358 | LOG(INFO) << "dex2oat: option[" << i << "]=" << argv[i]; |
| 359 | } |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 360 | if (option.starts_with("--dex-file=")) { |
| 361 | dex_filenames.push_back(option.substr(strlen("--dex-file=")).data()); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 362 | } else if (option.starts_with("--oat=")) { |
| 363 | oat_filename = option.substr(strlen("--oat=")).data(); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 364 | } else if (option.starts_with("--image=")) { |
| 365 | image_filename = option.substr(strlen("--image=")).data(); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 366 | } else if (option.starts_with("--image-classes=")) { |
| 367 | image_classes_filename = option.substr(strlen("--image-classes=")).data(); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 368 | } else if (option.starts_with("--base=")) { |
| 369 | const char* image_base_str = option.substr(strlen("--base=")).data(); |
| 370 | char* end; |
| 371 | image_base = strtoul(image_base_str, &end, 16); |
| 372 | if (end == image_base_str || *end != '\0') { |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 373 | fprintf(stderr, "Failed to parse hexadecimal value for option %s\n", option.data()); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 374 | usage(); |
| 375 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 376 | } else if (option.starts_with("--boot-image=")) { |
Brian Carlstrom | b001126 | 2011-12-09 12:17:24 -0800 | [diff] [blame] | 377 | boot_image_filename = option.substr(strlen("--boot-image=")).data(); |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 378 | } else if (option.starts_with("--host-prefix=")) { |
| 379 | host_prefix = option.substr(strlen("--host-prefix=")).data(); |
jeffhao | 5d84040 | 2011-10-24 17:09:45 -0700 | [diff] [blame] | 380 | } else if (option == "--runtime-arg") { |
| 381 | if (++i >= argc) { |
| 382 | fprintf(stderr, "Missing required argument for --runtime-arg\n"); |
| 383 | usage(); |
| 384 | } |
| 385 | runtime_args.push_back(argv[i]); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 386 | } else { |
| 387 | fprintf(stderr, "unknown argument %s\n", option.data()); |
| 388 | usage(); |
| 389 | } |
| 390 | } |
| 391 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 392 | if (oat_filename.empty()) { |
Elliott Hughes | 362f9bc | 2011-10-17 18:56:41 -0700 | [diff] [blame] | 393 | fprintf(stderr, "--oat file name not specified\n"); |
| 394 | return EXIT_FAILURE; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 395 | } |
| 396 | |
Brian Carlstrom | b001126 | 2011-12-09 12:17:24 -0800 | [diff] [blame] | 397 | if (host_prefix.empty()) { |
| 398 | const char* android_product_out = getenv("ANDROID_PRODUCT_OUT"); |
| 399 | if (android_product_out != NULL) { |
| 400 | host_prefix = android_product_out; |
| 401 | } |
| 402 | } |
| 403 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 404 | bool image = (image_filename != NULL); |
Brian Carlstrom | b001126 | 2011-12-09 12:17:24 -0800 | [diff] [blame] | 405 | if (!image && boot_image_filename.empty()) { |
| 406 | boot_image_filename += host_prefix; |
| 407 | boot_image_filename += "/data/art-cache/boot.art"; |
| 408 | } |
| 409 | std::string boot_image_option; |
| 410 | if (boot_image_filename != NULL) { |
| 411 | boot_image_option += "-Ximage:"; |
| 412 | boot_image_option += boot_image_filename; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 413 | } |
| 414 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 415 | if (image_classes_filename != NULL && !image) { |
| 416 | fprintf(stderr, "--image-classes should only be used with --image\n"); |
| 417 | return EXIT_FAILURE; |
| 418 | } |
| 419 | |
| 420 | if (image_classes_filename != NULL && !boot_image_option.empty()) { |
| 421 | fprintf(stderr, "--image-classes should not be used with --boot-image\n"); |
Elliott Hughes | 362f9bc | 2011-10-17 18:56:41 -0700 | [diff] [blame] | 422 | return EXIT_FAILURE; |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 423 | } |
| 424 | |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 425 | if (boot_image_option.empty()) { |
| 426 | if (image_base == 0) { |
| 427 | fprintf(stderr, "non-zero --base not specified\n"); |
| 428 | return EXIT_FAILURE; |
| 429 | } |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 430 | } |
| 431 | |
Brian Carlstrom | 6ef827a | 2011-12-11 14:57:47 -0800 | [diff] [blame] | 432 | // Check early that the result of compilation can be written |
| 433 | UniquePtr<File> oat_file(OS::OpenFile(oat_filename.c_str(), true)); |
| 434 | if (oat_file.get() == NULL) { |
| 435 | PLOG(ERROR) << "Unable to create oat file " << oat_filename; |
| 436 | return EXIT_FAILURE; |
Ian Rogers | 5e863dd | 2011-11-02 20:00:10 -0700 | [diff] [blame] | 437 | } |
Brian Carlstrom | 6ef827a | 2011-12-11 14:57:47 -0800 | [diff] [blame] | 438 | LOG(INFO) << "dex2oat: " << oat_filename; |
Ian Rogers | 5e863dd | 2011-11-02 20:00:10 -0700 | [diff] [blame] | 439 | |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 440 | Runtime::Options options; |
Brian Carlstrom | 5de8fe5 | 2011-10-16 14:10:09 -0700 | [diff] [blame] | 441 | options.push_back(std::make_pair("compiler", reinterpret_cast<void*>(NULL))); |
Brian Carlstrom | b7bbba4 | 2011-10-13 14:58:47 -0700 | [diff] [blame] | 442 | std::string boot_class_path_string; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 443 | if (boot_image_option.empty()) { |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 444 | boot_class_path_string += "-Xbootclasspath:"; |
| 445 | for (size_t i = 0; i < dex_filenames.size()-1; i++) { |
| 446 | boot_class_path_string += dex_filenames[i]; |
| 447 | boot_class_path_string += ":"; |
| 448 | } |
| 449 | boot_class_path_string += dex_filenames[dex_filenames.size()-1]; |
| 450 | options.push_back(std::make_pair(boot_class_path_string.c_str(), reinterpret_cast<void*>(NULL))); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 451 | } else { |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 452 | options.push_back(std::make_pair(boot_image_option.c_str(), reinterpret_cast<void*>(NULL))); |
| 453 | } |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 454 | if (!host_prefix.empty()) { |
| 455 | options.push_back(std::make_pair("host-prefix", host_prefix.c_str())); |
| 456 | } |
jeffhao | 5d84040 | 2011-10-24 17:09:45 -0700 | [diff] [blame] | 457 | for (size_t i = 0; i < runtime_args.size(); i++) { |
| 458 | options.push_back(std::make_pair(runtime_args[i], reinterpret_cast<void*>(NULL))); |
| 459 | } |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 460 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 461 | UniquePtr<Dex2Oat> dex2oat(Dex2Oat::Create(options)); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 462 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 463 | // If --image-classes was specified, calculate the full list classes to include in the image |
| 464 | UniquePtr<const std::set<std::string> > image_classes(NULL); |
| 465 | if (image_classes_filename != NULL) { |
| 466 | image_classes.reset(dex2oat->GetImageClassDescriptors(image_classes_filename)); |
| 467 | if (image_classes.get() == NULL) { |
| 468 | LOG(ERROR) << "Failed to create list of image classes from " << image_classes_filename; |
| 469 | return EXIT_FAILURE; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 470 | } |
| 471 | } |
| 472 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 473 | if (!dex2oat->CreateOatFile(boot_image_option, |
| 474 | dex_filenames, |
| 475 | host_prefix, |
| 476 | oat_file.get(), |
| 477 | image, |
| 478 | image_classes.get())) { |
| 479 | LOG(ERROR) << "Failed to create oat file" << oat_filename; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 480 | return EXIT_FAILURE; |
| 481 | } |
| 482 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 483 | if (!image) { |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 484 | LOG(INFO) << "Oat file written successfully " << oat_filename; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 485 | return EXIT_SUCCESS; |
| 486 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 487 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 488 | if (!dex2oat->CreateImageFile(image_filename, |
| 489 | image_base, |
| 490 | image_classes.get(), |
| 491 | oat_filename, |
| 492 | host_prefix)) { |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 493 | return EXIT_FAILURE; |
| 494 | } |
| 495 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 496 | // We wrote the oat file successfully, and want to keep it. |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 497 | LOG(INFO) << "Oat file written successfully " << oat_filename; |
Elliott Hughes | 234da57 | 2011-11-03 22:13:06 -0700 | [diff] [blame] | 498 | LOG(INFO) << "Image written successfully " << image_filename; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 499 | return EXIT_SUCCESS; |
| 500 | } |
| 501 | |
| 502 | } // namespace art |
| 503 | |
| 504 | int main(int argc, char** argv) { |
| 505 | return art::dex2oat(argc, argv); |
| 506 | } |