xref: /aosp_15_r20/frameworks/base/tools/aapt2/cmd/Compile.h (revision d57664e9bc4670b3ecf6748a746a57c557b6bc9e)
1 /*
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 
17 #ifndef AAPT2_COMPILE_H
18 #define AAPT2_COMPILE_H
19 
20 #include <androidfw/StringPiece.h>
21 
22 #include <optional>
23 
24 #include "Command.h"
25 #include "ResourceTable.h"
26 #include "androidfw/IDiagnostics.h"
27 #include "cmd/Util.h"
28 #include "format/Archive.h"
29 #include "process/IResourceTableConsumer.h"
30 
31 namespace aapt {
32 
33 struct CompileOptions {
34   std::string output_path;
35   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;
39   std::optional<std::string> pseudo_localize_gender_values;
40   std::optional<std::string> pseudo_localize_gender_ratio;
41   std::optional<Visibility::Level> visibility;
42   bool pseudolocalize = false;
43   bool no_png_crunch = false;
44   bool legacy_mode = false;
45   // See comments on aapt::ResourceParserOptions.
46   bool preserve_visibility_of_styleables = false;
47   bool verbose = false;
48   std::optional<std::string> product_;
49   FeatureFlagValues feature_flag_values;
50   std::optional<std::string> png_compression_level;
51   int png_compression_level_int = 9;
52 };
53 
54 /** Parses flags and compiles resources to be used in linking.  */
55 class CompileCommand : public Command {
56  public:
CompileCommand(android::IDiagnostics * diagnostic)57   explicit CompileCommand(android::IDiagnostics* diagnostic)
58       : Command("compile", "c"), diagnostic_(diagnostic) {
59     SetDescription("Compiles resources to be linked into an apk.");
60     AddRequiredFlag("-o", "Output path", &options_.output_path, Command::kPath);
61     AddOptionalFlag("--dir", "Directory to scan for resources", &options_.res_dir, Command::kPath);
62     AddOptionalFlag("--zip", "Zip file containing the res directory to scan for resources",
63         &options_.res_zip, Command::kPath);
64     AddOptionalFlag("--output-text-symbols",
65         "Generates a text file containing the resource symbols in the\n"
66             "specified file", &options_.generate_text_symbols_path, Command::kPath);
67     AddOptionalSwitch("--pseudo-localize", "Generate resources for pseudo-locales "
68         "(en-XA and ar-XB)", &options_.pseudolocalize);
69     AddOptionalSwitch("--no-crunch", "Disables PNG processing", &options_.no_png_crunch);
70     AddOptionalFlag("--png-compression-level",
71                     "Set the zlib compression level for crunched PNG images, [0-9], 9 by default.",
72                     &options_.png_compression_level);
73     AddOptionalSwitch("--legacy", "Treat errors that used to be valid in AAPT as warnings",
74         &options_.legacy_mode);
75     AddOptionalSwitch("--preserve-visibility-of-styleables",
76                       "If specified, apply the same visibility rules for\n"
77                       "styleables as are used for all other resources.\n"
78                       "Otherwise, all stylesables will be made public.",
79                       &options_.preserve_visibility_of_styleables);
80     AddOptionalFlag("--visibility",
81         "Sets the visibility of the compiled resources to the specified\n"
82             "level. Accepted levels: public, private, default", &visibility_);
83     AddOptionalSwitch("-v", "Enables verbose logging", &options_.verbose);
84     AddOptionalFlag("--trace-folder", "Generate systrace json trace fragment to specified folder.",
85                     &trace_folder_);
86     AddOptionalFlag("--source-path",
87                       "Sets the compiled resource file source file path to the given string.",
88                       &options_.source_path);
89     AddOptionalFlag("--pseudo-localize-gender-values",
90                     "Sets the gender values to pick up for generating grammatical gender strings, "
91                     "gender values should be f, m, or n, which are shortcuts for feminine, "
92                     "masculine and neuter, and split with comma.",
93                     &options_.pseudo_localize_gender_values);
94     AddOptionalFlag("--pseudo-localize-gender-ratio",
95                     "Sets the ratio of resources to generate grammatical gender strings for. The "
96                     "ratio has to be a float number between 0 and 1.",
97                     &options_.pseudo_localize_gender_ratio);
98     AddOptionalFlag("--filter-product",
99                     "Leave only resources specific to the given product. All "
100                     "other resources (including defaults) are removed.",
101                     &options_.product_);
102     AddOptionalFlagList("--feature-flags",
103                         "Specify the values of feature flags. The pairs in the argument\n"
104                         "are separated by ',' the name is separated from the value by '='.\n"
105                         "The name can have a suffix of ':ro' to indicate it is read only."
106                         "Example: \"flag1=true,flag2:ro=false,flag3=\" (flag3 has no given value).",
107                         &feature_flags_args_);
108   }
109 
110   int Action(const std::vector<std::string>& args) override;
111 
112  private:
113   android::IDiagnostics* diagnostic_;
114   CompileOptions options_;
115   std::optional<std::string> visibility_;
116   std::optional<std::string> trace_folder_;
117   std::vector<std::string> feature_flags_args_;
118 };
119 
120 int Compile(IAaptContext* context, io::IFileCollection* inputs, IArchiveWriter* output_writer,
121             CompileOptions& options);
122 
123 }// namespace aapt
124 
125 #endif //AAPT2_COMPILE_H
126