blob: 1261bb9360faad2093b31c92592e415e075f5805 [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 Rogers6d4d9fc2011-11-30 16:24:48 -080018#include "object_utils.h"
Ian Rogers5e863dd2011-11-02 20:00:10 -070019#include "os.h"
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070020#include "runtime.h"
21#include "stringpiece.h"
22
23namespace art {
24
25static void usage() {
26 fprintf(stderr,
27 "Usage: dex2oat [options]...\n"
28 "\n");
29 fprintf(stderr,
Brian Carlstrom78128a62011-09-15 17:21:19 -070030 " --dex-file=<dex-file>: specifies a .dex file to compile. At least one .dex\n"
31 " file must be specified. \n"
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070032 " Example: --dex-file=/system/framework/core.jar\n"
33 "\n");
34 fprintf(stderr,
Brian Carlstromae826982011-11-09 01:33:42 -080035 " --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 Carlstrom47a0d5a2011-10-12 21:20:05 -070040 " Example: --image=/data/art-cache/boot.art\n"
Brian Carlstrome24fa612011-09-29 00:53:55 -070041 "\n");
Brian Carlstrome24fa612011-09-29 00:53:55 -070042 fprintf(stderr,
Brian Carlstromae826982011-11-09 01:33:42 -080043 " --image-classes=<classname-file>: specifies classes to include in an image.\n"
44 " Example: --image=frameworks/base/preloaded-classes\n"
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070045 "\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 Carlstrome24fa612011-09-29 00:53:55 -070051 " --boot-image=<file.art>: provide the image file for the boot class path.\n"
Brian Carlstrom47a0d5a2011-10-12 21:20:05 -070052 " Example: --boot-image=/data/art-cache/boot.art\n"
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070053 "\n");
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070054 fprintf(stderr,
Brian Carlstrom58ae9412011-10-04 00:56:06 -070055 " --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 Carlstrom16192862011-09-12 17:50:06 -070058 "\n");
Brian Carlstrom27ec9612011-09-19 20:20:38 -070059 fprintf(stderr,
jeffhao5d840402011-10-24 17:09:45 -070060 " --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 Carlstrom27ec9612011-09-19 20:20:38 -070064 "\n");
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070065 exit(EXIT_FAILURE);
66}
67
Elliott Hughes234da572011-11-03 22:13:06 -070068class FileJanitor {
69public:
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 Hughesb1f9f222011-11-04 18:04:00 -070080 int rc = TEMP_FAILURE_RETRY(flock(fd_, LOCK_UN));
81 if (rc == -1) {
82 PLOG(ERROR) << "Failed to unlock " << filename_;
83 }
Elliott Hughes234da572011-11-03 22:13:06 -070084 }
85 if (do_unlink_) {
Elliott Hughesb1f9f222011-11-04 18:04:00 -070086 int rc = TEMP_FAILURE_RETRY(unlink(filename_.c_str()));
87 if (rc == -1) {
88 PLOG(ERROR) << "Failed to unlink " << filename_;
89 }
Elliott Hughes234da572011-11-03 22:13:06 -070090 }
91 }
92
93private:
94 std::string filename_;
95 int fd_;
96 bool do_unlink_;
97};
98
Brian Carlstromae826982011-11-09 01:33:42 -080099class 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 Wilson254db0f2011-11-16 16:44:11 -0500244 return true;
245 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800246 image_classes->insert(ClassHelper(klass).GetDescriptor());
Brian Carlstromae826982011-11-09 01:33:42 -0800247 return true;
Jesse Wilson254db0f2011-11-16 16:44:11 -0500248 }
Jesse Wilson254db0f2011-11-16 16:44:11 -0500249
Brian Carlstromae826982011-11-09 01:33:42 -0800250 // 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 Wilson254db0f2011-11-16 16:44:11 -0500265 }
266 }
Brian Carlstromae826982011-11-09 01:33:42 -0800267
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 Wilson254db0f2011-11-16 16:44:11 -0500283
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700284int 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 Carlstrome24fa612011-09-29 00:53:55 -0700295 std::string oat_filename;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700296 const char* image_filename = NULL;
Brian Carlstromae826982011-11-09 01:33:42 -0800297 const char* image_classes_filename = NULL;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700298 std::string boot_image_option;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700299 uintptr_t image_base = 0;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700300 std::string host_prefix;
jeffhao5d840402011-10-24 17:09:45 -0700301 std::vector<const char*> runtime_args;
Brian Carlstrom16192862011-09-12 17:50:06 -0700302
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700303 for (int i = 0; i < argc; i++) {
304 const StringPiece option(argv[i]);
Brian Carlstromb7bbba42011-10-13 14:58:47 -0700305 if (false) {
306 LOG(INFO) << "dex2oat: option[" << i << "]=" << argv[i];
307 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700308 if (option.starts_with("--dex-file=")) {
309 dex_filenames.push_back(option.substr(strlen("--dex-file=")).data());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700310 } else if (option.starts_with("--oat=")) {
311 oat_filename = option.substr(strlen("--oat=")).data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700312 } else if (option.starts_with("--image=")) {
313 image_filename = option.substr(strlen("--image=")).data();
Brian Carlstromae826982011-11-09 01:33:42 -0800314 } else if (option.starts_with("--image-classes=")) {
315 image_classes_filename = option.substr(strlen("--image-classes=")).data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700316 } 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 Carlstrom27ec9612011-09-19 20:20:38 -0700321 fprintf(stderr, "Failed to parse hexadecimal value for option %s\n", option.data());
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700322 usage();
323 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700324 } else if (option.starts_with("--boot-image=")) {
325 const char* boot_image_filename = option.substr(strlen("--boot-image=")).data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700326 boot_image_option.clear();
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700327 boot_image_option += "-Ximage:";
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700328 boot_image_option += boot_image_filename;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700329 } else if (option.starts_with("--host-prefix=")) {
330 host_prefix = option.substr(strlen("--host-prefix=")).data();
jeffhao5d840402011-10-24 17:09:45 -0700331 } 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 Carlstrom69b15fb2011-09-03 12:25:21 -0700337 } else {
338 fprintf(stderr, "unknown argument %s\n", option.data());
339 usage();
340 }
341 }
342
Brian Carlstromae826982011-11-09 01:33:42 -0800343 if (oat_filename.empty()) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700344 fprintf(stderr, "--oat file name not specified\n");
345 return EXIT_FAILURE;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700346 }
347
Brian Carlstromae826982011-11-09 01:33:42 -0800348 bool image = (image_filename != NULL);
349 if (!image && boot_image_option.empty()) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700350 fprintf(stderr, "Either --image or --boot-image must be specified\n");
351 return EXIT_FAILURE;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700352 }
353
Brian Carlstromae826982011-11-09 01:33:42 -0800354 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 Hughes362f9bc2011-10-17 18:56:41 -0700361 return EXIT_FAILURE;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700362 }
363
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700364 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 Carlstrom69b15fb2011-09-03 12:25:21 -0700369 }
370
Elliott Hughes234da572011-11-03 22:13:06 -0700371 // 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 Rogers5e863dd2011-11-02 20:00:10 -0700376 PLOG(ERROR) << "Unable to create oat file " << oat_filename;
377 return EXIT_FAILURE;
378 }
Elliott Hughes234da572011-11-03 22:13:06 -0700379 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 Rogers5e863dd2011-11-02 20:00:10 -0700385 }
Elliott Hughes234da572011-11-03 22:13:06 -0700386
387 // Handles removing the file on failure and unlocking on both failure and success.
Brian Carlstromae826982011-11-09 01:33:42 -0800388 FileJanitor oat_file_janitor(oat_filename, fd);
Elliott Hughes234da572011-11-03 22:13:06 -0700389
Elliott Hughes234da572011-11-03 22:13:06 -0700390 // 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 Hughesb1f9f222011-11-04 18:04:00 -0700396 while (TEMP_FAILURE_RETRY(flock(fd, LOCK_EX)) != 0) {
Elliott Hughes234da572011-11-03 22:13:06 -0700397 // 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 Hughesb1f9f222011-11-04 18:04:00 -0700402 while (TEMP_FAILURE_RETRY(flock(fd, LOCK_EX | LOCK_NB)) == 0) {
Elliott Hughes234da572011-11-03 22:13:06 -0700403 // Give up the lock and hope the creator has taken the lock next time round.
Elliott Hughesb1f9f222011-11-04 18:04:00 -0700404 int rc = TEMP_FAILURE_RETRY(flock(fd, LOCK_UN));
405 if (rc == -1) {
406 PLOG(FATAL) << "Failed to unlock " << oat_filename;
407 }
Elliott Hughes234da572011-11-03 22:13:06 -0700408 }
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 Hughesb1f9f222011-11-04 18:04:00 -0700412 if (TEMP_FAILURE_RETRY(flock(fd, LOCK_EX)) != 0) {
Elliott Hughes234da572011-11-03 22:13:06 -0700413 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 Carlstromae826982011-11-09 01:33:42 -0800420 oat_file_janitor.KeepFile();
Elliott Hughes234da572011-11-03 22:13:06 -0700421 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 Rogers5e863dd2011-11-02 20:00:10 -0700428
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700429 Runtime::Options options;
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700430 options.push_back(std::make_pair("compiler", reinterpret_cast<void*>(NULL)));
Brian Carlstromb7bbba42011-10-13 14:58:47 -0700431 std::string boot_class_path_string;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700432 if (boot_image_option.empty()) {
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700433 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 Carlstrom69b15fb2011-09-03 12:25:21 -0700440 } else {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700441 options.push_back(std::make_pair(boot_image_option.c_str(), reinterpret_cast<void*>(NULL)));
442 }
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700443 if (!host_prefix.empty()) {
444 options.push_back(std::make_pair("host-prefix", host_prefix.c_str()));
445 }
jeffhao5d840402011-10-24 17:09:45 -0700446 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 Carlstrom69b15fb2011-09-03 12:25:21 -0700449
Brian Carlstromae826982011-11-09 01:33:42 -0800450 UniquePtr<Dex2Oat> dex2oat(Dex2Oat::Create(options));
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700451
Brian Carlstromae826982011-11-09 01:33:42 -0800452 // 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 Carlstrom69b15fb2011-09-03 12:25:21 -0700459 }
460 }
461
Brian Carlstromae826982011-11-09 01:33:42 -0800462 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 Carlstrome24fa612011-09-29 00:53:55 -0700469 return EXIT_FAILURE;
470 }
471
Brian Carlstromae826982011-11-09 01:33:42 -0800472 if (!image) {
473 oat_file_janitor.KeepFile();
474 LOG(INFO) << "Oat file written successfully " << oat_filename;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700475 return EXIT_SUCCESS;
476 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700477
Brian Carlstromae826982011-11-09 01:33:42 -0800478 if (!dex2oat->CreateImageFile(image_filename,
479 image_base,
480 image_classes.get(),
481 oat_filename,
482 host_prefix)) {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700483 return EXIT_FAILURE;
484 }
485
Brian Carlstromae826982011-11-09 01:33:42 -0800486 // 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 Hughes234da572011-11-03 22:13:06 -0700489 LOG(INFO) << "Image written successfully " << image_filename;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700490 return EXIT_SUCCESS;
491}
492
493} // namespace art
494
495int main(int argc, char** argv) {
496 return art::dex2oat(argc, argv);
497}