xref: /aosp_15_r20/art/test/generate-boot-image/generate-boot-image.cc (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2022 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker  *
4*795d594fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker  *
8*795d594fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker  *
10*795d594fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker  * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker  */
16*795d594fSAndroid Build Coastguard Worker 
17*795d594fSAndroid Build Coastguard Worker #include <sys/stat.h>
18*795d594fSAndroid Build Coastguard Worker #include <sysexits.h>
19*795d594fSAndroid Build Coastguard Worker 
20*795d594fSAndroid Build Coastguard Worker #include <algorithm>
21*795d594fSAndroid Build Coastguard Worker #include <cstdlib>
22*795d594fSAndroid Build Coastguard Worker #include <iostream>
23*795d594fSAndroid Build Coastguard Worker #include <iterator>
24*795d594fSAndroid Build Coastguard Worker #include <string>
25*795d594fSAndroid Build Coastguard Worker #include <vector>
26*795d594fSAndroid Build Coastguard Worker 
27*795d594fSAndroid Build Coastguard Worker #include "android-base/parsebool.h"
28*795d594fSAndroid Build Coastguard Worker #include "android-base/stringprintf.h"
29*795d594fSAndroid Build Coastguard Worker #include "android-base/strings.h"
30*795d594fSAndroid Build Coastguard Worker #include "arch/instruction_set.h"
31*795d594fSAndroid Build Coastguard Worker #include "base/file_utils.h"
32*795d594fSAndroid Build Coastguard Worker #include "base/globals.h"
33*795d594fSAndroid Build Coastguard Worker #include "base/macros.h"
34*795d594fSAndroid Build Coastguard Worker #include "base/os.h"
35*795d594fSAndroid Build Coastguard Worker #include "base/testing.h"
36*795d594fSAndroid Build Coastguard Worker 
37*795d594fSAndroid Build Coastguard Worker namespace art {
38*795d594fSAndroid Build Coastguard Worker namespace gbi {
39*795d594fSAndroid Build Coastguard Worker 
40*795d594fSAndroid Build Coastguard Worker namespace {
41*795d594fSAndroid Build Coastguard Worker 
42*795d594fSAndroid Build Coastguard Worker using ::android::base::ConsumePrefix;
43*795d594fSAndroid Build Coastguard Worker using ::android::base::ConsumeSuffix;
44*795d594fSAndroid Build Coastguard Worker using ::android::base::Join;
45*795d594fSAndroid Build Coastguard Worker using ::android::base::ParseBool;
46*795d594fSAndroid Build Coastguard Worker using ::android::base::ParseBoolResult;
47*795d594fSAndroid Build Coastguard Worker using ::android::base::StringPrintf;
48*795d594fSAndroid Build Coastguard Worker using ::art::testing::GetLibCoreDexFileNames;
49*795d594fSAndroid Build Coastguard Worker using ::art::testing::GetLibCoreDexLocations;
50*795d594fSAndroid Build Coastguard Worker 
51*795d594fSAndroid Build Coastguard Worker constexpr const char* kUsage = R"(
52*795d594fSAndroid Build Coastguard Worker A commandline tool to generate a primary boot image for testing.
53*795d594fSAndroid Build Coastguard Worker 
54*795d594fSAndroid Build Coastguard Worker Usage: generate-boot-image --output-dir=OUTPUT_DIR [OPTIONS]... [-- [DEX2OAT_OPTIONS]...]
55*795d594fSAndroid Build Coastguard Worker 
56*795d594fSAndroid Build Coastguard Worker Supported options:
57*795d594fSAndroid Build Coastguard Worker   --help: Print this text.
58*795d594fSAndroid Build Coastguard Worker   --output-dir=OUTPUT_DIR: The directory to output the boot image. Required.
59*795d594fSAndroid Build Coastguard Worker   --compiler-filter=COMPILER_FILTER: The compiler filter option to pass to dex2oat. Default: verify
60*795d594fSAndroid Build Coastguard Worker   --use-profile=true|false: If true, use a profile. Default: true
61*795d594fSAndroid Build Coastguard Worker   --dex2oat-bin=DEX2OAT_BIN: The path to the dex2oat binary. Required when running on host. Default
62*795d594fSAndroid Build Coastguard Worker       on target: /apex/com.android.art/bin/dex2oat{32,64,32d,64d}
63*795d594fSAndroid Build Coastguard Worker   --android-root=ANDROID_ROOT: The root directory to search for bootclasspath jars. The file
64*795d594fSAndroid Build Coastguard Worker       structure under the root must be in the form of:
65*795d594fSAndroid Build Coastguard Worker       /apex
66*795d594fSAndroid Build Coastguard Worker         /com.android.art
67*795d594fSAndroid Build Coastguard Worker           /javalib
68*795d594fSAndroid Build Coastguard Worker             /core-oj.jar
69*795d594fSAndroid Build Coastguard Worker             ...
70*795d594fSAndroid Build Coastguard Worker         /com.android.i18n
71*795d594fSAndroid Build Coastguard Worker           /javalib
72*795d594fSAndroid Build Coastguard Worker             ...
73*795d594fSAndroid Build Coastguard Worker         /com.android.conscrypt
74*795d594fSAndroid Build Coastguard Worker           /javalib
75*795d594fSAndroid Build Coastguard Worker             ...
76*795d594fSAndroid Build Coastguard Worker       Required when running on host. Default on target: /
77*795d594fSAndroid Build Coastguard Worker   --profile-file=PROFILE_FILE: The path to the profile file. Required when running on host and
78*795d594fSAndroid Build Coastguard Worker       --use-profile is true. Default on target: /apex/com.android.art/etc/boot-image.prof
79*795d594fSAndroid Build Coastguard Worker   --instruction-set=ISA: The instruction set option to pass to dex2oat. Required when running on
80*795d594fSAndroid Build Coastguard Worker       host. The default on target is based on the ISA of this binary.
81*795d594fSAndroid Build Coastguard Worker   --core-only=true|false: If true, only compile ART jars. Otherwise, also compile core-icu4j and
82*795d594fSAndroid Build Coastguard Worker       conscrypt. Default: false
83*795d594fSAndroid Build Coastguard Worker   --: Arguments following '--' are directly passed to dex2oat.
84*795d594fSAndroid Build Coastguard Worker )";
85*795d594fSAndroid Build Coastguard Worker 
86*795d594fSAndroid Build Coastguard Worker struct Options {
87*795d594fSAndroid Build Coastguard Worker   std::string output_dir = "";
88*795d594fSAndroid Build Coastguard Worker   // Set the compiler filter to `verify` by default to make test preparation
89*795d594fSAndroid Build Coastguard Worker   // faster.
90*795d594fSAndroid Build Coastguard Worker   std::string compiler_filter = "verify";
91*795d594fSAndroid Build Coastguard Worker   bool use_profile = true;
92*795d594fSAndroid Build Coastguard Worker   std::string dex2oat_bin = "";
93*795d594fSAndroid Build Coastguard Worker   std::string android_root = "";
94*795d594fSAndroid Build Coastguard Worker   std::string profile_file = "";
95*795d594fSAndroid Build Coastguard Worker   std::string instruction_set = "";
96*795d594fSAndroid Build Coastguard Worker   bool core_only = false;
97*795d594fSAndroid Build Coastguard Worker   std::vector<std::string> dex2oat_options;
98*795d594fSAndroid Build Coastguard Worker };
99*795d594fSAndroid Build Coastguard Worker 
Usage(const std::string & message)100*795d594fSAndroid Build Coastguard Worker [[noreturn]] void Usage(const std::string& message) {
101*795d594fSAndroid Build Coastguard Worker   LOG(ERROR) << message << '\n';
102*795d594fSAndroid Build Coastguard Worker   std::cerr << message << "\n" << kUsage << "\n";
103*795d594fSAndroid Build Coastguard Worker   exit(EX_USAGE);
104*795d594fSAndroid Build Coastguard Worker }
105*795d594fSAndroid Build Coastguard Worker 
GetCompilerExecutable()106*795d594fSAndroid Build Coastguard Worker std::string GetCompilerExecutable() {
107*795d594fSAndroid Build Coastguard Worker   std::string compiler_executable = GetArtBinDir() + "/dex2oat";
108*795d594fSAndroid Build Coastguard Worker   if (kIsDebugBuild) {
109*795d594fSAndroid Build Coastguard Worker     compiler_executable += 'd';
110*795d594fSAndroid Build Coastguard Worker   }
111*795d594fSAndroid Build Coastguard Worker   compiler_executable += Is64BitInstructionSet(kRuntimeISA) ? "64" : "32";
112*795d594fSAndroid Build Coastguard Worker   return compiler_executable;
113*795d594fSAndroid Build Coastguard Worker }
114*795d594fSAndroid Build Coastguard Worker 
115*795d594fSAndroid Build Coastguard Worker // Joins a list of commandline args into a single string, where each part is quoted with double
116*795d594fSAndroid Build Coastguard Worker // quotes. Note that this is a naive implementation that does NOT escape existing double quotes,
117*795d594fSAndroid Build Coastguard Worker // which is fine since we don't have existing double quotes in the args in this particular use case
118*795d594fSAndroid Build Coastguard Worker // and this code is never used in production.
BuildCommand(const std::vector<std::string> & args)119*795d594fSAndroid Build Coastguard Worker std::string BuildCommand(const std::vector<std::string>& args) {
120*795d594fSAndroid Build Coastguard Worker   std::string command = "";
121*795d594fSAndroid Build Coastguard Worker   for (const std::string& arg : args) {
122*795d594fSAndroid Build Coastguard Worker     if (!command.empty()) {
123*795d594fSAndroid Build Coastguard Worker       command += " ";
124*795d594fSAndroid Build Coastguard Worker     }
125*795d594fSAndroid Build Coastguard Worker     command += '"' + arg + '"';
126*795d594fSAndroid Build Coastguard Worker   }
127*795d594fSAndroid Build Coastguard Worker   return command;
128*795d594fSAndroid Build Coastguard Worker }
129*795d594fSAndroid Build Coastguard Worker 
GenerateBootImage(const Options & options)130*795d594fSAndroid Build Coastguard Worker int GenerateBootImage(const Options& options) {
131*795d594fSAndroid Build Coastguard Worker   std::vector<std::string> args;
132*795d594fSAndroid Build Coastguard Worker   args.push_back(options.dex2oat_bin);
133*795d594fSAndroid Build Coastguard Worker 
134*795d594fSAndroid Build Coastguard Worker   std::vector<std::string> dex_files =
135*795d594fSAndroid Build Coastguard Worker       GetLibCoreDexFileNames(options.android_root, options.core_only);
136*795d594fSAndroid Build Coastguard Worker   std::vector<std::string> dex_locations = GetLibCoreDexLocations(options.core_only);
137*795d594fSAndroid Build Coastguard Worker   args.push_back("--runtime-arg");
138*795d594fSAndroid Build Coastguard Worker   args.push_back("-Xbootclasspath:" + Join(dex_files, ":"));
139*795d594fSAndroid Build Coastguard Worker   args.push_back("--runtime-arg");
140*795d594fSAndroid Build Coastguard Worker   args.push_back("-Xbootclasspath-locations:" + Join(dex_locations, ":"));
141*795d594fSAndroid Build Coastguard Worker   for (const std::string& file : dex_files) {
142*795d594fSAndroid Build Coastguard Worker     args.push_back("--dex-file=" + file);
143*795d594fSAndroid Build Coastguard Worker   }
144*795d594fSAndroid Build Coastguard Worker   for (const std::string& location : dex_locations) {
145*795d594fSAndroid Build Coastguard Worker     args.push_back("--dex-location=" + location);
146*795d594fSAndroid Build Coastguard Worker   }
147*795d594fSAndroid Build Coastguard Worker 
148*795d594fSAndroid Build Coastguard Worker   args.push_back("--instruction-set=" + options.instruction_set);
149*795d594fSAndroid Build Coastguard Worker   args.push_back(StringPrintf("--base=0x%08x", ART_BASE_ADDRESS));
150*795d594fSAndroid Build Coastguard Worker   args.push_back("--compiler-filter=" + options.compiler_filter);
151*795d594fSAndroid Build Coastguard Worker   if (options.use_profile) {
152*795d594fSAndroid Build Coastguard Worker     args.push_back("--profile-file=" + options.profile_file);
153*795d594fSAndroid Build Coastguard Worker   }
154*795d594fSAndroid Build Coastguard Worker   args.push_back("--avoid-storing-invocation");
155*795d594fSAndroid Build Coastguard Worker   args.push_back("--generate-debug-info");
156*795d594fSAndroid Build Coastguard Worker   args.push_back("--generate-build-id");
157*795d594fSAndroid Build Coastguard Worker   args.push_back("--image-format=lz4hc");
158*795d594fSAndroid Build Coastguard Worker   args.push_back("--strip");
159*795d594fSAndroid Build Coastguard Worker   args.push_back("--android-root=out/empty");
160*795d594fSAndroid Build Coastguard Worker 
161*795d594fSAndroid Build Coastguard Worker   std::string path = ART_FORMAT("{}/{}", options.output_dir, options.instruction_set);
162*795d594fSAndroid Build Coastguard Worker   if (!OS::DirectoryExists(path.c_str())) {
163*795d594fSAndroid Build Coastguard Worker     CHECK_EQ(mkdir(path.c_str(), S_IRWXU), 0);
164*795d594fSAndroid Build Coastguard Worker   }
165*795d594fSAndroid Build Coastguard Worker   args.push_back(StringPrintf("--image=%s/boot.art", path.c_str()));
166*795d594fSAndroid Build Coastguard Worker   args.push_back(StringPrintf("--oat-file=%s/boot.oat", path.c_str()));
167*795d594fSAndroid Build Coastguard Worker 
168*795d594fSAndroid Build Coastguard Worker   std::move(
169*795d594fSAndroid Build Coastguard Worker       options.dex2oat_options.begin(), options.dex2oat_options.end(), std::back_inserter(args));
170*795d594fSAndroid Build Coastguard Worker 
171*795d594fSAndroid Build Coastguard Worker   int exit_code = system(BuildCommand(args).c_str());
172*795d594fSAndroid Build Coastguard Worker   if (exit_code != 0) {
173*795d594fSAndroid Build Coastguard Worker     LOG(ERROR) << "dex2oat invocation failed. Exit code: " << exit_code;
174*795d594fSAndroid Build Coastguard Worker   }
175*795d594fSAndroid Build Coastguard Worker   return exit_code;
176*795d594fSAndroid Build Coastguard Worker }
177*795d594fSAndroid Build Coastguard Worker 
Main(int argc,char ** argv)178*795d594fSAndroid Build Coastguard Worker int Main(int argc, char** argv) {
179*795d594fSAndroid Build Coastguard Worker   android::base::InitLogging(argv, android::base::LogdLogger(android::base::SYSTEM));
180*795d594fSAndroid Build Coastguard Worker 
181*795d594fSAndroid Build Coastguard Worker   Options options;
182*795d594fSAndroid Build Coastguard Worker   for (int i = 1; i < argc; i++) {
183*795d594fSAndroid Build Coastguard Worker     std::string_view arg{argv[i]};
184*795d594fSAndroid Build Coastguard Worker     if (arg == "--help") {
185*795d594fSAndroid Build Coastguard Worker       std::cerr << kUsage << "\n";
186*795d594fSAndroid Build Coastguard Worker       exit(0);
187*795d594fSAndroid Build Coastguard Worker     } else if (ConsumePrefix(&arg, "--output-dir=")) {
188*795d594fSAndroid Build Coastguard Worker       options.output_dir = arg;
189*795d594fSAndroid Build Coastguard Worker     } else if (ConsumePrefix(&arg, "--compiler-filter=")) {
190*795d594fSAndroid Build Coastguard Worker       options.compiler_filter = arg;
191*795d594fSAndroid Build Coastguard Worker     } else if (ConsumePrefix(&arg, "--use-profile=")) {
192*795d594fSAndroid Build Coastguard Worker       ParseBoolResult result = ParseBool(arg);
193*795d594fSAndroid Build Coastguard Worker       if (result == ParseBoolResult::kError) {
194*795d594fSAndroid Build Coastguard Worker         Usage(ART_FORMAT("Unrecognized --use-profile value: '{}'", arg));
195*795d594fSAndroid Build Coastguard Worker       }
196*795d594fSAndroid Build Coastguard Worker       options.use_profile = result == ParseBoolResult::kTrue;
197*795d594fSAndroid Build Coastguard Worker     } else if (ConsumePrefix(&arg, "--dex2oat-bin=")) {
198*795d594fSAndroid Build Coastguard Worker       options.dex2oat_bin = arg;
199*795d594fSAndroid Build Coastguard Worker     } else if (ConsumePrefix(&arg, "--android-root=")) {
200*795d594fSAndroid Build Coastguard Worker       ConsumeSuffix(&arg, "/");
201*795d594fSAndroid Build Coastguard Worker       options.android_root = arg;
202*795d594fSAndroid Build Coastguard Worker     } else if (ConsumePrefix(&arg, "--profile-file=")) {
203*795d594fSAndroid Build Coastguard Worker       options.profile_file = arg;
204*795d594fSAndroid Build Coastguard Worker     } else if (ConsumePrefix(&arg, "--instruction-set=")) {
205*795d594fSAndroid Build Coastguard Worker       options.instruction_set = arg;
206*795d594fSAndroid Build Coastguard Worker     } else if (ConsumePrefix(&arg, "--core-only=")) {
207*795d594fSAndroid Build Coastguard Worker       ParseBoolResult result = ParseBool(arg);
208*795d594fSAndroid Build Coastguard Worker       if (result == ParseBoolResult::kError) {
209*795d594fSAndroid Build Coastguard Worker         Usage(ART_FORMAT("Unrecognized --core-only value: '{}'", arg));
210*795d594fSAndroid Build Coastguard Worker       }
211*795d594fSAndroid Build Coastguard Worker       options.core_only = result == ParseBoolResult::kTrue;
212*795d594fSAndroid Build Coastguard Worker     } else if (arg == "--") {
213*795d594fSAndroid Build Coastguard Worker       for (i++; i < argc; i++) {
214*795d594fSAndroid Build Coastguard Worker         options.dex2oat_options.push_back(argv[i]);
215*795d594fSAndroid Build Coastguard Worker       }
216*795d594fSAndroid Build Coastguard Worker     } else {
217*795d594fSAndroid Build Coastguard Worker       Usage(ART_FORMAT("Unrecognized argument: '{}'", argv[i]));
218*795d594fSAndroid Build Coastguard Worker     }
219*795d594fSAndroid Build Coastguard Worker   }
220*795d594fSAndroid Build Coastguard Worker 
221*795d594fSAndroid Build Coastguard Worker   if (options.output_dir.empty()) {
222*795d594fSAndroid Build Coastguard Worker     Usage("--output-dir must be specified");
223*795d594fSAndroid Build Coastguard Worker   }
224*795d594fSAndroid Build Coastguard Worker 
225*795d594fSAndroid Build Coastguard Worker   if (options.dex2oat_bin.empty()) {
226*795d594fSAndroid Build Coastguard Worker     if (kIsTargetBuild) {
227*795d594fSAndroid Build Coastguard Worker       options.dex2oat_bin = GetCompilerExecutable();
228*795d594fSAndroid Build Coastguard Worker     } else {
229*795d594fSAndroid Build Coastguard Worker       Usage("--dex2oat-bin must be specified when running on host");
230*795d594fSAndroid Build Coastguard Worker     }
231*795d594fSAndroid Build Coastguard Worker   }
232*795d594fSAndroid Build Coastguard Worker 
233*795d594fSAndroid Build Coastguard Worker   if (options.android_root.empty()) {
234*795d594fSAndroid Build Coastguard Worker     if (!kIsTargetBuild) {
235*795d594fSAndroid Build Coastguard Worker       Usage("--android-root must be specified when running on host");
236*795d594fSAndroid Build Coastguard Worker     }
237*795d594fSAndroid Build Coastguard Worker   }
238*795d594fSAndroid Build Coastguard Worker 
239*795d594fSAndroid Build Coastguard Worker   if (options.use_profile && options.profile_file.empty()) {
240*795d594fSAndroid Build Coastguard Worker     if (kIsTargetBuild) {
241*795d594fSAndroid Build Coastguard Worker       options.profile_file = ART_FORMAT("{}/etc/boot-image.prof", GetArtRoot());
242*795d594fSAndroid Build Coastguard Worker     } else {
243*795d594fSAndroid Build Coastguard Worker       Usage("--profile-file must be specified when running on host and --use-profile is true");
244*795d594fSAndroid Build Coastguard Worker     }
245*795d594fSAndroid Build Coastguard Worker   }
246*795d594fSAndroid Build Coastguard Worker 
247*795d594fSAndroid Build Coastguard Worker   if (options.instruction_set.empty()) {
248*795d594fSAndroid Build Coastguard Worker     if (kIsTargetBuild) {
249*795d594fSAndroid Build Coastguard Worker       options.instruction_set = GetInstructionSetString(kRuntimeISA);
250*795d594fSAndroid Build Coastguard Worker     } else {
251*795d594fSAndroid Build Coastguard Worker       Usage("--instruction-set must be specified when running on host");
252*795d594fSAndroid Build Coastguard Worker     }
253*795d594fSAndroid Build Coastguard Worker   }
254*795d594fSAndroid Build Coastguard Worker 
255*795d594fSAndroid Build Coastguard Worker   return GenerateBootImage(options);
256*795d594fSAndroid Build Coastguard Worker }
257*795d594fSAndroid Build Coastguard Worker 
258*795d594fSAndroid Build Coastguard Worker }  // namespace
259*795d594fSAndroid Build Coastguard Worker 
260*795d594fSAndroid Build Coastguard Worker }  // namespace gbi
261*795d594fSAndroid Build Coastguard Worker }  // namespace art
262*795d594fSAndroid Build Coastguard Worker 
main(int argc,char ** argv)263*795d594fSAndroid Build Coastguard Worker int main(int argc, char** argv) { return art::gbi::Main(argc, argv); }
264