java_test.go 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486
  1. // Copyright 2017 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 java
  15. import (
  16. "fmt"
  17. "os"
  18. "path/filepath"
  19. "reflect"
  20. "runtime"
  21. "strconv"
  22. "strings"
  23. "testing"
  24. "github.com/google/blueprint/proptools"
  25. "android/soong/android"
  26. "android/soong/cc"
  27. "android/soong/dexpreopt"
  28. "android/soong/genrule"
  29. "android/soong/python"
  30. )
  31. // Legacy preparer used for running tests within the java package.
  32. //
  33. // This includes everything that was needed to run any test in the java package prior to the
  34. // introduction of the test fixtures. Tests that are being converted to use fixtures directly
  35. // rather than through the testJava...() methods should avoid using this and instead use the
  36. // various preparers directly, using android.GroupFixturePreparers(...) to group them when
  37. // necessary.
  38. //
  39. // deprecated
  40. var prepareForJavaTest = android.GroupFixturePreparers(
  41. genrule.PrepareForTestWithGenRuleBuildComponents,
  42. // Get the CC build components but not default modules.
  43. cc.PrepareForTestWithCcBuildComponents,
  44. // Include all the default java modules.
  45. PrepareForTestWithJavaDefaultModules,
  46. PrepareForTestWithOverlayBuildComponents,
  47. python.PrepareForTestWithPythonBuildComponents,
  48. android.FixtureRegisterWithContext(func(ctx android.RegistrationContext) {
  49. ctx.RegisterPreSingletonType("sdk_versions", sdkPreSingletonFactory)
  50. }),
  51. PrepareForTestWithDexpreopt,
  52. )
  53. func TestMain(m *testing.M) {
  54. os.Exit(m.Run())
  55. }
  56. // testJavaError is a legacy way of running tests of java modules that expect errors.
  57. //
  58. // See testJava for an explanation as to how to stop using this deprecated method.
  59. //
  60. // deprecated
  61. func testJavaError(t *testing.T, pattern string, bp string) (*android.TestContext, android.Config) {
  62. t.Helper()
  63. result := android.GroupFixturePreparers(
  64. prepareForJavaTest, dexpreopt.PrepareForTestByEnablingDexpreopt).
  65. ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(pattern)).
  66. RunTestWithBp(t, bp)
  67. return result.TestContext, result.Config
  68. }
  69. // testJavaWithFS runs tests using the prepareForJavaTest
  70. //
  71. // See testJava for an explanation as to how to stop using this deprecated method.
  72. //
  73. // deprecated
  74. func testJavaWithFS(t *testing.T, bp string, fs android.MockFS) (*android.TestContext, android.Config) {
  75. t.Helper()
  76. result := android.GroupFixturePreparers(
  77. prepareForJavaTest, fs.AddToFixture()).RunTestWithBp(t, bp)
  78. return result.TestContext, result.Config
  79. }
  80. // testJava runs tests using the prepareForJavaTest
  81. //
  82. // Do not add any new usages of this, instead use the prepareForJavaTest directly as it makes it
  83. // much easier to customize the test behavior.
  84. //
  85. // If it is necessary to customize the behavior of an existing test that uses this then please first
  86. // convert the test to using prepareForJavaTest first and then in a following change add the
  87. // appropriate fixture preparers. Keeping the conversion change separate makes it easy to verify
  88. // that it did not change the test behavior unexpectedly.
  89. //
  90. // deprecated
  91. func testJava(t *testing.T, bp string) (*android.TestContext, android.Config) {
  92. t.Helper()
  93. result := prepareForJavaTest.RunTestWithBp(t, bp)
  94. return result.TestContext, result.Config
  95. }
  96. // defaultModuleToPath constructs a path to the turbine generate jar for a default test module that
  97. // is defined in PrepareForIntegrationTestWithJava
  98. func defaultModuleToPath(name string) string {
  99. switch {
  100. case name == `""`:
  101. return name
  102. case strings.HasSuffix(name, ".jar"):
  103. return name
  104. default:
  105. return filepath.Join("out", "soong", ".intermediates", defaultJavaDir, name, "android_common", "turbine-combined", name+".jar")
  106. }
  107. }
  108. // Test that the PrepareForTestWithJavaDefaultModules provides all the files that it uses by
  109. // running it in a fixture that requires all source files to exist.
  110. func TestPrepareForTestWithJavaDefaultModules(t *testing.T) {
  111. android.GroupFixturePreparers(
  112. PrepareForTestWithJavaDefaultModules,
  113. android.PrepareForTestDisallowNonExistentPaths,
  114. ).RunTest(t)
  115. }
  116. func TestJavaLinkType(t *testing.T) {
  117. testJava(t, `
  118. java_library {
  119. name: "foo",
  120. srcs: ["a.java"],
  121. libs: ["bar"],
  122. static_libs: ["baz"],
  123. }
  124. java_library {
  125. name: "bar",
  126. sdk_version: "current",
  127. srcs: ["b.java"],
  128. }
  129. java_library {
  130. name: "baz",
  131. sdk_version: "system_current",
  132. srcs: ["c.java"],
  133. }
  134. `)
  135. testJavaError(t, "consider adjusting sdk_version: OR platform_apis:", `
  136. java_library {
  137. name: "foo",
  138. srcs: ["a.java"],
  139. libs: ["bar"],
  140. sdk_version: "current",
  141. static_libs: ["baz"],
  142. }
  143. java_library {
  144. name: "bar",
  145. sdk_version: "current",
  146. srcs: ["b.java"],
  147. }
  148. java_library {
  149. name: "baz",
  150. sdk_version: "system_current",
  151. srcs: ["c.java"],
  152. }
  153. `)
  154. testJava(t, `
  155. java_library {
  156. name: "foo",
  157. srcs: ["a.java"],
  158. libs: ["bar"],
  159. sdk_version: "system_current",
  160. static_libs: ["baz"],
  161. }
  162. java_library {
  163. name: "bar",
  164. sdk_version: "current",
  165. srcs: ["b.java"],
  166. }
  167. java_library {
  168. name: "baz",
  169. sdk_version: "system_current",
  170. srcs: ["c.java"],
  171. }
  172. `)
  173. testJavaError(t, "consider adjusting sdk_version: OR platform_apis:", `
  174. java_library {
  175. name: "foo",
  176. srcs: ["a.java"],
  177. libs: ["bar"],
  178. sdk_version: "system_current",
  179. static_libs: ["baz"],
  180. }
  181. java_library {
  182. name: "bar",
  183. sdk_version: "current",
  184. srcs: ["b.java"],
  185. }
  186. java_library {
  187. name: "baz",
  188. srcs: ["c.java"],
  189. }
  190. `)
  191. }
  192. func TestSimple(t *testing.T) {
  193. ctx, _ := testJava(t, `
  194. java_library {
  195. name: "foo",
  196. srcs: ["a.java"],
  197. libs: ["bar"],
  198. static_libs: ["baz"],
  199. }
  200. java_library {
  201. name: "bar",
  202. srcs: ["b.java"],
  203. }
  204. java_library {
  205. name: "baz",
  206. srcs: ["c.java"],
  207. }
  208. `)
  209. javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
  210. combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
  211. if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
  212. t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
  213. }
  214. baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
  215. barTurbine := filepath.Join("out", "soong", ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
  216. bazTurbine := filepath.Join("out", "soong", ".intermediates", "baz", "android_common", "turbine-combined", "baz.jar")
  217. android.AssertStringDoesContain(t, "foo classpath", javac.Args["classpath"], barTurbine)
  218. android.AssertStringDoesContain(t, "foo classpath", javac.Args["classpath"], bazTurbine)
  219. if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
  220. t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
  221. }
  222. }
  223. func TestExportedPlugins(t *testing.T) {
  224. type Result struct {
  225. library string
  226. processors string
  227. disableTurbine bool
  228. }
  229. var tests = []struct {
  230. name string
  231. extra string
  232. results []Result
  233. }{
  234. {
  235. name: "Exported plugin is not a direct plugin",
  236. extra: `java_library { name: "exports", srcs: ["a.java"], exported_plugins: ["plugin"] }`,
  237. results: []Result{{library: "exports", processors: "-proc:none"}},
  238. },
  239. {
  240. name: "Exports plugin to dependee",
  241. extra: `
  242. java_library{name: "exports", exported_plugins: ["plugin"]}
  243. java_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
  244. java_library{name: "bar", srcs: ["a.java"], static_libs: ["exports"]}
  245. `,
  246. results: []Result{
  247. {library: "foo", processors: "-processor com.android.TestPlugin"},
  248. {library: "bar", processors: "-processor com.android.TestPlugin"},
  249. },
  250. },
  251. {
  252. name: "Exports plugin to android_library",
  253. extra: `
  254. java_library{name: "exports", exported_plugins: ["plugin"]}
  255. android_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
  256. android_library{name: "bar", srcs: ["a.java"], static_libs: ["exports"]}
  257. `,
  258. results: []Result{
  259. {library: "foo", processors: "-processor com.android.TestPlugin"},
  260. {library: "bar", processors: "-processor com.android.TestPlugin"},
  261. },
  262. },
  263. {
  264. name: "Exports plugin is not propagated via transitive deps",
  265. extra: `
  266. java_library{name: "exports", exported_plugins: ["plugin"]}
  267. java_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
  268. java_library{name: "bar", srcs: ["a.java"], static_libs: ["foo"]}
  269. `,
  270. results: []Result{
  271. {library: "foo", processors: "-processor com.android.TestPlugin"},
  272. {library: "bar", processors: "-proc:none"},
  273. },
  274. },
  275. {
  276. name: "Exports plugin appends to plugins",
  277. extra: `
  278. java_plugin{name: "plugin2", processor_class: "com.android.TestPlugin2"}
  279. java_library{name: "exports", exported_plugins: ["plugin"]}
  280. java_library{name: "foo", srcs: ["a.java"], libs: ["exports"], plugins: ["plugin2"]}
  281. `,
  282. results: []Result{
  283. {library: "foo", processors: "-processor com.android.TestPlugin,com.android.TestPlugin2"},
  284. },
  285. },
  286. {
  287. name: "Exports plugin to with generates_api to dependee",
  288. extra: `
  289. java_library{name: "exports", exported_plugins: ["plugin_generates_api"]}
  290. java_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
  291. java_library{name: "bar", srcs: ["a.java"], static_libs: ["exports"]}
  292. `,
  293. results: []Result{
  294. {library: "foo", processors: "-processor com.android.TestPlugin", disableTurbine: true},
  295. {library: "bar", processors: "-processor com.android.TestPlugin", disableTurbine: true},
  296. },
  297. },
  298. }
  299. for _, test := range tests {
  300. t.Run(test.name, func(t *testing.T) {
  301. ctx, _ := testJava(t, `
  302. java_plugin {
  303. name: "plugin",
  304. processor_class: "com.android.TestPlugin",
  305. }
  306. java_plugin {
  307. name: "plugin_generates_api",
  308. generates_api: true,
  309. processor_class: "com.android.TestPlugin",
  310. }
  311. `+test.extra)
  312. for _, want := range test.results {
  313. javac := ctx.ModuleForTests(want.library, "android_common").Rule("javac")
  314. if javac.Args["processor"] != want.processors {
  315. t.Errorf("For library %v, expected %v, found %v", want.library, want.processors, javac.Args["processor"])
  316. }
  317. turbine := ctx.ModuleForTests(want.library, "android_common").MaybeRule("turbine")
  318. disableTurbine := turbine.BuildParams.Rule == nil
  319. if disableTurbine != want.disableTurbine {
  320. t.Errorf("For library %v, expected disableTurbine %v, found %v", want.library, want.disableTurbine, disableTurbine)
  321. }
  322. }
  323. })
  324. }
  325. }
  326. func TestSdkVersionByPartition(t *testing.T) {
  327. testJavaError(t, "sdk_version must have a value when the module is located at vendor or product", `
  328. java_library {
  329. name: "foo",
  330. srcs: ["a.java"],
  331. vendor: true,
  332. }
  333. `)
  334. testJava(t, `
  335. java_library {
  336. name: "bar",
  337. srcs: ["b.java"],
  338. }
  339. `)
  340. for _, enforce := range []bool{true, false} {
  341. bp := `
  342. java_library {
  343. name: "foo",
  344. srcs: ["a.java"],
  345. product_specific: true,
  346. }
  347. `
  348. errorHandler := android.FixtureExpectsNoErrors
  349. if enforce {
  350. errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern("sdk_version must have a value when the module is located at vendor or product")
  351. }
  352. android.GroupFixturePreparers(
  353. PrepareForTestWithJavaDefaultModules,
  354. android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
  355. variables.EnforceProductPartitionInterface = proptools.BoolPtr(enforce)
  356. }),
  357. ).
  358. ExtendWithErrorHandler(errorHandler).
  359. RunTestWithBp(t, bp)
  360. }
  361. }
  362. func TestArchSpecific(t *testing.T) {
  363. ctx, _ := testJava(t, `
  364. java_library {
  365. name: "foo",
  366. srcs: ["a.java"],
  367. target: {
  368. android: {
  369. srcs: ["b.java"],
  370. },
  371. },
  372. }
  373. `)
  374. javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
  375. if len(javac.Inputs) != 2 || javac.Inputs[0].String() != "a.java" || javac.Inputs[1].String() != "b.java" {
  376. t.Errorf(`foo inputs %v != ["a.java", "b.java"]`, javac.Inputs)
  377. }
  378. }
  379. func TestBinary(t *testing.T) {
  380. ctx, _ := testJava(t, `
  381. java_library_host {
  382. name: "foo",
  383. srcs: ["a.java"],
  384. }
  385. java_binary_host {
  386. name: "bar",
  387. srcs: ["b.java"],
  388. static_libs: ["foo"],
  389. jni_libs: ["libjni"],
  390. }
  391. cc_library_shared {
  392. name: "libjni",
  393. host_supported: true,
  394. device_supported: false,
  395. stl: "none",
  396. }
  397. `)
  398. buildOS := ctx.Config().BuildOS.String()
  399. bar := ctx.ModuleForTests("bar", buildOS+"_common")
  400. barJar := bar.Output("bar.jar").Output.String()
  401. barWrapper := ctx.ModuleForTests("bar", buildOS+"_x86_64")
  402. barWrapperDeps := barWrapper.Output("bar").Implicits.Strings()
  403. libjni := ctx.ModuleForTests("libjni", buildOS+"_x86_64_shared")
  404. libjniSO := libjni.Rule("Cp").Output.String()
  405. // Test that the install binary wrapper depends on the installed jar file
  406. if g, w := barWrapperDeps, barJar; !android.InList(w, g) {
  407. t.Errorf("expected binary wrapper implicits to contain %q, got %q", w, g)
  408. }
  409. // Test that the install binary wrapper depends on the installed JNI libraries
  410. if g, w := barWrapperDeps, libjniSO; !android.InList(w, g) {
  411. t.Errorf("expected binary wrapper implicits to contain %q, got %q", w, g)
  412. }
  413. }
  414. func TestTest(t *testing.T) {
  415. ctx, _ := testJava(t, `
  416. java_test_host {
  417. name: "foo",
  418. srcs: ["a.java"],
  419. jni_libs: ["libjni"],
  420. }
  421. cc_library_shared {
  422. name: "libjni",
  423. host_supported: true,
  424. device_supported: false,
  425. stl: "none",
  426. }
  427. `)
  428. buildOS := ctx.Config().BuildOS.String()
  429. foo := ctx.ModuleForTests("foo", buildOS+"_common").Module().(*TestHost)
  430. expected := "lib64/libjni.so"
  431. if runtime.GOOS == "darwin" {
  432. expected = "lib64/libjni.dylib"
  433. }
  434. fooTestData := foo.data
  435. if len(fooTestData) != 1 || fooTestData[0].Rel() != expected {
  436. t.Errorf(`expected foo test data relative path [%q], got %q`,
  437. expected, fooTestData.Strings())
  438. }
  439. }
  440. func TestHostBinaryNoJavaDebugInfoOverride(t *testing.T) {
  441. bp := `
  442. java_library {
  443. name: "target_library",
  444. srcs: ["a.java"],
  445. }
  446. java_binary_host {
  447. name: "host_binary",
  448. srcs: ["b.java"],
  449. }
  450. `
  451. result := android.GroupFixturePreparers(
  452. PrepareForTestWithJavaDefaultModules,
  453. android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
  454. variables.MinimizeJavaDebugInfo = proptools.BoolPtr(true)
  455. }),
  456. ).RunTestWithBp(t, bp)
  457. // first, check that the -g flag is added to target modules
  458. targetLibrary := result.ModuleForTests("target_library", "android_common")
  459. targetJavaFlags := targetLibrary.Module().VariablesForTests()["javacFlags"]
  460. if !strings.Contains(targetJavaFlags, "-g:source,lines") {
  461. t.Errorf("target library javac flags %v should contain "+
  462. "-g:source,lines override with MinimizeJavaDebugInfo", targetJavaFlags)
  463. }
  464. // check that -g is not overridden for host modules
  465. buildOS := result.Config.BuildOS.String()
  466. hostBinary := result.ModuleForTests("host_binary", buildOS+"_common")
  467. hostJavaFlags := hostBinary.Module().VariablesForTests()["javacFlags"]
  468. if strings.Contains(hostJavaFlags, "-g:source,lines") {
  469. t.Errorf("java_binary_host javac flags %v should not have "+
  470. "-g:source,lines override with MinimizeJavaDebugInfo", hostJavaFlags)
  471. }
  472. }
  473. func TestPrebuilts(t *testing.T) {
  474. ctx, _ := testJava(t, `
  475. java_library {
  476. name: "foo",
  477. srcs: ["a.java", ":stubs-source"],
  478. libs: ["bar", "sdklib"],
  479. static_libs: ["baz"],
  480. }
  481. java_import {
  482. name: "bar",
  483. jars: ["a.jar"],
  484. }
  485. java_import {
  486. name: "baz",
  487. jars: ["b.jar"],
  488. sdk_version: "current",
  489. compile_dex: true,
  490. }
  491. dex_import {
  492. name: "qux",
  493. jars: ["b.jar"],
  494. }
  495. java_sdk_library_import {
  496. name: "sdklib",
  497. public: {
  498. jars: ["c.jar"],
  499. },
  500. }
  501. prebuilt_stubs_sources {
  502. name: "stubs-source",
  503. srcs: ["stubs/sources"],
  504. }
  505. java_test_import {
  506. name: "test",
  507. jars: ["a.jar"],
  508. test_suites: ["cts"],
  509. test_config: "AndroidTest.xml",
  510. }
  511. `)
  512. fooModule := ctx.ModuleForTests("foo", "android_common")
  513. javac := fooModule.Rule("javac")
  514. combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
  515. barModule := ctx.ModuleForTests("bar", "android_common")
  516. barJar := barModule.Rule("combineJar").Output
  517. bazModule := ctx.ModuleForTests("baz", "android_common")
  518. bazJar := bazModule.Rule("combineJar").Output
  519. sdklibStubsJar := ctx.ModuleForTests("sdklib.stubs", "android_common").Rule("combineJar").Output
  520. fooLibrary := fooModule.Module().(*Library)
  521. assertDeepEquals(t, "foo java sources incorrect",
  522. []string{"a.java"}, fooLibrary.compiledJavaSrcs.Strings())
  523. assertDeepEquals(t, "foo java source jars incorrect",
  524. []string{".intermediates/stubs-source/android_common/stubs-source-stubs.srcjar"},
  525. android.NormalizePathsForTesting(fooLibrary.compiledSrcJars))
  526. if !strings.Contains(javac.Args["classpath"], barJar.String()) {
  527. t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barJar.String())
  528. }
  529. barDexJar := barModule.Module().(*Import).DexJarBuildPath()
  530. if barDexJar != nil {
  531. t.Errorf("bar dex jar build path expected to be nil, got %q", barDexJar)
  532. }
  533. if !strings.Contains(javac.Args["classpath"], sdklibStubsJar.String()) {
  534. t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], sdklibStubsJar.String())
  535. }
  536. if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != bazJar.String() {
  537. t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, bazJar.String())
  538. }
  539. bazDexJar := bazModule.Module().(*Import).DexJarBuildPath()
  540. expectedDexJar := "out/soong/.intermediates/baz/android_common/dex/baz.jar"
  541. android.AssertPathRelativeToTopEquals(t, "baz dex jar build path", expectedDexJar, bazDexJar)
  542. ctx.ModuleForTests("qux", "android_common").Rule("Cp")
  543. }
  544. func assertDeepEquals(t *testing.T, message string, expected interface{}, actual interface{}) {
  545. if !reflect.DeepEqual(expected, actual) {
  546. t.Errorf("%s: expected %q, found %q", message, expected, actual)
  547. }
  548. }
  549. func TestPrebuiltStubsSources(t *testing.T) {
  550. test := func(t *testing.T, sourcesPath string, expectedInputs []string) {
  551. ctx, _ := testJavaWithFS(t, fmt.Sprintf(`
  552. prebuilt_stubs_sources {
  553. name: "stubs-source",
  554. srcs: ["%s"],
  555. }`, sourcesPath), map[string][]byte{
  556. "stubs/sources/pkg/A.java": nil,
  557. "stubs/sources/pkg/B.java": nil,
  558. })
  559. zipSrc := ctx.ModuleForTests("stubs-source", "android_common").Rule("zip_src")
  560. if expected, actual := expectedInputs, zipSrc.Inputs.Strings(); !reflect.DeepEqual(expected, actual) {
  561. t.Errorf("mismatch of inputs to soong_zip: expected %q, actual %q", expected, actual)
  562. }
  563. }
  564. t.Run("empty/missing directory", func(t *testing.T) {
  565. test(t, "empty-directory", nil)
  566. })
  567. t.Run("non-empty set of sources", func(t *testing.T) {
  568. test(t, "stubs/sources", []string{
  569. "stubs/sources/pkg/A.java",
  570. "stubs/sources/pkg/B.java",
  571. })
  572. })
  573. }
  574. func TestDefaults(t *testing.T) {
  575. ctx, _ := testJava(t, `
  576. java_defaults {
  577. name: "defaults",
  578. srcs: ["a.java"],
  579. libs: ["bar"],
  580. static_libs: ["baz"],
  581. optimize: {enabled: false},
  582. }
  583. java_library {
  584. name: "foo",
  585. defaults: ["defaults"],
  586. }
  587. java_library {
  588. name: "bar",
  589. srcs: ["b.java"],
  590. }
  591. java_library {
  592. name: "baz",
  593. srcs: ["c.java"],
  594. }
  595. android_test {
  596. name: "atestOptimize",
  597. defaults: ["defaults"],
  598. optimize: {enabled: true},
  599. }
  600. android_test {
  601. name: "atestNoOptimize",
  602. defaults: ["defaults"],
  603. }
  604. android_test {
  605. name: "atestDefault",
  606. srcs: ["a.java"],
  607. }
  608. `)
  609. javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
  610. combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
  611. if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
  612. t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
  613. }
  614. barTurbine := filepath.Join("out", "soong", ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
  615. if !strings.Contains(javac.Args["classpath"], barTurbine) {
  616. t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
  617. }
  618. baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
  619. if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
  620. t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
  621. }
  622. atestOptimize := ctx.ModuleForTests("atestOptimize", "android_common").MaybeRule("r8")
  623. if atestOptimize.Output == nil {
  624. t.Errorf("atestOptimize should optimize APK")
  625. }
  626. atestNoOptimize := ctx.ModuleForTests("atestNoOptimize", "android_common").MaybeRule("d8")
  627. if atestNoOptimize.Output == nil {
  628. t.Errorf("atestNoOptimize should not optimize APK")
  629. }
  630. atestDefault := ctx.ModuleForTests("atestDefault", "android_common").MaybeRule("r8")
  631. if atestDefault.Output == nil {
  632. t.Errorf("atestDefault should optimize APK")
  633. }
  634. }
  635. func TestResources(t *testing.T) {
  636. var table = []struct {
  637. name string
  638. prop string
  639. extra string
  640. args string
  641. }{
  642. {
  643. // Test that a module with java_resource_dirs includes the files
  644. name: "resource dirs",
  645. prop: `java_resource_dirs: ["java-res"]`,
  646. args: "-C java-res -f java-res/a/a -f java-res/b/b",
  647. },
  648. {
  649. // Test that a module with java_resources includes the files
  650. name: "resource files",
  651. prop: `java_resources: ["java-res/a/a", "java-res/b/b"]`,
  652. args: "-C . -f java-res/a/a -f java-res/b/b",
  653. },
  654. {
  655. // Test that a module with a filegroup in java_resources includes the files with the
  656. // path prefix
  657. name: "resource filegroup",
  658. prop: `java_resources: [":foo-res"]`,
  659. extra: `
  660. filegroup {
  661. name: "foo-res",
  662. path: "java-res",
  663. srcs: ["java-res/a/a", "java-res/b/b"],
  664. }`,
  665. args: "-C java-res -f java-res/a/a -f java-res/b/b",
  666. },
  667. {
  668. // Test that a module with wildcards in java_resource_dirs has the correct path prefixes
  669. name: "wildcard dirs",
  670. prop: `java_resource_dirs: ["java-res/*"]`,
  671. args: "-C java-res/a -f java-res/a/a -C java-res/b -f java-res/b/b",
  672. },
  673. {
  674. // Test that a module exclude_java_resource_dirs excludes the files
  675. name: "wildcard dirs",
  676. prop: `java_resource_dirs: ["java-res/*"], exclude_java_resource_dirs: ["java-res/b"]`,
  677. args: "-C java-res/a -f java-res/a/a",
  678. },
  679. {
  680. // Test wildcards in java_resources
  681. name: "wildcard files",
  682. prop: `java_resources: ["java-res/**/*"]`,
  683. args: "-C . -f java-res/a/a -f java-res/b/b",
  684. },
  685. {
  686. // Test exclude_java_resources with java_resources
  687. name: "wildcard files with exclude",
  688. prop: `java_resources: ["java-res/**/*"], exclude_java_resources: ["java-res/b/*"]`,
  689. args: "-C . -f java-res/a/a",
  690. },
  691. {
  692. // Test exclude_java_resources with java_resource_dirs
  693. name: "resource dirs with exclude files",
  694. prop: `java_resource_dirs: ["java-res"], exclude_java_resources: ["java-res/b/b"]`,
  695. args: "-C java-res -f java-res/a/a",
  696. },
  697. {
  698. // Test exclude_java_resource_dirs with java_resource_dirs
  699. name: "resource dirs with exclude files",
  700. prop: `java_resource_dirs: ["java-res", "java-res2"], exclude_java_resource_dirs: ["java-res2"]`,
  701. args: "-C java-res -f java-res/a/a -f java-res/b/b",
  702. },
  703. }
  704. for _, test := range table {
  705. t.Run(test.name, func(t *testing.T) {
  706. ctx, _ := testJavaWithFS(t, `
  707. java_library {
  708. name: "foo",
  709. srcs: [
  710. "a.java",
  711. "b.java",
  712. "c.java",
  713. ],
  714. `+test.prop+`,
  715. }
  716. `+test.extra,
  717. map[string][]byte{
  718. "java-res/a/a": nil,
  719. "java-res/b/b": nil,
  720. "java-res2/a": nil,
  721. },
  722. )
  723. foo := ctx.ModuleForTests("foo", "android_common").Output("withres/foo.jar")
  724. fooRes := ctx.ModuleForTests("foo", "android_common").Output("res/foo.jar")
  725. if !inList(fooRes.Output.String(), foo.Inputs.Strings()) {
  726. t.Errorf("foo combined jars %v does not contain %q",
  727. foo.Inputs.Strings(), fooRes.Output.String())
  728. }
  729. if fooRes.Args["jarArgs"] != test.args {
  730. t.Errorf("foo resource jar args %q is not %q",
  731. fooRes.Args["jarArgs"], test.args)
  732. }
  733. })
  734. }
  735. }
  736. func TestIncludeSrcs(t *testing.T) {
  737. ctx, _ := testJavaWithFS(t, `
  738. java_library {
  739. name: "foo",
  740. srcs: [
  741. "a.java",
  742. "b.java",
  743. "c.java",
  744. ],
  745. include_srcs: true,
  746. }
  747. java_library {
  748. name: "bar",
  749. srcs: [
  750. "a.java",
  751. "b.java",
  752. "c.java",
  753. ],
  754. java_resource_dirs: ["java-res"],
  755. include_srcs: true,
  756. }
  757. `, map[string][]byte{
  758. "java-res/a/a": nil,
  759. "java-res/b/b": nil,
  760. "java-res2/a": nil,
  761. })
  762. // Test a library with include_srcs: true
  763. foo := ctx.ModuleForTests("foo", "android_common").Output("withres/foo.jar")
  764. fooSrcJar := ctx.ModuleForTests("foo", "android_common").Output("foo.srcjar")
  765. if g, w := fooSrcJar.Output.String(), foo.Inputs.Strings(); !inList(g, w) {
  766. t.Errorf("foo combined jars %v does not contain %q", w, g)
  767. }
  768. if g, w := fooSrcJar.Args["jarArgs"], "-C . -f a.java -f b.java -f c.java"; g != w {
  769. t.Errorf("foo source jar args %q is not %q", w, g)
  770. }
  771. // Test a library with include_srcs: true and resources
  772. bar := ctx.ModuleForTests("bar", "android_common").Output("withres/bar.jar")
  773. barResCombined := ctx.ModuleForTests("bar", "android_common").Output("res-combined/bar.jar")
  774. barRes := ctx.ModuleForTests("bar", "android_common").Output("res/bar.jar")
  775. barSrcJar := ctx.ModuleForTests("bar", "android_common").Output("bar.srcjar")
  776. if g, w := barSrcJar.Output.String(), barResCombined.Inputs.Strings(); !inList(g, w) {
  777. t.Errorf("bar combined resource jars %v does not contain %q", w, g)
  778. }
  779. if g, w := barRes.Output.String(), barResCombined.Inputs.Strings(); !inList(g, w) {
  780. t.Errorf("bar combined resource jars %v does not contain %q", w, g)
  781. }
  782. if g, w := barResCombined.Output.String(), bar.Inputs.Strings(); !inList(g, w) {
  783. t.Errorf("bar combined jars %v does not contain %q", w, g)
  784. }
  785. if g, w := barSrcJar.Args["jarArgs"], "-C . -f a.java -f b.java -f c.java"; g != w {
  786. t.Errorf("bar source jar args %q is not %q", w, g)
  787. }
  788. if g, w := barRes.Args["jarArgs"], "-C java-res -f java-res/a/a -f java-res/b/b"; g != w {
  789. t.Errorf("bar resource jar args %q is not %q", w, g)
  790. }
  791. }
  792. func TestGeneratedSources(t *testing.T) {
  793. ctx, _ := testJavaWithFS(t, `
  794. java_library {
  795. name: "foo",
  796. srcs: [
  797. "a*.java",
  798. ":gen",
  799. "b*.java",
  800. ],
  801. }
  802. genrule {
  803. name: "gen",
  804. tool_files: ["java-res/a"],
  805. out: ["gen.java"],
  806. }
  807. `, map[string][]byte{
  808. "a.java": nil,
  809. "b.java": nil,
  810. })
  811. javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
  812. genrule := ctx.ModuleForTests("gen", "").Rule("generator")
  813. if filepath.Base(genrule.Output.String()) != "gen.java" {
  814. t.Fatalf(`gen output file %v is not ".../gen.java"`, genrule.Output.String())
  815. }
  816. if len(javac.Inputs) != 3 ||
  817. javac.Inputs[0].String() != "a.java" ||
  818. javac.Inputs[1].String() != genrule.Output.String() ||
  819. javac.Inputs[2].String() != "b.java" {
  820. t.Errorf(`foo inputs %v != ["a.java", ".../gen.java", "b.java"]`, javac.Inputs)
  821. }
  822. }
  823. func TestTurbine(t *testing.T) {
  824. result := android.GroupFixturePreparers(
  825. prepareForJavaTest, FixtureWithPrebuiltApis(map[string][]string{"14": {"foo"}})).
  826. RunTestWithBp(t, `
  827. java_library {
  828. name: "foo",
  829. srcs: ["a.java"],
  830. sdk_version: "14",
  831. }
  832. java_library {
  833. name: "bar",
  834. srcs: ["b.java"],
  835. static_libs: ["foo"],
  836. sdk_version: "14",
  837. }
  838. java_library {
  839. name: "baz",
  840. srcs: ["c.java"],
  841. libs: ["bar"],
  842. sdk_version: "14",
  843. }
  844. `)
  845. fooTurbine := result.ModuleForTests("foo", "android_common").Rule("turbine")
  846. barTurbine := result.ModuleForTests("bar", "android_common").Rule("turbine")
  847. barJavac := result.ModuleForTests("bar", "android_common").Rule("javac")
  848. barTurbineCombined := result.ModuleForTests("bar", "android_common").Description("for turbine")
  849. bazJavac := result.ModuleForTests("baz", "android_common").Rule("javac")
  850. android.AssertPathsRelativeToTopEquals(t, "foo inputs", []string{"a.java"}, fooTurbine.Inputs)
  851. fooHeaderJar := filepath.Join("out", "soong", ".intermediates", "foo", "android_common", "turbine-combined", "foo.jar")
  852. barTurbineJar := filepath.Join("out", "soong", ".intermediates", "bar", "android_common", "turbine", "bar.jar")
  853. android.AssertStringDoesContain(t, "bar turbine classpath", barTurbine.Args["classpath"], fooHeaderJar)
  854. android.AssertStringDoesContain(t, "bar javac classpath", barJavac.Args["classpath"], fooHeaderJar)
  855. android.AssertPathsRelativeToTopEquals(t, "bar turbine combineJar", []string{barTurbineJar, fooHeaderJar}, barTurbineCombined.Inputs)
  856. android.AssertStringDoesContain(t, "baz javac classpath", bazJavac.Args["classpath"], "prebuilts/sdk/14/public/android.jar")
  857. }
  858. func TestSharding(t *testing.T) {
  859. ctx, _ := testJava(t, `
  860. java_library {
  861. name: "bar",
  862. srcs: ["a.java","b.java","c.java"],
  863. javac_shard_size: 1
  864. }
  865. `)
  866. barHeaderJar := filepath.Join("out", "soong", ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
  867. for i := 0; i < 3; i++ {
  868. barJavac := ctx.ModuleForTests("bar", "android_common").Description("javac" + strconv.Itoa(i))
  869. if !strings.Contains(barJavac.Args["classpath"], barHeaderJar) {
  870. t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], barHeaderJar)
  871. }
  872. }
  873. }
  874. func TestJarGenrules(t *testing.T) {
  875. ctx, _ := testJava(t, `
  876. java_library {
  877. name: "foo",
  878. srcs: ["a.java"],
  879. }
  880. java_genrule {
  881. name: "jargen",
  882. tool_files: ["b.java"],
  883. cmd: "$(location b.java) $(in) $(out)",
  884. out: ["jargen.jar"],
  885. srcs: [":foo"],
  886. }
  887. java_library {
  888. name: "bar",
  889. static_libs: ["jargen"],
  890. srcs: ["c.java"],
  891. }
  892. java_library {
  893. name: "baz",
  894. libs: ["jargen"],
  895. srcs: ["c.java"],
  896. }
  897. `)
  898. foo := ctx.ModuleForTests("foo", "android_common").Output("javac/foo.jar")
  899. jargen := ctx.ModuleForTests("jargen", "android_common").Output("jargen.jar")
  900. bar := ctx.ModuleForTests("bar", "android_common").Output("javac/bar.jar")
  901. baz := ctx.ModuleForTests("baz", "android_common").Output("javac/baz.jar")
  902. barCombined := ctx.ModuleForTests("bar", "android_common").Output("combined/bar.jar")
  903. if g, w := jargen.Implicits.Strings(), foo.Output.String(); !android.InList(w, g) {
  904. t.Errorf("expected jargen inputs [%q], got %q", w, g)
  905. }
  906. if !strings.Contains(bar.Args["classpath"], jargen.Output.String()) {
  907. t.Errorf("bar classpath %v does not contain %q", bar.Args["classpath"], jargen.Output.String())
  908. }
  909. if !strings.Contains(baz.Args["classpath"], jargen.Output.String()) {
  910. t.Errorf("baz classpath %v does not contain %q", baz.Args["classpath"], jargen.Output.String())
  911. }
  912. if len(barCombined.Inputs) != 2 ||
  913. barCombined.Inputs[0].String() != bar.Output.String() ||
  914. barCombined.Inputs[1].String() != jargen.Output.String() {
  915. t.Errorf("bar combined jar inputs %v is not [%q, %q]",
  916. barCombined.Inputs.Strings(), bar.Output.String(), jargen.Output.String())
  917. }
  918. }
  919. func TestExcludeFileGroupInSrcs(t *testing.T) {
  920. ctx, _ := testJava(t, `
  921. java_library {
  922. name: "foo",
  923. srcs: ["a.java", ":foo-srcs"],
  924. exclude_srcs: ["a.java", ":foo-excludes"],
  925. }
  926. filegroup {
  927. name: "foo-srcs",
  928. srcs: ["java-fg/a.java", "java-fg/b.java", "java-fg/c.java"],
  929. }
  930. filegroup {
  931. name: "foo-excludes",
  932. srcs: ["java-fg/a.java", "java-fg/b.java"],
  933. }
  934. `)
  935. javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
  936. if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "java-fg/c.java" {
  937. t.Errorf(`foo inputs %v != ["java-fg/c.java"]`, javac.Inputs)
  938. }
  939. }
  940. func TestJavaLibrary(t *testing.T) {
  941. testJavaWithFS(t, "", map[string][]byte{
  942. "libcore/Android.bp": []byte(`
  943. java_library {
  944. name: "core",
  945. sdk_version: "none",
  946. system_modules: "none",
  947. }
  948. filegroup {
  949. name: "core-jar",
  950. srcs: [":core{.jar}"],
  951. }
  952. `),
  953. })
  954. }
  955. func TestJavaImport(t *testing.T) {
  956. testJavaWithFS(t, "", map[string][]byte{
  957. "libcore/Android.bp": []byte(`
  958. java_import {
  959. name: "core",
  960. sdk_version: "none",
  961. }
  962. filegroup {
  963. name: "core-jar",
  964. srcs: [":core{.jar}"],
  965. }
  966. `),
  967. })
  968. }
  969. var compilerFlagsTestCases = []struct {
  970. in string
  971. out bool
  972. }{
  973. {
  974. in: "a",
  975. out: false,
  976. },
  977. {
  978. in: "-a",
  979. out: true,
  980. },
  981. {
  982. in: "-no-jdk",
  983. out: false,
  984. },
  985. {
  986. in: "-no-stdlib",
  987. out: false,
  988. },
  989. {
  990. in: "-kotlin-home",
  991. out: false,
  992. },
  993. {
  994. in: "-kotlin-home /some/path",
  995. out: false,
  996. },
  997. {
  998. in: "-include-runtime",
  999. out: false,
  1000. },
  1001. {
  1002. in: "-Xintellij-plugin-root",
  1003. out: false,
  1004. },
  1005. }
  1006. type mockContext struct {
  1007. android.ModuleContext
  1008. result bool
  1009. }
  1010. func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) {
  1011. // CheckBadCompilerFlags calls this function when the flag should be rejected
  1012. ctx.result = false
  1013. }
  1014. func TestCompilerFlags(t *testing.T) {
  1015. for _, testCase := range compilerFlagsTestCases {
  1016. ctx := &mockContext{result: true}
  1017. CheckKotlincFlags(ctx, []string{testCase.in})
  1018. if ctx.result != testCase.out {
  1019. t.Errorf("incorrect output:")
  1020. t.Errorf(" input: %#v", testCase.in)
  1021. t.Errorf(" expected: %#v", testCase.out)
  1022. t.Errorf(" got: %#v", ctx.result)
  1023. }
  1024. }
  1025. }
  1026. // TODO(jungjw): Consider making this more robust by ignoring path order.
  1027. func checkPatchModuleFlag(t *testing.T, ctx *android.TestContext, moduleName string, expected string) {
  1028. variables := ctx.ModuleForTests(moduleName, "android_common").VariablesForTestsRelativeToTop()
  1029. flags := strings.Split(variables["javacFlags"], " ")
  1030. got := ""
  1031. for _, flag := range flags {
  1032. keyEnd := strings.Index(flag, "=")
  1033. if keyEnd > -1 && flag[:keyEnd] == "--patch-module" {
  1034. got = flag[keyEnd+1:]
  1035. break
  1036. }
  1037. }
  1038. if expected != android.StringPathRelativeToTop(ctx.Config().BuildDir(), got) {
  1039. t.Errorf("Unexpected patch-module flag for module %q - expected %q, but got %q", moduleName, expected, got)
  1040. }
  1041. }
  1042. func TestPatchModule(t *testing.T) {
  1043. t.Run("Java language level 8", func(t *testing.T) {
  1044. // Test with legacy javac -source 1.8 -target 1.8
  1045. bp := `
  1046. java_library {
  1047. name: "foo",
  1048. srcs: ["a.java"],
  1049. java_version: "1.8",
  1050. }
  1051. java_library {
  1052. name: "bar",
  1053. srcs: ["b.java"],
  1054. sdk_version: "none",
  1055. system_modules: "none",
  1056. patch_module: "java.base",
  1057. java_version: "1.8",
  1058. }
  1059. java_library {
  1060. name: "baz",
  1061. srcs: ["c.java"],
  1062. patch_module: "java.base",
  1063. java_version: "1.8",
  1064. }
  1065. `
  1066. ctx, _ := testJava(t, bp)
  1067. checkPatchModuleFlag(t, ctx, "foo", "")
  1068. checkPatchModuleFlag(t, ctx, "bar", "")
  1069. checkPatchModuleFlag(t, ctx, "baz", "")
  1070. })
  1071. t.Run("Java language level 9", func(t *testing.T) {
  1072. // Test with default javac -source 9 -target 9
  1073. bp := `
  1074. java_library {
  1075. name: "foo",
  1076. srcs: ["a.java"],
  1077. }
  1078. java_library {
  1079. name: "bar",
  1080. srcs: ["b.java"],
  1081. sdk_version: "none",
  1082. system_modules: "none",
  1083. patch_module: "java.base",
  1084. }
  1085. java_library {
  1086. name: "baz",
  1087. srcs: [
  1088. "c.java",
  1089. // Tests for b/150878007
  1090. "dir/d.java",
  1091. "dir2/e.java",
  1092. "dir2/f.java",
  1093. "nested/dir/g.java"
  1094. ],
  1095. patch_module: "java.base",
  1096. }
  1097. `
  1098. ctx, _ := testJava(t, bp)
  1099. checkPatchModuleFlag(t, ctx, "foo", "")
  1100. expected := "java.base=.:out/soong"
  1101. checkPatchModuleFlag(t, ctx, "bar", expected)
  1102. expected = "java.base=" + strings.Join([]string{
  1103. ".", "out/soong", "dir", "dir2", "nested", defaultModuleToPath("ext"), defaultModuleToPath("framework")}, ":")
  1104. checkPatchModuleFlag(t, ctx, "baz", expected)
  1105. })
  1106. }
  1107. func TestJavaLibraryWithSystemModules(t *testing.T) {
  1108. ctx, _ := testJava(t, `
  1109. java_library {
  1110. name: "lib-with-source-system-modules",
  1111. srcs: [
  1112. "a.java",
  1113. ],
  1114. sdk_version: "none",
  1115. system_modules: "source-system-modules",
  1116. }
  1117. java_library {
  1118. name: "source-jar",
  1119. srcs: [
  1120. "a.java",
  1121. ],
  1122. }
  1123. java_system_modules {
  1124. name: "source-system-modules",
  1125. libs: ["source-jar"],
  1126. }
  1127. java_library {
  1128. name: "lib-with-prebuilt-system-modules",
  1129. srcs: [
  1130. "a.java",
  1131. ],
  1132. sdk_version: "none",
  1133. system_modules: "prebuilt-system-modules",
  1134. }
  1135. java_import {
  1136. name: "prebuilt-jar",
  1137. jars: ["a.jar"],
  1138. }
  1139. java_system_modules_import {
  1140. name: "prebuilt-system-modules",
  1141. libs: ["prebuilt-jar"],
  1142. }
  1143. `)
  1144. checkBootClasspathForSystemModule(t, ctx, "lib-with-source-system-modules", "/source-jar.jar")
  1145. checkBootClasspathForSystemModule(t, ctx, "lib-with-prebuilt-system-modules", "/prebuilt-jar.jar")
  1146. }
  1147. func checkBootClasspathForSystemModule(t *testing.T, ctx *android.TestContext, moduleName string, expectedSuffix string) {
  1148. javacRule := ctx.ModuleForTests(moduleName, "android_common").Rule("javac")
  1149. bootClasspath := javacRule.Args["bootClasspath"]
  1150. if strings.HasPrefix(bootClasspath, "--system ") && strings.HasSuffix(bootClasspath, expectedSuffix) {
  1151. t.Errorf("bootclasspath of %q must start with --system and end with %q, but was %#v.", moduleName, expectedSuffix, bootClasspath)
  1152. }
  1153. }
  1154. func TestAidlExportIncludeDirsFromImports(t *testing.T) {
  1155. ctx, _ := testJava(t, `
  1156. java_library {
  1157. name: "foo",
  1158. srcs: ["aidl/foo/IFoo.aidl"],
  1159. libs: ["bar"],
  1160. }
  1161. java_import {
  1162. name: "bar",
  1163. jars: ["a.jar"],
  1164. aidl: {
  1165. export_include_dirs: ["aidl/bar"],
  1166. },
  1167. }
  1168. `)
  1169. aidlCommand := ctx.ModuleForTests("foo", "android_common").Rule("aidl").RuleParams.Command
  1170. expectedAidlFlag := "-Iaidl/bar"
  1171. if !strings.Contains(aidlCommand, expectedAidlFlag) {
  1172. t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
  1173. }
  1174. }
  1175. func TestAidlFlagsArePassedToTheAidlCompiler(t *testing.T) {
  1176. ctx, _ := testJava(t, `
  1177. java_library {
  1178. name: "foo",
  1179. srcs: ["aidl/foo/IFoo.aidl"],
  1180. aidl: { flags: ["-Werror"], },
  1181. }
  1182. `)
  1183. aidlCommand := ctx.ModuleForTests("foo", "android_common").Rule("aidl").RuleParams.Command
  1184. expectedAidlFlag := "-Werror"
  1185. if !strings.Contains(aidlCommand, expectedAidlFlag) {
  1186. t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
  1187. }
  1188. }
  1189. func TestDataNativeBinaries(t *testing.T) {
  1190. ctx, _ := testJava(t, `
  1191. java_test_host {
  1192. name: "foo",
  1193. srcs: ["a.java"],
  1194. data_native_bins: ["bin"]
  1195. }
  1196. python_binary_host {
  1197. name: "bin",
  1198. srcs: ["bin.py"],
  1199. }
  1200. `)
  1201. buildOS := ctx.Config().BuildOS.String()
  1202. test := ctx.ModuleForTests("foo", buildOS+"_common").Module().(*TestHost)
  1203. entries := android.AndroidMkEntriesForTest(t, ctx, test)[0]
  1204. expected := []string{"out/soong/.intermediates/bin/" + buildOS + "_x86_64_PY3/bin:bin"}
  1205. actual := entries.EntryMap["LOCAL_COMPATIBILITY_SUPPORT_FILES"]
  1206. android.AssertStringPathsRelativeToTopEquals(t, "LOCAL_COMPATIBILITY_SUPPORT_FILES", ctx.Config(), expected, actual)
  1207. }
  1208. func TestDefaultInstallable(t *testing.T) {
  1209. ctx, _ := testJava(t, `
  1210. java_test_host {
  1211. name: "foo"
  1212. }
  1213. `)
  1214. buildOS := ctx.Config().BuildOS.String()
  1215. module := ctx.ModuleForTests("foo", buildOS+"_common").Module().(*TestHost)
  1216. assertDeepEquals(t, "Default installable value should be true.", proptools.BoolPtr(true),
  1217. module.properties.Installable)
  1218. }
  1219. func TestErrorproneEnabled(t *testing.T) {
  1220. ctx, _ := testJava(t, `
  1221. java_library {
  1222. name: "foo",
  1223. srcs: ["a.java"],
  1224. errorprone: {
  1225. enabled: true,
  1226. },
  1227. }
  1228. `)
  1229. javac := ctx.ModuleForTests("foo", "android_common").Description("javac")
  1230. // Test that the errorprone plugins are passed to javac
  1231. expectedSubstring := "-Xplugin:ErrorProne"
  1232. if !strings.Contains(javac.Args["javacFlags"], expectedSubstring) {
  1233. t.Errorf("expected javacFlags to contain %q, got %q", expectedSubstring, javac.Args["javacFlags"])
  1234. }
  1235. // Modules with errorprone { enabled: true } will include errorprone checks
  1236. // in the main javac build rule. Only when RUN_ERROR_PRONE is true will
  1237. // the explicit errorprone build rule be created.
  1238. errorprone := ctx.ModuleForTests("foo", "android_common").MaybeDescription("errorprone")
  1239. if errorprone.RuleParams.Description != "" {
  1240. t.Errorf("expected errorprone build rule to not exist, but it did")
  1241. }
  1242. }
  1243. func TestErrorproneDisabled(t *testing.T) {
  1244. bp := `
  1245. java_library {
  1246. name: "foo",
  1247. srcs: ["a.java"],
  1248. errorprone: {
  1249. enabled: false,
  1250. },
  1251. }
  1252. `
  1253. ctx := android.GroupFixturePreparers(
  1254. PrepareForTestWithJavaDefaultModules,
  1255. android.FixtureMergeEnv(map[string]string{
  1256. "RUN_ERROR_PRONE": "true",
  1257. }),
  1258. ).RunTestWithBp(t, bp)
  1259. javac := ctx.ModuleForTests("foo", "android_common").Description("javac")
  1260. // Test that the errorprone plugins are not passed to javac, like they would
  1261. // be if enabled was true.
  1262. expectedSubstring := "-Xplugin:ErrorProne"
  1263. if strings.Contains(javac.Args["javacFlags"], expectedSubstring) {
  1264. t.Errorf("expected javacFlags to not contain %q, got %q", expectedSubstring, javac.Args["javacFlags"])
  1265. }
  1266. // Check that no errorprone build rule is created, like there would be
  1267. // if enabled was unset and RUN_ERROR_PRONE was true.
  1268. errorprone := ctx.ModuleForTests("foo", "android_common").MaybeDescription("errorprone")
  1269. if errorprone.RuleParams.Description != "" {
  1270. t.Errorf("expected errorprone build rule to not exist, but it did")
  1271. }
  1272. }
  1273. func TestErrorproneEnabledOnlyByEnvironmentVariable(t *testing.T) {
  1274. bp := `
  1275. java_library {
  1276. name: "foo",
  1277. srcs: ["a.java"],
  1278. }
  1279. `
  1280. ctx := android.GroupFixturePreparers(
  1281. PrepareForTestWithJavaDefaultModules,
  1282. android.FixtureMergeEnv(map[string]string{
  1283. "RUN_ERROR_PRONE": "true",
  1284. }),
  1285. ).RunTestWithBp(t, bp)
  1286. javac := ctx.ModuleForTests("foo", "android_common").Description("javac")
  1287. errorprone := ctx.ModuleForTests("foo", "android_common").Description("errorprone")
  1288. // Check that the errorprone plugins are not passed to javac, because they
  1289. // will instead be passed to the separate errorprone compilation
  1290. expectedSubstring := "-Xplugin:ErrorProne"
  1291. if strings.Contains(javac.Args["javacFlags"], expectedSubstring) {
  1292. t.Errorf("expected javacFlags to not contain %q, got %q", expectedSubstring, javac.Args["javacFlags"])
  1293. }
  1294. // Check that the errorprone plugin is enabled
  1295. if !strings.Contains(errorprone.Args["javacFlags"], expectedSubstring) {
  1296. t.Errorf("expected errorprone to contain %q, got %q", expectedSubstring, javac.Args["javacFlags"])
  1297. }
  1298. }