Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 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 | |
| 15 | package rust |
| 16 | |
| 17 | import ( |
| 18 | "strings" |
| 19 | "testing" |
| 20 | ) |
| 21 | |
| 22 | // Test that the prefer_dynamic property is handled correctly. |
| 23 | func TestPreferDynamicBinary(t *testing.T) { |
| 24 | ctx := testRust(t, ` |
| 25 | rust_binary_host { |
| 26 | name: "fizz-buzz-dynamic", |
| 27 | srcs: ["foo.rs"], |
| 28 | prefer_dynamic: true, |
| 29 | } |
| 30 | |
| 31 | rust_binary_host { |
| 32 | name: "fizz-buzz", |
| 33 | srcs: ["foo.rs"], |
| 34 | }`) |
| 35 | |
| 36 | fizzBuzz := ctx.ModuleForTests("fizz-buzz", "linux_glibc_x86_64").Output("fizz-buzz") |
| 37 | fizzBuzzDynamic := ctx.ModuleForTests("fizz-buzz-dynamic", "linux_glibc_x86_64").Output("fizz-buzz-dynamic") |
| 38 | |
Chih-Hung Hsieh | a756270 | 2020-08-10 21:50:43 -0700 | [diff] [blame] | 39 | path := ctx.ModuleForTests("fizz-buzz", "linux_glibc_x86_64").Module().(*Module).HostToolPath() |
| 40 | if g, w := path.String(), "/host/linux-x86/bin/fizz-buzz"; !strings.Contains(g, w) { |
| 41 | t.Errorf("wrong host tool path, expected %q got %q", w, g) |
| 42 | } |
| 43 | |
Chih-Hung Hsieh | a5f22ed | 2019-10-24 20:47:54 -0700 | [diff] [blame] | 44 | // Do not compile binary modules with the --test flag. |
| 45 | flags := fizzBuzzDynamic.Args["rustcFlags"] |
| 46 | if strings.Contains(flags, "--test") { |
| 47 | t.Errorf("extra --test flag, rustcFlags: %#v", flags) |
| 48 | } |
| 49 | if !strings.Contains(flags, "prefer-dynamic") { |
| 50 | t.Errorf("missing prefer-dynamic flag, rustcFlags: %#v", flags) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 51 | } |
| 52 | |
Chih-Hung Hsieh | a5f22ed | 2019-10-24 20:47:54 -0700 | [diff] [blame] | 53 | flags = fizzBuzz.Args["rustcFlags"] |
| 54 | if strings.Contains(flags, "--test") { |
| 55 | t.Errorf("extra --test flag, rustcFlags: %#v", flags) |
| 56 | } |
| 57 | if strings.Contains(flags, "prefer-dynamic") { |
| 58 | t.Errorf("unexpected prefer-dynamic flag, rustcFlags: %#v", flags) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 59 | } |
| 60 | } |
Ivan Lozano | 2093af2 | 2020-08-25 12:48:19 -0400 | [diff] [blame^] | 61 | |
| 62 | func TestLinkObjects(t *testing.T) { |
| 63 | ctx := testRust(t, ` |
| 64 | rust_binary { |
| 65 | name: "fizz-buzz", |
| 66 | srcs: ["foo.rs"], |
| 67 | shared_libs: ["libfoo"], |
| 68 | } |
| 69 | cc_library { |
| 70 | name: "libfoo", |
| 71 | }`) |
| 72 | |
| 73 | fizzBuzz := ctx.ModuleForTests("fizz-buzz", "android_arm64_armv8-a").Output("fizz-buzz") |
| 74 | linkFlags := fizzBuzz.Args["linkFlags"] |
| 75 | if !strings.Contains(linkFlags, "/libfoo.so") { |
| 76 | t.Errorf("missing shared dependency 'libfoo.so' in linkFlags: %#v", linkFlags) |
| 77 | } |
| 78 | } |