Merge "[viewcompiler] Enable input from file descriptor" am: 040579c6a3

am: 88f9ca38fb

Change-Id: I828cd906b95daac5f74d2cc0289c211ade0be1d9
This commit is contained in:
Eric Holk
2019-01-15 14:00:35 -08:00
committed by android-build-merger
3 changed files with 39 additions and 10 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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;