test.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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.AutoGenRustTestConfig(ctx,
  107. test.Properties.Test_config,
  108. test.Properties.Test_config_template,
  109. test.Properties.Test_suites,
  110. configs,
  111. test.Properties.Auto_gen_config,
  112. testInstallBase)
  113. dataSrcPaths := android.PathsForModuleSrc(ctx, test.Properties.Data)
  114. ctx.VisitDirectDepsWithTag(dataLibDepTag, func(dep android.Module) {
  115. depName := ctx.OtherModuleName(dep)
  116. linkableDep, ok := dep.(cc.LinkableInterface)
  117. if !ok {
  118. ctx.ModuleErrorf("data_lib %q is not a linkable module", depName)
  119. }
  120. if linkableDep.OutputFile().Valid() {
  121. // Copy the output in "lib[64]" so that it's compatible with
  122. // the default rpath values.
  123. libDir := "lib"
  124. if linkableDep.Target().Arch.ArchType.Multilib == "lib64" {
  125. libDir = "lib64"
  126. }
  127. test.data = append(test.data,
  128. android.DataPath{SrcPath: linkableDep.OutputFile().Path(),
  129. RelativeInstallPath: filepath.Join(libDir, linkableDep.RelativeInstallPath())})
  130. }
  131. })
  132. ctx.VisitDirectDepsWithTag(dataBinDepTag, func(dep android.Module) {
  133. depName := ctx.OtherModuleName(dep)
  134. linkableDep, ok := dep.(cc.LinkableInterface)
  135. if !ok {
  136. ctx.ModuleErrorf("data_bin %q is not a linkable module", depName)
  137. }
  138. if linkableDep.OutputFile().Valid() {
  139. test.data = append(test.data,
  140. android.DataPath{SrcPath: linkableDep.OutputFile().Path(),
  141. RelativeInstallPath: linkableDep.RelativeInstallPath()})
  142. }
  143. })
  144. for _, dataSrcPath := range dataSrcPaths {
  145. test.data = append(test.data, android.DataPath{SrcPath: dataSrcPath})
  146. }
  147. // default relative install path is module name
  148. if !Bool(test.Properties.No_named_install_directory) {
  149. test.baseCompiler.relative = ctx.ModuleName()
  150. } else if String(test.baseCompiler.Properties.Relative_install_path) == "" {
  151. ctx.PropertyErrorf("no_named_install_directory", "Module install directory may only be disabled if relative_install_path is set")
  152. }
  153. if ctx.Host() && test.Properties.Test_options.Unit_test == nil {
  154. test.Properties.Test_options.Unit_test = proptools.BoolPtr(true)
  155. }
  156. test.binaryDecorator.install(ctx)
  157. }
  158. func (test *testDecorator) compilerFlags(ctx ModuleContext, flags Flags) Flags {
  159. flags = test.binaryDecorator.compilerFlags(ctx, flags)
  160. if test.testHarness() {
  161. flags.RustFlags = append(flags.RustFlags, "--test")
  162. }
  163. if ctx.Device() {
  164. flags.RustFlags = append(flags.RustFlags, "-Z panic_abort_tests")
  165. }
  166. return flags
  167. }
  168. func (test *testDecorator) autoDep(ctx android.BottomUpMutatorContext) autoDep {
  169. return rlibAutoDep
  170. }
  171. func init() {
  172. // Rust tests are binary files built with --test.
  173. android.RegisterModuleType("rust_test", RustTestFactory)
  174. android.RegisterModuleType("rust_test_host", RustTestHostFactory)
  175. }
  176. func RustTestFactory() android.Module {
  177. module, _ := NewRustTest(android.HostAndDeviceSupported)
  178. // NewRustTest will set MultilibBoth true, however the host variant
  179. // cannot produce the non-primary target. Therefore, add the
  180. // rustTestHostMultilib load hook to set MultilibFirst for the
  181. // host target.
  182. android.AddLoadHook(module, rustTestHostMultilib)
  183. return module.Init()
  184. }
  185. func RustTestHostFactory() android.Module {
  186. module, _ := NewRustTest(android.HostSupported)
  187. return module.Init()
  188. }
  189. func (test *testDecorator) stdLinkage(ctx *depsContext) RustLinkage {
  190. return RlibLinkage
  191. }
  192. func (test *testDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps {
  193. deps = test.binaryDecorator.compilerDeps(ctx, deps)
  194. deps.Rustlibs = append(deps.Rustlibs, "libtest")
  195. deps.DataLibs = append(deps.DataLibs, test.Properties.Data_libs...)
  196. deps.DataBins = append(deps.DataBins, test.Properties.Data_bins...)
  197. return deps
  198. }
  199. func (test *testDecorator) testBinary() bool {
  200. return true
  201. }
  202. func rustTestHostMultilib(ctx android.LoadHookContext) {
  203. type props struct {
  204. Target struct {
  205. Host struct {
  206. Compile_multilib *string
  207. }
  208. }
  209. }
  210. p := &props{}
  211. p.Target.Host.Compile_multilib = proptools.StringPtr("first")
  212. ctx.AppendProperties(p)
  213. }