blob: 0e4c224adb6bd650a35c063a94e0118e1cf93d3c [file] [log] [blame]
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
3#include <stdio.h>
4#include <stdlib.h>
Elliott Hughes234da572011-11-03 22:13:06 -07005#include <sys/file.h>
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07006
Brian Carlstromae826982011-11-09 01:33:42 -08007#include <iostream>
8#include <fstream>
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07009#include <string>
10#include <vector>
11
12#include "class_linker.h"
13#include "class_loader.h"
14#include "compiler.h"
Ian Rogers5e863dd2011-11-02 20:00:10 -070015#include "file.h"
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070016#include "image_writer.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070017#include "oat_writer.h"
Ian Rogers5e863dd2011-11-02 20:00:10 -070018#include "os.h"
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070019#include "runtime.h"
20#include "stringpiece.h"
21
22namespace art {
23
24static void usage() {
25 fprintf(stderr,
26 "Usage: dex2oat [options]...\n"
27 "\n");
28 fprintf(stderr,
Brian Carlstrom78128a62011-09-15 17:21:19 -070029 " --dex-file=<dex-file>: specifies a .dex file to compile. At least one .dex\n"
30 " file must be specified. \n"
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070031 " Example: --dex-file=/system/framework/core.jar\n"
32 "\n");
33 fprintf(stderr,
Brian Carlstromae826982011-11-09 01:33:42 -080034 " --oat=<file.oat>: specifies the required oat filename.\n"
35 " Example: --oat=/data/art-cache/boot.oat\n"
36 "\n");
37 fprintf(stderr,
38 " --image=<file.art>: specifies the output image filename.\n"
Brian Carlstrom47a0d5a2011-10-12 21:20:05 -070039 " Example: --image=/data/art-cache/boot.art\n"
Brian Carlstrome24fa612011-09-29 00:53:55 -070040 "\n");
Brian Carlstrome24fa612011-09-29 00:53:55 -070041 fprintf(stderr,
Brian Carlstromae826982011-11-09 01:33:42 -080042 " --image-classes=<classname-file>: specifies classes to include in an image.\n"
43 " Example: --image=frameworks/base/preloaded-classes\n"
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070044 "\n");
45 fprintf(stderr,
46 " --base=<hex-address>: specifies the base address when creating a boot image.\n"
47 " Example: --base=0x50000000\n"
48 "\n");
49 fprintf(stderr,
Brian Carlstrome24fa612011-09-29 00:53:55 -070050 " --boot-image=<file.art>: provide the image file for the boot class path.\n"
Brian Carlstrom47a0d5a2011-10-12 21:20:05 -070051 " Example: --boot-image=/data/art-cache/boot.art\n"
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070052 "\n");
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070053 fprintf(stderr,
Brian Carlstrom58ae9412011-10-04 00:56:06 -070054 " --host-prefix may be used to translate host paths to target paths during\n"
55 " cross compilation.\n"
56 " Example: --host-prefix=out/target/product/crespo\n"
Brian Carlstrom16192862011-09-12 17:50:06 -070057 "\n");
Brian Carlstrom27ec9612011-09-19 20:20:38 -070058 fprintf(stderr,
jeffhao5d840402011-10-24 17:09:45 -070059 " --runtime-arg <argument>: used to specify various arguments for the runtime,\n"
60 " such as initial heap size, maximum heap size, and verbose output.\n"
61 " Use a separate --runtime-arg switch for each argument.\n"
62 " Example: --runtime-arg -Xms256m\n"
Brian Carlstrom27ec9612011-09-19 20:20:38 -070063 "\n");
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070064 exit(EXIT_FAILURE);
65}
66
Elliott Hughes234da572011-11-03 22:13:06 -070067class FileJanitor {
68public:
69 FileJanitor(const std::string& filename, int fd)
70 : filename_(filename), fd_(fd), do_unlink_(true) {
71 }
72
73 void KeepFile() {
74 do_unlink_ = false;
75 }
76
77 ~FileJanitor() {
78 if (fd_ != -1) {
Elliott Hughesb1f9f222011-11-04 18:04:00 -070079 int rc = TEMP_FAILURE_RETRY(flock(fd_, LOCK_UN));
80 if (rc == -1) {
81 PLOG(ERROR) << "Failed to unlock " << filename_;
82 }
Elliott Hughes234da572011-11-03 22:13:06 -070083 }
84 if (do_unlink_) {
Elliott Hughesb1f9f222011-11-04 18:04:00 -070085 int rc = TEMP_FAILURE_RETRY(unlink(filename_.c_str()));
86 if (rc == -1) {
87 PLOG(ERROR) << "Failed to unlink " << filename_;
88 }
Elliott Hughes234da572011-11-03 22:13:06 -070089 }
90 }
91
92private:
93 std::string filename_;
94 int fd_;
95 bool do_unlink_;
96};
97
Brian Carlstromae826982011-11-09 01:33:42 -080098class Dex2Oat {
99 public:
100
101 static Dex2Oat* Create(Runtime::Options& options) {
102 UniquePtr<Runtime> runtime(CreateRuntime(options));
103 if (runtime.get() == NULL) {
104 return NULL;
105 }
106 return new Dex2Oat(runtime.release());
107 }
108
109 ~Dex2Oat() {
110 delete runtime_;
111 }
112
113 // Make a list of descriptors for classes to include in the image
114 const std::set<std::string>* GetImageClassDescriptors(const char* image_classes_filename) {
115 UniquePtr<std::ifstream> image_classes_file(new std::ifstream(image_classes_filename, std::ifstream::in));
116 if (image_classes_file.get() == NULL) {
117 LOG(ERROR) << "Failed to open image classes file " << image_classes_filename;
118 return NULL;
119 }
120
121 // Load all the classes specifed in the file
122 ClassLinker* class_linker = runtime_->GetClassLinker();
123 while (image_classes_file->good()) {
124 std::string dot;
125 std::getline(*image_classes_file.get(), dot);
126 if (StringPiece(dot).starts_with("#") || dot.empty()) {
127 continue;
128 }
129 std::string descriptor = DotToDescriptor(dot.c_str());
130 SirtRef<Class> klass(class_linker->FindSystemClass(descriptor));
131 if (klass.get() == NULL) {
132 LOG(WARNING) << "Failed to find class " << descriptor;
133 Thread::Current()->ClearException();
134 }
135 }
136 image_classes_file->close();
137
138 // We walk the roots looking for classes so that we'll pick up the
139 // above classes plus any classes them depend on such super
140 // classes, interfaces, and the required ClassLinker roots.
141 UniquePtr<std::set<std::string> > image_classes(new std::set<std::string>());
142 class_linker->VisitClasses(ClassVisitor, image_classes.get());
143 CHECK_NE(image_classes->size(), 0U);
144 return image_classes.release();
145 }
146
147 bool CreateOatFile(const std::string& boot_image_option,
148 const std::vector<const char*>& dex_filenames,
149 const std::string& host_prefix,
150 File* oat_file,
151 bool image,
152 const std::set<std::string>* image_classes) {
153 // SirtRef and ClassLoader creation needs to come after Runtime::Create
154 UniquePtr<SirtRef<ClassLoader> > class_loader(new SirtRef<ClassLoader>(NULL));
155 if (class_loader.get() == NULL) {
156 LOG(ERROR) << "Failed to create SirtRef for class loader";
157 return false;
158 }
159
160 std::vector<const DexFile*> dex_files;
161 if (!boot_image_option.empty()) {
162 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
163 DexFile::OpenDexFiles(dex_filenames, dex_files, host_prefix);
164 std::vector<const DexFile*> class_path_files(dex_files);
165 OpenClassPathFiles(runtime_->GetClassPath(), class_path_files);
166 for (size_t i = 0; i < class_path_files.size(); i++) {
167 class_linker->RegisterDexFile(*class_path_files[i]);
168 }
169 class_loader.get()->reset(PathClassLoader::AllocCompileTime(class_path_files));
170 } else {
171 dex_files = runtime_->GetClassLinker()->GetBootClassPath();
172 }
173
174 Compiler compiler(instruction_set_, image, image_classes);
175 compiler.CompileAll(class_loader->get(), dex_files);
176
177 if (!OatWriter::Create(oat_file, class_loader->get(), compiler)) {
178 LOG(ERROR) << "Failed to create oat file " << oat_file->name();
179 return false;
180 }
181 return true;
182 }
183
184 bool CreateImageFile(const char* image_filename,
185 uintptr_t image_base,
186 const std::set<std::string>* image_classes,
187 const std::string& oat_filename,
188 const std::string& host_prefix) {
189 // If we have an existing boot image, position new space after its oat file
190 if (Heap::GetSpaces().size() > 1) {
191 Space* last_image_space = Heap::GetSpaces()[Heap::GetSpaces().size()-2];
192 CHECK(last_image_space != NULL);
193 CHECK(last_image_space->IsImageSpace());
194 CHECK(!Heap::GetSpaces()[Heap::GetSpaces().size()-1]->IsImageSpace());
195 byte* oat_limit_addr = last_image_space->GetImageHeader().GetOatLimitAddr();
196 image_base = RoundUp(reinterpret_cast<uintptr_t>(oat_limit_addr), kPageSize);
197 }
198
199 ImageWriter image_writer(image_classes);
200 if (!image_writer.Write(image_filename, image_base, oat_filename, host_prefix)) {
201 LOG(ERROR) << "Failed to create image file " << image_filename;
202 return false;
203 }
204 return true;
205 }
206
207 private:
208
209 Dex2Oat(Runtime* runtime) : runtime_(runtime) {}
210
211 static Runtime* CreateRuntime(Runtime::Options& options) {
212 Runtime* runtime = Runtime::Create(options, false);
213 if (runtime == NULL) {
214 LOG(ERROR) << "Failed to create runtime";
215 return NULL;
216 }
217
218 // if we loaded an existing image, we will reuse values from the image roots.
219 if (!runtime->HasJniDlsymLookupStub()) {
220 runtime->SetJniDlsymLookupStub(Compiler::CreateJniDlysmLookupStub(instruction_set_));
221 }
222 if (!runtime->HasAbstractMethodErrorStubArray()) {
223 runtime->SetAbstractMethodErrorStubArray(Compiler::CreateAbstractMethodErrorStub(instruction_set_));
224 }
225 for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) {
226 Runtime::TrampolineType type = Runtime::TrampolineType(i);
227 if (!runtime->HasResolutionStubArray(type)) {
228 runtime->SetResolutionStubArray(Compiler::CreateResolutionStub(instruction_set_, type), type);
229 }
230 }
231 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
232 Runtime::CalleeSaveType type = Runtime::CalleeSaveType(i);
233 if (!runtime->HasCalleeSaveMethod(type)) {
234 runtime->SetCalleeSaveMethod(runtime->CreateCalleeSaveMethod(instruction_set_, type), type);
235 }
236 }
237 return runtime;
238 }
239
240 static bool ClassVisitor(Class* klass, void* arg) {
241 std::set<std::string>* image_classes = reinterpret_cast<std::set<std::string>*>(arg);
242 if (klass->IsArrayClass() || klass->IsPrimitive()) {
Jesse Wilson254db0f2011-11-16 16:44:11 -0500243 return true;
244 }
Brian Carlstromae826982011-11-09 01:33:42 -0800245 image_classes->insert(klass->GetDescriptor()->ToModifiedUtf8());
246 return true;
Jesse Wilson254db0f2011-11-16 16:44:11 -0500247 }
Jesse Wilson254db0f2011-11-16 16:44:11 -0500248
Brian Carlstromae826982011-11-09 01:33:42 -0800249 // Appends to dex_files any elements of class_path that it doesn't already
250 // contain. This will open those dex files as necessary.
251 static void OpenClassPathFiles(const std::string& class_path, std::vector<const DexFile*>& dex_files) {
252 std::vector<std::string> parsed;
253 Split(class_path, ':', parsed);
254 for (size_t i = 0; i < parsed.size(); ++i) {
255 if (DexFilesContains(dex_files, parsed[i])) {
256 continue;
257 }
258 const DexFile* dex_file = DexFile::Open(parsed[i], Runtime::Current()->GetHostPrefix());
259 if (dex_file == NULL) {
260 LOG(WARNING) << "Failed to open dex file " << parsed[i];
261 } else {
262 dex_files.push_back(dex_file);
263 }
Jesse Wilson254db0f2011-11-16 16:44:11 -0500264 }
265 }
Brian Carlstromae826982011-11-09 01:33:42 -0800266
267 // Returns true if dex_files has a dex with the named location.
268 static bool DexFilesContains(const std::vector<const DexFile*>& dex_files, const std::string& location) {
269 for (size_t i = 0; i < dex_files.size(); ++i) {
270 if (dex_files[i]->GetLocation() == location) {
271 return true;
272 }
273 }
274 return false;
275 }
276
277 Runtime* runtime_;
278 static const InstructionSet instruction_set_ = kThumb2;
279
280 DISALLOW_IMPLICIT_CONSTRUCTORS(Dex2Oat);
281};
Jesse Wilson254db0f2011-11-16 16:44:11 -0500282
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700283int dex2oat(int argc, char** argv) {
284 // Skip over argv[0].
285 argv++;
286 argc--;
287
288 if (argc == 0) {
289 fprintf(stderr, "no arguments specified\n");
290 usage();
291 }
292
293 std::vector<const char*> dex_filenames;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700294 std::string oat_filename;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700295 const char* image_filename = NULL;
Brian Carlstromae826982011-11-09 01:33:42 -0800296 const char* image_classes_filename = NULL;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700297 std::string boot_image_option;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700298 uintptr_t image_base = 0;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700299 std::string host_prefix;
jeffhao5d840402011-10-24 17:09:45 -0700300 std::vector<const char*> runtime_args;
Brian Carlstrom16192862011-09-12 17:50:06 -0700301
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700302 for (int i = 0; i < argc; i++) {
303 const StringPiece option(argv[i]);
Brian Carlstromb7bbba42011-10-13 14:58:47 -0700304 if (false) {
305 LOG(INFO) << "dex2oat: option[" << i << "]=" << argv[i];
306 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700307 if (option.starts_with("--dex-file=")) {
308 dex_filenames.push_back(option.substr(strlen("--dex-file=")).data());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700309 } else if (option.starts_with("--oat=")) {
310 oat_filename = option.substr(strlen("--oat=")).data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700311 } else if (option.starts_with("--image=")) {
312 image_filename = option.substr(strlen("--image=")).data();
Brian Carlstromae826982011-11-09 01:33:42 -0800313 } else if (option.starts_with("--image-classes=")) {
314 image_classes_filename = option.substr(strlen("--image-classes=")).data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700315 } else if (option.starts_with("--base=")) {
316 const char* image_base_str = option.substr(strlen("--base=")).data();
317 char* end;
318 image_base = strtoul(image_base_str, &end, 16);
319 if (end == image_base_str || *end != '\0') {
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700320 fprintf(stderr, "Failed to parse hexadecimal value for option %s\n", option.data());
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700321 usage();
322 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700323 } else if (option.starts_with("--boot-image=")) {
324 const char* boot_image_filename = option.substr(strlen("--boot-image=")).data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700325 boot_image_option.clear();
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700326 boot_image_option += "-Ximage:";
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700327 boot_image_option += boot_image_filename;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700328 } else if (option.starts_with("--host-prefix=")) {
329 host_prefix = option.substr(strlen("--host-prefix=")).data();
jeffhao5d840402011-10-24 17:09:45 -0700330 } else if (option == "--runtime-arg") {
331 if (++i >= argc) {
332 fprintf(stderr, "Missing required argument for --runtime-arg\n");
333 usage();
334 }
335 runtime_args.push_back(argv[i]);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700336 } else {
337 fprintf(stderr, "unknown argument %s\n", option.data());
338 usage();
339 }
340 }
341
Brian Carlstromae826982011-11-09 01:33:42 -0800342 if (oat_filename.empty()) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700343 fprintf(stderr, "--oat file name not specified\n");
344 return EXIT_FAILURE;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700345 }
346
Brian Carlstromae826982011-11-09 01:33:42 -0800347 bool image = (image_filename != NULL);
348 if (!image && boot_image_option.empty()) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700349 fprintf(stderr, "Either --image or --boot-image must be specified\n");
350 return EXIT_FAILURE;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700351 }
352
Brian Carlstromae826982011-11-09 01:33:42 -0800353 if (image_classes_filename != NULL && !image) {
354 fprintf(stderr, "--image-classes should only be used with --image\n");
355 return EXIT_FAILURE;
356 }
357
358 if (image_classes_filename != NULL && !boot_image_option.empty()) {
359 fprintf(stderr, "--image-classes should not be used with --boot-image\n");
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700360 return EXIT_FAILURE;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700361 }
362
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700363 if (boot_image_option.empty()) {
364 if (image_base == 0) {
365 fprintf(stderr, "non-zero --base not specified\n");
366 return EXIT_FAILURE;
367 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700368 }
369
Elliott Hughes234da572011-11-03 22:13:06 -0700370 // Create the output file if we can, or open it read-only if we weren't first.
371 bool did_create = true;
372 int fd = open(oat_filename.c_str(), O_EXCL | O_CREAT | O_TRUNC | O_RDWR, 0666);
373 if (fd == -1) {
374 if (errno != EEXIST) {
Ian Rogers5e863dd2011-11-02 20:00:10 -0700375 PLOG(ERROR) << "Unable to create oat file " << oat_filename;
376 return EXIT_FAILURE;
377 }
Elliott Hughes234da572011-11-03 22:13:06 -0700378 did_create = false;
379 fd = open(oat_filename.c_str(), O_RDONLY);
380 if (fd == -1) {
381 PLOG(ERROR) << "Unable to open oat file for reading " << oat_filename;
382 return EXIT_FAILURE;
383 }
Ian Rogers5e863dd2011-11-02 20:00:10 -0700384 }
Elliott Hughes234da572011-11-03 22:13:06 -0700385
386 // Handles removing the file on failure and unlocking on both failure and success.
Brian Carlstromae826982011-11-09 01:33:42 -0800387 FileJanitor oat_file_janitor(oat_filename, fd);
Elliott Hughes234da572011-11-03 22:13:06 -0700388
Elliott Hughes234da572011-11-03 22:13:06 -0700389 // If we won the creation race, block trying to take the lock (since we're going to be doing
390 // the work, we need the lock). If we lost the creation race, spin trying to take the lock
391 // non-blocking until we fail -- at which point we know the other guy has the lock -- and then
392 // block trying to take the now-taken lock.
393 if (did_create) {
394 LOG(INFO) << "This process created " << oat_filename;
Elliott Hughesb1f9f222011-11-04 18:04:00 -0700395 while (TEMP_FAILURE_RETRY(flock(fd, LOCK_EX)) != 0) {
Elliott Hughes234da572011-11-03 22:13:06 -0700396 // Try again.
397 }
398 LOG(INFO) << "This process created and locked " << oat_filename;
399 } else {
400 LOG(INFO) << "Another process has already created " << oat_filename;
Elliott Hughesb1f9f222011-11-04 18:04:00 -0700401 while (TEMP_FAILURE_RETRY(flock(fd, LOCK_EX | LOCK_NB)) == 0) {
Elliott Hughes234da572011-11-03 22:13:06 -0700402 // Give up the lock and hope the creator has taken the lock next time round.
Elliott Hughesb1f9f222011-11-04 18:04:00 -0700403 int rc = TEMP_FAILURE_RETRY(flock(fd, LOCK_UN));
404 if (rc == -1) {
405 PLOG(FATAL) << "Failed to unlock " << oat_filename;
406 }
Elliott Hughes234da572011-11-03 22:13:06 -0700407 }
408 // Now a non-blocking attempt to take the lock has failed, we know the other guy has the
409 // lock, so block waiting to take it.
410 LOG(INFO) << "Another process is already working on " << oat_filename;
Elliott Hughesb1f9f222011-11-04 18:04:00 -0700411 if (TEMP_FAILURE_RETRY(flock(fd, LOCK_EX)) != 0) {
Elliott Hughes234da572011-11-03 22:13:06 -0700412 PLOG(ERROR) << "Waiter unable to wait for creator to finish " << oat_filename;
413 return EXIT_FAILURE;
414 }
415 // We have the lock and the creator has finished.
416 // TODO: check the creator did a good job by checking the header.
417 LOG(INFO) << "Another process finished working on " << oat_filename;
418 // Job done.
Brian Carlstromae826982011-11-09 01:33:42 -0800419 oat_file_janitor.KeepFile();
Elliott Hughes234da572011-11-03 22:13:06 -0700420 return EXIT_SUCCESS;
421 }
422
423 // If we get this far, we won the creation race and have locked the file.
424 UniquePtr<File> oat_file(OS::FileFromFd(oat_filename.c_str(), fd));
425
426 LOG(INFO) << "dex2oat: " << oat_file->name();
Ian Rogers5e863dd2011-11-02 20:00:10 -0700427
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700428 Runtime::Options options;
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700429 options.push_back(std::make_pair("compiler", reinterpret_cast<void*>(NULL)));
Brian Carlstromb7bbba42011-10-13 14:58:47 -0700430 std::string boot_class_path_string;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700431 if (boot_image_option.empty()) {
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700432 boot_class_path_string += "-Xbootclasspath:";
433 for (size_t i = 0; i < dex_filenames.size()-1; i++) {
434 boot_class_path_string += dex_filenames[i];
435 boot_class_path_string += ":";
436 }
437 boot_class_path_string += dex_filenames[dex_filenames.size()-1];
438 options.push_back(std::make_pair(boot_class_path_string.c_str(), reinterpret_cast<void*>(NULL)));
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700439 } else {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700440 options.push_back(std::make_pair(boot_image_option.c_str(), reinterpret_cast<void*>(NULL)));
441 }
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700442 if (!host_prefix.empty()) {
443 options.push_back(std::make_pair("host-prefix", host_prefix.c_str()));
444 }
jeffhao5d840402011-10-24 17:09:45 -0700445 for (size_t i = 0; i < runtime_args.size(); i++) {
446 options.push_back(std::make_pair(runtime_args[i], reinterpret_cast<void*>(NULL)));
447 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700448
Brian Carlstromae826982011-11-09 01:33:42 -0800449 UniquePtr<Dex2Oat> dex2oat(Dex2Oat::Create(options));
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700450
Brian Carlstromae826982011-11-09 01:33:42 -0800451 // If --image-classes was specified, calculate the full list classes to include in the image
452 UniquePtr<const std::set<std::string> > image_classes(NULL);
453 if (image_classes_filename != NULL) {
454 image_classes.reset(dex2oat->GetImageClassDescriptors(image_classes_filename));
455 if (image_classes.get() == NULL) {
456 LOG(ERROR) << "Failed to create list of image classes from " << image_classes_filename;
457 return EXIT_FAILURE;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700458 }
459 }
460
Brian Carlstromae826982011-11-09 01:33:42 -0800461 if (!dex2oat->CreateOatFile(boot_image_option,
462 dex_filenames,
463 host_prefix,
464 oat_file.get(),
465 image,
466 image_classes.get())) {
467 LOG(ERROR) << "Failed to create oat file" << oat_filename;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700468 return EXIT_FAILURE;
469 }
470
Brian Carlstromae826982011-11-09 01:33:42 -0800471 if (!image) {
472 oat_file_janitor.KeepFile();
473 LOG(INFO) << "Oat file written successfully " << oat_filename;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700474 return EXIT_SUCCESS;
475 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700476
Brian Carlstromae826982011-11-09 01:33:42 -0800477 if (!dex2oat->CreateImageFile(image_filename,
478 image_base,
479 image_classes.get(),
480 oat_filename,
481 host_prefix)) {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700482 return EXIT_FAILURE;
483 }
484
Brian Carlstromae826982011-11-09 01:33:42 -0800485 // We wrote the oat file successfully, and want to keep it.
486 oat_file_janitor.KeepFile();
487 LOG(INFO) << "Oat file written successfully " << oat_filename;
Elliott Hughes234da572011-11-03 22:13:06 -0700488 LOG(INFO) << "Image written successfully " << image_filename;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700489 return EXIT_SUCCESS;
490}
491
492} // namespace art
493
494int main(int argc, char** argv) {
495 return art::dex2oat(argc, argv);
496}