blob: 3cd900f4c482fd65d7fc3acd264e80f06f07607b [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 (
Julien Desprez66534a02021-02-09 09:27:39 -080018 "github.com/google/blueprint/proptools"
19
Nan Zhang5323f8e2017-05-10 13:37:54 -070020 "android/soong/android"
yelinhsieh80880a32018-11-06 11:49:55 +080021 "android/soong/tradefed"
Nan Zhang5323f8e2017-05-10 13:37:54 -070022)
23
24// This file contains the module types for building Python test.
25
26func init() {
Paul Duffind0890452021-03-17 21:57:08 +000027 registerPythonTestComponents(android.InitRegistrationContext)
28}
29
30func registerPythonTestComponents(ctx android.RegistrationContext) {
31 ctx.RegisterModuleType("python_test_host", PythonTestHostFactory)
32 ctx.RegisterModuleType("python_test", PythonTestFactory)
Nan Zhang5323f8e2017-05-10 13:37:54 -070033}
34
Dan Shid79572f2020-11-13 14:33:46 -080035// Test option struct.
36type TestOptions struct {
37 // If the test is a hostside(no device required) unittest that shall be run during presubmit check.
38 Unit_test *bool
39}
40
Julien Despreze146e392018-08-02 15:00:46 -070041type TestProperties struct {
42 // the name of the test configuration (for example "AndroidTest.xml") that should be
43 // installed with the module.
Colin Crossa6384822020-06-09 15:09:22 -070044 Test_config *string `android:"path,arch_variant"`
yelinhsieh80880a32018-11-06 11:49:55 +080045
46 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
47 // should be installed with the module.
Colin Crossa6384822020-06-09 15:09:22 -070048 Test_config_template *string `android:"path,arch_variant"`
Dan Shi31949122020-09-21 12:11:02 -070049
50 // list of files or filegroup modules that provide data that should be installed alongside
51 // the test
52 Data []string `android:"path,arch_variant"`
Dan Shid79572f2020-11-13 14:33:46 -080053
Colin Cross1bc63932020-11-22 20:12:45 -080054 // list of java modules that provide data that should be installed alongside the test.
55 Java_data []string
56
Dan Shid79572f2020-11-13 14:33:46 -080057 // Test options.
58 Test_options TestOptions
Julien Despreze146e392018-08-02 15:00:46 -070059}
60
Nan Zhangd4e641b2017-07-12 12:55:28 -070061type testDecorator struct {
62 *binaryDecorator
Julien Despreze146e392018-08-02 15:00:46 -070063
64 testProperties TestProperties
yelinhsieh80880a32018-11-06 11:49:55 +080065
66 testConfig android.Path
Dan Shi31949122020-09-21 12:11:02 -070067
68 data []android.DataPath
Julien Despreze146e392018-08-02 15:00:46 -070069}
70
71func (test *testDecorator) bootstrapperProps() []interface{} {
72 return append(test.binaryDecorator.bootstrapperProps(), &test.testProperties)
Nan Zhang5323f8e2017-05-10 13:37:54 -070073}
74
Nan Zhangd4e641b2017-07-12 12:55:28 -070075func (test *testDecorator) install(ctx android.ModuleContext, file android.Path) {
yelinhsieh80880a32018-11-06 11:49:55 +080076 test.testConfig = tradefed.AutoGenPythonBinaryHostTestConfig(ctx, test.testProperties.Test_config,
Dan Shi6ffaaa82019-09-26 11:41:36 -070077 test.testProperties.Test_config_template, test.binaryDecorator.binaryProperties.Test_suites,
78 test.binaryDecorator.binaryProperties.Auto_gen_config)
yelinhsieh80880a32018-11-06 11:49:55 +080079
Nan Zhangd9ec5e72017-12-01 20:00:31 +000080 test.binaryDecorator.pythonInstaller.dir = "nativetest"
81 test.binaryDecorator.pythonInstaller.dir64 = "nativetest64"
82
83 test.binaryDecorator.pythonInstaller.relative = ctx.ModuleName()
84
85 test.binaryDecorator.pythonInstaller.install(ctx, file)
Dan Shi31949122020-09-21 12:11:02 -070086
87 dataSrcPaths := android.PathsForModuleSrc(ctx, test.testProperties.Data)
88
89 for _, dataSrcPath := range dataSrcPaths {
90 test.data = append(test.data, android.DataPath{SrcPath: dataSrcPath})
91 }
Colin Cross1bc63932020-11-22 20:12:45 -080092
93 // Emulate the data property for java_data dependencies.
94 for _, javaData := range ctx.GetDirectDepsWithTag(javaDataTag) {
95 for _, javaDataSrcPath := range android.OutputFilesForModule(ctx, javaData, "") {
96 test.data = append(test.data, android.DataPath{SrcPath: javaDataSrcPath})
97 }
98 }
Nan Zhang5323f8e2017-05-10 13:37:54 -070099}
100
Nan Zhangd4e641b2017-07-12 12:55:28 -0700101func NewTest(hod android.HostOrDeviceSupported) *Module {
102 module, binary := NewBinary(hod)
103
Wei Li664a4fd2021-10-07 06:26:48 +0000104 binary.pythonInstaller = NewPythonInstaller("nativetest", "nativetest64", module)
Nan Zhangd4e641b2017-07-12 12:55:28 -0700105
106 test := &testDecorator{binaryDecorator: binary}
Julien Desprez66534a02021-02-09 09:27:39 -0800107 if hod == android.HostSupportedNoCross && test.testProperties.Test_options.Unit_test == nil {
108 test.testProperties.Test_options.Unit_test = proptools.BoolPtr(true)
109 }
Nan Zhangd4e641b2017-07-12 12:55:28 -0700110
111 module.bootstrapper = test
112 module.installer = test
113
114 return module
Nan Zhang5323f8e2017-05-10 13:37:54 -0700115}
116
Colin Cross36242852017-06-23 15:06:31 -0700117func PythonTestHostFactory() android.Module {
Nan Zhangd4e641b2017-07-12 12:55:28 -0700118 module := NewTest(android.HostSupportedNoCross)
Nan Zhang5323f8e2017-05-10 13:37:54 -0700119
Liz Kammerd737d022020-11-16 15:42:51 -0800120 return module.init()
Nan Zhang5323f8e2017-05-10 13:37:54 -0700121}
Nan Zhangd9ec5e72017-12-01 20:00:31 +0000122
123func PythonTestFactory() android.Module {
124 module := NewTest(android.HostAndDeviceSupported)
125 module.multilib = android.MultilibBoth
126
Liz Kammerd737d022020-11-16 15:42:51 -0800127 return module.init()
Nan Zhangd9ec5e72017-12-01 20:00:31 +0000128}