blob: 9eaf785493caa761256e2a7b67637a13cefadd33 [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
Colin Cross8eebb132020-01-29 20:07:03 -080045func (t *testSuiteFiles) GenerateBuildActions(ctx SingletonContext) {
46 files := make(map[string]map[string]InstallPaths)
47
Yu Liu367827f2025-02-15 00:18:33 +000048 ctx.VisitAllModuleProxies(func(m ModuleProxy) {
49 if tsm, ok := OtherModuleProvider(ctx, m, TestSuiteInfoProvider); ok {
50 for _, testSuite := range tsm.TestSuites {
Colin Cross8eebb132020-01-29 20:07:03 -080051 if files[testSuite] == nil {
52 files[testSuite] = make(map[string]InstallPaths)
53 }
54 name := ctx.ModuleName(m)
Yu Liud46e5ae2024-08-15 18:46:17 +000055 files[testSuite][name] = append(files[testSuite][name],
Yu Liu367827f2025-02-15 00:18:33 +000056 OtherModuleProviderOrDefault(ctx, m, InstallFilesProvider).InstallFiles...)
Colin Cross8eebb132020-01-29 20:07:03 -080057 }
58 }
59 })
60
Cole Faust725f8382025-03-11 18:31:15 -070061 robolectricZip, robolectrictListZip := buildTestSuite(ctx, "robolectric-tests", files["robolectric-tests"])
62 ctx.Phony("robolectric-tests", robolectricZip, robolectrictListZip)
63 ctx.DistForGoal("robolectric-tests", robolectricZip, robolectrictListZip)
Makoto Onuki4a9869d2023-10-20 10:42:47 -070064
Cole Faust725f8382025-03-11 18:31:15 -070065 ravenwoodZip, ravenwoodListZip := buildTestSuite(ctx, "ravenwood-tests", files["ravenwood-tests"])
66 ctx.Phony("ravenwood-tests", ravenwoodZip, ravenwoodListZip)
67 ctx.DistForGoal("ravenwood-tests", ravenwoodZip, ravenwoodListZip)
Colin Cross8eebb132020-01-29 20:07:03 -080068}
69
Cole Faust725f8382025-03-11 18:31:15 -070070func buildTestSuite(ctx SingletonContext, suiteName string, files map[string]InstallPaths) (Path, Path) {
Colin Cross8eebb132020-01-29 20:07:03 -080071 var installedPaths InstallPaths
Cole Faust18994c72023-02-28 16:02:16 -080072 for _, module := range SortedKeys(files) {
Colin Cross8eebb132020-01-29 20:07:03 -080073 installedPaths = append(installedPaths, files[module]...)
74 }
Colin Cross8eebb132020-01-29 20:07:03 -080075
Cole Faust725f8382025-03-11 18:31:15 -070076 outputFile := pathForPackaging(ctx, suiteName+".zip")
Colin Crossf1a035e2020-11-16 17:32:30 -080077 rule := NewRuleBuilder(pctx, ctx)
78 rule.Command().BuiltTool("soong_zip").
Colin Cross8eebb132020-01-29 20:07:03 -080079 FlagWithOutput("-o ", outputFile).
80 FlagWithArg("-P ", "host/testcases").
Weijia He299d62d2024-04-18 22:55:10 +000081 FlagWithArg("-C ", pathForTestCases(ctx).String()).
Julien Desprezf3680732023-06-27 20:48:04 +000082 FlagWithRspFileInputList("-r ", outputFile.ReplaceExtension(ctx, "rsp"), installedPaths.Paths()).
Weijia He299d62d2024-04-18 22:55:10 +000083 Flag("-sha256") // necessary to save cas_uploader's time
84
Cole Faust725f8382025-03-11 18:31:15 -070085 testList := buildTestList(ctx, suiteName+"_list", installedPaths)
86 testListZipOutputFile := pathForPackaging(ctx, suiteName+"_list.zip")
Weijia He299d62d2024-04-18 22:55:10 +000087
88 rule.Command().BuiltTool("soong_zip").
89 FlagWithOutput("-o ", testListZipOutputFile).
90 FlagWithArg("-C ", pathForPackaging(ctx).String()).
91 FlagWithInput("-f ", testList).
Cole Faust3b703f32023-10-16 13:30:51 -070092 Flag("-sha256")
Weijia He299d62d2024-04-18 22:55:10 +000093
Cole Faust725f8382025-03-11 18:31:15 -070094 rule.Build(strings.ReplaceAll(suiteName, "-", "_")+"_zip", suiteName+".zip")
Colin Cross8eebb132020-01-29 20:07:03 -080095
Cole Faust725f8382025-03-11 18:31:15 -070096 return outputFile, testListZipOutputFile
Weijia He299d62d2024-04-18 22:55:10 +000097}
98
99func buildTestList(ctx SingletonContext, listFile string, installedPaths InstallPaths) Path {
100 buf := &strings.Builder{}
101 for _, p := range installedPaths {
102 if p.Ext() != ".config" {
103 continue
104 }
105 pc, err := toTestListPath(p.String(), pathForTestCases(ctx).String(), "host/testcases")
106 if err != nil {
107 ctx.Errorf("Failed to convert path: %s, %v", p.String(), err)
108 continue
109 }
110 buf.WriteString(pc)
111 buf.WriteString("\n")
112 }
113 outputFile := pathForPackaging(ctx, listFile)
114 WriteFileRuleVerbatim(ctx, outputFile, buf.String())
Makoto Onuki4a9869d2023-10-20 10:42:47 -0700115 return outputFile
116}
Weijia He299d62d2024-04-18 22:55:10 +0000117
118func toTestListPath(path, relativeRoot, prefix string) (string, error) {
119 dest, err := filepath.Rel(relativeRoot, path)
120 if err != nil {
121 return "", err
122 }
123 return filepath.Join(prefix, dest), nil
124}
125
126func pathForPackaging(ctx PathContext, pathComponents ...string) OutputPath {
127 pathComponents = append([]string{"packaging"}, pathComponents...)
128 return PathForOutput(ctx, pathComponents...)
129}
130
131func pathForTestCases(ctx PathContext) InstallPath {
132 return pathForInstall(ctx, ctx.Config().BuildOS, X86, "testcases")
133}