library_stub_test.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. // Copyright 2021 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. _ "sort"
  18. "testing"
  19. "android/soong/android"
  20. "github.com/google/blueprint"
  21. )
  22. func hasDirectDependency(t *testing.T, ctx *android.TestResult, from android.Module, to android.Module) bool {
  23. t.Helper()
  24. var found bool
  25. ctx.VisitDirectDeps(from, func(dep blueprint.Module) {
  26. if dep == to {
  27. found = true
  28. }
  29. })
  30. return found
  31. }
  32. func TestApiLibraryReplacesExistingModule(t *testing.T) {
  33. bp := `
  34. cc_library {
  35. name: "libfoo",
  36. shared_libs: ["libbar"],
  37. vendor_available: true,
  38. }
  39. cc_library {
  40. name: "libbar",
  41. }
  42. cc_api_library {
  43. name: "libbar",
  44. vendor_available: true,
  45. src: "libbar.so",
  46. }
  47. api_imports {
  48. name: "api_imports",
  49. shared_libs: [
  50. "libbar",
  51. ],
  52. }
  53. `
  54. ctx := prepareForCcTest.RunTestWithBp(t, bp)
  55. libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
  56. libbar := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_shared").Module()
  57. libbarApiImport := ctx.ModuleForTests("libbar.apiimport", "android_arm64_armv8-a_shared").Module()
  58. android.AssertBoolEquals(t, "original library should be linked with non-stub variant", true, hasDirectDependency(t, ctx, libfoo, libbar))
  59. android.AssertBoolEquals(t, "Stub library from API surface should be not linked with non-stub variant", false, hasDirectDependency(t, ctx, libfoo, libbarApiImport))
  60. libfooVendor := ctx.ModuleForTests("libfoo", "android_vendor.29_arm64_armv8-a_shared").Module()
  61. libbarApiImportVendor := ctx.ModuleForTests("libbar.apiimport", "android_vendor.29_arm64_armv8-a_shared").Module()
  62. android.AssertBoolEquals(t, "original library should not be linked", false, hasDirectDependency(t, ctx, libfooVendor, libbar))
  63. android.AssertBoolEquals(t, "Stub library from API surface should be linked", true, hasDirectDependency(t, ctx, libfooVendor, libbarApiImportVendor))
  64. }
  65. func TestApiLibraryDoNotRequireOriginalModule(t *testing.T) {
  66. bp := `
  67. cc_library {
  68. name: "libfoo",
  69. shared_libs: ["libbar"],
  70. vendor: true,
  71. }
  72. cc_api_library {
  73. name: "libbar",
  74. src: "libbar.so",
  75. vendor_available: true,
  76. }
  77. api_imports {
  78. name: "api_imports",
  79. shared_libs: [
  80. "libbar",
  81. ],
  82. }
  83. `
  84. ctx := prepareForCcTest.RunTestWithBp(t, bp)
  85. libfoo := ctx.ModuleForTests("libfoo", "android_vendor.29_arm64_armv8-a_shared").Module()
  86. libbarApiImport := ctx.ModuleForTests("libbar.apiimport", "android_vendor.29_arm64_armv8-a_shared").Module()
  87. android.AssertBoolEquals(t, "Stub library from API surface should be linked", true, hasDirectDependency(t, ctx, libfoo, libbarApiImport))
  88. }
  89. func TestApiLibraryShouldNotReplaceWithoutApiImport(t *testing.T) {
  90. bp := `
  91. cc_library {
  92. name: "libfoo",
  93. shared_libs: ["libbar"],
  94. vendor_available: true,
  95. }
  96. cc_library {
  97. name: "libbar",
  98. vendor_available: true,
  99. }
  100. cc_api_library {
  101. name: "libbar",
  102. src: "libbar.so",
  103. vendor_available: true,
  104. }
  105. api_imports {
  106. name: "api_imports",
  107. shared_libs: [],
  108. }
  109. `
  110. ctx := prepareForCcTest.RunTestWithBp(t, bp)
  111. libfoo := ctx.ModuleForTests("libfoo", "android_vendor.29_arm64_armv8-a_shared").Module()
  112. libbar := ctx.ModuleForTests("libbar", "android_vendor.29_arm64_armv8-a_shared").Module()
  113. libbarApiImport := ctx.ModuleForTests("libbar.apiimport", "android_vendor.29_arm64_armv8-a_shared").Module()
  114. android.AssertBoolEquals(t, "original library should be linked", true, hasDirectDependency(t, ctx, libfoo, libbar))
  115. android.AssertBoolEquals(t, "Stub library from API surface should not be linked", false, hasDirectDependency(t, ctx, libfoo, libbarApiImport))
  116. }
  117. func TestExportDirFromStubLibrary(t *testing.T) {
  118. bp := `
  119. cc_library {
  120. name: "libfoo",
  121. export_include_dirs: ["source_include_dir"],
  122. export_system_include_dirs: ["source_system_include_dir"],
  123. vendor_available: true,
  124. }
  125. cc_api_library {
  126. name: "libfoo",
  127. export_include_dirs: ["stub_include_dir"],
  128. export_system_include_dirs: ["stub_system_include_dir"],
  129. vendor_available: true,
  130. src: "libfoo.so",
  131. }
  132. api_imports {
  133. name: "api_imports",
  134. shared_libs: [
  135. "libfoo",
  136. ],
  137. header_libs: [],
  138. }
  139. // vendor binary
  140. cc_binary {
  141. name: "vendorbin",
  142. vendor: true,
  143. srcs: ["vendor.cc"],
  144. shared_libs: ["libfoo"],
  145. }
  146. `
  147. ctx := prepareForCcTest.RunTestWithBp(t, bp)
  148. vendorCFlags := ctx.ModuleForTests("vendorbin", "android_vendor.29_arm64_armv8-a").Rule("cc").Args["cFlags"]
  149. android.AssertStringDoesContain(t, "Vendor binary should compile using headers provided by stub", vendorCFlags, "-Istub_include_dir")
  150. android.AssertStringDoesNotContain(t, "Vendor binary should not compile using headers of source", vendorCFlags, "-Isource_include_dir")
  151. android.AssertStringDoesContain(t, "Vendor binary should compile using system headers provided by stub", vendorCFlags, "-isystem stub_system_include_dir")
  152. android.AssertStringDoesNotContain(t, "Vendor binary should not compile using system headers of source", vendorCFlags, "-isystem source_system_include_dir")
  153. vendorImplicits := ctx.ModuleForTests("vendorbin", "android_vendor.29_arm64_armv8-a").Rule("cc").OrderOnly.Strings()
  154. // Building the stub.so file first assembles its .h files in multi-tree out.
  155. // These header files are required for compiling the other API domain (vendor in this case)
  156. android.AssertStringListContains(t, "Vendor binary compilation should have an implicit dep on the stub .so file", vendorImplicits, "libfoo.so")
  157. }
  158. func TestApiLibraryWithLlndkVariant(t *testing.T) {
  159. bp := `
  160. cc_binary {
  161. name: "binfoo",
  162. vendor: true,
  163. srcs: ["binfoo.cc"],
  164. shared_libs: ["libbar"],
  165. }
  166. cc_api_library {
  167. name: "libbar",
  168. // TODO(b/244244438) Remove src property once all variants are implemented.
  169. src: "libbar.so",
  170. vendor_available: true,
  171. variants: [
  172. "llndk",
  173. ],
  174. }
  175. cc_api_variant {
  176. name: "libbar",
  177. variant: "llndk",
  178. src: "libbar_llndk.so",
  179. export_include_dirs: ["libbar_llndk_include"]
  180. }
  181. api_imports {
  182. name: "api_imports",
  183. shared_libs: [
  184. "libbar",
  185. ],
  186. header_libs: [],
  187. }
  188. `
  189. ctx := prepareForCcTest.RunTestWithBp(t, bp)
  190. binfoo := ctx.ModuleForTests("binfoo", "android_vendor.29_arm64_armv8-a").Module()
  191. libbarApiImport := ctx.ModuleForTests("libbar.apiimport", "android_vendor.29_arm64_armv8-a_shared").Module()
  192. libbarApiVariant := ctx.ModuleForTests("libbar.llndk.apiimport", "android_vendor.29_arm64_armv8-a").Module()
  193. android.AssertBoolEquals(t, "Stub library from API surface should be linked", true, hasDirectDependency(t, ctx, binfoo, libbarApiImport))
  194. android.AssertBoolEquals(t, "Stub library variant from API surface should be linked", true, hasDirectDependency(t, ctx, libbarApiImport, libbarApiVariant))
  195. binFooLibFlags := ctx.ModuleForTests("binfoo", "android_vendor.29_arm64_armv8-a").Rule("ld").Args["libFlags"]
  196. android.AssertStringDoesContain(t, "Vendor binary should be linked with LLNDK variant source", binFooLibFlags, "libbar.llndk.apiimport.so")
  197. binFooCFlags := ctx.ModuleForTests("binfoo", "android_vendor.29_arm64_armv8-a").Rule("cc").Args["cFlags"]
  198. android.AssertStringDoesContain(t, "Vendor binary should include headers from the LLNDK variant source", binFooCFlags, "-Ilibbar_llndk_include")
  199. }
  200. func TestApiLibraryWithNdkVariant(t *testing.T) {
  201. bp := `
  202. cc_binary {
  203. name: "binfoo",
  204. sdk_version: "29",
  205. srcs: ["binfoo.cc"],
  206. shared_libs: ["libbar"],
  207. stl: "c++_shared",
  208. }
  209. cc_binary {
  210. name: "binbaz",
  211. sdk_version: "30",
  212. srcs: ["binbaz.cc"],
  213. shared_libs: ["libbar"],
  214. stl: "c++_shared",
  215. }
  216. cc_binary {
  217. name: "binqux",
  218. srcs: ["binfoo.cc"],
  219. shared_libs: ["libbar"],
  220. }
  221. cc_library {
  222. name: "libbar",
  223. srcs: ["libbar.cc"],
  224. }
  225. cc_api_library {
  226. name: "libbar",
  227. // TODO(b/244244438) Remove src property once all variants are implemented.
  228. src: "libbar.so",
  229. variants: [
  230. "ndk.29",
  231. "ndk.30",
  232. "ndk.current",
  233. ],
  234. }
  235. cc_api_variant {
  236. name: "libbar",
  237. variant: "ndk",
  238. version: "29",
  239. src: "libbar_ndk_29.so",
  240. export_include_dirs: ["libbar_ndk_29_include"]
  241. }
  242. cc_api_variant {
  243. name: "libbar",
  244. variant: "ndk",
  245. version: "30",
  246. src: "libbar_ndk_30.so",
  247. export_include_dirs: ["libbar_ndk_30_include"]
  248. }
  249. cc_api_variant {
  250. name: "libbar",
  251. variant: "ndk",
  252. version: "current",
  253. src: "libbar_ndk_current.so",
  254. export_include_dirs: ["libbar_ndk_current_include"]
  255. }
  256. api_imports {
  257. name: "api_imports",
  258. shared_libs: [
  259. "libbar",
  260. ],
  261. header_libs: [],
  262. }
  263. `
  264. ctx := prepareForCcTest.RunTestWithBp(t, bp)
  265. binfoo := ctx.ModuleForTests("binfoo", "android_arm64_armv8-a_sdk").Module()
  266. libbarApiImportv29 := ctx.ModuleForTests("libbar.apiimport", "android_arm64_armv8-a_sdk_shared_29").Module()
  267. libbarApiVariantv29 := ctx.ModuleForTests("libbar.ndk.29.apiimport", "android_arm64_armv8-a_sdk").Module()
  268. libbarApiImportv30 := ctx.ModuleForTests("libbar.apiimport", "android_arm64_armv8-a_sdk_shared_30").Module()
  269. libbarApiVariantv30 := ctx.ModuleForTests("libbar.ndk.30.apiimport", "android_arm64_armv8-a_sdk").Module()
  270. android.AssertBoolEquals(t, "Stub library from API surface should be linked with target version", true, hasDirectDependency(t, ctx, binfoo, libbarApiImportv29))
  271. android.AssertBoolEquals(t, "Stub library variant from API surface should be linked with target version", true, hasDirectDependency(t, ctx, libbarApiImportv29, libbarApiVariantv29))
  272. android.AssertBoolEquals(t, "Stub library from API surface should not be linked with different version", false, hasDirectDependency(t, ctx, binfoo, libbarApiImportv30))
  273. android.AssertBoolEquals(t, "Stub library variant from API surface should not be linked with different version", false, hasDirectDependency(t, ctx, libbarApiImportv29, libbarApiVariantv30))
  274. binbaz := ctx.ModuleForTests("binbaz", "android_arm64_armv8-a_sdk").Module()
  275. android.AssertBoolEquals(t, "Stub library from API surface should be linked with target version", true, hasDirectDependency(t, ctx, binbaz, libbarApiImportv30))
  276. android.AssertBoolEquals(t, "Stub library from API surface should not be linked with different version", false, hasDirectDependency(t, ctx, binbaz, libbarApiImportv29))
  277. binFooLibFlags := ctx.ModuleForTests("binfoo", "android_arm64_armv8-a_sdk").Rule("ld").Args["libFlags"]
  278. android.AssertStringDoesContain(t, "Binary using sdk should be linked with NDK variant source", binFooLibFlags, "libbar.ndk.29.apiimport.so")
  279. binFooCFlags := ctx.ModuleForTests("binfoo", "android_arm64_armv8-a_sdk").Rule("cc").Args["cFlags"]
  280. android.AssertStringDoesContain(t, "Binary using sdk should include headers from the NDK variant source", binFooCFlags, "-Ilibbar_ndk_29_include")
  281. binQux := ctx.ModuleForTests("binqux", "android_arm64_armv8-a").Module()
  282. android.AssertBoolEquals(t, "NDK Stub library from API surface should not be linked with nonSdk binary", false,
  283. (hasDirectDependency(t, ctx, binQux, libbarApiImportv30) || hasDirectDependency(t, ctx, binQux, libbarApiImportv29)))
  284. }
  285. func TestApiLibraryWithMultipleVariants(t *testing.T) {
  286. bp := `
  287. cc_binary {
  288. name: "binfoo",
  289. sdk_version: "29",
  290. srcs: ["binfoo.cc"],
  291. shared_libs: ["libbar"],
  292. stl: "c++_shared",
  293. }
  294. cc_binary {
  295. name: "binbaz",
  296. vendor: true,
  297. srcs: ["binbaz.cc"],
  298. shared_libs: ["libbar"],
  299. }
  300. cc_library {
  301. name: "libbar",
  302. srcs: ["libbar.cc"],
  303. }
  304. cc_api_library {
  305. name: "libbar",
  306. // TODO(b/244244438) Remove src property once all variants are implemented.
  307. src: "libbar.so",
  308. vendor_available: true,
  309. variants: [
  310. "llndk",
  311. "ndk.29",
  312. "ndk.30",
  313. "ndk.current",
  314. "apex.29",
  315. "apex.30",
  316. "apex.current",
  317. ],
  318. }
  319. cc_api_variant {
  320. name: "libbar",
  321. variant: "ndk",
  322. version: "29",
  323. src: "libbar_ndk_29.so",
  324. export_include_dirs: ["libbar_ndk_29_include"]
  325. }
  326. cc_api_variant {
  327. name: "libbar",
  328. variant: "ndk",
  329. version: "30",
  330. src: "libbar_ndk_30.so",
  331. export_include_dirs: ["libbar_ndk_30_include"]
  332. }
  333. cc_api_variant {
  334. name: "libbar",
  335. variant: "ndk",
  336. version: "current",
  337. src: "libbar_ndk_current.so",
  338. export_include_dirs: ["libbar_ndk_current_include"]
  339. }
  340. cc_api_variant {
  341. name: "libbar",
  342. variant: "apex",
  343. version: "29",
  344. src: "libbar_apex_29.so",
  345. export_include_dirs: ["libbar_apex_29_include"]
  346. }
  347. cc_api_variant {
  348. name: "libbar",
  349. variant: "apex",
  350. version: "30",
  351. src: "libbar_apex_30.so",
  352. export_include_dirs: ["libbar_apex_30_include"]
  353. }
  354. cc_api_variant {
  355. name: "libbar",
  356. variant: "apex",
  357. version: "current",
  358. src: "libbar_apex_current.so",
  359. export_include_dirs: ["libbar_apex_current_include"]
  360. }
  361. cc_api_variant {
  362. name: "libbar",
  363. variant: "llndk",
  364. src: "libbar_llndk.so",
  365. export_include_dirs: ["libbar_llndk_include"]
  366. }
  367. api_imports {
  368. name: "api_imports",
  369. shared_libs: [
  370. "libbar",
  371. ],
  372. apex_shared_libs: [
  373. "libbar",
  374. ],
  375. }
  376. `
  377. ctx := prepareForCcTest.RunTestWithBp(t, bp)
  378. binfoo := ctx.ModuleForTests("binfoo", "android_arm64_armv8-a_sdk").Module()
  379. libbarApiImportv29 := ctx.ModuleForTests("libbar.apiimport", "android_arm64_armv8-a_sdk_shared_29").Module()
  380. libbarApiImportLlndk := ctx.ModuleForTests("libbar.apiimport", "android_vendor.29_arm64_armv8-a_shared").Module()
  381. android.AssertBoolEquals(t, "Binary using SDK should be linked with API library from NDK variant", true, hasDirectDependency(t, ctx, binfoo, libbarApiImportv29))
  382. android.AssertBoolEquals(t, "Binary using SDK should not be linked with API library from LLNDK variant", false, hasDirectDependency(t, ctx, binfoo, libbarApiImportLlndk))
  383. binbaz := ctx.ModuleForTests("binbaz", "android_vendor.29_arm64_armv8-a").Module()
  384. android.AssertBoolEquals(t, "Vendor binary should be linked with API library from LLNDK variant", true, hasDirectDependency(t, ctx, binbaz, libbarApiImportLlndk))
  385. android.AssertBoolEquals(t, "Vendor binary should not be linked with API library from NDK variant", false, hasDirectDependency(t, ctx, binbaz, libbarApiImportv29))
  386. }