Strip relative paths from java_import output files

androidx.annotation_annotation is used as a test data file, and
converting it from a java_library to a java_library_import causes
the relative path used in the test data path to change. Clear the
relative path in java_import the same way that other java based
modules do.

Bug: 288358614
Test: TestJavaLibraryOutputFileRel
Change-Id: I1f494110da32e916043ca94ac6ebeeafccc06f9a
This commit is contained in:
Colin Cross 2024-04-11 15:28:18 -07:00
parent 28ac2ffc80
commit 5e87f349b8
3 changed files with 57 additions and 8 deletions

View file

@ -16,7 +16,7 @@ package java
import ( import (
"android/soong/android" "android/soong/android"
"reflect" "slices"
"strings" "strings"
"testing" "testing"
) )
@ -84,7 +84,7 @@ func TestDeviceForHost(t *testing.T) {
deviceImportCombined.Output, deviceImportCombined.Output,
} }
if !reflect.DeepEqual(combined.Inputs, expectedInputs) { if !slices.Equal(combined.Inputs.Strings(), expectedInputs.Strings()) {
t.Errorf("expected host_module combined inputs:\n%q\ngot:\n%q", t.Errorf("expected host_module combined inputs:\n%q\ngot:\n%q",
expectedInputs, combined.Inputs) expectedInputs, combined.Inputs)
} }
@ -95,7 +95,7 @@ func TestDeviceForHost(t *testing.T) {
deviceRes.Output, deviceRes.Output,
} }
if !reflect.DeepEqual(resCombined.Inputs, expectedInputs) { if !slices.Equal(resCombined.Inputs.Strings(), expectedInputs.Strings()) {
t.Errorf("expected host_module res combined inputs:\n%q\ngot:\n%q", t.Errorf("expected host_module res combined inputs:\n%q\ngot:\n%q",
expectedInputs, resCombined.Inputs) expectedInputs, resCombined.Inputs)
} }
@ -165,7 +165,7 @@ func TestHostForDevice(t *testing.T) {
hostImportCombined.Output, hostImportCombined.Output,
} }
if !reflect.DeepEqual(combined.Inputs, expectedInputs) { if !slices.Equal(combined.Inputs.Strings(), expectedInputs.Strings()) {
t.Errorf("expected device_module combined inputs:\n%q\ngot:\n%q", t.Errorf("expected device_module combined inputs:\n%q\ngot:\n%q",
expectedInputs, combined.Inputs) expectedInputs, combined.Inputs)
} }
@ -176,7 +176,7 @@ func TestHostForDevice(t *testing.T) {
hostRes.Output, hostRes.Output,
} }
if !reflect.DeepEqual(resCombined.Inputs, expectedInputs) { if !slices.Equal(resCombined.Inputs.Strings(), expectedInputs.Strings()) {
t.Errorf("expected device_module res combined inputs:\n%q\ngot:\n%q", t.Errorf("expected device_module res combined inputs:\n%q\ngot:\n%q",
expectedInputs, resCombined.Inputs) expectedInputs, resCombined.Inputs)
} }

View file

@ -2555,7 +2555,7 @@ func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
// header jar for this module. // header jar for this module.
reuseImplementationJarAsHeaderJar := slices.Equal(staticJars, staticHeaderJars) reuseImplementationJarAsHeaderJar := slices.Equal(staticJars, staticHeaderJars)
var headerOutputFile android.WritablePath var headerOutputFile android.ModuleOutPath
if reuseImplementationJarAsHeaderJar { if reuseImplementationJarAsHeaderJar {
headerOutputFile = outputFile headerOutputFile = outputFile
} else { } else {
@ -2578,8 +2578,12 @@ func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
headerOutputFile = outputFile headerOutputFile = outputFile
} }
} }
j.combinedHeaderFile = headerOutputFile
j.combinedImplementationFile = outputFile // Save the output file with no relative path so that it doesn't end up in a subdirectory when used as a resource.
// Also strip the relative path from the header output file so that the reuseImplementationJarAsHeaderJar check
// in a module that depends on this module considers them equal.
j.combinedHeaderFile = headerOutputFile.WithoutRel()
j.combinedImplementationFile = outputFile.WithoutRel()
j.maybeInstall(ctx, jarName, outputFile) j.maybeInstall(ctx, jarName, outputFile)

View file

@ -2872,3 +2872,48 @@ func TestJavaLibWithStem(t *testing.T) {
t.Errorf("Module output does not contain expected jar %s", "foo-new.jar") t.Errorf("Module output does not contain expected jar %s", "foo-new.jar")
} }
} }
func TestJavaLibraryOutputFilesRel(t *testing.T) {
result := android.GroupFixturePreparers(
PrepareForTestWithJavaDefaultModules,
).RunTestWithBp(t, `
java_library {
name: "foo",
srcs: ["a.java"],
}
java_import {
name: "bar",
jars: ["bar.aar"],
}
java_import {
name: "baz",
jars: ["baz.aar"],
static_libs: ["bar"],
}
`)
foo := result.ModuleForTests("foo", "android_common")
bar := result.ModuleForTests("bar", "android_common")
baz := result.ModuleForTests("baz", "android_common")
fooOutputPath := android.OutputFileForModule(android.PathContext(nil), foo.Module(), "")
barOutputPath := android.OutputFileForModule(android.PathContext(nil), bar.Module(), "")
bazOutputPath := android.OutputFileForModule(android.PathContext(nil), baz.Module(), "")
android.AssertPathRelativeToTopEquals(t, "foo output path",
"out/soong/.intermediates/foo/android_common/javac/foo.jar", fooOutputPath)
android.AssertPathRelativeToTopEquals(t, "bar output path",
"out/soong/.intermediates/bar/android_common/combined/bar.jar", barOutputPath)
android.AssertPathRelativeToTopEquals(t, "baz output path",
"out/soong/.intermediates/baz/android_common/combined/baz.jar", bazOutputPath)
android.AssertStringEquals(t, "foo relative output path",
"foo.jar", fooOutputPath.Rel())
android.AssertStringEquals(t, "bar relative output path",
"bar.jar", barOutputPath.Rel())
android.AssertStringEquals(t, "baz relative output path",
"baz.jar", bazOutputPath.Rel())
}