prebuilt_test.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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. "runtime"
  17. "testing"
  18. "android/soong/android"
  19. "github.com/google/blueprint"
  20. )
  21. var prepareForPrebuiltTest = android.GroupFixturePreparers(
  22. prepareForCcTest,
  23. android.PrepareForTestWithAndroidMk,
  24. )
  25. func testPrebuilt(t *testing.T, bp string, fs android.MockFS, handlers ...android.FixturePreparer) *android.TestContext {
  26. result := android.GroupFixturePreparers(
  27. prepareForPrebuiltTest,
  28. fs.AddToFixture(),
  29. android.GroupFixturePreparers(handlers...),
  30. ).RunTestWithBp(t, bp)
  31. return result.TestContext
  32. }
  33. type configCustomizer func(config android.Config)
  34. func TestPrebuilt(t *testing.T) {
  35. bp := `
  36. cc_library {
  37. name: "liba",
  38. }
  39. cc_prebuilt_library_shared {
  40. name: "liba",
  41. srcs: ["liba.so"],
  42. }
  43. cc_library {
  44. name: "libb",
  45. }
  46. cc_prebuilt_library_static {
  47. name: "libb",
  48. srcs: ["libb.a"],
  49. }
  50. cc_library_shared {
  51. name: "libd",
  52. }
  53. cc_prebuilt_library_shared {
  54. name: "libd",
  55. srcs: ["libd.so"],
  56. }
  57. cc_library_static {
  58. name: "libe",
  59. }
  60. cc_prebuilt_library_static {
  61. name: "libe",
  62. srcs: ["libe.a"],
  63. }
  64. cc_library {
  65. name: "libf",
  66. }
  67. cc_prebuilt_library {
  68. name: "libf",
  69. static: {
  70. srcs: ["libf.a"],
  71. },
  72. shared: {
  73. srcs: ["libf.so"],
  74. },
  75. }
  76. cc_object {
  77. name: "crtx",
  78. }
  79. cc_prebuilt_object {
  80. name: "crtx",
  81. srcs: ["crtx.o"],
  82. }
  83. `
  84. ctx := testPrebuilt(t, bp, map[string][]byte{
  85. "liba.so": nil,
  86. "libb.a": nil,
  87. "libd.so": nil,
  88. "libe.a": nil,
  89. "libf.a": nil,
  90. "libf.so": nil,
  91. "crtx.o": nil,
  92. })
  93. // Verify that all the modules exist and that their dependencies were connected correctly
  94. liba := ctx.ModuleForTests("liba", "android_arm64_armv8-a_shared").Module()
  95. libb := ctx.ModuleForTests("libb", "android_arm64_armv8-a_static").Module()
  96. libd := ctx.ModuleForTests("libd", "android_arm64_armv8-a_shared").Module()
  97. libe := ctx.ModuleForTests("libe", "android_arm64_armv8-a_static").Module()
  98. libfStatic := ctx.ModuleForTests("libf", "android_arm64_armv8-a_static").Module()
  99. libfShared := ctx.ModuleForTests("libf", "android_arm64_armv8-a_shared").Module()
  100. crtx := ctx.ModuleForTests("crtx", "android_arm64_armv8-a").Module()
  101. prebuiltLiba := ctx.ModuleForTests("prebuilt_liba", "android_arm64_armv8-a_shared").Module()
  102. prebuiltLibb := ctx.ModuleForTests("prebuilt_libb", "android_arm64_armv8-a_static").Module()
  103. prebuiltLibd := ctx.ModuleForTests("prebuilt_libd", "android_arm64_armv8-a_shared").Module()
  104. prebuiltLibe := ctx.ModuleForTests("prebuilt_libe", "android_arm64_armv8-a_static").Module()
  105. prebuiltLibfStatic := ctx.ModuleForTests("prebuilt_libf", "android_arm64_armv8-a_static").Module()
  106. prebuiltLibfShared := ctx.ModuleForTests("prebuilt_libf", "android_arm64_armv8-a_shared").Module()
  107. prebuiltCrtx := ctx.ModuleForTests("prebuilt_crtx", "android_arm64_armv8-a").Module()
  108. hasDep := func(m android.Module, wantDep android.Module) bool {
  109. t.Helper()
  110. var found bool
  111. ctx.VisitDirectDeps(m, func(dep blueprint.Module) {
  112. if dep == wantDep {
  113. found = true
  114. }
  115. })
  116. return found
  117. }
  118. if !hasDep(liba, prebuiltLiba) {
  119. t.Errorf("liba missing dependency on prebuilt_liba")
  120. }
  121. if !hasDep(libb, prebuiltLibb) {
  122. t.Errorf("libb missing dependency on prebuilt_libb")
  123. }
  124. if !hasDep(libd, prebuiltLibd) {
  125. t.Errorf("libd missing dependency on prebuilt_libd")
  126. }
  127. if !hasDep(libe, prebuiltLibe) {
  128. t.Errorf("libe missing dependency on prebuilt_libe")
  129. }
  130. if !hasDep(libfStatic, prebuiltLibfStatic) {
  131. t.Errorf("libf static missing dependency on prebuilt_libf")
  132. }
  133. if !hasDep(libfShared, prebuiltLibfShared) {
  134. t.Errorf("libf shared missing dependency on prebuilt_libf")
  135. }
  136. if !hasDep(crtx, prebuiltCrtx) {
  137. t.Errorf("crtx missing dependency on prebuilt_crtx")
  138. }
  139. }
  140. func TestPrebuiltLibraryShared(t *testing.T) {
  141. ctx := testPrebuilt(t, `
  142. cc_prebuilt_library_shared {
  143. name: "libtest",
  144. srcs: ["libf.so"],
  145. strip: {
  146. none: true,
  147. },
  148. }
  149. `, map[string][]byte{
  150. "libf.so": nil,
  151. })
  152. shared := ctx.ModuleForTests("libtest", "android_arm64_armv8-a_shared").Module().(*Module)
  153. assertString(t, shared.OutputFile().Path().Base(), "libtest.so")
  154. }
  155. func TestPrebuiltLibraryStatic(t *testing.T) {
  156. ctx := testPrebuilt(t, `
  157. cc_prebuilt_library_static {
  158. name: "libtest",
  159. srcs: ["libf.a"],
  160. }
  161. `, map[string][]byte{
  162. "libf.a": nil,
  163. })
  164. static := ctx.ModuleForTests("libtest", "android_arm64_armv8-a_static").Module().(*Module)
  165. assertString(t, static.OutputFile().Path().Base(), "libf.a")
  166. }
  167. func TestPrebuiltLibrary(t *testing.T) {
  168. ctx := testPrebuilt(t, `
  169. cc_prebuilt_library {
  170. name: "libtest",
  171. static: {
  172. srcs: ["libf.a"],
  173. },
  174. shared: {
  175. srcs: ["libf.so"],
  176. },
  177. strip: {
  178. none: true,
  179. },
  180. }
  181. `, map[string][]byte{
  182. "libf.a": nil,
  183. "libf.so": nil,
  184. })
  185. shared := ctx.ModuleForTests("libtest", "android_arm64_armv8-a_shared").Module().(*Module)
  186. assertString(t, shared.OutputFile().Path().Base(), "libtest.so")
  187. static := ctx.ModuleForTests("libtest", "android_arm64_armv8-a_static").Module().(*Module)
  188. assertString(t, static.OutputFile().Path().Base(), "libf.a")
  189. }
  190. func TestPrebuiltLibraryStem(t *testing.T) {
  191. ctx := testPrebuilt(t, `
  192. cc_prebuilt_library {
  193. name: "libfoo",
  194. stem: "libbar",
  195. static: {
  196. srcs: ["libfoo.a"],
  197. },
  198. shared: {
  199. srcs: ["libfoo.so"],
  200. },
  201. strip: {
  202. none: true,
  203. },
  204. }
  205. `, map[string][]byte{
  206. "libfoo.a": nil,
  207. "libfoo.so": nil,
  208. })
  209. static := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Module().(*Module)
  210. assertString(t, static.OutputFile().Path().Base(), "libfoo.a")
  211. shared := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*Module)
  212. assertString(t, shared.OutputFile().Path().Base(), "libbar.so")
  213. }
  214. func TestPrebuiltLibrarySharedStem(t *testing.T) {
  215. ctx := testPrebuilt(t, `
  216. cc_prebuilt_library_shared {
  217. name: "libfoo",
  218. stem: "libbar",
  219. srcs: ["libfoo.so"],
  220. strip: {
  221. none: true,
  222. },
  223. }
  224. `, map[string][]byte{
  225. "libfoo.so": nil,
  226. })
  227. shared := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*Module)
  228. assertString(t, shared.OutputFile().Path().Base(), "libbar.so")
  229. }
  230. func TestPrebuiltSymlinkedHostBinary(t *testing.T) {
  231. if runtime.GOOS != "linux" {
  232. t.Skipf("Skipping host prebuilt testing that is only supported on linux not %s", runtime.GOOS)
  233. }
  234. ctx := testPrebuilt(t, `
  235. cc_prebuilt_library_shared {
  236. name: "libfoo",
  237. device_supported: false,
  238. host_supported: true,
  239. target: {
  240. linux_glibc_x86_64: {
  241. srcs: ["linux_glibc_x86_64/lib64/libfoo.so"],
  242. },
  243. },
  244. }
  245. cc_prebuilt_binary {
  246. name: "foo",
  247. device_supported: false,
  248. host_supported: true,
  249. shared_libs: ["libfoo"],
  250. target: {
  251. linux_glibc_x86_64: {
  252. srcs: ["linux_glibc_x86_64/bin/foo"],
  253. },
  254. },
  255. }
  256. `, map[string][]byte{
  257. "libfoo.so": nil,
  258. "foo": nil,
  259. })
  260. fooRule := ctx.ModuleForTests("foo", "linux_glibc_x86_64").Rule("Symlink")
  261. assertString(t, fooRule.Output.String(), "out/soong/.intermediates/foo/linux_glibc_x86_64/foo")
  262. assertString(t, fooRule.Args["fromPath"], "$$PWD/linux_glibc_x86_64/bin/foo")
  263. var libfooDep android.Path
  264. for _, dep := range fooRule.Implicits {
  265. if dep.Base() == "libfoo.so" {
  266. libfooDep = dep
  267. break
  268. }
  269. }
  270. assertString(t, libfooDep.String(), "out/soong/.intermediates/libfoo/linux_glibc_x86_64_shared/libfoo.so")
  271. }
  272. func TestPrebuiltLibrarySanitized(t *testing.T) {
  273. bp := `cc_prebuilt_library {
  274. name: "libtest",
  275. static: {
  276. sanitized: { none: { srcs: ["libf.a"], }, hwaddress: { srcs: ["libf.hwasan.a"], }, },
  277. },
  278. shared: {
  279. sanitized: { none: { srcs: ["libf.so"], }, hwaddress: { srcs: ["hwasan/libf.so"], }, },
  280. },
  281. }
  282. cc_prebuilt_library_static {
  283. name: "libtest_static",
  284. sanitized: { none: { srcs: ["libf.a"], }, hwaddress: { srcs: ["libf.hwasan.a"], }, },
  285. }
  286. cc_prebuilt_library_shared {
  287. name: "libtest_shared",
  288. sanitized: { none: { srcs: ["libf.so"], }, hwaddress: { srcs: ["hwasan/libf.so"], }, },
  289. }`
  290. fs := map[string][]byte{
  291. "libf.a": nil,
  292. "libf.hwasan.a": nil,
  293. "libf.so": nil,
  294. "hwasan/libf.so": nil,
  295. }
  296. // Without SANITIZE_TARGET.
  297. ctx := testPrebuilt(t, bp, fs)
  298. shared_rule := ctx.ModuleForTests("libtest", "android_arm64_armv8-a_shared").Rule("android/soong/cc.strip")
  299. assertString(t, shared_rule.Input.String(), "libf.so")
  300. static := ctx.ModuleForTests("libtest", "android_arm64_armv8-a_static").Module().(*Module)
  301. assertString(t, static.OutputFile().Path().Base(), "libf.a")
  302. shared_rule2 := ctx.ModuleForTests("libtest_shared", "android_arm64_armv8-a_shared").Rule("android/soong/cc.strip")
  303. assertString(t, shared_rule2.Input.String(), "libf.so")
  304. static2 := ctx.ModuleForTests("libtest_static", "android_arm64_armv8-a_static").Module().(*Module)
  305. assertString(t, static2.OutputFile().Path().Base(), "libf.a")
  306. // With SANITIZE_TARGET=hwaddress
  307. ctx = testPrebuilt(t, bp, fs,
  308. android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
  309. variables.SanitizeDevice = []string{"hwaddress"}
  310. }),
  311. )
  312. shared_rule = ctx.ModuleForTests("libtest", "android_arm64_armv8-a_shared_hwasan").Rule("android/soong/cc.strip")
  313. assertString(t, shared_rule.Input.String(), "hwasan/libf.so")
  314. static = ctx.ModuleForTests("libtest", "android_arm64_armv8-a_static_hwasan").Module().(*Module)
  315. assertString(t, static.OutputFile().Path().Base(), "libf.hwasan.a")
  316. shared_rule2 = ctx.ModuleForTests("libtest_shared", "android_arm64_armv8-a_shared_hwasan").Rule("android/soong/cc.strip")
  317. assertString(t, shared_rule2.Input.String(), "hwasan/libf.so")
  318. static2 = ctx.ModuleForTests("libtest_static", "android_arm64_armv8-a_static_hwasan").Module().(*Module)
  319. assertString(t, static2.OutputFile().Path().Base(), "libf.hwasan.a")
  320. }