blob: fa20eef26000c2362c20fed8fd7f1fbd5b9f9269 [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"
19 "fmt"
20 "strings"
21 "testing"
22)
23
24func TestAfdoEnabled(t *testing.T) {
25 bp := `
26 rust_binary {
27 name: "foo",
28 srcs: ["foo.rs"],
29 afdo: true,
30 }
31`
32 result := android.GroupFixturePreparers(
33 prepareForRustTest,
34 android.FixtureAddTextFile("toolchain/pgo-profiles/sampling/foo.afdo", ""),
35 rustMockedFiles.AddToFixture(),
36 ).RunTestWithBp(t, bp)
37
38 foo := result.ModuleForTests("foo", "android_arm64_armv8-a").Rule("rustc")
39
40 expectedCFlag := fmt.Sprintf(afdoFlagFormat, "toolchain/pgo-profiles/sampling/foo.afdo")
41
42 if !strings.Contains(foo.Args["rustcFlags"], expectedCFlag) {
43 t.Errorf("Expected 'foo' to enable afdo, but did not find %q in cflags %q", expectedCFlag, foo.Args["rustcFlags"])
44 }
45}
46
47func TestAfdoEnabledWithMultiArchs(t *testing.T) {
48 bp := `
49 rust_binary {
50 name: "foo",
51 srcs: ["foo.rs"],
52 afdo: true,
53 compile_multilib: "both",
54 }
55`
56 result := android.GroupFixturePreparers(
57 prepareForRustTest,
58 android.FixtureAddTextFile("toolchain/pgo-profiles/sampling/foo_arm.afdo", ""),
59 android.FixtureAddTextFile("toolchain/pgo-profiles/sampling/foo_arm64.afdo", ""),
60 rustMockedFiles.AddToFixture(),
61 ).RunTestWithBp(t, bp)
62
63 fooArm := result.ModuleForTests("foo", "android_arm_armv7-a-neon").Rule("rustc")
64 fooArm64 := result.ModuleForTests("foo", "android_arm64_armv8-a").Rule("rustc")
65
66 expectedCFlagArm := fmt.Sprintf(afdoFlagFormat, "toolchain/pgo-profiles/sampling/foo_arm.afdo")
67 expectedCFlagArm64 := fmt.Sprintf(afdoFlagFormat, "toolchain/pgo-profiles/sampling/foo_arm64.afdo")
68
69 if !strings.Contains(fooArm.Args["rustcFlags"], expectedCFlagArm) {
70 t.Errorf("Expected 'fooArm' to enable afdo, but did not find %q in cflags %q", expectedCFlagArm, fooArm.Args["rustcFlags"])
71 }
72
73 if !strings.Contains(fooArm64.Args["rustcFlags"], expectedCFlagArm64) {
74 t.Errorf("Expected 'fooArm64' to enable afdo, but did not find %q in cflags %q", expectedCFlagArm64, fooArm64.Args["rustcFlags"])
75 }
76}