cc_library_shared_conversion_test.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794
  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 bp2build
  15. import (
  16. "fmt"
  17. "testing"
  18. "android/soong/android"
  19. "android/soong/cc"
  20. )
  21. const (
  22. // See cc/testing.go for more context
  23. // TODO(alexmarquez): Split out the preamble into common code?
  24. soongCcLibrarySharedPreamble = soongCcLibraryStaticPreamble
  25. )
  26. func registerCcLibrarySharedModuleTypes(ctx android.RegistrationContext) {
  27. cc.RegisterCCBuildComponents(ctx)
  28. ctx.RegisterModuleType("cc_library_headers", cc.LibraryHeaderFactory)
  29. ctx.RegisterModuleType("cc_library_static", cc.LibraryStaticFactory)
  30. ctx.RegisterModuleType("cc_library", cc.LibraryFactory)
  31. }
  32. func runCcLibrarySharedTestCase(t *testing.T, tc Bp2buildTestCase) {
  33. t.Helper()
  34. (&tc).ModuleTypeUnderTest = "cc_library_shared"
  35. (&tc).ModuleTypeUnderTestFactory = cc.LibrarySharedFactory
  36. RunBp2BuildTestCase(t, registerCcLibrarySharedModuleTypes, tc)
  37. }
  38. func TestCcLibrarySharedSimple(t *testing.T) {
  39. runCcLibrarySharedTestCase(t, Bp2buildTestCase{
  40. Description: "cc_library_shared simple overall test",
  41. Filesystem: map[string]string{
  42. // NOTE: include_dir headers *should not* appear in Bazel hdrs later (?)
  43. "include_dir_1/include_dir_1_a.h": "",
  44. "include_dir_1/include_dir_1_b.h": "",
  45. "include_dir_2/include_dir_2_a.h": "",
  46. "include_dir_2/include_dir_2_b.h": "",
  47. // NOTE: local_include_dir headers *should not* appear in Bazel hdrs later (?)
  48. "local_include_dir_1/local_include_dir_1_a.h": "",
  49. "local_include_dir_1/local_include_dir_1_b.h": "",
  50. "local_include_dir_2/local_include_dir_2_a.h": "",
  51. "local_include_dir_2/local_include_dir_2_b.h": "",
  52. // NOTE: export_include_dir headers *should* appear in Bazel hdrs later
  53. "export_include_dir_1/export_include_dir_1_a.h": "",
  54. "export_include_dir_1/export_include_dir_1_b.h": "",
  55. "export_include_dir_2/export_include_dir_2_a.h": "",
  56. "export_include_dir_2/export_include_dir_2_b.h": "",
  57. // NOTE: Soong implicitly includes headers in the current directory
  58. "implicit_include_1.h": "",
  59. "implicit_include_2.h": "",
  60. },
  61. Blueprint: soongCcLibrarySharedPreamble + `
  62. cc_library_headers {
  63. name: "header_lib_1",
  64. export_include_dirs: ["header_lib_1"],
  65. bazel_module: { bp2build_available: false },
  66. }
  67. cc_library_headers {
  68. name: "header_lib_2",
  69. export_include_dirs: ["header_lib_2"],
  70. bazel_module: { bp2build_available: false },
  71. }
  72. cc_library_shared {
  73. name: "shared_lib_1",
  74. srcs: ["shared_lib_1.cc"],
  75. bazel_module: { bp2build_available: false },
  76. }
  77. cc_library_shared {
  78. name: "shared_lib_2",
  79. srcs: ["shared_lib_2.cc"],
  80. bazel_module: { bp2build_available: false },
  81. }
  82. cc_library_static {
  83. name: "whole_static_lib_1",
  84. srcs: ["whole_static_lib_1.cc"],
  85. bazel_module: { bp2build_available: false },
  86. }
  87. cc_library_static {
  88. name: "whole_static_lib_2",
  89. srcs: ["whole_static_lib_2.cc"],
  90. bazel_module: { bp2build_available: false },
  91. }
  92. cc_library_shared {
  93. name: "foo_shared",
  94. srcs: [
  95. "foo_shared1.cc",
  96. "foo_shared2.cc",
  97. ],
  98. cflags: [
  99. "-Dflag1",
  100. "-Dflag2"
  101. ],
  102. shared_libs: [
  103. "shared_lib_1",
  104. "shared_lib_2"
  105. ],
  106. whole_static_libs: [
  107. "whole_static_lib_1",
  108. "whole_static_lib_2"
  109. ],
  110. include_dirs: [
  111. "include_dir_1",
  112. "include_dir_2",
  113. ],
  114. local_include_dirs: [
  115. "local_include_dir_1",
  116. "local_include_dir_2",
  117. ],
  118. export_include_dirs: [
  119. "export_include_dir_1",
  120. "export_include_dir_2"
  121. ],
  122. header_libs: [
  123. "header_lib_1",
  124. "header_lib_2"
  125. ],
  126. sdk_version: "current",
  127. min_sdk_version: "29",
  128. // TODO: Also support export_header_lib_headers
  129. }`,
  130. ExpectedBazelTargets: []string{
  131. MakeBazelTarget("cc_library_shared", "foo_shared", AttrNameToString{
  132. "absolute_includes": `[
  133. "include_dir_1",
  134. "include_dir_2",
  135. ]`,
  136. "copts": `[
  137. "-Dflag1",
  138. "-Dflag2",
  139. ]`,
  140. "export_includes": `[
  141. "export_include_dir_1",
  142. "export_include_dir_2",
  143. ]`,
  144. "implementation_deps": `[
  145. ":header_lib_1",
  146. ":header_lib_2",
  147. ]`,
  148. "implementation_dynamic_deps": `[
  149. ":shared_lib_1",
  150. ":shared_lib_2",
  151. ]`,
  152. "local_includes": `[
  153. "local_include_dir_1",
  154. "local_include_dir_2",
  155. ".",
  156. ]`,
  157. "srcs": `[
  158. "foo_shared1.cc",
  159. "foo_shared2.cc",
  160. ]`,
  161. "whole_archive_deps": `[
  162. ":whole_static_lib_1",
  163. ":whole_static_lib_2",
  164. ]`,
  165. "sdk_version": `"current"`,
  166. "min_sdk_version": `"29"`,
  167. }),
  168. },
  169. })
  170. }
  171. func TestCcLibrarySharedArchSpecificSharedLib(t *testing.T) {
  172. runCcLibrarySharedTestCase(t, Bp2buildTestCase{
  173. Description: "cc_library_shared arch-specific shared_libs with whole_static_libs",
  174. Filesystem: map[string]string{},
  175. Blueprint: soongCcLibrarySharedPreamble + `
  176. cc_library_static {
  177. name: "static_dep",
  178. bazel_module: { bp2build_available: false },
  179. }
  180. cc_library_shared {
  181. name: "shared_dep",
  182. bazel_module: { bp2build_available: false },
  183. }
  184. cc_library_shared {
  185. name: "foo_shared",
  186. arch: { arm64: { shared_libs: ["shared_dep"], whole_static_libs: ["static_dep"] } },
  187. include_build_directory: false,
  188. }`,
  189. ExpectedBazelTargets: []string{
  190. MakeBazelTarget("cc_library_shared", "foo_shared", AttrNameToString{
  191. "implementation_dynamic_deps": `select({
  192. "//build/bazel/platforms/arch:arm64": [":shared_dep"],
  193. "//conditions:default": [],
  194. })`,
  195. "whole_archive_deps": `select({
  196. "//build/bazel/platforms/arch:arm64": [":static_dep"],
  197. "//conditions:default": [],
  198. })`,
  199. }),
  200. },
  201. })
  202. }
  203. func TestCcLibrarySharedOsSpecificSharedLib(t *testing.T) {
  204. runCcLibrarySharedTestCase(t, Bp2buildTestCase{
  205. Description: "cc_library_shared os-specific shared_libs",
  206. Filesystem: map[string]string{},
  207. Blueprint: soongCcLibrarySharedPreamble + `
  208. cc_library_shared {
  209. name: "shared_dep",
  210. bazel_module: { bp2build_available: false },
  211. }
  212. cc_library_shared {
  213. name: "foo_shared",
  214. target: { android: { shared_libs: ["shared_dep"], } },
  215. include_build_directory: false,
  216. }`,
  217. ExpectedBazelTargets: []string{
  218. MakeBazelTarget("cc_library_shared", "foo_shared", AttrNameToString{
  219. "implementation_dynamic_deps": `select({
  220. "//build/bazel/platforms/os:android": [":shared_dep"],
  221. "//conditions:default": [],
  222. })`,
  223. }),
  224. },
  225. })
  226. }
  227. func TestCcLibrarySharedBaseArchOsSpecificSharedLib(t *testing.T) {
  228. runCcLibrarySharedTestCase(t, Bp2buildTestCase{
  229. Description: "cc_library_shared base, arch, and os-specific shared_libs",
  230. Filesystem: map[string]string{},
  231. Blueprint: soongCcLibrarySharedPreamble + `
  232. cc_library_shared {
  233. name: "shared_dep",
  234. bazel_module: { bp2build_available: false },
  235. }
  236. cc_library_shared {
  237. name: "shared_dep2",
  238. bazel_module: { bp2build_available: false },
  239. }
  240. cc_library_shared {
  241. name: "shared_dep3",
  242. bazel_module: { bp2build_available: false },
  243. }
  244. cc_library_shared {
  245. name: "foo_shared",
  246. shared_libs: ["shared_dep"],
  247. target: { android: { shared_libs: ["shared_dep2"] } },
  248. arch: { arm64: { shared_libs: ["shared_dep3"] } },
  249. include_build_directory: false,
  250. }`,
  251. ExpectedBazelTargets: []string{
  252. MakeBazelTarget("cc_library_shared", "foo_shared", AttrNameToString{
  253. "implementation_dynamic_deps": `[":shared_dep"] + select({
  254. "//build/bazel/platforms/arch:arm64": [":shared_dep3"],
  255. "//conditions:default": [],
  256. }) + select({
  257. "//build/bazel/platforms/os:android": [":shared_dep2"],
  258. "//conditions:default": [],
  259. })`,
  260. }),
  261. },
  262. })
  263. }
  264. func TestCcLibrarySharedSimpleExcludeSrcs(t *testing.T) {
  265. runCcLibrarySharedTestCase(t, Bp2buildTestCase{
  266. Description: "cc_library_shared simple exclude_srcs",
  267. Filesystem: map[string]string{
  268. "common.c": "",
  269. "foo-a.c": "",
  270. "foo-excluded.c": "",
  271. },
  272. Blueprint: soongCcLibrarySharedPreamble + `
  273. cc_library_shared {
  274. name: "foo_shared",
  275. srcs: ["common.c", "foo-*.c"],
  276. exclude_srcs: ["foo-excluded.c"],
  277. include_build_directory: false,
  278. }`,
  279. ExpectedBazelTargets: []string{
  280. MakeBazelTarget("cc_library_shared", "foo_shared", AttrNameToString{
  281. "srcs_c": `[
  282. "common.c",
  283. "foo-a.c",
  284. ]`,
  285. }),
  286. },
  287. })
  288. }
  289. func TestCcLibrarySharedStrip(t *testing.T) {
  290. runCcLibrarySharedTestCase(t, Bp2buildTestCase{
  291. Description: "cc_library_shared stripping",
  292. Filesystem: map[string]string{},
  293. Blueprint: soongCcLibrarySharedPreamble + `
  294. cc_library_shared {
  295. name: "foo_shared",
  296. strip: {
  297. keep_symbols: false,
  298. keep_symbols_and_debug_frame: true,
  299. keep_symbols_list: ["sym", "sym2"],
  300. all: true,
  301. none: false,
  302. },
  303. include_build_directory: false,
  304. }`,
  305. ExpectedBazelTargets: []string{
  306. MakeBazelTarget("cc_library_shared", "foo_shared", AttrNameToString{
  307. "strip": `{
  308. "all": True,
  309. "keep_symbols": False,
  310. "keep_symbols_and_debug_frame": True,
  311. "keep_symbols_list": [
  312. "sym",
  313. "sym2",
  314. ],
  315. "none": False,
  316. }`,
  317. }),
  318. },
  319. })
  320. }
  321. func TestCcLibrarySharedVersionScript(t *testing.T) {
  322. runCcLibrarySharedTestCase(t, Bp2buildTestCase{
  323. Description: "cc_library_shared version script",
  324. Filesystem: map[string]string{
  325. "version_script": "",
  326. },
  327. Blueprint: soongCcLibrarySharedPreamble + `
  328. cc_library_shared {
  329. name: "foo_shared",
  330. version_script: "version_script",
  331. include_build_directory: false,
  332. }`,
  333. ExpectedBazelTargets: []string{
  334. MakeBazelTarget("cc_library_shared", "foo_shared", AttrNameToString{
  335. "additional_linker_inputs": `["version_script"]`,
  336. "linkopts": `["-Wl,--version-script,$(location version_script)"]`,
  337. }),
  338. },
  339. })
  340. }
  341. func TestCcLibrarySharedNoCrtTrue(t *testing.T) {
  342. runCcLibrarySharedTestCase(t, Bp2buildTestCase{
  343. Description: "cc_library_shared - nocrt: true emits attribute",
  344. Filesystem: map[string]string{
  345. "impl.cpp": "",
  346. },
  347. Blueprint: soongCcLibraryPreamble + `
  348. cc_library_shared {
  349. name: "foo_shared",
  350. srcs: ["impl.cpp"],
  351. nocrt: true,
  352. include_build_directory: false,
  353. }
  354. `,
  355. ExpectedBazelTargets: []string{
  356. MakeBazelTarget("cc_library_shared", "foo_shared", AttrNameToString{
  357. "link_crt": `False`,
  358. "srcs": `["impl.cpp"]`,
  359. }),
  360. },
  361. })
  362. }
  363. func TestCcLibrarySharedNoCrtFalse(t *testing.T) {
  364. runCcLibrarySharedTestCase(t, Bp2buildTestCase{
  365. Description: "cc_library_shared - nocrt: false doesn't emit attribute",
  366. Filesystem: map[string]string{
  367. "impl.cpp": "",
  368. },
  369. Blueprint: soongCcLibraryPreamble + `
  370. cc_library_shared {
  371. name: "foo_shared",
  372. srcs: ["impl.cpp"],
  373. nocrt: false,
  374. include_build_directory: false,
  375. }
  376. `,
  377. ExpectedBazelTargets: []string{
  378. MakeBazelTarget("cc_library_shared", "foo_shared", AttrNameToString{
  379. "srcs": `["impl.cpp"]`,
  380. }),
  381. },
  382. })
  383. }
  384. func TestCcLibrarySharedNoCrtArchVariant(t *testing.T) {
  385. runCcLibrarySharedTestCase(t, Bp2buildTestCase{
  386. Description: "cc_library_shared - nocrt in select",
  387. Filesystem: map[string]string{
  388. "impl.cpp": "",
  389. },
  390. Blueprint: soongCcLibraryPreamble + `
  391. cc_library_shared {
  392. name: "foo_shared",
  393. srcs: ["impl.cpp"],
  394. arch: {
  395. arm: {
  396. nocrt: true,
  397. },
  398. x86: {
  399. nocrt: false,
  400. },
  401. },
  402. include_build_directory: false,
  403. }
  404. `,
  405. ExpectedErr: fmt.Errorf("module \"foo_shared\": nocrt is not supported for arch variants"),
  406. })
  407. }
  408. func TestCcLibrarySharedProto(t *testing.T) {
  409. runCcLibrarySharedTestCase(t, Bp2buildTestCase{
  410. Blueprint: soongCcProtoPreamble + `cc_library_shared {
  411. name: "foo",
  412. srcs: ["foo.proto"],
  413. proto: {
  414. export_proto_headers: true,
  415. },
  416. include_build_directory: false,
  417. }`,
  418. ExpectedBazelTargets: []string{
  419. MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
  420. "srcs": `["foo.proto"]`,
  421. }), MakeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
  422. "deps": `[":foo_proto"]`,
  423. }), MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
  424. "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
  425. "whole_archive_deps": `[":foo_cc_proto_lite"]`,
  426. }),
  427. },
  428. })
  429. }
  430. func TestCcLibrarySharedUseVersionLib(t *testing.T) {
  431. runCcLibrarySharedTestCase(t, Bp2buildTestCase{
  432. Filesystem: map[string]string{
  433. soongCcVersionLibBpPath: soongCcVersionLibBp,
  434. },
  435. Blueprint: soongCcProtoPreamble + `cc_library_shared {
  436. name: "foo",
  437. use_version_lib: true,
  438. include_build_directory: false,
  439. }`,
  440. ExpectedBazelTargets: []string{
  441. MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
  442. "use_version_lib": "True",
  443. "implementation_whole_archive_deps": `["//build/soong/cc/libbuildversion:libbuildversion"]`,
  444. }),
  445. },
  446. })
  447. }
  448. func TestCcLibrarySharedStubs(t *testing.T) {
  449. runCcLibrarySharedTestCase(t, Bp2buildTestCase{
  450. Description: "cc_library_shared stubs",
  451. ModuleTypeUnderTest: "cc_library_shared",
  452. ModuleTypeUnderTestFactory: cc.LibrarySharedFactory,
  453. Dir: "foo/bar",
  454. Filesystem: map[string]string{
  455. "foo/bar/Android.bp": `
  456. cc_library_shared {
  457. name: "a",
  458. stubs: { symbol_file: "a.map.txt", versions: ["28", "29", "current"] },
  459. bazel_module: { bp2build_available: true },
  460. include_build_directory: false,
  461. }
  462. `,
  463. },
  464. Blueprint: soongCcLibraryPreamble,
  465. ExpectedBazelTargets: []string{MakeBazelTarget("cc_library_shared", "a", AttrNameToString{
  466. "has_stubs": `True`,
  467. }),
  468. },
  469. },
  470. )
  471. }
  472. func TestCcLibrarySharedSystemSharedLibsSharedEmpty(t *testing.T) {
  473. runCcLibrarySharedTestCase(t, Bp2buildTestCase{
  474. Description: "cc_library_shared system_shared_libs empty shared default",
  475. ModuleTypeUnderTest: "cc_library_shared",
  476. ModuleTypeUnderTestFactory: cc.LibrarySharedFactory,
  477. Blueprint: soongCcLibrarySharedPreamble + `
  478. cc_defaults {
  479. name: "empty_defaults",
  480. shared: {
  481. system_shared_libs: [],
  482. },
  483. include_build_directory: false,
  484. }
  485. cc_library_shared {
  486. name: "empty",
  487. defaults: ["empty_defaults"],
  488. }
  489. `,
  490. ExpectedBazelTargets: []string{MakeBazelTarget("cc_library_shared", "empty", AttrNameToString{
  491. "system_dynamic_deps": "[]",
  492. })},
  493. })
  494. }
  495. func TestCcLibrarySharedConvertLex(t *testing.T) {
  496. runCcLibrarySharedTestCase(t, Bp2buildTestCase{
  497. Description: "cc_library_shared with lex files",
  498. ModuleTypeUnderTest: "cc_library_shared",
  499. ModuleTypeUnderTestFactory: cc.LibrarySharedFactory,
  500. Filesystem: map[string]string{
  501. "foo.c": "",
  502. "bar.cc": "",
  503. "foo1.l": "",
  504. "bar1.ll": "",
  505. "foo2.l": "",
  506. "bar2.ll": "",
  507. },
  508. Blueprint: `cc_library_shared {
  509. name: "foo_lib",
  510. srcs: ["foo.c", "bar.cc", "foo1.l", "foo2.l", "bar1.ll", "bar2.ll"],
  511. lex: { flags: ["--foo_flags"] },
  512. include_build_directory: false,
  513. bazel_module: { bp2build_available: true },
  514. }`,
  515. ExpectedBazelTargets: []string{
  516. MakeBazelTarget("genlex", "foo_lib_genlex_l", AttrNameToString{
  517. "srcs": `[
  518. "foo1.l",
  519. "foo2.l",
  520. ]`,
  521. "lexopts": `["--foo_flags"]`,
  522. }),
  523. MakeBazelTarget("genlex", "foo_lib_genlex_ll", AttrNameToString{
  524. "srcs": `[
  525. "bar1.ll",
  526. "bar2.ll",
  527. ]`,
  528. "lexopts": `["--foo_flags"]`,
  529. }),
  530. MakeBazelTarget("cc_library_shared", "foo_lib", AttrNameToString{
  531. "srcs": `[
  532. "bar.cc",
  533. ":foo_lib_genlex_ll",
  534. ]`,
  535. "srcs_c": `[
  536. "foo.c",
  537. ":foo_lib_genlex_l",
  538. ]`,
  539. }),
  540. },
  541. })
  542. }
  543. func TestCcLibrarySharedClangUnknownFlags(t *testing.T) {
  544. runCcLibrarySharedTestCase(t, Bp2buildTestCase{
  545. Blueprint: soongCcProtoPreamble + `cc_library_shared {
  546. name: "foo",
  547. conlyflags: ["-a", "-finline-functions"],
  548. cflags: ["-b","-finline-functions"],
  549. cppflags: ["-c", "-finline-functions"],
  550. ldflags: ["-d","-finline-functions", "-e"],
  551. include_build_directory: false,
  552. }`,
  553. ExpectedBazelTargets: []string{
  554. MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
  555. "conlyflags": `["-a"]`,
  556. "copts": `["-b"]`,
  557. "cppflags": `["-c"]`,
  558. "linkopts": `[
  559. "-d",
  560. "-e",
  561. ]`,
  562. }),
  563. },
  564. })
  565. }
  566. func TestCCLibraryFlagSpaceSplitting(t *testing.T) {
  567. runCcLibrarySharedTestCase(t, Bp2buildTestCase{
  568. Blueprint: soongCcProtoPreamble + `cc_library_shared {
  569. name: "foo",
  570. conlyflags: [ "-include header.h"],
  571. cflags: ["-include header.h"],
  572. cppflags: ["-include header.h"],
  573. version_script: "version_script",
  574. include_build_directory: false,
  575. }`,
  576. ExpectedBazelTargets: []string{
  577. MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
  578. "additional_linker_inputs": `["version_script"]`,
  579. "conlyflags": `[
  580. "-include",
  581. "header.h",
  582. ]`,
  583. "copts": `[
  584. "-include",
  585. "header.h",
  586. ]`,
  587. "cppflags": `[
  588. "-include",
  589. "header.h",
  590. ]`,
  591. "linkopts": `["-Wl,--version-script,$(location version_script)"]`,
  592. }),
  593. },
  594. })
  595. }
  596. func TestCCLibrarySharedRuntimeDeps(t *testing.T) {
  597. runCcLibrarySharedTestCase(t, Bp2buildTestCase{
  598. Blueprint: `cc_library_shared {
  599. name: "bar",
  600. }
  601. cc_library_shared {
  602. name: "foo",
  603. runtime_libs: ["foo"],
  604. }`,
  605. ExpectedBazelTargets: []string{
  606. MakeBazelTarget("cc_library_shared", "bar", AttrNameToString{
  607. "local_includes": `["."]`,
  608. }),
  609. MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
  610. "runtime_deps": `[":foo"]`,
  611. "local_includes": `["."]`,
  612. }),
  613. },
  614. })
  615. }
  616. func TestCcLibrarySharedEmptySuffix(t *testing.T) {
  617. runCcLibrarySharedTestCase(t, Bp2buildTestCase{
  618. Description: "cc_library_shared with empty suffix",
  619. Filesystem: map[string]string{
  620. "foo.c": "",
  621. },
  622. Blueprint: soongCcLibrarySharedPreamble + `
  623. cc_library_shared {
  624. name: "foo_shared",
  625. suffix: "",
  626. srcs: ["foo.c"],
  627. include_build_directory: false,
  628. }`,
  629. ExpectedBazelTargets: []string{
  630. MakeBazelTarget("cc_library_shared", "foo_shared", AttrNameToString{
  631. "srcs_c": `["foo.c"]`,
  632. "suffix": `""`,
  633. }),
  634. },
  635. })
  636. }
  637. func TestCcLibrarySharedSuffix(t *testing.T) {
  638. runCcLibrarySharedTestCase(t, Bp2buildTestCase{
  639. Description: "cc_library_shared with suffix",
  640. Filesystem: map[string]string{
  641. "foo.c": "",
  642. },
  643. Blueprint: soongCcLibrarySharedPreamble + `
  644. cc_library_shared {
  645. name: "foo_shared",
  646. suffix: "-suf",
  647. srcs: ["foo.c"],
  648. include_build_directory: false,
  649. }`,
  650. ExpectedBazelTargets: []string{
  651. MakeBazelTarget("cc_library_shared", "foo_shared", AttrNameToString{
  652. "srcs_c": `["foo.c"]`,
  653. "suffix": `"-suf"`,
  654. }),
  655. },
  656. })
  657. }
  658. func TestCcLibrarySharedArchVariantSuffix(t *testing.T) {
  659. runCcLibrarySharedTestCase(t, Bp2buildTestCase{
  660. Description: "cc_library_shared with arch-variant suffix",
  661. Filesystem: map[string]string{
  662. "foo.c": "",
  663. },
  664. Blueprint: soongCcLibrarySharedPreamble + `
  665. cc_library_shared {
  666. name: "foo_shared",
  667. arch: {
  668. arm64: { suffix: "-64" },
  669. arm: { suffix: "-32" },
  670. },
  671. srcs: ["foo.c"],
  672. include_build_directory: false,
  673. }`,
  674. ExpectedBazelTargets: []string{
  675. MakeBazelTarget("cc_library_shared", "foo_shared", AttrNameToString{
  676. "srcs_c": `["foo.c"]`,
  677. "suffix": `select({
  678. "//build/bazel/platforms/arch:arm": "-32",
  679. "//build/bazel/platforms/arch:arm64": "-64",
  680. "//conditions:default": None,
  681. })`,
  682. }),
  683. },
  684. })
  685. }
  686. func TestCcLibrarySharedWithSyspropSrcs(t *testing.T) {
  687. runCcLibrarySharedTestCase(t, Bp2buildTestCase{
  688. Description: "cc_library_shared with sysprop sources",
  689. Blueprint: `
  690. cc_library_shared {
  691. name: "foo",
  692. srcs: [
  693. "bar.sysprop",
  694. "baz.sysprop",
  695. "blah.cpp",
  696. ],
  697. min_sdk_version: "5",
  698. }`,
  699. ExpectedBazelTargets: []string{
  700. MakeBazelTarget("sysprop_library", "foo_sysprop_library", AttrNameToString{
  701. "srcs": `[
  702. "bar.sysprop",
  703. "baz.sysprop",
  704. ]`,
  705. }),
  706. MakeBazelTarget("cc_sysprop_library_static", "foo_cc_sysprop_library_static", AttrNameToString{
  707. "dep": `":foo_sysprop_library"`,
  708. "min_sdk_version": `"5"`,
  709. }),
  710. MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
  711. "srcs": `["blah.cpp"]`,
  712. "local_includes": `["."]`,
  713. "min_sdk_version": `"5"`,
  714. "whole_archive_deps": `[":foo_cc_sysprop_library_static"]`,
  715. }),
  716. },
  717. })
  718. }
  719. func TestCcLibrarySharedWithSyspropSrcsSomeConfigs(t *testing.T) {
  720. runCcLibrarySharedTestCase(t, Bp2buildTestCase{
  721. Description: "cc_library_shared with sysprop sources in some configs but not others",
  722. Blueprint: `
  723. cc_library_shared {
  724. name: "foo",
  725. srcs: [
  726. "blah.cpp",
  727. ],
  728. target: {
  729. android: {
  730. srcs: ["bar.sysprop"],
  731. },
  732. },
  733. min_sdk_version: "5",
  734. }`,
  735. ExpectedBazelTargets: []string{
  736. MakeBazelTarget("sysprop_library", "foo_sysprop_library", AttrNameToString{
  737. "srcs": `select({
  738. "//build/bazel/platforms/os:android": ["bar.sysprop"],
  739. "//conditions:default": [],
  740. })`,
  741. }),
  742. MakeBazelTarget("cc_sysprop_library_static", "foo_cc_sysprop_library_static", AttrNameToString{
  743. "dep": `":foo_sysprop_library"`,
  744. "min_sdk_version": `"5"`,
  745. }),
  746. MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
  747. "srcs": `["blah.cpp"]`,
  748. "local_includes": `["."]`,
  749. "min_sdk_version": `"5"`,
  750. "whole_archive_deps": `select({
  751. "//build/bazel/platforms/os:android": [":foo_cc_sysprop_library_static"],
  752. "//conditions:default": [],
  753. })`,
  754. }),
  755. },
  756. })
  757. }