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 #include "format/Archive.h"
22 #include "process/IResourceTableConsumer.h"
23 #include "Command.h"
24 #include "Diagnostics.h"
25 #include "ResourceTable.h"
26 
27 namespace aapt {
28 
29 struct CompileOptions {
30   std::string output_path;
31   Maybe<std::string> source_path;
32   Maybe<std::string> res_dir;
33   Maybe<std::string> res_zip;
34   Maybe<std::string> generate_text_symbols_path;
35   Maybe<Visibility::Level> visibility;
36   bool pseudolocalize = false;
37   bool no_png_crunch = false;
38   bool legacy_mode = false;
39   // See comments on aapt::ResourceParserOptions.
40   bool preserve_visibility_of_styleables = false;
41   bool verbose = false;
42 };
43 
44 /** Parses flags and compiles resources to be used in linking.  */
45 class CompileCommand : public Command {
46  public:
CompileCommand(IDiagnostics * diagnostic)47   explicit CompileCommand(IDiagnostics* diagnostic) : Command("compile", "c"),
48                                                       diagnostic_(diagnostic) {
49     SetDescription("Compiles resources to be linked into an apk.");
50     AddRequiredFlag("-o", "Output path", &options_.output_path, Command::kPath);
51     AddOptionalFlag("--dir", "Directory to scan for resources", &options_.res_dir, Command::kPath);
52     AddOptionalFlag("--zip", "Zip file containing the res directory to scan for resources",
53         &options_.res_zip, Command::kPath);
54     AddOptionalFlag("--output-text-symbols",
55         "Generates a text file containing the resource symbols in the\n"
56             "specified file", &options_.generate_text_symbols_path, Command::kPath);
57     AddOptionalSwitch("--pseudo-localize", "Generate resources for pseudo-locales "
58         "(en-XA and ar-XB)", &options_.pseudolocalize);
59     AddOptionalSwitch("--no-crunch", "Disables PNG processing", &options_.no_png_crunch);
60     AddOptionalSwitch("--legacy", "Treat errors that used to be valid in AAPT as warnings",
61         &options_.legacy_mode);
62     AddOptionalSwitch("--preserve-visibility-of-styleables",
63                       "If specified, apply the same visibility rules for\n"
64                       "styleables as are used for all other resources.\n"
65                       "Otherwise, all stylesables will be made public.",
66                       &options_.preserve_visibility_of_styleables);
67     AddOptionalFlag("--visibility",
68         "Sets the visibility of the compiled resources to the specified\n"
69             "level. Accepted levels: public, private, default", &visibility_);
70     AddOptionalSwitch("-v", "Enables verbose logging", &options_.verbose);
71     AddOptionalFlag("--trace-folder", "Generate systrace json trace fragment to specified folder.",
72                     &trace_folder_);
73     AddOptionalFlag("--source-path",
74                       "Sets the compiled resource file source file path to the given string.",
75                       &options_.source_path);
76   }
77 
78   int Action(const std::vector<std::string>& args) override;
79 
80  private:
81   IDiagnostics* diagnostic_;
82   CompileOptions options_;
83   Maybe<std::string> visibility_;
84   Maybe<std::string> trace_folder_;
85 };
86 
87 int Compile(IAaptContext* context, io::IFileCollection* inputs, IArchiveWriter* output_writer,
88             CompileOptions& options);
89 
90 }// namespace aapt
91 
92 #endif //AAPT2_COMPILE_H
93