prebuilt_test.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. "path/filepath"
  17. "testing"
  18. "android/soong/android"
  19. "github.com/google/blueprint"
  20. )
  21. func testPrebuilt(t *testing.T, bp string, fs map[string][]byte) *android.TestContext {
  22. config := TestConfig(buildDir, android.Android, nil, bp, fs)
  23. ctx := CreateTestContext()
  24. // Enable androidmk support.
  25. // * Register the singleton
  26. // * Configure that we are inside make
  27. // * Add CommonOS to ensure that androidmk processing works.
  28. android.RegisterAndroidMkBuildComponents(ctx)
  29. android.SetInMakeForTests(config)
  30. ctx.Register(config)
  31. _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
  32. android.FailIfErrored(t, errs)
  33. _, errs = ctx.PrepareBuildActions(config)
  34. android.FailIfErrored(t, errs)
  35. return ctx
  36. }
  37. func TestPrebuilt(t *testing.T) {
  38. bp := `
  39. cc_library {
  40. name: "liba",
  41. }
  42. cc_prebuilt_library_shared {
  43. name: "liba",
  44. srcs: ["liba.so"],
  45. }
  46. cc_library {
  47. name: "libb",
  48. }
  49. cc_prebuilt_library_static {
  50. name: "libb",
  51. srcs: ["libb.a"],
  52. }
  53. cc_library_shared {
  54. name: "libd",
  55. }
  56. cc_prebuilt_library_shared {
  57. name: "libd",
  58. srcs: ["libd.so"],
  59. }
  60. cc_library_static {
  61. name: "libe",
  62. }
  63. cc_prebuilt_library_static {
  64. name: "libe",
  65. srcs: ["libe.a"],
  66. }
  67. cc_library {
  68. name: "libf",
  69. }
  70. cc_prebuilt_library {
  71. name: "libf",
  72. static: {
  73. srcs: ["libf.a"],
  74. },
  75. shared: {
  76. srcs: ["libf.so"],
  77. },
  78. }
  79. cc_object {
  80. name: "crtx",
  81. }
  82. cc_prebuilt_object {
  83. name: "crtx",
  84. srcs: ["crtx.o"],
  85. }
  86. `
  87. ctx := testPrebuilt(t, bp, map[string][]byte{
  88. "liba.so": nil,
  89. "libb.a": nil,
  90. "libd.so": nil,
  91. "libe.a": nil,
  92. "libf.a": nil,
  93. "libf.so": nil,
  94. "crtx.o": nil,
  95. })
  96. // Verify that all the modules exist and that their dependencies were connected correctly
  97. liba := ctx.ModuleForTests("liba", "android_arm64_armv8-a_shared").Module()
  98. libb := ctx.ModuleForTests("libb", "android_arm64_armv8-a_static").Module()
  99. libd := ctx.ModuleForTests("libd", "android_arm64_armv8-a_shared").Module()
  100. libe := ctx.ModuleForTests("libe", "android_arm64_armv8-a_static").Module()
  101. libfStatic := ctx.ModuleForTests("libf", "android_arm64_armv8-a_static").Module()
  102. libfShared := ctx.ModuleForTests("libf", "android_arm64_armv8-a_shared").Module()
  103. crtx := ctx.ModuleForTests("crtx", "android_arm64_armv8-a").Module()
  104. prebuiltLiba := ctx.ModuleForTests("prebuilt_liba", "android_arm64_armv8-a_shared").Module()
  105. prebuiltLibb := ctx.ModuleForTests("prebuilt_libb", "android_arm64_armv8-a_static").Module()
  106. prebuiltLibd := ctx.ModuleForTests("prebuilt_libd", "android_arm64_armv8-a_shared").Module()
  107. prebuiltLibe := ctx.ModuleForTests("prebuilt_libe", "android_arm64_armv8-a_static").Module()
  108. prebuiltLibfStatic := ctx.ModuleForTests("prebuilt_libf", "android_arm64_armv8-a_static").Module()
  109. prebuiltLibfShared := ctx.ModuleForTests("prebuilt_libf", "android_arm64_armv8-a_shared").Module()
  110. prebuiltCrtx := ctx.ModuleForTests("prebuilt_crtx", "android_arm64_armv8-a").Module()
  111. hasDep := func(m android.Module, wantDep android.Module) bool {
  112. t.Helper()
  113. var found bool
  114. ctx.VisitDirectDeps(m, func(dep blueprint.Module) {
  115. if dep == wantDep {
  116. found = true
  117. }
  118. })
  119. return found
  120. }
  121. if !hasDep(liba, prebuiltLiba) {
  122. t.Errorf("liba missing dependency on prebuilt_liba")
  123. }
  124. if !hasDep(libb, prebuiltLibb) {
  125. t.Errorf("libb missing dependency on prebuilt_libb")
  126. }
  127. if !hasDep(libd, prebuiltLibd) {
  128. t.Errorf("libd missing dependency on prebuilt_libd")
  129. }
  130. if !hasDep(libe, prebuiltLibe) {
  131. t.Errorf("libe missing dependency on prebuilt_libe")
  132. }
  133. if !hasDep(libfStatic, prebuiltLibfStatic) {
  134. t.Errorf("libf static missing dependency on prebuilt_libf")
  135. }
  136. if !hasDep(libfShared, prebuiltLibfShared) {
  137. t.Errorf("libf shared missing dependency on prebuilt_libf")
  138. }
  139. if !hasDep(crtx, prebuiltCrtx) {
  140. t.Errorf("crtx missing dependency on prebuilt_crtx")
  141. }
  142. }
  143. func TestPrebuiltLibraryShared(t *testing.T) {
  144. ctx := testPrebuilt(t, `
  145. cc_prebuilt_library_shared {
  146. name: "libtest",
  147. srcs: ["libf.so"],
  148. strip: {
  149. none: true,
  150. },
  151. }
  152. `, map[string][]byte{
  153. "libf.so": nil,
  154. })
  155. shared := ctx.ModuleForTests("libtest", "android_arm64_armv8-a_shared").Module().(*Module)
  156. assertString(t, shared.OutputFile().Path().Base(), "libtest.so")
  157. }
  158. func TestPrebuiltLibraryStatic(t *testing.T) {
  159. ctx := testPrebuilt(t, `
  160. cc_prebuilt_library_static {
  161. name: "libtest",
  162. srcs: ["libf.a"],
  163. }
  164. `, map[string][]byte{
  165. "libf.a": nil,
  166. })
  167. static := ctx.ModuleForTests("libtest", "android_arm64_armv8-a_static").Module().(*Module)
  168. assertString(t, static.OutputFile().Path().Base(), "libf.a")
  169. }
  170. func TestPrebuiltLibrary(t *testing.T) {
  171. ctx := testPrebuilt(t, `
  172. cc_prebuilt_library {
  173. name: "libtest",
  174. static: {
  175. srcs: ["libf.a"],
  176. },
  177. shared: {
  178. srcs: ["libf.so"],
  179. },
  180. strip: {
  181. none: true,
  182. },
  183. }
  184. `, map[string][]byte{
  185. "libf.a": nil,
  186. "libf.so": nil,
  187. })
  188. shared := ctx.ModuleForTests("libtest", "android_arm64_armv8-a_shared").Module().(*Module)
  189. assertString(t, shared.OutputFile().Path().Base(), "libtest.so")
  190. static := ctx.ModuleForTests("libtest", "android_arm64_armv8-a_static").Module().(*Module)
  191. assertString(t, static.OutputFile().Path().Base(), "libf.a")
  192. }
  193. func TestPrebuiltLibraryStem(t *testing.T) {
  194. ctx := testPrebuilt(t, `
  195. cc_prebuilt_library {
  196. name: "libfoo",
  197. stem: "libbar",
  198. static: {
  199. srcs: ["libfoo.a"],
  200. },
  201. shared: {
  202. srcs: ["libfoo.so"],
  203. },
  204. strip: {
  205. none: true,
  206. },
  207. }
  208. `, map[string][]byte{
  209. "libfoo.a": nil,
  210. "libfoo.so": nil,
  211. })
  212. static := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Module().(*Module)
  213. assertString(t, static.OutputFile().Path().Base(), "libfoo.a")
  214. shared := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*Module)
  215. assertString(t, shared.OutputFile().Path().Base(), "libbar.so")
  216. }
  217. func TestPrebuiltLibrarySharedStem(t *testing.T) {
  218. ctx := testPrebuilt(t, `
  219. cc_prebuilt_library_shared {
  220. name: "libfoo",
  221. stem: "libbar",
  222. srcs: ["libfoo.so"],
  223. strip: {
  224. none: true,
  225. },
  226. }
  227. `, map[string][]byte{
  228. "libfoo.so": nil,
  229. })
  230. shared := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*Module)
  231. assertString(t, shared.OutputFile().Path().Base(), "libbar.so")
  232. }
  233. func TestPrebuiltSymlinkedHostBinary(t *testing.T) {
  234. if android.BuildOs != android.Linux {
  235. t.Skipf("Skipping host prebuilt testing that is only supported on %s not %s", android.Linux, android.BuildOs)
  236. }
  237. ctx := testPrebuilt(t, `
  238. cc_prebuilt_library_shared {
  239. name: "libfoo",
  240. device_supported: false,
  241. host_supported: true,
  242. target: {
  243. linux_glibc_x86_64: {
  244. srcs: ["linux_glibc_x86_64/lib64/libfoo.so"],
  245. },
  246. },
  247. }
  248. cc_prebuilt_binary {
  249. name: "foo",
  250. device_supported: false,
  251. host_supported: true,
  252. shared_libs: ["libfoo"],
  253. target: {
  254. linux_glibc_x86_64: {
  255. srcs: ["linux_glibc_x86_64/bin/foo"],
  256. },
  257. },
  258. }
  259. `, map[string][]byte{
  260. "libfoo.so": nil,
  261. "foo": nil,
  262. })
  263. fooRule := ctx.ModuleForTests("foo", "linux_glibc_x86_64").Rule("Symlink")
  264. assertString(t, fooRule.Output.String(),
  265. filepath.Join(buildDir, ".intermediates/foo/linux_glibc_x86_64/foo"))
  266. assertString(t, fooRule.Args["fromPath"], "$$PWD/linux_glibc_x86_64/bin/foo")
  267. var libfooDep android.Path
  268. for _, dep := range fooRule.Implicits {
  269. if dep.Base() == "libfoo.so" {
  270. libfooDep = dep
  271. break
  272. }
  273. }
  274. assertString(t, libfooDep.String(),
  275. filepath.Join(buildDir, ".intermediates/libfoo/linux_glibc_x86_64_shared/libfoo.so"))
  276. }