diff options
| -rw-r--r-- | startop/view_compiler/apk_layout_compiler.cc | 21 | ||||
| -rw-r--r-- | startop/view_compiler/apk_layout_compiler.h | 4 | ||||
| -rw-r--r-- | startop/view_compiler/main.cc | 24 |
3 files changed, 39 insertions, 10 deletions
diff --git a/startop/view_compiler/apk_layout_compiler.cc b/startop/view_compiler/apk_layout_compiler.cc index e95041ba34a4..09cdbd5fee58 100644 --- a/startop/view_compiler/apk_layout_compiler.cc +++ b/startop/view_compiler/apk_layout_compiler.cc @@ -79,9 +79,9 @@ bool CanCompileLayout(ResXMLParser* parser) { return visitor.can_compile(); } -void CompileApkLayouts(const std::string& filename, CompilationTarget target, - std::ostream& target_out) { - auto assets = android::ApkAssets::Load(filename); +namespace { +void CompileApkAssetsLayouts(const std::unique_ptr<const android::ApkAssets>& assets, + CompilationTarget target, std::ostream& target_out) { android::AssetManager2 resources; resources.SetApkAssets({assets.get()}); @@ -155,5 +155,20 @@ void CompileApkLayouts(const std::string& filename, CompilationTarget target, target_out.write(image.ptr<const char>(), image.size()); } } +} // namespace + +void CompileApkLayouts(const std::string& filename, CompilationTarget target, + std::ostream& target_out) { + auto assets = android::ApkAssets::Load(filename); + CompileApkAssetsLayouts(assets, target, target_out); +} + +void CompileApkLayoutsFd(android::base::unique_fd fd, CompilationTarget target, + std::ostream& target_out) { + constexpr const char* friendly_name{"viewcompiler assets"}; + auto assets = android::ApkAssets::LoadFromFd( + std::move(fd), friendly_name, /*system=*/false, /*force_shared_lib=*/false); + CompileApkAssetsLayouts(assets, target, target_out); +} } // namespace startop diff --git a/startop/view_compiler/apk_layout_compiler.h b/startop/view_compiler/apk_layout_compiler.h index c85ddd65ac1b..03bd545d9121 100644 --- a/startop/view_compiler/apk_layout_compiler.h +++ b/startop/view_compiler/apk_layout_compiler.h @@ -19,12 +19,16 @@ #include <string> +#include "android-base/unique_fd.h" + namespace startop { enum class CompilationTarget { kJavaLanguage, kDex }; void CompileApkLayouts(const std::string& filename, CompilationTarget target, std::ostream& target_out); +void CompileApkLayoutsFd(android::base::unique_fd fd, CompilationTarget target, + std::ostream& target_out); } // namespace startop diff --git a/startop/view_compiler/main.cc b/startop/view_compiler/main.cc index 871a421cee2d..11ecde27f5cd 100644 --- a/startop/view_compiler/main.cc +++ b/startop/view_compiler/main.cc @@ -49,6 +49,7 @@ constexpr char kStdoutFilename[]{"stdout"}; DEFINE_bool(apk, false, "Compile layouts in an APK"); DEFINE_bool(dex, false, "Generate a DEX file instead of Java"); +DEFINE_int32(infd, -1, "Read input from the given file descriptor"); DEFINE_string(out, kStdoutFilename, "Where to write the generated class"); DEFINE_string(package, "", "The package name for the generated class (required)"); @@ -95,7 +96,7 @@ void CompileLayout(XMLDocument* xml, Builder* builder) { int main(int argc, char** argv) { constexpr size_t kProgramName = 0; constexpr size_t kFileNameParam = 1; - constexpr size_t kNumRequiredArgs = 2; + constexpr size_t kNumRequiredArgs = 1; gflags::SetUsageMessage( "Compile XML layout files into equivalent Java language code\n" @@ -104,12 +105,11 @@ int main(int argc, char** argv) { gflags::ParseCommandLineFlags(&argc, &argv, /*remove_flags*/ true); gflags::CommandLineFlagInfo cmd = gflags::GetCommandLineFlagInfoOrDie("package"); - if (argc != kNumRequiredArgs || cmd.is_default) { + if (argc < kNumRequiredArgs || cmd.is_default) { gflags::ShowUsageWithFlags(argv[kProgramName]); return 1; } - const char* const filename = argv[kFileNameParam]; const bool is_stdout = FLAGS_out == kStdoutFilename; std::ofstream outfile; @@ -118,13 +118,23 @@ int main(int argc, char** argv) { } if (FLAGS_apk) { - startop::CompileApkLayouts( - filename, - FLAGS_dex ? startop::CompilationTarget::kDex : startop::CompilationTarget::kJavaLanguage, - is_stdout ? std::cout : outfile); + const startop::CompilationTarget target = + FLAGS_dex ? startop::CompilationTarget::kDex : startop::CompilationTarget::kJavaLanguage; + if (FLAGS_infd >= 0) { + startop::CompileApkLayoutsFd( + android::base::unique_fd{FLAGS_infd}, target, is_stdout ? std::cout : outfile); + } else { + if (argc < 2) { + gflags::ShowUsageWithFlags(argv[kProgramName]); + return 1; + } + const char* const filename = argv[kFileNameParam]; + startop::CompileApkLayouts(filename, target, is_stdout ? std::cout : outfile); + } return 0; } + const char* const filename = argv[kFileNameParam]; const string layout_name = startop::util::FindLayoutNameFromFilename(filename); XMLDocument xml; |