afdo_test.go 3.3 KB

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