diff options
Diffstat (limited to 'rust/test.go')
-rw-r--r-- | rust/test.go | 162 |
1 files changed, 74 insertions, 88 deletions
diff --git a/rust/test.go b/rust/test.go index 469bec652..6caa7b168 100644 --- a/rust/test.go +++ b/rust/test.go @@ -15,14 +15,24 @@ package rust import ( - "path/filepath" - "strings" + "github.com/google/blueprint/proptools" "android/soong/android" "android/soong/tradefed" ) +// Test option struct. +type TestOptions struct { + // If the test is a hostside(no device required) unittest that shall be run during presubmit check. + Unit_test *bool +} + type TestProperties struct { + // Disables the creation of a test-specific directory when used with + // relative_install_path. Useful if several tests need to be in the same + // directory, but test_per_src doesn't work. + No_named_install_directory *bool + // the name of the test configuration (for example "AndroidTest.xml") that should be // installed with the module. Test_config *string `android:"path,arch_variant"` @@ -35,10 +45,20 @@ type TestProperties struct { // installed into. Test_suites []string `android:"arch_variant"` + // list of files or filegroup modules that provide data that should be installed alongside + // the test + Data []string `android:"path,arch_variant"` + // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true // explicitly. Auto_gen_config *bool + + // if set, build with the standard Rust test harness. Defaults to true. + Test_harness *bool + + // Test options. + Test_options TestOptions } // A test module is a binary module with extra --test compiler flag @@ -48,10 +68,31 @@ type testDecorator struct { *binaryDecorator Properties TestProperties testConfig android.Path + + data []android.DataPath +} + +func (test *testDecorator) dataPaths() []android.DataPath { + return test.data +} + +func (test *testDecorator) nativeCoverage() bool { + return true +} + +func (test *testDecorator) testHarness() bool { + return BoolDefault(test.Properties.Test_harness, true) } func NewRustTest(hod android.HostOrDeviceSupported) (*Module, *testDecorator) { - module := newModule(hod, android.MultilibFirst) + // Build both 32 and 64 targets for device tests. + // Cannot build both for host tests yet if the test depends on + // something like proc-macro2 that cannot be built for both. + multilib := android.MultilibBoth + if hod != android.DeviceSupported && hod != android.HostAndDeviceSupported { + multilib = android.MultilibFirst + } + module := newModule(hod, multilib) test := &testDecorator{ binaryDecorator: &binaryDecorator{ @@ -60,7 +101,6 @@ func NewRustTest(hod android.HostOrDeviceSupported) (*Module, *testDecorator) { } module.compiler = test - return module, test } @@ -68,45 +108,48 @@ func (test *testDecorator) compilerProps() []interface{} { return append(test.binaryDecorator.compilerProps(), &test.Properties) } -func (test *testDecorator) getMutatedModuleSubName(moduleName string) string { - stem := String(test.baseCompiler.Properties.Stem) - if stem != "" && !strings.HasSuffix(moduleName, "_"+stem) { - // Avoid repeated suffix in the module name. - return "_" + stem - } - return "" -} - -func (test *testDecorator) install(ctx ModuleContext, file android.Path) { - name := ctx.ModuleName() - path := test.baseCompiler.relativeInstallPath() - // on device, use mutated module name - name = name + test.getMutatedModuleSubName(name) - if !ctx.Device() { // on host, use mutated module name + arch type + stem name - stem := String(test.baseCompiler.Properties.Stem) - if stem == "" { - stem = name - } - name = filepath.Join(name, ctx.Arch().ArchType.String(), stem) - } - test.testConfig = tradefed.AutoGenRustTestConfig(ctx, name, +func (test *testDecorator) install(ctx ModuleContext) { + test.testConfig = tradefed.AutoGenRustTestConfig(ctx, test.Properties.Test_config, test.Properties.Test_config_template, test.Properties.Test_suites, + nil, test.Properties.Auto_gen_config) + + dataSrcPaths := android.PathsForModuleSrc(ctx, test.Properties.Data) + + for _, dataSrcPath := range dataSrcPaths { + test.data = append(test.data, android.DataPath{SrcPath: dataSrcPath}) + } + // default relative install path is module name - if path == "" { + if !Bool(test.Properties.No_named_install_directory) { test.baseCompiler.relative = ctx.ModuleName() + } else if String(test.baseCompiler.Properties.Relative_install_path) == "" { + ctx.PropertyErrorf("no_named_install_directory", "Module install directory may only be disabled if relative_install_path is set") } - test.binaryDecorator.install(ctx, file) + + if ctx.Host() && test.Properties.Test_options.Unit_test == nil { + test.Properties.Test_options.Unit_test = proptools.BoolPtr(true) + } + test.binaryDecorator.install(ctx) } func (test *testDecorator) compilerFlags(ctx ModuleContext, flags Flags) Flags { flags = test.binaryDecorator.compilerFlags(ctx, flags) - flags.RustFlags = append(flags.RustFlags, "--test") + if test.testHarness() { + flags.RustFlags = append(flags.RustFlags, "--test") + } + if ctx.Device() { + flags.RustFlags = append(flags.RustFlags, "-Z panic_abort_tests") + } return flags } +func (test *testDecorator) autoDep(ctx android.BottomUpMutatorContext) autoDep { + return rlibAutoDep +} + func init() { // Rust tests are binary files built with --test. android.RegisterModuleType("rust_test", RustTestFactory) @@ -123,63 +166,6 @@ func RustTestHostFactory() android.Module { return module.Init() } -func (test *testDecorator) testPerSrc() bool { - return true -} - -func (test *testDecorator) srcs() []string { - return test.binaryDecorator.Properties.Srcs -} - -func (test *testDecorator) setSrc(name, src string) { - test.binaryDecorator.Properties.Srcs = []string{src} - test.baseCompiler.Properties.Stem = StringPtr(name) -} - -func (test *testDecorator) unsetSrc() { - test.binaryDecorator.Properties.Srcs = nil - test.baseCompiler.Properties.Stem = StringPtr("") -} - -type testPerSrc interface { - testPerSrc() bool - srcs() []string - setSrc(string, string) - unsetSrc() -} - -var _ testPerSrc = (*testDecorator)(nil) - -func TestPerSrcMutator(mctx android.BottomUpMutatorContext) { - if m, ok := mctx.Module().(*Module); ok { - if test, ok := m.compiler.(testPerSrc); ok { - numTests := len(test.srcs()) - if test.testPerSrc() && numTests > 0 { - if duplicate, found := android.CheckDuplicate(test.srcs()); found { - mctx.PropertyErrorf("srcs", "found a duplicate entry %q", duplicate) - return - } - // Rust compiler always compiles one source file at a time and - // uses the crate name as output file name. - // Cargo uses the test source file name as default crate name, - // but that can be redefined. - // So when there are multiple source files, the source file names will - // be the output file names, but when there is only one test file, - // use the crate name. - testNames := make([]string, numTests) - for i, src := range test.srcs() { - testNames[i] = strings.TrimSuffix(filepath.Base(src), filepath.Ext(src)) - } - crateName := m.compiler.crateName() - if numTests == 1 && crateName != "" { - testNames[0] = crateName - } - // TODO(chh): Add an "all tests" variation like cc/test.go? - tests := mctx.CreateLocalVariations(testNames...) - for i, src := range test.srcs() { - tests[i].(*Module).compiler.(testPerSrc).setSrc(testNames[i], src) - } - } - } - } +func (test *testDecorator) stdLinkage(ctx *depsContext) RustLinkage { + return RlibLinkage } |