| Vinh Tran | 2e7b0fd | 2023-03-27 11:30:19 -0400 | [diff] [blame] | 1 | // Copyright 2023 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 |  | 
|  | 15 | package rust | 
|  | 16 |  | 
|  | 17 | import ( | 
|  | 18 | "android/soong/android" | 
| Vinh Tran | 611f036 | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 19 | "android/soong/cc" | 
| Vinh Tran | 2e7b0fd | 2023-03-27 11:30:19 -0400 | [diff] [blame] | 20 | "fmt" | 
|  | 21 | "strings" | 
|  | 22 | "testing" | 
|  | 23 | ) | 
|  | 24 |  | 
|  | 25 | func TestAfdoEnabled(t *testing.T) { | 
|  | 26 | bp := ` | 
|  | 27 | rust_binary { | 
|  | 28 | name: "foo", | 
|  | 29 | srcs: ["foo.rs"], | 
|  | 30 | afdo: true, | 
|  | 31 | } | 
|  | 32 | ` | 
|  | 33 | result := android.GroupFixturePreparers( | 
|  | 34 | prepareForRustTest, | 
| Vinh Tran | 611f036 | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 35 | cc.PrepareForTestWithFdoProfile, | 
|  | 36 | android.FixtureAddTextFile("afdo_profiles_package/foo.afdo", ""), | 
|  | 37 | android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) { | 
|  | 38 | variables.AfdoProfiles = []string{ | 
|  | 39 | "foo://afdo_profiles_package:foo_afdo", | 
|  | 40 | } | 
|  | 41 | }), | 
|  | 42 | android.MockFS{ | 
|  | 43 | "afdo_profiles_package/Android.bp": []byte(` | 
|  | 44 | fdo_profile { | 
|  | 45 | name: "foo_afdo", | 
|  | 46 | profile: "foo.afdo", | 
|  | 47 | } | 
|  | 48 | `), | 
|  | 49 | }.AddToFixture(), | 
| Vinh Tran | 2e7b0fd | 2023-03-27 11:30:19 -0400 | [diff] [blame] | 50 | rustMockedFiles.AddToFixture(), | 
|  | 51 | ).RunTestWithBp(t, bp) | 
|  | 52 |  | 
|  | 53 | foo := result.ModuleForTests("foo", "android_arm64_armv8-a").Rule("rustc") | 
|  | 54 |  | 
| Vinh Tran | 611f036 | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 55 | expectedCFlag := fmt.Sprintf(afdoFlagFormat, "afdo_profiles_package/foo.afdo") | 
| Vinh Tran | 2e7b0fd | 2023-03-27 11:30:19 -0400 | [diff] [blame] | 56 |  | 
| Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame] | 57 | if !strings.Contains(foo.Args["rustcFlags"], expectedCFlag) { | 
|  | 58 | t.Errorf("Expected 'foo' to enable afdo, but did not find %q in cflags %q", expectedCFlag, foo.Args["rustcFlags"]) | 
| Vinh Tran | 2e7b0fd | 2023-03-27 11:30:19 -0400 | [diff] [blame] | 59 | } | 
|  | 60 | } | 
|  | 61 |  | 
|  | 62 | func TestAfdoEnabledWithMultiArchs(t *testing.T) { | 
|  | 63 | bp := ` | 
|  | 64 | rust_binary { | 
|  | 65 | name: "foo", | 
|  | 66 | srcs: ["foo.rs"], | 
|  | 67 | afdo: true, | 
|  | 68 | compile_multilib: "both", | 
|  | 69 | } | 
|  | 70 | ` | 
|  | 71 | result := android.GroupFixturePreparers( | 
|  | 72 | prepareForRustTest, | 
| Vinh Tran | 611f036 | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 73 | cc.PrepareForTestWithFdoProfile, | 
|  | 74 | android.FixtureAddTextFile("afdo_profiles_package/foo_arm.afdo", ""), | 
|  | 75 | android.FixtureAddTextFile("afdo_profiles_package/foo_arm64.afdo", ""), | 
|  | 76 | android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) { | 
|  | 77 | variables.AfdoProfiles = []string{ | 
|  | 78 | "foo://afdo_profiles_package:foo_afdo", | 
|  | 79 | } | 
|  | 80 | }), | 
|  | 81 | android.MockFS{ | 
|  | 82 | "afdo_profiles_package/Android.bp": []byte(` | 
|  | 83 | fdo_profile { | 
|  | 84 | name: "foo_afdo", | 
|  | 85 | arch: { | 
|  | 86 | arm: { | 
|  | 87 | profile: "foo_arm.afdo", | 
|  | 88 | }, | 
|  | 89 | arm64: { | 
|  | 90 | profile: "foo_arm64.afdo", | 
|  | 91 | } | 
|  | 92 | } | 
|  | 93 | } | 
|  | 94 | `), | 
|  | 95 | }.AddToFixture(), | 
| Vinh Tran | 2e7b0fd | 2023-03-27 11:30:19 -0400 | [diff] [blame] | 96 | rustMockedFiles.AddToFixture(), | 
|  | 97 | ).RunTestWithBp(t, bp) | 
|  | 98 |  | 
| Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame] | 99 | fooArm := result.ModuleForTests("foo", "android_arm_armv7-a-neon").Rule("rustc") | 
|  | 100 | fooArm64 := result.ModuleForTests("foo", "android_arm64_armv8-a").Rule("rustc") | 
| Vinh Tran | 2e7b0fd | 2023-03-27 11:30:19 -0400 | [diff] [blame] | 101 |  | 
| Vinh Tran | 611f036 | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 102 | expectedCFlagArm := fmt.Sprintf(afdoFlagFormat, "afdo_profiles_package/foo_arm.afdo") | 
|  | 103 | expectedCFlagArm64 := fmt.Sprintf(afdoFlagFormat, "afdo_profiles_package/foo_arm64.afdo") | 
| Vinh Tran | 2e7b0fd | 2023-03-27 11:30:19 -0400 | [diff] [blame] | 104 |  | 
| Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame] | 105 | if !strings.Contains(fooArm.Args["rustcFlags"], expectedCFlagArm) { | 
|  | 106 | t.Errorf("Expected 'fooArm' to enable afdo, but did not find %q in cflags %q", expectedCFlagArm, fooArm.Args["rustcFlags"]) | 
| Vinh Tran | 2e7b0fd | 2023-03-27 11:30:19 -0400 | [diff] [blame] | 107 | } | 
|  | 108 |  | 
| Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame] | 109 | if !strings.Contains(fooArm64.Args["rustcFlags"], expectedCFlagArm64) { | 
|  | 110 | t.Errorf("Expected 'fooArm64' to enable afdo, but did not find %q in cflags %q", expectedCFlagArm64, fooArm64.Args["rustcFlags"]) | 
| Vinh Tran | 2e7b0fd | 2023-03-27 11:30:19 -0400 | [diff] [blame] | 111 | } | 
|  | 112 | } |