Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit fa36e4a2 authored by Yurii Zubrytskyi's avatar Yurii Zubrytskyi Committed by Android (Google) Code Review
Browse files

Merge changes Ibe5f47b8,I845f6730 into main

* changes:
  [aapt2] Add a compression control option to 'compile'
  [aapt2] Allow --flag=value command line options
parents b117f67c c9414082
Loading
Loading
Loading
Loading
+1 −2
Original line number Diff line number Diff line
@@ -506,8 +506,7 @@ bool WritePng(const Image* image, const NinePatch* nine_patch, OutputStream* out
  // Set up the write functions which write to our custom data sources.
  png_set_write_fn(write_ptr, (png_voidp)out, WriteDataToStream, nullptr);

  // We want small files and can take the performance hit to achieve this goal.
  png_set_compression_level(write_ptr, Z_BEST_COMPRESSION);
  png_set_compression_level(write_ptr, options.compression_level);

  // Begin analysis of the image data.
  // Scan the entire image and determine if:
+2 −0
Original line number Diff line number Diff line
@@ -31,6 +31,8 @@ constexpr size_t kPngSignatureSize = 8u;

struct PngOptions {
  int grayscale_tolerance = 0;
  // By default we want small files and can take the performance hit to achieve this goal.
  int compression_level = 9;
};

/**
+20 −7
Original line number Diff line number Diff line
@@ -213,15 +213,28 @@ int Command::Execute(const std::vector<StringPiece>& args, std::ostream* out_err

    bool match = false;
    for (Flag& flag : flags_) {
      if (arg == flag.name) {
      // Allow both "--arg value" and "--arg=value" syntax.
      if (arg.starts_with(flag.name) &&
          (arg.size() == flag.name.size() || (flag.num_args > 0 && arg[flag.name.size()] == '='))) {
        if (flag.num_args > 0) {
          if (arg.size() == flag.name.size()) {
            i++;
            if (i >= args.size()) {
              *out_error << flag.name << " missing argument.\n\n";
              Usage(out_error);
            return false;
              return 1;
            }
            arg = args[i];
          } else {
            arg.remove_prefix(flag.name.size() + 1);
            // Disallow empty arguments after '='.
            if (arg.empty()) {
              *out_error << flag.name << " has empty argument.\n\n";
              Usage(out_error);
              return 1;
            }
          }
          flag.action(args[i]);
          flag.action(arg);
        } else {
          flag.action({});
        }
+24 −0
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@
#include "test/Test.h"

using ::testing::Eq;
using namespace std::literals;

namespace aapt {

@@ -94,4 +95,27 @@ TEST(CommandTest, LongFullyQualifiedPathWindows) {
}
#endif

TEST(CommandTest, OptionsWithValues) {
  TestCommand command;
  std::string flag;
  command.AddRequiredFlag("--flag", "", &flag);

  ASSERT_EQ(0, command.Execute({"--flag"s, "1"s}, &std::cerr));
  EXPECT_STREQ("1", flag.c_str());

  ASSERT_EQ(0, command.Execute({"--flag=1"s}, &std::cerr));
  EXPECT_STREQ("1", flag.c_str());

  ASSERT_EQ(0, command.Execute({"--flag"s, "=2"s}, &std::cerr));
  EXPECT_STREQ("=2", flag.c_str());

  ASSERT_EQ(0, command.Execute({"--flag"s, "--flag"s}, &std::cerr));
  EXPECT_STREQ("--flag", flag.c_str());

  EXPECT_NE(0, command.Execute({"--flag"s}, &std::cerr));
  EXPECT_NE(0, command.Execute({"--flag="s}, &std::cerr));
  EXPECT_NE(0, command.Execute({"--flag1=2"s}, &std::cerr));
  EXPECT_NE(0, command.Execute({"--flag1"s, "2"s}, &std::cerr));
}

}  // namespace aapt
 No newline at end of file
+16 −2
Original line number Diff line number Diff line
@@ -605,8 +605,9 @@ static bool CompilePng(IAaptContext* context, const CompileOptions& options,
    }

    // Write the crunched PNG.
    if (!android::WritePng(image.get(), nine_patch.get(), &crunched_png_buffer_out, {},
                           &source_diag, context->IsVerbose())) {
    if (!android::WritePng(image.get(), nine_patch.get(), &crunched_png_buffer_out,
                           {.compression_level = options.png_compression_level_int}, &source_diag,
                           context->IsVerbose())) {
      return false;
    }

@@ -924,6 +925,19 @@ int CompileCommand::Action(const std::vector<std::string>& args) {
    }
  }

  if (!options_.png_compression_level) {
    options_.png_compression_level_int = 9;
  } else {
    if (options_.png_compression_level->size() != 1 ||
        options_.png_compression_level->front() < '0' ||
        options_.png_compression_level->front() > '9') {
      context.GetDiagnostics()->Error(
          android::DiagMessage() << "PNG compression level should be a number in [0..9] range");
      return 1;
    }
    options_.png_compression_level_int = options_.png_compression_level->front() - '0';
  }

  return Compile(&context, file_collection.get(), archive_writer.get(), options_);
}

Loading