blob: dbcd48c792449b5fe852f30cf31aa8f03aec0bba [file] [log] [blame]
Colin Cross8eebb132020-01-29 20:07:03 -08001// Copyright 2020 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package android
16
Weijia He299d62d2024-04-18 22:55:10 +000017import (
18 "path/filepath"
19 "strings"
Yu Liu367827f2025-02-15 00:18:33 +000020
21 "github.com/google/blueprint"
Weijia He299d62d2024-04-18 22:55:10 +000022)
23
Colin Cross8eebb132020-01-29 20:07:03 -080024func init() {
LaMont Jones0c10e4d2023-05-16 00:58:37 +000025 RegisterParallelSingletonType("testsuites", testSuiteFilesFactory)
Colin Cross8eebb132020-01-29 20:07:03 -080026}
27
28func testSuiteFilesFactory() Singleton {
29 return &testSuiteFiles{}
30}
31
Cole Faust725f8382025-03-11 18:31:15 -070032type testSuiteFiles struct{}
Colin Cross8eebb132020-01-29 20:07:03 -080033
34type TestSuiteModule interface {
35 Module
36 TestSuites() []string
37}
38
Yu Liu367827f2025-02-15 00:18:33 +000039type TestSuiteInfo struct {
40 TestSuites []string
41}
42
43var TestSuiteInfoProvider = blueprint.NewProvider[TestSuiteInfo]()
44
mrziwangfb4b9962025-03-18 23:23:44 +000045type SupportFilesInfo struct {
46 SupportFiles InstallPaths
47}
48
49var SupportFilesInfoProvider = blueprint.NewProvider[SupportFilesInfo]()
50
Colin Cross8eebb132020-01-29 20:07:03 -080051func (t *testSuiteFiles) GenerateBuildActions(ctx SingletonContext) {
52 files := make(map[string]map[string]InstallPaths)
53
Yu Liu367827f2025-02-15 00:18:33 +000054 ctx.VisitAllModuleProxies(func(m ModuleProxy) {
55 if tsm, ok := OtherModuleProvider(ctx, m, TestSuiteInfoProvider); ok {
56 for _, testSuite := range tsm.TestSuites {
Colin Cross8eebb132020-01-29 20:07:03 -080057 if files[testSuite] == nil {
58 files[testSuite] = make(map[string]InstallPaths)
59 }
60 name := ctx.ModuleName(m)
Yu Liud46e5ae2024-08-15 18:46:17 +000061 files[testSuite][name] = append(files[testSuite][name],
Yu Liu367827f2025-02-15 00:18:33 +000062 OtherModuleProviderOrDefault(ctx, m, InstallFilesProvider).InstallFiles...)
Colin Cross8eebb132020-01-29 20:07:03 -080063 }
64 }
65 })
66
Cole Faust725f8382025-03-11 18:31:15 -070067 robolectricZip, robolectrictListZip := buildTestSuite(ctx, "robolectric-tests", files["robolectric-tests"])
68 ctx.Phony("robolectric-tests", robolectricZip, robolectrictListZip)
69 ctx.DistForGoal("robolectric-tests", robolectricZip, robolectrictListZip)
Makoto Onuki4a9869d2023-10-20 10:42:47 -070070
Cole Faust725f8382025-03-11 18:31:15 -070071 ravenwoodZip, ravenwoodListZip := buildTestSuite(ctx, "ravenwood-tests", files["ravenwood-tests"])
72 ctx.Phony("ravenwood-tests", ravenwoodZip, ravenwoodListZip)
73 ctx.DistForGoal("ravenwood-tests", ravenwoodZip, ravenwoodListZip)
Colin Cross8eebb132020-01-29 20:07:03 -080074}
75
Cole Faust725f8382025-03-11 18:31:15 -070076func buildTestSuite(ctx SingletonContext, suiteName string, files map[string]InstallPaths) (Path, Path) {
Colin Cross8eebb132020-01-29 20:07:03 -080077 var installedPaths InstallPaths
Cole Faust18994c72023-02-28 16:02:16 -080078 for _, module := range SortedKeys(files) {
Colin Cross8eebb132020-01-29 20:07:03 -080079 installedPaths = append(installedPaths, files[module]...)
80 }
Colin Cross8eebb132020-01-29 20:07:03 -080081
Cole Faust725f8382025-03-11 18:31:15 -070082 outputFile := pathForPackaging(ctx, suiteName+".zip")
Colin Crossf1a035e2020-11-16 17:32:30 -080083 rule := NewRuleBuilder(pctx, ctx)
84 rule.Command().BuiltTool("soong_zip").
Colin Cross8eebb132020-01-29 20:07:03 -080085 FlagWithOutput("-o ", outputFile).
86 FlagWithArg("-P ", "host/testcases").
Weijia He299d62d2024-04-18 22:55:10 +000087 FlagWithArg("-C ", pathForTestCases(ctx).String()).
Julien Desprezf3680732023-06-27 20:48:04 +000088 FlagWithRspFileInputList("-r ", outputFile.ReplaceExtension(ctx, "rsp"), installedPaths.Paths()).
Weijia He299d62d2024-04-18 22:55:10 +000089 Flag("-sha256") // necessary to save cas_uploader's time
90
Cole Faust725f8382025-03-11 18:31:15 -070091 testList := buildTestList(ctx, suiteName+"_list", installedPaths)
92 testListZipOutputFile := pathForPackaging(ctx, suiteName+"_list.zip")
Weijia He299d62d2024-04-18 22:55:10 +000093
94 rule.Command().BuiltTool("soong_zip").
95 FlagWithOutput("-o ", testListZipOutputFile).
96 FlagWithArg("-C ", pathForPackaging(ctx).String()).
97 FlagWithInput("-f ", testList).
Cole Faust3b703f32023-10-16 13:30:51 -070098 Flag("-sha256")
Weijia He299d62d2024-04-18 22:55:10 +000099
Cole Faust725f8382025-03-11 18:31:15 -0700100 rule.Build(strings.ReplaceAll(suiteName, "-", "_")+"_zip", suiteName+".zip")
Colin Cross8eebb132020-01-29 20:07:03 -0800101
Cole Faust725f8382025-03-11 18:31:15 -0700102 return outputFile, testListZipOutputFile
Weijia He299d62d2024-04-18 22:55:10 +0000103}
104
105func buildTestList(ctx SingletonContext, listFile string, installedPaths InstallPaths) Path {
106 buf := &strings.Builder{}
107 for _, p := range installedPaths {
108 if p.Ext() != ".config" {
109 continue
110 }
111 pc, err := toTestListPath(p.String(), pathForTestCases(ctx).String(), "host/testcases")
112 if err != nil {
113 ctx.Errorf("Failed to convert path: %s, %v", p.String(), err)
114 continue
115 }
116 buf.WriteString(pc)
117 buf.WriteString("\n")
118 }
119 outputFile := pathForPackaging(ctx, listFile)
120 WriteFileRuleVerbatim(ctx, outputFile, buf.String())
Makoto Onuki4a9869d2023-10-20 10:42:47 -0700121 return outputFile
122}
Weijia He299d62d2024-04-18 22:55:10 +0000123
124func toTestListPath(path, relativeRoot, prefix string) (string, error) {
125 dest, err := filepath.Rel(relativeRoot, path)
126 if err != nil {
127 return "", err
128 }
129 return filepath.Join(prefix, dest), nil
130}
131
132func pathForPackaging(ctx PathContext, pathComponents ...string) OutputPath {
133 pathComponents = append([]string{"packaging"}, pathComponents...)
134 return PathForOutput(ctx, pathComponents...)
135}
136
137func pathForTestCases(ctx PathContext) InstallPath {
138 return pathForInstall(ctx, ctx.Config().BuildOS, X86, "testcases")
139}