xref: /aosp_15_r20/build/soong/rust/afdo_test.go (revision 333d2b3687b3a337dbcca9d65000bca186795e39)
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
15package rust
16
17import (
18	"android/soong/android"
19	"android/soong/cc"
20	"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,
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(),
50		rustMockedFiles.AddToFixture(),
51	).RunTestWithBp(t, bp)
52
53	foo := result.ModuleForTests("foo", "android_arm64_armv8-a").Rule("rustc")
54
55	expectedCFlag := fmt.Sprintf(afdoFlagFormat, "afdo_profiles_package/foo.afdo")
56
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"])
59	}
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,
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(),
96		rustMockedFiles.AddToFixture(),
97	).RunTestWithBp(t, bp)
98
99	fooArm := result.ModuleForTests("foo", "android_arm_armv7-a-neon").Rule("rustc")
100	fooArm64 := result.ModuleForTests("foo", "android_arm64_armv8-a").Rule("rustc")
101
102	expectedCFlagArm := fmt.Sprintf(afdoFlagFormat, "afdo_profiles_package/foo_arm.afdo")
103	expectedCFlagArm64 := fmt.Sprintf(afdoFlagFormat, "afdo_profiles_package/foo_arm64.afdo")
104
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"])
107	}
108
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"])
111	}
112}
113