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

Commit 02e8b4da authored by Makoto Onuki's avatar Makoto Onuki Committed by Android (Google) Code Review
Browse files

Merge "HostStubGen: Stub generation is now optional, etc" into main

parents 8bec254e c8a04dca
Loading
Loading
Loading
Loading
+0 −2
Original line number Diff line number Diff line
@@ -32,7 +32,6 @@ java_genrule {
    cmd: "$(location hoststubgen) " +
        "@$(location ravenwood/ravenwood-standard-options.txt) " +

        "--out-stub-jar $(location ravenwood_stub.jar) " +
        "--out-impl-jar $(location ravenwood.jar) " +

        "--gen-keep-all-file $(location hoststubgen_keep_all.txt) " +
@@ -49,7 +48,6 @@ java_genrule {
    ],
    out: [
        "ravenwood.jar",
        "ravenwood_stub.jar", // It's not used. TODO: Update hoststubgen to make it optional.

        // Following files are created just as FYI.
        "hoststubgen_keep_all.txt",
+4 −3
Original line number Diff line number Diff line
@@ -27,9 +27,10 @@ import java.util.concurrent.atomic.AtomicLong;

/**
 * Tentative, partial implementation of the Parcel native methods, using Java's
 * {@link ByteBuffer}. It turned out there's enough semantics differences between Parcel
 * and {@link ByteBuffer}, so it didn't actually work.
 * (e.g. Parcel seems to allow moving the data position to be beyond its size? Which
 * {@code byte[]}.
 * (We don't use a {@link ByteBuffer} because there's enough semantics differences between Parcel
 * and {@link ByteBuffer}, and it didn't work out.
 * e.g. Parcel seems to allow moving the data position to be beyond its size? Which
 * {@link ByteBuffer} wouldn't allow...)
 */
public class Parcel_host {
+45 −6
Original line number Diff line number Diff line
@@ -13,6 +13,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# This command is expected to be executed with: atest hoststubgen-invoke-test

set -e # Exit when any command files

# This script runs HostStubGen directly with various arguments and make sure
@@ -35,6 +37,12 @@ if [[ "$TEMP" == "" ]] ; then
  mkdir -p $TEMP
fi

cleanup_temp() {
  rm -fr $TEMP/*
}

cleanup_temp

JAR=hoststubgen-test-tiny-framework.jar
STUB=$TEMP/stub.jar
IMPL=$TEMP/impl.jar
@@ -47,12 +55,10 @@ HOSTSTUBGEN_OUT=$TEMP/output.txt
# HostStubGen result in it.
HOSTSTUBGEN_RC=0

# Define the functions to


# Note, because the build rule will only install hoststubgen.jar, but not the wrapper script,
# we need to execute it manually with the java command.
hoststubgen() {
  echo "Running hoststubgen with: $*"
  java -jar ./hoststubgen.jar "$@"
}

@@ -62,7 +68,7 @@ run_hoststubgen() {

  echo "# Test: $test_name"

  rm -f $HOSTSTUBGEN_OUT
  cleanup_temp

  local filter_arg=""

@@ -73,11 +79,21 @@ run_hoststubgen() {
    cat $ANNOTATION_FILTER
  fi

  local stub_arg=""
  local impl_arg=""

  if [[ "$STUB" != "" ]] ; then
    stub_arg="--out-stub-jar $STUB"
  fi
  if [[ "$IMPL" != "" ]] ; then
    impl_arg="--out-impl-jar $IMPL"
  fi

  hoststubgen \
      --debug \
      --in-jar $JAR \
      --out-stub-jar $STUB \
      --out-impl-jar $IMPL \
      $stub_arg \
      $impl_arg \
      --stub-annotation \
          android.hosttest.annotation.HostSideTestStub \
      --keep-annotation \
@@ -105,6 +121,21 @@ run_hoststubgen() {
  return 0
}

assert_file_generated() {
  local file="$1"
  if [[ "$file" == "" ]] ; then
    if [[ -f "$file" ]] ; then
      echo "HostStubGen shouldn't have generated $file"
      return 1
    fi
  else
    if ! [[ -f "$file" ]] ; then
      echo "HostStubGen didn't generate $file"
      return 1
    fi
  fi
}

run_hoststubgen_for_success() {
  run_hoststubgen "$@"

@@ -112,6 +143,9 @@ run_hoststubgen_for_success() {
    echo "HostStubGen expected to finish successfully, but failed with $rc"
    return 1
  fi

  assert_file_generated "$STUB"
  assert_file_generated "$IMPL"
}

run_hoststubgen_for_failure() {
@@ -189,6 +223,11 @@ run_hoststubgen_for_success "One specific class disallowed, but it doesn't use a
* # All other classes allowed
"

STUB="" run_hoststubgen_for_success "No stub generation" ""

IMPL="" run_hoststubgen_for_success "No impl generation" ""

STUB="" IMPL="" run_hoststubgen_for_success "No stub, no impl generation" ""


echo "All tests passed"
+22 −15
Original line number Diff line number Diff line
@@ -237,8 +237,8 @@ class HostStubGen(val options: HostStubGenOptions) {
     */
    private fun convert(
            inJar: String,
            outStubJar: String,
            outImplJar: String,
            outStubJar: String?,
            outImplJar: String?,
            filter: OutputFilter,
            enableChecker: Boolean,
            classes: ClassNodes,
@@ -254,8 +254,8 @@ class HostStubGen(val options: HostStubGenOptions) {
        log.withIndent {
            // Open the input jar file and process each entry.
            ZipFile(inJar).use { inZip ->
                ZipOutputStream(FileOutputStream(outStubJar)).use { stubOutStream ->
                    ZipOutputStream(FileOutputStream(outImplJar)).use { implOutStream ->
                maybeWithZipOutputStream(outStubJar) { stubOutStream ->
                    maybeWithZipOutputStream(outImplJar) { implOutStream ->
                        val inEntries = inZip.entries()
                        while (inEntries.hasMoreElements()) {
                            val entry = inEntries.nextElement()
@@ -265,22 +265,29 @@ class HostStubGen(val options: HostStubGenOptions) {
                        log.i("Converted all entries.")
                    }
                }
                log.i("Created stub: $outStubJar")
                log.i("Created impl: $outImplJar")
                outStubJar?.let { log.i("Created stub: $it") }
                outImplJar?.let { log.i("Created impl: $it") }
            }
        }
        val end = System.currentTimeMillis()
        log.v("Done transforming the jar in %.1f second(s).", (end - start) / 1000.0)
    }

    private fun <T> maybeWithZipOutputStream(filename: String?, block: (ZipOutputStream?) -> T): T {
        if (filename == null) {
            return block(null)
        }
        return ZipOutputStream(FileOutputStream(filename)).use(block)
    }

    /**
     * Convert a single ZIP entry, which may or may not be a class file.
     */
    private fun convertSingleEntry(
            inZip: ZipFile,
            entry: ZipEntry,
            stubOutStream: ZipOutputStream,
            implOutStream: ZipOutputStream,
            stubOutStream: ZipOutputStream?,
            implOutStream: ZipOutputStream?,
            filter: OutputFilter,
            packageRedirector: PackageRedirectRemapper,
            enableChecker: Boolean,
@@ -316,8 +323,8 @@ class HostStubGen(val options: HostStubGenOptions) {
            // Unknown type, we just copy it to both output zip files.
            // TODO: We probably shouldn't do it for stub jar?
            log.v("Copying: %s", entry.name)
            copyZipEntry(inZip, entry, stubOutStream)
            copyZipEntry(inZip, entry, implOutStream)
            stubOutStream?.let { copyZipEntry(inZip, entry, it) }
            implOutStream?.let { copyZipEntry(inZip, entry, it) }
        }
    }

@@ -346,8 +353,8 @@ class HostStubGen(val options: HostStubGenOptions) {
    private fun processSingleClass(
            inZip: ZipFile,
            entry: ZipEntry,
            stubOutStream: ZipOutputStream,
            implOutStream: ZipOutputStream,
            stubOutStream: ZipOutputStream?,
            implOutStream: ZipOutputStream?,
            filter: OutputFilter,
            packageRedirector: PackageRedirectRemapper,
            enableChecker: Boolean,
@@ -361,7 +368,7 @@ class HostStubGen(val options: HostStubGenOptions) {
            return
        }
        // Generate stub first.
        if (classPolicy.policy.needsInStub) {
        if (stubOutStream != null && classPolicy.policy.needsInStub) {
            log.v("Creating stub class: %s Policy: %s", classInternalName, classPolicy)
            log.withIndent {
                BufferedInputStream(inZip.getInputStream(entry)).use { bis ->
@@ -374,8 +381,8 @@ class HostStubGen(val options: HostStubGenOptions) {
                }
            }
        }
        if (implOutStream != null && classPolicy.policy.needsInImpl) {
            log.v("Creating impl class: %s Policy: %s", classInternalName, classPolicy)
        if (classPolicy.policy.needsInImpl) {
            log.withIndent {
                BufferedInputStream(inZip.getInputStream(entry)).use { bis ->
                    val newEntry = ZipEntry(entry.name)
+10 −7
Original line number Diff line number Diff line
@@ -28,10 +28,10 @@ class HostStubGenOptions(
        var inJar: String = "",

        /** Output stub jar file */
        var outStubJar: String = "",
        var outStubJar: String? = null,

        /** Output implementation jar file */
        var outImplJar: String = "",
        var outImplJar: String? = null,

        var inputJarDumpFile: String? = null,

@@ -71,7 +71,7 @@ class HostStubGenOptions(
        var enablePreTrace: Boolean = false,
        var enablePostTrace: Boolean = false,

        var enableNonStubMethodCallDetection: Boolean = true,
        var enableNonStubMethodCallDetection: Boolean = false,
) {
    companion object {

@@ -209,11 +209,14 @@ class HostStubGenOptions(
            if (ret.inJar.isEmpty()) {
                throw ArgumentsException("Required option missing: --in-jar")
            }
            if (ret.outStubJar.isEmpty()) {
                throw ArgumentsException("Required option missing: --out-stub-jar")
            if (ret.outStubJar == null && ret.outImplJar == null) {
                log.w("Neither --out-stub-jar nor --out-impl-jar is set." +
                        " $COMMAND_NAME will not generate jar files.")
            }
            if (ret.outImplJar.isEmpty()) {
                throw ArgumentsException("Required option missing: --out-impl-jar")

            if (ret.enableNonStubMethodCallDetection) {
                log.w("--enable-non-stub-method-check is not fully implemented yet." +
                    " See the todo in doesMethodNeedNonStubCallCheck().")
            }

            return ret
Loading