object_test.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // Copyright 2019 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. "fmt"
  17. "testing"
  18. "android/soong/android"
  19. )
  20. func TestMinSdkVersionsOfCrtObjects(t *testing.T) {
  21. ctx := testCc(t, `
  22. cc_object {
  23. name: "crt_foo",
  24. srcs: ["foo.c"],
  25. crt: true,
  26. stl: "none",
  27. min_sdk_version: "28",
  28. vendor_available: true,
  29. }`)
  30. variants := []struct {
  31. variant string
  32. num string
  33. }{
  34. {"android_arm64_armv8-a", "10000"},
  35. {"android_arm64_armv8-a_sdk_28", "28"},
  36. {"android_arm64_armv8-a_sdk_29", "29"},
  37. {"android_arm64_armv8-a_sdk_30", "30"},
  38. {"android_arm64_armv8-a_sdk_current", "10000"},
  39. {"android_vendor.29_arm64_armv8-a", "29"},
  40. }
  41. for _, v := range variants {
  42. cflags := ctx.ModuleForTests("crt_foo", v.variant).Rule("cc").Args["cFlags"]
  43. expected := "-target aarch64-linux-android" + v.num + " "
  44. android.AssertStringDoesContain(t, "cflag", cflags, expected)
  45. }
  46. }
  47. func TestUseCrtObjectOfCorrectVersion(t *testing.T) {
  48. ctx := testCc(t, `
  49. cc_binary {
  50. name: "bin",
  51. srcs: ["foo.c"],
  52. stl: "none",
  53. min_sdk_version: "29",
  54. sdk_version: "current",
  55. }
  56. `)
  57. // Sdk variant uses the crt object of the matching min_sdk_version
  58. variant := "android_arm64_armv8-a_sdk"
  59. crt := ctx.ModuleForTests("bin", variant).Rule("ld").Args["crtBegin"]
  60. android.AssertStringDoesContain(t, "crt dep of sdk variant", crt,
  61. "29/crtbegin_dynamic.o")
  62. // platform variant uses the crt object built for platform
  63. variant = "android_arm64_armv8-a"
  64. crt = ctx.ModuleForTests("bin", variant).Rule("ld").Args["crtBegin"]
  65. android.AssertStringDoesContain(t, "crt dep of platform variant", crt,
  66. variant+"/crtbegin_dynamic.o")
  67. }
  68. func TestLinkerScript(t *testing.T) {
  69. t.Run("script", func(t *testing.T) {
  70. testCc(t, `
  71. cc_object {
  72. name: "foo",
  73. srcs: ["baz.o"],
  74. linker_script: "foo.lds",
  75. }`)
  76. })
  77. }
  78. func TestCcObjectWithBazel(t *testing.T) {
  79. bp := `
  80. cc_object {
  81. name: "foo",
  82. srcs: ["baz.o"],
  83. bazel_module: { label: "//foo/bar:bar" },
  84. }`
  85. config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
  86. config.BazelContext = android.MockBazelContext{
  87. OutputBaseDir: "outputbase",
  88. LabelToOutputFiles: map[string][]string{
  89. "//foo/bar:bar": []string{"bazel_out.o"}}}
  90. ctx := testCcWithConfig(t, config)
  91. module := ctx.ModuleForTests("foo", "android_arm_armv7-a-neon").Module()
  92. outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
  93. if err != nil {
  94. t.Errorf("Unexpected error getting cc_object outputfiles %s", err)
  95. }
  96. expectedOutputFiles := []string{"outputbase/execroot/__main__/bazel_out.o"}
  97. android.AssertDeepEquals(t, "output files", expectedOutputFiles, outputFiles.Strings())
  98. }
  99. func TestCcObjectOutputFile(t *testing.T) {
  100. testcases := []struct {
  101. name string
  102. moduleName string
  103. bp string
  104. }{
  105. {
  106. name: "normal",
  107. moduleName: "foo",
  108. bp: `
  109. srcs: ["bar.c"],
  110. `,
  111. },
  112. {
  113. name: "suffix",
  114. moduleName: "foo.o",
  115. bp: `
  116. srcs: ["bar.c"],
  117. `,
  118. },
  119. {
  120. name: "keep symbols",
  121. moduleName: "foo",
  122. bp: `
  123. srcs: ["bar.c"],
  124. prefix_symbols: "foo_",
  125. `,
  126. },
  127. {
  128. name: "partial linking",
  129. moduleName: "foo",
  130. bp: `
  131. srcs: ["bar.c", "baz.c"],
  132. `,
  133. },
  134. {
  135. name: "partial linking and prefix symbols",
  136. moduleName: "foo",
  137. bp: `
  138. srcs: ["bar.c", "baz.c"],
  139. prefix_symbols: "foo_",
  140. `,
  141. },
  142. }
  143. for _, testcase := range testcases {
  144. bp := fmt.Sprintf(`
  145. cc_object {
  146. name: "%s",
  147. %s
  148. }
  149. `, testcase.moduleName, testcase.bp)
  150. t.Run(testcase.name, func(t *testing.T) {
  151. ctx := PrepareForIntegrationTestWithCc.RunTestWithBp(t, bp)
  152. android.AssertPathRelativeToTopEquals(t, "expected output file foo.o",
  153. fmt.Sprintf("out/soong/.intermediates/%s/android_arm64_armv8-a/foo.o", testcase.moduleName),
  154. ctx.ModuleForTests(testcase.moduleName, "android_arm64_armv8-a").Output("foo.o").Output)
  155. })
  156. }
  157. }