blob: 314b7ee44dcb7a040ae47efe726eb05835c6a5d3 [file] [log] [blame]
Brian Carlstromb7bbba42011-10-13 14:58:47 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
3#include <stdio.h>
4#include <stdlib.h>
5
6#include "dex_file.h"
7#include "file.h"
8#include "logging.h"
9#include "oat_file.h"
10#include "os.h"
11#include "UniquePtr.h"
12#include "zip_archive.h"
13
14namespace art {
15
Elliott Hughesc1f143d2011-12-01 17:31:10 -080016int ProcessZipFile(int zip_fd, int cache_fd, const char* zip_name, const char* flags) {
Brian Carlstromb7bbba42011-10-13 14:58:47 -070017 // TODO: need to read/write to installd opened file descriptors
18 if (false) {
19 UniquePtr<ZipArchive> zip_archive(ZipArchive::Open(zip_fd));
20 if (zip_archive.get() == NULL) {
21 LOG(ERROR) << "Failed to open " << zip_name << " when looking for classes.dex";
22 return -1;
23 }
24
25 UniquePtr<ZipEntry> zip_entry(zip_archive->Find(DexFile::kClassesDex));
26 if (zip_entry.get() == NULL) {
27 LOG(ERROR) << "Failed to find classes.dex within " << zip_name;
28 return -1;
29 }
30
31 UniquePtr<File> file(OS::FileFromFd("oatopt cache file descriptor", cache_fd));
32 bool success = zip_entry->Extract(*file);
33 if (!success) {
34 LOG(ERROR) << "Failed to extract classes.dex from " << zip_name;
35 return -1;
36 }
37 }
38
39 // Opening a zip file for a dex will extract to art-cache
40 UniquePtr<const DexFile> dex_file(DexFile::Open(zip_name, ""));
41 if (dex_file.get() == NULL) {
42 LOG(ERROR) << "Failed to open " << zip_name;
43 return -1;
44 }
45
46 std::string dex_file_option("--dex-file=");
47 dex_file_option += zip_name;
48
49 std::string oat_file_option("--oat=");
jeffhao262bf462011-10-20 18:36:32 -070050 oat_file_option += GetArtCacheFilenameOrDie(OatFile::DexFilenameToOatFilename(dex_file.get()->GetLocation()));
Brian Carlstromb7bbba42011-10-13 14:58:47 -070051
Elliott Hughes234da572011-11-03 22:13:06 -070052 std::string dex2oat("/system/bin/dex2oat");
53#ifndef NDEBUG
54 dex2oat += 'd';
55#endif
56
57 execl(dex2oat.c_str(), dex2oat.c_str(),
jeffhao5d840402011-10-24 17:09:45 -070058 "--runtime-arg", "-Xms64m",
59 "--runtime-arg", "-Xmx64m",
Brian Carlstromb7bbba42011-10-13 14:58:47 -070060 "--boot-image=/data/art-cache/boot.art",
61 dex_file_option.c_str(),
62 oat_file_option.c_str(),
63 NULL);
64 PLOG(FATAL) << "execl(dex2oatd) failed";
65 return -1;
66}
67
68// Parse arguments. We want:
69// 0. (name of command -- ignored)
70// 1. "--zip"
71// 2. zip fd (input, read-only)
72// 3. cache fd (output, read-write, locked with flock)
73// 4. filename of zipfile
74// 5. flags
75int FromZip(const int argc, const char* const argv[]) {
76 if (argc != 6) {
77 LOG(ERROR) << "Wrong number of args for --zip (found " << argc << ")";
78 return -1;
79 }
80
81 // ignore program name
82
83 // verify --zip
84 CHECK_STREQ(argv[1], "--zip");
85
86 char* zip_end;
87 int zip_fd = strtol(argv[2], &zip_end, 0);
88 if (*zip_end != '\0') {
89 LOG(ERROR) << "bad zip fd: " << argv[2];
90 return -1;
91 }
92#ifndef NDEBUG
93 LOG(INFO) << "zip_fd=" << zip_fd;
94#endif
95
96 char* cache_end;
97 int cache_fd = strtol(argv[3], &cache_end, 0);
98 if (*cache_end != '\0') {
99 LOG(ERROR) << "bad cache fd: " << argv[3];
100 return -1;
101 }
102#ifndef NDEBUG
103 LOG(INFO) << "cache_fd=" << cache_fd;
104#endif
105
106 const char* zip_name = argv[4];
107#ifndef NDEBUG
108 LOG(INFO) << "zip_name=" << zip_name;
109#endif
110
111 const char* flags = argv[5];
112#ifndef NDEBUG
113 LOG(INFO) << "flags=" << flags;
114#endif
115
116 return ProcessZipFile(zip_fd, cache_fd, zip_name, flags);
117}
118
119int oatopt(int argc, char** argv) {
Brian Carlstromb7bbba42011-10-13 14:58:47 -0700120 setvbuf(stdout, NULL, _IONBF, 0);
121
122 if (true) {
123 for (int i = 0; i < argc; ++i) {
124 LOG(INFO) << "oatopt: option[" << i << "]=" << argv[i];
125 }
126 }
127
128 if (argc > 1) {
129 if (strcmp(argv[1], "--zip") == 0) {
130 return FromZip(argc, argv);
131 }
132 }
133
134 fprintf(stderr,
135 "Usage:\n\n"
136 "Short version: Don't use this.\n\n"
137 "Slightly longer version: This system-internal tool is used to extract\n"
138 "dex files and produce oat files. See the source code for details.\n");
139
140 return 1;
141}
142
143} // namespace art
144
145int main(int argc, char** argv) {
146 return art::oatopt(argc, argv);
147}