blob: 0cdf704917f82548d89364bf8186cb2302a72251 [file] [log] [blame]
Vinh Tran2e7b0fd2023-03-27 11:30:19 -04001// 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
15package rust
16
17import (
18 "android/soong/android"
Vinh Tran611f0362023-03-09 22:07:19 -050019 "android/soong/cc"
Vinh Tran2e7b0fd2023-03-27 11:30:19 -040020 "fmt"
21 "strings"
22 "testing"
23)
24
25func 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 Tran611f0362023-03-09 22:07:19 -050035 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 Tran2e7b0fd2023-03-27 11:30:19 -040050 rustMockedFiles.AddToFixture(),
51 ).RunTestWithBp(t, bp)
52
53 foo := result.ModuleForTests("foo", "android_arm64_armv8-a").Rule("rustc")
54
Vinh Tran611f0362023-03-09 22:07:19 -050055 expectedCFlag := fmt.Sprintf(afdoFlagFormat, "afdo_profiles_package/foo.afdo")
Vinh Tran2e7b0fd2023-03-27 11:30:19 -040056
Wen-yi Chu41326c12023-09-22 03:58:59 +000057 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 Tran2e7b0fd2023-03-27 11:30:19 -040059 }
60}
61
62func 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 Tran611f0362023-03-09 22:07:19 -050073 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 Tran2e7b0fd2023-03-27 11:30:19 -040096 rustMockedFiles.AddToFixture(),
97 ).RunTestWithBp(t, bp)
98
Wen-yi Chu41326c12023-09-22 03:58:59 +000099 fooArm := result.ModuleForTests("foo", "android_arm_armv7-a-neon").Rule("rustc")
100 fooArm64 := result.ModuleForTests("foo", "android_arm64_armv8-a").Rule("rustc")
Vinh Tran2e7b0fd2023-03-27 11:30:19 -0400101
Vinh Tran611f0362023-03-09 22:07:19 -0500102 expectedCFlagArm := fmt.Sprintf(afdoFlagFormat, "afdo_profiles_package/foo_arm.afdo")
103 expectedCFlagArm64 := fmt.Sprintf(afdoFlagFormat, "afdo_profiles_package/foo_arm64.afdo")
Vinh Tran2e7b0fd2023-03-27 11:30:19 -0400104
Wen-yi Chu41326c12023-09-22 03:58:59 +0000105 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 Tran2e7b0fd2023-03-27 11:30:19 -0400107 }
108
Wen-yi Chu41326c12023-09-22 03:58:59 +0000109 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 Tran2e7b0fd2023-03-27 11:30:19 -0400111 }
112}