cc_object_conversion_test.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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. "testing"
  17. "android/soong/android"
  18. "android/soong/cc"
  19. )
  20. func registerCcObjectModuleTypes(ctx android.RegistrationContext) {
  21. // Always register cc_defaults module factory
  22. ctx.RegisterModuleType("cc_defaults", func() android.Module { return cc.DefaultsFactory() })
  23. ctx.RegisterModuleType("cc_library_headers", cc.LibraryHeaderFactory)
  24. }
  25. func runCcObjectTestCase(t *testing.T, tc Bp2buildTestCase) {
  26. t.Helper()
  27. (&tc).ModuleTypeUnderTest = "cc_object"
  28. (&tc).ModuleTypeUnderTestFactory = cc.ObjectFactory
  29. RunBp2BuildTestCase(t, registerCcObjectModuleTypes, tc)
  30. }
  31. func TestCcObjectSimple(t *testing.T) {
  32. runCcObjectTestCase(t, Bp2buildTestCase{
  33. Description: "simple cc_object generates cc_object with include header dep",
  34. Filesystem: map[string]string{
  35. "a/b/foo.h": "",
  36. "a/b/bar.h": "",
  37. "a/b/exclude.c": "",
  38. "a/b/c.c": "",
  39. },
  40. Blueprint: `cc_object {
  41. name: "foo",
  42. local_include_dirs: ["include"],
  43. system_shared_libs: [],
  44. cflags: [
  45. "-Wno-gcc-compat",
  46. "-Wall",
  47. "-Werror",
  48. ],
  49. srcs: [
  50. "a/b/*.c"
  51. ],
  52. exclude_srcs: ["a/b/exclude.c"],
  53. sdk_version: "current",
  54. min_sdk_version: "29",
  55. crt: true,
  56. }
  57. `,
  58. ExpectedBazelTargets: []string{
  59. MakeBazelTarget("cc_object", "foo", AttrNameToString{
  60. "copts": `[
  61. "-fno-addrsig",
  62. "-Wno-gcc-compat",
  63. "-Wall",
  64. "-Werror",
  65. ]`,
  66. "local_includes": `[
  67. "include",
  68. ".",
  69. ]`,
  70. "srcs": `["a/b/c.c"]`,
  71. "system_dynamic_deps": `[]`,
  72. "sdk_version": `"current"`,
  73. "min_sdk_version": `"29"`,
  74. "crt": "True",
  75. }),
  76. },
  77. })
  78. }
  79. func TestCcObjectDefaults(t *testing.T) {
  80. runCcObjectTestCase(t, Bp2buildTestCase{
  81. Blueprint: `cc_object {
  82. name: "foo",
  83. system_shared_libs: [],
  84. srcs: [
  85. "a/b/*.h",
  86. "a/b/c.c"
  87. ],
  88. defaults: ["foo_defaults"],
  89. }
  90. cc_defaults {
  91. name: "foo_defaults",
  92. defaults: ["foo_bar_defaults"],
  93. }
  94. cc_defaults {
  95. name: "foo_bar_defaults",
  96. cflags: [
  97. "-Werror",
  98. ],
  99. }
  100. `,
  101. ExpectedBazelTargets: []string{
  102. MakeBazelTarget("cc_object", "foo", AttrNameToString{
  103. "copts": `[
  104. "-Werror",
  105. "-fno-addrsig",
  106. ]`,
  107. "local_includes": `["."]`,
  108. "srcs": `["a/b/c.c"]`,
  109. "system_dynamic_deps": `[]`,
  110. }),
  111. }})
  112. }
  113. func TestCcObjectCcObjetDepsInObjs(t *testing.T) {
  114. runCcObjectTestCase(t, Bp2buildTestCase{
  115. Description: "cc_object with cc_object deps in objs props",
  116. Filesystem: map[string]string{
  117. "a/b/c.c": "",
  118. "x/y/z.c": "",
  119. },
  120. Blueprint: `cc_object {
  121. name: "foo",
  122. system_shared_libs: [],
  123. srcs: ["a/b/c.c"],
  124. objs: ["bar"],
  125. include_build_directory: false,
  126. }
  127. cc_object {
  128. name: "bar",
  129. system_shared_libs: [],
  130. srcs: ["x/y/z.c"],
  131. include_build_directory: false,
  132. }
  133. `,
  134. ExpectedBazelTargets: []string{
  135. MakeBazelTarget("cc_object", "bar", AttrNameToString{
  136. "copts": `["-fno-addrsig"]`,
  137. "srcs": `["x/y/z.c"]`,
  138. "system_dynamic_deps": `[]`,
  139. }), MakeBazelTarget("cc_object", "foo", AttrNameToString{
  140. "copts": `["-fno-addrsig"]`,
  141. "objs": `[":bar"]`,
  142. "srcs": `["a/b/c.c"]`,
  143. "system_dynamic_deps": `[]`,
  144. }),
  145. },
  146. })
  147. }
  148. func TestCcObjectIncludeBuildDirFalse(t *testing.T) {
  149. runCcObjectTestCase(t, Bp2buildTestCase{
  150. Description: "cc_object with include_build_dir: false",
  151. Filesystem: map[string]string{
  152. "a/b/c.c": "",
  153. "x/y/z.c": "",
  154. },
  155. Blueprint: `cc_object {
  156. name: "foo",
  157. system_shared_libs: [],
  158. srcs: ["a/b/c.c"],
  159. include_build_directory: false,
  160. }
  161. `,
  162. ExpectedBazelTargets: []string{
  163. MakeBazelTarget("cc_object", "foo", AttrNameToString{
  164. "copts": `["-fno-addrsig"]`,
  165. "srcs": `["a/b/c.c"]`,
  166. "system_dynamic_deps": `[]`,
  167. }),
  168. },
  169. })
  170. }
  171. func TestCcObjectProductVariable(t *testing.T) {
  172. runCcObjectTestCase(t, Bp2buildTestCase{
  173. Description: "cc_object with product variable",
  174. Blueprint: `cc_object {
  175. name: "foo",
  176. system_shared_libs: [],
  177. include_build_directory: false,
  178. product_variables: {
  179. platform_sdk_version: {
  180. asflags: ["-DPLATFORM_SDK_VERSION=%d"],
  181. },
  182. },
  183. srcs: ["src.S"],
  184. }
  185. `,
  186. ExpectedBazelTargets: []string{
  187. MakeBazelTarget("cc_object", "foo", AttrNameToString{
  188. "asflags": `select({
  189. "//build/bazel/product_variables:platform_sdk_version": ["-DPLATFORM_SDK_VERSION=$(Platform_sdk_version)"],
  190. "//conditions:default": [],
  191. })`,
  192. "copts": `["-fno-addrsig"]`,
  193. "srcs_as": `["src.S"]`,
  194. "system_dynamic_deps": `[]`,
  195. }),
  196. },
  197. })
  198. }
  199. func TestCcObjectCflagsOneArch(t *testing.T) {
  200. runCcObjectTestCase(t, Bp2buildTestCase{
  201. Description: "cc_object setting cflags for one arch",
  202. Blueprint: `cc_object {
  203. name: "foo",
  204. system_shared_libs: [],
  205. srcs: ["a.cpp"],
  206. arch: {
  207. x86: {
  208. cflags: ["-fPIC"], // string list
  209. },
  210. arm: {
  211. srcs: ["arch/arm/file.cpp"], // label list
  212. },
  213. },
  214. include_build_directory: false,
  215. }
  216. `,
  217. ExpectedBazelTargets: []string{
  218. MakeBazelTarget("cc_object", "foo", AttrNameToString{
  219. "copts": `["-fno-addrsig"] + select({
  220. "//build/bazel/platforms/arch:x86": ["-fPIC"],
  221. "//conditions:default": [],
  222. })`,
  223. "srcs": `["a.cpp"] + select({
  224. "//build/bazel/platforms/arch:arm": ["arch/arm/file.cpp"],
  225. "//conditions:default": [],
  226. })`,
  227. "system_dynamic_deps": `[]`,
  228. }),
  229. },
  230. })
  231. }
  232. func TestCcObjectCflagsFourArch(t *testing.T) {
  233. runCcObjectTestCase(t, Bp2buildTestCase{
  234. Description: "cc_object setting cflags for 4 architectures",
  235. Blueprint: `cc_object {
  236. name: "foo",
  237. system_shared_libs: [],
  238. srcs: ["base.cpp"],
  239. arch: {
  240. x86: {
  241. srcs: ["x86.cpp"],
  242. cflags: ["-fPIC"],
  243. },
  244. x86_64: {
  245. srcs: ["x86_64.cpp"],
  246. cflags: ["-fPIC"],
  247. },
  248. arm: {
  249. srcs: ["arm.cpp"],
  250. cflags: ["-Wall"],
  251. },
  252. arm64: {
  253. srcs: ["arm64.cpp"],
  254. cflags: ["-Wall"],
  255. },
  256. },
  257. include_build_directory: false,
  258. }
  259. `,
  260. ExpectedBazelTargets: []string{
  261. MakeBazelTarget("cc_object", "foo", AttrNameToString{
  262. "copts": `["-fno-addrsig"] + select({
  263. "//build/bazel/platforms/arch:arm": ["-Wall"],
  264. "//build/bazel/platforms/arch:arm64": ["-Wall"],
  265. "//build/bazel/platforms/arch:x86": ["-fPIC"],
  266. "//build/bazel/platforms/arch:x86_64": ["-fPIC"],
  267. "//conditions:default": [],
  268. })`,
  269. "srcs": `["base.cpp"] + select({
  270. "//build/bazel/platforms/arch:arm": ["arm.cpp"],
  271. "//build/bazel/platforms/arch:arm64": ["arm64.cpp"],
  272. "//build/bazel/platforms/arch:x86": ["x86.cpp"],
  273. "//build/bazel/platforms/arch:x86_64": ["x86_64.cpp"],
  274. "//conditions:default": [],
  275. })`,
  276. "system_dynamic_deps": `[]`,
  277. }),
  278. },
  279. })
  280. }
  281. func TestCcObjectLinkerScript(t *testing.T) {
  282. runCcObjectTestCase(t, Bp2buildTestCase{
  283. Description: "cc_object setting linker_script",
  284. Blueprint: `cc_object {
  285. name: "foo",
  286. srcs: ["base.cpp"],
  287. linker_script: "bunny.lds",
  288. include_build_directory: false,
  289. }
  290. `,
  291. ExpectedBazelTargets: []string{
  292. MakeBazelTarget("cc_object", "foo", AttrNameToString{
  293. "copts": `["-fno-addrsig"]`,
  294. "linker_script": `"bunny.lds"`,
  295. "srcs": `["base.cpp"]`,
  296. }),
  297. },
  298. })
  299. }
  300. func TestCcObjectDepsAndLinkerScriptSelects(t *testing.T) {
  301. runCcObjectTestCase(t, Bp2buildTestCase{
  302. Description: "cc_object setting deps and linker_script across archs",
  303. Blueprint: `cc_object {
  304. name: "foo",
  305. srcs: ["base.cpp"],
  306. arch: {
  307. x86: {
  308. objs: ["x86_obj"],
  309. linker_script: "x86.lds",
  310. },
  311. x86_64: {
  312. objs: ["x86_64_obj"],
  313. linker_script: "x86_64.lds",
  314. },
  315. arm: {
  316. objs: ["arm_obj"],
  317. linker_script: "arm.lds",
  318. },
  319. },
  320. include_build_directory: false,
  321. }
  322. cc_object {
  323. name: "x86_obj",
  324. system_shared_libs: [],
  325. srcs: ["x86.cpp"],
  326. include_build_directory: false,
  327. bazel_module: { bp2build_available: false },
  328. }
  329. cc_object {
  330. name: "x86_64_obj",
  331. system_shared_libs: [],
  332. srcs: ["x86_64.cpp"],
  333. include_build_directory: false,
  334. bazel_module: { bp2build_available: false },
  335. }
  336. cc_object {
  337. name: "arm_obj",
  338. system_shared_libs: [],
  339. srcs: ["arm.cpp"],
  340. include_build_directory: false,
  341. bazel_module: { bp2build_available: false },
  342. }
  343. `,
  344. ExpectedBazelTargets: []string{
  345. MakeBazelTarget("cc_object", "foo", AttrNameToString{
  346. "copts": `["-fno-addrsig"]`,
  347. "objs": `select({
  348. "//build/bazel/platforms/arch:arm": [":arm_obj"],
  349. "//build/bazel/platforms/arch:x86": [":x86_obj"],
  350. "//build/bazel/platforms/arch:x86_64": [":x86_64_obj"],
  351. "//conditions:default": [],
  352. })`,
  353. "linker_script": `select({
  354. "//build/bazel/platforms/arch:arm": "arm.lds",
  355. "//build/bazel/platforms/arch:x86": "x86.lds",
  356. "//build/bazel/platforms/arch:x86_64": "x86_64.lds",
  357. "//conditions:default": None,
  358. })`,
  359. "srcs": `["base.cpp"]`,
  360. }),
  361. },
  362. })
  363. }
  364. func TestCcObjectSelectOnLinuxAndBionicArchs(t *testing.T) {
  365. runCcObjectTestCase(t, Bp2buildTestCase{
  366. Description: "cc_object setting srcs based on linux and bionic archs",
  367. Blueprint: `cc_object {
  368. name: "foo",
  369. srcs: ["base.cpp"],
  370. target: {
  371. linux_arm64: {
  372. srcs: ["linux_arm64.cpp",]
  373. },
  374. linux_x86: {
  375. srcs: ["linux_x86.cpp",]
  376. },
  377. bionic_arm64: {
  378. srcs: ["bionic_arm64.cpp",]
  379. },
  380. },
  381. include_build_directory: false,
  382. }
  383. `,
  384. ExpectedBazelTargets: []string{
  385. MakeBazelTarget("cc_object", "foo", AttrNameToString{
  386. "copts": `["-fno-addrsig"]`,
  387. "srcs": `["base.cpp"] + select({
  388. "//build/bazel/platforms/os_arch:android_arm64": [
  389. "linux_arm64.cpp",
  390. "bionic_arm64.cpp",
  391. ],
  392. "//build/bazel/platforms/os_arch:android_x86": ["linux_x86.cpp"],
  393. "//build/bazel/platforms/os_arch:linux_bionic_arm64": [
  394. "linux_arm64.cpp",
  395. "bionic_arm64.cpp",
  396. ],
  397. "//build/bazel/platforms/os_arch:linux_glibc_x86": ["linux_x86.cpp"],
  398. "//build/bazel/platforms/os_arch:linux_musl_arm64": ["linux_arm64.cpp"],
  399. "//build/bazel/platforms/os_arch:linux_musl_x86": ["linux_x86.cpp"],
  400. "//conditions:default": [],
  401. })`,
  402. }),
  403. },
  404. })
  405. }
  406. func TestCcObjectHeaderLib(t *testing.T) {
  407. runCcObjectTestCase(t, Bp2buildTestCase{
  408. Description: "simple cc_object generates cc_object with include header dep",
  409. Filesystem: map[string]string{
  410. "a/b/foo.h": "",
  411. "a/b/bar.h": "",
  412. "a/b/exclude.c": "",
  413. "a/b/c.c": "",
  414. },
  415. Blueprint: `cc_object {
  416. name: "foo",
  417. header_libs: ["libheaders"],
  418. system_shared_libs: [],
  419. cflags: [
  420. "-Wno-gcc-compat",
  421. "-Wall",
  422. "-Werror",
  423. ],
  424. srcs: [
  425. "a/b/*.c"
  426. ],
  427. exclude_srcs: ["a/b/exclude.c"],
  428. sdk_version: "current",
  429. min_sdk_version: "29",
  430. }
  431. cc_library_headers {
  432. name: "libheaders",
  433. export_include_dirs: ["include"],
  434. }
  435. `,
  436. ExpectedBazelTargets: []string{
  437. MakeBazelTarget("cc_object", "foo", AttrNameToString{
  438. "copts": `[
  439. "-fno-addrsig",
  440. "-Wno-gcc-compat",
  441. "-Wall",
  442. "-Werror",
  443. ]`,
  444. "deps": `[":libheaders"]`,
  445. "local_includes": `["."]`,
  446. "srcs": `["a/b/c.c"]`,
  447. "system_dynamic_deps": `[]`,
  448. "sdk_version": `"current"`,
  449. "min_sdk_version": `"29"`,
  450. }),
  451. MakeBazelTarget("cc_library_headers", "libheaders", AttrNameToString{
  452. "export_includes": `["include"]`,
  453. }),
  454. },
  455. })
  456. }