genrule_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. // Copyright 2018 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 cc
  15. import (
  16. "reflect"
  17. "testing"
  18. "android/soong/android"
  19. )
  20. func testGenruleContext(config android.Config) *android.TestContext {
  21. ctx := android.NewTestArchContext(config)
  22. ctx.RegisterModuleType("cc_genrule", GenRuleFactory)
  23. ctx.Register()
  24. return ctx
  25. }
  26. func TestArchGenruleCmd(t *testing.T) {
  27. fs := map[string][]byte{
  28. "tool": nil,
  29. "foo": nil,
  30. "bar": nil,
  31. }
  32. bp := `
  33. cc_genrule {
  34. name: "gen",
  35. tool_files: ["tool"],
  36. cmd: "$(location tool) $(in) $(out)",
  37. out: ["out_arm"],
  38. arch: {
  39. arm: {
  40. srcs: ["foo"],
  41. },
  42. arm64: {
  43. srcs: ["bar"],
  44. },
  45. },
  46. }
  47. `
  48. config := android.TestArchConfig(t.TempDir(), nil, bp, fs)
  49. ctx := testGenruleContext(config)
  50. _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
  51. if errs == nil {
  52. _, errs = ctx.PrepareBuildActions(config)
  53. }
  54. if errs != nil {
  55. t.Fatal(errs)
  56. }
  57. gen := ctx.ModuleForTests("gen", "android_arm_armv7-a-neon").Output("out_arm")
  58. expected := []string{"foo"}
  59. if !reflect.DeepEqual(expected, gen.Implicits.Strings()[:len(expected)]) {
  60. t.Errorf(`want arm inputs %v, got %v`, expected, gen.Implicits.Strings())
  61. }
  62. gen = ctx.ModuleForTests("gen", "android_arm64_armv8-a").Output("out_arm")
  63. expected = []string{"bar"}
  64. if !reflect.DeepEqual(expected, gen.Implicits.Strings()[:len(expected)]) {
  65. t.Errorf(`want arm64 inputs %v, got %v`, expected, gen.Implicits.Strings())
  66. }
  67. }
  68. func TestLibraryGenruleCmd(t *testing.T) {
  69. bp := `
  70. cc_library {
  71. name: "libboth",
  72. }
  73. cc_library_shared {
  74. name: "libshared",
  75. }
  76. cc_library_static {
  77. name: "libstatic",
  78. }
  79. cc_genrule {
  80. name: "gen",
  81. tool_files: ["tool"],
  82. srcs: [
  83. ":libboth",
  84. ":libshared",
  85. ":libstatic",
  86. ],
  87. cmd: "$(location tool) $(in) $(out)",
  88. out: ["out"],
  89. }
  90. `
  91. ctx := testCc(t, bp)
  92. gen := ctx.ModuleForTests("gen", "android_arm_armv7-a-neon").Output("out")
  93. expected := []string{"libboth.so", "libshared.so", "libstatic.a"}
  94. var got []string
  95. for _, input := range gen.Implicits {
  96. got = append(got, input.Base())
  97. }
  98. if !reflect.DeepEqual(expected, got[:len(expected)]) {
  99. t.Errorf(`want inputs %v, got %v`, expected, got)
  100. }
  101. }
  102. func TestCmdPrefix(t *testing.T) {
  103. bp := `
  104. cc_genrule {
  105. name: "gen",
  106. cmd: "echo foo",
  107. out: ["out"],
  108. native_bridge_supported: true,
  109. }
  110. `
  111. testCases := []struct {
  112. name string
  113. variant string
  114. preparer android.FixturePreparer
  115. arch string
  116. nativeBridge string
  117. multilib string
  118. }{
  119. {
  120. name: "arm",
  121. variant: "android_arm_armv7-a-neon",
  122. arch: "arm",
  123. multilib: "lib32",
  124. },
  125. {
  126. name: "arm64",
  127. variant: "android_arm64_armv8-a",
  128. arch: "arm64",
  129. multilib: "lib64",
  130. },
  131. {
  132. name: "nativebridge",
  133. variant: "android_native_bridge_arm_armv7-a-neon",
  134. preparer: android.FixtureModifyConfig(func(config android.Config) {
  135. config.Targets[android.Android] = []android.Target{
  136. {
  137. Os: android.Android,
  138. Arch: android.Arch{ArchType: android.X86, ArchVariant: "silvermont", Abi: []string{"armeabi-v7a"}},
  139. NativeBridge: android.NativeBridgeDisabled,
  140. },
  141. {
  142. Os: android.Android,
  143. Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}},
  144. NativeBridge: android.NativeBridgeEnabled,
  145. NativeBridgeHostArchName: "x86",
  146. NativeBridgeRelativePath: "arm",
  147. },
  148. }
  149. }),
  150. arch: "arm",
  151. multilib: "lib32",
  152. nativeBridge: "arm",
  153. },
  154. }
  155. for _, tt := range testCases {
  156. t.Run(tt.name, func(t *testing.T) {
  157. result := android.GroupFixturePreparers(
  158. PrepareForIntegrationTestWithCc,
  159. android.OptionalFixturePreparer(tt.preparer),
  160. ).RunTestWithBp(t, bp)
  161. gen := result.ModuleForTests("gen", tt.variant)
  162. sboxProto := android.RuleBuilderSboxProtoForTests(t, gen.Output("genrule.sbox.textproto"))
  163. cmd := *sboxProto.Commands[0].Command
  164. android.AssertStringDoesContain(t, "incorrect CC_ARCH", cmd, "CC_ARCH="+tt.arch+" ")
  165. android.AssertStringDoesContain(t, "incorrect CC_NATIVE_BRIDGE", cmd, "CC_NATIVE_BRIDGE="+tt.nativeBridge+" ")
  166. android.AssertStringDoesContain(t, "incorrect CC_MULTILIB", cmd, "CC_MULTILIB="+tt.multilib+" ")
  167. })
  168. }
  169. }