blob: 6713189fdc5bb77cc00411ceb2c28f2b5f0e5556 [file] [log] [blame]
Nan Zhang5323f8e2017-05-10 13:37:54 -07001// Copyright 2017 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 python
16
17import (
18 "android/soong/android"
yelinhsieh80880a32018-11-06 11:49:55 +080019 "android/soong/tradefed"
Nan Zhang5323f8e2017-05-10 13:37:54 -070020)
21
22// This file contains the module types for building Python test.
23
24func init() {
Paul Duffind0890452021-03-17 21:57:08 +000025 registerPythonTestComponents(android.InitRegistrationContext)
26}
27
28func registerPythonTestComponents(ctx android.RegistrationContext) {
29 ctx.RegisterModuleType("python_test_host", PythonTestHostFactory)
30 ctx.RegisterModuleType("python_test", PythonTestFactory)
Nan Zhang5323f8e2017-05-10 13:37:54 -070031}
32
Dan Shid79572f2020-11-13 14:33:46 -080033// Test option struct.
34type TestOptions struct {
35 // If the test is a hostside(no device required) unittest that shall be run during presubmit check.
36 Unit_test *bool
37}
38
Julien Despreze146e392018-08-02 15:00:46 -070039type TestProperties struct {
40 // the name of the test configuration (for example "AndroidTest.xml") that should be
41 // installed with the module.
Colin Crossa6384822020-06-09 15:09:22 -070042 Test_config *string `android:"path,arch_variant"`
yelinhsieh80880a32018-11-06 11:49:55 +080043
44 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
45 // should be installed with the module.
Colin Crossa6384822020-06-09 15:09:22 -070046 Test_config_template *string `android:"path,arch_variant"`
Dan Shi31949122020-09-21 12:11:02 -070047
48 // list of files or filegroup modules that provide data that should be installed alongside
49 // the test
50 Data []string `android:"path,arch_variant"`
Dan Shid79572f2020-11-13 14:33:46 -080051
Colin Cross1bc63932020-11-22 20:12:45 -080052 // list of java modules that provide data that should be installed alongside the test.
53 Java_data []string
54
Dan Shid79572f2020-11-13 14:33:46 -080055 // Test options.
56 Test_options TestOptions
Julien Despreze146e392018-08-02 15:00:46 -070057}
58
Nan Zhangd4e641b2017-07-12 12:55:28 -070059type testDecorator struct {
60 *binaryDecorator
Julien Despreze146e392018-08-02 15:00:46 -070061
62 testProperties TestProperties
yelinhsieh80880a32018-11-06 11:49:55 +080063
64 testConfig android.Path
Dan Shi31949122020-09-21 12:11:02 -070065
66 data []android.DataPath
Julien Despreze146e392018-08-02 15:00:46 -070067}
68
69func (test *testDecorator) bootstrapperProps() []interface{} {
70 return append(test.binaryDecorator.bootstrapperProps(), &test.testProperties)
Nan Zhang5323f8e2017-05-10 13:37:54 -070071}
72
Nan Zhangd4e641b2017-07-12 12:55:28 -070073func (test *testDecorator) install(ctx android.ModuleContext, file android.Path) {
yelinhsieh80880a32018-11-06 11:49:55 +080074 test.testConfig = tradefed.AutoGenPythonBinaryHostTestConfig(ctx, test.testProperties.Test_config,
Dan Shi6ffaaa82019-09-26 11:41:36 -070075 test.testProperties.Test_config_template, test.binaryDecorator.binaryProperties.Test_suites,
76 test.binaryDecorator.binaryProperties.Auto_gen_config)
yelinhsieh80880a32018-11-06 11:49:55 +080077
Nan Zhangd9ec5e72017-12-01 20:00:31 +000078 test.binaryDecorator.pythonInstaller.dir = "nativetest"
79 test.binaryDecorator.pythonInstaller.dir64 = "nativetest64"
80
81 test.binaryDecorator.pythonInstaller.relative = ctx.ModuleName()
82
83 test.binaryDecorator.pythonInstaller.install(ctx, file)
Dan Shi31949122020-09-21 12:11:02 -070084
85 dataSrcPaths := android.PathsForModuleSrc(ctx, test.testProperties.Data)
86
87 for _, dataSrcPath := range dataSrcPaths {
88 test.data = append(test.data, android.DataPath{SrcPath: dataSrcPath})
89 }
Colin Cross1bc63932020-11-22 20:12:45 -080090
91 // Emulate the data property for java_data dependencies.
92 for _, javaData := range ctx.GetDirectDepsWithTag(javaDataTag) {
93 for _, javaDataSrcPath := range android.OutputFilesForModule(ctx, javaData, "") {
94 test.data = append(test.data, android.DataPath{SrcPath: javaDataSrcPath})
95 }
96 }
Nan Zhang5323f8e2017-05-10 13:37:54 -070097}
98
Nan Zhangd4e641b2017-07-12 12:55:28 -070099func NewTest(hod android.HostOrDeviceSupported) *Module {
100 module, binary := NewBinary(hod)
101
Nan Zhangd9ec5e72017-12-01 20:00:31 +0000102 binary.pythonInstaller = NewPythonInstaller("nativetest", "nativetest64")
Nan Zhangd4e641b2017-07-12 12:55:28 -0700103
104 test := &testDecorator{binaryDecorator: binary}
105
106 module.bootstrapper = test
107 module.installer = test
108
109 return module
Nan Zhang5323f8e2017-05-10 13:37:54 -0700110}
111
Colin Cross36242852017-06-23 15:06:31 -0700112func PythonTestHostFactory() android.Module {
Nan Zhangd4e641b2017-07-12 12:55:28 -0700113 module := NewTest(android.HostSupportedNoCross)
Nan Zhang5323f8e2017-05-10 13:37:54 -0700114
Liz Kammerd737d022020-11-16 15:42:51 -0800115 return module.init()
Nan Zhang5323f8e2017-05-10 13:37:54 -0700116}
Nan Zhangd9ec5e72017-12-01 20:00:31 +0000117
118func PythonTestFactory() android.Module {
119 module := NewTest(android.HostAndDeviceSupported)
120 module.multilib = android.MultilibBoth
121
Liz Kammerd737d022020-11-16 15:42:51 -0800122 return module.init()
Nan Zhangd9ec5e72017-12-01 20:00:31 +0000123}