diff options
author | Paul Duffin <paulduffin@google.com> | 2019-12-10 13:41:51 +0000 |
---|---|---|
committer | Paul Duffin <paulduffin@google.com> | 2019-12-20 08:13:45 +0000 |
commit | 9b478b083197297f7a08b795e1d392aa12d1c493 (patch) | |
tree | bcb4fe785c8003911a706f0f8f108bbf4544ee89 | |
parent | 120d73fe4fdfe463b5bacc413912c0b9ad2536a1 (diff) | |
download | build_soong-9b478b083197297f7a08b795e1d392aa12d1c493.tar.gz build_soong-9b478b083197297f7a08b795e1d392aa12d1c493.tar.bz2 build_soong-9b478b083197297f7a08b795e1d392aa12d1c493.zip |
Generate .srcjar for prebuilt_stubs_sources
Changes prebuilt_stubs_sources to generate a .srcjar from its input
instead of just exposing the srcs it is given. This ensures that it can
be used as a drop in replacement for a droidstubs module.
Updates the test for prebuilt_stubs_sources to be more representative
of the actual use made of it by sdk snapshot which outputs a directory
not a glob pattern. Added some documentation of the
prebuilts_stubs_sources srcs property to make it clear that it is
supposed to be a set of directories.
Extracts common code from sdk/testing.go for normalizing path/paths
for testing.
Bug: 143678475
Test: m conscrypt-module-sdk conscrypt-module-host-sdk conscrypt-module-test-sdk
unzip those in place of external/conscrypt
build core-current-stubs-source which expects it to provide a .srcjar.
Change-Id: I8204a022557a9b0b45e19eac79ecba98ff95213d
-rw-r--r-- | android/paths.go | 19 | ||||
-rw-r--r-- | android/testing.go | 31 | ||||
-rw-r--r-- | java/droiddoc.go | 38 | ||||
-rw-r--r-- | java/java_test.go | 28 | ||||
-rw-r--r-- | sdk/testing.go | 23 |
5 files changed, 102 insertions, 37 deletions
diff --git a/android/paths.go b/android/paths.go index 024432ea..c841372d 100644 --- a/android/paths.go +++ b/android/paths.go @@ -118,6 +118,9 @@ type Path interface { type WritablePath interface { Path + // return the path to the build directory. + buildDir() string + // the writablePath method doesn't directly do anything, // but it allows a struct to distinguish between whether or not it implements the WritablePath interface writablePath() @@ -848,7 +851,12 @@ func (p OutputPath) WithoutRel() OutputPath { return p } +func (p OutputPath) buildDir() string { + return p.config.buildDir +} + var _ Path = OutputPath{} +var _ WritablePath = OutputPath{} // PathForOutput joins the provided paths and returns an OutputPath that is // validated to not escape the build dir. @@ -1151,6 +1159,13 @@ type InstallPath struct { baseDir string // "../" for Make paths to convert "out/soong" to "out", "" for Soong paths } +func (p InstallPath) buildDir() string { + return p.config.buildDir +} + +var _ Path = InstallPath{} +var _ WritablePath = InstallPath{} + func (p InstallPath) writablePath() {} func (p InstallPath) String() string { @@ -1302,6 +1317,10 @@ type PhonyPath struct { func (p PhonyPath) writablePath() {} +func (p PhonyPath) buildDir() string { + return p.config.buildDir +} + var _ Path = PhonyPath{} var _ WritablePath = PhonyPath{} diff --git a/android/testing.go b/android/testing.go index aaf98f59..4f0591b1 100644 --- a/android/testing.go +++ b/android/testing.go @@ -16,6 +16,7 @@ package android import ( "fmt" + "path/filepath" "regexp" "strings" "testing" @@ -411,3 +412,33 @@ func AndroidMkDataForTest(t *testing.T, config Config, bpPath string, mod bluepr data.fillInData(config, bpPath, mod) return data } + +// Normalize the path for testing. +// +// If the path is relative to the build directory then return the relative path +// to avoid tests having to deal with the dynamically generated build directory. +// +// Otherwise, return the supplied path as it is almost certainly a source path +// that is relative to the root of the source tree. +// +// The build and source paths should be distinguishable based on their contents. +func NormalizePathForTesting(path Path) string { + p := path.String() + if w, ok := path.(WritablePath); ok { + rel, err := filepath.Rel(w.buildDir(), p) + if err != nil { + panic(err) + } + return rel + } + return p +} + +func NormalizePathsForTesting(paths Paths) []string { + var result []string + for _, path := range paths { + relative := NormalizePathForTesting(path) + result = append(result, relative) + } + return result +} diff --git a/java/droiddoc.go b/java/droiddoc.go index 3b7f772d..ff3f10a9 100644 --- a/java/droiddoc.go +++ b/java/droiddoc.go @@ -1948,12 +1948,42 @@ type PrebuiltStubsSources struct { properties PrebuiltStubsSourcesProperties - srcs android.Paths + // The source directories containing stubs source files. + srcDirs android.Paths stubsSrcJar android.ModuleOutPath } +func (p *PrebuiltStubsSources) OutputFiles(tag string) (android.Paths, error) { + switch tag { + case "": + return android.Paths{p.stubsSrcJar}, nil + default: + return nil, fmt.Errorf("unsupported module reference tag %q", tag) + } +} + func (p *PrebuiltStubsSources) GenerateAndroidBuildActions(ctx android.ModuleContext) { - p.srcs = android.PathsForModuleSrc(ctx, p.properties.Srcs) + p.stubsSrcJar = android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"stubs.srcjar") + + p.srcDirs = android.PathsForModuleSrc(ctx, p.properties.Srcs) + + rule := android.NewRuleBuilder() + command := rule.Command(). + BuiltTool(ctx, "soong_zip"). + Flag("-write_if_changed"). + Flag("-jar"). + FlagWithOutput("-o ", p.stubsSrcJar) + + for _, d := range p.srcDirs { + dir := d.String() + command. + FlagWithArg("-C ", dir). + FlagWithInput("-D ", d) + } + + rule.Restat() + + rule.Build(pctx, ctx, "zip src", "Create srcjar from prebuilt source") } func (p *PrebuiltStubsSources) Prebuilt() *android.Prebuilt { @@ -1964,10 +1994,6 @@ func (p *PrebuiltStubsSources) Name() string { return p.prebuilt.Name(p.ModuleBase.Name()) } -func (p *PrebuiltStubsSources) Srcs() android.Paths { - return append(android.Paths{}, p.srcs...) -} - // prebuilt_stubs_sources imports a set of java source files as if they were // generated by droidstubs. // diff --git a/java/java_test.go b/java/java_test.go index 096cdb90..2f67cdaa 100644 --- a/java/java_test.go +++ b/java/java_test.go @@ -484,28 +484,24 @@ func TestPrebuilts(t *testing.T) { prebuilt_stubs_sources { name: "stubs-source", - srcs: ["stubs/sources/**/*.java"], + srcs: ["stubs/sources"], } `) - javac := ctx.ModuleForTests("foo", "android_common").Rule("javac") + fooModule := ctx.ModuleForTests("foo", "android_common") + javac := fooModule.Rule("javac") combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac") barJar := ctx.ModuleForTests("bar", "android_common").Rule("combineJar").Output bazJar := ctx.ModuleForTests("baz", "android_common").Rule("combineJar").Output sdklibStubsJar := ctx.ModuleForTests("sdklib.stubs", "android_common").Rule("combineJar").Output - inputs := []string{} - for _, p := range javac.BuildParams.Inputs { - inputs = append(inputs, p.String()) - } + fooLibrary := fooModule.Module().(*Library) + assertDeepEquals(t, "foo java sources incorrect", + []string{"a.java"}, fooLibrary.compiledJavaSrcs.Strings()) - expected := []string{ - "a.java", - "stubs/sources/foo/Foo.java", - } - if !reflect.DeepEqual(expected, inputs) { - t.Errorf("foo inputs incorrect: expected %q, found %q", expected, inputs) - } + assertDeepEquals(t, "foo java source jars incorrect", + []string{".intermediates/stubs-source/android_common/stubs-source-stubs.srcjar"}, + android.NormalizePathsForTesting(fooLibrary.compiledSrcJars)) if !strings.Contains(javac.Args["classpath"], barJar.String()) { t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barJar.String()) @@ -522,6 +518,12 @@ func TestPrebuilts(t *testing.T) { ctx.ModuleForTests("qux", "android_common").Rule("Cp") } +func assertDeepEquals(t *testing.T, message string, expected interface{}, actual interface{}) { + if !reflect.DeepEqual(expected, actual) { + t.Errorf("%s: expected %q, found %q", message, expected, actual) + } +} + func TestDefaults(t *testing.T) { ctx, _ := testJava(t, ` java_defaults { diff --git a/sdk/testing.go b/sdk/testing.go index eec7f019..09d0247c 100644 --- a/sdk/testing.go +++ b/sdk/testing.go @@ -185,7 +185,7 @@ func (r *testSdkResult) getSdkSnapshotBuildInfo(sdk *sdk) *snapshotBuildInfo { switch bp.Rule.String() { case android.Cp.String(): // Get source relative to build directory. - src := r.pathRelativeToBuildDir(bp.Input) + src := android.NormalizePathForTesting(bp.Input) // Get destination relative to the snapshot root dest := bp.Output.Rel() _, _ = fmt.Fprintf(copyRules, "%s -> %s\n", src, dest) @@ -201,12 +201,12 @@ func (r *testSdkResult) getSdkSnapshotBuildInfo(sdk *sdk) *snapshotBuildInfo { // This could be an intermediate zip file and not the actual output zip. // In that case this will be overridden when the rule to merge the zips // is processed. - info.outputZip = r.pathRelativeToBuildDir(bp.Output) + info.outputZip = android.NormalizePathForTesting(bp.Output) case mergeZips.String(): // Copy the current outputZip to the intermediateZip. info.intermediateZip = info.outputZip - mergeInput := r.pathRelativeToBuildDir(bp.Input) + mergeInput := android.NormalizePathForTesting(bp.Input) if info.intermediateZip != mergeInput { r.t.Errorf("Expected intermediate zip %s to be an input to merge zips but found %s instead", info.intermediateZip, mergeInput) @@ -214,10 +214,10 @@ func (r *testSdkResult) getSdkSnapshotBuildInfo(sdk *sdk) *snapshotBuildInfo { // Override output zip (which was actually the intermediate zip file) with the actual // output zip. - info.outputZip = r.pathRelativeToBuildDir(bp.Output) + info.outputZip = android.NormalizePathForTesting(bp.Output) // Save the zips to be merged into the intermediate zip. - info.mergeZips = r.pathsRelativeToBuildDir(bp.Inputs) + info.mergeZips = android.NormalizePathsForTesting(bp.Inputs) } } @@ -234,19 +234,6 @@ func (r *testSdkResult) ModuleForTests(name string, variant string) android.Test return r.ctx.ModuleForTests(name, variant) } -func (r *testSdkResult) pathRelativeToBuildDir(path android.Path) string { - buildDir := filepath.Clean(r.config.BuildDir()) + "/" - return strings.TrimPrefix(filepath.Clean(path.String()), buildDir) -} - -func (r *testSdkResult) pathsRelativeToBuildDir(paths android.Paths) []string { - var result []string - for _, path := range paths { - result = append(result, r.pathRelativeToBuildDir(path)) - } - return result -} - // Check the snapshot build rules. // // Takes a list of functions which check different facets of the snapshot build rules. |