test.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. // Copyright 2019 The Android Open Source Project
  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. package rust
  15. import (
  16. "path/filepath"
  17. "github.com/google/blueprint/proptools"
  18. "android/soong/android"
  19. "android/soong/cc"
  20. "android/soong/tradefed"
  21. )
  22. type TestProperties struct {
  23. // Disables the creation of a test-specific directory when used with
  24. // relative_install_path. Useful if several tests need to be in the same
  25. // directory, but test_per_src doesn't work.
  26. No_named_install_directory *bool
  27. // the name of the test configuration (for example "AndroidTest.xml") that should be
  28. // installed with the module.
  29. Test_config *string `android:"path,arch_variant"`
  30. // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
  31. // should be installed with the module.
  32. Test_config_template *string `android:"path,arch_variant"`
  33. // list of compatibility suites (for example "cts", "vts") that the module should be
  34. // installed into.
  35. Test_suites []string `android:"arch_variant"`
  36. // list of files or filegroup modules that provide data that should be installed alongside
  37. // the test
  38. Data []string `android:"path,arch_variant"`
  39. // list of shared library modules that should be installed alongside the test
  40. Data_libs []string `android:"arch_variant"`
  41. // list of binary modules that should be installed alongside the test
  42. Data_bins []string `android:"arch_variant"`
  43. // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
  44. // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
  45. // explicitly.
  46. Auto_gen_config *bool
  47. // if set, build with the standard Rust test harness. Defaults to true.
  48. Test_harness *bool
  49. // Test options.
  50. Test_options android.CommonTestOptions
  51. // Add RootTargetPreparer to auto generated test config. This guarantees the test to run
  52. // with root permission.
  53. Require_root *bool
  54. }
  55. // A test module is a binary module with extra --test compiler flag
  56. // and different default installation directory.
  57. // In golang, inheriance is written as a component.
  58. type testDecorator struct {
  59. *binaryDecorator
  60. Properties TestProperties
  61. testConfig android.Path
  62. data []android.DataPath
  63. }
  64. func (test *testDecorator) dataPaths() []android.DataPath {
  65. return test.data
  66. }
  67. func (test *testDecorator) nativeCoverage() bool {
  68. return true
  69. }
  70. func (test *testDecorator) testHarness() bool {
  71. return BoolDefault(test.Properties.Test_harness, true)
  72. }
  73. func NewRustTest(hod android.HostOrDeviceSupported) (*Module, *testDecorator) {
  74. // Build both 32 and 64 targets for device tests.
  75. // Cannot build both for host tests yet if the test depends on
  76. // something like proc-macro2 that cannot be built for both.
  77. multilib := android.MultilibBoth
  78. if hod != android.DeviceSupported && hod != android.HostAndDeviceSupported {
  79. multilib = android.MultilibFirst
  80. }
  81. module := newModule(hod, multilib)
  82. test := &testDecorator{
  83. binaryDecorator: &binaryDecorator{
  84. baseCompiler: NewBaseCompiler("nativetest", "nativetest64", InstallInData),
  85. },
  86. }
  87. module.compiler = test
  88. return module, test
  89. }
  90. func (test *testDecorator) compilerProps() []interface{} {
  91. return append(test.binaryDecorator.compilerProps(), &test.Properties)
  92. }
  93. func (test *testDecorator) install(ctx ModuleContext) {
  94. testInstallBase := "/data/local/tests/unrestricted"
  95. if ctx.RustModule().InVendor() || ctx.RustModule().UseVndk() {
  96. testInstallBase = "/data/local/tests/vendor"
  97. }
  98. var configs []tradefed.Config
  99. if Bool(test.Properties.Require_root) {
  100. configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.RootTargetPreparer", nil})
  101. } else {
  102. var options []tradefed.Option
  103. options = append(options, tradefed.Option{Name: "force-root", Value: "false"})
  104. configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.RootTargetPreparer", options})
  105. }
  106. test.testConfig = tradefed.AutoGenTestConfig(ctx, tradefed.AutoGenTestConfigOptions{
  107. TestConfigProp: test.Properties.Test_config,
  108. TestConfigTemplateProp: test.Properties.Test_config_template,
  109. TestSuites: test.Properties.Test_suites,
  110. Config: configs,
  111. AutoGenConfig: test.Properties.Auto_gen_config,
  112. TestInstallBase: testInstallBase,
  113. DeviceTemplate: "${RustDeviceTestConfigTemplate}",
  114. HostTemplate: "${RustHostTestConfigTemplate}",
  115. })
  116. dataSrcPaths := android.PathsForModuleSrc(ctx, test.Properties.Data)
  117. ctx.VisitDirectDepsWithTag(dataLibDepTag, func(dep android.Module) {
  118. depName := ctx.OtherModuleName(dep)
  119. linkableDep, ok := dep.(cc.LinkableInterface)
  120. if !ok {
  121. ctx.ModuleErrorf("data_lib %q is not a linkable module", depName)
  122. }
  123. if linkableDep.OutputFile().Valid() {
  124. // Copy the output in "lib[64]" so that it's compatible with
  125. // the default rpath values.
  126. libDir := "lib"
  127. if linkableDep.Target().Arch.ArchType.Multilib == "lib64" {
  128. libDir = "lib64"
  129. }
  130. test.data = append(test.data,
  131. android.DataPath{SrcPath: linkableDep.OutputFile().Path(),
  132. RelativeInstallPath: filepath.Join(libDir, linkableDep.RelativeInstallPath())})
  133. }
  134. })
  135. ctx.VisitDirectDepsWithTag(dataBinDepTag, func(dep android.Module) {
  136. depName := ctx.OtherModuleName(dep)
  137. linkableDep, ok := dep.(cc.LinkableInterface)
  138. if !ok {
  139. ctx.ModuleErrorf("data_bin %q is not a linkable module", depName)
  140. }
  141. if linkableDep.OutputFile().Valid() {
  142. test.data = append(test.data,
  143. android.DataPath{SrcPath: linkableDep.OutputFile().Path(),
  144. RelativeInstallPath: linkableDep.RelativeInstallPath()})
  145. }
  146. })
  147. for _, dataSrcPath := range dataSrcPaths {
  148. test.data = append(test.data, android.DataPath{SrcPath: dataSrcPath})
  149. }
  150. // default relative install path is module name
  151. if !Bool(test.Properties.No_named_install_directory) {
  152. test.baseCompiler.relative = ctx.ModuleName()
  153. } else if String(test.baseCompiler.Properties.Relative_install_path) == "" {
  154. ctx.PropertyErrorf("no_named_install_directory", "Module install directory may only be disabled if relative_install_path is set")
  155. }
  156. if ctx.Host() && test.Properties.Test_options.Unit_test == nil {
  157. test.Properties.Test_options.Unit_test = proptools.BoolPtr(true)
  158. }
  159. test.binaryDecorator.install(ctx)
  160. }
  161. func (test *testDecorator) compilerFlags(ctx ModuleContext, flags Flags) Flags {
  162. flags = test.binaryDecorator.compilerFlags(ctx, flags)
  163. if test.testHarness() {
  164. flags.RustFlags = append(flags.RustFlags, "--test")
  165. }
  166. if ctx.Device() {
  167. flags.RustFlags = append(flags.RustFlags, "-Z panic_abort_tests")
  168. }
  169. return flags
  170. }
  171. func (test *testDecorator) autoDep(ctx android.BottomUpMutatorContext) autoDep {
  172. return rlibAutoDep
  173. }
  174. func init() {
  175. // Rust tests are binary files built with --test.
  176. android.RegisterModuleType("rust_test", RustTestFactory)
  177. android.RegisterModuleType("rust_test_host", RustTestHostFactory)
  178. }
  179. func RustTestFactory() android.Module {
  180. module, _ := NewRustTest(android.HostAndDeviceSupported)
  181. // NewRustTest will set MultilibBoth true, however the host variant
  182. // cannot produce the non-primary target. Therefore, add the
  183. // rustTestHostMultilib load hook to set MultilibFirst for the
  184. // host target.
  185. android.AddLoadHook(module, rustTestHostMultilib)
  186. return module.Init()
  187. }
  188. func RustTestHostFactory() android.Module {
  189. module, _ := NewRustTest(android.HostSupported)
  190. return module.Init()
  191. }
  192. func (test *testDecorator) stdLinkage(ctx *depsContext) RustLinkage {
  193. return RlibLinkage
  194. }
  195. func (test *testDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps {
  196. deps = test.binaryDecorator.compilerDeps(ctx, deps)
  197. deps.Rustlibs = append(deps.Rustlibs, "libtest")
  198. deps.DataLibs = append(deps.DataLibs, test.Properties.Data_libs...)
  199. deps.DataBins = append(deps.DataBins, test.Properties.Data_bins...)
  200. return deps
  201. }
  202. func (test *testDecorator) testBinary() bool {
  203. return true
  204. }
  205. func rustTestHostMultilib(ctx android.LoadHookContext) {
  206. type props struct {
  207. Target struct {
  208. Host struct {
  209. Compile_multilib *string
  210. }
  211. }
  212. }
  213. p := &props{}
  214. p.Target.Host.Compile_multilib = proptools.StringPtr("first")
  215. ctx.AppendProperties(p)
  216. }