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> |
Elliott Hughes | 234da57 | 2011-11-03 22:13:06 -0700 | [diff] [blame] | 5 | #include <sys/file.h> |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 6 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 7 | #include <iostream> |
| 8 | #include <fstream> |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 9 | #include <string> |
| 10 | #include <vector> |
| 11 | |
| 12 | #include "class_linker.h" |
| 13 | #include "class_loader.h" |
| 14 | #include "compiler.h" |
Ian Rogers | 5e863dd | 2011-11-02 20:00:10 -0700 | [diff] [blame] | 15 | #include "file.h" |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 16 | #include "image_writer.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 | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 53 | "\n"); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 54 | fprintf(stderr, |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 55 | " --host-prefix may be used to translate host paths to target paths during\n" |
| 56 | " cross compilation.\n" |
| 57 | " Example: --host-prefix=out/target/product/crespo\n" |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 58 | "\n"); |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 59 | fprintf(stderr, |
jeffhao | 5d84040 | 2011-10-24 17:09:45 -0700 | [diff] [blame] | 60 | " --runtime-arg <argument>: used to specify various arguments for the runtime,\n" |
| 61 | " such as initial heap size, maximum heap size, and verbose output.\n" |
| 62 | " Use a separate --runtime-arg switch for each argument.\n" |
| 63 | " Example: --runtime-arg -Xms256m\n" |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 64 | "\n"); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 65 | exit(EXIT_FAILURE); |
| 66 | } |
| 67 | |
Elliott Hughes | 234da57 | 2011-11-03 22:13:06 -0700 | [diff] [blame] | 68 | class FileJanitor { |
| 69 | public: |
| 70 | FileJanitor(const std::string& filename, int fd) |
| 71 | : filename_(filename), fd_(fd), do_unlink_(true) { |
| 72 | } |
| 73 | |
| 74 | void KeepFile() { |
| 75 | do_unlink_ = false; |
| 76 | } |
| 77 | |
| 78 | ~FileJanitor() { |
| 79 | if (fd_ != -1) { |
Elliott Hughes | b1f9f22 | 2011-11-04 18:04:00 -0700 | [diff] [blame] | 80 | int rc = TEMP_FAILURE_RETRY(flock(fd_, LOCK_UN)); |
| 81 | if (rc == -1) { |
| 82 | PLOG(ERROR) << "Failed to unlock " << filename_; |
| 83 | } |
Elliott Hughes | 234da57 | 2011-11-03 22:13:06 -0700 | [diff] [blame] | 84 | } |
| 85 | if (do_unlink_) { |
Elliott Hughes | b1f9f22 | 2011-11-04 18:04:00 -0700 | [diff] [blame] | 86 | int rc = TEMP_FAILURE_RETRY(unlink(filename_.c_str())); |
| 87 | if (rc == -1) { |
| 88 | PLOG(ERROR) << "Failed to unlink " << filename_; |
| 89 | } |
Elliott Hughes | 234da57 | 2011-11-03 22:13:06 -0700 | [diff] [blame] | 90 | } |
| 91 | } |
| 92 | |
| 93 | private: |
| 94 | std::string filename_; |
| 95 | int fd_; |
| 96 | bool do_unlink_; |
| 97 | }; |
| 98 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 99 | class Dex2Oat { |
| 100 | public: |
| 101 | |
| 102 | static Dex2Oat* Create(Runtime::Options& options) { |
| 103 | UniquePtr<Runtime> runtime(CreateRuntime(options)); |
| 104 | if (runtime.get() == NULL) { |
| 105 | return NULL; |
| 106 | } |
| 107 | return new Dex2Oat(runtime.release()); |
| 108 | } |
| 109 | |
| 110 | ~Dex2Oat() { |
| 111 | delete runtime_; |
| 112 | } |
| 113 | |
| 114 | // Make a list of descriptors for classes to include in the image |
| 115 | const std::set<std::string>* GetImageClassDescriptors(const char* image_classes_filename) { |
| 116 | UniquePtr<std::ifstream> image_classes_file(new std::ifstream(image_classes_filename, std::ifstream::in)); |
| 117 | if (image_classes_file.get() == NULL) { |
| 118 | LOG(ERROR) << "Failed to open image classes file " << image_classes_filename; |
| 119 | return NULL; |
| 120 | } |
| 121 | |
| 122 | // Load all the classes specifed in the file |
| 123 | ClassLinker* class_linker = runtime_->GetClassLinker(); |
| 124 | while (image_classes_file->good()) { |
| 125 | std::string dot; |
| 126 | std::getline(*image_classes_file.get(), dot); |
| 127 | if (StringPiece(dot).starts_with("#") || dot.empty()) { |
| 128 | continue; |
| 129 | } |
| 130 | std::string descriptor = DotToDescriptor(dot.c_str()); |
| 131 | SirtRef<Class> klass(class_linker->FindSystemClass(descriptor)); |
| 132 | if (klass.get() == NULL) { |
| 133 | LOG(WARNING) << "Failed to find class " << descriptor; |
| 134 | Thread::Current()->ClearException(); |
| 135 | } |
| 136 | } |
| 137 | image_classes_file->close(); |
| 138 | |
| 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>()); |
| 143 | class_linker->VisitClasses(ClassVisitor, image_classes.get()); |
| 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 | |
| 241 | static bool ClassVisitor(Class* klass, void* arg) { |
| 242 | std::set<std::string>* image_classes = reinterpret_cast<std::set<std::string>*>(arg); |
| 243 | if (klass->IsArrayClass() || klass->IsPrimitive()) { |
Jesse Wilson | 254db0f | 2011-11-16 16:44:11 -0500 | [diff] [blame] | 244 | return true; |
| 245 | } |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 246 | image_classes->insert(ClassHelper(klass).GetDescriptor()); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 247 | return true; |
Jesse Wilson | 254db0f | 2011-11-16 16:44:11 -0500 | [diff] [blame] | 248 | } |
Jesse Wilson | 254db0f | 2011-11-16 16:44:11 -0500 | [diff] [blame] | 249 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 250 | // Appends to dex_files any elements of class_path that it doesn't already |
| 251 | // contain. This will open those dex files as necessary. |
| 252 | static void OpenClassPathFiles(const std::string& class_path, std::vector<const DexFile*>& dex_files) { |
| 253 | std::vector<std::string> parsed; |
| 254 | Split(class_path, ':', parsed); |
| 255 | for (size_t i = 0; i < parsed.size(); ++i) { |
| 256 | if (DexFilesContains(dex_files, parsed[i])) { |
| 257 | continue; |
| 258 | } |
| 259 | const DexFile* dex_file = DexFile::Open(parsed[i], Runtime::Current()->GetHostPrefix()); |
| 260 | if (dex_file == NULL) { |
| 261 | LOG(WARNING) << "Failed to open dex file " << parsed[i]; |
| 262 | } else { |
| 263 | dex_files.push_back(dex_file); |
| 264 | } |
Jesse Wilson | 254db0f | 2011-11-16 16:44:11 -0500 | [diff] [blame] | 265 | } |
| 266 | } |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 267 | |
| 268 | // Returns true if dex_files has a dex with the named location. |
| 269 | static bool DexFilesContains(const std::vector<const DexFile*>& dex_files, const std::string& location) { |
| 270 | for (size_t i = 0; i < dex_files.size(); ++i) { |
| 271 | if (dex_files[i]->GetLocation() == location) { |
| 272 | return true; |
| 273 | } |
| 274 | } |
| 275 | return false; |
| 276 | } |
| 277 | |
| 278 | Runtime* runtime_; |
| 279 | static const InstructionSet instruction_set_ = kThumb2; |
| 280 | |
| 281 | DISALLOW_IMPLICIT_CONSTRUCTORS(Dex2Oat); |
| 282 | }; |
Jesse Wilson | 254db0f | 2011-11-16 16:44:11 -0500 | [diff] [blame] | 283 | |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 284 | int dex2oat(int argc, char** argv) { |
| 285 | // Skip over argv[0]. |
| 286 | argv++; |
| 287 | argc--; |
| 288 | |
| 289 | if (argc == 0) { |
| 290 | fprintf(stderr, "no arguments specified\n"); |
| 291 | usage(); |
| 292 | } |
| 293 | |
| 294 | std::vector<const char*> dex_filenames; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 295 | std::string oat_filename; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 296 | const char* image_filename = NULL; |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 297 | const char* image_classes_filename = NULL; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 298 | std::string boot_image_option; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 299 | uintptr_t image_base = 0; |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 300 | std::string host_prefix; |
jeffhao | 5d84040 | 2011-10-24 17:09:45 -0700 | [diff] [blame] | 301 | std::vector<const char*> runtime_args; |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 302 | |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 303 | for (int i = 0; i < argc; i++) { |
| 304 | const StringPiece option(argv[i]); |
Brian Carlstrom | b7bbba4 | 2011-10-13 14:58:47 -0700 | [diff] [blame] | 305 | if (false) { |
| 306 | LOG(INFO) << "dex2oat: option[" << i << "]=" << argv[i]; |
| 307 | } |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 308 | if (option.starts_with("--dex-file=")) { |
| 309 | dex_filenames.push_back(option.substr(strlen("--dex-file=")).data()); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 310 | } else if (option.starts_with("--oat=")) { |
| 311 | oat_filename = option.substr(strlen("--oat=")).data(); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 312 | } else if (option.starts_with("--image=")) { |
| 313 | image_filename = option.substr(strlen("--image=")).data(); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 314 | } else if (option.starts_with("--image-classes=")) { |
| 315 | image_classes_filename = option.substr(strlen("--image-classes=")).data(); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 316 | } else if (option.starts_with("--base=")) { |
| 317 | const char* image_base_str = option.substr(strlen("--base=")).data(); |
| 318 | char* end; |
| 319 | image_base = strtoul(image_base_str, &end, 16); |
| 320 | if (end == image_base_str || *end != '\0') { |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 321 | 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] | 322 | usage(); |
| 323 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 324 | } else if (option.starts_with("--boot-image=")) { |
| 325 | const char* boot_image_filename = option.substr(strlen("--boot-image=")).data(); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 326 | boot_image_option.clear(); |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 327 | boot_image_option += "-Ximage:"; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 328 | boot_image_option += boot_image_filename; |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 329 | } else if (option.starts_with("--host-prefix=")) { |
| 330 | host_prefix = option.substr(strlen("--host-prefix=")).data(); |
jeffhao | 5d84040 | 2011-10-24 17:09:45 -0700 | [diff] [blame] | 331 | } else if (option == "--runtime-arg") { |
| 332 | if (++i >= argc) { |
| 333 | fprintf(stderr, "Missing required argument for --runtime-arg\n"); |
| 334 | usage(); |
| 335 | } |
| 336 | runtime_args.push_back(argv[i]); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 337 | } else { |
| 338 | fprintf(stderr, "unknown argument %s\n", option.data()); |
| 339 | usage(); |
| 340 | } |
| 341 | } |
| 342 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 343 | if (oat_filename.empty()) { |
Elliott Hughes | 362f9bc | 2011-10-17 18:56:41 -0700 | [diff] [blame] | 344 | fprintf(stderr, "--oat file name not specified\n"); |
| 345 | return EXIT_FAILURE; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 346 | } |
| 347 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 348 | bool image = (image_filename != NULL); |
| 349 | if (!image && boot_image_option.empty()) { |
Elliott Hughes | 362f9bc | 2011-10-17 18:56:41 -0700 | [diff] [blame] | 350 | fprintf(stderr, "Either --image or --boot-image must be specified\n"); |
| 351 | return EXIT_FAILURE; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 352 | } |
| 353 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 354 | if (image_classes_filename != NULL && !image) { |
| 355 | fprintf(stderr, "--image-classes should only be used with --image\n"); |
| 356 | return EXIT_FAILURE; |
| 357 | } |
| 358 | |
| 359 | if (image_classes_filename != NULL && !boot_image_option.empty()) { |
| 360 | fprintf(stderr, "--image-classes should not be used with --boot-image\n"); |
Elliott Hughes | 362f9bc | 2011-10-17 18:56:41 -0700 | [diff] [blame] | 361 | return EXIT_FAILURE; |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 362 | } |
| 363 | |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 364 | if (boot_image_option.empty()) { |
| 365 | if (image_base == 0) { |
| 366 | fprintf(stderr, "non-zero --base not specified\n"); |
| 367 | return EXIT_FAILURE; |
| 368 | } |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 369 | } |
| 370 | |
Elliott Hughes | 234da57 | 2011-11-03 22:13:06 -0700 | [diff] [blame] | 371 | // Create the output file if we can, or open it read-only if we weren't first. |
| 372 | bool did_create = true; |
| 373 | int fd = open(oat_filename.c_str(), O_EXCL | O_CREAT | O_TRUNC | O_RDWR, 0666); |
| 374 | if (fd == -1) { |
| 375 | if (errno != EEXIST) { |
Ian Rogers | 5e863dd | 2011-11-02 20:00:10 -0700 | [diff] [blame] | 376 | PLOG(ERROR) << "Unable to create oat file " << oat_filename; |
| 377 | return EXIT_FAILURE; |
| 378 | } |
Elliott Hughes | 234da57 | 2011-11-03 22:13:06 -0700 | [diff] [blame] | 379 | did_create = false; |
| 380 | fd = open(oat_filename.c_str(), O_RDONLY); |
| 381 | if (fd == -1) { |
| 382 | PLOG(ERROR) << "Unable to open oat file for reading " << oat_filename; |
| 383 | return EXIT_FAILURE; |
| 384 | } |
Ian Rogers | 5e863dd | 2011-11-02 20:00:10 -0700 | [diff] [blame] | 385 | } |
Elliott Hughes | 234da57 | 2011-11-03 22:13:06 -0700 | [diff] [blame] | 386 | |
| 387 | // Handles removing the file on failure and unlocking on both failure and success. |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 388 | FileJanitor oat_file_janitor(oat_filename, fd); |
Elliott Hughes | 234da57 | 2011-11-03 22:13:06 -0700 | [diff] [blame] | 389 | |
Elliott Hughes | 234da57 | 2011-11-03 22:13:06 -0700 | [diff] [blame] | 390 | // If we won the creation race, block trying to take the lock (since we're going to be doing |
| 391 | // the work, we need the lock). If we lost the creation race, spin trying to take the lock |
| 392 | // non-blocking until we fail -- at which point we know the other guy has the lock -- and then |
| 393 | // block trying to take the now-taken lock. |
| 394 | if (did_create) { |
| 395 | LOG(INFO) << "This process created " << oat_filename; |
Elliott Hughes | b1f9f22 | 2011-11-04 18:04:00 -0700 | [diff] [blame] | 396 | while (TEMP_FAILURE_RETRY(flock(fd, LOCK_EX)) != 0) { |
Elliott Hughes | 234da57 | 2011-11-03 22:13:06 -0700 | [diff] [blame] | 397 | // Try again. |
| 398 | } |
| 399 | LOG(INFO) << "This process created and locked " << oat_filename; |
| 400 | } else { |
| 401 | LOG(INFO) << "Another process has already created " << oat_filename; |
Elliott Hughes | b1f9f22 | 2011-11-04 18:04:00 -0700 | [diff] [blame] | 402 | while (TEMP_FAILURE_RETRY(flock(fd, LOCK_EX | LOCK_NB)) == 0) { |
Elliott Hughes | 234da57 | 2011-11-03 22:13:06 -0700 | [diff] [blame] | 403 | // Give up the lock and hope the creator has taken the lock next time round. |
Elliott Hughes | b1f9f22 | 2011-11-04 18:04:00 -0700 | [diff] [blame] | 404 | int rc = TEMP_FAILURE_RETRY(flock(fd, LOCK_UN)); |
| 405 | if (rc == -1) { |
| 406 | PLOG(FATAL) << "Failed to unlock " << oat_filename; |
| 407 | } |
Elliott Hughes | 234da57 | 2011-11-03 22:13:06 -0700 | [diff] [blame] | 408 | } |
| 409 | // Now a non-blocking attempt to take the lock has failed, we know the other guy has the |
| 410 | // lock, so block waiting to take it. |
| 411 | LOG(INFO) << "Another process is already working on " << oat_filename; |
Elliott Hughes | b1f9f22 | 2011-11-04 18:04:00 -0700 | [diff] [blame] | 412 | if (TEMP_FAILURE_RETRY(flock(fd, LOCK_EX)) != 0) { |
Elliott Hughes | 234da57 | 2011-11-03 22:13:06 -0700 | [diff] [blame] | 413 | PLOG(ERROR) << "Waiter unable to wait for creator to finish " << oat_filename; |
| 414 | return EXIT_FAILURE; |
| 415 | } |
| 416 | // We have the lock and the creator has finished. |
| 417 | // TODO: check the creator did a good job by checking the header. |
| 418 | LOG(INFO) << "Another process finished working on " << oat_filename; |
| 419 | // Job done. |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 420 | oat_file_janitor.KeepFile(); |
Elliott Hughes | 234da57 | 2011-11-03 22:13:06 -0700 | [diff] [blame] | 421 | return EXIT_SUCCESS; |
| 422 | } |
| 423 | |
| 424 | // If we get this far, we won the creation race and have locked the file. |
| 425 | UniquePtr<File> oat_file(OS::FileFromFd(oat_filename.c_str(), fd)); |
| 426 | |
| 427 | LOG(INFO) << "dex2oat: " << oat_file->name(); |
Ian Rogers | 5e863dd | 2011-11-02 20:00:10 -0700 | [diff] [blame] | 428 | |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 429 | Runtime::Options options; |
Brian Carlstrom | 5de8fe5 | 2011-10-16 14:10:09 -0700 | [diff] [blame] | 430 | options.push_back(std::make_pair("compiler", reinterpret_cast<void*>(NULL))); |
Brian Carlstrom | b7bbba4 | 2011-10-13 14:58:47 -0700 | [diff] [blame] | 431 | std::string boot_class_path_string; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 432 | if (boot_image_option.empty()) { |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 433 | boot_class_path_string += "-Xbootclasspath:"; |
| 434 | for (size_t i = 0; i < dex_filenames.size()-1; i++) { |
| 435 | boot_class_path_string += dex_filenames[i]; |
| 436 | boot_class_path_string += ":"; |
| 437 | } |
| 438 | boot_class_path_string += dex_filenames[dex_filenames.size()-1]; |
| 439 | 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] | 440 | } else { |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 441 | options.push_back(std::make_pair(boot_image_option.c_str(), reinterpret_cast<void*>(NULL))); |
| 442 | } |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 443 | if (!host_prefix.empty()) { |
| 444 | options.push_back(std::make_pair("host-prefix", host_prefix.c_str())); |
| 445 | } |
jeffhao | 5d84040 | 2011-10-24 17:09:45 -0700 | [diff] [blame] | 446 | for (size_t i = 0; i < runtime_args.size(); i++) { |
| 447 | options.push_back(std::make_pair(runtime_args[i], reinterpret_cast<void*>(NULL))); |
| 448 | } |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 449 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 450 | UniquePtr<Dex2Oat> dex2oat(Dex2Oat::Create(options)); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 451 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 452 | // If --image-classes was specified, calculate the full list classes to include in the image |
| 453 | UniquePtr<const std::set<std::string> > image_classes(NULL); |
| 454 | if (image_classes_filename != NULL) { |
| 455 | image_classes.reset(dex2oat->GetImageClassDescriptors(image_classes_filename)); |
| 456 | if (image_classes.get() == NULL) { |
| 457 | LOG(ERROR) << "Failed to create list of image classes from " << image_classes_filename; |
| 458 | return EXIT_FAILURE; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 459 | } |
| 460 | } |
| 461 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 462 | if (!dex2oat->CreateOatFile(boot_image_option, |
| 463 | dex_filenames, |
| 464 | host_prefix, |
| 465 | oat_file.get(), |
| 466 | image, |
| 467 | image_classes.get())) { |
| 468 | LOG(ERROR) << "Failed to create oat file" << oat_filename; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 469 | return EXIT_FAILURE; |
| 470 | } |
| 471 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 472 | if (!image) { |
| 473 | oat_file_janitor.KeepFile(); |
| 474 | LOG(INFO) << "Oat file written successfully " << oat_filename; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 475 | return EXIT_SUCCESS; |
| 476 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 477 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 478 | if (!dex2oat->CreateImageFile(image_filename, |
| 479 | image_base, |
| 480 | image_classes.get(), |
| 481 | oat_filename, |
| 482 | host_prefix)) { |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 483 | return EXIT_FAILURE; |
| 484 | } |
| 485 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 486 | // We wrote the oat file successfully, and want to keep it. |
| 487 | oat_file_janitor.KeepFile(); |
| 488 | LOG(INFO) << "Oat file written successfully " << oat_filename; |
Elliott Hughes | 234da57 | 2011-11-03 22:13:06 -0700 | [diff] [blame] | 489 | LOG(INFO) << "Image written successfully " << image_filename; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 490 | return EXIT_SUCCESS; |
| 491 | } |
| 492 | |
| 493 | } // namespace art |
| 494 | |
| 495 | int main(int argc, char** argv) { |
| 496 | return art::dex2oat(argc, argv); |
| 497 | } |