blob: 70c8791524c884809ae930b065aad07d5556d27e [file] [log] [blame]
Ryan Mitchell833a1a62018-07-10 13:51:36 -07001/*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
yd6b83292018-04-11 09:54:56 -070017#ifndef AAPT2_COMPILE_H
18#define AAPT2_COMPILE_H
19
Ryan Mitchell4382e442021-07-14 12:53:01 -070020#include <androidfw/StringPiece.h>
21
Jeremy Meyer56f36e82022-05-20 20:35:42 +000022#include <optional>
23
24#include "Command.h"
25#include "ResourceTable.h"
26#include "androidfw/IDiagnostics.h"
Jeremy Meyerb5c0ef02024-06-12 10:42:55 -070027#include "cmd/Util.h"
Ryan Mitchellf3649d62018-08-02 16:16:45 -070028#include "format/Archive.h"
29#include "process/IResourceTableConsumer.h"
yd6b83292018-04-11 09:54:56 -070030
31namespace aapt {
32
Ryan Mitchell833a1a62018-07-10 13:51:36 -070033struct CompileOptions {
34 std::string output_path;
Ryan Mitchell4382e442021-07-14 12:53:01 -070035 std::optional<std::string> source_path;
36 std::optional<std::string> res_dir;
37 std::optional<std::string> res_zip;
38 std::optional<std::string> generate_text_symbols_path;
Brandon Liu5274e062023-05-25 22:41:10 +000039 std::optional<std::string> pseudo_localize_gender_values;
40 std::optional<std::string> pseudo_localize_gender_ratio;
Ryan Mitchell4382e442021-07-14 12:53:01 -070041 std::optional<Visibility::Level> visibility;
Ryan Mitchell833a1a62018-07-10 13:51:36 -070042 bool pseudolocalize = false;
43 bool no_png_crunch = false;
44 bool legacy_mode = false;
Donald Chai94e4a012020-01-06 13:52:41 -080045 // See comments on aapt::ResourceParserOptions.
46 bool preserve_visibility_of_styleables = false;
Ryan Mitchell833a1a62018-07-10 13:51:36 -070047 bool verbose = false;
Inseob Kim5fe521e2023-09-15 16:38:50 +090048 std::optional<std::string> product_;
Jeremy Meyerb5c0ef02024-06-12 10:42:55 -070049 FeatureFlagValues feature_flag_values;
Ryan Mitchell833a1a62018-07-10 13:51:36 -070050};
51
Ryan Mitchellf3649d62018-08-02 16:16:45 -070052/** Parses flags and compiles resources to be used in linking. */
Ryan Mitchell833a1a62018-07-10 13:51:36 -070053class CompileCommand : public Command {
54 public:
Jeremy Meyer56f36e82022-05-20 20:35:42 +000055 explicit CompileCommand(android::IDiagnostics* diagnostic)
56 : Command("compile", "c"), diagnostic_(diagnostic) {
Ryan Mitchell833a1a62018-07-10 13:51:36 -070057 SetDescription("Compiles resources to be linked into an apk.");
Ryan Mitchell2c8fc862018-12-13 16:56:07 -080058 AddRequiredFlag("-o", "Output path", &options_.output_path, Command::kPath);
59 AddOptionalFlag("--dir", "Directory to scan for resources", &options_.res_dir, Command::kPath);
Ryan Mitchellf3649d62018-08-02 16:16:45 -070060 AddOptionalFlag("--zip", "Zip file containing the res directory to scan for resources",
Ryan Mitchell2c8fc862018-12-13 16:56:07 -080061 &options_.res_zip, Command::kPath);
Ryan Mitchell833a1a62018-07-10 13:51:36 -070062 AddOptionalFlag("--output-text-symbols",
63 "Generates a text file containing the resource symbols in the\n"
Ryan Mitchell2c8fc862018-12-13 16:56:07 -080064 "specified file", &options_.generate_text_symbols_path, Command::kPath);
Ryan Mitchell833a1a62018-07-10 13:51:36 -070065 AddOptionalSwitch("--pseudo-localize", "Generate resources for pseudo-locales "
66 "(en-XA and ar-XB)", &options_.pseudolocalize);
67 AddOptionalSwitch("--no-crunch", "Disables PNG processing", &options_.no_png_crunch);
68 AddOptionalSwitch("--legacy", "Treat errors that used to be valid in AAPT as warnings",
69 &options_.legacy_mode);
Donald Chai94e4a012020-01-06 13:52:41 -080070 AddOptionalSwitch("--preserve-visibility-of-styleables",
71 "If specified, apply the same visibility rules for\n"
72 "styleables as are used for all other resources.\n"
73 "Otherwise, all stylesables will be made public.",
74 &options_.preserve_visibility_of_styleables);
Ryan Mitchell833a1a62018-07-10 13:51:36 -070075 AddOptionalFlag("--visibility",
76 "Sets the visibility of the compiled resources to the specified\n"
77 "level. Accepted levels: public, private, default", &visibility_);
Ryan Mitchellf3649d62018-08-02 16:16:45 -070078 AddOptionalSwitch("-v", "Enables verbose logging", &options_.verbose);
Fabien Sanglard2d34e762019-02-21 15:13:29 -080079 AddOptionalFlag("--trace-folder", "Generate systrace json trace fragment to specified folder.",
80 &trace_folder_);
lukeedgar19b8ebf2020-06-23 10:43:42 +010081 AddOptionalFlag("--source-path",
82 "Sets the compiled resource file source file path to the given string.",
83 &options_.source_path);
Brandon Liu5274e062023-05-25 22:41:10 +000084 AddOptionalFlag("--pseudo-localize-gender-values",
85 "Sets the gender values to pick up for generating grammatical gender strings, "
86 "gender values should be f, m, or n, which are shortcuts for feminine, "
87 "masculine and neuter, and split with comma.",
88 &options_.pseudo_localize_gender_values);
89 AddOptionalFlag("--pseudo-localize-gender-ratio",
90 "Sets the ratio of resources to generate grammatical gender strings for. The "
91 "ratio has to be a float number between 0 and 1.",
92 &options_.pseudo_localize_gender_ratio);
Inseob Kim5fe521e2023-09-15 16:38:50 +090093 AddOptionalFlag("--filter-product",
94 "Leave only resources specific to the given product. All "
95 "other resources (including defaults) are removed.",
96 &options_.product_);
Jeremy Meyerb5c0ef02024-06-12 10:42:55 -070097 AddOptionalFlagList("--feature-flags",
98 "Specify the values of feature flags. The pairs in the argument\n"
99 "are separated by ',' the name is separated from the value by '='.\n"
100 "The name can have a suffix of ':ro' to indicate it is read only."
101 "Example: \"flag1=true,flag2:ro=false,flag3=\" (flag3 has no given value).",
102 &feature_flags_args_);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700103 }
104
105 int Action(const std::vector<std::string>& args) override;
106
107 private:
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000108 android::IDiagnostics* diagnostic_;
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700109 CompileOptions options_;
Ryan Mitchell4382e442021-07-14 12:53:01 -0700110 std::optional<std::string> visibility_;
111 std::optional<std::string> trace_folder_;
Jeremy Meyerb5c0ef02024-06-12 10:42:55 -0700112 std::vector<std::string> feature_flags_args_;
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700113};
yd6b83292018-04-11 09:54:56 -0700114
Ryan Mitchell2c8fc862018-12-13 16:56:07 -0800115int Compile(IAaptContext* context, io::IFileCollection* inputs, IArchiveWriter* output_writer,
116 CompileOptions& options);
117
yd6b83292018-04-11 09:54:56 -0700118}// namespace aapt
119
120#endif //AAPT2_COMPILE_H